/**
 * Your Cover Story: styles for the station and the mobile app.
 *
 * File structure:
 *   1. Design tokens and base elements
 *   2. Role visibility (station vs. guest)
 *   3. Desktop layout and components
 *   4. Dialogs and toast
 *   5. Responsive layouts, from general to specific
 *
 * Layout model:
 *   - 1200 px and up: three columns (photos, preview, controls).
 *   - Below 1200 px: a preview plus a bottom "dock" with Photo/Cover/Name steps.
 *     The active step comes from `.layout[data-step]`, set in app.js.
 *   - Landscape below 1200 px: the dock moves to the right of the preview.
 *
 * Mobile rules used throughout:
 *   - The page itself never scrolls, so dragging the photo can't move the page.
 *   - Inputs are at least 16px, because iOS zooms into smaller ones on focus.
 *   - Hover styles are gated behind `(hover: hover)` so they don't stick on touch.
 *   - `env(safe-area-inset-*)` keeps content clear of the notch and home bar.
 */

/* ==========================================================================
   1. Design tokens and base elements
   ========================================================================== */

:root {
  --bg: #0d0c0b;
  --panel: #161513;
  --line: #2a2825;
  --ink: #f3efe8;
  --muted: #9b958c;
  --hot: #ff4fa3;
  --ok: #7bd88f;
  --radius: 14px;
  /* Device safe areas (notch, home indicator) */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html {
  height: 100%;
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  margin: 0;
  height: 100vh;
  height: var(--app-h, 100dvh); /* --app-h tracks the on-screen keyboard (app.js) */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 Inter, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
input, select { font-size: 16px; } /* Prevents iOS zoom on focus */
button, a, label { touch-action: manipulation; }
button, .ghost, .pick-btn, .step, .tpl, .photo {
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
button, .ghost, .pick-btn { cursor: pointer; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ==========================================================================
   2. Role visibility
   app.js adds .is-station (the Mac) or .is-guest (network devices) to <body>.
   ========================================================================== */
body:not(.is-station) .station-only { display: none !important; }
body:not(.is-guest) .guest-only { display: none !important; }

/* Visually hidden file input. Unlike display:none, it stays keyboard-focusable. */
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; pointer-events: none; }

/* ==========================================================================
   3. Desktop layout and components
   ========================================================================== */

/* Header */
.top {
  flex: none;
  height: calc(64px + var(--safe-t));
  padding: var(--safe-t) max(24px, var(--safe-r)) 0 max(24px, var(--safe-l));
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 16px; min-width: 0; }
.brand-mark { font: italic 500 26px/1 'Bodoni Moda', serif; letter-spacing: -.01em; white-space: nowrap; }
.brand-sub { font-size: 11px; letter-spacing: .24em; text-transform: uppercase; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-right { display: flex; align-items: center; gap: 12px; flex: none; }
.stat { color: var(--muted); font-size: 13px; white-space: nowrap; }
.stat b { color: var(--ink); font-variant-numeric: tabular-nums; }
.ip-chip {
  display: inline-flex; align-items: center; gap: 7px; min-height: 36px; padding: 6px 13px;
  border: 1px solid var(--line); border-radius: 99px; background: #1b1a18; font-size: 13px;
  transition: transform 100ms var(--ease-out), border-color 150ms ease;
}
.ip-chip span { color: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .12em; }
.ip-chip b { font-weight: 600; font-variant-numeric: tabular-nums; }
.ip-chip:active { transform: scale(.97); }

/* Three-column layout. `.dock` only becomes a real box on smaller screens. */
.layout {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 300px minmax(0, 1fr) 360px; grid-template-rows: minmax(0, 1fr);
}
.dock { display: contents; }
.steps { display: none; }
.stage { grid-column: 2; grid-row: 1; }
.inbox { grid-column: 1; grid-row: 1; }
.controls { grid-column: 3; grid-row: 1; }

.panel { padding: 20px; overflow-y: auto; overscroll-behavior: contain; }
.inbox { border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.controls {
  padding: 20px; overflow-y: auto; overscroll-behavior: contain;
  border-left: 1px solid var(--line); display: flex; flex-direction: column; gap: 22px;
}
.pane { display: flex; flex-direction: column; gap: 8px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; }
h2 {
  margin: 8px 0 6px; font-size: 12px; font-weight: 700; letter-spacing: .2em; text-transform: uppercase; color: var(--muted);
  display: flex; align-items: center; gap: 10px;
}
.num {
  width: 22px; height: 22px; border-radius: 50%; border: 1px solid var(--line); flex: none;
  display: inline-grid; place-items: center; font-size: 11px; letter-spacing: 0; color: var(--ink);
}

/* Incoming photo grid (station) */
.photo-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.photo {
  position: relative; aspect-ratio: 3/4; border-radius: 10px; overflow: hidden; border: 2px solid transparent;
  background: #222; cursor: pointer; padding: 0;
  transition: transform 120ms var(--ease-out), border-color 150ms ease;
}
.photo:active { transform: scale(.97); }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo.selected { border-color: var(--ink); }
.photo .badge {
  position: absolute; top: 6px; left: 6px; background: var(--hot); color: #fff; font-size: 10px; font-weight: 800;
  letter-spacing: .12em; padding: 3px 7px; border-radius: 99px;
}
.photo .time { position: absolute; bottom: 6px; right: 8px; font-size: 11px; color: #fff; text-shadow: 0 1px 4px #000; }
.photo.fresh { animation: arrive .5s var(--ease-out); }
@keyframes arrive { from { opacity: 0; transform: translateY(-8px) scale(.96); } }
.empty { color: var(--muted); font-size: 13px; padding: 16px 4px; }
.empty b { color: var(--ink); }
.empty p { margin: 0 0 8px; }
.watch { margin: auto 0 0; padding-top: 14px; font-size: 11px; color: #7b766e; word-break: break-all; }

/* Take or choose a photo (guest) */
.pick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.pick .fine { grid-column: 1 / -1; margin: 2px 2px 0; }
.pick-btn {
  position: relative; min-height: 52px; padding: 10px 14px; display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--line); border-radius: 14px; background: #1f1d1b; font-weight: 700; text-align: center;
  transition: transform 100ms var(--ease-out), background 120ms ease;
}
.pick-btn:active { transform: scale(.97); }
.pick-main { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.pick-btn:focus-within { outline: 2px solid var(--ink); outline-offset: 2px; }

/* Cover preview */
.stage { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; gap: 14px; min-width: 0; min-height: 0; }
.canvas-wrap {
  position: relative; flex: 1; min-height: 0; width: 100%;
  display: flex; align-items: center; justify-content: center;
  container-type: size; /* Lets #preview size itself with cqw/cqh units */
}
#preview {
  display: block; max-width: 100%; max-height: 100%;
  width: min(100cqw, 80cqh); height: auto; aspect-ratio: 4 / 5; /* Largest 4:5 box that fits */
  border-radius: 4px; box-shadow: 0 24px 60px rgba(0,0,0,.55);
  cursor: grab; background: #222;
  touch-action: none; /* app.js handles drag and pinch itself */
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
#preview:active { cursor: grabbing; }
.drop-hint {
  position: absolute; inset: 0; display: none; place-items: center; border: 2px dashed var(--ink);
  border-radius: var(--radius); background: rgba(13,12,11,.75); font-size: 18px; font-weight: 600;
}
body.dragging .drop-hint { display: grid; }
.stage-tools { flex: none; display: flex; align-items: center; gap: 14px; color: var(--muted); font-size: 13px; }
body:not(.has-photo) .stage-tools { visibility: hidden; }
.hint-touch { display: none; }
input[type=range] { accent-color: var(--ink); width: 180px; }
@media (pointer: coarse) {
  .hint-mouse { display: none; }
  .hint-touch { display: inline; }
  input[type=range] { display: none; }
}

/* Cover template picker */
.templates { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tpl {
  min-width: 0; padding: 0; border: 2px solid transparent; border-radius: 10px; background: none; cursor: pointer; text-align: left;
  transition: border-color 150ms ease, transform 120ms var(--ease-out);
}
.tpl:active { transform: scale(.97); }
.tpl canvas { width: 100%; height: auto; aspect-ratio: 4/5; display: block; border-radius: 8px; }
.tpl-name { display: block; padding: 6px 4px 2px; font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.tpl small { display: block; padding: 0 4px 6px; font-size: 11px; color: var(--muted); }
.tpl .short { display: none; }
.tpl.selected { border-color: var(--ink); }

/* Name field */
input[type=text] {
  width: 100%; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line); background: #0f0e0d;
  font-size: 18px; outline: none;
}
input[type=text]:focus { border-color: var(--ink); }

/* Buttons */
.cta { display: flex; flex-direction: column; gap: 6px; }
.primary {
  min-height: 52px; padding: 14px 20px; border: 0; border-radius: 99px; background: var(--ink); color: var(--bg);
  font-weight: 700; font-size: 16px; letter-spacing: .02em;
  transition: transform 100ms var(--ease-out), opacity 150ms ease;
}
.primary:active { transform: scale(.98); }
.primary:disabled { opacity: .35; cursor: not-allowed; transform: none; }
.ghost {
  display: inline-flex; align-items: center; justify-content: center; min-height: 40px; padding: 9px 16px; border-radius: 99px;
  border: 1px solid var(--line); background: none; text-decoration: none; font-size: 14px; color: var(--ink);
  transition: transform 100ms var(--ease-out), background 120ms ease;
}
.ghost:active { transform: scale(.97); background: #24221f; }
.ghost.small { min-height: 34px; padding: 6px 13px; font-size: 13px; position: relative; }
.fine { font-size: 12px; color: var(--muted); margin: 4px 0 0; }
@media (pointer: coarse) {
  .ghost, .ghost.small, .ip-chip { min-height: 44px; }
}
@media (hover: hover) and (pointer: fine) {
  .ghost:hover, .ip-chip:hover { background: #24221f; }
  .pick-btn:not(.pick-main):hover { background: #282623; }
  .tpl:not(.selected):hover, .photo:not(.selected):hover { border-color: #55514b; }
}

/* ==========================================================================
   4. Dialogs and toast
   ========================================================================== */
dialog {
  border: 1px solid var(--line); border-radius: 20px; background: var(--panel); color: var(--ink); padding: 0;
  max-width: min(980px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  overflow: auto; overscroll-behavior: contain;
}
dialog::backdrop { background: rgba(0,0,0,.72); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
/* Result dialog */
.result-inner { display: grid; grid-template-columns: minmax(0, 1fr) 380px; }
.result-photo { background: #000; min-width: 0; display: flex; align-items: center; justify-content: center; }
/* The callout stays enabled: long-press is how iOS saves the image to Photos. */
.result-photo img { display: block; width: 100%; height: auto; max-height: calc(100dvh - 34px); object-fit: contain; -webkit-touch-callout: default; }
.result-side { padding: 28px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.eyebrow { margin: 0; font-size: 11px; letter-spacing: .22em; text-transform: uppercase; color: var(--muted); }
.result h3 { margin: 0 0 10px; font: italic 500 28px/1.15 'Bodoni Moda', serif; }
#qr { display: block; width: 100%; max-width: 300px; aspect-ratio: 1; background: #fff; border-radius: 14px; }
.url { font-size: 12px; color: var(--muted); word-break: break-all; margin: 4px 0 0; }
.save-tip { margin: 0; color: #d9d4cc; font-size: 14px; line-height: 1.5; }
.save-tip b { color: #fff; }
.stamp {
  margin-top: 12px; align-self: flex-start; padding: 8px 14px; border-radius: 8px; border: 1.5px dashed var(--hot); color: var(--hot);
  font-size: 13px; font-weight: 700; letter-spacing: .04em; transform: rotate(-2deg);
}
.result-actions { margin-top: auto; padding-top: 18px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.result-actions .primary { flex: 1; }
.result-actions .ghost { min-height: 52px; }
/* On a guest device, saving the cover is the primary action. */
body.is-guest #downloadLocal { background: var(--ink); color: var(--bg); border-color: var(--ink); font-weight: 700; font-size: 16px; }
body.is-guest #nextBtn { background: none; color: var(--ink); border: 1px solid var(--line); font-weight: 500; font-size: 14px; }

/* Settings dialog */
.settings form { padding: 24px; width: min(600px, calc(100vw - 32px)); display: flex; flex-direction: column; gap: 16px; }
.settings-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.settings h3 { margin: 0; font: italic 500 26px/1.1 'Bodoni Moda', serif; }
.conn {
  display: grid; grid-template-columns: 148px minmax(0, 1fr); gap: 12px 18px; align-items: center;
  padding: 16px; border: 1px solid #4b4740; border-radius: 16px; background: #1d1b19;
}
#appQr { display: block; width: 148px; height: 148px; background: #fff; border-radius: 10px; }
.conn-info { min-width: 0; }
.ip-big {
  margin: 4px 0 0; font-size: clamp(27px, 7vw, 38px); line-height: 1.1; font-weight: 700; letter-spacing: -.02em;
  font-variant-numeric: tabular-nums; -webkit-user-select: all; user-select: all; overflow-wrap: anywhere;
}
.conn-meta { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.conn .url { color: #d9d4cc; font-size: 13px; }
.conn-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.conn-note { grid-column: 1 / -1; margin: 0; }
.conn.no-net #appQr { visibility: hidden; }
.row { display: flex; gap: 10px; align-items: center; font-size: 14px; }
.row input { width: 20px; height: 20px; flex: none; accent-color: var(--ink); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
select { min-height: 44px; padding: 10px 12px; border-radius: 10px; background: #0f0e0d; border: 1px solid var(--line); color: var(--ink); }
.capture-box { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 16px; align-items: center; padding: 14px; border: 1px solid var(--line); border-radius: 14px; }
.capture-box p { margin: 0 0 4px; font-size: 14px; }
.capture-box .fine, .capture-box .url { font-size: 12px; }
#captureQr { display: block; width: 110px; height: 110px; background: #fff; border-radius: 10px; }
.danger { padding: 14px; border: 1px solid #5a2323; border-radius: 14px; display: flex; flex-direction: column; gap: 10px; }
.danger p { margin: 0; font-size: 14px; }
.danger-btn { align-self: flex-start; min-height: 44px; padding: 9px 18px; border-radius: 99px; border: 0; background: #b3261e; color: #fff; font-weight: 700; }

/* Toast */
.toast {
  position: fixed; z-index: 10; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); white-space: nowrap;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 99px; font-weight: 600; font-size: 14px;
  transition: opacity .2s ease, transform .2s var(--ease-out);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ==========================================================================
   5. Responsive layouts
   Order matters: later, more specific blocks override earlier ones.
   ========================================================================== */

/* Phones and tablets: preview on top, step dock at the bottom. */
@media (max-width: 1199px) {
  .top {
    height: calc(52px + var(--safe-t));
    padding-left: max(16px, var(--safe-l)); padding-right: max(16px, var(--safe-r));
  }
  .brand-mark { font-size: 22px; }
  .brand-sub, .stat { display: none; }
  .top-right { gap: 8px; }
  .ip-chip, .top-right .ghost { min-height: 38px; padding: 5px 11px; font-size: 13px; }

  .layout { display: flex; flex-direction: column; }
  .stage { flex: 1; padding: 10px max(16px, var(--safe-r)) 4px max(16px, var(--safe-l)); gap: 4px; }
  .stage-tools { min-height: 36px; font-size: 12px; gap: 10px; }
  .stage-tools .ghost.small { min-height: 32px; padding: 4px 12px; }

  .dock {
    display: flex; flex-direction: column; flex: none; min-height: 0;
    padding: 0 max(16px, var(--safe-r)) calc(12px + var(--safe-b)) max(16px, var(--safe-l));
    background: var(--panel); border-top: 1px solid var(--line); border-radius: 18px 18px 0 0;
  }
  .steps {
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px;
    margin: 8px 0; padding: 3px; border-radius: 12px; background: #0f0e0d;
  }
  .step {
    display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 0 4px;
    border: 0; border-radius: 9px; background: none; color: var(--muted); font-size: 13px; font-weight: 700;
    transition: background 150ms ease, color 150ms ease, transform 100ms var(--ease-out);
  }
  .step .num { width: 19px; height: 19px; font-size: 10px; color: inherit; border-color: currentColor; }
  .step:active:not(:disabled) { transform: scale(.97); }
  .step.active { background: var(--ink); color: var(--bg); }
  .step:disabled { opacity: .4; cursor: default; }

  .controls { display: contents; }
  .inbox, .pane { display: none; border: 0; padding: 0; min-height: 0; }
  .inbox h2, .pane h2 { display: none; }
  .layout[data-step="photo"] .inbox { display: block; overflow: visible; }
  .layout[data-step="cover"] #paneCover,
  .layout[data-step="name"] #paneName { display: block; }

  /* Step tabs replace the headings; only the station's "Subir" button remains. */
  .panel-head { display: none; }
  body.is-station .layout[data-step="photo"] .panel-head { display: flex; margin-bottom: 6px; justify-content: flex-end; }
  .pick { margin-top: 0; }

  /* Edge-to-edge horizontal strip: negative margins cancel the dock padding. */
  .photo-grid {
    display: flex; gap: 8px; margin: 0 calc(-1 * max(16px, var(--safe-r))) 0 calc(-1 * max(16px, var(--safe-l)));
    padding: 0 max(16px, var(--safe-r)) 2px max(16px, var(--safe-l));
    overflow-x: auto; overscroll-behavior-x: contain; scroll-snap-type: x proximity; scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .photo-grid::-webkit-scrollbar { display: none; }
  .photo { flex: 0 0 72px; scroll-snap-align: start; }
  .photo .time { display: none; }
  .photo .badge { top: 3px; left: 3px; font-size: 8px; padding: 2px 5px; }
  .empty { padding: 4px 2px; font-size: 12px; }
  .empty p:last-child { margin-bottom: 0; }
  .watch { display: none; }

  .templates { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  .tpl .long { display: none; }
  .tpl .short { display: inline; }
  .tpl-name { padding: 4px 2px 0; font-size: 10px; letter-spacing: .04em; text-align: center; }
  .tpl small { padding: 0 2px 3px; font-size: 10px; text-align: center; }

  input[type=text] { min-height: 52px; font-size: 18px; }
  .cta { margin-top: 10px; }
  .cta .fine { display: none; }
  body:not(.has-photo) .cta { display: none; }

  /* Full-screen dialogs */
  dialog {
    width: 100%; height: 100%; max-width: none; max-height: none; margin: 0;
    border: 0; border-radius: 0;
  }
  .result-inner {
    display: flex; flex-direction: column; min-height: 100%;
    padding-top: var(--safe-t);
  }
  .result-photo { flex: 1 1 auto; min-height: 0; padding: 12px 16px 0; background: none; }
  .result-photo img { width: auto; max-width: 100%; max-height: 56dvh; border-radius: 4px; box-shadow: 0 16px 40px rgba(0,0,0,.5); }
  .result-side { flex: none; padding: 14px max(16px, var(--safe-r)) calc(14px + var(--safe-b)) max(16px, var(--safe-l)); }
  .result h3 { font-size: 24px; margin-bottom: 4px; }
  #qr { max-width: 220px; margin: 4px auto 0; }
  .stamp { margin-top: 8px; }
  .result-actions { padding-top: 12px; flex-wrap: nowrap; }
  .result-actions > * { flex: 1 1 0; min-width: 0; text-align: center; }

  .settings form {
    width: 100%; max-width: 640px; margin: 0 auto;
    padding: calc(16px + var(--safe-t)) max(16px, var(--safe-r)) calc(24px + var(--safe-b)) max(16px, var(--safe-l));
  }
  .toast { bottom: auto; top: calc(60px + var(--safe-t)); transform: translate(-50%, -12px); }
  .toast.show { transform: translate(-50%, 0); }
  dialog .toast { top: auto; bottom: calc(16px + var(--safe-b)); transform: translate(-50%, 12px); }
  dialog .toast.show { transform: translate(-50%, 0); }
}

/* Portrait tablets: more breathing room and a narrower control column. */
@media (min-width: 700px) and (max-width: 1199px) and (orientation: portrait) {
  .dock { padding-left: 24px; padding-right: 24px; }
  .steps, .pick, .templates, #nameInput, .cta { width: 100%; max-width: 620px; margin-left: auto; margin-right: auto; }
  .steps { margin-top: 10px; margin-bottom: 10px; }
  .cta { margin-top: 12px; }
  .photo { flex-basis: 96px; }
  .tpl-name { font-size: 11px; }
  .tpl small { font-size: 11px; }
  .result-photo img { max-height: 62dvh; }
}

/* Landscape phones and tablets: preview on the left, controls on the right. */
@media (max-width: 1199px) and (orientation: landscape) {
  .layout { flex-direction: row; }
  .stage { padding: 8px 12px 4px max(12px, var(--safe-l)); }
  .dock {
    width: clamp(300px, 40vw, 400px); height: 100%; flex: none;
    border-top: 0; border-left: 1px solid var(--line); border-radius: 0;
    padding: 0 max(14px, var(--safe-r)) calc(10px + var(--safe-b)) 14px;
  }
  .layout[data-step="photo"] .inbox,
  .layout[data-step="cover"] #paneCover,
  .layout[data-step="name"] #paneName { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
  .photo-grid {
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); margin: 0; padding: 0;
    overflow: visible; scroll-snap-type: none;
  }
  .photo { flex: none; }
  .templates { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .cta { flex: none; margin-top: auto; padding-top: 10px; }

  .result-inner { flex-direction: row; height: 100%; min-height: 0; padding-top: 0; }
  .result-photo { flex: 1 1 0; height: 100%; padding: calc(10px + var(--safe-t)) 10px 10px max(10px, var(--safe-l)); }
  .result-photo img { max-height: 100%; height: 100%; width: auto; }
  .result-side { flex: 0 0 clamp(280px, 40vw, 400px); height: 100%; overflow-y: auto; padding-top: calc(16px + var(--safe-t)); }
  #qr { max-width: 150px; margin-left: 0; }
}

/* Landscape phones: vertical space is scarce, so everything gets tighter. */
@media (max-height: 480px) and (orientation: landscape) {
  .top { height: calc(42px + var(--safe-t)); }
  .brand-mark { font-size: 18px; }
  .ip-chip, .top-right .ghost { min-height: 32px; padding: 3px 10px; font-size: 12px; }
  .steps { margin: 5px 0; }
  .step { min-height: 36px; font-size: 12px; }
  .primary { min-height: 44px; padding: 8px 16px; }
  .pick-btn { min-height: 44px; }
  .pick .fine { display: none; }
  .stage-tools { display: none; }
  input[type=text] { min-height: 44px; padding: 8px 14px; }
  .templates { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  .tpl-name, .tpl small { font-size: 9px; }
  .result h3 { font-size: 20px; }
  .eyebrow, .stamp { display: none; }
  .save-tip { font-size: 13px; }
  .result-actions { padding-top: 8px; }
  .result-actions .ghost, .result-actions .primary { min-height: 44px; padding: 6px 10px; font-size: 14px; }
}

/* Short portrait phones (iPhone SE and similar): prioritize the cover. */
@media (max-height: 660px) and (orientation: portrait) {
  .top { height: calc(44px + var(--safe-t)); }
  .stage { position: relative; padding-top: 6px; padding-bottom: 6px; }
  .stage-tools { position: absolute; right: max(10px, var(--safe-r)); bottom: 10px; min-height: 0; }
  .stage-tools .hint { display: none; }
  .stage-tools .ghost.small { min-height: 32px; padding: 3px 10px; font-size: 12px; background: rgba(13,12,11,.82); }
  .steps { margin: 5px 0; }
  .step { min-height: 40px; font-size: 12px; }
  .tpl-name, .tpl small { display: none; }
  .pick .fine { font-size: 11px; }
  .cta { margin-top: 7px; }
  .primary { min-height: 46px; padding: 10px 18px; }
  input[type=text] { min-height: 46px; padding: 10px 14px; }
  .dock { padding-bottom: calc(8px + var(--safe-b)); }
  .result-photo img { max-height: 46dvh; }
  .result h3 { font-size: 21px; }
  .eyebrow { display: none; }
  .save-tip { font-size: 13px; }
  .result-actions { padding-top: 8px; }
}

/* Phones: compact header and settings. */
@media (max-width: 480px) {
  body.is-station .brand-mark { font-size: 18px; overflow: hidden; text-overflow: ellipsis; }
  .ip-chip span { display: none; }
  .conn { grid-template-columns: 112px minmax(0, 1fr); gap: 10px 12px; padding: 12px; }
  #appQr { width: 112px; height: 112px; }
  .conn .eyebrow { font-size: 10px; letter-spacing: .1em; }
  .ip-big { font-size: clamp(22px, 6.5vw, 27px); }
  .conn .url { font-size: 12px; }
  .conn-actions { flex-wrap: nowrap; gap: 6px; }
  .conn-actions .ghost { flex: 1 1 auto; padding: 4px 8px; font-size: 12px; white-space: nowrap; }
}
/* Very narrow phones: stack the connection panel. */
@media (max-width: 360px) {
  .brand-mark { font-size: 19px; }
  .ip-chip b { font-size: 12px; }
  .conn { grid-template-columns: minmax(0, 1fr); justify-items: center; text-align: center; }
  .conn-actions { justify-content: center; }
  #appQr { width: 150px; height: 150px; }
  .capture-box { grid-template-columns: 84px minmax(0, 1fr); gap: 12px; }
  #captureQr { width: 84px; height: 84px; }
  body.is-guest #downloadLocal { font-size: 14px; padding-left: 8px; padding-right: 8px; white-space: nowrap; }
}

/* Respect the operating system's "reduce motion" setting. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
