/* =========================================================================
   DESIGNER page (/designer, /textures - index.html)
   Two states, per the approved design:
     INPUT  - a full-width hero (mode tiles + input card). No param sidebar:
              there is nothing to tune before a shape exists.
     RESULT - the sidebar slides in with the section cards, and the
              artifact owns the canvas with floating actions over it.
   `body.has-result` (set by setState) switches between them.

   Everything reusable lives in system.css (.btn, .card, .sec, .tile, .chip,
   .segmented, .num-input, .csel, the floating stage chrome); this file
   holds the page's layout and the pieces unique to it.
   ========================================================================= */


/* --------------------------------------------------------------------------
   BASE: this page owns the viewport - the topbar is a fixed height and the
   .main grid takes the rest; only the sidebar and stage scroll internally.
   -------------------------------------------------------------------------- */

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  display: flex; flex-direction: column;
}


/* --------------------------------------------------------------------------
   MAIN LAYOUT
   The sidebar column collapses to 0 in the input state. It stays in the DOM
   (so the mode tiles and param controls keep their values) but takes no space.
   -------------------------------------------------------------------------- */

.main {
  flex: 1;
  display: grid;
  grid-template-columns: 0 1fr;
  min-height: 0;
  transition: grid-template-columns .25s var(--ease);
}
body.has-result .main { grid-template-columns: var(--sidebar-width) 1fr; }


/* --------------------------------------------------------------------------
   SIDEBAR
   -------------------------------------------------------------------------- */

.sidebar {
  background: var(--paper);
  border-right: 1px solid var(--line-soft);
  display: flex; flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
/* Hidden outright in the input state: `visibility` (not display) so the
   controls inside stay measurable for the param collection. */
body:not(.has-result) .sidebar { visibility: hidden; }

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-lg) .9rem;
}
/* scrollbar: the shared .scroll-thin utility (system.css) at its default width */

/* The mode tiles sit at the top of the sidebar, above the first section card. */
#mode-tiles { margin-bottom: var(--space-md); }

/* The pinned quota footer is GONE, with the box and the "unlock more" pill it held.
   The balance is the count badge on the topbar's account icon (`.acct-count`), the one
   place it appears outside /account; selling happens at the moment of a refusal, where
   the intent is, rather than from a pill sitting in the sidebar all day. The sidebar is
   now params and nothing else, which is what it is for. */

/* "Restore the defaults", under the last parameter section. The look is the shared
   `.btn--reset`; this file owns only the placement — `display: flex` because `.btn`
   is inline-flex and `margin: auto` cannot centre an inline box. */
.sidebar-reset { display: flex; margin: var(--space-md) auto 0; }


/* --------------------------------------------------------------------------
   SIDEBAR: advanced-settings accordion
   An outer container (.adv-group) holding a clickable header (.adv-toggle) and
   the collapsing body (.adv-sec). The caret is a Lucide chevron in the title.
   -------------------------------------------------------------------------- */

/* The group is a recessed well, NOT another paper-2 card: its children are .sec
   cards, and stacking a card on a card of the same tone is what made the
   accordion read as three unrelated boxes at the same level as Dimension and
   Blade. A darker inset field with the cards floating on it reverses the
   figure/ground so the group is visibly one block, subordinate to the sections
   above it. */
.adv-group {
  background: var(--surface-inset);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-md);
  padding: var(--space-xs);
  margin-bottom: .7rem;   /* matches .sec, so the sidebar rhythm is unchanged */
}

/* The header composes the shared .sec card (paper-2, border, radius, padding);
   these rules only add the click affordance and lay the title/desc beside the
   caret. .sec-title is flex+space-between by default, so it is neutralized here
   (the row-level spacing is handled by .adv-toggle itself). */
.adv-toggle {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-sm);
  cursor: pointer; user-select: none;
}
.adv-toggle-text { display: flex; flex-direction: column; min-width: 0; }
.adv-toggle .sec-title { display: block; }
.adv-toggle .sec-desc { margin-bottom: 0; }
.adv-toggle svg {
  width: 18px; height: 18px;
  flex-shrink: 0;
  color: var(--cocoa);
  transition: transform var(--dur-base);
}
.adv-toggle.open svg { transform: rotate(180deg); }
/* Inside the group the header owns the bottom gap only when the body is open,
   so a closed accordion is a single flush card in its well. */
.adv-group .adv-toggle { margin-bottom: 0; }
.adv-group .adv-toggle.open { margin-bottom: var(--space-xs); }
.adv-sec { display: none; }
.adv-sec.open { display: block; }
/* The last sub-card sits flush with the well's padding. */
.adv-sec > .sec:last-child { margin-bottom: 0; }



/* --------------------------------------------------------------------------
   INPUT hero: mode tiles + the input card
   -------------------------------------------------------------------------- */

.canvas-idle {
  text-align: center;
  width: 100%; max-width: 620px;
  min-width: 0;   /* a flex item defaults to min-content: without this the mode
                     tile row forces the canvas wider than the viewport */
  /* Centred vertically by AUTO MARGINS, not by `.canvas`'s `align-items: center`
     alone — same fix as `.bf-overlay-box`. Flex centring overflows BOTH ways: when
     the hero is taller than the canvas (a phone, a short window, Photo's upload
     zone) its top lands above the scroll origin, where no scrolling can reach, and
     the headline disappeared whole or in part (found 2026-10-07). An auto margin
     centres while there is room and drops to 0 when there is not, so the overflow
     goes downward, into the scroll. */
  margin-block: auto;
}
/* `min()` with no lower bound, like the landing headline this now shares its
   string with: the clamp's 1.7rem floor was wider than a 420px viewport, so the
   title overflowed on a phone between 480px (where the rule below takes over)
   and the point where 3.2vw alone would have fitted. */
.canvas-idle h2 {
  font-family: var(--heading);
  font-size: min(3.2vw, 2.4rem); font-weight: 700;
  color: var(--ink);
  line-height: 1.16;
  margin-bottom: var(--space-sm);
}
.canvas-idle h2 .accent { color: var(--burgundy); }
.canvas-idle p {
  font-family: var(--body);
  font-size: var(--text-lg); color: var(--ink-soft);
  margin-bottom: var(--space-xl);
  margin-left: auto; margin-right: auto;
  text-wrap: balance;
}
/* The hero shares the landing's headline/sub strings, so it must also share the
   accent treatment inside the sub copy: the heading face in burgundy, which is
   what tells it apart from the Inter body text around it. No italic - Outfit has
   no italic cut and the browser's synthetic one is visibly sheared (tokens.css). */
.canvas-idle p .accent {
  font-family: var(--heading); font-weight: 600;
  color: var(--burgundy);
}

/* Mode tiles in the hero run as a wider, centered row: this is the primary
   choice on the screen, so they get more presence than the sidebar variant. */
/* The row is centred and sized by its CONTENT, not by a fixed column count:
   /designer has 4 modes but /textures fewer (2 when written; 1 while stencil is
   disabled in features.js), and `repeat(4, 1fr)` left those filling the first
   columns, i.e. hugging the left edge with empty tracks beside them.
   auto-flow with a min tile width keeps 4 across on the designer and centres
   whatever number the mode registry yields. */
.hero-tiles {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: var(--space-md);
  /* Tight to the note below it (they are one unit: the row and its caption); the
     generous separation moved to the note's own margin-bottom. */
  margin-bottom: var(--space-md);
}
.hero-tiles .tile { flex: 1 1 130px; max-width: 200px; }
.hero-tiles .tile {
  flex-direction: column; justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-lg) var(--space-sm);
  text-align: center;
  font-size: var(--text-base);
}
.hero-tiles .tile svg { width: 24px; height: 24px; }

/* What the chosen mode makes: ONE panel for the whole row, not four captions
   inside it (four labels + four captions is eight things to read to make one
   choice, and each wrapped at a different point, which broke the row's rhythm).

   It is a panel rather than a bare line so the caption has a body of its own: a
   line of text alone under a row of cards reads as something left over, while a
   paper-2 box with the mode's glyph in it reads as the row's answer. It describes
   the SELECTION only — a hover preview used to describe a mode that was not the
   selected one, and could not exist on a touch screen at all. (It briefly carried a
   caret pointing at the active tile; that added a moving part to say what the panel
   already says by sitting under the row.) The text still differs from the tile
   labels by SIZE and TONE, not by being a fainter copy of them (it carried the
   house's Playfair italic until the heading face became Outfit, which has no
   italic — see tokens.css). `--ink-soft`, not `--cocoa`: on the panel's own
   `--paper-2` background cocoa lands at 3.9:1 for a 15px face, ink-soft at 5.5:1. */
.mode-note {
  /* Block-level flex sized to its content, so `margin: 0 auto` really centres it
     under the row — an inline-flex box would follow the parent's text alignment
     instead, which the hero does not set. */
  display: flex; align-items: center; gap: var(--space-sm);
  width: fit-content;
  margin: 0 auto var(--space-xl);
  padding: .45rem .9rem;
  max-width: 38ch;
  background: var(--paper-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  font-family: var(--heading);
  font-size: var(--text-lg);
  line-height: 1.45;
  color: var(--ink-soft);
  text-wrap: balance;
}
/* The tile's own glyph, repeated small and in the tiles' accent tone, so the
   panel is recognisably about that card and not a generic hint box. */
.mode-note svg {
  width: 16px; height: 16px;
  flex-shrink: 0;
  color: var(--crust);
}
/* The entrance, restarted by JS whenever the words change (remove class → reflow
   → add): a 3px rise out of transparent, on the CONTENT only — the box itself stays
   put, so switching mode reads as the note being rewritten rather than as a panel
   appearing. The duration lives here only, so nothing has to stay in sync with a
   timeout, and the reduced-motion block at the end of system.css zeroes it. */
.mode-note.is-fresh > * { animation: modeNoteIn var(--dur-base) var(--ease); }
@keyframes modeNoteIn {
  from { opacity: 0; transform: translateY(3px); }
  to   { opacity: 1; transform: none; }
}

/* The input card composes the shared .card; only the alignment and the roomier
   padding are page-specific. */
.hero-input {
  text-align: left;
  padding: 1.2rem;
}
/* The tab track is centered above the input, not left-aligned to it. */
.hero-input .input-tabs {
  display: flex; margin: 0 auto var(--space-md);
  width: fit-content;
}
.hero-input .prompt-textarea { background: var(--paper); }
.hero-input .chips { justify-content: center; margin-top: var(--space-md); }
.hero-input .gen-btn { margin-top: var(--space-md); }
.hero-params .field { margin-top: var(--space-md); }


/* --------------------------------------------------------------------------
   INPUT: upload zone + preview
   -------------------------------------------------------------------------- */

.upload-zone {
  width: 100%; padding: 1.4rem .75rem;
  border: 1.5px dashed var(--line);
  border-radius: var(--radius-md);
  background: var(--paper);
  display: none; flex-direction: column;
  align-items: center; gap: var(--space-xs); cursor: pointer;
  transition: all var(--dur-fast); text-align: center;
}
.upload-zone.active { display: flex; }
.upload-zone:hover { border-color: var(--cocoa); background: var(--focus-bg); }
.upload-zone.drag-over {
  border-color: var(--burgundy); background: rgb(var(--burgundy-rgb) / .06);
  border-style: solid;
}
.upload-zone svg { width: 28px; height: 28px; color: var(--shortcrust-dk); }
.upload-zone .main-txt {
  font-family: var(--heading); font-size: var(--text-xl); font-weight: 600;
  color: var(--ink);
}
.upload-zone .sub-txt {
  font-size: var(--text-xs); color: var(--ink-faded); font-style: italic;
}

.upload-preview {
  display: none; padding: var(--space-sm); background: var(--paper);
  border: 1px solid var(--line); border-radius: var(--radius-md);
  align-items: center; gap: var(--space-sm);
}
.upload-preview.active { display: flex; }
.upload-preview img {
  width: 40px; height: 40px; object-fit: contain;
  border: 1px solid var(--line-soft); border-radius: var(--radius-sm); background: white;
}
.upload-preview .name {
  flex: 1; font-size: var(--text-sm); color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.upload-preview-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; padding: 0;
  background: none; border: none; border-radius: 50%;
  color: var(--ink-faded); cursor: pointer;
  transition: color var(--dur-fast), background var(--dur-fast);
}
.upload-preview-btn svg { width: 15px; height: 15px; }
.upload-preview-btn:hover { color: var(--cocoa-dk); background: var(--paper-2); }
.upload-preview-btn--remove:hover { color: var(--burgundy); }

.upload-instr {
  display: none;
  margin-top: var(--space-sm);
  font-size: var(--text-sm);
}
.upload-instr.active { display: block; }
.upload-instr::placeholder { font-style: italic; }


/* --------------------------------------------------------------------------
   STAGE (preview area) - the signature surface
   -------------------------------------------------------------------------- */

.stage {
  position: relative;
  display: flex; flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* decorative blotches */
.stage::before {
  content: ''; position: absolute;
  top: 8%; right: 12%;
  width: 180px; height: 180px;
  background: radial-gradient(circle, rgba(184,138,90,.07) 0%, transparent 65%);
  border-radius: 50%; pointer-events: none;
}
.stage::after {
  content: ''; position: absolute;
  bottom: 12%; left: 8%;
  width: 220px; height: 220px;
  background: radial-gradient(circle, rgb(var(--burgundy-rgb) / .04) 0%, transparent 65%);
  border-radius: 50%; pointer-events: none;
}

.canvas {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-xl);
  position: relative;
  min-height: 0; min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
}


/* --------------------------------------------------------------------------
   MOBILE: sidebar drawer toggle + backdrop
   -------------------------------------------------------------------------- */

.sidebar-toggle {
  display: none;
  position: fixed; bottom: var(--space-lg); left: 50%; transform: translateX(-50%);
  padding: .6rem 1.4rem;
  background: var(--ink); color: var(--paper);
  border: none; border-radius: var(--radius-pill);
  font-family: var(--heading); font-size: var(--text-lg); font-weight: 600;
  cursor: pointer; z-index: var(--z-drawer-fab);
  box-shadow: var(--shadow-cta-h);
}
.sidebar-toggle:active { transform: translateX(-50%) scale(.96); }

.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgb(var(--ink-rgb) / .4);
  z-index: var(--z-backdrop);
}
.sidebar-backdrop.open { display: block; }


/* --------------------------------------------------------------------------
   CANVAS: drag-and-drop overlay
   -------------------------------------------------------------------------- */

.drop-overlay {
  position: absolute; inset: 0;
  background: rgba(250,243,226,.95);
  backdrop-filter: blur(4px);
  border: 3px dashed var(--burgundy);
  border-radius: var(--radius-md);
  margin: var(--space-lg);
  display: none;
  align-items: center; justify-content: center;
  z-index: var(--z-drop);
  pointer-events: none;
}
.drop-overlay.active { display: flex; }
.drop-inner {
  text-align: center;
  pointer-events: none;
}
.drop-inner svg {
  width: 52px; height: 52px;
  color: var(--shortcrust-dk);
  margin-bottom: var(--space-md);
}
.drop-inner h3 {
  font-family: var(--heading);
  font-size: var(--text-3xl); color: var(--ink);
  margin-bottom: var(--space-xs);
}
.drop-inner p {
  font-style: italic; color: var(--ink-soft);
}


/* --------------------------------------------------------------------------
   CANVAS STATE: loading (cookie loader)
   -------------------------------------------------------------------------- */

.canvas-loading {
  display: none; flex-direction: column;
  align-items: center; justify-content: center; gap: .6rem;
}
/* The step line belongs to the headline above it, so it sits closer than the
   .6rem the column gap gives every other pair. */
.loading-title + .loading-step { margin-top: -.35rem; }
/* The full-size loader: the shared .cookie-* component at 110px. */
.cookie-loader {
  display: flex; flex-direction: column; align-items: center;
  --cookie-size: 110px;
  --cookie-shadow-w: 64px;
  --cookie-shadow-h: 7px;
}
/* Fixed headline above the rotating step. Same heading face + burgundy accent as
   the idle hero and the landing pitch, so the loading state reads as the same voice
   rather than as a system message. */
.loading-title {
  font-family: var(--heading);
  font-size: min(2.6vw, 1.7rem); font-weight: 700;
  color: var(--ink);
  line-height: 1.16;
  text-align: center;
  margin: 0;
}
.loading-title .accent { color: var(--burgundy); }
.loading-step {
  font-family: var(--body); font-style: italic;
  font-size: var(--text-md); color: var(--ink-soft);
  min-height: 1.4em;
  transition: opacity var(--dur-base);
  text-align: center;
}


/* --------------------------------------------------------------------------
   CANVAS STATE: result (3D viewer, live re-render pill, floating actions)
   -------------------------------------------------------------------------- */

.canvas-result {
  display: none;
  position: absolute; inset: 0;
}

.viewer3d {
  display: none;
  position: absolute; inset: 0;
  background: radial-gradient(ellipse at 50% 30%, #fffdf8, #f0e5d3);
  overflow: hidden;
  cursor: grab;
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Ccircle cx='10' cy='10' r='7' fill='none' stroke='%239a9a9a' stroke-width='1.5'/%3E%3Ccircle cx='10' cy='10' r='2' fill='%239a9a9a'/%3E%3C/svg%3E") 10 10, grab;
}
.viewer3d.active { display: block; }
.viewer3d:active {
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Ccircle cx='10' cy='10' r='7' fill='none' stroke='%236b6b6b' stroke-width='1.5'/%3E%3Ccircle cx='10' cy='10' r='2' fill='%236b6b6b'/%3E%3C/svg%3E") 10 10, grabbing;
}

/* "New image" turns burgundy once the prompt no longer matches the shape on
   screen - the one place the outline button changes meaning. */
.btn--ghost.dirty {
  background: var(--burgundy);
  color: var(--paper);
  border-color: var(--burgundy);
}
.btn--ghost.dirty:hover:not(:disabled) { background: var(--burgundy); color: var(--paper); }

/* `.live-pill` now lives in system.css: /lettering shows the same rebuild pill and
   does not load this file — the same reason `.loader-art` is shared. */

.result-svg-wrap {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--paper);
}
.result-svg-wrap svg { max-width: 80%; max-height: 80%; }
/* Pipeline error shown in place of the 2D preview. */
.stage-error {
  text-align: center; padding: var(--space-lg);
  font-family: var(--body); font-style: italic;
  color: var(--burgundy);
}

/* The one error that has a remedy: out of creations. Appended by showErrorAction(),
   so every other error keeps the shape above. `display: block` + `margin: auto`
   rather than a flex parent, because the box is a text block first — the link is an
   addition to a sentence, not a second column. The italic is undone: the message is
   the app's voice, the button is a control. */
.stage-error-action {
  display: block; width: max-content;
  margin: var(--space-md) auto 0;
  font-style: normal;
}

/* source line-art thumbnail in the result view */
.src-thumb {
  position: absolute; left: 12px; bottom: 12px;
  width: 68px; height: 68px;
  background: white;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-md);
  padding: 4px;
  cursor: zoom-in;
  z-index: 5;
  box-shadow: var(--shadow-md);
  transition: width .18s var(--ease), height .18s var(--ease);
}
.src-thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.src-thumb.big {
  width: min(52vh, 380px); height: min(52vh, 380px);
  cursor: zoom-out;
}


/* --------------------------------------------------------------------------
   CROP OVERLAY (raster uploads pass through here before the pipeline)
   -------------------------------------------------------------------------- */

.crop-overlay {
  position: fixed; inset: 0;
  background: rgb(var(--ink-rgb) / .55);
  display: none;
  align-items: center; justify-content: center;
  z-index: var(--z-overlay);
  padding: var(--space-lg);
}
.crop-overlay.active { display: flex; }
/* desktop: center over the CANVAS (stage), not the whole window - the
   340px sidebar sits on the left once there is a result */
@media (min-width: 821px) {
  body.has-result .crop-overlay { padding-left: calc(var(--sidebar-width) + 1rem); }
}
.crop-box {
  position: relative;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 1.2rem var(--space-xl) 1.4rem;
  width: min(100%, 900px);
  /* dvh: on a phone 94vh is measured with the address bar hidden, so the bottom of
     the box (Confirm) could sit under the browser's toolbar. vh is the fallback. */
  max-height: 94vh;
  max-height: 94dvh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgb(var(--ink-rgb) / .35);
}
.crop-close {
  position: absolute;
  top: .7rem; right: .8rem;
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px;
  color: var(--ink-faded);
  background: none; border: none; border-radius: 50%;
  cursor: pointer;
  z-index: 2;
  transition: color var(--dur-fast), background var(--dur-fast);
}
.crop-close svg { width: 18px; height: 18px; }
.crop-close:hover { color: var(--burgundy); background: var(--paper-2); }
.crop-head h3 {
  font-family: var(--heading);
  font-size: var(--text-3xl); color: var(--ink);
  margin-bottom: .15rem;
}
.crop-head p {
  font-style: italic; color: var(--ink-soft);
  font-size: var(--text-md); margin-bottom: var(--space-md);
}
.crop-tools {
  display: flex; align-items: center; justify-content: center; gap: var(--space-sm);
  margin-bottom: .7rem; flex-wrap: wrap;
}
/* The crop tools are shared .chip pills; only the disabled/wait state is local. */
.crop-tools .chip:disabled { opacity: .55; cursor: wait; }

/* The undo chip is NOT a fifth tool: the four change the image, this one throws
   their work away. So it is separated and it is the only coloured thing in the
   row — a reader has to be able to tell at a glance that it does not belong to
   the group.

   Three deliberate choices:
   1. A hairline that fades at both ends, the same motif as the topbar's seam
      between the nav and its icon cluster (system.css `.topbar-icons::before`).
      Same problem — two kinds of control in one row — so the same answer, rather
      than a second invention.
   2. `--burgundy`, which in this palette is reserved for meaning and is already
      the colour of the selection rectangle right below it (so it introduces no
      new hue into the overlay). It follows `.btn--danger`'s grammar: quiet by
      default, committing only on hover — because it removes work, even though
      nothing is lost for good (the original is exactly where it returns to).
   3. Visible ONLY once the overlay is `.dirty` (crop.js cropMarkDirty). An undo
      permanently on screen with nothing to undo is precisely what the old
      "Full image" button was, and why nobody could tell what it did. */
.crop-undo {
  display: none;
  position: relative;
  margin-left: 1.4rem;
  color: var(--burgundy);
  border-color: rgb(var(--burgundy-rgb) / .35);
}
.crop-undo::before {
  content: '';
  position: absolute; left: -.85rem; top: 50%;
  transform: translateY(-50%);
  width: 1px; height: 20px;
  background: linear-gradient(to bottom,
              transparent, var(--line) 28%, var(--line) 72%, transparent);
}
.crop-undo:hover {
  color: var(--burgundy);
  border-color: var(--burgundy);
  background: rgb(var(--burgundy-rgb) / .08);
}
.crop-overlay.dirty .crop-undo { display: inline-flex; }
.crop-stage {
  position: relative;
  display: block;
  margin: 0 auto;              /* centered in the box (width pinned by JS) */
  line-height: 0;
  user-select: none;
  touch-action: none;          /* pointer events handle the dragging */
  background: white;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  overflow: auto;              /* zoomed image (mouse wheel) pans via scroll */
}
.crop-stage img {
  /* 100% of the box, not a share of the viewport: 88vw overflowed the box whenever
     the box was narrower than that (a phone's padding, the result sidebar). */
  max-width: min(100%, 840px);
  /* as tall as the screen allows while keeping toolbar + prompt row + actions
     in view (~370px of chrome inside the 94vh box) */
  max-height: max(320px, calc(94vh - 370px));
  max-height: max(320px, calc(94dvh - 370px));
  display: block;
  pointer-events: none;
}
.crop-rect {
  position: absolute;
  border: 2px dashed var(--burgundy);
  box-shadow: 0 0 0 9999px rgb(var(--paper-rgb) / .72);  /* dims everything outside */
  cursor: move;
  display: none;                    /* selection is a tool: click to arm */
}
.crop-overlay.armed .crop-rect { display: block; }
.crop-handle {
  position: absolute;
  width: 14px; height: 14px;
  background: var(--burgundy);
  border: 2px solid var(--paper);
  border-radius: 50%;
}
.crop-handle[data-h="nw"] { top: -8px;  left: -8px;  cursor: nwse-resize; }
.crop-handle[data-h="ne"] { top: -8px;  right: -8px; cursor: nesw-resize; }
.crop-handle[data-h="sw"] { bottom: -8px; left: -8px;  cursor: nesw-resize; }
.crop-handle[data-h="se"] { bottom: -8px; right: -8px; cursor: nwse-resize; }

.crop-instr-row {
  margin-top: var(--space-md);
  padding: .65rem .85rem;
  background: var(--paper-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-md);
}
.crop-instr-label {
  display: block;
  font-family: var(--heading);
  font-size: var(--text-xl); font-weight: 600;
  color: var(--ink);
  margin-bottom: var(--space-xs);
}
.crop-instr {
  display: block;
  width: 100%;
  padding: .65rem var(--space-md);
  font-family: var(--body); font-size: var(--text-lg);
  color: var(--ink);
  background: white;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.crop-instr:focus { outline: none; border-color: var(--cocoa); background: var(--focus-bg); }
/* Same flush 1px ring as the shared .input (see system.css): this is the one
   text field that does not carry the .input class. */
.crop-instr:focus-visible { outline: 1px solid var(--cocoa); outline-offset: 0; }
.crop-instr::placeholder { color: var(--ink-faded); font-style: italic; }
.crop-instr-hint {
  margin-top: var(--space-xs);
  font-size: var(--text-sm); font-style: italic;
  color: var(--ink-faded);
}
.crop-actions {
  display: flex; justify-content: flex-end; gap: var(--space-sm);
  margin-top: .9rem;
}


/* --------------------------------------------------------------------------
   FOCUS-VISIBLE (page-local controls; the shared ones live in system.css)
   -------------------------------------------------------------------------- */

.input-tab:focus-visible,
.upload-zone:focus-within,
.crop-close:focus-visible,
.upload-preview-btn:focus-visible,
.adv-toggle:focus-visible {
  outline: 2px solid var(--cocoa);
  outline-offset: 2px;
}


/* --------------------------------------------------------------------------
   RESPONSIVE: sidebar-to-drawer (820px) and small phones (480px)
   -------------------------------------------------------------------------- */

@media (max-width: 820px) {
  /* Main layout: stage only, sidebar is a bottom drawer in both states. */
  .main, body.has-result .main { grid-template-columns: 1fr; grid-template-rows: 1fr; }

  .sidebar {
    position: fixed; bottom: 0; left: 0; right: 0;
    max-height: 75vh; height: auto;
    border-right: none;
    border-top: 1px solid var(--line-soft);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 24px rgb(var(--ink-rgb) / .15);
    z-index: var(--z-drawer);
    transform: translateY(100%);
    transition: transform var(--dur-slow) var(--ease);
  }
  .sidebar.open { transform: translateY(0); }
  /* The grab handle: the conventional sign that the sheet can be pulled down to
     close (app.js `bindSidebarSwipe`). Decoration only — the whole panel takes
     the gesture, not just this strip. */
  .sidebar::before {
    content: ''; flex-shrink: 0;
    width: 40px; height: 4px;
    margin: var(--space-sm) auto 0;
    border-radius: var(--radius-pill);
    background: var(--line);
  }
  /* The drawer is only reachable once there is something to tune. */
  body.has-result .sidebar-toggle { display: block; }

  /* Bottom padding clears the floating stack, which on a phone is taller than
     on desktop: the drawer pill (1rem) + the action row (4.4rem) + the zoom row
     (7.2rem, ~2.4rem tall) all sit over the canvas. */
  .canvas { padding: var(--space-lg) var(--space-md) 10rem; }
  .canvas-idle { max-width: 100%; }
  /* Two per row on a phone: the flex-basis forces the wrap, the row stays centred. */
  .hero-tiles { gap: var(--space-sm); }
  .hero-tiles .tile { flex-basis: 40%; }
  /* Quieter on a phone, where the tiles are two per row and the panel would
     otherwise be the loudest thing under them: one step down in size, tighter
     padding, and a narrower measure so it wraps to two short lines instead of
     running the full width of the viewport. */
  .mode-note {
    font-size: var(--text-md);
    padding: .4rem .7rem;
    max-width: 30ch;
    margin-bottom: var(--space-lg);
  }
  .hero-input { padding: .9rem; }
  /* Above the drawer pill, not under it: `.sidebar-toggle` ("Parametri") is
     FIXED at bottom:1rem and about 2.6rem tall, so the old .8rem put the
     download row directly beneath it — the pill covered the buttons. */
  .stage-actions { bottom: 4.4rem; gap: var(--space-xs); }
  .stage-actions-sep { display: none; }
  .src-thumb { width: 56px; height: 56px; bottom: 8.4rem; }
  .src-thumb.big { width: min(70vw, 320px); height: min(70vw, 320px); }

  /* Full-screen editor: tools on top, the image in the room that is left, the
     touch-up and the actions pinned at the bottom. Nothing has to scroll, which is
     the point: the stage is `touch-action: none` (it takes the selection drags),
     so a swipe that starts on the image can never scroll the box — a popup taller
     than the screen could leave the image covering everything visible and
     Confirm out of reach. */
  .crop-overlay { padding: 0; }
  .crop-box {
    display: flex; flex-direction: column;
    width: 100%;
    height: 100vh; height: 100dvh;
    max-height: none;
    padding: var(--space-md);
    border: none; border-radius: 0;
  }
  .crop-head p { font-size: var(--text-sm); margin-bottom: var(--space-sm); }
  .crop-tools { margin-bottom: var(--space-sm); }
  /* The room: whatever the other rows leave. `container-type: size` lets the image
     be capped at it (cqw/cqh) by the same fit-then-pin step as on desktop. The
     min-height is a floor for very short screens: there the box scrolls instead,
     and a 160px room is still well under what is visible, so some of the box is
     always free of the image for a swipe to start on. */
  .crop-stage-wrap {
    flex: 1; min-height: 160px;
    display: flex; align-items: center; justify-content: center;
    container-type: size;
  }
  .crop-stage img { max-width: calc(100cqw - 2px); max-height: calc(100cqh - 2px); }
  .crop-instr-row { margin-top: var(--space-sm); padding: .5rem .7rem; }
  .crop-instr-label { font-size: var(--text-md); }
  .crop-instr { padding: .5rem var(--space-sm); }
  .crop-instr-hint { font-size: var(--text-xs); }
  .crop-actions { margin-top: var(--space-sm); }
  .crop-handle { width: 18px; height: 18px; }  /* bigger touch targets */
  .crop-handle[data-h="nw"] { top: -10px;  left: -10px; }
  .crop-handle[data-h="ne"] { top: -10px;  right: -10px; }
  .crop-handle[data-h="sw"] { bottom: -10px; left: -10px; }
  .crop-handle[data-h="se"] { bottom: -10px; right: -10px; }
}

/* Below 820px the hero is the whole viewport rather than a column beside the
   sidebar, so the title scales off a much larger share of the width - 3.2vw
   would read as body text on a phone. */
@media (max-width: 820px) {
  .canvas-idle h2 { font-size: min(6.5vw, 2.4rem); }
  /* Same trap, same fix: 2.6vw is 10px on a 390px phone, so the loader headline
     came out smaller than the step line under it. */
  .loading-title { font-size: min(5.6vw, 1.7rem); }

  /* The preview card is full-width on a phone (max-width: 100% - 2rem), so the
     2D/3D switch pinned to the same `top` landed on top of it. Move the switch
     below the card instead of beside it. */
  /* 9.6rem left only 0.4px between this and the zoom row below (which ends at
     7.2rem + 38px): the two groups read as one block. 10.2rem gives ~10px — a
     visible seam without pushing the switch away from the stage corner. */
  .view-tabs { top: auto; bottom: 10.2rem; right: var(--space-md); }
}

@media (max-width: 480px) {
  .sidebar-toggle { font-size: var(--text-md); padding: var(--space-sm) 1.2rem; }
}
