:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #17212b;
  --muted: #5d6b7a;
  --border: #d9dee4;
  --accent: #14505c;
  --accent-contrast: #ffffff;
  --accent-soft: #e3eff1;
  --danger: #b42318;
  --success-bg: #e7f6ec;
  --error-bg: #fdecea;
  --info-bg: #e8f0fb;
  --radius: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161b;
    --surface: #1a2128;
    --text: #e6ebf0;
    --muted: #97a4b1;
    --border: #2e3843;
    --accent: #4fb3c4;
    --accent-contrast: #0b1418;
    --accent-soft: #1d3338;
    --danger: #ff7b6e;
    --success-bg: #173322;
    --error-bg: #3a1a17;
    --info-bg: #182a40;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }
code { font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pre { white-space: pre-wrap; }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .6rem 1rem; padding-top: max(.6rem, env(safe-area-inset-top));
  background: var(--accent); color: var(--accent-contrast);
}
.topbar a, .topbar .link { color: var(--accent-contrast); text-decoration: none; }
.brand { font-weight: 700; }
.topbar nav { display: flex; gap: 1rem; align-items: center; font-size: .95rem; }
.topbar nav a.active { text-decoration: underline; text-underline-offset: 4px; }

.container { max-width: 960px; margin: 0 auto; padding: 1rem 16px 6rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin: .5rem 0 1rem; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; margin-bottom: 1rem;
}

/* forms */
.stack { display: flex; flex-direction: column; gap: .85rem; }
.row { display: flex; gap: .75rem; flex-wrap: wrap; }
.row > label { min-width: 0; }
.grow { flex: 1 1 12rem; }
label { display: flex; flex-direction: column; gap: .25rem; font-weight: 500; font-size: .95rem; }
label.check { flex-direction: row; align-items: center; gap: .5rem; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: .6rem .7rem;
  width: 100%; min-height: 2.75rem;
}
input[type="checkbox"] { width: 1.25rem; min-height: 1.25rem; height: 1.25rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem; margin: 0; }
legend { font-weight: 600; padding: 0 .25rem; }

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  min-height: 2.75rem; padding: .55rem 1rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
button.link {
  background: none; border: none; padding: 0; min-height: 0; font-weight: 500;
  color: var(--accent); text-decoration: underline; cursor: pointer;
}
button.link.danger { color: var(--danger); }
button:disabled { opacity: .6; cursor: progress; }
form.inline { display: inline; }
.actions { display: flex; gap: .75rem; flex-wrap: wrap; }

.flash { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; background: var(--info-bg); }
.flash-success { background: var(--success-bg); }
.flash-error { background: var(--error-bg); }

/* login */
.login-card { max-width: 380px; margin: 12vh auto 0; }
.login-card h1 { margin-bottom: 1.5rem; text-align: center; }

/* list */
.filters { display: grid; grid-template-columns: 1fr; gap: .5rem; margin-bottom: 1rem; }
@media (min-width: 700px) { .filters { grid-template-columns: 1fr 14rem 12rem auto; } }
.item-grid {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .75rem;
}
.item-card {
  display: flex; flex-direction: column; height: 100%;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; color: inherit; text-decoration: none;
}
.item-card img, .no-photo { width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--accent-soft); }
.no-photo { display: grid; place-items: center; font-size: 2.5rem; font-weight: 700; color: var(--accent); text-transform: uppercase; }
.item-card-body { padding: .5rem .65rem .7rem; display: flex; flex-direction: column; gap: .1rem; }
.item-card-body strong { overflow-wrap: anywhere; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 1.25rem; }
.empty { text-align: center; padding: 3rem 1rem; }

/* photos */
.photo-buttons { display: flex; gap: .5rem; flex-wrap: wrap; }
.photo-buttons .button { flex: 1 1 10rem; }
.previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: .5rem; margin-top: .75rem; }
.previews:empty { display: none; }
.previews figure { position: relative; margin: 0; }
.previews img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; display: block; }
.previews .remove {
  position: absolute; top: 4px; right: 4px; min-height: 0; width: 1.75rem; height: 1.75rem; padding: 0;
  border-radius: 50%; background: rgba(0,0,0,.65); color: #fff; border: none; font-size: 1.1rem;
}
.progress { position: relative; height: 1.75rem; border-radius: 6px; background: var(--accent-soft); overflow: hidden; }
.progress .bar { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--accent); transition: width .2s; }
.progress span { position: relative; display: block; text-align: center; font-size: .85rem; line-height: 1.75rem; font-weight: 600; mix-blend-mode: difference; color: #fff; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.gallery figure { margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.gallery figure.primary { border-color: var(--accent); border-width: 2px; }
.gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.gallery figcaption { display: flex; justify-content: space-between; align-items: center; padding: .35rem .5rem; gap: .5rem; }
.badge { font-size: .75rem; font-weight: 700; color: var(--accent); }
.add-photo { display: flex; flex-direction: column; gap: .5rem; }
.add-photo-tile {
  aspect-ratio: 1; display: grid; place-items: center; cursor: pointer; text-align: center;
  border: 2px dashed var(--border); border-radius: var(--radius); color: var(--accent); font-weight: 600;
}

/* detail */
.details { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1rem; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; overflow-wrap: anywhere; }
.links { list-style: none; margin: 0 0 1rem; padding: 0; }
.links li { display: flex; justify-content: space-between; gap: 1rem; padding: .6rem 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.links a { overflow-wrap: anywhere; }
.link-actions { display: flex; gap: .75rem; align-items: center; }
.link-actions select { min-height: 2.25rem; padding: .3rem .5rem; width: auto; }
.status { font-weight: 600; }
.status-verified { color: #1f7a3d; }
.status-needs_review { color: #b25e00; }
details summary { cursor: pointer; font-weight: 600; padding: .25rem 0; }
details[open] > summary { margin-bottom: .75rem; }

/* categories */
.tree { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.tree li {
  display: flex; flex-wrap: wrap; gap: .25rem 1rem; align-items: baseline;
  padding: .65rem 1rem .65rem calc(1rem + var(--depth) * 1.5rem); border-bottom: 1px solid var(--border);
}
.tree li:last-child { border-bottom: none; }
.tree li > a { font-weight: 600; }
.tree-actions { margin-left: auto; display: flex; gap: 1rem; }
.custom-fields:not(:has(label)) { display: none; }

/* barcode lookup */
.barcode-box { display: flex; flex-direction: column; gap: .6rem; }
.barcode-box .actions button { flex: 1 1 10rem; }
.lookup-panel { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem; background: var(--surface); display: flex; flex-direction: column; gap: .6rem; }
.lookup-head { display: flex; gap: .5rem; align-items: baseline; flex-wrap: wrap; }
.lookup-head .link { margin-left: auto; }
.candidate { display: flex; gap: .75rem; border-top: 1px solid var(--border); padding-top: .75rem; }
.candidate-img { width: 72px; height: 72px; object-fit: contain; flex: none; border-radius: 6px; background: var(--accent-soft); }
.candidate-body { display: flex; flex-direction: column; gap: .3rem; min-width: 0; flex: 1; }
.lookup-rows { display: flex; flex-direction: column; gap: .25rem; margin: .25rem 0 .4rem; }
.lookup-row { font-weight: 400; align-items: flex-start; overflow-wrap: anywhere; }
.lookup-row input { margin-top: .15rem; flex: none; }
.warn { color: #b25e00; }
.pending-links { background: var(--accent-soft); border-radius: 8px; padding: .6rem .8rem; }
.pending-links ul { margin: .3rem 0 0; padding-left: 1.1rem; }
.filter-buttons { display: flex; gap: .5rem; }
.filter-buttons button { flex: 1; }

/* review */
.tabs { display: flex; gap: 1rem; flex-wrap: wrap; margin: .5rem 0 1rem; }
.tabs a { text-decoration: none; padding-bottom: .2rem; }
.tabs a.active { font-weight: 700; border-bottom: 2px solid var(--accent); }
.card.links { padding: 0 1rem; }
.small-button { min-height: 2.25rem; padding: .3rem .7rem; font-size: .9rem; }
.runs { width: 100%; border-collapse: collapse; font-size: .9rem; }
.runs th, .runs td { text-align: left; padding: .35rem .5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.runs tr.undone td { color: var(--muted); text-decoration: line-through; }
@media (max-width: 699px) { .runs th:nth-child(5), .runs td:nth-child(5) { display: none; } .topbar nav { gap: .7rem; font-size: .9rem; } }

/* scanner overlay */
.no-scroll { overflow: hidden; }
.scanner { position: fixed; inset: 0; z-index: 50; background: #000; }
.scanner-inner { position: relative; width: 100%; height: 100%; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner-frame {
  position: absolute; left: 10%; right: 10%; top: 35%; height: 30%;
  border: 3px solid rgba(255,255,255,.9); border-radius: 12px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35);
}
.scanner-status {
  position: absolute; left: 16px; right: 16px; top: calc(16px + env(safe-area-inset-top)); margin: 0;
  color: #fff; text-align: center; font-weight: 600; text-shadow: 0 1px 3px #000;
}
.scanner-close {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(24px + env(safe-area-inset-bottom));
  min-width: 10rem; background: #fff; color: #111; border: none;
}
.field-row { border-bottom: 1px solid var(--border); padding: .5rem 0; }

/* floating add button (mobile) */
.fab {
  position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  width: 3.5rem; height: 3.5rem; border-radius: 50%;
  display: grid; place-items: center; font-size: 2rem; line-height: 1; text-decoration: none;
  background: var(--accent); color: var(--accent-contrast); box-shadow: 0 4px 14px rgba(0,0,0,.25);
}
@media (min-width: 700px) { .fab { display: none; } }
@media (max-width: 699px) { .desktop-only { display: none; } }
