:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f3f2ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-wash: rgba(42, 120, 214, 0.1);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --seq-hi: #184f95;
  --good-text: #006300;
  --bad-text: #b02a2a;
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-critical: #d03b3b;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --seq-hi: #3987e5;
    --good-text: #0ca30c;
    --bad-text: #e66767;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -0.01em; text-decoration: none; color: var(--text-primary); }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; flex: none; }
.brand-mark svg { display: block; }

/* ---------- app shell ---------- */
.shell { display: grid; grid-template-columns: 224px minmax(0, 1fr); min-height: 100vh; }

.sidebar { background: var(--surface-1); border-right: 1px solid var(--border); padding: 20px 12px; display: flex; flex-direction: column; gap: 20px; position: sticky; top: 0; height: 100vh; }
.sidebar .brand { padding: 0 8px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text-secondary); text-decoration: none; font-weight: 500; white-space: nowrap; }
.nav a:hover { background: var(--surface-2); color: var(--text-primary); }
.nav a[aria-current="page"] { background: var(--accent-wash); color: var(--text-primary); }
.nav svg { flex: none; }
.sidebar-foot { margin-top: auto; padding: 0 8px; color: var(--text-muted); font-size: 12px; }

.main { min-width: 0; }

.topbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 14px 28px; border-bottom: 1px solid var(--border); background: var(--surface-1); position: sticky; top: 0; z-index: 5; }
.topbar .spacer { flex: 1; }
.pill { font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border); color: var(--text-secondary); background: var(--surface-2); white-space: nowrap; }
.user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; font-weight: 600; font-size: 13px; flex: none; }
.user-email { color: var(--text-secondary); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

select, .input { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-height: 34px; }
select:focus-visible, .input:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface-1); }
.seg button { border: 0; background: transparent; padding: 7px 12px; cursor: pointer; color: var(--text-secondary); font-weight: 500; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-wash); color: var(--text-primary); }

.btn { border: 1px solid var(--border); background: var(--surface-1); border-radius: 8px; padding: 7px 12px; cursor: pointer; font-weight: 500; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn[disabled] { opacity: 0.5; cursor: default; }

.content { padding: 24px 28px 48px; max-width: 1280px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { font-size: 22px; letter-spacing: -0.01em; }
.page-head p { color: var(--text-secondary); margin-top: 2px; }

/* ---------- tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.tile-label { color: var(--text-secondary); font-size: 13px; }
.tile-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; margin-top: 6px; }
.tile-value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.1; }
.tile-delta { margin-top: 6px; font-size: 12px; color: var(--text-muted); }
.delta-up { color: var(--good-text); font-weight: 600; }
.delta-down { color: var(--bad-text); font-weight: 600; }
.delta-flat { color: var(--text-secondary); font-weight: 600; }
.spark { flex: none; }

/* ---------- cards ---------- */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid > .wide { grid-column: 1 / -1; }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px 14px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.card-head h3 { font-size: 15px; font-weight: 600; }
.card-head p { color: var(--text-secondary); font-size: 13px; margin-top: 1px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 2px 0 8px; color: var(--text-secondary); font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; touch-action: pan-y; }
.chart text { font: 11px system-ui, -apple-system, "Segoe UI", sans-serif; fill: var(--text-muted); font-variant-numeric: tabular-nums; }
.chart .end-label { fill: var(--text-secondary); font-size: 12px; }

/* ---------- tables ---------- */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--text-secondary); font-weight: 500; font-size: 12px; }
td.num, th.num { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }
.muted { color: var(--text-muted); }
.sharebar { display: inline-block; height: 6px; border-radius: 3px; background: var(--series-1); vertical-align: middle; margin-right: 8px; }

.heat td.cell { text-align: center; padding: 0; border-bottom: 2px solid var(--surface-1); border-left: 2px solid var(--surface-1); }
.heat td.cell span { display: block; padding: 8px 6px; border-radius: 4px; min-width: 48px; }
.heat td.cell.empty span { color: var(--text-muted); }

.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }
.status i { width: 14px; height: 14px; border-radius: 50%; display: inline-grid; place-items: center; font-style: normal; font-size: 10px; font-weight: 700; color: #fff; flex: none; }
.status.good i { background: var(--status-good); }
.status.warn i { background: var(--status-warning); color: #0b0b0b; }
.status.bad i { background: var(--status-critical); }

/* ---------- funnel ---------- */
.hbars { display: grid; grid-template-columns: max-content minmax(0, 1fr) max-content; gap: 9px 12px; align-items: center; }
.hbars .lab { color: var(--text-secondary); }
.hbars .track { height: 18px; }
.hbars .bar { height: 18px; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.hbars .val { font-variant-numeric: tabular-nums; text-align: right; }
.hbars .row { display: contents; }

/* ---------- remote config ---------- */
.cfg td { vertical-align: middle; }
.cfg code { font: 12.5px ui-monospace, SFMono-Regular, Menlo, monospace; }
.cfg .desc { color: var(--text-secondary); font-size: 12px; white-space: normal; }
.cfg .input { width: 130px; }
.cfg tr.dirty td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
.switch { position: relative; display: inline-block; width: 38px; height: 22px; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--axis); transition: background 0.15s; pointer-events: none; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- tooltip + toast ---------- */
#tip { position: fixed; z-index: 50; pointer-events: none; background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.14); font-size: 12.5px; min-width: 120px; max-width: 260px; }
#tip .tt { color: var(--text-secondary); margin-bottom: 4px; }
#tip .tr { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
#tip .tr span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); }
#tip .tr b { font-weight: 600; font-variant-numeric: tabular-nums; margin-left: 14px; }
#tip .tot { border-top: 1px solid var(--grid); margin-top: 4px; padding-top: 4px; }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text-primary); color: var(--page); padding: 10px 16px; border-radius: 8px; font-size: 13px; z-index: 60; max-width: calc(100vw - 32px); }

/* ---------- auth pages ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 21px; margin: 22px 0 4px; letter-spacing: -0.01em; }
.auth-card .sub { color: var(--text-secondary); margin-bottom: 20px; }
.field { display: grid; gap: 5px; margin-bottom: 14px; }
.field label { font-weight: 500; font-size: 13px; }
.field .input { width: 100%; min-height: 40px; }
.auth-card .btn.primary { width: 100%; min-height: 40px; margin-top: 4px; }
.auth-note { margin-top: 14px; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); color: var(--text-secondary); font-size: 12.5px; }
.auth-alt { margin-top: 16px; text-align: center; color: var(--text-secondary); }
.error { color: var(--bad-text); font-size: 13px; min-height: 18px; margin-bottom: 6px; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; padding: 10px 16px; gap: 12px; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .sidebar .brand span.word { display: none; }
  .sidebar .brand { padding: 0; }
  .nav { flex-direction: row; }
  .sidebar-foot { display: none; }
  .topbar { padding: 10px 16px; position: static; }
  .content { padding: 18px 16px 40px; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .user-email { display: none; }
}
