:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --fg: #1d2430;
  --muted: #5b6676;
  --line: #e2e6eb;
  --grid: rgba(29, 36, 48, 0.1);
  --accent: #0b7a75;
  --accent-fg: #ffffff;
  --rec: #e03131;
  --series-a: #0b7a75;
  --series-b: #d9480f;
  --series-c: #5f3dc4;
  --delta-up: #d9480f;
  --delta-down: #1971c2;
  --sev-warn: #c92a2a;
  --sev-watch: #e67700;
  --sev-info: #1971c2;
  --sev-ok: #2f9e44;
  --radius: 14px;
  --tabbar-h: 62px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1720;
    --card: #17212c;
    --fg: #e6ebf1;
    --muted: #93a0b0;
    --line: #26323f;
    --grid: rgba(230, 235, 241, 0.09);
    --accent: #2bb3a8;
    --accent-fg: #06201e;
    --series-a: #3cc9bd;
    --series-b: #ff8a4c;
    --series-c: #a48bff;
    --delta-up: #ff8a4c;
    --delta-down: #5aa9f0;
    --sev-warn: #ff6b6b;
    --sev-watch: #ffa94d;
    --sev-info: #4dabf7;
    --sev-ok: #51cf66;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
main { max-width: 760px; margin: 0 auto; padding: 0 16px 24px; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: -0.01em; }
.brand svg { width: 22px; height: 22px; color: var(--accent); }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 99px; background: var(--rec); color: #fff; font-weight: 600; }

.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin: 12px 0;
}
.card.flat { border: 0; margin: 0; }
.eyebrow { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 4px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
h2 { font-size: 18px; margin: 0 0 4px; }
h3 { font-size: 15px; margin: 18px 0 8px; }

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 12px 0; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; }

/* ---- Buttons ---- */
.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  border-radius: 10px; padding: 10px 14px; min-height: 42px;
}
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: 14px; }
.btn.primary { background: var(--rec); border-color: var(--rec); color: #fff; }
.btn.accent { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.secondary { border-color: var(--accent); color: var(--accent); }
.btn.danger { color: var(--sev-warn); }
.btn.big { width: 100%; font-size: 17px; min-height: 56px; display: flex; gap: 10px; align-items: center; justify-content: center; }
.rec-dot { width: 14px; height: 14px; border-radius: 50%; background: #fff; }
.btn.recording .rec-dot { border-radius: 3px; }

/* ---- Forms ---- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin-bottom: 8px; }
.form-grid .span2 { grid-column: 1 / -1; }
@media (max-width: 420px) { .form-grid { grid-template-columns: 1fr; } }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
label.inline { flex-direction: row; align-items: center; gap: 8px; color: var(--fg); font-weight: 500; font-size: 14px; }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--fg); background: var(--bg);
  border: 1px solid var(--line); border-radius: 9px; padding: 9px 10px; width: 100%; min-width: 0;
}
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
textarea { resize: vertical; }
.file-btn { display: block; margin-top: 14px; color: var(--accent); font-weight: 600; cursor: pointer; text-align: center; font-size: 14px; }
.file-btn input { display: none; }
.tips { font-size: 13px; color: var(--muted); margin-top: 10px; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.chip { border: 1px solid var(--line); background: var(--bg); color: var(--fg); border-radius: 99px; padding: 6px 12px; font: inherit; font-size: 14px; cursor: pointer; }
.chip[aria-checked="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

/* ---- Live ---- */
.live-head { display: flex; justify-content: space-between; align-items: center; }
.meter-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.meter-value small { font-size: 13px; color: var(--muted); font-weight: 600; }
.meter { height: 6px; border-radius: 4px; background: var(--line); margin: 8px 0 12px; overflow: hidden; }
.meter div { height: 100%; width: 0; background: linear-gradient(90deg, var(--sev-ok), var(--sev-watch) 75%, var(--sev-warn)); transition: width 80ms linear; }
.canvas-wrap { position: relative; width: 100%; }
.canvas-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.live-spectrum { height: 150px; }
.live-fall { height: 110px; margin-top: 8px; border-radius: 8px; overflow: hidden; background: #000; }
.chart { height: 230px; margin: 8px 0; }
.chart.tall { height: 260px; }
.chart.short { height: 180px; }
.spectro { height: 170px; border-radius: 8px; overflow: hidden; }
.overlay-hint { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 20px; color: var(--muted); font-size: 14px; }

.progress { height: 8px; border-radius: 5px; background: var(--line); margin-top: 12px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--rec); }

/* ---- Result / fingerprint ---- */
.fp-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); word-break: break-all; }
.barcode { display: flex; height: 22px; border-radius: 5px; overflow: hidden; margin: 10px 0; }
.barcode span { flex: 1; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0; }
@media (max-width: 520px) { .stats { grid-template-columns: repeat(2, 1fr); } }
.stat { background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.stat .k { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.stat .v { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .v small { font-size: 12px; color: var(--muted); font-weight: 600; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.tag { font-size: 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 99px; padding: 2px 9px; }
.tag.base { border-color: var(--sev-watch); color: var(--sev-watch); }

.findings { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.finding { border-left: 4px solid var(--sev-info); background: var(--bg); border-radius: 8px; padding: 9px 12px; }
.finding.warn { border-color: var(--sev-warn); }
.finding.watch { border-color: var(--sev-watch); }
.finding.ok { border-color: var(--sev-ok); }
.finding b { display: block; font-size: 14.5px; }
.finding p { margin: 3px 0 0; font-size: 13.5px; color: var(--muted); }
.sev { font-size: 10.5px; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }
.sev.warn { color: var(--sev-warn); } .sev.watch { color: var(--sev-watch); } .sev.info { color: var(--sev-info); } .sev.ok { color: var(--sev-ok); }

.similarity { display: flex; align-items: center; gap: 14px; margin: 8px 0 12px; }
.similarity .ring { --p: 0; width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center; font-weight: 800;
  background: conic-gradient(var(--c, var(--sev-ok)) calc(var(--p) * 1%), var(--line) 0); position: relative; flex: none; }
.similarity .ring::before { content: ""; position: absolute; inset: 6px; border-radius: 50%; background: var(--card); }
.similarity .ring span { position: relative; font-size: 15px; }

table.peaks { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
table.peaks th, table.peaks td { text-align: right; padding: 5px 6px; border-bottom: 1px solid var(--line); }
table.peaks th:first-child, table.peaks td:first-child { text-align: left; }
table.peaks th { color: var(--muted); font-weight: 600; font-size: 12px; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.actions .btn { flex: 1 1 auto; }
.player { margin: 8px 0; }

/* ---- Library ---- */
.toolbar { display: flex; gap: 6px; flex-wrap: wrap; margin: 10px 0; }
.fp-list { list-style: none; padding: 0; margin: 0; }
.fp-item { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.fp-item .barcode { height: 10px; margin: 5px 0 0; }
.fp-item .title { font-weight: 600; }
.fp-item .sub { font-size: 12.5px; color: var(--muted); }
.fp-item .lvl { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.fp-item .lvl small { display: block; font-size: 11px; color: var(--muted); font-weight: 600; }
.fp-item .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: 1px; }
.star { background: none; border: 0; font-size: 20px; cursor: pointer; color: var(--muted); padding: 4px; }
.star.on { color: var(--sev-watch); }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

/* ---- Tab bar ---- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar button { font: inherit; font-size: 11.5px; font-weight: 600; background: none; border: 0; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; cursor: pointer; }
.tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--accent); }

/* ---- Dialogs ---- */
.sheet { border: 0; padding: 0; width: 100%; max-width: 760px; max-height: 100dvh; height: 100dvh; margin: 0 auto; background: var(--bg); color: var(--fg); }
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-head { position: sticky; top: 0; z-index: 2; padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--bg); display: flex; justify-content: flex-end; }
#sheet-body { padding: 0 16px 30px; }
.small-sheet { height: auto; max-width: 420px; border-radius: var(--radius); margin: auto; background: var(--card); }
.small-sheet form { display: grid; gap: 10px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; max-width: calc(100% - 32px); z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }
