:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --surface-2: #ece8e1;
  --text: #1c1b19;
  --muted: #6b675f;
  --border: #ddd7cc;
  --accent: #c2410c;
  --accent-text: #ffffff;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.06);
  --radius: 12px;
  --games: #7c3aed; --movies: #dc2626; --tcg: #d97706; --books: #059669; --music: #2563eb; --other: #64748b;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121110;
    --surface: #1c1b19;
    --surface-2: #272522;
    --text: #f1eee8;
    --muted: #a19b90;
    --border: #34312c;
    --accent: #fb923c;
    --accent-text: #1c1b19;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    --games: #a78bfa; --movies: #f87171; --tcg: #fbbf24; --books: #34d399; --music: #60a5fa; --other: #94a3b8;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
h1, h2, h3 { font-family: "Space Grotesk", Inter, sans-serif; letter-spacing: -.01em; line-height: 1.2; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 12px max(16px, calc((100vw - 1280px) / 2));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; font: 700 18px "Space Grotesk", sans-serif; white-space: nowrap; }
.brand-mark { font-size: 22px; }
.search { flex: 1; max-width: 420px; margin-left: auto; }
.search input { width: 100%; }
.actions { display: flex; gap: 8px; }

/* ---------- Controls ---------- */
input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
.btn {
  font: 600 14px Inter, sans-serif; cursor: pointer; white-space: nowrap;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger { color: #dc2626; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { background: none; border: 0; font-size: 18px; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Layout ---------- */
main { max-width: 1280px; margin: 0 auto; padding: 24px 16px 64px; }
.page-head { display: flex; align-items: end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { margin: 0; font-size: clamp(26px, 4vw, 36px); }
.crumb { font-size: 14px; color: var(--muted); text-decoration: none; }
.crumb:hover { color: var(--text); }

.totals { display: flex; gap: 10px; flex-wrap: wrap; }
.total {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 16px; min-width: 120px;
}
.total b { display: block; font: 700 22px "Space Grotesk", sans-serif; font-variant-numeric: tabular-nums; }
.total span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }

/* ---------- Home: category cards ---------- */
.categories { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.cat-card {
  --c: var(--other);
  position: relative; display: flex; flex-direction: column; text-decoration: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); transition: transform .15s, border-color .15s;
}
.cat-card:hover { transform: translateY(-2px); border-color: var(--c); }
.cat-card .mosaic {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; height: 130px;
  background: color-mix(in srgb, var(--c) 18%, var(--surface-2));
}
.cat-card .mosaic img { width: 100%; height: 130px; object-fit: cover; }
.cat-card .mosaic .empty-mosaic { grid-column: 1 / -1; display: grid; place-items: center; font-size: 48px; }
.cat-card .cat-info { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-top: 3px solid var(--c); }
.cat-card h2 { margin: 0; font-size: 19px; }
.cat-card .count { font: 700 20px "Space Grotesk", sans-serif; color: var(--c); font-variant-numeric: tabular-nums; }
.cat-card .sub { font-size: 13px; color: var(--muted); }

/* ---------- Category toolbar ---------- */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar input[type=search] { flex: 1 1 220px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 20px; }
.chip {
  font: 500 13px Inter, sans-serif; cursor: pointer;
  padding: 4px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip small { opacity: .65; margin-left: 4px; }

/* ---------- Item grid ---------- */
.items { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 20px 16px; }
.item { cursor: pointer; text-align: left; background: none; border: 0; padding: 0; color: inherit; font: inherit; }
.item .cover {
  position: relative; aspect-ratio: 2 / 3; border-radius: 8px; overflow: hidden;
  background: var(--surface-2); box-shadow: var(--shadow); transition: transform .15s;
}
.items.square .item .cover { aspect-ratio: 1; }
.items.card .item .cover { aspect-ratio: 63 / 88; }
.item:hover .cover, .item:focus-visible .cover { transform: translateY(-3px); }
.item:focus-visible { outline: none; }
.item:focus-visible .cover { outline: 3px solid var(--accent); }
.item .cover img { width: 100%; height: 100%; object-fit: cover; }
.cover-fallback {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; gap: 6px;
  padding: 12px; text-align: center; font: 600 14px "Space Grotesk", sans-serif;
  background: linear-gradient(160deg, color-mix(in srgb, var(--c, var(--other)) 35%, var(--surface-2)), var(--surface-2));
}
.cover-fallback span { font-size: 28px; }
.qty { position: absolute; top: 6px; right: 6px; background: rgba(0,0,0,.75); color: #fff; font: 700 12px Inter; padding: 2px 7px; border-radius: 999px; }
.item h3 { margin: 8px 0 0; font: 600 14px/1.3 Inter, sans-serif; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.item p { margin: 2px 0 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.empty { text-align: center; padding: 64px 16px; color: var(--muted); }
.empty .big { font-size: 48px; }

/* ---------- Dialogs ---------- */
.dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 0;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35);
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
}
.dialog.wide { width: min(920px, calc(100vw - 24px)); }
.dialog.narrow { width: min(380px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(2px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dialog-head h2 { margin: 0; font-size: 20px; }
.dialog-foot { display: flex; align-items: center; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); position: sticky; bottom: 0; background: var(--surface); }
.dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--muted); }
.dialog label input, .dialog label select, .dialog label textarea { color: var(--text); font-weight: 400; font-size: 15px; }
#loginForm > label { padding: 20px; }
.form-error { color: #dc2626; font-size: 14px; margin: 0 auto 0 0; }
#loginForm .form-error { padding: 0 20px; margin: 0; }

/* Detail */
.detail { display: grid; grid-template-columns: 280px 1fr; gap: 24px; padding: 24px; }
.detail .cover { position: relative; border-radius: 10px; overflow: hidden; background: var(--surface-2); aspect-ratio: 2/3; box-shadow: var(--shadow); }
.detail .cover.square { aspect-ratio: 1; }
.detail .cover.card { aspect-ratio: 63 / 88; }
.detail .cover img { width: 100%; height: 100%; object-fit: cover; }
.detail h2 { margin: 0 0 4px; font-size: 26px; }
.detail .byline { margin: 0 0 16px; color: var(--muted); }
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.badge { font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); }
.badge.cat { background: color-mix(in srgb, var(--c) 20%, transparent); color: var(--c); }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0 0 16px; }
.facts dt { color: var(--muted); font-size: 13px; padding-top: 1px; }
.facts dd { margin: 0; font-variant-numeric: tabular-nums; }
.notes { white-space: pre-wrap; background: var(--surface-2); padding: 12px; border-radius: 8px; }

/* Edit */
.edit-body { padding: 16px 20px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.dialog { overflow-x: hidden; }
.lookup { background: var(--surface-2); border-radius: 12px; padding: 12px; }
.lookup-row { display: flex; gap: 8px; flex-wrap: wrap; }
.lookup-row input { flex: 1 1 200px; }
.lookup-status { margin: 8px 0 0; font-size: 13px; color: var(--muted); min-height: 1em; }
.lookup-status:empty { display: none; }
.lookup-results { display: flex; gap: 10px; overflow-x: auto; padding: 4px 2px 6px; }
.lookup-results:empty { display: none; }
.result {
  flex: 0 0 120px; cursor: pointer; text-align: left; font: inherit; color: inherit;
  background: var(--surface); border: 2px solid transparent; border-radius: 10px; padding: 6px;
}
.result:hover, .result.picked { border-color: var(--accent); }
.result .thumb { aspect-ratio: 2/3; border-radius: 6px; background: var(--surface-2); overflow: hidden; display: grid; place-items: center; font-size: 24px; }
.result .thumb img { width: 100%; height: 100%; object-fit: cover; }
.result b { display: block; font-size: 12px; line-height: 1.3; margin-top: 6px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.result small { display: block; font-size: 11px; color: var(--muted); line-height: 1.3; }
.edit-fields { display: grid; grid-template-columns: 160px 1fr; gap: 20px; align-items: start; }
.cover-edit { position: sticky; top: 76px; }
.cover-edit img, .cover-placeholder { width: 100%; aspect-ratio: 2/3; object-fit: cover; border-radius: 10px; background: var(--surface-2); }
.cover-placeholder { display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.grid-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.span2 { grid-column: span 2; }
.money { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 0; }
.money legend { font-size: 13px; font-weight: 600; padding: 0 4px; }
.money legend small { font-weight: 400; color: var(--muted); }

/* Scan */
.scan-body { padding: 16px 20px 20px; display: grid; gap: 12px; }
#scanVideo { width: 100%; border-radius: 10px; background: #000; max-height: 50vh; object-fit: cover; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-weight: 500; box-shadow: var(--shadow);
}

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .actions { margin-left: auto; }
  .brand span:last-child { font-size: 16px; }
  .items { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 16px 12px; }
  .detail { grid-template-columns: 1fr; padding: 16px; }
  .detail .cover { max-width: 220px; }
  .edit-fields { grid-template-columns: 1fr; }
  .cover-edit { position: static; width: 120px; }
  .grid-fields, .money { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .dialog-foot .form-error { flex-basis: 100%; }
  .dialog-foot { flex-wrap: wrap; }
}
