:root {
  --bg: #14161b;
  --bg-2: #1d2027;
  --bg-3: #262a33;
  --fg: #eef0f4;
  --fg-2: #a6acb8;
  --fg-3: #6c7380;
  --accent: #f0b429;
  --accent-fg: #1a1400;
  --ok: #3ccf7a;
  --danger: #ef5350;
  --line: #2f343f;
  --tabbar-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }

#views { height: 100%; }
.view {
  position: fixed; inset: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-b));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-top: var(--safe-t);
}
.view.no-scroll { overflow: hidden; }

/* ---------- Tab bar ---------- */
#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--bg-2);
  border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(4, 1fr);
  z-index: 20;
}
#tabbar button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--fg-3); font-size: 11px; font-weight: 600; letter-spacing: .02em;
}
#tabbar button svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#tabbar button.active { color: var(--accent); }

/* ---------- Generic ---------- */
.page { padding: 14px 16px 24px; }
.page h1 { font-size: 22px; margin: 6px 0 14px; }
.page h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--fg-3); margin: 22px 0 8px; }
.muted { color: var(--fg-2); }
.small { font-size: 12.5px; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 10px;
  background: var(--bg-3); color: var(--fg); font-weight: 600;
  border: 1px solid var(--line);
}
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.icon { width: 42px; height: 42px; padding: 0; border-radius: 50%; }
.btn.icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn.on { background: var(--accent); color: var(--accent-fg); }
.btn:disabled { opacity: .45; }

.input, select.input, textarea.input {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  background: var(--bg-2); border: 1px solid var(--line); color: var(--fg);
  outline: none;
}
.input:focus { border-color: var(--accent); }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--fg-2); margin-bottom: 5px; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--bg-3); color: var(--fg-2); border: 1px solid var(--line);
  white-space: nowrap;
}
.chip.on { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.chip .dot { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(0,0,0,.25); }

.swatch {
  width: 44px; height: 44px; border-radius: 10px; flex: 0 0 auto;
  border: 1px solid rgba(255,255,255,.12);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.2);
}
.swatch.metallic { background-image: linear-gradient(135deg, rgba(255,255,255,.35), transparent 40%, rgba(0,0,0,.25)); }
.swatch.sm { width: 30px; height: 30px; border-radius: 8px; }

.list { display: flex; flex-direction: column; gap: 6px; }
.item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: var(--radius);
  background: var(--bg-2); border: 1px solid var(--line);
  text-align: left; width: 100%;
}
.item .name { font-weight: 600; }
.item .meta { color: var(--fg-2); font-size: 12.5px; }
.item .right { text-align: right; flex: 0 0 auto; color: var(--fg-2); font-size: 12.5px; }
.item.owned { border-color: rgba(60,207,122,.45); }
.badge {
  display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 700;
  background: var(--bg-3); color: var(--fg-2); vertical-align: middle;
}
.badge.ok { background: rgba(60,207,122,.18); color: var(--ok); }
.badge.acc { background: rgba(240,180,41,.18); color: var(--accent); }
.badge.approx { background: rgba(239,83,80,.18); color: #ff8a80; }

.empty { text-align: center; color: var(--fg-3); padding: 48px 20px; }
.empty svg { width: 48px; height: 48px; stroke: currentColor; fill: none; stroke-width: 1.4; margin-bottom: 10px; }

/* ---------- Scan view ---------- */
#view-scan { background: #000; padding-top: 0; }
.scan-stage { position: absolute; inset: 0; display: flex; flex-direction: column; }
.scan-video-wrap { position: relative; flex: 1 1 55%; min-height: 220px; overflow: hidden; background: #000; }
.scan-video-wrap video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-overlay { position: absolute; inset: 0; pointer-events: none; }
.ocr-box {
  position: absolute; left: 6%; right: 6%; top: 14%; height: 62%;
  border: 2px dashed rgba(240,180,41,.85); border-radius: 12px;
}
.ocr-box .tag { position: absolute; top: -10px; left: 12px; background: rgba(0,0,0,.65); color: #ddd; font-size: 11px; padding: 1px 7px; border-radius: 6px; }
.scan-top {
  position: absolute; left: 0; right: 0; top: 0; padding: calc(var(--safe-t) + 10px) 12px 10px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: linear-gradient(rgba(0,0,0,.55), transparent);
}
.scan-top .btn.icon { background: rgba(20,22,27,.75); border-color: rgba(255,255,255,.12); }
.scan-top .btn.icon.on { background: var(--accent); }
@keyframes pulse { 50% { opacity: .2; } }

.scan-results {
  flex: 1 1 48%; min-height: 0; overflow-y: auto;
  background: var(--bg); border-top: 1px solid var(--line);
  padding: 10px 12px 12px;
}
.scan-results .hint { color: var(--fg-3); font-size: 12.5px; margin: 2px 2px 8px; }
.scan-results .item { padding: 8px 10px; }
.scan-results .item.top { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.scan-results .item .swatch { width: 40px; height: 40px; }
.scan-results .add-custom { margin-top: 8px; width: 100%; }
.manual-block { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.scan-results .sec-h { font-size: 11.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--fg-3); margin: 12px 2px 6px; }
.scan-status { font-size: 13px; color: var(--fg-2); margin: 0 2px 4px; min-height: 18px; }
.scan-status .lbl { color: var(--fg-3); }
.scan-status .ocr-text { color: var(--fg); font-weight: 600; }
.scan-status.busy::before { content: ''; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-right: 6px; animation: pulse 1s infinite; }
.scan-status.error { color: var(--danger); }
.confirm-card {
  background: var(--bg-2); border: 1px solid var(--accent); box-shadow: 0 0 0 1px var(--accent);
  border-radius: var(--radius); padding: 12px; margin-top: 6px;
}
.confirm-card .name { font-size: 20px; font-weight: 700; line-height: 1.2; }
.confirm-card .actions { display: flex; gap: 8px; margin-top: 12px; }
.confirm-card .actions .btn { flex: 1; padding: 13px 10px; }
.scan-debug {
  position: absolute; right: 12px; bottom: 12px; width: 46%; max-height: 60%; overflow: hidden;
  background: rgba(20,22,27,.85); border-radius: 10px; padding: 6px; font-size: 11px; color: #ddd;
}
.scan-debug img { width: 100%; border-radius: 6px; display: block; margin-bottom: 4px; }
.scan-msg { padding: 24px 16px; text-align: center; color: var(--fg-2); }
.scan-msg .btn { margin-top: 12px; }

/* ---------- Library ---------- */
.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.toolbar select.input { width: auto; padding: 8px 10px; }
.stats { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.stat { background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; font-size: 12.5px; color: var(--fg-2); }
.stat b { color: var(--fg); font-size: 16px; display: block; }
.group-h { font-size: 12.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--fg-3); margin: 16px 2px 6px; }
.qty { display: inline-flex; align-items: center; gap: 6px; }
.qty button { width: 34px; height: 34px; border-radius: 50%; background: var(--bg-3); border: 1px solid var(--line); font-size: 18px; }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 30; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 31;
  background: var(--bg-2); border-radius: 18px 18px 0 0; border-top: 1px solid var(--line);
  padding: 10px 16px calc(18px + var(--safe-b));
  max-height: 88vh; overflow-y: auto;
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet .grab { width: 40px; height: 4px; border-radius: 2px; background: var(--fg-3); margin: 0 auto 12px; }
.sheet h3 { margin: 4px 0 2px; font-size: 18px; }
.sheet .actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.sheet .actions .btn { flex: 1; }
.sheet .big-swatch { width: 100%; height: 70px; border-radius: 12px; margin: 10px 0; border: 1px solid rgba(255,255,255,.12); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 14px);
  transform: translateX(-50%); z-index: 40;
  background: var(--fg); color: var(--bg); padding: 9px 14px; border-radius: 10px; font-weight: 600; font-size: 14px;
  box-shadow: 0 6px 24px rgba(0,0,0,.4);
  animation: up .15s ease-out;
}

/* ---------- Settings ---------- */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting .lbl { font-weight: 600; }
.setting .sub { color: var(--fg-2); font-size: 12.5px; }
.switch { position: relative; width: 46px; height: 28px; border-radius: 14px; background: var(--bg-3); border: 1px solid var(--line); flex: 0 0 auto; transition: background .15s; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--fg-2); transition: transform .15s, background .15s; }
.switch.on { background: var(--accent); border-color: transparent; }
.switch.on::after { transform: translateX(18px); background: var(--accent-fg); }
.brand-list .setting { padding: 9px 0; }
a { color: var(--accent); }
