:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --compare: #b4b3ad;
  --grid: #e9e8e4;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --critical-text: #b42626;
  --row-hover: #f2f1ee;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
}
@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;
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --compare: #57564f;
    --grid: #2a2a27;
    --good-text: #3fc23f;
    --critical-text: #ef7b7b;
    --row-hover: #222220;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2b;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #8e8d86; --accent: #3987e5;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --compare: #57564f; --grid: #2a2a27;
  --good-text: #3fc23f; --critical-text: #ef7b7b; --row-hover: #222220; --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, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }

/* ---------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; font-size: 15px; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.brand-dot { width: 10px; height: 10px; border-radius: 3px; background: var(--accent); }
.tabs { display: flex; gap: 2px; }
.tab {
  border: 0; background: none; padding: 7px 12px; border-radius: 8px; cursor: pointer; color: var(--text-2);
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab[aria-selected="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.spacer { flex: 1; }
.fresh { color: var(--muted); font-size: 12.5px; }
.fresh.err { color: var(--critical-text); }
select, input[type=text], input[type=password], input[type=search] {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; min-height: 34px;
}
select:focus, input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 12px; cursor: pointer; min-height: 34px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical-text); }
.btn.small { padding: 3px 9px; min-height: 28px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.menu { position: relative; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 200px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 6px; box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
.menu-pop button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu-pop button:hover { background: var(--surface-2); }
.menu-pop .who { padding: 6px 10px 8px; color: var(--muted); font-size: 12.5px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }

/* ---------- layout */
main { max-width: 1320px; margin: 0 auto; padding: 20px 24px 60px; }
.view-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin: 4px 0 16px; }
.view-head h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
.view-head .sub { color: var(--muted); }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; box-shadow: var(--shadow);
  min-width: 0;
}
.card h2 { font-size: 14px; font-weight: 600; margin: 0 0 2px; }
.card .hint { color: var(--muted); font-size: 12.5px; margin: 0 0 12px; }
.chart-box { position: relative; height: 260px; }
.chart-box.short { height: 200px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--text-2); margin: -4px 0 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend i.line { height: 2px; border-radius: 1px; width: 14px; }
.legend i.dash { height: 0; border-top: 2px dashed var(--compare); border-radius: 0; width: 14px; background: none !important; }

/* ---------- stat tiles */
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.tile .label { color: var(--text-2); font-size: 12.5px; }
.tile .value { font-size: 24px; font-weight: 600; letter-spacing: -.02em; margin: 2px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile.hero .value { font-size: 30px; }
.tile .delta { font-size: 12.5px; color: var(--muted); }
.delta .up { color: var(--good-text); font-weight: 600; }
.delta .down { color: var(--critical-text); font-weight: 600; }
.tile.status { cursor: pointer; }
.tile.status:hover { border-color: var(--accent); }

/* ---------- tables */
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 12px; position: sticky; top: 0; background: var(--surface); }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sorted::after { content: " ↓"; }
th.sorted.asc::after { content: " ↑"; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--row-hover); }
tbody tr.click { cursor: pointer; }
td .pos { color: var(--good-text); }
td .neg { color: var(--critical-text); }
td.user { font-weight: 550; }
.empty { color: var(--muted); padding: 18px 0; text-align: center; }
.more { margin-top: 10px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 1px 8px 1px 6px; border-radius: 99px; font-size: 12px;
  background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.chip::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.chip.active::before { background: var(--good); }
.chip.new::before { background: var(--accent); }
.chip.cooling::before { background: var(--warning); }
.chip.stopped::before { background: var(--critical); }
.chip.drop { margin-left: 4px; }
.chip.drop::before { background: var(--critical); border-radius: 1px; transform: rotate(45deg); width: 6px; height: 6px; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input[type=search] { min-width: 240px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 11px; cursor: pointer; color: var(--text-2); min-height: 32px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.count { color: var(--muted); font-size: 12.5px; }

/* ---------- player drawer */
.drawer-back { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100%); background: var(--bg); z-index: 21;
  border-left: 1px solid var(--border); overflow-y: auto; padding: 20px;
}
.drawer-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.drawer-head h2 { margin: 0; font-size: 19px; }
.kv { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 16px; }
.kv div span { display: block; color: var(--muted); font-size: 12px; }
.kv div b { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- modal */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 20px; width: min(420px, 100%); }
.modal h3 { margin: 0 0 12px; font-size: 16px; }
.modal label { display: block; font-size: 12.5px; color: var(--text-2); margin: 10px 0 4px; }
.modal input, .modal select { width: 100%; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal .err { color: var(--critical-text); font-size: 13px; margin-top: 8px; min-height: 1em; }
.secret { font: 600 16px ui-monospace, Consolas, monospace; background: var(--surface-2); padding: 10px 12px; border-radius: 8px; user-select: all; word-break: break-all; }

/* ---------- login */
.login-page { 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: 14px; padding: 26px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 18px; margin: 0 0 18px; display: flex; align-items: center; gap: 8px; }
.login-card label { display: block; font-size: 12.5px; color: var(--text-2); margin: 12px 0 4px; }
.login-card input { width: 100%; }
.login-card .btn { width: 100%; margin-top: 18px; min-height: 38px; }
.login-card .err { color: var(--critical-text); font-size: 13px; margin-top: 10px; min-height: 1.2em; }

.loading { color: var(--muted); padding: 40px 0; text-align: center; }

@media (max-width: 1000px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cols-2, .cols-3 { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  main { padding: 16px 16px 48px; }
  .tabs { order: 5; width: 100%; overflow-x: auto; }
  .fresh { display: none; }
  .tile .value { font-size: 20px; }
  .filters input[type=search] { min-width: 0; flex: 1; }
}

/* ---------- period picker */
.period-btn { display: inline-flex; align-items: center; gap: 8px; max-width: 420px; }
.period-btn .pl { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.period-btn .pc { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.period-pop {
  position: absolute; right: 0; top: calc(100% + 6px); width: min(560px, calc(100vw - 32px)); background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; padding: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.18); z-index: 15;
  display: grid; grid-template-columns: 180px 1fr; gap: 14px;
}
.period-pop h4 { margin: 0 0 6px; font-size: 12px; color: var(--muted); font-weight: 500; }
.preset-list button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.preset-list button:hover { background: var(--surface-2); }
.preset-list button[aria-pressed="true"] { background: var(--surface-2); font-weight: 600; }
.period-pop .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.period-pop input[type=date] { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; min-height: 32px; }
.period-pop .sum { color: var(--text-2); font-size: 13px; background: var(--surface-2); border-radius: 8px; padding: 8px 10px; margin: 6px 0 10px; }
.period-pop .actions { display: flex; justify-content: flex-end; gap: 8px; }
@media (max-width: 640px) { .period-pop { grid-template-columns: 1fr; right: -60px; } .period-btn { max-width: 220px; } .period-btn .pc { display: none; } }

/* ---------- misc additions */
.card-head { display: flex; align-items: flex-start; gap: 10px; }
.card-head > div:first-child { flex: 1; min-width: 0; }
.csv { flex: none; }
.chart-box.tall { height: 320px; }
textarea {
  width: 100%; min-height: 120px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  font: 13px/1.5 ui-monospace, Consolas, monospace; color: var(--text); resize: vertical;
}
textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
td.sel, th.sel { width: 28px; padding-right: 0; }
td.muted { color: var(--muted); }
tr.total td { font-weight: 600; border-top: 2px solid var(--border); }
.heat td.num { text-align: center; min-width: 46px; font-size: 12px; }
.heat td.cell { border: 2px solid var(--surface); border-radius: 4px; }
.heat th { text-align: center; }
.tiles.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.note { color: var(--muted); font-size: 12.5px; margin-top: 8px; }
.waiting { color: var(--text-2); }
@media (max-width: 1000px) { .tiles.three { grid-template-columns: 1fr; } }

/* keep the top bar on one line: the data date moves into the Account menu on narrower screens */
@media (max-width: 1640px) { .topbar .fresh { display: none; } }
@media (max-width: 1250px) { .period-btn .pc { display: none; } }
.topbar { flex-wrap: nowrap; }
@media (max-width: 1000px) { .topbar { flex-wrap: wrap; } }
