/* =============================================================================
   Mobile-first. Base styles target a 390px viewport (iPhone 14/15/16); the only
   breakpoint widens the grids for larger phones and tablets. Nothing here assumes
   hover — every interactive element is a tap target of at least 44px.
   ========================================================================== */

:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #0f172a;
  --mute: #64748b;
  --faint: #94a3b8;
  --line: #e5e9f0;
  --accent: #4f46e5;
  --good: #059669;
  --warn: #d97706;
  --bad: #dc2626;
  --info: #0891b2;
  --tabbar: rgba(255, 255, 255, .88);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --card: #151b2e;
    --ink: #e8ecf5;
    --mute: #94a3b8;
    --faint: #64748b;
    --line: #233049;
    --tabbar: rgba(21, 27, 46, .90);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  /* Stop the whole page rubber-banding horizontally; wide content scrolls in its
     own container instead. */
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

/* Standalone PWAs sit under the status bar without this. */
body { padding-top: var(--safe-t); }

.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .92em; }

/* ------------------------------------------------------------------ auth */
#gate {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; justify-content: center;
  padding: 28px 22px calc(28px + var(--safe-b));
  background: var(--bg);
}
#gate h1 { font-size: 24px; margin: 0 0 6px; letter-spacing: -.02em; }
#gate p  { color: var(--mute); margin: 0 0 22px; font-size: 14px; }
#gate input {
  width: 100%; padding: 15px 16px; font-size: 16px; /* 16px stops iOS zoom-on-focus */
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--ink); margin-bottom: 12px;
}
.btn {
  width: 100%; padding: 15px 16px; font-size: 16px; font-weight: 600;
  border: 0; border-radius: 12px; background: var(--accent); color: #fff;
  min-height: 48px; cursor: pointer;
}
.btn:disabled { opacity: .55; }
.btn-sub {
  background: transparent; color: var(--mute); font-weight: 500;
  font-size: 14px; min-height: 44px; padding: 10px;
}
#gate-msg { margin-top: 14px; font-size: 14px; min-height: 20px; }
#gate-msg.err  { color: var(--bad); }
#gate-msg.ok   { color: var(--good); }

/* ---------------------------------------------------------------- header */
header {
  position: sticky; top: 0; z-index: 20;
  padding: 12px 16px 10px;
  background: var(--tabbar);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid var(--line);
}
header h1 { font-size: 19px; margin: 0; letter-spacing: -.02em; }
header .sub { font-size: 12px; color: var(--mute); margin-top: 2px; }

/* --------------------------------------------------------------- banners */
.banner {
  margin: 10px 12px 0; padding: 11px 13px; border-radius: 11px;
  font-size: 13px; line-height: 1.4;
  border: 1px solid transparent;
}
.banner.stale   { background: rgba(217,119,6,.13);  border-color: rgba(217,119,6,.35);  color: var(--warn); }
.banner.offline { background: rgba(100,116,139,.14); border-color: rgba(100,116,139,.32); color: var(--mute); }
.banner.err     { background: rgba(220,38,38,.12);  border-color: rgba(220,38,38,.35);  color: var(--bad); }
.banner b { font-weight: 650; }

/* ----------------------------------------------------------- section nav */
/* Horizontally scrollable chips — the mobile replacement for the desktop tab row. */
.chips {
  display: flex; gap: 7px; overflow-x: auto; scrollbar-width: none;
  padding: 11px 12px 3px; -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: 0 0 auto; padding: 8px 14px; min-height: 36px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--mute);
  font-size: 13.5px; font-weight: 550; white-space: nowrap; cursor: pointer;
}
.chips button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* -------------------------------------------------------------- content */
main { padding: 0 12px calc(76px + var(--safe-b)); }
.pane { display: none; }
.pane.on { display: block; }
.sect { display: none; }
.sect.on { display: block; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; margin-top: 11px;
}
.card > h2 {
  font-size: 14.5px; margin: 0 0 3px; letter-spacing: -.01em; font-weight: 650;
}
.card > .hint { font-size: 12.5px; color: var(--mute); margin: 0 0 11px; line-height: 1.45; }

/* KPI cards stack on a phone — one per row keeps the number legible. */
.kpis { display: grid; grid-template-columns: 1fr; gap: 9px; margin-top: 11px; }
.kpi {
  background: var(--card); border: 1px solid var(--line);
  border-left: 3px solid var(--faint);
  border-radius: var(--radius); padding: 12px 13px;
}
.kpi .k { font-size: 11.5px; color: var(--mute); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.kpi .v { font-size: 25px; font-weight: 680; margin: 3px 0 2px; letter-spacing: -.025em; }
.kpi .v small { font-size: 13px; font-weight: 500; color: var(--mute); }
.kpi .d { font-size: 12px; color: var(--mute); line-height: 1.4; }
.kpi.good { border-left-color: var(--good); }
.kpi.warn { border-left-color: var(--warn); }
.kpi.bad  { border-left-color: var(--bad); }
.kpi.info { border-left-color: var(--info); }

.up { color: var(--good); } .dn { color: var(--bad); } .fl { color: var(--mute); }

/* Charts get a fixed height so Chart.js has a box to fill; responsive:true
   handles the width. 200px is the smallest that stays readable at 390px. */
.chart { position: relative; height: 210px; margin-top: 4px; }
.chart.tall { height: 260px; }

/* Wide tables scroll inside their own container so the page body never does. */
.scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -14px; padding: 0 14px; }
table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
th, td { padding: 8px 9px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
th { color: var(--mute); font-weight: 600; font-size: 11.5px; text-align: right; }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--card); }
tbody tr:last-child td { border-bottom: 0; }

.tag { display: inline-block; padding: 2px 7px; border-radius: 6px; font-size: 11px; font-weight: 600; }
.t-good { background: rgba(5,150,105,.14);  color: var(--good); }
.t-bad  { background: rgba(220,38,38,.13);  color: var(--bad); }
.t-warn { background: rgba(217,119,6,.14);  color: var(--warn); }
.t-info { background: rgba(8,145,178,.13);  color: var(--info); }
.t-mute { background: rgba(100,116,139,.13); color: var(--mute); }

.note { font-size: 12.5px; color: var(--mute); line-height: 1.5; margin-top: 10px;
        padding: 10px 12px; border-radius: 10px; background: rgba(100,116,139,.07); }
.note.bad  { background: rgba(220,38,38,.09);  color: var(--bad); }
.note.warn { background: rgba(217,119,6,.10);  color: var(--warn); }

dl.kv { display: grid; grid-template-columns: 1fr auto; gap: 7px 12px; margin: 0; font-size: 13px; }
dl.kv dt { color: var(--mute); }
dl.kv dd { margin: 0; text-align: right; font-weight: 600; }

/* leave bars */
.lblk { margin-bottom: 15px; }
.lrow { display: flex; justify-content: space-between; font-size: 13.5px; margin-bottom: 6px; }
.bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 99px; }

/* collapsible detail — keeps the phone screen short by default */
details { margin-top: 11px; }
details > summary {
  list-style: none; cursor: pointer; min-height: 44px;
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13.5px; font-weight: 600; color: var(--mute);
}
details > summary::-webkit-details-marker { display: none; }
details > summary::after { content: '›'; transform: rotate(90deg); font-size: 19px; color: var(--faint); }
details[open] > summary::after { transform: rotate(-90deg); }

.loading { padding: 46px 16px; text-align: center; color: var(--mute); font-size: 14px; }

/* -------------------------------------------------------------- tab bar */
nav.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex;
  background: var(--tabbar);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--line);
  /* Without this the bar sits under the home indicator. */
  padding-bottom: var(--safe-b);
}
nav.tabbar button {
  flex: 1; border: 0; background: none; cursor: pointer;
  padding: 8px 4px 6px; min-height: 50px;
  color: var(--mute); font-size: 11px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
nav.tabbar button.on { color: var(--accent); }
nav.tabbar .ico { font-size: 19px; line-height: 1; }

/* --------------------------------------------------------- wider phones */
@media (min-width: 480px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .chart { height: 240px; }
}
@media (min-width: 760px) {
  main { max-width: 900px; margin: 0 auto; }
  .kpis { grid-template-columns: repeat(3, 1fr); }
}
