:root {
  --bg: #f3f5f8; --surface: #ffffff; --surface-2: #f7f9fb; --ink: #111827; --ink-2: #4b5563; --ink-3: #8a94a3;
  --line: #e2e7ee; --line-2: #cfd6e0;
  --brand: #c8102e; --brand-ink: #ffffff; --top: #0f1720; --top-ink: #e8edf3;
  --input-bg: #fffbea; --input-line: #e7cf74; --input-focus: #b8860b;
  --ref: #2563eb; --run: #0d9488; --new: #b45309; --out: #7c3aed;
  --good: #15803d; --warn: #b45309; --bad: #b91c1c;
  --hl: #fde68a; --hl-col: #fef3c7; --sel: #16a34a;
  --radius: 10px; --shadow: 0 1px 2px rgba(15,23,32,.06), 0 1px 1px rgba(15,23,32,.04);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1016; --surface: #121a23; --surface-2: #17212c; --ink: #e6ebf1; --ink-2: #a8b3c0; --ink-3: #6f7c8b;
    --line: #223040; --line-2: #2d3e52; --top: #070b10; --top-ink: #e6ebf1;
    --input-bg: #2a2410; --input-line: #6b5a1d; --input-focus: #e0b84a;
    --ref: #60a5fa; --run: #2dd4bf; --new: #f59e0b; --out: #a78bfa;
    --hl: #4a3b0c; --hl-col: #2b2410; --sel: #22c55e; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1016; --surface: #121a23; --surface-2: #17212c; --ink: #e6ebf1; --ink-2: #a8b3c0; --ink-3: #6f7c8b;
  --line: #223040; --line-2: #2d3e52; --top: #070b10; --top-ink: #e6ebf1;
  --input-bg: #2a2410; --input-line: #6b5a1d; --input-focus: #e0b84a;
  --ref: #60a5fa; --run: #2dd4bf; --new: #f59e0b; --out: #a78bfa;
  --hl: #4a3b0c; --hl-col: #2b2410; --sel: #22c55e; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
button, input, select { font: inherit; color: inherit; }
.boot { padding: 40px; text-align: center; color: var(--ink-3); }
.num { font-variant-numeric: tabular-nums; font-family: var(--mono); }

/* top bar */
.top { position: sticky; top: 0; z-index: 20; background: var(--top); color: var(--top-ink); display: flex; align-items: center; gap: 12px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; white-space: nowrap; cursor: pointer; }
.brand .mark { width: 26px; height: 26px; border-radius: 6px; background: var(--brand); display: grid; place-items: center; font-size: 12px; font-weight: 800; color: #fff; }
.top .grow { flex: 1; }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 4px 10px; border-radius: 999px; background: rgba(255,255,255,.08); color: var(--top-ink); white-space: nowrap; border: 0; cursor: pointer; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; }
.pill.synced .dot { background: #22c55e; } .pill.syncing .dot { background: #60a5fa; animation: pulse 1s infinite; }
.pill.offline .dot { background: #f59e0b; } .pill.error .dot { background: #ef4444; }
@keyframes pulse { 50% { opacity: .3; } }
.iconbtn { background: transparent; border: 0; color: inherit; padding: 6px 8px; border-radius: 8px; cursor: pointer; }
.iconbtn:hover { background: rgba(255,255,255,.1); }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink); padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:disabled { opacity: .5; cursor: default; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger { color: var(--bad); }

main { max-width: 1500px; margin: 0 auto; padding: 16px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 500px at 50% -10%, rgba(200,16,46,.18), transparent), var(--bg); }
.login .card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.login h1 { margin: 10px 0 4px; font-size: 22px; } .login p { color: var(--ink-2); margin: 0 0 18px; }
.field { display: grid; gap: 6px; margin-bottom: 12px; } .field label { font-size: 12px; color: var(--ink-2); font-weight: 600; }
.field input { border: 1px solid var(--line-2); background: var(--surface-2); border-radius: 8px; padding: 10px 12px; width: 100%; }
.err { color: var(--bad); font-size: 13px; min-height: 18px; } .note { color: var(--ink-3); font-size: 12px; }
.linkbtn { background: none; border: 0; color: var(--ref); cursor: pointer; padding: 0; font-size: 13px; }

/* run list */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar h2 { margin: 0 8px 0 0; font-size: 20px; }
.search { flex: 1 1 260px; min-width: 0; border: 1px solid var(--line-2); background: var(--surface); border-radius: 8px; padding: 9px 12px; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { border: 1px solid var(--line-2); background: var(--surface); border-radius: 999px; padding: 4px 11px; font-size: 13px; cursor: pointer; }
.chip.on { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.list table { width: 100%; border-collapse: collapse; }
.list th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--ink-3); padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--surface-2); white-space: nowrap; }
.list td { padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.list tr:last-child td { border-bottom: 0; }
.list tbody tr { cursor: pointer; } .list tbody tr:hover { background: var(--surface-2); }
.tag { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2); }
.tag.warn { color: var(--warn); border-color: currentColor; } .tag.bad { color: var(--bad); border-color: currentColor; }
.empty { padding: 48px 16px; text-align: center; color: var(--ink-2); }
.hide-sm { } @media (max-width: 760px) { .hide-sm { display: none; } .list td, .list th { padding: 9px 8px; } }

/* sheet */
.sheethead { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; box-shadow: var(--shadow); }
.sheethead .hf { display: grid; gap: 4px; } .sheethead .hf label { font-size: 11px; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .5px; }
.sheethead input, .sheethead select { border: 1px solid var(--input-line); background: var(--input-bg); border-radius: 7px; padding: 7px 9px; min-width: 0; }
.sheethead .hf.w-sm input, .sheethead .hf.w-sm select { width: 90px; } .sheethead .hf.w-md input { width: 150px; }
@media (max-width: 600px) { .sheethead .hf.w-md input { width: 140px; } }
.sheethead .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; margin: 0 0 12px; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 9px 14px; cursor: pointer; font-weight: 600; color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.on { color: var(--ink); border-bottom-color: var(--brand); }
.banner { border: 1px solid var(--warn); background: color-mix(in srgb, var(--warn) 10%, var(--surface)); color: var(--ink); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 12px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.banner.bad { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 8%, var(--surface)); }

.grid { columns: 340px; column-gap: 12px; }
.card { break-inside: avoid; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin: 0 0 12px; box-shadow: var(--shadow); overflow: hidden; }
.card > h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .6px; padding: 9px 12px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; cursor: default; }
.card > h3::before { content: ""; width: 4px; height: 14px; border-radius: 2px; background: var(--accent, var(--ink-3)); }
.card.ref { --accent: var(--ref); } .card.run { --accent: var(--run); } .card.new { --accent: var(--new); } .card.out { --accent: var(--out); }
.card.hl { border-color: color-mix(in srgb, var(--out) 45%, var(--line)); }
.card.collapsible > h3 { cursor: pointer; } .card.collapsible > h3::after { content: "▾"; margin-left: auto; color: var(--ink-3); }
.card.collapsed > h3::after { content: "▸"; } .card.collapsed > .body { display: none; }
.grp + .grp { border-top: 1px dashed var(--line); }
.grp h4 { margin: 0; padding: 8px 12px 0; font-size: 11px; color: var(--ink-3); font-weight: 700; text-transform: uppercase; letter-spacing: .4px; }
.row { display: grid; grid-template-columns: 1fr 128px; align-items: center; gap: 8px; padding: 4px 12px; min-height: 34px; }
.row .lbl { color: var(--ink-2); font-size: 13px; }
.row .val { text-align: right; font-weight: 650; }
.row.big .val { font-size: 18px; color: var(--out); } .row.big .lbl { color: var(--ink); font-weight: 600; }
.cell-in { width: 100%; text-align: right; border: 1px solid var(--input-line); background: var(--input-bg); border-radius: 6px; padding: 5px 8px; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.cell-in:focus { outline: 2px solid var(--input-focus); outline-offset: -1px; }
.cell-in.changed { box-shadow: inset 3px 0 0 var(--input-focus); }
.cell-in[readonly] { background: var(--surface-2); border-color: var(--line); }
.pctwrap { position: relative; } .pctwrap .cell-in { padding-right: 20px; } .pctwrap::after { content: "%"; position: absolute; right: 7px; top: 50%; transform: translateY(-50%); color: var(--ink-3); font-size: 12px; }
table.mini { width: 100%; border-collapse: collapse; }
table.mini th { font-size: 11px; text-transform: uppercase; color: var(--ink-3); font-weight: 700; text-align: right; padding: 6px 12px 4px; }
table.mini th:first-child { text-align: left; }
table.mini td { padding: 4px 12px; text-align: right; font-weight: 650; height: 34px; }
table.mini td:first-child { text-align: left; color: var(--ink-2); font-weight: 400; font-size: 13px; }
table.mini td .cell-in { max-width: 120px; }
table.mini tr.sub td { color: var(--ink-3); font-size: 11px; text-transform: uppercase; font-weight: 700; height: auto; padding-top: 8px; }
.after { border-top: 1px dashed var(--line); }

/* gear chart */
.gcwrap { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr) 320px; align-items: start; }
@media (max-width: 1100px) { .gcwrap { grid-template-columns: 1fr; } }
.gc { overflow: auto; max-height: calc(100vh - 220px); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.gc table { border-collapse: separate; border-spacing: 0; font-size: 13px; }
.gc th, .gc td { padding: 0 6px; height: 30px; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); text-align: right; white-space: nowrap; }
.gc thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 2; }
.gc th:first-child, .gc td:first-child { position: sticky; left: 0; background: var(--surface); z-index: 1; text-align: left; }
.gc thead th:first-child { z-index: 3; background: var(--surface-2); }
.gc tr.gear td { background: var(--surface-2); font-weight: 800; text-transform: uppercase; font-size: 11px; letter-spacing: .5px; color: var(--ink-2); text-align: left; }
.gc tr.target td { background: color-mix(in srgb, var(--out) 8%, var(--surface)); color: var(--out); font-weight: 700; }
.gc td.col { background: var(--hl-col); } .gc th.col { background: var(--hl); }
.gc td.pick { background: var(--sel); color: #fff; font-weight: 800; border-radius: 4px; }
.gc input { width: 64px; border: 1px solid transparent; background: transparent; padding: 3px 4px; border-radius: 5px; font-family: var(--mono); text-align: left; }
.gc input:hover { border-color: var(--input-line); } .gc input:focus { border-color: var(--input-focus); background: var(--input-bg); outline: 0; }
.gc th input { text-align: center; font-weight: 700; } .gc td.qty input { width: 36px; text-align: right; }
.side .card { margin-bottom: 12px; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -2px; margin-right: 4px; }

/* dialogs */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--ink); width: min(560px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10,15,22,.45); }
dialog header { padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 700; }
dialog .content { padding: 14px 16px; max-height: 60vh; overflow: auto; }
dialog footer { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.menu { position: absolute; right: 16px; top: 52px; background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.18); min-width: 230px; padding: 6px; z-index: 50; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 7px; cursor: pointer; }
.menu button:hover { background: var(--surface-2); } .menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.menu .who { padding: 8px 10px; font-size: 12px; color: var(--ink-3); }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--top); color: var(--top-ink); padding: 10px 16px; border-radius: 10px; z-index: 100; box-shadow: 0 10px 30px rgba(0,0,0,.25); font-size: 13px; max-width: calc(100vw - 32px); }
.kbd { font-family: var(--mono); font-size: 11px; color: var(--ink-3); }
@media (max-width: 600px) { main { padding: 12px; } .top { padding: 10px 12px; } .grid { columns: 1; } .sheethead .actions { margin-left: 0; width: 100%; } .row { grid-template-columns: 1fr 120px; } }
@media print { .top, .tabs, .sheethead .actions, .banner { display: none !important; } .card { box-shadow: none; } body { background: #fff; } }
