:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #efeeea;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --grid: #e6e5e0;
  --series: #2a78d6;
  --series-soft: rgba(42, 120, 214, 0.12);
  --good: #0f7b3e;
  --bad: #c0362c;
  --warn: #9a6400;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #2e2e2b;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #8e8d86;
    --grid: #2a2a27;
    --series: #3987e5;
    --series-soft: rgba(57, 135, 229, 0.16);
    --good: #4cc27a;
    --bad: #ef6b60;
    --warn: #e0a83a;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #2e2e2b;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #8e8d86;
  --grid: #2a2a27;
  --series: #3987e5;
  --series-soft: rgba(57, 135, 229, 0.16);
  --good: #4cc27a;
  --bad: #ef6b60;
  --warn: #e0a83a;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
button, input, select { font: inherit; color: inherit; }
h2 { font-size: 16px; margin: 0; font-weight: 650; }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }

/* login */
body.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px; display: grid; gap: 10px;
}
.login-card .brand { margin-bottom: 12px; }
.login-card label { color: var(--text-2); font-size: 13px; }
.login-card input {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.login-card button, .btn {
  padding: 10px 14px; border: 0; border-radius: 8px; background: var(--series); color: #fff; font-weight: 600; cursor: pointer;
}
.error { color: var(--bad); min-height: 1.2em; margin: 0; font-size: 13px; }

/* layout */
.top {
  position: sticky; top: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: 12px 24px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.top-right { display: flex; align-items: center; gap: 12px; font-size: 13px; }
main { max-width: 1400px; margin: 0 auto; padding: 20px 24px 60px; display: grid; gap: 28px; }
.section-head { display: grid; gap: 4px; margin-bottom: 12px; }
.section-head.row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; }
.section-head p { margin: 0; font-size: 13px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.ghost {
  background: transparent; border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.ghost:hover { background: var(--surface-2); }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi .label { color: var(--text-2); font-size: 12.5px; }
.kpi .value { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; margin-top: 2px; }
.kpi .sub { font-size: 12.5px; color: var(--muted); }
.up { color: var(--good); }
.down { color: var(--bad); }

/* channel cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px;
  display: grid; gap: 10px; cursor: pointer; text-align: left; transition: border-color .15s; min-width: 0;
}
.card:hover { border-color: color-mix(in srgb, var(--series) 50%, var(--border)); }
.card.selected { border-color: var(--series); box-shadow: 0 0 0 1px var(--series); }
.card-head { display: flex; align-items: center; gap: 10px; }
.card-head > div { min-width: 0; }
.card-head img { width: 32px; height: 32px; border-radius: 50%; background: var(--surface-2); }
.card-head .name { font-weight: 650; }
.card-head .handle { font-size: 12px; color: var(--muted); }
.card-head .refresh { margin-left: auto; }
.card-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.card-stats .v { font-size: 18px; font-weight: 700; }
.card-stats .l { font-size: 11.5px; color: var(--muted); }
.card-foot { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); gap: 8px; }
.card .warn { font-size: 12px; color: var(--warn); overflow-wrap: anywhere; }
.refresh {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; padding: 5px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); cursor: pointer;
}
.refresh:hover:not(:disabled) { background: var(--surface-2); }
.refresh:disabled { opacity: .7; cursor: default; }
.refresh svg { width: 14px; height: 14px; }
.refresh.busy svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.spark { width: 100%; height: 36px; display: block; }

/* segmented control & filters */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--surface-2); font-weight: 600; }
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; font-size: 13px; color: var(--text-2); }
.filters select { margin-left: 6px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.check { display: inline-flex; align-items: center; gap: 6px; }
input[type="search"] {
  padding: 7px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); width: min(320px, 100%);
}

/* charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.axis text { fill: var(--muted); font-size: 11px; }
.gridline { stroke: var(--grid); stroke-width: 1; }
.line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.area { fill: var(--series-soft); }
.cross { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; font-size: 13px; }

/* pattern bars */
.bars { display: grid; gap: 6px; }
.bar-row { display: grid; grid-template-columns: minmax(120px, 220px) 1fr 140px; gap: 12px; align-items: center; font-size: 13px; }
.bar-row .k { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.bar-track { height: 18px; position: relative; }
.bar { height: 100%; background: var(--series); border-radius: 0 4px 4px 0; min-width: 2px; }
.bar-row .n { color: var(--muted); font-size: 12px; }
.bar-row .n b { color: var(--text); font-weight: 650; }
.bar-row.weak { opacity: .55; }
.bar-row:hover .bar { filter: brightness(1.1); }

/* table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
th { font-size: 12px; color: var(--text-2); font-weight: 600; cursor: pointer; user-select: none; position: sticky; top: 0; background: var(--surface); }
th.sorted::after { content: " ↓"; }
th.sorted.asc::after { content: " ↑"; }
th.l, td.l { text-align: left; }
td.title { white-space: normal; min-width: 260px; max-width: 420px; }
td.title a { color: var(--text); text-decoration: none; font-weight: 550; }
td.title a:hover { text-decoration: underline; }
td.title .sub { color: var(--muted); font-size: 12px; }
td img { width: 64px; height: 36px; object-fit: cover; border-radius: 4px; display: block; background: var(--surface-2); }
tr:hover td { background: var(--surface-2); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); font-size: 11.5px; color: var(--text-2); }

.tooltip {
  position: fixed; z-index: 20; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12.5px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .15); max-width: 280px;
}
.tooltip b { font-weight: 650; }

@media (max-width: 700px) {
  .top { padding: 10px 16px; }
  main { padding: 16px 16px 40px; }
  .top-right .muted { display: none; }
  .bar-row { grid-template-columns: 100px 1fr 90px; }
}
