/* ===========================
   SUBTEXT — stylesheet
   Mobile-first, serif-forward
   =========================== */

/* ── Garamond fallback (garamontio) ── */
@font-face {
  font-family: 'Garamontio';
  src: url('fonts/garamontio.woff2') format('woff2'),
       url('fonts/garamontio.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Garamontio';
  src: url('fonts/garamontio_it.woff2') format('woff2'),
       url('fonts/garamontio_it.woff') format('woff');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

/* ── Garamontio Sans ── */
@font-face {
  font-family: 'GaramontioSans';
  src: url('fonts/garamontio-sans.woff2') format('woff2'),
       url('fonts/garamontio-sans.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'GaramontioSans';
  src: url('fonts/garamontio-sans-italic.woff2') format('woff2'),
       url('fonts/garamontio-sans-italic.woff') format('woff');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'GaramontioSans';
  src: url('fonts/garamontio-sans-bold.woff2') format('woff2'),
       url('fonts/garamontio-sans-bold.woff') format('woff');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'GaramontioSans';
  src: url('fonts/garamontio-sans-bold-italic.woff2') format('woff2'),
       url('fonts/garamontio-sans-bold-italic.woff') format('woff');
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

/* ── TeX Gyre Pagella (Palatino-compatible serif fallback) ── */
@font-face {
  font-family: 'TeXGyrePagella';
  src: url('fonts/texgyrepagella-regular.woff2') format('woff2'),
       url('fonts/texgyrepagella-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'TeXGyrePagella';
  src: url('fonts/texgyrepagella-italic.woff2') format('woff2'),
       url('fonts/texgyrepagella-italic.woff') format('woff');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'TeXGyrePagella';
  src: url('fonts/texgyrepagella-bold.woff2') format('woff2'),
       url('fonts/texgyrepagella-bold.woff') format('woff');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'TeXGyrePagella';
  src: url('fonts/texgyrepagella-bolditalic.woff2') format('woff2'),
       url('fonts/texgyrepagella-bolditalic.woff') format('woff');
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

/* ── Saira variable font (UI sans) ── */
@font-face {
  font-family: 'Saira';
  src: url('fonts/saira.woff2') format('woff2'),
       url('fonts/saira.woff') format('woff');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Saira';
  src: url('fonts/saira-italic.woff2') format('woff2'),
       url('fonts/saira-italic.woff') format('woff');
  font-weight: 100 900;
  font-style: italic;
  font-display: swap;
}

/* ── Text-field display fonts ── */
@font-face {
  font-family: 'CoralPixels';
  src: url('fonts/coral-pixels.woff2') format('woff2'),
       url('fonts/coral-pixels.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Handjet';
  src: url('fonts/handjet.woff2') format('woff2'),
       url('fonts/handjet.woff') format('woff');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'PressStart2P';
  src: url('fonts/press-start-2p.woff2') format('woff2'),
       url('fonts/press-start-2p.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Silkscreen';
  src: url('fonts/silkscreen.woff2') format('woff2'),
       url('fonts/silkscreen.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Silkscreen';
  src: url('fonts/silkscreen-bold.woff2') format('woff2'),
       url('fonts/silkscreen-bold.woff') format('woff');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

/* ── Nunito variable font ── */
@font-face {
  font-family: 'Nunito';
  src: url('fonts/nunito.woff2') format('woff2'),
       url('fonts/nunito.woff') format('woff');
  font-weight: 200 1000;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Nunito';
  src: url('fonts/nunito-italic.woff2') format('woff2'),
       url('fonts/nunito-italic.woff') format('woff');
  font-weight: 200 1000;
  font-style: italic;
  font-display: swap;
}

/* ── Added display fonts ── */
@font-face {
  font-family: 'Jabin';
  src: url('fonts/jabin-regular.woff2') format('woff2'),
       url('fonts/jabin-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Jabin';
  src: url('fonts/jabin-medium.woff2') format('woff2'),
       url('fonts/jabin-medium.woff') format('woff');
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Jabin';
  src: url('fonts/jabin-bold.woff2') format('woff2'),
       url('fonts/jabin-bold.woff') format('woff');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'ManufacturingConsent';
  src: url('fonts/manufacturing-consent-regular.woff2') format('woff2'),
       url('fonts/manufacturing-consent-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'VCROSDMono';
  src: url('fonts/vcr-osd-mono-regular.woff2') format('woff2'),
       url('fonts/vcr-osd-mono-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Canterbury';
  src: url('fonts/canterbury-regular.woff2') format('woff2'),
       url('fonts/canterbury-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'OldEnglishFive';
  src: url('fonts/old-english-five-regular.woff2') format('woff2'),
       url('fonts/old-english-five-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Amarante';
  src: url('fonts/amarante-regular.woff2') format('woff2'),
       url('fonts/amarante-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Limelight';
  src: url('fonts/limelight-regular.woff2') format('woff2'),
       url('fonts/limelight-regular.woff') format('woff');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg:        #f5f0e8;
  --border:    #d4c9b0;
  --ink:       #1a1410;
  --ink-muted: #7a6e5e;
  --accent:    #3b2e1e;
  --danger:    #b33a2e;
  --panel-h:   248px;
  --top-bar-h: 52px;
  --radius:    6px;
  --font-ui:   'Saira', system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;
  --font-serif: 'Garamontio', 'EB Garamond', Garamond, 'Times New Roman', serif;

  /* Centralized text font stacks for picker + render paths */
  --font-helvetica: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  --font-georgia: Georgia, 'Times New Roman', serif;
  --font-times: 'Times New Roman', Times, serif;
  --font-palatino: 'Palatino Linotype', 'Book Antiqua', Palatino, 'TeXGyrePagella', 'URW Palladio L', serif;
  --font-garamontio: 'Garamontio', 'EB Garamond', Garamond, 'Times New Roman', serif;
  --font-garamontio-sans: 'GaramontioSans', 'Saira', sans-serif;
  --font-baskerville: Baskerville, 'Libre Baskerville', serif;
  --font-futura: Futura, 'Century Gothic', sans-serif;
  --font-gill-sans: 'Gill Sans', 'Gill Sans MT', sans-serif;
  --font-impact: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
  --font-courier: 'Courier New', Courier, monospace;
  --font-coral-pixels: 'CoralPixels', sans-serif;
  --font-jabin: 'Jabin', serif;
  --font-manufacturing-consent: 'ManufacturingConsent', sans-serif;
  --font-vcr-osd-mono: 'VCROSDMono', monospace;
  --font-canterbury: 'Canterbury', serif;
  --font-old-english-five: 'OldEnglishFive', serif;
  --font-amarante: 'Amarante', serif;
  --font-limelight: 'Limelight', serif;
  --font-handjet: 'Handjet', sans-serif;
  --font-press-start: 'PressStart2P', monospace;
  --font-silkscreen: 'Silkscreen', monospace;
  --font-nunito: 'Nunito', 'Saira', system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;
}

@media (max-width: 769px) {
  :root {
    --mobile-chip-scroll-inset: 0.85rem;
  }

  :root {
    --panel-h: 288px;
  }
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  /* Saira wdth axis — slightly condensed for a tighter UI feel */
  font-variation-settings: 'wdth' 88;
}

/* ===== SCREENS ===== */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ===== UPLOAD SCREEN ===== */

#upload-screen {
  align-items: center;
  justify-content: center;
  background: var(--bg);
  transition: background 0.15s;
}

#upload-screen.drag-over {
  background: #ede8dc;
}

#upload-screen.drag-over::after {
  content: '';
  position: fixed;
  inset: 14px;
  border: 2px dashed var(--accent);
  border-radius: calc(var(--radius) * 2);
  pointer-events: none;
  opacity: 0.5;
}

.upload-inner {
  text-align: center;
  padding: 2rem 1.5rem;
  max-width: 360px;
}

.wordmark {
  font-size: 2.8rem;
  font-weight: 700;
  letter-spacing: 0.025em;
  margin: 0 0 2.5rem;
  color: var(--accent);
  font-style: italic;
  font-family: var(--font-serif);
}

.tagline {
  font-size: 0.95rem;
  color: var(--ink-muted);
  margin: 0 0 2rem;
  font-style: italic;
}

.upload-btn {
  display: inline-block;
  padding: 0.75rem 2rem;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-family: var(--font-ui);
  font-size: 1rem;
  cursor: pointer;
  letter-spacing: 0.03em;
  transition: background 0.15s;
  border: none;
}

.upload-btn:hover {
  background: #5a4430;
}

.upload-btn input[type="file"] {
  display: none;
}

.upload-btn-shortcut {
  display: none;
  opacity: 0.62;
  font-size: 0.9em;
  letter-spacing: 0.015em;
}

.upload-hint {
  margin-top: 1.2rem;
  font-size: 0.8rem;
  color: var(--ink-muted);
  font-style: italic;
}

.upload-status {
  margin-top: 0.8rem;
  font-size: 0.82rem;
  color: var(--ink-muted);
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

.upload-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid rgba(122, 110, 94, 0.35);
  border-top-color: var(--ink-muted);
  border-radius: 50%;
  animation: upload-spin 0.8s linear infinite;
}

@keyframes upload-spin {
  to { transform: rotate(360deg); }
}

#upload-quote {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 1.5rem);
  left: 0;
  right: 0;
  margin: 0;
  padding: 0 2rem;
  text-align: center;
  font-family: var(--font-serif);
  font-weight: 700;
  letter-spacing: 0.01em;
  font-style: italic;
  font-size: 0.88rem;
  line-height: 1.8;
  color: var(--ink-muted);
  opacity: 0.55;
  transition: opacity 0.2s;
}

#upload-quote:has(a:hover) {
  opacity: 0.8;
}

#upload-quote a {
  color: inherit;
  text-decoration: none;
}

#upload-quote em {
  font-style: normal;
  font-weight: 700;
}

/* ===== EDITOR SCREEN ===== */

#editor-screen {
  background: var(--bg);
}

#editor-screen.drag-over::after {
  content: 'Drop to add image object';
  position: fixed;
  inset: 14px;
  border: 2px dashed var(--accent);
  border-radius: calc(var(--radius) * 2);
  background: rgba(59, 46, 30, 0.07);
  color: var(--accent);
  font-family: var(--font-ui);
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 1200;
}

/* TOP BAR */

#top-bar {
  /* Extend into status-bar safe area at the top */
  height: calc(var(--top-bar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 1rem 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  flex-shrink: 0;
  z-index: 100;
}

.wordmark-small {
  font-size: 1.25rem;
  font-weight: 700;
  font-style: italic;
  color: var(--accent);
  letter-spacing: 0.02em;
  font-family: var(--font-serif);
  text-align: center;
}

#top-actions {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

#back-btn {
  justify-self: start;
}

#top-actions .top-action-btn {
  width: auto;
  min-width: 36px;
  height: 36px;
  padding: 0 0.52rem;
  gap: 0.24rem;
  font-size: 0.76rem;
  letter-spacing: 0.02em;
}

#top-actions .top-action-btn img {
  width: 18px;
  height: 18px;
  display: block;
  opacity: 0.88;
  filter: var(--top-icon-filter, none);
}

.top-action-label {
  display: none;
}

.top-action-shortcut {
  opacity: 0.56;
  font-size: 0.92em;
  letter-spacing: 0.015em;
}

.icon-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--ink);
  font-size: 1.1rem;
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-ui);
  transition: background 0.12s;
}

.icon-btn:hover {
  background: var(--bg);
}

.icon-btn:active {
  background: var(--border);
}

/* Match chip hover behavior across button controls. */
:where(.icon-btn, .top-action-btn, .font-select-trigger, .style-btn, .align-btn, .control-chip):hover:not(.active):not(:disabled) {
  border-color: var(--ink-muted);
}

/* CANVAS HINT */

#canvas-hint {
  position: absolute;
  top: calc(var(--top-bar-h) + env(safe-area-inset-top, 0px) + 14px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.52);
  color: rgba(255, 255, 255, 0.88);
  font-family: var(--font-ui);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  padding: 0.35em 0.85em;
  border-radius: 999px;
  max-width: calc(100vw - 24px);
  white-space: normal;
  overflow-wrap: anywhere;
  text-align: center;
  pointer-events: none;
  z-index: 200;
  opacity: 1;
  transition: opacity 0.6s ease;
}

#canvas-hint.hidden {
  opacity: 0;
}

/* CANVAS WRAPPER */

#canvas-wrapper {
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #1a1a1a;
  /* Padding ensures the selection outline (outline-offset: 4px) is never
     clipped when the image fills the full width of the wrapper. */
  padding: 10px;
}

#canvas-container {
  position: relative;
  display: inline-block;
  line-height: 0;
  user-select: none;
}

#base-image {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - var(--top-bar-h) - var(--panel-h) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 40px);
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
}

/* ===== SNAP GUIDES ===== */

.guide {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 80;
}

.guide.visible {
  opacity: 1;
}

.guide-v {
  top: 0;
  bottom: 0;
  width: 1px;
  transform: translateX(-0.5px);
  background: rgba(255, 215, 60, 0.35);
}

.guide-h {
  left: 0;
  right: 0;
  height: 1px;
  transform: translateY(-0.5px);
  background: rgba(255, 215, 60, 0.35);
}

.guide-v.snapped {
  background: rgba(255, 215, 60, 0.85);
  box-shadow: 0 0 4px rgba(255, 215, 60, 0.5);
}

.guide-h.snapped {
  background: rgba(255, 215, 60, 0.85);
  box-shadow: 0 0 4px rgba(255, 215, 60, 0.5);
}

.guide-rotate {
  z-index: 85;
}

.guide-rotate-v {
  top: 0;
  bottom: 0;
  width: 1px;
  transform: translateX(-0.5px);
  background: rgba(86, 171, 255, 0.28);
}

.guide-rotate-h {
  left: 0;
  right: 0;
  height: 1px;
  transform: translateY(-0.5px);
  background: rgba(86, 171, 255, 0.28);
}

.guide-rotate-d1,
.guide-rotate-d2 {
  left: 0;
  width: 170%;
  height: 1px;
  transform-origin: center;
  border-top: 1px dashed rgba(86, 171, 255, 0.24);
}

.guide-rotate-d1 {
  transform: translate(-50%, -0.5px) rotate(45deg);
}

.guide-rotate-d2 {
  transform: translate(-50%, -0.5px) rotate(-45deg);
}

@keyframes rotateGuideFlash {
  from { opacity: 1; }
  to { opacity: 0.75; }
}

.guide-rotate-v.snapped,
.guide-rotate-h.snapped,
.guide-rotate-d1.snapped,
.guide-rotate-d2.snapped {
  background: rgba(86, 171, 255, 0.95);
  box-shadow: 0 0 8px rgba(86, 171, 255, 0.65);
  animation: rotateGuideFlash 120ms ease-in-out 0s 2 alternate;
}

.guide-rotate-d1.snapped,
.guide-rotate-d2.snapped {
  border-top-color: rgba(86, 171, 255, 0.95);
  background: transparent;
}

/* ===== TEXT FIELDS ===== */

.text-field {
  position: absolute;
  cursor: grab;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Size to content width so the browser doesn't constrain it based on
     the remaining space to the right of the `left` position, which would
     cause text to reflow/wrap when the field is dragged toward the right edge. */
  width: max-content;
  /* Centered on tap point via JS transform */
}


.text-field-delete,
.text-field-rotate,
.text-field-resize {
  display: none;
  position: absolute;
  width: 22px;
  height: 22px;
  color: #fff;
  border: 2px solid #fff;
  border-radius: 50%;
  font-size: 13px;
  line-height: 1;
  align-items: center;
  justify-content: center;
  z-index: 20;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
  font-family: sans-serif;
  font-weight: 700;
}

.text-field.selected .text-field-delete,
.text-field.selected .text-field-rotate,
.text-field.selected .text-field-resize {
  display: flex;
}

.text-field-delete {
  top: -14px;
  left: -14px;
  background: var(--danger);
  cursor: pointer;
}

.text-field-rotate {
  top: -14px;
  right: -14px;
  background: var(--accent);
  cursor: nwse-resize;
  padding: 0;
}

.text-field-rotate img {
  width: 14px;
  height: 14px;
  display: block;
  filter: brightness(0) invert(1);
}

.text-field-resize {
  right: -14px;
  bottom: -14px;
  background: var(--accent);
  cursor: nwse-resize;
  padding: 0;
}

.text-field-resize img {
  width: 14px;
  height: 14px;
  display: block;
  filter: brightness(0) invert(1);
  transform: scaleY(-1);
}

.text-field-delete > svg {
  transform: rotate(calc(var(--object-rotate-deg, 0deg) * -1));
  transform-origin: center;
}

.text-field-rotate.snapped {
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.35), 0 0 8px rgba(255, 255, 255, 0.4);
}

.text-field.dragging,
.text-field.rotating .text-field-rotate,
.text-field.resizing .text-field-resize {
  cursor: nwse-resize;
}

.text-field-inner {
  position: relative;
  min-width: 60px;
  white-space: pre-wrap;
  word-break: break-word;
  outline: none;
  cursor: inherit;
  text-align: center;
  /* Explicit line-height and padding so the selection outline
     properly contains cap-height, descenders, and stroke overflow */
  line-height: 1.25;
  padding: 0.15em 0.2em;
  /* Override parent's user-select: none so text editing works */
  user-select: text;
  -webkit-user-select: text;
}

.text-field.editing,
.text-field.editing .text-field-inner {
  cursor: text;
}

.text-field.selected {
  outline: 1.5px dashed rgba(255,255,255,0.6);
  outline-offset: 4px;
}

/* Placeholder for empty contenteditable */
.text-field-inner:empty::before {
  content: attr(data-placeholder);
  color: rgba(255, 255, 255, 0.55);
  pointer-events: none;
}

/* Keep native autocorrect/substitutions but hide spell/grammar underlines
   so editor preview matches rendered output. */
.text-field-inner::spelling-error,
.text-field-inner::grammar-error,
.text-field-inner::-webkit-spelling-error,
.text-field-inner::-webkit-grammar-error {
  text-decoration: none;
}

.image-object-inner {
  display: block;
  max-width: none;
  max-height: none;
  border-radius: 6px;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

/* text-stroke via CSS paint-order trick, applied via JS inline styles */

/* ===== BOTTOM PANEL ===== */

#bottom-panel {
  /* Extend into home-indicator safe area at the bottom */
  height: calc(var(--panel-h) + env(safe-area-inset-bottom, 0px));
  padding: 0.45rem 0.85rem calc(1.2rem + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  overflow: visible;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

#font-controls {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  justify-content: space-evenly;
}

#object-controls {
  display: flex;
  flex-direction: column;
  gap: 1.05rem;
}

.object-add-row {
  justify-content: flex-start;
}

.object-add-btn {
  width: auto;
  min-width: 0;
  height: 34px;
  padding: 0 0.6rem;
  gap: 0.38rem;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.object-add-btn img {
  width: 16px;
  height: 16px;
  display: block;
}

.object-add-btn--desktop {
  display: none;
}

.object-bg-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.object-bg-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  color: var(--ink-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}

#ctrl-bg-color:disabled {
  opacity: 0.45;
  cursor: default;
}

/* ── Mobile section tabs ── */

#panel-tabs {
  display: flex;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.5rem;
  /* Bleed into the side padding so the border runs edge-to-edge */
  margin-left: -0.85rem;
  margin-right: -0.85rem;
  padding: 0 0.85rem;
}

.panel-tab {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px; /* overlap the container's border-bottom */
  padding: 0.35rem 0.5rem;
  font-family: var(--font-ui);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--ink-muted);
  cursor: pointer;
  transition: color 0.12s, border-color 0.12s;
}

.panel-tab.active {
  color: var(--ink);
  font-weight: 600;
  border-bottom-color: var(--accent);
}

/* ── Shared row structure ── */

.controls-row {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  flex-wrap: nowrap;
}

.control-label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--ink-muted);
  white-space: nowrap;
  display: block;
  margin-bottom: 3px;
  font-family: var(--font-ui);
  font-weight: 500;
}

.control-group {
  display: flex;
  flex-direction: column;
}

/* ── Presets row ── */

.controls-row--presets {
  flex-wrap: wrap;
}

.chip-scroller {
  display: block;
}

.chip-track {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.control-chip {
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.8rem;
  padding: 0.22rem 0.8rem;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  font-style: normal;
  white-space: nowrap;
  letter-spacing: 0.01em;
  line-height: 1.4;
}

.control-chip:hover:not(.active) {
  border-color: var(--ink-muted);
}

.control-chip.active {
  background: var(--ink);
  color: var(--bg);
  border-color: var(--ink);
}

.control-chip:active {
  opacity: 0.72;
}

/* ── Typeface row (column: label above dropdown+B+I) ── */

.controls-row--typeface {
  flex-direction: column;
  align-items: stretch;
  gap: 0.28rem;
}

.typeface-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.typeface-row select {
  flex: 1;
  min-width: 0;
}

.native-font-select {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  pointer-events: none;
}

.font-select {
  position: relative;
  flex: 1;
  min-width: 0;
}

.font-select-trigger {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.85rem;
  width: 100%;
  height: 36px;
  padding: 0.28rem 0.55rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

#ctrl-font-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.font-select-caret {
  color: var(--ink-muted);
  font-size: 0.78rem;
  flex-shrink: 0;
}

.font-select-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  max-height: 240px;
  overflow-y: auto;
  padding: 0.3rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.12);
  z-index: 140;
  display: none;
}

.font-select.open .font-select-menu {
  display: block;
}

.font-select-option {
  width: 100%;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font-size: 0.9rem;
  padding: 0.45rem 0.5rem;
  cursor: pointer;
}

.font-select-option:hover,
.font-select-option:focus-visible {
  background: rgba(59, 46, 30, 0.12);
  outline: none;
}

.font-select-option.active {
  background: rgba(59, 46, 30, 0.16);
}

/* Bold / Italic style-toggle buttons */

.style-btn {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.88rem;
  font-family: var(--font-ui);
  flex-shrink: 0;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  line-height: 1;
  padding: 0;
}


.style-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.style-btn:active { opacity: 0.7; }

/* ── Size + Align row ── */

.controls-row--size-align {
  align-items: flex-end;
  gap: 0.85rem;
}

.control-group--size {
  flex: 2;
  min-width: 0;
}

/* No extra width override needed — input[type=range] is block by default */

.control-group--align {
  flex-shrink: 0;
}

.row-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 3px;
}

.row-header .control-label {
  margin-bottom: 0;
}

#ctrl-size-val,
#ctrl-line-height-val,
#ctrl-outline-width-val {
  display: none;
}

/* ── Colors + Stroke row ── */

.controls-row--colors {
  align-items: flex-end;
  gap: 0.7rem;
}

.colors-left {
  display: flex;
  gap: 0.55rem;
  flex-shrink: 0;
  align-items: flex-end;
}

.cycle-btn {
  width: 24px;
  height: 24px;
  padding: 0;
  margin-bottom: 5px;
  flex-shrink: 0;
  border: 0;
  background: transparent;
}

.cycle-btn img {
  width: 16px;
  height: 16px;
  display: block;
  opacity: 0.85;
}

.cycle-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.cycle-btn:hover {
  background: transparent;
}

.control-group--stroke,
.control-group--focus {
  flex: 1;
  min-width: 0;
}

/* ── Form elements ── */

select,
input[type="color"] {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.85rem;
}

select {
  padding: 0.28rem 0.4rem;
  width: 100%;
  cursor: pointer;
  height: 36px;
}

input[type="color"] {
  width: 40px;
  height: 36px;
  padding: 2px 3px;
  cursor: pointer;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  /* Match the height of color inputs (36px) so all controls on a row align. */
  height: 36px;
  background: transparent;
  outline: none;
  cursor: pointer;
  padding: 0;
  margin: 0;
  display: block;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

input[type="range"]::-moz-range-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 22px;
  border-radius: 4px;
  background: var(--accent);
  cursor: pointer;
  margin-top: -9px; /* center on 4px track: -(22-4)/2 */
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 22px;
  border-radius: 4px;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

/* Full-width standalone sliders (Size, Intensity, Grain, Scanlines, Chroma)
   can be shorter than the 36px needed to align with color pickers. */
.control-group--size input[type="range"] {
  height: 28px;
}

.vibe-layer-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  cursor: pointer;
  user-select: none;
  position: relative;
}

.vibe-layer-toggle input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.vibe-layer-switch {
  width: 36px;
  height: 20px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: color-mix(in srgb, var(--bg) 78%, var(--border) 22%);
  position: relative;
  transition: background 0.14s ease, border-color 0.14s ease;
  flex-shrink: 0;
}

.vibe-layer-thumb {
  position: absolute;
  left: 2px;
  top: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--ink-muted);
  transition: transform 0.14s ease, background 0.14s ease;
}

.vibe-layer-label {
  margin-bottom: 0;
}

.vibe-layer-toggle input[type="checkbox"]:checked + .vibe-layer-switch {
  background: color-mix(in srgb, var(--accent) 36%, var(--bg) 64%);
  border-color: var(--accent);
}

.vibe-layer-toggle input[type="checkbox"]:checked + .vibe-layer-switch .vibe-layer-thumb {
  transform: translateX(16px);
  background: var(--accent);
}

.vibe-layer-toggle input[type="checkbox"]:focus-visible + .vibe-layer-switch {
  outline: 2px solid color-mix(in srgb, var(--accent) 62%, transparent);
  outline-offset: 2px;
}

/* ── Section heading rows ── */

.section-heading-row {
  /* Hidden on mobile — the panel tabs serve as section labels.
     Desktop restores these via the media query below. */
  display: none;
}

.section-divider {
  /* Likewise hidden on mobile (no visual separation needed in tabs). */
  display: none;
  height: 1px;
  background: var(--border);
}

.control-label.section-heading {
  font-size: 0.68rem;
  letter-spacing: 0.11em;
  font-weight: 600;
  color: var(--ink);
}

/* ── Align buttons — segmented control ── */

.btn-group {
  display: flex;
}

.align-btn {
  border: 1px solid var(--border);
  border-radius: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 1rem;
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  /* Collapse adjacent borders into one */
  margin-left: -1px;
}

.align-btn:first-child {
  border-radius: var(--radius) 0 0 var(--radius);
  margin-left: 0;
}

.align-btn:last-child {
  border-radius: 0 var(--radius) var(--radius) 0;
}

.align-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  z-index: 1; /* sit above neighbours so accent border isn't overlapped */
  position: relative;
}

.align-btn:active {
  opacity: 0.7;
}

/* ===== FILTER SECTION ===== */

#filter-section {
  display: none; /* shown only on desktop (or mobile vibe tab) */
}

.controls-row--vibe-chips {
  flex-wrap: wrap;
}

.vibe-extra-controls {
  display: flex;
  flex-direction: column;
  gap: 1.05rem;
}

.controls-row--split {
  gap: 0.85rem;
}
.controls-row--split .control-group {
  flex: 1;
  min-width: 0;
}

/* ===== iOS SAVE OVERLAY ===== */

#ios-save-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.88);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  justify-items: center;
  gap: 0.9rem;
  padding:
    calc(env(safe-area-inset-top, 0px) + 1rem)
    1rem
    calc(env(safe-area-inset-bottom, 0px) + 1rem);
}

.ios-overlay-top {
  width: min(900px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
}

.ios-overlay-image-wrap {
  min-height: 0;
  width: min(1200px, 100%);
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.2rem 0.25rem;
}

#ios-save-overlay img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0,0,0,0.6);
}

.ios-overlay-actions {
  display: flex;
  align-items: center;
  justify-content: center;
}

#ios-save-overlay p {
  color: #fff;
  font-family: var(--font-ui);
  font-style: italic;
  font-size: 0.95rem;
  text-align: center;
  margin: 0;
}

.ios-overlay-divider {
  color: rgba(255,255,255,0.4) !important;
  font-size: 0.75rem !important;
}

.ios-open-btn {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.7rem 2.2rem;
  font-family: var(--font-ui);
  font-size: 1.05rem;
  text-decoration: none;
  cursor: pointer;
  letter-spacing: 0.02em;
}

.ios-open-btn:active {
  opacity: 0.8;
}

#ios-save-overlay button {
  background: transparent;
  color: rgba(255,255,255,0.5);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius);
  padding: 0.5rem 1.5rem;
  font-family: var(--font-ui);
  font-size: 0.9rem;
  cursor: pointer;
}

/* ===== Utility ===== */
.hidden { display: none !important; }

/* ===== MOBILE — tab-based panel switching ===== */

@media (max-width: 769px) {
  /* Use the visible viewport on mobile Safari/PWA so the bottom tray never
     sits underneath browser chrome. */
  .screen {
    left: 0;
    right: 0;
    top: 0;
    bottom: auto;
    height: 100vh;
  }

  @supports (height: 100dvh) {
    .screen {
      height: 100dvh;
    }
  }

  #base-image {
    max-height: none;
  }

  #bottom-panel {
    position: relative;
    height: auto;
    min-height: 0;
    padding-top: 0.2rem;
    padding-bottom: calc(0.85rem + env(safe-area-inset-bottom, 0px));
    overflow-x: visible;
    overflow-y: visible;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto;
    align-content: start;
  }

  #panel-tabs {
    grid-column: 1;
    grid-row: 1;
    margin-bottom: 0;
  }

  #font-controls {
    grid-column: 1;
    grid-row: 2;
    flex: none;
    min-height: 0;
    justify-content: flex-start;
    gap: 0.45rem;
    padding-top: 0.4rem;
  }

  #object-controls {
    grid-column: 1;
    grid-row: 2;
    flex: none;
    min-height: 0;
    height: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    gap: 0.55rem;
    padding-top: 0;
  }

  /* Keep sections in layout so panel height stays intrinsic. Active panel is
     switched by data-panel, without JS height measurements. */
  #filter-section {
    grid-column: 1;
    grid-row: 2;
    display: flex;
    flex-direction: column;
    flex: none;
    min-height: 0;
    height: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    gap: 0.45rem;
    padding-top: 0;
  }

  #bottom-panel[data-panel="object"] #font-controls,
  #bottom-panel[data-panel="vibe"] #font-controls {
    visibility: hidden;
    pointer-events: none;
  }

  #bottom-panel[data-panel="object"] #object-controls {
    height: 100%;
    overflow-y: auto;
    opacity: 1;
    pointer-events: auto;
    padding-top: 0.4rem;
  }

  #bottom-panel[data-panel="vibe"] #object-controls {
    visibility: hidden;
    pointer-events: none;
  }

  #bottom-panel[data-panel="vibe"] #filter-section {
    height: auto;
    overflow: visible;
    opacity: 1;
    pointer-events: auto;
    padding-top: 0.4rem;
  }

  #bottom-panel[data-panel="vibe"] {
    overflow-y: auto;
  }

  /* Compact form controls — save vertical space in the mobile tray */
  select,
  input[type="color"],
  input[type="range"] {
    height: 30px;
  }

  /* .control-group--size inputs are already 28px (higher specificity wins),
     so this only affects the stroke/blur sliders in the colors row, keeping
     them aligned with the shrunken color pickers. */

  .style-btn,
  .align-btn {
    width: 30px;
    height: 30px;
  }

  .font-select-menu {
    top: auto;
    bottom: calc(100% + 6px);
    max-height: min(40vh, 240px);
    z-index: 320;
  }

  /* Tighter label/header gaps */
  .control-label {
    margin-bottom: 1px;
  }

  .row-header {
    margin-bottom: 1px;
  }

  /* Vibe-extra-controls (scanlines + chroma stacked rows) */
  .vibe-extra-controls {
    gap: 0.45rem;
  }

  /* Save vertical space: presets scroll horizontally on mobile. */
  .controls-row--presets {
    flex-wrap: nowrap;
  }

  .chip-scroller {
    box-sizing: border-box;
    width: calc(100% + (var(--mobile-chip-scroll-inset) * 2));
    max-width: none;
    margin-left: calc(var(--mobile-chip-scroll-inset) * -1);
    margin-right: calc(var(--mobile-chip-scroll-inset) * -1);
    padding: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .chip-scroller::-webkit-scrollbar {
    display: none;
  }

  .chip-track {
    display: flex;
    width: max-content;
    min-width: 100%;
    box-sizing: border-box;
    padding: 0 var(--mobile-chip-scroll-inset);
    gap: 0.4rem;
    flex-wrap: nowrap;
  }

  .control-chip {
    flex: 0 0 auto;
  }

  .controls-row--vibe-chips {
    flex-wrap: nowrap;
    overflow: visible;
  }

  .vibe-chips-track {
    padding: 0 var(--mobile-chip-scroll-inset) 2px;
  }

  #bottom-panel[data-panel="vibe"] #filter-section {
    overflow-x: visible;
  }
}

/* ===== DESKTOP — sidebar layout ===== */

@media (min-width: 770px) {
  /* Override the mobile flex-column with a 2-column grid:
     top bar spans full width; canvas + sidebar share the row below. */
  #editor-screen.active {
    display: grid;
    grid-template-columns: 1fr 350px;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "topbar topbar"
      "canvas sidebar";
  }

  #top-bar      { grid-area: topbar; }
  #canvas-wrapper { grid-area: canvas; }

  /* Sidebar replaces the bottom panel */
  #bottom-panel {
    grid-area: sidebar;
    height: auto;          /* fill the grid row */
    border-top: none;
    border-left: 1px solid var(--border);
    padding: 1.1rem 1rem;
    justify-content: flex-start;
    overflow-y: auto;
  }

  /* Hide mobile tab bar */
  #panel-tabs {
    display: none;
  }

  /* Restore section headings and dividers */
  .section-heading-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Negative margin counteracts the flex gap so the heading sits
       close to its first content row */
    margin-bottom: -0.7rem;
  }

  .object-add-row {
    display: none;
  }

  .object-add-btn--desktop {
    display: inline-flex;
    width: 30px;
    min-width: 30px;
    height: 30px;
    padding: 0;
    gap: 0;
  }

  .object-add-btn--desktop span {
    display: none;
  }

  .section-heading-row--object {
    position: relative;
    justify-content: flex-start;
    padding-right: 36px;
  }

  .section-heading-row--object .object-add-btn--desktop {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
  }

  .section-divider {
    display: block;
    margin: 0.75rem 0 0;
  }

  #font-controls,
  #object-controls,
  #filter-section {
    height: auto;
    flex: none;         /* reset the mobile flex: 1 */
    justify-content: flex-start;
    display: flex;
    flex-direction: column;
    gap: 1.05rem;
    overflow-y: visible; /* sidebar itself scrolls */
  }

  /* Image no longer constrained by panel height */
  #base-image {
    max-height: calc(100vh - var(--top-bar-h) - env(safe-area-inset-top, 0px) - 50px);
    max-width: 100%;
  }
}

@media (min-width: 900px) and (hover: hover) and (pointer: fine) {
  .top-action-label {
    display: inline;
  }

  .upload-btn-shortcut {
    display: inline;
    margin-left: 0.36rem;
  }

  #back-btn {
    width: auto;
    min-width: 36px;
    padding: 0 0.52rem;
    gap: 0.24rem;
    font-size: 0.76rem;
    letter-spacing: 0.02em;
  }
}

/* ===== DARK MODE ===== */

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #191410;
    --border:    #3a3028;
    --ink:       #f0e6d8;
    --ink-muted: #917e6c;
    --accent:    #c49a6c;
    --danger:    #e05548;
  }

  /* Drag-over hint darkened to match dark bg palette */
  #upload-screen.drag-over {
    background: #252018;
  }

  #editor-screen.drag-over::after {
    background: rgba(25, 20, 16, 0.55);
  }

  .font-select-menu {
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.35);
  }

  .font-select-option:hover,
  .font-select-option:focus-visible,
  .font-select-option.active {
    background: rgba(196, 154, 108, 0.2);
  }

  /* Canvas surround can go a touch darker */
  #canvas-wrapper {
    background: #0f0d0a;
  }

  /* Hover/active overrides for buttons that use hardcoded values */
  .upload-btn:hover {
    background: #d4aa80;
  }

  #top-actions .top-action-btn:hover,
  #top-actions .top-action-btn:active {
    background: transparent;
  }

  #top-actions .top-action-btn img {
    --top-icon-filter: invert(1);
  }

  .cycle-btn img {
    filter: invert(1) brightness(1.08);
    opacity: 0.95;
  }
}
