:root {
  color-scheme: dark;
  --bg: #121415;         /* app background */
  --panel: #1b1e1f;      /* cards, logo tile */
  --raise: #232728;      /* inputs, rows */
  --line: #2e3335;
  --fg: #e9ecec;
  --muted: #9aa2a4;
  --amber: #f5a623;      /* brand, savings */
  --amber-deep: #c47f12;
  --green: #1fa676;      /* primary action */
  --green-hi: #25b884;
  --red: #e5574f;
  --accent: var(--amber);   /* amber as text or line: darker in the light theme, to stay readable */
  --ok-text: var(--green-hi);
  --err-text: #ffb0ab;
  --well: #0f1112;       /* drop zone */
  --edge-shadow: #0006;
  --thumb-edge: transparent;
  --lift: #000;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}
/* THEME in docker-compose.yml: dark (above), light, or auto, which follows the device */
:root[data-theme="light"] {
    color-scheme: light;
    --bg: #f3f4f4; --panel: #ffffff; --raise: #e9ecec; --line: #d3d8d9; --fg: #1b1e1f; --muted: #5a6466;
    --red: #c7362e; --accent: #a86500; --ok-text: #11795a; --err-text: #a32a23; --well: #fafbfb;
    --edge-shadow: #0002; --thumb-edge: var(--line); --lift: #0005;
}
@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] {
    color-scheme: light;
    --bg: #f3f4f4; --panel: #ffffff; --raise: #e9ecec; --line: #d3d8d9; --fg: #1b1e1f; --muted: #5a6466;
    --red: #c7362e; --accent: #a86500; --ok-text: #11795a; --err-text: #a32a23; --well: #fafbfb;
    --edge-shadow: #0002; --thumb-edge: var(--line); --lift: #0005;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; }

.app { container-type: inline-size; background: var(--bg); color: var(--fg); font-family: var(--sans); font-size: 15px; min-height: 100vh; min-height: 100dvh; }
:where(.app button, .app input) { font: inherit; color: inherit; }
.app button { cursor: pointer; }
.app :focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.top { display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg); z-index: 3; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 1.25rem; letter-spacing: -.01em; min-width: 0; }
.brand svg, .brand img { width: 32px; height: 32px; flex: none; }
.brand img { object-fit: contain; }
.brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand b { color: var(--accent); font-weight: inherit; }
.top .grow { flex: 1; }
.conn { display: inline-flex; flex: none; white-space: nowrap; align-items: center; gap: 7px; font: 12.5px var(--mono); color: var(--muted); }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.conn.on i { background: var(--green); box-shadow: 0 0 0 3px #1fa67633; }
.conn.off i { background: var(--red); box-shadow: 0 0 0 3px #e5574f33; }
.btn { border: 0; border-radius: 9px; padding: 10px 16px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; transition: background .15s; }
.btn:disabled { opacity: .45; cursor: default; }
.btn svg { width: 16px; height: 16px; }
.btn-quiet { background: var(--raise); }
.btn-quiet:hover { background: var(--line); }
.btn-go { background: var(--green); color: #fff; }
.btn-go:hover:not(:disabled) { background: var(--green-hi); }
.btn-del { background: transparent; color: var(--red); border: 1px solid var(--line); }
.btn-del:hover:not(:disabled) { background: #e5574f1a; }
.btn-sm { padding: 7px 12px; font-size: .88rem; }
.icon-btn { width: 38px; height: 38px; padding: 0; }

.main { display: grid; grid-template-columns: minmax(0, 1fr); gap: 20px; padding: 16px; }
/* Shown while an unsafe option is on: hard to overlook, and it cannot be dismissed */
.unsafes { display: grid; gap: 8px; }
.app > .unsafes { padding: 14px 16px 0; }
.unsafe { color: var(--err-text); background: #e5574f1f; border: 1px solid var(--red); border-radius: 9px; padding: 9px 12px; font-size: .88rem; line-height: 1.45; }
.unsafe b { color: var(--red); }
/* Same size as the strong notice, quieter only in its colours */
.unsafe.soft { color: var(--muted); background: none; border-color: #f5a62359; }
.unsafe.soft b { color: var(--accent); }
.login-hint { color: var(--muted); font-size: .85rem; line-height: 1.45; margin: 0; }
.main > * { min-width: 0; }
.h { display: flex; align-items: baseline; gap: 10px; margin: 0 0 10px; font-size: 1rem; font-weight: 650; }
.h .count { font: 12px var(--mono); color: var(--muted); background: var(--raise); border-radius: 999px; padding: 2px 8px; }
.h .spacer { flex: 1; }
.empty { color: var(--muted); font-size: .9rem; margin: 0; }
/* placeholder the size of one file card */
.empty-box { display: grid; place-items: center; text-align: center; min-height: 94px; padding: 14px 16px; border: 2px dashed var(--line); border-radius: 12px; line-height: 1.5; }
.gone { visibility: hidden; }   /* keeps its place, so the heading does not jump */

/* drop panel */
.drop { border: 2px dashed var(--line); border-radius: 14px; background: var(--well); padding: 22px 16px; display: grid; gap: 14px; justify-items: center; text-align: center; transition: border-color .15s; }
.drop.over { border-color: var(--accent); }
.drop .glyph { width: 46px; height: 46px; color: var(--accent); }
.drop .t1 { font-weight: 600; }
.drop .t2 { color: var(--muted); font-size: .88rem; margin-top: 2px; }
.drop .picks { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; }
.drop .picks .btn { padding: 14px 6px; gap: 6px; font-size: .93rem; white-space: nowrap; }
.cam { background: var(--amber); color: #1b1300; }
.cam:hover { background: #ffb83d; }
.only-wide { display: none; }

.staged { display: grid; gap: 6px; }
.staged:empty { display: none; }
.chip { display: flex; align-items: center; gap: 10px; background: var(--panel); border-radius: 9px; padding: 8px 10px; font-size: .9rem; }
.chip svg { width: 18px; height: 18px; color: var(--muted); flex: none; }
.chip .n { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .s { font: 12px var(--mono); color: var(--muted); font-variant-numeric: tabular-nums; }
.chip button { background: none; border: 0; color: var(--muted); padding: 2px 4px; font-size: 1.1rem; line-height: 1; }
.chip button:hover { color: var(--fg); }
/* Reordering: a mouse drags the whole row, a finger the grip, so that the page still scrolls */
.chip .grip { padding: 2px 0; margin-right: -6px; touch-action: none; cursor: grab; }
.chip:only-child .grip { display: none; }
.staged .chip:not(:only-child) { cursor: grab; user-select: none; }
.chip.dragging { box-shadow: 0 6px 18px var(--lift); outline: 1px solid var(--line); position: relative; z-index: 1; }
.staged.sorting, .staged.sorting * { cursor: grabbing; }

.opts { display: grid; gap: 12px; }
.seg-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; font-size: .93rem; }
.seg { display: inline-flex; background: var(--raise); border-radius: 9px; padding: 3px; }
.seg button { border: 0; background: none; padding: 6px 11px; border-radius: 7px; font-size: .85rem; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--panel); color: var(--fg); box-shadow: 0 1px 0 var(--edge-shadow); }
.compress { width: 100%; padding: 13px; font-size: 1rem; }

/* ---------- document scanner: full screen over the page ---------- */
.scanning, .scanning body { overflow: hidden; }
.scan { position: fixed; inset: 0; z-index: 20; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; background: #0b0c0d; color: #e9ecec; font-family: var(--sans); }
/* A long press or a missed tap must not select the photo or open the browser's "save / copy" menu */
.scan, .scan * { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
.scan-stage { touch-action: none; }
.scan[hidden] { display: none; }
.scan button { cursor: pointer; font: inherit; color: inherit; }
.scan-top { display: flex; align-items: center; gap: 12px; padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top)); }
.scan-note { font-size: .88rem; color: #b5bcbe; line-height: 1.35; }
.scan-stage { display: grid; place-items: center; padding: 12px 22px; min-height: 0; }
.scan-pic { position: relative; }
.scan-pic canvas, .scan-pic svg { display: block; }
.scan-pic svg { position: absolute; inset: 0; overflow: visible; touch-action: none; }
.scan-pic polygon { fill: #f5a6231f; stroke: var(--amber); stroke-width: 2.5; stroke-linejoin: round; }
.scan-pic circle { fill: #f5a62333; stroke: var(--amber); stroke-width: 3; cursor: grab; }
.scan-pic circle:active { fill: #f5a62380; cursor: grabbing; }
.scan-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); }
/* Magnifier while a corner is moved; it never takes the touch itself */
.scan-loupe { position: absolute; left: 0; top: 0; border-radius: 50%; border: 3px solid var(--amber); box-shadow: 0 8px 24px #000a; pointer-events: none; z-index: 2; }
.scan-loupe[hidden] { display: none; }
.scan-bar [data-scan-done], .scan-bar [data-scan-use] { flex: 1; min-width: 130px; padding: 13px; }
.scan-bar [data-scan-full], .scan-bar [data-scan-back] { background: #232728; }
.scan-bar [hidden], .scan-pic[hidden], .scan-result[hidden] { display: none; }
/* The straightened page before it is taken */
.scan-result { display: block; border-radius: 4px; box-shadow: 0 8px 28px #000c; }
.compress:disabled { background: var(--raise); color: var(--muted); opacity: 1; }
.alerts { display: grid; gap: 6px; }
.alert { display: flex; gap: 10px; align-items: start; background: #e5574f1a; color: var(--err-text); border-radius: 9px; padding: 8px 10px; font-size: .88rem; line-height: 1.4; overflow-wrap: anywhere; }
.alert span { flex: 1; min-width: 0; }
.alert button { background: none; border: 0; padding: 0 4px; font-size: 1.1rem; line-height: 1.2; }

/* review queue */
.queue { display: grid; gap: 10px; }
.doc { background: var(--panel); border-radius: 12px; padding: 12px; display: grid; grid-template-columns: 52px 1fr; gap: 6px 12px; align-items: start; }
.thumb { grid-row: span 2; width: 52px; height: 66px; border: 0; padding: 0; border-radius: 6px; background: #f3efe6; box-shadow: inset 0 0 0 1px var(--thumb-edge); position: relative; overflow: hidden; }
.thumb::before { content: ""; position: absolute; inset: 12px 9px auto; height: 30px; background: repeating-linear-gradient(#c9c2b3 0 3px, transparent 3px 8px); }
.thumb .pg { position: absolute; right: 3px; bottom: 3px; font: 10px var(--mono); background: #1b1e1fdd; color: #fff; border-radius: 4px; padding: 1px 4px; }
.thumb:hover:not(:disabled) { box-shadow: 0 0 0 2px var(--accent); }
.title { width: 100%; min-width: 0; background: transparent; border: 1px solid transparent; border-radius: 7px; padding: 4px 6px; margin: -4px -6px 0; font-weight: 600; font-size: 1rem; text-overflow: ellipsis; }
.title:hover { border-color: var(--line); }
.title:focus { background: var(--raise); border-color: var(--line); outline: none; }
.meta { grid-column: 2; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.save { color: var(--accent); font-weight: 600; }
.meta .fail { color: var(--err-text); flex-basis: 100%; overflow-wrap: anywhere; }
.acts { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 6px; }
.doc.busy .acts { display: none; }
.prog { grid-column: 2; height: 6px; border-radius: 3px; background: var(--raise); overflow: hidden; margin-top: 4px; }
.prog i { display: block; height: 100%; width: 58%; background: linear-gradient(90deg, var(--amber-deep), var(--amber)); border-radius: 3px; animation: grow 2.4s ease-in-out infinite alternate; }
@keyframes grow { from { width: 30%; } to { width: 78%; } }
.doc.sending { opacity: .55; }

/* sent list */
.sent-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.sent-list li { display: flex; align-items: center; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--line); font-size: .9rem; }
.sent-list .n { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sent-list time { font: 12px var(--mono); color: var(--muted); }
.pill { font: 11.5px var(--mono); border-radius: 999px; padding: 2px 8px; white-space: nowrap; }
.pill.ok { color: var(--ok-text); background: #1fa6761f; }
.pill.wait { color: var(--accent); background: #f5a6231f; }
.pill.err { color: var(--red); background: #e5574f1f; }

.foot { color: var(--muted); font-size: .8rem; line-height: 1.5; margin: 0; padding: 4px 16px 110px; text-align: center; }

/* mobile sticky action bar */
.dock { position: sticky; bottom: 0; z-index: 3; padding: 12px 16px 18px; background: linear-gradient(transparent, var(--bg) 30%); display: flex; gap: 10px; }
.dock .btn { flex: 1; padding: 14px; font-size: 1rem; box-shadow: 0 8px 24px -8px var(--lift); }

/* ---------- wide layout (desktop / tablet landscape) ---------- */
@container (min-width: 820px) {
  /* content stays in a centered column of 1120px; the header line still spans the screen */
  .top, .main, .foot, .unsafes { --side: max(28px, calc(50% - 560px)); }
  .top { padding: 16px var(--side); position: static; }
  .app > .unsafes { padding: 20px var(--side) 0; }
  .main { grid-template-columns: 380px minmax(0, 1fr); gap: 28px; padding: 24px var(--side); align-items: start; }
  .left { position: sticky; top: 24px; display: grid; gap: 16px; }
  .drop { padding: 36px 20px; }
  .drop .picks { grid-template-columns: 1fr; width: auto; }
  .only-narrow { display: none !important; }
  .only-wide { display: inline-flex; }
  .doc { grid-template-columns: 52px 1fr auto; padding: 14px 16px; align-items: center; }
  .acts { grid-column: 3; grid-row: 1 / span 2; display: flex; margin: 0; }
  .prog { grid-column: 2 / 4; }
  .dock { display: none; }
  .foot { padding: 0 var(--side) 28px; }
  .right { display: grid; gap: 24px; }
}
@container (max-width: 819px) { .left, .right { display: grid; gap: 18px; } }
@media (prefers-reduced-motion: reduce) { .prog i { animation: none; } }

/* ---------- login ---------- */
.login { display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 14px; padding: 28px 24px; display: grid; gap: 16px; }
.login-card .brand { font-size: 1.5rem; margin-bottom: 4px; }
.login-card .brand svg, .login-card .brand img { width: 40px; height: 40px; }
.login-card form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; color: var(--muted); font-size: .88rem; }
.field input { width: 100%; background: var(--raise); border: 1px solid var(--line); border-radius: 9px; padding: 11px 12px; color: var(--fg); font-size: 1rem; }
.field input:focus { outline: none; border-color: var(--accent); }
.login-card .btn { padding: 13px; font-size: 1rem; margin-top: 4px; }
.login-err { color: var(--err-text); background: #e5574f1a; border-radius: 9px; padding: 9px 12px; font-size: .9rem; }
