:root {
  --bg: #f5f2ec;
  --surface: #ffffff;
  --ink: #1d2230;
  --ink-2: #545b6b;
  --ink-3: #8a90a0;
  --line: #e6e1d8;
  --red: #e2483d;
  --red-d: #b8372e;
  --yellow: #f5c242;
  --blue: #2f6fd6;
  --green: #2f9e62;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(29,34,48,.06), 0 4px 14px rgba(29,34,48,.06);
  --shadow-lg: 0 10px 40px rgba(29,34,48,.22);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161c; --surface: #1d2029; --ink: #eef0f4; --ink-2: #b2b7c3; --ink-3: #7d8392; --line: #2c303b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.25); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: var(--font); background: var(--bg); color: var(--ink); line-height: 1.45; -webkit-font-smoothing: antialiased; min-height: 100vh; display: flex; flex-direction: column; }
a { color: var(--blue); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 20; background: var(--red); color: #fff; box-shadow: 0 2px 0 var(--red-d); padding-top: env(safe-area-inset-top); }
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 800; font-size: 1.2rem; letter-spacing: -.01em; margin-right: auto; }
.brand-mark { position: relative; width: 34px; height: 22px; background: var(--yellow); border-radius: 5px; margin-top: 8px; box-shadow: inset 0 -4px 0 rgba(0,0,0,.12); }
.brand-mark i { position: absolute; top: -7px; width: 11px; height: 8px; background: var(--yellow); border-radius: 3px 3px 0 0; }
.brand-mark i:first-child { left: 5px; } .brand-mark i:last-child { right: 5px; }
.brand-mark.big { display: inline-block; width: 64px; height: 40px; border-radius: 8px; margin: 16px 0 8px; }
.brand-mark.big i { top: -12px; width: 20px; height: 13px; border-radius: 5px 5px 0 0; }
.brand-mark.big i:first-child { left: 9px; } .brand-mark.big i:last-child { right: 9px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.topbar .btn-primary { background: #fff; color: var(--red-d); box-shadow: 0 2px 0 rgba(0,0,0,.18); }
.topbar .btn-primary:hover { background: #fff6e0; }
.topbar .btn-ghost { color: #fff; }
.topbar .btn-ghost:hover { background: rgba(255,255,255,.15); }

/* menu */
.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; }
.menu summary::-webkit-details-marker { display: none; }
.menu-list { position: absolute; right: 0; top: calc(100% + 8px); background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow-lg); min-width: 230px; padding: 6px; display: flex; flex-direction: column; }
.menu-list button, .menu-list a, .menu-file { text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 8px; cursor: pointer; color: inherit; text-decoration: none; font-size: .95rem; }
.menu-list button:hover, .menu-list a:hover, .menu-file:hover { background: var(--bg); }
.menu-sep { height: 1px; background: var(--line); margin: 6px 4px; }
.menu-user { padding: 6px 12px; font-size: .82rem; color: var(--ink-3); word-break: break-all; }

.banner { background: #fff4cf; color: #5b4500; border-bottom: 1px solid #f0dc93; padding: 10px 16px; text-align: center; font-size: .92rem; }
@media (prefers-color-scheme: dark) { .banner { background: #3a3215; color: #f6e2a0; border-color: #574a1a; } }

/* layout */
.wrap { max-width: 1200px; width: 100%; margin: 0 auto; padding: 20px 16px 40px; flex: 1; }
.foot { text-align: center; color: var(--ink-3); font-size: .8rem; padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); }
.foot p { max-width: 640px; margin: 0 auto; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); border-top: 5px solid var(--red); display: flex; flex-direction: column; }
.stat:nth-child(2) { border-top-color: var(--yellow); }
.stat:nth-child(3) { border-top-color: var(--blue); }
.stat:nth-child(4) { border-top-color: var(--green); }
.stat-num { font-size: 1.7rem; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--ink-2); font-size: .85rem; }

/* controls */
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 8px; }
.search { position: relative; flex: 1 1 280px; }
.search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-52%); color: var(--ink-3); font-size: 1.2rem; pointer-events: none; }
.search input { width: 100%; padding: 11px 14px 11px 36px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); outline: none; box-shadow: var(--shadow); }
.search input:focus, select:focus, .field input:focus, .field textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(47,111,214,.18); outline: none; }
.tabs { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; box-shadow: var(--shadow); }
.tab { border: 0; background: none; padding: 7px 14px; border-radius: 9px; cursor: pointer; color: var(--ink-2); font-weight: 600; font-size: .92rem; }
.tab[aria-selected="true"] { background: var(--ink); color: var(--surface); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
select { padding: 9px 30px 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238a90a0'/%3E%3C/svg%3E") no-repeat right 11px center; appearance: none; cursor: pointer; max-width: 200px; }
.result-line { color: var(--ink-3); font-size: .85rem; margin: 6px 2px 12px; min-height: 1.2em; }

/* grid + cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; cursor: pointer; transition: transform .12s ease, box-shadow .12s ease; border: 1px solid transparent; text-align: left; padding: 0; }
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.card:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
.card-img { aspect-ratio: 4 / 3; background: #fff; display: grid; place-items: center; position: relative; border-bottom: 1px solid var(--line); }
.card-img img { width: 100%; height: 100%; object-fit: contain; padding: 10px; }
.card-img .ph { width: 64px; height: 40px; border-radius: 7px; background: var(--line); position: relative; margin-top: 10px; }
.card-img .ph::before, .card-img .ph::after { content: ""; position: absolute; top: -10px; width: 18px; height: 11px; background: var(--line); border-radius: 4px 4px 0 0; }
.card-img .ph::before { left: 9px; } .card-img .ph::after { right: 9px; }
.badge { position: absolute; top: 10px; left: 10px; font-size: .72rem; font-weight: 700; padding: 3px 8px; border-radius: 999px; letter-spacing: .02em; text-transform: uppercase; }
.badge.owned { background: #e3f5ea; color: #1d7546; }
.badge.wishlist { background: #fdeceb; color: #b23128; }
.qty { position: absolute; top: 10px; right: 10px; background: var(--ink); color: #fff; font-size: .75rem; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.card-num { font-size: .78rem; font-weight: 700; color: var(--red); letter-spacing: .02em; font-variant-numeric: tabular-nums; }
.card-name { font-weight: 700; font-size: 1rem; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-meta { color: var(--ink-2); font-size: .83rem; display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: auto; padding-top: 6px; }
.card-notes { color: var(--ink-3); font-size: .8rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* empty */
.empty { text-align: center; background: var(--surface); border-radius: 20px; box-shadow: var(--shadow); padding: 48px 20px 52px; max-width: 620px; margin: 10px auto; }
.empty h1 { margin: 10px 0 6px; font-size: 1.6rem; letter-spacing: -.02em; }
.empty h2 { margin: 0 0 6px; }
.empty p { color: var(--ink-2); max-width: 440px; margin: 0 auto 22px; }
.empty-small { padding: 32px 20px; }
.empty-art { position: relative; height: 92px; width: 160px; margin: 0 auto; }
.empty-art .b { position: absolute; border-radius: 6px; box-shadow: inset 0 -5px 0 rgba(0,0,0,.14); }
.empty-art .b::before, .empty-art .b::after { content: ""; position: absolute; top: -8px; width: 14px; height: 9px; border-radius: 3px 3px 0 0; background: inherit; }
.empty-art .b::before { left: 7px; } .empty-art .b::after { right: 7px; }
.b1 { width: 70px; height: 30px; background: var(--red); left: 10px; bottom: 0; }
.b2 { width: 70px; height: 30px; background: var(--blue); left: 80px; bottom: 0; }
.b3 { width: 70px; height: 30px; background: var(--yellow); left: 45px; bottom: 38px; animation: drop 1.8s ease-in-out infinite alternate; }
@keyframes drop { from { transform: translateY(-10px); } to { transform: translateY(0); } }
@media (prefers-reduced-motion: reduce) { .b3 { animation: none; } .card { transition: none; } }

.loading { display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--ink-2); padding: 60px 0; }
.spinner { width: 18px; height: 18px; border: 3px solid var(--line); border-top-color: var(--red); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; border-radius: 10px; padding: 9px 16px; font-weight: 700; cursor: pointer; text-decoration: none; font-size: .95rem; white-space: nowrap; transition: background .12s, transform .05s; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--red); color: #fff; box-shadow: 0 2px 0 var(--red-d); }
.btn-primary:hover { background: #d63e33; }
.btn-secondary { background: var(--ink); color: var(--surface); }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: rgba(127,127,127,.12); }
.btn-danger { background: #c9352b; color: #fff; }
.btn-danger-ghost { background: transparent; color: #c9352b; }
.btn-danger-ghost:hover { background: rgba(201,53,43,.1); }
.btn-sm { padding: 6px 10px; font-size: .85rem; }
.btn-lg { padding: 13px 22px; font-size: 1.05rem; border-radius: 12px; }
.btn.block { display: flex; width: 100%; margin-top: 10px; }
.icon-btn { width: 40px; height: 40px; padding: 0; font-size: 1.2rem; }

/* dialog */
dialog.sheet { border: 0; padding: 0; border-radius: 18px; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 32px); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg); }
dialog.sheet-sm { width: min(420px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(20,22,28,.5); backdrop-filter: blur(2px); }
dialog form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 6px 20px; }
.sheet-head h2 { margin: 0; font-size: 1.2rem; }
.sheet-body { padding: 8px 20px 12px; overflow-y: auto; }
.sheet-body h2 { margin: 6px 0 8px; font-size: 1.15rem; }
.sheet-body p { color: var(--ink-2); }
.sheet-foot { display: flex; gap: 8px; align-items: center; padding: 12px 16px calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.spacer { flex: 1; }
.field { display: flex; flex-direction: column; gap: 5px; margin: 0 0 12px; border: 0; padding: 0; min-width: 0; }
.field > span, .field legend { font-size: .8rem; font-weight: 700; color: var(--ink-2); padding: 0; }
.field em { font-weight: 400; color: var(--ink-3); font-style: normal; }
.field input, .field textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); min-width: 0; }
.field textarea { resize: vertical; }
.lookup-row { display: flex; gap: 8px; align-items: flex-end; }
.lookup-row .field { margin-bottom: 0; }
.lookup-row .btn { height: 43px; }
.grow { flex: 1; }
.hint { font-size: .84rem; color: var(--ink-3); margin: 6px 2px 10px; min-height: 1.1em; }
.hint.ok { color: var(--green); } .hint.warn { color: #b06a00; } .hint.err { color: #c9352b; }
.form-grid { display: grid; grid-template-columns: 150px 1fr; gap: 14px; margin-top: 4px; }
.preview { aspect-ratio: 1; border-radius: 12px; border: 1px dashed var(--line); background: #fff; display: grid; place-items: center; overflow: hidden; color: var(--ink-3); font-size: .8rem; }
.preview img { width: 100%; height: 100%; object-fit: contain; padding: 6px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 8px 16px; border-radius: 9px; font-weight: 700; color: var(--ink-2); font-size: .92rem; }
.seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--blue); }
.form-error { background: #fdeceb; color: #a12a21; padding: 9px 12px; border-radius: 10px; font-size: .9rem; }

/* toasts */
.toasts { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; align-items: center; pointer-events: none; }
.toast { background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 12px; box-shadow: var(--shadow-lg); font-size: .92rem; animation: up .2s ease; pointer-events: auto; display: flex; gap: 12px; align-items: center; }
.toast button { background: none; border: 0; color: var(--yellow); font-weight: 700; cursor: pointer; padding: 0; }
.toast.err { background: #c9352b; color: #fff; }
@keyframes up { from { opacity: 0; transform: translateY(8px); } }


/* mobile */
@media (max-width: 720px) {
  .stats { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat { padding: 10px 12px; }
  .stat-num { font-size: 1.4rem; }
  .controls { gap: 8px; }
  .tabs { width: 100%; } .tab { flex: 1; }
  .filters { width: 100%; } .filters select { flex: 1; min-width: 0; max-width: none; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .card-body { padding: 10px; }
  .card-name { font-size: .92rem; }
  .card-notes { display: none; }
  .brand-text { font-size: 1.05rem; }
  dialog.sheet { width: 100vw; max-width: 100vw; max-height: 94vh; margin: auto 0 0; border-radius: 18px 18px 0 0; }
  dialog form { max-height: 94vh; }
  .form-grid { grid-template-columns: 96px 1fr; gap: 10px; }
  .row3 { grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
}
@media (max-width: 380px) { .grid { grid-template-columns: 1fr; } .row2 { grid-template-columns: 1fr; } }

/* passcode screen */
.lock-screen { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; background: var(--red); overflow: auto; }
.lock-card { background: var(--surface); border-radius: 20px; padding: 28px 28px 22px; width: min(400px, 100%); text-align: center; box-shadow: var(--shadow-lg); }
.lock-card h1 { margin: 8px 0 4px; }
.lock-card p { color: var(--ink-2); margin: 0 0 14px; }
.lock-card .field { text-align: left; margin-bottom: 10px; }
.lock-card input { width: 100%; font-size: 1.1rem; text-align: center; letter-spacing: .08em; }
.lock-card .form-error { margin: 0 0 10px; }
.lock-card .fine { font-size: .78rem; color: var(--ink-3); margin-top: 16px; }
body.locked { overflow: hidden; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* BrickEconomy */
.card-value { display: flex; align-items: center; gap: 8px; font-size: .85rem; font-weight: 700; margin-top: 4px; }
.sync { font-size: .7rem; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: var(--line); color: var(--ink-2); }
.sync.synced { background: #e3f5ea; color: #1d7546; }
.sync.pending { background: #fff4cf; color: #7a5b00; }
.sync.error { background: #fdeceb; color: #a12a21; }
.be-info { margin-top: 2px; }
.stat-value .stat-num { font-size: 1.35rem; }
@media (max-width: 720px) { .stat-value { grid-column: span 2; } }
@media (min-width: 721px) and (max-width: 980px) { .stats { grid-template-columns: repeat(3, 1fr); } }
