:root {
    --bg: #FFF7ED;
    --panel: #FCEEDD;
    --panel-hi: #F8E4CC;
    --surface2: #FBF2E4;
    --border: #EDD5B0;
    --text: #DB3800;
    --muted: #B86030;
    --accent: #DB3800;
    --accent-h: #BA2F00;
    --success: #15803d;
    --warn: #92400e;
    --danger: #991b1b;
    --radius: 8px;
    /* Bubbly tie-dye rainbow (Belgium-jersey energy) — shared by the global
       Recolor button and the per-photo era chip's border. */
    --recolor-rainbow: linear-gradient(100deg,
        #ff2d55 0%, #ff9500 17%, #ffd60a 34%, #34c759 52%,
        #00c7be 66%, #0a84ff 83%, #bf5af2 100%);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    min-height: 100vh;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
header {
    display: flex;
    align-items: center;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
}
.app-brand {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    color: inherit;
    text-decoration: none;
}
.app-brand:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 2px; }
header h1 { font-size: 18px; font-weight: 600; letter-spacing: -0.3px; }
#status-bar {
    margin-left: auto;
    display: flex;
    gap: 10px;
    font-size: 12px;
    color: var(--muted);
}
.badge {
    padding: 2px 8px;
    border-radius: 12px;
    background: var(--panel-hi);
    border: 1px solid var(--border);
}
.badge.ok  { color: var(--success); border-color: var(--success); }
.badge.off { color: var(--danger);  border-color: var(--danger); }

/* ── Tab bar ─────────────────────────────────────────────────────────────── */
#tab-bar {
    display: flex;
    gap: 2px;
    padding: 0 24px;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
}
.tab-btn {
    background: none;
    border: none;
    color: var(--muted);
    padding: 10px 18px;
    cursor: pointer;
    font-size: 13px;
    font-family: inherit;
    border-bottom: 2px solid transparent;
    transition: color .15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
/* The global `button/.btn { display: inline-flex }` below overrides the UA
   [hidden] rule, so the hidden attribute won't hide buttons without this.
   Restore [hidden] app-wide (conceals experimental tabs + debug buttons). */
[hidden] { display: none !important; }
.tab-btn .badge-count {
    display: inline-block;
    background: var(--accent);
    color: #fff;
    border-radius: 20px;
    padding: 0 6px;
    font-size: 10px;
    font-weight: 700;
    margin-left: 5px;
    vertical-align: middle;
}

/* ── Layout ──────────────────────────────────────────────────────────────── */
/* Tab panels fill remaining viewport height */
#tab-edit  { display: block; }
#tab-export { background: var(--bg); min-height: calc(100vh - 142px); }
main { display: flex; min-height: calc(100vh - 142px); overflow: visible; }

/* ── Canvas section title bar ────────────────────────────────────────────── */
.section-title-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}
.section-title-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .8px;
    color: var(--muted);
    white-space: nowrap;
}
.section-title-heading {
    position: relative;
    display: flex;
    align-items: center;
    gap: 5px;
}
.shortcut-help-button {
    width: 24px;
    height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 2px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}
.shortcut-help-button:hover,
.shortcut-help-button[aria-expanded="true"] { color: var(--accent); background: var(--surface2); }
.shortcut-help-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.shortcut-help-button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; }
.shortcut-help-button .info-dot { fill: currentColor; stroke: none; }
.shortcut-help-popover {
    position: absolute;
    z-index: 50;
    top: calc(100% + 8px);
    left: 0;
    width: min(340px, calc(100vw - 32px));
    max-height: min(560px, calc(100vh - 190px));
    overflow-y: auto;
    padding: 14px 16px;
    background: var(--panel-hi);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 10px 30px rgba(0,0,0,.22);
}
.shortcut-help-popover[hidden] { display: none; }
.shortcut-help-popover::before {
    content: "";
    position: absolute;
    top: -5px;
    left: 76px;
    width: 9px;
    height: 9px;
    transform: rotate(45deg);
    background: var(--panel-hi);
    border-left: 1px solid var(--border);
    border-top: 1px solid var(--border);
}
.shortcut-help-title { margin-bottom: 10px; font-size: 13px; font-weight: 700; color: var(--text); }
.shortcut-help-section { margin-top: 12px; }

/* ── Mark Ready bar ──────────────────────────────────────────────────────── */
.mark-ready-btn {
    display: block;
    width: 100%;
    margin-top: 10px;
    padding: 13px 16px;
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    text-align: center;
    letter-spacing: .2px;
    transition: border-color .15s, color .15s, background .15s;
}
.mark-ready-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(219,56,0,.04);
}
.mark-ready-btn.active {
    border-style: solid;
    border-color: var(--success);
    background: rgba(21,128,61,.07);
    color: var(--success);
}

/* ── Exporter page ───────────────────────────────────────────────────────── */
.exporter-page {
    max-width: 1500px;
    margin: 0 auto;
    padding: 28px 32px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}
.exporter-header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 16px;
    flex-wrap: wrap;
}

/* ── Solitaire stack cards ───────────────────────────────────────────────── */
/* Aligned + adaptive: every batch uses one shared column width (--col-w, set to
   the ~80th-percentile natural cascade width in JS); wide batches tighten to fit
   it. auto-fill wraps into as many equal columns as the screen allows. */
#export-stack-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, var(--col-w, 360px));
    justify-content: start; align-items: start;
    gap: 28px 30px; padding: 12px 4px 28px;
}

/* Wrapper provides room for peeking layers below */
.export-stack {
    position: relative;
    padding-bottom: 14px;
}
/* Layer 2 — peeks from the bottom of the stack */
.export-stack::before {
    content: '';
    position: absolute;
    bottom: 6px;
    left: 6px; right: 6px;
    height: 20px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    z-index: 0;
}
/* Layer 3 — peeks even further below */
.export-stack::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 12px; right: 12px;
    height: 14px;
    background: var(--panel-hi);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    z-index: -1;
}

/* Main card sits on top */
.export-stack-card {
    position: relative;
    z-index: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: transform .15s, box-shadow .15s;
}
.export-stack-card:hover:not(.expanded) {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(0,0,0,.1);
}
.export-stack-card.expanded { cursor: default; box-shadow: 0 2px 8px rgba(0,0,0,.06); }

/* Collapsed header row */
.export-stack-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    gap: 12px;
}
.export-stack-name { font-size: 13px; font-weight: 600; }
.export-stack-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.export-stack-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.export-stack-count {
    background: var(--panel-hi);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 2px 9px;
    font-size: 11px;
    font-weight: 600;
    color: var(--muted);
}
.export-stack-chevron { font-size: 11px; color: var(--muted); transition: transform .2s; }
.export-stack-card.expanded .export-stack-chevron { transform: rotate(180deg); }

/* Expanded crop grid */
.export-stack-body {
    display: none;
    border-top: 1px solid var(--border);
    padding: 14px 16px;
    gap: 14px;
    flex-direction: column;
}
.export-stack-card.expanded .export-stack-body { display: flex; }
.export-crops-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}
.export-crop-thumb {
    max-width: 120px;
    background: transparent;
    border-radius: 4px;
    overflow: hidden;
    flex-shrink: 0;
}
.export-crop-thumb canvas { display: block; max-width: 100%; height: auto; border-radius: 3px; }

/* ── Exporter board: a page of solitaire cascades ────────────────────────── */
.export-board {
    display: flex; flex-wrap: wrap; align-items: flex-start;
    gap: 30px 26px; padding: 10px 4px 28px;
}
/* Batches float — no box, left-aligned, locked to the shared column width. */
/* overflow visible so a focused card's hover overlay (wider than a narrow
   portrait card) isn't clipped; the cascade itself is laid out to fit --col-w. */
.export-column { width: var(--col-w, 360px); box-sizing: border-box; display: flex; flex-direction: column; align-items: flex-start; gap: 8px; overflow: visible; }
.export-col-label { display: flex; flex-direction: row; align-items: baseline; gap: 8px; flex-wrap: nowrap; }
.export-col-name { font-size: 13px; font-weight: 600; color: var(--text); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.export-col-meta { font-size: 11px; color: var(--muted); white-space: nowrap; flex: none; }

/* Cascade: cards peek beside one another; hover raises one to full view. */
.export-cascade { position: relative; margin: 0; }
.export-photo-card {
    position: absolute; top: 0;
    border-radius: 0; overflow: hidden; cursor: pointer;
    border: 1px solid #fff; background: #d8d8d8;
    transition: box-shadow .16s ease, filter .15s;
}
/* Focus = pure z-index reveal (no movement → no hover jitter) + accent ring. */
.export-photo-card.focused {
    z-index: 999;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent);
    filter: brightness(1.04);
}
.export-photo-canvas { display: block; width: 100%; height: 100%; }
.export-photo-cap {
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 2;
    display: flex; justify-content: space-between; align-items: center; gap: 6px;
    padding: 4px 7px; font-size: 10px; font-weight: 600; color: #fff;
    background: linear-gradient(transparent, rgba(0,0,0,.62)); font-variant-numeric: tabular-nums;
    opacity: 0; transition: opacity .15s;   /* only on the hovered/raised photo */
}
.export-photo-card.focused .export-photo-cap { opacity: 1; }
.export-photo-cap .bw { font-size: 8px; font-weight: 800; letter-spacing: .5px; background: rgba(255,255,255,.9); color: #222; border-radius: 3px; padding: 0 4px; margin-left: 4px; }
.export-photo-rot {
    position: absolute; top: 6px; left: 0; right: 0; z-index: 3;
    display: flex; flex-direction: column; align-items: center; gap: 6px;
    opacity: 0; pointer-events: none; transition: opacity .15s;
}
.export-photo-card.focused .export-photo-rot { opacity: 1; pointer-events: auto; }
.export-photo-rot-row { display: flex; gap: 6px; }
.export-photo-rot button {
    width: 26px; height: 26px; border-radius: 999px; border: none; cursor: pointer;
    background: rgba(20,12,8,.5); color: #fff; font-size: 13px; line-height: 1; display: grid; place-items: center;
}
.export-photo-rot button:hover { background: var(--accent); }
/* Per-photo era chip — the SELECT carries only the same translucent fill as the
   rotate buttons (colour appears ONLY in the ring, no rainbow fill). The rainbow
   lives on a masked ring drawn by the wrapper, because a <select> can't host a
   pseudo-element. */
.export-photo-era-wrap {
    position: relative; display: inline-block; border-radius: 999px;
}
.export-photo-era-wrap::before {
    content: ''; position: absolute; inset: 0; border-radius: 999px; padding: 2px;
    background: var(--recolor-rainbow); pointer-events: none;
    -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
            mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
    -webkit-mask-composite: xor; mask-composite: exclude;
}
.export-photo-era {
    display: block; font-size: 10px; font-weight: 700; letter-spacing: .2px; cursor: pointer;
    padding: 3px 17px 3px 10px; max-width: 96px; line-height: 1.3;
    color: #fff; border: none; border-radius: 999px;
    -webkit-appearance: none; appearance: none;
    text-align: center; text-align-last: center;
    background-image:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23fff' stroke-width='1.6' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"),
        linear-gradient(rgba(20,12,8,.5), rgba(20,12,8,.5));
    background-repeat: no-repeat, no-repeat;
    background-position: right 6px center, 0 0;
    background-size: 8px, auto;
}
.export-photo-era:hover { filter: brightness(1.14); }
.export-photo-era option { color: #222; background: #fff; font-weight: 600; }

/* Caption + capture-date fields. Hidden until "Add Caption & Date?" is on
   (the board gets .pd-meta-on). Date sits inline on the filename row; the
   description is a full-width row just below. Month/day carry a grey "1"
   placeholder (the value EXIF pads to). */
.export-col-datefield, .export-col-descfield { display: none; }
.export-photo-meta { display: none; }
.pd-meta-on .export-col-datefield { display: inline-flex; align-items: center; }
.pd-meta-on .export-col-descfield { display: flex; align-items: center; gap: 6px; width: 100%; box-sizing: border-box; }
.pd-meta-on .export-photo-meta { display: flex; }

/* Label spans the full column width so the date box right-aligns to the column
   (cascade) boundary; the filename ellipsises to make room for it. */
.export-col-label { align-self: stretch; align-items: center; }
.export-col-datefield { margin-left: auto; flex: 0 0 auto; }
.export-col-name { max-width: none; min-width: 0; flex: 0 1 auto; }
.export-col-descfield .pd-desc-in { flex: 1 1 auto; width: auto; min-width: 0; }
.export-col-clearmeta {
    flex: 0 0 auto; width: 22px; height: 22px; padding: 0; border-radius: 5px;
    border: 1px solid var(--border); background: var(--panel); color: var(--muted);
    font-size: 11px; line-height: 1; cursor: pointer; display: grid; place-items: center;
}
.export-col-clearmeta:hover { color: #fff; background: var(--accent); border-color: var(--accent); }

.pd-date-row { display: inline-flex; align-items: center; gap: 2px; }
.pd-date-in, .pd-desc-in {
    background: #ececec; border: 1px solid #d8d2c8; border-radius: 5px;
    font-size: 11px; color: #222; box-sizing: border-box; font-family: inherit;
}
.pd-date-in { text-align: center; padding: 2px 1px; }
.pd-date-year { width: 2.9em; }
.pd-date-month, .pd-date-day { width: 1.9em; }
.pd-desc-in { padding: 3px 8px; }
.pd-date-in::placeholder, .pd-desc-in::placeholder { color: #b0a99e; }
.pd-date-in:focus, .pd-desc-in:focus { outline: none; border-color: var(--accent); background: #fff; }
.pd-date-sep { color: var(--muted); font-size: 10px; user-select: none; }
/* inherited (mirroring the batch) → greyed; deliberately-blank caption → "Empty". */
.pd-date-row.inherited .pd-date-in, .pd-desc-in.inherited { color: #b0a99e; }
.pd-desc-in.forced-empty::placeholder { color: #c07a4e; font-style: italic; opacity: 1; }

/* Per-photo override in the dark hover overlay — date row on top, description
   below (stacked), fixed width so portrait and landscape look the same. */
/* Fitted to the photo card: fills its width exactly (wider on landscape,
   a sliver on portrait) and never extends past it. */
.export-photo-meta { flex-direction: column; align-items: stretch; gap: 4px; margin-top: 4px; width: 100%; }
.export-photo-meta .pd-date-row { gap: 1px; justify-content: center; }
.export-photo-meta .pd-date-in, .export-photo-meta .pd-desc-in {
    background: rgba(255,255,255,.94); border: 1px solid transparent; font-size: 9px; color: #222;
}
.export-photo-meta .pd-date-in { padding: 1px 1px; }
.export-photo-meta .pd-date-year { width: 2.7em; }
.export-photo-meta .pd-date-month, .export-photo-meta .pd-date-day { width: 1.7em; }
.export-photo-meta .pd-desc-in { width: 100%; padding: 1px 5px; }
.export-photo-meta .pd-desc-in.inherited { color: #7d7469; }
.export-photo-meta .pd-date-sep { color: #fff; font-size: 8px; }

.export-col-actions { display: flex; align-items: center; gap: 8px; }
.export-col-status { font-size: 11px; color: var(--muted); }
.export-col-edit { font-size: 16px; line-height: 1; color: var(--muted); background: none; border: none; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.export-col-edit:hover { color: var(--text); background: var(--panel-hi); }
.export-col-export {
    font-size: 10.5px; padding: 4px 12px;
    background: rgba(176,152,116,.13); color: var(--muted);
    border: 1px solid rgba(176,152,116,.30); border-radius: 999px;
}
.export-col-export:hover { background: rgba(176,152,116,.24); border-color: rgba(176,152,116,.48); color: var(--text); }
.export-col-export:disabled { opacity: .6; cursor: default; background: rgba(176,152,116,.13); border-color: rgba(176,152,116,.30); color: var(--muted); }
.export-col-export .spinner-sm {
    display: inline-block; vertical-align: -2px; margin-right: 5px;
    width: 10px; height: 10px;
    border: 2px solid rgba(176,152,116,.35); border-top-color: var(--muted);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

/* Fullscreen single-photo zoom (export resolution) */
#photo-lightbox { position: fixed; inset: 0; z-index: 4000; display: none; place-items: center; padding: 28px 28px 96px; background: rgba(20, 12, 8, 0.82); backdrop-filter: blur(3px); }
#photo-lightbox.open { display: grid; }
.photo-lightbox-frame { position: relative; display: grid; place-items: center; min-width: 120px; min-height: 120px; max-width: 94vw; max-height: calc(100vh - 124px); }
.photo-lightbox-frame img { display: block; max-width: 94vw; max-height: calc(100vh - 134px); border: 5px solid #fff; border-radius: 8px; box-shadow: 0 30px 80px -20px rgba(0,0,0,.7); }
#photo-lightbox-close { position: fixed; top: 18px; right: 20px; width: 40px; height: 40px; border-radius: 999px; border: none; cursor: pointer; background: rgba(0,0,0,.45); color: #fff; font-size: 20px; display: grid; place-items: center; }
.photo-lightbox-hint { position: fixed; bottom: 20px; left: 0; right: 0; text-align: center; color: rgba(255,255,255,.75); font-size: 13px; }
.photo-lightbox-nav { position: fixed; top: 50%; transform: translateY(-50%); width: 52px; height: 52px; border-radius: 999px; border: none; cursor: pointer; background: rgba(0,0,0,.45); color: #fff; font-size: 30px; line-height: 1; display: grid; place-items: center; transition: background .15s; }
.photo-lightbox-nav:hover { background: rgba(0,0,0,.7); }
.photo-lightbox-nav:disabled { opacity: .25; cursor: default; }
#photo-lightbox-prev { left: 20px; }
#photo-lightbox-next { right: 20px; }
.photo-lightbox-bar { position: fixed; bottom: 46px; left: 0; right: 0; display: flex; gap: 14px; align-items: center; justify-content: center; color: rgba(255,255,255,.85); font-size: 12px; }
.photo-lightbox-bar .photo-lightbox-count { min-width: 54px; text-align: right; opacity: .8; }
.photo-lightbox-amount { display: flex; align-items: center; gap: 6px; }
.photo-lightbox-amount.is-off { opacity: .4; }
.photo-lightbox-amount select { font-size: 12px; background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.3); border-radius: 5px; color: #fff; padding: 3px 6px; cursor: pointer; }
.photo-lightbox-amount select:disabled { cursor: default; }
.photo-lightbox-amount select option { color: #000; }

/* TEMP/DEV clear-cache button */
#dev-clear-cache {
    position: fixed; bottom: 14px; left: 14px; z-index: 5000;
    font-size: 12px; font-weight: 600; padding: 6px 12px; border-radius: 8px;
    border: 1px dashed var(--muted); background: var(--panel); color: var(--muted);
    cursor: pointer; opacity: .8;
}
#dev-clear-cache:hover { opacity: 1; color: var(--text); border-color: var(--text); }

@media (prefers-reduced-motion: reduce) { .export-photo-card { transition: none; } }

.export-stack-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-wrap: wrap;
}
.export-stack-result { font-size: 12px; }
.export-stack-result a { color: var(--accent); text-decoration: none; }
.export-stack-result a:hover { text-decoration: underline; }
.export-stack-remove { font-size: 11px; color: var(--muted); background: none; border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 10px; cursor: pointer; font-family: inherit; }
.export-stack-remove:hover { color: var(--danger); border-color: var(--danger); }
.export-stack-edit   { font-size: 11px; color: var(--muted); background: none; border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 10px; cursor: pointer; font-family: inherit; }
.export-stack-edit:hover { color: var(--accent); border-color: var(--accent); }

/* ── Scan bubble ready state ─────────────────────────────────────────────── */
.scan-list-item.ready {
    border-color: var(--success);
    color: var(--success);
}

.sidebar {
    width: 260px;
    min-width: 260px;
    border-right: 1px solid var(--border);
    background: var(--panel);
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;
}
.content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 16px;
    overflow: auto;
    gap: 16px;
}

/* ── Tabs visibility ─────────────────────────────────────────────────────── */
.tab-panel { display: none; width: 100%; }
.tab-panel.active { display: contents; }

/* ── Upload zone ─────────────────────────────────────────────────────────── */
#upload-zone {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 32px 16px;
    text-align: center;
    cursor: pointer;
    transition: border-color .2s, background .2s;
    color: var(--muted);
}
#upload-zone:hover,
#upload-zone.dragover {
    border-color: var(--accent);
    background: rgba(74,158,255,.06);
    color: var(--text);
}
#upload-zone svg { display: block; margin: 0 auto 10px; opacity: .5; }
#upload-zone input[type=file] { display: none; }
#upload-zone p { font-size: 13px; }
#upload-zone p.sub { font-size: 11px; color: var(--muted); margin-top: 4px; }


/* ── Section headings ────────────────────────────────────────────────────── */
.section-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .8px;
    color: var(--muted);
    margin-bottom: 6px;
}

/* ── Controls ────────────────────────────────────────────────────────────── */
.control-row {
    display: flex;
    align-items: center;
    gap: 8px;
}
.control-row label { flex: 1; color: var(--muted); font-size: 13px; }

.slider-row { display: flex; flex-direction: column; gap: 4px; }
.slider-row .row-top { display: flex; justify-content: space-between; }
input[type=range] {
    width: 100%;
    accent-color: var(--accent);
    cursor: pointer;
}

/* ── Classifier tab (experimental, isolated, removable) ──────────────────── */
.classifier-page { padding: 24px 32px; max-width: 1400px; margin: 0 auto; }
.classifier-header h2 { font-size: 18px; font-weight: 600; margin-bottom: 4px; }
.classifier-header p {
    font-size: 13px; color: var(--muted); margin-bottom: 14px;
    max-width: 700px;
}
.class-legend {
    display: flex; flex-wrap: wrap; gap: 12px; margin: 12px 0;
    font-size: 12px; color: var(--muted);
}
.class-legend .swatch {
    display: inline-flex; align-items: center; gap: 6px;
}
.class-legend .swatch::before {
    content: ''; width: 12px; height: 12px; border-radius: 2px;
    display: inline-block; border: 1px solid var(--border);
}
.class-legend .bordered_color::before   { background: #3CC83C; }
.class-legend .bordered_bw::before      { background: #00C8FF; }
.class-legend .borderless_color::before { background: #DCDC00; }
.class-legend .borderless_bw::before    { background: #DC3CDC; }
.class-legend .uncertain::before        { background: #808080; }
.class-legend .flatbed::before          { background: #FF8C00; }
.class-legend .main::before             { background: #3CC83C; }
.class-legend .combined::before         { background: #DC3CDC; }
#classifier-summary {
    font-size: 13px; color: var(--text); margin: 10px 0;
    padding: 8px 12px; background: rgba(0,0,0,.03);
    border-radius: var(--radius);
}
#classifier-summary:empty { display: none; }
.classifier-scan-block {
    margin-top: 20px; padding-top: 16px;
    border-top: 1px solid var(--border);
}
.classifier-scan-block:first-child { border-top: none; padding-top: 0; }
.classifier-scan-block h3 {
    font-size: 14px; font-weight: 600; margin-bottom: 4px;
}
.classifier-scan-block .scan-summary {
    font-size: 12px; color: var(--text); margin-bottom: 8px;
}
#classifier-result img {
    max-width: 100%; height: auto; margin-top: 8px;
    border: 1px solid var(--border); border-radius: var(--radius);
}

/* ── Zoom lightbox (Combined / Merged debug panels) ──────────────────────── */
#combined-result img, #merged-result img { cursor: zoom-in; }
.zoom-overlay {
    position: fixed; inset: 0; z-index: 1000;
    background: rgba(20, 16, 10, 0.92);
    display: none; overflow: hidden;
    touch-action: none;
}
.zoom-overlay.open { display: block; }
.zoom-overlay img {
    position: absolute; top: 0; left: 0;
    transform-origin: 0 0;
    max-width: none; user-select: none; -webkit-user-drag: none;
    cursor: grab; image-rendering: pixelated;
}
.zoom-overlay img.dragging { cursor: grabbing; }
.zoom-hint {
    position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
    color: var(--cream, #f5ecd9); background: rgba(0,0,0,0.45);
    font-size: 12px; padding: 6px 12px; border-radius: 999px;
    pointer-events: none; z-index: 1001;
}
.zoom-close {
    position: fixed; top: 14px; right: 18px; z-index: 1001;
    color: #fff; background: rgba(0,0,0,0.45); border: none;
    width: 36px; height: 36px; border-radius: 50%; font-size: 20px;
    cursor: pointer; line-height: 1;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
button, .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: none;
    border-radius: var(--radius);
    padding: 7px 12px;
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    transition: background .15s, opacity .15s;
    text-decoration: none;
}
.btn-default { background: var(--panel-hi); color: var(--text); border: 1px solid var(--border); }
.btn-default:hover { background: var(--panel); }
.btn-default.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-h); }
/* Global Recolor toggle — bubbly rainbow. Full colour when ON (active),
   desaturated when OFF so the on/off state still reads. The shifting
   background-position on hover gives it a lively tie-dye shimmer. */
#btn-recolor-all {
    background: var(--recolor-rainbow); background-size: 165% 100%; background-position: 0 0;
    color: #fff; border: none; font-weight: 700;
    text-shadow: 0 1px 2px rgba(0,0,0,.4);
    transition: background-position .5s ease, filter .2s ease;
}
#btn-recolor-all:hover { background-position: 100% 0; }
#btn-recolor-all:not(.active) { filter: grayscale(.82) opacity(.72); }
/* Orientate-All button in its "done" state — reads as a status, not a CTA. */
#btn-orient-all.is-oriented {
    background: rgba(67,160,71,.12); color: var(--success);
    border-color: var(--success); font-weight: 600; opacity: 1;
}
.btn-danger  { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-danger:hover  { background: rgba(229,57,53,.1); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { opacity: .9; }
button:disabled, .btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-group { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-full { width: 100%; }

/* ── Scan bubbles (sidebar) ──────────────────────────────────────────────── */
#scan-list {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    outline: none;
}
#scan-list-section.kb-focus #scan-list {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
    border-radius: var(--radius);
}
.scan-list-item {
    padding: 3px 9px;
    font-size: 11px;
    cursor: pointer;
    white-space: nowrap;
    color: var(--muted);
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 20px;
    transition: background 0.1s, color 0.1s, border-color 0.1s;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
}
.scan-list-item:hover  { border-color: var(--accent); color: var(--text); }
.scan-list-item.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── Keyboard shortcuts list ─────────────────────────────────────────────── */
.shortcut-list { display: flex; flex-direction: column; gap: 3px; margin-top: 4px; }
.shortcut-row  { display: flex; align-items: baseline; gap: 6px; font-size: 11px; color: var(--muted); }
.shortcut-row .key {
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--surface2); border: 1px solid var(--border);
    border-radius: 3px; padding: 1px 5px; font-size: 10px;
    font-family: monospace; color: var(--text); white-space: nowrap; flex-shrink: 0;
}

.mode-toggle {
    display: flex;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid var(--border);
}
.mode-toggle button {
    flex: 1;
    border-radius: 0;
    background: transparent;
    color: var(--muted);
    padding: 6px 10px;
    border: none;
}
.mode-toggle button.active { background: var(--accent); color: #fff; }

/* ── Checkbox ────────────────────────────────────────────────────────────── */
.check-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 3px 0;
    cursor: pointer;
}
.check-row input { accent-color: var(--accent); cursor: pointer; }
.check-row span { font-size: 13px; }

/* ── Crop count pill ─────────────────────────────────────────────────────── */
#crop-count {
    display: inline-block;
    background: var(--accent);
    color: #fff;
    border-radius: 20px;
    padding: 1px 10px;
    font-size: 12px;
    font-weight: 600;
}

/* ── Canvas area ─────────────────────────────────────────────────────────── */
#canvas-container {
    overflow: hidden;
    background: var(--bg);
    line-height: 0;
    display: none;
    outline: 2px solid transparent;
    outline-offset: 2px;
    transition: outline-color 0.15s;
}
#canvas-container.visible { display: block; }
#canvas-container.kb-focus { outline-color: var(--accent); }
#crop-canvas { display: block; }

/* ── Results grid ────────────────────────────────────────────────────────── */
#results-section { display: none; }
#results-section.visible { display: block; }
.results-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
}
.results-header h3 { font-size: 15px; }
.results-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 12px;
}
.result-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}
.result-card img {
    width: 100%;
    aspect-ratio: 4/3;
    object-fit: cover;
    display: block;
    background: #000;
}
.result-card .card-info {
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.card-title { font-weight: 600; font-size: 13px; }
.card-meta  { font-size: 11px; color: var(--muted); }
.card-meta span { display: inline-block; margin-right: 8px; }
.card-date  { color: var(--warn); }
.card-actions { padding: 0 10px 10px; }

/* ── Batch tab ───────────────────────────────────────────────────────────── */
.batch-form {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    max-width: 600px;
}
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; color: var(--muted); }
.field input[type=text] {
    background: var(--panel-hi);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 8px 10px;
    font-size: 13px;
    font-family: monospace;
    outline: none;
    transition: border-color .15s;
}
.field input[type=text]:focus { border-color: var(--accent); }

#batch-results {
    max-width: 600px;
    display: none;
}
#batch-results.visible { display: block; }
.summary-box {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    margin-bottom: 12px;
}
.summary-box p { margin-bottom: 4px; }
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
th, td {
    padding: 7px 10px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }

/* ── Loading overlay ─────────────────────────────────────────────────────── */
#loading {
    display: none;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 40px;
    color: var(--muted);
}
#loading.visible { display: flex; }
.spinner {
    width: 24px; height: 24px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ───────────────────────────────────────────────────────────────── */
#toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: var(--panel-hi);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 20px;
    font-size: 13px;
    box-shadow: 0 4px 16px rgba(0,0,0,.15);
    transition: transform .25s ease;
    z-index: 100;
    white-space: nowrap;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.error { border-color: var(--danger); color: var(--danger); }
#toast.success { border-color: var(--success); color: var(--success); }

/* ── Divider ─────────────────────────────────────────────────────────────── */
hr { border: none; border-top: 1px solid var(--border); }

/* ── Crop preview panel — horizontal strip below canvas ──────────────────── */
#crop-panel {
    border-top: 1px solid var(--border);
    outline: 2px solid transparent;
    outline-offset: 2px;
    transition: outline-color 0.15s;
    background: var(--panel);
    padding: 10px 0 10px 10px;
    display: none;
    flex-direction: column;
    gap: 8px;
}
#crop-panel.visible { display: flex; }
#crop-panel.kb-focus { outline-color: var(--accent); }

.crop-panel-header {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .8px;
    color: var(--muted);
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
    padding-right: 10px;
}
.panel-loading {
    font-size: 12px;
    color: var(--muted);
    text-align: center;
    padding: 16px 0;
}
/* Cards scroll horizontally */
#crop-panel-list {
    display: flex;
    flex-direction: row;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 6px;
    padding-right: 10px;
}
.crop-panel-card {
    background: var(--panel-hi);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: border-color .15s;
    flex-shrink: 0;
    /* Width hugs the photo — set by the thumbnail canvas, no fixed width */
}
.crop-panel-card:hover  { border-color: var(--muted); }
.crop-panel-card.active { border-color: var(--accent); border-width: 2px; }
.crop-panel-thumb {
    display: block;
    line-height: 0;
    overflow: hidden;
}
.crop-panel-thumb canvas {
    display: block;   /* canvas is sized to the photo exactly — no letterbox */
}
.crop-panel-info {
    padding: 5px 9px 3px;
    width: 0;             /* don't let the text widen the card past the photo */
    min-width: 100%;
    box-sizing: border-box;
}
.crop-panel-label   { font-size: 12px; font-weight: 600; }
.crop-panel-meta    { font-size: 10px; color: var(--muted); margin-top: 2px; }
.crop-panel-bw-badge {
    display: inline-block;
    margin-top: 3px;
    font-size: 9px;
    font-weight: 700;
    letter-spacing: .5px;
    color: var(--ink);
    background: #d8d8d8;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 5px;
}
.crop-panel-controls {
    padding: 4px 8px 6px;
    display: flex;
    gap: 4px;
}
.crop-panel-controls .btn {
    padding: 3px 6px;
    font-size: 13px;
    flex: 1;
}
.crop-panel-color {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    color: var(--muted);
    margin-left: 2px;
    cursor: pointer;
}
.crop-panel-color input { accent-color: var(--accent); cursor: pointer; }

/* ── Scrollbar ───────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ══ Mobile / touch ═══════════════════════════════════════════════════════════
   The editor was built mouse + keyboard first. These rules add a touch-usable
   layout and on-screen controls, gated on `.is-touch` (set from
   matchMedia('(pointer: coarse)')) plus a phone-width query — desktop is
   untouched. Corner dragging is replaced by a joystick, so the canvas can own
   pan/pinch gestures without any drag-vs-scroll ambiguity. */

#mobile-crop-controls { display: none; }
.scan-del-btn { display: none; }

.is-touch #mobile-crop-controls { display: flex; }
.is-touch .shortcut-help-button { display: none; }     /* no keyboard → no shortcuts */
.is-touch #crop-canvas { touch-action: none; }         /* the canvas owns its gestures */
.is-touch .export-photo-card,
.is-touch .scan-list-item,
.is-touch .btn { touch-action: manipulation; }         /* kill 300ms delay / dbl-tap zoom */

/* Scan chips carry a delete ✕ on touch (no keyboard Del). */
.is-touch .scan-list-item {
    display: inline-flex; align-items: center; gap: 7px;
    overflow: visible; padding: 6px 10px; font-size: 12px;
}
.scan-del-btn {
    border: none; background: rgba(0,0,0,.12); color: inherit; padding: 0;
    width: 18px; height: 18px; border-radius: 50%; flex: none;
    font-size: 10px; line-height: 1; cursor: pointer;
    align-items: center; justify-content: center;
}
.is-touch .scan-del-btn { display: inline-flex; }
.scan-list-item.active .scan-del-btn { background: rgba(255,255,255,.3); color: #fff; }

/* ── Mobile crop controls: corner picker · joystick · rotate ──────────────── */
#mobile-crop-controls {
    align-items: center; justify-content: space-between; gap: 14px;
    margin-top: 10px; padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
    user-select: none; -webkit-user-select: none; touch-action: none;
}
.mcc-group { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.mcc-label { font-size: 10px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.mcc-corner { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; width: 58px; height: 58px; }
.mcc-corner-btn {
    position: relative; padding: 0; cursor: pointer;
    border: 1.5px solid var(--border); background: var(--surface2); border-radius: 6px;
}
.mcc-corner-btn.active { border-color: var(--accent); background: rgba(219,56,0,.12); }
.mcc-corner-btn::after {
    content: ''; position: absolute; width: 9px; height: 9px; border: 2px solid var(--muted);
}
.mcc-corner-btn.active::after { border-color: var(--accent); }
.mcc-corner-btn[data-corner="0"]::after { top: 5px; left: 5px; border-right: 0; border-bottom: 0; }
.mcc-corner-btn[data-corner="1"]::after { top: 5px; right: 5px; border-left: 0; border-bottom: 0; }
.mcc-corner-btn[data-corner="3"]::after { bottom: 5px; left: 5px; border-right: 0; border-top: 0; }
.mcc-corner-btn[data-corner="2"]::after { bottom: 5px; right: 5px; border-left: 0; border-top: 0; }

.mcc-joystick {
    position: relative; flex: none; width: 92px; height: 92px; border-radius: 50%;
    background: radial-gradient(circle at 50% 50%, var(--surface2), var(--panel-hi));
    border: 1.5px solid var(--border); touch-action: none;
}
.mcc-joystick.disabled { opacity: .4; pointer-events: none; }
.mcc-thumb {
    position: absolute; top: 50%; left: 50%; width: 38px; height: 38px; margin: -19px 0 0 -19px;
    border-radius: 50%; background: var(--accent); box-shadow: 0 2px 8px rgba(0,0,0,.25);
    transition: transform .08s ease-out;
}
.mcc-joystick.dragging .mcc-thumb { transition: none; }

.mcc-rotate { display: flex; gap: 6px; }
.mcc-rot-btn {
    width: 40px; height: 40px; border-radius: 8px; cursor: pointer;
    border: 1.5px solid var(--border); background: var(--surface2);
    color: var(--text); font-size: 20px; line-height: 1; display: grid; place-items: center;
}
.mcc-rot-btn:active { background: var(--panel-hi); }
.mcc-rotate.disabled { opacity: .4; pointer-events: none; }

/* ── Phone layout ─────────────────────────────────────────────────────────── */
@media (max-width: 720px) {
    header { padding: 10px 14px; }
    header h1 { font-size: 16px; }
    #tab-bar { padding: 0 8px; }
    .tab-btn { padding: 10px 12px; font-size: 12px; }

    /* Sidebar becomes a compact strip stacked above the canvas. */
    main { flex-direction: column; min-height: auto; }
    .sidebar {
        width: auto; min-width: 0; flex-direction: column;
        border-right: none; border-bottom: 1px solid var(--border);
        padding: 12px; gap: 12px;
    }
    #upload-zone { padding: 18px 12px; }
    #scan-list { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
    .content { padding: 12px; }

    /* dvh so the mobile browser's shrinking chrome doesn't leave dead space. */
    #tab-export { min-height: calc(100dvh - 96px); }
    .exporter-page { padding-bottom: env(safe-area-inset-bottom, 0px); }
}

/* `?mobile=1` preview: force the phone layout regardless of viewport width, and
   frame the app to a phone column so it reads as a device in a wide desktop pane.
   Mirrors the ≤720px rules above (a media query and a class can't share one
   selector list). Interaction still needs real/emulated touch — see the JS note. */
body.force-mobile { max-width: 430px; margin: 0 auto; box-shadow: 0 0 0 1px var(--border); }
body.force-mobile header { padding: 10px 14px; }
body.force-mobile header h1 { font-size: 16px; }
body.force-mobile #tab-bar { padding: 0 8px; }
body.force-mobile .tab-btn { padding: 10px 12px; font-size: 12px; }
body.force-mobile main { flex-direction: column; min-height: auto; }
body.force-mobile .sidebar {
    width: auto; min-width: 0; flex-direction: column;
    border-right: none; border-bottom: 1px solid var(--border); padding: 12px; gap: 12px;
}
body.force-mobile #upload-zone { padding: 18px 12px; }
body.force-mobile #scan-list { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
body.force-mobile .content { padding: 12px; }
