:root {
  --bg: #f6f6f4; --panel: #fff; --text: #1d1d1f; --muted: #6b6b70; --line: #e2e2e0;
  --accent: #2459d6; --accent-text: #fff; --ok: #1d8a4a; --warn: #b5560b; --danger: #c62f2f;
  --sel: #e8eefc; --header-h: 58px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #151517; --panel: #1f1f22; --text: #ececee; --muted: #9a9aa2; --line: #33333a;
    --accent: #5b8cff; --accent-text: #0b0b0d; --ok: #4cc27f; --warn: #f0a050; --danger: #ff6b6b;
    --sel: #26304a; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); }

/* ---------------------------------------------------------------- header */
header { display: flex; align-items: center; gap: 16px; padding: 0 20px; height: var(--header-h);
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 3; }
h1 { font-size: 18px; margin: 0; white-space: nowrap; }
#coll-summary { margin-left: auto; font-size: 14px; }
#coll-summary b { color: var(--text); }
#coll-summary { display: flex; gap: 6px; align-items: baseline; }
.sum-value::before { content: "·"; margin-right: 6px; }

/* ---------------------------------------------------------------- layout: collection left, details right */
main { display: grid; grid-template-columns: minmax(0, 1fr) minmax(480px, 40%); gap: 20px; align-items: start;
  max-width: 1400px; margin: 0 auto; padding: 20px 20px 60px; }
#details { position: sticky; top: calc(var(--header-h) + 20px); max-height: calc(100vh - var(--header-h) - 40px);
  overflow-y: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.d-back, .show-m { display: none; }

/* ---------------------------------------------------------------- controls */
button, .button { font: inherit; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); cursor: pointer; text-decoration: none; display: inline-block; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--danger); }
input, select { font: inherit; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); }
a { color: var(--accent); }
.muted { color: var(--muted); } .warn { color: var(--warn); } .ok { color: var(--ok); font-weight: 600; }
.small { font-size: 13px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.qty { width: 70px; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); }
.price { font-variant-numeric: tabular-nums; white-space: nowrap; cursor: help; }
.warn-icon { color: var(--warn); }

/* ---------------------------------------------------------------- collection table */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
#coll-filter { min-width: 200px; }
#coll-status button { min-height: 36px; padding: 6px 12px; }
select { appearance: none; -webkit-appearance: none; padding-right: 30px; cursor: pointer; line-height: 1.45;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 15px center, right 10px center;
  background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }
input, select { min-height: 36px; }
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
#coll-table th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
#coll-table th[data-sort] { cursor: pointer; user-select: none; }
#coll-table th[data-sort]:hover, #coll-table th.sorted { color: var(--text); }
#coll-table th[data-sort]::after { content: attr(data-dir); margin-left: 4px; font-size: 10px; }
#coll-table tbody tr[data-id] { cursor: pointer; }
#coll-table tbody tr[data-id]:hover { background: var(--bg); }
#coll-table tbody tr.sel, #coll-table tbody tr.sel:hover { background: var(--sel); }
#coll-table tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right; }
.thumb { width: 40px; border-radius: 3px; display: block; }
.c-name { font-weight: 600; }
.empty { padding: 30px; text-align: center; }
.status { font-size: 12px; padding: 2px 8px; border-radius: 10px; border: 1px solid var(--line); white-space: nowrap; }
.status.s1 { color: var(--warn); border-color: currentColor; }
.status.s2 { color: var(--ok); border-color: currentColor; }

/* ---------------------------------------------------------------- details panel */
.d-empty { padding: 40px 10px; text-align: center; }
.d-head { display: grid; grid-template-columns: 170px 1fr; gap: 16px; align-items: start; }
.d-head img { width: 100%; border-radius: 8px; display: block; }
.d-info { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.d-title { display: flex; justify-content: space-between; align-items: start; gap: 8px; }
.d-title h2 { margin: 0 0 2px; font-size: 20px; line-height: 1.2; }
button.icon { padding: 4px 8px; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; }
.copy-row { gap: 8px; }
#details label { display: flex; flex-direction: column; gap: 2px; font-size: 13px; color: var(--muted); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; border-right: 1px solid var(--line); padding: 6px 10px; }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--text); color: var(--bg); }
.d-price { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 14px; }
.big-price { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.figs { display: grid; grid-template-columns: auto auto; gap: 0 14px; font-size: 13px; color: var(--muted);
  font-variant-numeric: tabular-nums; }
.figs b { color: var(--text); font-weight: 600; }
#details > .warn { margin-top: 8px; }
#details section { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 10px; }
#details h3 { margin: 0 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.steps { margin: 0; padding-left: 20px; } .steps li { margin: 6px 0; }
.fields { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 8px; margin: 6px 0 4px; }
.fld { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); min-width: 0; }
.fld .copy { text-align: left; font-weight: 600; padding: 4px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------------------------------------------------------- scan popup */
dialog { width: min(980px, calc(100vw - 32px)); max-height: calc(100vh - 48px); padding: 0; border: 1px solid var(--line);
  border-radius: 14px; background: var(--bg); color: var(--text); }
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 1; }
.dlg-head h2 { margin: 0; font-size: 17px; }
.dlg-body { padding: 16px; }
.drop { display: flex; flex-direction: column; gap: 4px; align-items: center; text-align: center;
  padding: 36px 16px; border: 2px dashed var(--line); border-radius: 14px; background: var(--panel); cursor: pointer; }
.drop.over { border-color: var(--accent); }
/* Camera button: only on touch devices (on a desktop, capture is ignored and it would just repeat "Add photos"). */
.camera-btn { display: none; }
@media (pointer: coarse) {
  .camera-btn { display: flex; align-items: center; justify-content: center; gap: 6px; margin-bottom: 10px;
    padding: 14px; font-size: 16px; font-weight: 600; cursor: pointer;
    background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
}
#picked, #scan-status, #scan-actions { margin: 10px 0; }
.det { display: grid; grid-template-columns: 260px 1fr; gap: 16px; padding: 14px; margin: 12px 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.det.added { opacity: .6; }
.imgs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
figure { margin: 0; } figure img { width: 100%; border-radius: 6px; object-fit: contain; max-height: 200px; background: var(--bg); }
figcaption { font-size: 12px; color: var(--muted); text-align: center; }
.noimg { aspect-ratio: 63/88; display: grid; place-items: center; background: var(--bg); border-radius: 6px; color: var(--muted); }
.form { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.form label { display: flex; flex-direction: column; gap: 2px; font-size: 13px; color: var(--muted); }
.form label.inline, .form .inline { flex-direction: row; align-items: center; gap: 6px; color: var(--text); display: flex; }
.form select.cand { width: 100%; }
.conf.high { color: var(--ok); } .conf.medium { color: var(--warn); } .conf.low { color: var(--danger); }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }
details .row { margin-top: 6px; }
/* ---------------------------------------------------------------- narrow screens
   One column: the collection fills the page; tapping a card opens its details full-screen. */
@media (max-width: 960px) {
  main { grid-template-columns: minmax(0, 1fr); padding: 12px 12px 40px; }
  #details { display: none; position: fixed; inset: 0; z-index: 5; max-height: none;
    border: 0; border-radius: 0; padding: 0 16px 40px; }
  body.detail-open #details { display: block; }
  body.detail-open { overflow: hidden; }
  .d-back { display: block; position: sticky; top: 0; z-index: 1; width: calc(100% + 32px); margin: 0 -16px 12px;
    padding: 12px 16px; text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
    background: var(--panel); font-weight: 600; }
}
@media (max-width: 700px) {
  header { gap: 10px; padding: 0 12px; }
  /* Two lines on phones: title + buttons on top, the collection values underneath. */
  header { flex-wrap: wrap; height: auto; gap: 10px 8px; padding: 14px 16px 12px; }
  #open-scan { margin-left: auto; white-space: nowrap; }
  #coll-summary { order: 3; flex: 1 1 100%; margin: 0; padding-top: 10px; border-top: 1px solid var(--line);
    justify-content: space-between; font-size: 14px; line-height: 1.3; }
  .sum-value::before { content: none; }
  main { padding: 16px 16px 40px; }
  .toolbar { gap: 6px; }
  #coll-filter { min-width: 0; flex: 1 1 auto; }
  #coll-filter { flex-basis: 100%; }
  #coll-status { flex: 1 1 100%; display: flex; }
  #coll-status button { flex: 1; }
  /* 16px text: iPhone Safari zooms into smaller inputs when you tap them */
  input, select { font-size: 16px; }
  .toolbar .spacer { display: none; }

  .hide-m { display: none; }
  .show-m { display: block; margin-top: 2px; }
  th, td { padding: 6px; }
  .d-head { grid-template-columns: 110px 1fr; gap: 12px; }
  .d-title h2 { font-size: 18px; }
  .fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .det { grid-template-columns: 1fr; }
  dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border: 0; border-radius: 0; }
}

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text);
  color: var(--bg); padding: 8px 14px; border-radius: 8px; z-index: 9; }

/* ---------------------------------------------------------------- login page */
.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-box { width: min(340px, 100%); display: flex; flex-direction: column; gap: 14px; padding: 24px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.login-box h1 { font-size: 20px; }
.login-box label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-box input { font-size: 16px; }
.login-box p { margin: 0; min-height: 1.2em; font-size: 14px; }
@media (max-width: 700px) { #logout { padding: 7px 9px; font-size: 13px; } }

/* ---------------------------------------------------------------- image-matching scan */
.scanner { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; }
.scanner p { margin: 6px 0; }
.scanner progress { width: 100%; height: 10px; margin: 6px 0 2px; accent-color: var(--accent); }
.sc-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.sc-row .spacer, .row .spacer { flex: 1; }
.sc-opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 8px; margin: 8px 0 10px; }
.sc-opt { display: flex; gap: 8px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--line);
  border-radius: 8px; cursor: pointer; }
.sc-opt:has(input:checked) { border-color: var(--accent); }
.sc-opt input { margin-top: 3px; }
button.link { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; cursor: pointer; font-size: 13px; }

.det { grid-template-columns: minmax(180px, 280px) 1fr; }
.frame-col { display: flex; flex-direction: column; gap: 4px; align-items: center; }
/* The wrapper is exactly the size of the shown photo (no letterbox bars), so the frame's percentages line up. */
.frame-wrap { position: relative; display: inline-block; max-width: 100%; touch-action: none; user-select: none; line-height: 0; }
.frame-wrap img { display: block; width: auto; height: auto; max-width: 100%; max-height: 420px; }
.frame-box { position: absolute; border: 2px solid var(--accent); border-radius: 6px; cursor: move;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45); }
.frame-wrap { overflow: hidden; border-radius: 6px; }
.frame-wrap.locked .frame-box { pointer-events: none; box-shadow: 0 0 0 9999px rgba(0, 0, 0, .3); }
.frame-wrap.locked .frame-handle { display: none; }
.frame-handle { position: absolute; right: -8px; bottom: -8px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--panel); cursor: nwse-resize; }
.cands { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.cand { display: flex; flex-direction: column; gap: 2px; padding: 4px; text-align: left; border-radius: 8px; }
.cand img { width: 100%; aspect-ratio: 63 / 88; object-fit: cover; border-radius: 4px; background: var(--bg); }
.cand.on { border-color: var(--accent); outline: 2px solid var(--accent); }
.c-set { font-size: 11px; line-height: 1.2; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-num { font-size: 12px; font-weight: 600; }
.c-num i { font-style: normal; font-size: 10px; padding: 0 4px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted); }
@media (max-width: 700px) {
  .det { grid-template-columns: 1fr; }
  .frame-wrap img { max-height: 55vh; }
  .frame-col .muted, .frame-col .reframe { align-self: flex-start; }
}

/* No rubber-band bounce / scroll chaining: dragging inside the popup or the details view must not move the page or its header. */
html, body { overscroll-behavior: none; }
dialog, .dlg-body, #details { overscroll-behavior: contain; }
.frame-box, .frame-handle { touch-action: none; }
