:root {
  color-scheme: light;
  --bg: #f5f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --line: #e4e3de;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --brand: #1c5cab;
  --brand-ink: #ffffff;
  --brand-soft: #e6effa;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #0a7d0a;
  --good-bg: #e5f4e5;
  --warning: #8a5a00;
  --warning-bg: #fdf1d6;
  --serious: #a34b1f;
  --serious-bg: #fbe7dd;
  --critical: #b42d2d;
  --critical-bg: #fbe5e5;
  --info: #1c5cab;
  --info-bg: #e6effa;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --line: #33332f;
    --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8e86;
    --brand: #5598e7; --brand-ink: #0b0b0b; --brand-soft: #1b2a3d;
    --series-1: #3987e5; --series-2: #d95926;
    --good: #5fd35f; --good-bg: #16301a; --warning: #f5c04d; --warning-bg: #352a10;
    --serious: #f0956f; --serious-bg: #3a2218; --critical: #f07a7a; --critical-bg: #3a1a1a;
    --info: #86b6ef; --info-bg: #1b2a3d;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --line: #33332f;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8e86;
  --brand: #5598e7; --brand-ink: #0b0b0b; --brand-soft: #1b2a3d;
  --series-1: #3987e5; --series-2: #d95926;
  --good: #5fd35f; --good-bg: #16301a; --warning: #f5c04d; --warning-bg: #352a10;
  --serious: #f0956f; --serious-bg: #3a2218; --critical: #f07a7a; --critical-bg: #3a1a1a;
  --info: #86b6ef; --info-bg: #1b2a3d;
  --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.5 var(--font); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; line-height: 1.25; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
small { font-size: 12px; }
code { font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.neg { color: var(--critical); }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 15px; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand.big { font-size: 20px; justify-content: center; margin-bottom: 16px; }
.logo { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; background: var(--brand); color: var(--brand-ink); font-weight: 800; font-size: 12px; }
.mainnav { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.mainnav a { padding: 8px 12px; border-radius: 8px; color: var(--text-2); white-space: nowrap; }
.mainnav a:hover { background: var(--surface-2); text-decoration: none; }
.mainnav a[aria-current="page"] { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.me { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.me > a:not(.btn) { color: var(--text-2); }
.nav-toggle { display: none; }

.wrap { max-width: 1320px; margin: 0 auto; padding: 20px; }
.pagehead { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Buttons & forms */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 6px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--critical); border-color: var(--critical); background: transparent; }
.btn.sm { min-height: 30px; padding: 3px 10px; font-size: 13px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--brand); font: inherit; cursor: pointer; }
form.inline { display: inline; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 10px; min-height: 36px; width: 100%; }
textarea { resize: vertical; }
input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; accent-color: var(--brand); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.hint { font-size: 12px; color: var(--muted); margin: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: end; }
.w-sm { width: 80px; }

.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 14px; padding: 12px; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius); }
.filters label { min-width: 130px; }
.filters label.grow { flex: 1 1 220px; }

/* Cards & layout */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card.narrow { max-width: 480px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid2 > .card { margin: 0; }
.span2 { grid-column: 1 / -1; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.kpi-label { color: var(--text-2); font-size: 13px; }
.kpi-val { font-size: 24px; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.delta { font-size: 12px; }
.delta.up { color: var(--good); }
.delta.down { color: var(--critical); }
.note { font-size: 13px; color: var(--text-2); background: var(--warning-bg); border-radius: 8px; padding: 8px 12px; margin: 0 0 16px; }
.card .note { margin: 10px 0 0; background: none; padding: 0; font-size: 12px; color: var(--muted); }

/* Tables */
.tablewrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 16px; }
.card .tablewrap { border: 0; margin: 0 -16px 12px; border-radius: 0; }
table.t { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.t th, .t td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.t thead th { position: sticky; top: 0; background: var(--surface-2); font-size: 12px; font-weight: 600; color: var(--text-2); white-space: nowrap; }
.t tbody tr:hover { background: var(--surface-2); }
.t tr.sel { background: var(--brand-soft); }
.t .num { text-align: right; white-space: nowrap; }
.t.compact th, .t.compact td { padding: 6px 10px; }
.t td.clip { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.t td input, .t td select { min-width: 120px; }
.t tfoot td, .t tfoot th { background: var(--surface-2); border-top: 1px solid var(--line); }
.pivot tbody th { font-weight: 500; white-space: nowrap; }
.sumline { margin: 0 0 10px; color: var(--text-2); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--text-2); }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.warning { background: var(--warning-bg); color: var(--warning); }
.badge.serious { background: var(--serious-bg); color: var(--serious); }
.badge.critical { background: var(--critical-bg); color: var(--critical); }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.muted { background: var(--surface-2); color: var(--muted); }

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; background: var(--good-bg); color: var(--good); }
.flash.err { background: var(--critical-bg); color: var(--critical); }
.flash.warn { background: var(--warning-bg); color: var(--warning); }

.pager { display: flex; gap: 4px; flex-wrap: wrap; }
.pager a, .pager span { min-width: 34px; padding: 6px 10px; text-align: center; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); }
.pager span[aria-current] { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }

.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tabs a { padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--text-2); }
.tabs a[aria-current] { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); font-weight: 600; }

/* Charts */
.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { stroke: var(--muted); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 12px; font-family: var(--font); }
.chart .hitbox { fill: transparent; }
.chart .hit:hover .hitbox, .chart .hit:focus .hitbox { fill: var(--surface-2); }
.chart .hit:focus { outline: none; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-2); margin-bottom: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.tableview { margin-top: 8px; }
.tableview summary { cursor: pointer; color: var(--brand); font-size: 13px; }
.hbars { display: flex; flex-direction: column; gap: 8px; }
.hbar { display: grid; grid-template-columns: minmax(110px, 30%) 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.hb-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hb-track { height: 14px; background: var(--surface-2); border-radius: 0 4px 4px 0; }
.hb-fill { display: block; height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.hb-val { white-space: nowrap; font-variant-numeric: tabular-nums; text-align: right; min-width: 120px; }
.hb-val small { color: var(--muted); }
.chart-tip { position: fixed; z-index: 50; pointer-events: none; background: var(--text); color: var(--surface); padding: 6px 10px; border-radius: 6px; font-size: 12px; white-space: pre; box-shadow: 0 4px 12px rgba(0,0,0,.2); }

/* Lists */
.todo { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; }
.todo li { padding: 8px 10px; background: var(--surface-2); border-radius: 8px; }
.timeline { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; max-height: 420px; overflow: auto; }
.timeline li { border-left: 3px solid var(--brand); padding: 2px 0 2px 10px; }
.timeline li.done { border-color: var(--line); }
.tl-meta { display: block; font-size: 12px; color: var(--muted); }

/* Order form */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px 14px; margin: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
fieldset > .span2, fieldset > .hint.span2 { grid-column: 1 / -1; }
legend { padding: 0 6px; font-weight: 600; font-size: 13px; }
.formactions { display: flex; gap: 8px; }
.profit-preview { align-self: end; padding: 8px 12px; border-radius: 8px; background: var(--surface-2); font-variant-numeric: tabular-nums; min-height: 36px; }
.danger-zone { margin-top: 8px; }
.autocomplete { position: relative; grid-column: span 2; }
.ac-list { position: absolute; z-index: 30; left: 0; right: 0; top: 100%; margin: 4px 0 0; padding: 4px; list-style: none; background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.12); max-height: 320px; overflow: auto; }
.ac-list li { padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.ac-list li[aria-selected="true"], .ac-list li:hover { background: var(--brand-soft); }
.ac-list small { color: var(--muted); margin-left: 6px; }

.auth-card { max-width: 380px; margin: 10vh auto; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px; }
.auth-card h1 { margin-bottom: 8px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 10px 16px; }
  .nav-toggle { display: inline-flex; margin-left: auto; }
  .mainnav { display: none; order: 3; flex-basis: 100%; flex-direction: column; }
  .mainnav.open { display: flex; }
  .me { order: 2; flex-basis: 100%; justify-content: space-between; }
  .grid2, .form-grid { grid-template-columns: minmax(0, 1fr); }
  fieldset { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .wrap { padding: 16px; }
}
@media (max-width: 560px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-val { font-size: 20px; }
  fieldset, .row2 { grid-template-columns: minmax(0, 1fr); }
  .autocomplete { grid-column: auto; }
  .filters label { min-width: 0; flex: 1 1 45%; }
  .hbar { grid-template-columns: 1fr auto; }
  .hb-track { grid-column: 1 / -1; grid-row: 2; }
  .hb-val { min-width: 0; }
  .chart .tick { font-size: 28px; }
}
