:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --border: #e3e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --grid: #ebe9e4;
  --accent: #2a78d6;
  --good: #0ca30c;
  --warning: #b07800;
  --critical: #d03b3b;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
  --lag: rgba(120, 118, 112, .22);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --border: #2c2c2a;
    --text: #ffffff; --text-2: #c3c2b7; --text-3: #8f8e86; --grid: #2a2a28;
    --accent: #3987e5; --good: #2fbf2f; --warning: #fab219; --critical: #e66767;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --shadow: none; --lag: rgba(200, 198, 190, .16);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --border: #2c2c2a;
  --text: #ffffff; --text-2: #c3c2b7; --text-3: #8f8e86; --grid: #2a2a28;
  --accent: #3987e5; --good: #2fbf2f; --warning: #fab219; --critical: #e66767;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --shadow: none; --lag: rgba(200, 198, 190, .16);
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: inherit; }
.wrap { max-width: 1440px; margin: 0 auto; padding: 20px 16px 48px; }

/* Top bar */
.top { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; margin-bottom: 20px; }
.top h1 { font-size: 20px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
.top .meta { color: var(--text-3); font-size: 13px; }
.top .meta.stale { color: var(--critical); }
.top .spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button {
  border: 0; background: none; color: var(--text-2); padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--text); color: var(--surface); }
.back { text-decoration: none; color: var(--text-2); font-size: 13px; }
.back:hover { color: var(--text); }

/* Overview grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(330px, 100%), 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px 12px; box-shadow: var(--shadow); position: relative; min-width: 0;
}
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.card-head .name { font-size: 16px; font-weight: 650; text-decoration: none; }
.card-head .name:hover { text-decoration: underline; text-underline-offset: 3px; }
.card-head .cur { color: var(--text-3); font-size: 12px; }
.card-head .spacer { flex: 1; }
.icon-btn {
  border: 1px solid transparent; background: none; color: var(--text-3); border-radius: 6px;
  width: 28px; height: 28px; cursor: pointer; display: grid; place-items: center;
}
.icon-btn:hover, .icon-btn[aria-expanded="true"] { border-color: var(--border); color: var(--text); }

.picker {
  position: absolute; right: 12px; top: 46px; z-index: 5; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px; width: 230px;
  box-shadow: 0 8px 24px rgba(0,0,0,.14);
}
.picker label { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.picker label:hover { background: var(--bg); }
.picker .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.picker .hint { color: var(--text-3); font-size: 12px; padding: 4px 6px 2px; }

/* Budget pacing */
.pace { margin: 0 0 10px; font-size: 12.5px; color: var(--text-2); }
.pace .bar { height: 6px; background: var(--grid); border-radius: 3px; margin-top: 5px; position: relative; overflow: hidden; }
.pace .bar span { position: absolute; inset: 0 auto 0 0; background: var(--text-3); border-radius: 3px; }
.pace .bar i { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text); }
.status { font-weight: 600; white-space: nowrap; }
.status.good { color: var(--good); } .status.warn { color: var(--warning); } .status.bad { color: var(--critical); }

/* Metric rows in a card */
.metric { display: grid; grid-template-columns: 1fr auto; align-items: end; gap: 0 12px; padding: 8px 0 2px; border-top: 1px solid var(--grid); }
.metric .label { font-size: 12px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.metric .label .swatch { width: 8px; height: 8px; border-radius: 2px; }
.metric .value { font-size: 20px; font-weight: 650; letter-spacing: -.01em; }
.metric .delta { font-size: 12px; color: var(--text-3); text-align: right; }
.metric .chart { grid-column: 1 / -1; height: 44px; margin-top: 4px; }
.err { color: var(--critical); font-size: 13px; padding: 8px 0; }
.empty { color: var(--text-3); font-size: 13px; padding: 10px 0 4px; border-top: 1px solid var(--grid); }

/* Charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .axis { fill: var(--text-3); font-size: 10.5px; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--border); stroke-width: 1; }
.chart .target { stroke: var(--text-2); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .cross { stroke: var(--text-3); stroke-width: 1; }
.chart .lag { fill: var(--lag); pointer-events: none; }
.tip-lag { margin-top: 6px; padding-top: 6px; border-top: 1px solid rgba(127,127,127,.4); white-space: normal; width: 300px; max-width: 80vw; line-height: 1.4; }
.tip-lag div + div { margin-top: 4px; }
.tip {
  position: fixed; z-index: 20; pointer-events: none; background: var(--text); color: var(--surface);
  padding: 6px 9px; border-radius: 6px; font-size: 12px; line-height: 1.35; white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0,0,0,.18);
}
.tip b { font-weight: 650; }

/* Client page */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.kpi .label { font-size: 12px; color: var(--text-2); }
.kpi .value { font-size: 22px; font-weight: 650; letter-spacing: -.01em; }
.kpi .delta { font-size: 12px; color: var(--text-3); }
.panels { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(420px, 100%), 1fr)); gap: 16px; margin-bottom: 16px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; min-width: 0; }
.panel h2 { font-size: 14px; margin: 0 0 2px; font-weight: 650; }
.panel .sub { font-size: 12px; color: var(--text-3); margin-bottom: 8px; }
.panel .chart { height: 170px; }
.tbl-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 7px 10px; text-align: right; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--text-2); font-weight: 600; font-size: 12px; cursor: pointer; user-select: none; }
th:first-child, td:first-child { text-align: left; white-space: normal; min-width: 180px; }
td.muted, .muted { color: var(--text-3); }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; border: 1px solid var(--border); color: var(--text-2); }
.links { display: flex; gap: 14px; font-size: 13px; color: var(--text-2); }
.note { color: var(--text-3); font-size: 12px; margin-top: 24px; max-width: 760px; }

@media (max-width: 520px) {
  .panels { grid-template-columns: 1fr; }
  .grid { grid-template-columns: 1fr; }
}

/* Sign-in pages */
.auth { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 32px 16px; gap: 20px; }
.auth-brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 650; font-size: 18px; text-decoration: none; color: var(--text); }
.auth-brand svg { color: #c2410c; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 20px; margin: 0 0 14px; }
.auth-card p { margin: 0 0 12px; }
.auth-card form, .add-user { display: grid; gap: 12px; }
.auth-card label, .add-user label { display: grid; gap: 5px; font-size: 13px; color: var(--text-2); }
.auth-card input, .add-user input, .add-user select {
  font: inherit; font-size: 15px; color: var(--text); background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; width: 100%;
}
.auth-card input:focus, .add-user input:focus, .add-user select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.auth-card button, .add-user button {
  font: inherit; font-weight: 600; font-size: 15px; border: 0; border-radius: 8px; padding: 10px 14px; cursor: pointer;
  background: var(--text); color: var(--surface); margin-top: 4px;
}
.auth-foot { font-size: 13px; color: var(--text-2); margin: 14px 0 0 !important; }
.auth-msg { padding: 9px 11px; border-radius: 8px; font-size: 13px; }
.auth-msg.err { background: color-mix(in srgb, var(--critical) 12%, transparent); color: var(--text); }
.auth-msg.ok { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--text); }
.add-user { grid-template-columns: 2fr 2fr auto; align-items: end; }
@media (max-width: 640px) { .add-user { grid-template-columns: 1fr; } }
.banner { border-radius: 10px; padding: 12px 14px; margin-bottom: 16px; display: grid; gap: 8px; font-size: 13px; border: 1px solid var(--border); background: var(--surface); }
.banner.ok { border-color: color-mix(in srgb, var(--good) 45%, var(--border)); }
.banner.err { border-color: color-mix(in srgb, var(--critical) 45%, var(--border)); }
.link-box { font: 12px ui-monospace, Menlo, monospace; width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); }
table.users th, table.users td { text-align: left; vertical-align: middle; }
table.users td.actions { text-align: right; }
table.users td.actions form { display: inline-block; margin-left: 6px; }
.btn-quiet { font: inherit; font-size: 12px; border: 1px solid var(--border); background: none; color: var(--text-2); border-radius: 6px; padding: 4px 9px; cursor: pointer; }
.btn-quiet:hover { color: var(--text); border-color: var(--text-3); }
.btn-quiet.danger:hover { color: var(--critical); border-color: var(--critical); }
