:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #1c5cab;
  --accent-ink: #ffffff;
  --accent-soft: #e6eefa;
  --series: #2a78d6;
  --series-2: #eb6834;
  --grid: #e8e7e3;
  --good: #0a7d0a;
  --good-soft: #e5f3e5;
  --warn: #8a5a00;
  --warn-soft: #fdf2d9;
  --serious: #a8461f;
  --serious-soft: #fbe9e1;
  --bad: #b42f2f;
  --bad-soft: #f9e3e3;
  --info-soft: #eceaf7;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #f4f4f2;
    --text-2: #c3c2b7;
    --muted: #94938b;
    --accent: #5598e7;
    --accent-ink: #0b0b0b;
    --accent-soft: #1d2a3b;
    --series: #3987e5;
    --series-2: #d95926;
    --grid: #2c2c29;
    --good: #4cc44c;
    --good-soft: #17301a;
    --warn: #f0b43a;
    --warn-soft: #352a10;
    --serious: #ec835a;
    --serious-soft: #3a2218;
    --bad: #ef7676;
    --bad-soft: #3a1c1c;
    --info-soft: #25223a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text: #f4f4f2; --text-2: #c3c2b7; --muted: #94938b;
  --accent: #5598e7; --accent-ink: #0b0b0b; --accent-soft: #1d2a3b;
  --series: #3987e5; --series-2: #d95926; --grid: #2c2c29;
  --good: #4cc44c; --good-soft: #17301a; --warn: #f0b43a; --warn-soft: #352a10;
  --serious: #ec835a; --serious-soft: #3a2218; --bad: #ef7676; --bad-soft: #3a1c1c;
  --info-soft: #25223a; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.small { font-size: 12.5px; }
.hint { color: var(--muted); font-size: 12.5px; margin: 4px 0 0; }
code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

/* --- buttons & inputs --- */
.btn {
  font: inherit; font-weight: 550; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 7px 13px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.link { font: inherit; background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; }
.link:hover { text-decoration: underline; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
label { display: block; font-weight: 550; font-size: 13px; color: var(--text-2); }
label > input, label > select, label > textarea { margin-top: 4px; font-weight: 400; }
.form-error { color: var(--bad); min-height: 1em; margin: 8px 0 0; font-size: 13px; }

/* --- login --- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 12px;
}
.login-card h1 { margin-top: 4px; }
.login-card p { margin: 0; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 750; font-size: 14px; flex: none;
}

/* --- shell --- */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.nav {
  background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px;
  display: flex; flex-direction: column; gap: 16px; position: sticky; top: 0; height: 100vh;
}
.nav-brand { display: flex; gap: 10px; align-items: center; padding: 0 6px; }
.nav-client { font-weight: 650; line-height: 1.2; }
.nav-sub { color: var(--muted); font-size: 12px; font-family: var(--mono); }
.nav nav { display: grid; gap: 2px; }
.nav nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 520;
}
.nav nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .ico { width: 18px; text-align: center; opacity: .85; }
.badge {
  margin-left: auto; background: var(--bad); color: #fff; border-radius: 999px;
  font-size: 11.5px; font-weight: 700; padding: 1px 7px;
}
.nav-foot { margin-top: auto; padding: 0 6px; display: grid; gap: 4px; font-size: 13px; justify-items: start; }
.main { min-width: 0; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 12px; padding: 10px 24px; border-bottom: 1px solid var(--border);
  background: var(--surface); position: sticky; top: 0; z-index: 5;
}
.top-pills { display: flex; gap: 8px; flex-wrap: wrap; margin-left: auto; }
.nav-toggle { display: none; }
.view { padding: 24px; max-width: 1280px; width: 100%; outline: none; }
#banner:empty { display: none; }
#banner { padding: 12px 24px 0; display: grid; gap: 8px; }

/* --- pills (state: icon + label, never colour alone) --- */
.pill {
  display: inline-flex; align-items: center; gap: 5px; border-radius: 999px; padding: 2px 9px;
  font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--text-2);
  border: 1px solid transparent;
}
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.serious { background: var(--serious-soft); color: var(--serious); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.info { background: var(--accent-soft); color: var(--accent); }
.pill.outline { background: transparent; border-color: var(--border); }

/* --- callouts --- */
.callout {
  border-radius: var(--radius); padding: 12px 14px; border: 1px solid var(--border);
  background: var(--surface); display: flex; gap: 12px; align-items: flex-start;
}
.callout .ci { font-weight: 800; flex: none; width: 20px; text-align: center; }
.callout.bad { background: var(--bad-soft); border-color: transparent; }
.callout.bad .ci { color: var(--bad); }
.callout.warn { background: var(--warn-soft); border-color: transparent; }
.callout.warn .ci { color: var(--warn); }
.callout.info { background: var(--accent-soft); border-color: transparent; }
.callout.info .ci { color: var(--accent); }
.callout.good { background: var(--good-soft); border-color: transparent; }
.callout.good .ci { color: var(--good); }
.callout .cbody { flex: 1; min-width: 0; }
.callout .cbody p { margin: 2px 0 0; }
.callout .cact { flex: none; display: flex; gap: 8px; }

/* --- page layout --- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 0; }
.card-body { padding: 14px 16px 16px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: 1.6fr 1fr; }
.grid.half { grid-template-columns: 1fr 1fr; }
.stack { display: grid; gap: 16px; }
.gap-top { margin-top: 16px; }

/* --- KPI tiles --- */
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
.tile { padding: 14px 16px; }
.tile .t-label { color: var(--muted); font-size: 12.5px; font-weight: 550; }
.tile .t-value { font-size: 24px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.tile .t-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.tile.alert .t-value { color: var(--bad); }

/* --- tables --- */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; background: var(--surface); position: sticky; top: 0; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover td { background: var(--surface-2); }
td .sub { color: var(--muted); font-size: 12.5px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.reason { color: var(--text-2); font-size: 12.5px; max-width: 560px; }

/* --- filters --- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.filters input[type=search] { max-width: 280px; }
.filters select, .filters input[type=date] { width: auto; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { font: inherit; border: 0; background: none; padding: 6px 12px; cursor: pointer; color: var(--text-2); font-weight: 550; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent-soft); color: var(--accent); }
.pager { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; }

/* --- detail --- */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.kv dt { color: var(--muted); font-size: 13px; }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.timeline { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.timeline li { display: grid; grid-template-columns: 150px 1fr; gap: 12px; font-size: 13px; }
.timeline .when { color: var(--muted); font-variant-numeric: tabular-nums; }
.back { display: inline-block; margin-bottom: 10px; font-size: 13px; }
.problems { margin: 6px 0 0; padding-left: 18px; }
.problems li { margin: 2px 0; }

/* --- chart --- */
.chart { width: 100%; height: 220px; display: block; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--series); }
.chart .bar.credit { fill: var(--series-2); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.hover { filter: brightness(1.15); }
.chart-tip {
  position: fixed; pointer-events: none; z-index: 30; background: var(--text); color: var(--bg);
  border-radius: 8px; padding: 7px 10px; font-size: 12.5px; box-shadow: var(--shadow); white-space: nowrap;
}
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--text-2); }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.sw.s1 { background: var(--series); }
.sw.s2 { background: var(--series-2); }

/* --- state bars on overview --- */
.state-list { display: grid; gap: 8px; }
.state-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* --- modal --- */
dialog { border: 0; padding: 0; border-radius: 14px; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3); max-width: min(560px, calc(100vw - 32px)); width: 100%; }
dialog::backdrop { background: rgba(10,10,10,.45); }
.modal { padding: 22px; display: grid; gap: 14px; }
.modal p { margin: 0; }
.modal .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal .row.three { grid-template-columns: 1fr 1fr 1fr; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.modal.wide { min-width: 0; }
dialog.wide { max-width: min(760px, calc(100vw - 32px)); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 13.5px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 40;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; }
.toast.err { background: var(--bad); color: #fff; }

.class-results { max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.class-results button { display: block; width: 100%; text-align: left; font: inherit; border: 0; border-bottom: 1px solid var(--border); background: none; padding: 7px 10px; cursor: pointer; color: var(--text); }
.class-results button:hover { background: var(--surface-2); }
.class-results button:last-child { border-bottom: 0; }

.drag-cell { white-space: nowrap; }

@media (max-width: 1100px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.two, .grid.half { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .nav { position: fixed; inset: 0 auto 0 0; width: 250px; z-index: 20; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .nav.open { transform: none; }
  .nav-toggle { display: inline-flex; }
  .top { padding: 10px 16px; }
  .view { padding: 16px; }
  #banner { padding: 12px 16px 0; }
  .tiles { grid-template-columns: 1fr 1fr; gap: 10px; }
  .tile .t-value { font-size: 19px; }
  .modal .row, .modal .row.three { grid-template-columns: 1fr; }
  .timeline li { grid-template-columns: 1fr; gap: 0; }
  .filters input[type=search] { max-width: none; }
  .callout { flex-wrap: wrap; }
}
