/* Breakpoints: desktop >= 1024px (container 1200px, 24px gutters -> 1152px content),
   tablet 640-1023px (one column, settings in 2 columns), mobile < 640px (one column, bottom action bar). */
:root {
  --bg: #fffaf2;
  --surface: #ffffff;
  --surface-2: #fff4e0;
  --text: #2a1f12;
  --muted: #7c6a55;
  --border: #f0e1c8;
  --accent: #f59e0b;        /* amber */
  --accent-2: #ea580c;      /* orange */
  --accent-text: #9a3412;
  --on-accent: #ffffff;
  --ok: #16a34a;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(120, 70, 0, .06), 0 4px 16px rgba(120, 70, 0, .06);
  --radius: 14px;
  --container: 1200px;
  --gutter: 24px;
  --side: 340px;
  --gap: 20px;
  --topbar-h: 68px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17120c;
    --surface: #211a12;
    --surface-2: #2c2217;
    --text: #fbeedd;
    --muted: #b8a58c;
    --border: #3a2e20;
    --accent-text: #fdba74;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); overflow-x: hidden;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h2 { font-size: 16px; margin: 0; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; }
button, input, select { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
small { color: var(--muted); font-size: 12px; display: block; }
[hidden] { display: none !important; }

.container { width: 100%; max-width: var(--container); margin: 0 auto; padding-left: var(--gutter); padding-right: var(--gutter); }

/* ---------------------------------------------------------------- top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: var(--topbar-h); }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { width: 40px; height: 40px; flex: none; }
.brand-text { min-width: 0; }
.brand-name { font-weight: 700; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-tag { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 10px; flex: none; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); font-size: 13px; white-space: nowrap; max-width: 280px; overflow: hidden; text-overflow: ellipsis; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; flex: none; }
.pill.ready .dot { background: var(--ok); animation: none; }
.pill.error .dot { background: var(--bad); animation: none; }
@keyframes pulse { 50% { opacity: .3; } }
.lang { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.lang button { border: 0; background: transparent; padding: 6px 10px; cursor: pointer; font-size: 13px; font-weight: 600; min-width: 40px; }
.lang button.on { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--on-accent); }

/* ---------------------------------------------------------------- layout (desktop) */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) var(--side); gap: var(--gap);
  padding-top: var(--gutter); padding-bottom: 48px; align-items: start; }
.col-main { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }
.col-side { display: flex; flex-direction: column; gap: 12px; position: sticky; top: calc(var(--topbar-h) + 16px);
  max-height: calc(100vh - var(--topbar-h) - 32px); min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 8px; flex-wrap: wrap; }
.count { color: var(--muted); font-weight: 400; }

/* ---------------------------------------------------------------- buttons */
.primary, .ghost, .btn { border-radius: 10px; padding: 9px 16px; cursor: pointer; font-weight: 600; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-size: 14px; min-height: 40px; white-space: nowrap; }
.primary { border: 0; color: var(--on-accent); background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.primary:disabled, .ghost:disabled { opacity: .5; cursor: default; }
.ghost { border: 1px solid var(--border); background: var(--surface); color: var(--accent-text); }
.ghost:hover:not(:disabled) { background: var(--surface-2); }
.sm { padding: 5px 12px; font-size: 13px; min-height: 32px; }
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible, .drop:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- drop zone */
.drop { display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; cursor: pointer;
  padding: 36px 16px; border: 2px dashed var(--accent); border-radius: var(--radius); background: var(--surface-2); transition: background .15s; }
.drop:hover, .drop.over { background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }
.drop strong { font-size: 17px; }
.drop-hint { color: var(--muted); font-size: 13px; }
.drop-icon { width: 36px; height: 36px; color: var(--accent-2); margin-bottom: 6px; }
.drop-buttons { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; justify-content: center; }

/* ---------------------------------------------------------------- file list */
.file-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px;
  max-height: 360px; overflow-y: auto; overscroll-behavior: contain; }
.file-list li { display: flex; align-items: center; gap: 12px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 10px; }
.thumb { width: 56px; height: 40px; object-fit: cover; border-radius: 6px; background: var(--surface-2); flex: none; }
.vthumb { display: grid; place-items: center; font-size: 10px; font-weight: 700; color: var(--accent-text); }
.fname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fname small { overflow: hidden; text-overflow: ellipsis; }
.file-actions { display: flex; gap: 6px; flex: none; }

/* ---------------------------------------------------------------- settings */
.settings { padding: 0; overflow: auto; overscroll-behavior: contain; flex: 1 1 auto; min-height: 0; }
.settings summary { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px; cursor: pointer; list-style: none;
  position: sticky; top: 0; background: var(--surface); z-index: 1; border-radius: var(--radius) var(--radius) 0 0; }
.settings summary::-webkit-details-marker { display: none; }
.chev { width: 20px; height: 20px; color: var(--muted); transition: transform .15s; }
.settings[open] .chev { transform: rotate(180deg); }
.set-grid { display: grid; gap: 0; padding: 0 18px 8px; }
.set-group { padding: 14px 0; border-top: 1px solid var(--border); min-width: 0; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; min-width: 0; }
.field > span { font-weight: 600; font-size: 14px; display: flex; justify-content: space-between; gap: 8px; }
.field b { color: var(--accent-text); }
input[type=text], input[type=number], select { width: 100%; min-height: 40px; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg); }
input[type=range] { accent-color: var(--accent-2); width: 100%; min-height: 28px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--bg); padding: 8px; cursor: pointer; font-weight: 600; min-height: 38px; }
.seg button.on { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--on-accent); }
.toggle { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 12px; cursor: pointer; position: relative; }
.toggle input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.toggle > span { flex: none; width: 38px; height: 22px; border-radius: 999px; background: var(--border); position: relative; transition: .15s; margin-top: 1px; }
.toggle > span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: .15s; }
.toggle input:checked + span { background: var(--accent-2); }
.toggle input:checked + span::after { left: 18px; }
.toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- action bar (card on desktop, fixed bar below 1024px) */
.actionbar { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); flex: none; }
.actionbar-inner { display: flex; flex-direction: column; gap: 10px; padding: 14px; }
.ab-info { display: flex; flex-direction: column; min-width: 0; }
.ab-info b { font-size: 14px; }
.ab-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.ab-buttons .primary { flex: 1 1 auto; }

/* ---------------------------------------------------------------- progress + results (full width, split 50/50) */
.results-card { scroll-margin-top: calc(var(--topbar-h) + 12px); }
.results-head { flex-wrap: nowrap; }
.results-head h2 { min-width: 0; }
.results-head .btn { flex: none; }
.progress { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress div { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width .3s; }
.results-scroll { max-height: max(360px, calc(100vh - var(--topbar-h) - 190px)); overflow-y: auto; overscroll-behavior: contain;
  border: 1px solid var(--border); border-radius: 12px; margin-top: 8px; }
.results { display: flex; flex-direction: column; }
.res { padding: 12px; border-bottom: 1px solid var(--border); min-width: 0; }
.res:last-child { border-bottom: 0; }
.res-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.res-head .fname { font-weight: 600; font-size: 14px; }
.res-tools { display: flex; gap: 6px; align-items: center; flex: none; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.pair figure { margin: 0; min-width: 0; position: relative; }
.pair img, .pair video, .pair .ph { width: 100%; aspect-ratio: 16 / 9; object-fit: contain; border-radius: 8px;
  background: var(--surface-2); display: block; }
.pair img { cursor: zoom-in; transition: box-shadow .15s; }
.pair img:hover { box-shadow: 0 0 0 2px var(--accent); }
.pair .ph { display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.det-frame { position: relative; width: 100%; border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.pair .det-frame > img { position: absolute; inset: 0; width: 100%; height: 100%; aspect-ratio: auto; border-radius: 0; }
.det-layer { position: absolute; inset: 0; pointer-events: none; }
.det-box { position: absolute; border: 2px solid; border-radius: 3px; box-shadow: 0 0 0 1px rgba(255,255,255,.7); }
.det-box.skipped { border-style: dashed; opacity: .6; }
.det-summary { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 0; }
.det-chip { font-size: 12px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--c); color: var(--text);
  background: color-mix(in srgb, var(--c) 12%, transparent); }
.det-chip.none { border-style: dashed; color: var(--bad); }
.pair figcaption { font-size: 12px; color: var(--muted); margin-top: 4px; }
.results-hint { margin: 8px 0 0; }
.used-settings { margin: 0 0 4px; font-size: 13px; color: var(--accent-text); font-weight: 600; }
.update-wrap:has(.update-banner[hidden]) { display: none; }
.update-banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  margin-top: 16px; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--accent);
  background: var(--surface-2); color: var(--accent-text); font-weight: 600; font-size: 14px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.done { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge.error { background: color-mix(in srgb, var(--bad) 15%, transparent); color: var(--bad); }
.badge.processing { color: var(--accent-text); }
.err { color: var(--bad); font-size: 13px; margin: 6px 0 0; word-break: break-word; }

/* ---------------------------------------------------------------- image viewer (zoom, pan, before/after slider) */
.viewer { width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; padding: 0; border-radius: 0;
  background: #120d08; color: #fbeedd; display: none; flex-direction: column; }
.viewer[open] { display: flex; }
.viewer::backdrop { background: rgba(0, 0, 0, .8); }
.vw-bar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: #1d150d; border-bottom: 1px solid #3a2e20; flex-wrap: wrap; }
.vw-title { flex: 1 1 200px; min-width: 0; display: flex; flex-direction: column; }
.vw-title b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.vw-title span { color: #b8a58c; font-size: 12px; }
.viewer .ghost { background: transparent; border-color: #3a2e20; color: #fdba74; }
.viewer .ghost:hover:not(:disabled) { background: #2c2217; }
.vw-modes { grid-template-columns: repeat(3, auto); border-color: #3a2e20; }
.vw-modes button { background: transparent; padding: 6px 12px; min-height: 32px; font-size: 13px; }
.vw-modes button:disabled { opacity: .35; cursor: default; }
.vw-zoom, .vw-actions { display: flex; gap: 6px; align-items: center; }
.vw-pct { min-width: 64px; }
.vw-stage { position: relative; flex: 1; overflow: hidden; cursor: grab; touch-action: none; user-select: none;
  background: repeating-conic-gradient(#1a130c 0% 25%, #211810 0% 50%) 0 0 / 24px 24px; }
.vw-stage.dragging { cursor: grabbing; }
.vw-canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; --s: 1; }
.vw-canvas img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; image-rendering: auto; }
.vw-canvas.pixel img { image-rendering: pixelated; }
.vw-divider { position: absolute; top: 0; bottom: 0; width: calc(2px / var(--s)); margin-left: calc(-1px / var(--s)); background: #fff;
  cursor: ew-resize; box-shadow: 0 0 calc(6px / var(--s)) rgba(0, 0, 0, .6); display: none; }
.vw-divider span { position: absolute; top: 50%; left: 50%; width: calc(40px / var(--s)); height: calc(40px / var(--s));
  transform: translate(-50%, -50%); border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: calc(3px / var(--s)) solid #fff; box-shadow: 0 calc(2px / var(--s)) calc(8px / var(--s)) rgba(0, 0, 0, .5); }
.vw-divider::before { content: ""; position: absolute; inset: 0 calc(-14px / var(--s)); }  /* wider grab area */
.vw-tag { position: absolute; top: calc(10px / var(--s)); padding: calc(3px / var(--s)) calc(8px / var(--s)); border-radius: calc(6px / var(--s));
  background: rgba(0, 0, 0, .6); color: #fff; font-size: calc(12px / var(--s)); font-weight: 600; display: none; white-space: nowrap; }
.vw-tag.left { left: calc(10px / var(--s)); } .vw-tag.right { right: calc(10px / var(--s)); }
.viewer.compare .vw-divider, .viewer.compare .vw-tag { display: block; }
.vw-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 44px; height: 64px; border: 0; border-radius: 10px;
  background: rgba(0, 0, 0, .45); color: #fff; font-size: 34px; line-height: 1; cursor: pointer; z-index: 3; }
.vw-nav:hover:not(:disabled) { background: rgba(0, 0, 0, .7); }
.vw-nav:disabled { opacity: .25; cursor: default; }
.vw-nav.prev { left: 10px; } .vw-nav.next { right: 10px; }
.vw-hint { margin: 0; padding: 6px 14px; font-size: 12px; color: #b8a58c; background: #1d150d; border-top: 1px solid #3a2e20; text-align: center; }

/* ---------------------------------------------------------------- preview dialog */
dialog { border: 0; border-radius: var(--radius); padding: 18px; width: min(960px, calc(100vw - 32px)); max-height: calc(100vh - 32px);
  background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgba(20, 12, 0, .55); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.canvas-wrap { max-height: 70vh; overflow: auto; background: var(--surface-2); border-radius: 8px; }
canvas { width: 100%; display: block; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; margin-top: 10px; color: var(--muted); }
.sw { display: inline-block; width: 12px; height: 12px; border: 3px solid; border-radius: 3px; vertical-align: -2px; margin-right: 6px; }
.sw.ok { border-color: #22c55e; } .sw.skip { border-color: #ef4444; }

/* ---------------------------------------------------------------- tablet: one column, settings in 2 columns, fixed action bar */
@media (max-width: 1023px) {
  :root { --gutter: 20px; --topbar-h: 60px; }
  .layout { display: flex; flex-direction: column; align-items: stretch; padding-bottom: 120px; }
  .col-main, .col-side { display: contents; }   /* let children be ordered individually */
  #drop { order: 1; } #filesCard { order: 2; } #settingsCard { order: 3; } #resultsCard { order: 4; }
  .results-scroll { max-height: max(360px, calc(100vh - var(--topbar-h) - 260px)); }
  .settings { max-height: none; overflow: visible; }
  .settings summary { position: static; }
  .set-grid { grid-template-columns: 1fr 1fr; column-gap: 24px; }
  .actionbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; border-radius: 0; border-width: 1px 0 0;
    box-shadow: 0 -6px 20px rgba(80, 40, 0, .10); padding-bottom: env(safe-area-inset-bottom); }
  .actionbar-inner { flex-direction: row; align-items: center; justify-content: space-between;
    max-width: var(--container); margin: 0 auto; padding: 12px var(--gutter); }
  .ab-buttons { flex-wrap: nowrap; }
  .ab-buttons .primary { flex: none; min-width: 180px; }
}

/* ---------------------------------------------------------------- mobile */
@media (max-width: 639px) {
  :root { --gutter: 16px; --gap: 14px; --topbar-h: 56px; --radius: 12px; }
  body { font-size: 14px; }
  .logo { width: 34px; height: 34px; }
  .brand-name { font-size: 16px; }
  .brand-tag { display: none; }
  .pill { padding: 4px 8px; max-width: 120px; }
  .card:not(.settings) { padding: 14px; }
  .drop { padding: 24px 12px; }
  .drop-hint { font-size: 12px; }
  .set-grid { grid-template-columns: 1fr; padding: 0 14px 4px; }
  .settings summary { padding: 14px; }
  .file-list { max-height: 300px; }
  .file-list li { gap: 8px; }
  .thumb { width: 44px; height: 32px; }
  .pair { gap: 6px; }
  .res { padding: 10px 8px; }
  .results-scroll { max-height: max(320px, calc(100vh - var(--topbar-h) - 250px)); }
  .vw-bar { gap: 8px; padding: 8px 10px; }
  .vw-title { flex-basis: 100%; }
  .vw-actions { margin-left: auto; }
  .vw-nav { width: 36px; height: 52px; font-size: 28px; }
  .vw-hint { display: none; }
  .actionbar-inner { flex-direction: column; align-items: stretch; gap: 8px; padding: 10px var(--gutter); }
  .ab-info { flex-direction: row; justify-content: space-between; gap: 8px; }
  .ab-info #note { display: none; }
  .ab-buttons .primary { flex: 1 1 auto; min-width: 0; }
  .layout { padding-bottom: 140px; }
  dialog { padding: 12px; width: calc(100vw - 16px); }
}
@media (max-width: 380px) {
  .pill .txt { display: none; }       /* keep only the status dot on very small phones */
  .pill { padding: 6px; }
  .thumb { width: 36px; height: 28px; }
}
