/* ------------------------------------------------------------------------
   Design tokens — Apple-leaning, calm, generous whitespace.
   Palette: neutral surfaces + a single accent (blue), semantic colors
   only for health status (ok/warn/bad). Everything else is grayscale.
   ------------------------------------------------------------------------ */

:root {
  color-scheme: light dark;

  --font-sans: 'Inter', 'Noto Sans JP', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono: ui-monospace, 'SF Mono', 'JetBrains Mono', Menlo, Consolas, monospace;

  /* Light theme (default) */
  --bg:        #fbfbfd;
  --panel:     #ffffff;
  --panel-2:   #f5f5f7;
  --panel-hov: #eeeef2;
  --border:    rgba(0, 0, 0, .08);
  --border-strong: rgba(0, 0, 0, .14);
  --divider:   rgba(0, 0, 0, .06);

  --text:      #1d1d1f;
  --text-2:    #3c3c43;
  --muted:     #86868b;
  --muted-2:   #a1a1a6;

  --accent:    #0071e3;
  --accent-2:  #0077ed;
  --accent-bg: rgba(0, 113, 227, .08);

  --ok:        #34c759;
  --warn:      #ff9f0a;
  --bad:       #ff453a;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .04), 0 0 0 1px rgba(0, 0, 0, .04);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, .06), 0 0 0 1px rgba(0, 0, 0, .04);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, .10), 0 0 0 1px rgba(0, 0, 0, .06);

  --radius-sm: 8px;
  --radius:    14px;
  --radius-lg: 20px;
  --radius-pill: 999px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --space-8: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #000000;
    --panel:     #1c1c1e;
    --panel-2:   #2c2c2e;
    --panel-hov: #3a3a3c;
    --border:    rgba(255, 255, 255, .08);
    --border-strong: rgba(255, 255, 255, .16);
    --divider:   rgba(255, 255, 255, .06);

    --text:      #f5f5f7;
    --text-2:    #ebebf0;
    --muted:     #98989d;
    --muted-2:   #6e6e73;

    --accent:    #0a84ff;
    --accent-2:  #409cff;
    --accent-bg: rgba(10, 132, 255, .16);

    --ok:        #30d158;
    --warn:      #ff9f0a;
    --bad:       #ff453a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .30), 0 0 0 1px rgba(255, 255, 255, .04);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, .40), 0 0 0 1px rgba(255, 255, 255, .04);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, .50), 0 0 0 1px rgba(255, 255, 255, .06);
  }
}

/* ------------------------------------------------------------------------
   Reset & base
   ------------------------------------------------------------------------ */

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: 'cv11', 'ss01', 'ss03';
  letter-spacing: -0.005em;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.02em; }

/* ------------------------------------------------------------------------
   Header — quiet, minimal, sticky. On mobile the tab bar drops to the
   bottom (iOS-native pattern) and the header becomes a slim brand strip.
   ------------------------------------------------------------------------ */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  /* Solid-ish background; do NOT use backdrop-filter here because it makes
     .app-header a containing block for any position:fixed descendant
     (like .tabs on mobile), which pins them inside the header instead of
     the viewport. See the mobile bottom bar below. */
  background: color-mix(in oklab, var(--bg) 92%, transparent);
  border-bottom: 1px solid var(--divider);
  padding-top: env(safe-area-inset-top, 0);
}
.header-row {
  max-width: 1200px;
  margin: 0 auto;
  padding: 14px var(--space-5);
  display: flex;
  align-items: center;
  gap: var(--space-4);
}
.brand h1 {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand .sub { display: none; }

.tabs {
  display: flex;
  gap: 2px;
  margin: 0 auto;
  background: var(--panel-2);
  padding: 3px;
  border-radius: var(--radius-pill);
  top: auto;
}
.tab {
  border: 0;
  background: transparent;
  padding: 6px 14px;
  font-size: 13px;
  color: var(--muted);
  border-radius: var(--radius-pill);
  transition: color .15s, background .15s;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active {
  color: var(--text);
  background: var(--panel);
  box-shadow: var(--shadow-sm);
}

.settings-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  width: 32px; height: 32px;
  border-radius: var(--radius-pill);
  display: inline-flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s;
}
.settings-btn:hover { color: var(--text); background: var(--panel-hov); }
.settings-btn svg { width: 16px; height: 16px; }

.status-chip {
  font-size: 12px;
  color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
.status-chip .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted-2);
  flex-shrink: 0;
}
.status-chip.ok .dot { background: var(--ok); }
.status-chip.err .dot { background: var(--bad); }

/* Settings popover */
.settings-pop {
  position: fixed;
  top: 58px;
  right: 20px;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: var(--space-4);
  min-width: 300px;
  display: none;
}
.settings-pop.open { display: block; }
.settings-pop h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: var(--space-3);
}
.settings-pop label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: var(--space-1);
  margin-top: var(--space-3);
}
.settings-pop label:first-of-type { margin-top: 0; }
.settings-pop input {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  border-radius: var(--radius-sm);
  transition: border-color .15s, background .15s;
}
.settings-pop input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--panel);
}
.settings-pop .actions {
  margin-top: var(--space-4);
  display: flex;
  gap: var(--space-2);
  justify-content: flex-end;
}
.settings-pop .actions button {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--panel);
  font-size: 13px;
}
.settings-pop .actions button.primary {
  background: var(--accent);
  color: white;
  border-color: transparent;
}
.settings-pop .actions button.primary:hover { background: var(--accent-2); }

/* ------------------------------------------------------------------------
   Layout
   ------------------------------------------------------------------------ */

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-7) var(--space-5) var(--space-8);
}
.view { display: none; }
.view.active { display: block; animation: fade .25s ease; }
@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}

.section {
  margin-bottom: var(--space-7);
}
.section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: var(--space-4);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
  min-height: 28px;
}
.section-caption {
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: -0.005em;
}

/* Small pill segmented control — used for KPI range and other in-page
   view toggles. Visually quieter than the main tab bar so it doesn't
   compete with navigation. */
.segmented {
  display: inline-flex;
  background: var(--panel-2);
  padding: 2px;
  border-radius: var(--radius-pill);
}
.segmented .seg {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  padding: 4px 12px;
  border-radius: var(--radius-pill);
  transition: color .15s, background .15s;
  white-space: nowrap;
}
.segmented .seg:hover { color: var(--text); }
.segmented .seg.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* ------------------------------------------------------------------------
   Help tooltip — a tiny ⓘ trigger that opens a floating explainer.
   Works on hover (desktop) and tap (mobile). One popover element is
   toggled by JS; anchoring is done by positioning it near the trigger.
   ------------------------------------------------------------------------ */

.help {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  margin-left: 4px;
  border-radius: 50%;
  color: var(--muted);
  background: var(--panel-2);
  font-size: 10px;
  font-weight: 600;
  cursor: help;
  user-select: none;
  vertical-align: -1px;
  transition: color .12s, background .12s;
  flex-shrink: 0;
}
.help:hover, .help.active {
  color: var(--accent);
  background: var(--accent-bg);
}

#help-pop {
  position: fixed;
  z-index: 40;
  max-width: 280px;
  padding: 12px 14px;
  background: var(--panel);
  color: var(--text);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  font-size: 12px;
  line-height: 1.55;
  display: none;
  pointer-events: none;
}
#help-pop.open { display: block; pointer-events: auto; }
#help-pop .hp-title {
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
  font-size: 13px;
  letter-spacing: -0.005em;
}
#help-pop .hp-body { color: var(--text-2); }
#help-pop .hp-body strong { color: var(--text); font-weight: 600; }
#help-pop::before {
  /* small arrow — filled at runtime based on which side the popover flips to */
  content: '';
  position: absolute;
  width: 8px; height: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-right: 0; border-bottom: 0;
  transform: rotate(45deg);
}
#help-pop.arrow-top::before    { top: -5px;    left: var(--arrow-x, 20px); }
#help-pop.arrow-bottom::before { bottom: -5px; left: var(--arrow-x, 20px); border: 1px solid var(--border); border-top: 0; border-left: 0; }

/* ------------------------------------------------------------------------
   Insurance-eligibility banner — 3-month rolling check against the 2026
   revision (avg daily use < 1h for 3 straight months → management fee
   cannot be billed). One card, tinted by status.
   ------------------------------------------------------------------------ */

.ins-banner {
  display: flex;
  align-items: stretch;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: var(--panel);
  box-shadow: var(--shadow-sm);
}
.ins-banner .ins-icon {
  flex: 0 0 auto;
  width: 32px; height: 32px;
  border-radius: var(--radius-pill);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 16px;
  font-weight: 700;
  color: white;
  align-self: flex-start;
  margin-top: 2px;
}
.ins-banner .ins-body {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.ins-banner .ins-status {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.005em;
}
.ins-banner .ins-headline {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.015em;
  line-height: 1.35;
}
.ins-banner .ins-note {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}
.ins-months {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-3);
}
.ins-month {
  flex: 1;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border: 1px solid transparent;
}
.ins-month .im-label {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}
.ins-month .im-value {
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1.1;
  margin-top: 2px;
}
.ins-month .im-value .im-unit {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  margin-left: 2px;
}
.ins-month .im-partial {
  font-size: 9px;
  color: var(--muted);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  margin-left: 4px;
}
.ins-month.current { position: relative; }
.ins-month.current::after {
  content: '';
  position: absolute;
  top: 6px; right: 6px;
  width: 5px; height: 5px;
  border-radius: 50%;
  background: currentColor;
  opacity: .5;
}
.ins-month.pass .im-value { color: var(--ok); }
.ins-month.fail .im-value { color: var(--bad); }
.ins-month.pass { border-color: color-mix(in oklab, var(--ok) 40%, transparent); }
.ins-month.fail { border-color: color-mix(in oklab, var(--bad) 40%, transparent); }

/* Status tinting for the outer banner */
.ins-banner.safe    { background: color-mix(in oklab, var(--ok)   8%, var(--panel)); border-color: color-mix(in oklab, var(--ok)   25%, transparent); }
.ins-banner.warning { background: color-mix(in oklab, var(--warn) 10%, var(--panel)); border-color: color-mix(in oklab, var(--warn) 30%, transparent); }
.ins-banner.risk    { background: color-mix(in oklab, var(--bad)  10%, var(--panel)); border-color: color-mix(in oklab, var(--bad)  30%, transparent); }
.ins-banner.safe    .ins-icon { background: var(--ok); }
.ins-banner.warning .ins-icon { background: var(--warn); }
.ins-banner.risk    .ins-icon { background: var(--bad); }
.ins-banner.safe    .ins-status { color: var(--ok); }
.ins-banner.warning .ins-status { color: var(--warn); }
.ins-banner.risk    .ins-status { color: var(--bad); }

@media (max-width: 720px) {
  .ins-banner { padding: var(--space-3) var(--space-4); }
  .ins-banner .ins-headline { font-size: 15px; }
  .ins-months { gap: 6px; }
  .ins-month { padding: 6px 8px; }
  .ins-month .im-value { font-size: 15px; }
}

/* ------------------------------------------------------------------------
   KPI cards — big number, quiet label, subtle sub-caption
   ------------------------------------------------------------------------ */

.kpis {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-3);
}
@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
}
.kpi {
  background: var(--panel);
  border-radius: var(--radius);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-height: 128px;
}
.kpi .k-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  letter-spacing: 0.02em;
}
.kpi .k-value {
  font-size: 40px;
  line-height: 1.1;
  font-weight: 600;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.kpi .k-value .k-unit {
  font-size: 20px;
  font-weight: 500;
  color: var(--muted);
  margin-left: 4px;
  letter-spacing: -0.01em;
  /* pin to the baseline of the big number so it doesn't drift down */
  vertical-align: baseline;
  position: relative;
  top: -0.55em;
}
.kpi .k-sub {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin-top: auto;
}
.kpi .k-sub .up   { color: var(--ok); font-weight: 500; }
.kpi .k-sub .down { color: var(--bad); font-weight: 500; }
.kpi .k-sub .flat { color: var(--muted); font-weight: 500; }

/* ------------------------------------------------------------------------
   Calendar
   ------------------------------------------------------------------------ */

.calendar-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}
.cal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-5);
}
.cal-title {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.cal-nav { display: flex; gap: var(--space-1); }
.cal-nav button {
  width: 28px; height: 28px;
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.cal-nav button:hover { background: var(--panel-2); color: var(--text); }
.cal-nav button svg { width: 14px; height: 14px; }

.calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.cal-hd {
  text-align: center;
  padding: 6px 0 10px;
  color: var(--muted-2);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.cal-cell {
  /* Slightly wider than tall so 6 rows + header fit above the fold. */
  aspect-ratio: 1.35 / 1;
  min-height: 84px;
  border-radius: 10px;
  padding: 10px;
  position: relative;
  background: transparent;               /* no-data days: quiet */
  border: 1px solid var(--divider);
  transition: transform .12s ease, box-shadow .15s, border-color .15s;
  overflow: hidden;
}
.cal-cell.blank { background: transparent; border-color: transparent; }
.cal-cell .cal-day {
  font-size: 12px;
  color: var(--muted-2);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

/* usage buckets: accent alpha to convey intensity; borderless when filled */
.cal-cell.has { border-color: transparent; }
.cal-cell.sw-1 { background: color-mix(in oklab, var(--accent) 12%, var(--panel-2)); }
.cal-cell.sw-2 { background: color-mix(in oklab, var(--accent) 28%, var(--panel-2)); }
.cal-cell.sw-3 { background: color-mix(in oklab, var(--accent) 55%, var(--panel-2)); }
.cal-cell.sw-3 .cal-day { color: var(--text); }
.cal-cell.sw-2 .cal-day { color: var(--text-2); }

.cal-cell.has { cursor: pointer; }
.cal-cell.has:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
  border-color: var(--border-strong);
}

.cal-metric {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: var(--text);
}
.cal-metric .cal-unit {
  font-size: 10px;
  font-weight: 500;
  color: var(--muted);
  margin-left: 1px;
}
.cal-cell.sw-3 .cal-metric,
.cal-cell.sw-3 .cal-metric .cal-unit { color: white; }
.cal-cell.sw-3 .cal-day { color: white; }

/* AHI badge in the top-right — colored pill (tint background + solid text) */
.cal-ahi {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 2px 7px;
  border-radius: var(--radius-pill);
  font-size: 10px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
}
.cal-ahi.ahi-ok   { background: color-mix(in oklab, var(--ok)   22%, transparent); color: var(--ok); }
.cal-ahi.ahi-warn { background: color-mix(in oklab, var(--warn) 22%, transparent); color: var(--warn); }
.cal-ahi.ahi-bad  { background: color-mix(in oklab, var(--bad)  22%, transparent); color: var(--bad); }
/* On the darkest usage cells, use solid pills so the badge stays readable. */
.cal-cell.sw-3 .cal-ahi.ahi-ok   { background: var(--ok);   color: white; }
.cal-cell.sw-3 .cal-ahi.ahi-warn { background: var(--warn); color: white; }
.cal-cell.sw-3 .cal-ahi.ahi-bad  { background: var(--bad);  color: white; }

.cal-cell.today {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.cal-legend {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: var(--space-4);
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--divider);
  color: var(--muted);
  font-size: 11px;
}
.cal-legend .scale {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.cal-legend .scale i {
  width: 14px; height: 14px;
  border-radius: 3px;
  display: inline-block;
}
.cal-legend .scale i.s0 { background: var(--panel-2); }
.cal-legend .scale i.s1 { background: color-mix(in oklab, var(--accent) 10%, var(--panel-2)); }
.cal-legend .scale i.s2 { background: color-mix(in oklab, var(--accent) 22%, var(--panel-2)); }
.cal-legend .scale i.s3 { background: color-mix(in oklab, var(--accent) 40%, var(--panel-2)); }
.cal-legend .dot-ok, .cal-legend .dot-warn, .cal-legend .dot-bad {
  width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 4px;
}
.cal-legend .dot-ok { background: var(--ok); }
.cal-legend .dot-warn { background: var(--warn); }
.cal-legend .dot-bad { background: var(--bad); }

/* ------------------------------------------------------------------------
   Chart cards (Trends / Sessions)
   ------------------------------------------------------------------------ */

.chart-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: var(--space-5);
  margin-bottom: var(--space-4);
  box-shadow: var(--shadow-sm);
}
.chart-card h3 {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: var(--space-4);
  display: flex;
  align-items: center;
  gap: 6px;
}
.chart-card h3 .help {
  text-transform: none;    /* keep the ⓘ glyph upright */
  letter-spacing: 0;
  margin-left: 2px;
}
.chart-card .canvas-box { position: relative; height: 240px; }
.chart-card canvas { max-height: 240px; }

/* ------------------------------------------------------------------------
   Sessions view
   ------------------------------------------------------------------------ */

.sessions-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: var(--space-5);
}
@media (max-width: 900px) { .sessions-layout { grid-template-columns: 1fr; } }

.session-list {
  background: var(--panel);
  border-radius: var(--radius);
  padding: var(--space-3);
  max-height: 78vh;
  overflow: auto;
  box-shadow: var(--shadow-sm);
}
.session-list input {
  width: 100%;
  padding: 8px 12px;
  margin-bottom: var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-2);
}
.session-list input:focus { outline: none; border-color: var(--accent); }
.session-list ul { list-style: none; padding: 0; margin: 0; }
.session-list li {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  transition: background .12s;
}
.session-list li:hover { background: var(--panel-2); }
.session-list li.active {
  background: var(--accent-bg);
  color: var(--accent);
}
.session-list li .li-sub {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-top: 2px;
}
.session-list li.active .li-sub { color: color-mix(in oklab, var(--accent) 70%, var(--muted)); }

.session-detail {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.session-header {
  padding: var(--space-3) var(--space-2);
}
.session-header.muted { color: var(--muted); }
.session-header h2 {
  font-size: 22px;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}
.session-header .h-sub { color: var(--muted); font-size: 12px; }

.session-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: var(--space-2);
}
.stat {
  background: var(--panel);
  border-radius: var(--radius-sm);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-sm);
}
.stat .s-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.stat .s-val {
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
  margin-top: 2px;
}

#event-list table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
#event-list th, #event-list td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--divider);
  text-align: left;
  font-variant-numeric: tabular-nums;
}
#event-list th {
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.06em;
}

/* ------------------------------------------------------------------------
   Mobile — tab bar drops to the bottom (iOS pattern), everything else
   stacks and grows to full width. Breakpoint at 720px covers portrait
   phones and small landscape tablets.
   ------------------------------------------------------------------------ */

@media (max-width: 720px) {
  .header-row {
    padding: 10px var(--space-4);
    gap: var(--space-3);
  }
  .brand h1 { font-size: 16px; }

  /* Tabs move to a bottom bar */
  .app-header .tabs,
  .tabs {
    position: fixed;
    /* Explicitly override any inherited top so the bar sits at the bottom. */
    top: auto;
    left: 0; right: 0;
    bottom: 0;
    z-index: 30;
    margin: 0;
    border-radius: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0));
    background: color-mix(in oklab, var(--bg) 88%, transparent);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    border-top: 1px solid var(--divider);
    gap: 4px;
    justify-content: space-around;
  }
  .tab {
    flex: 1;
    padding: 10px 4px;
    font-size: 12px;
    font-weight: 500;
    border-radius: var(--radius-sm);
    text-align: center;
  }
  .tab.active { background: var(--accent-bg); color: var(--accent); box-shadow: none; }

  /* Push page content up so the bottom bar doesn't cover it */
  main {
    padding: var(--space-4) var(--space-4) calc(var(--space-6) + env(safe-area-inset-bottom, 0) + 60px);
  }
  .section { margin-bottom: var(--space-5); }

  /* KPIs: 2 columns, tighter values */
  .kpis {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-2);
  }
  .kpi { padding: var(--space-4); min-height: 108px; }
  .kpi .k-value { font-size: 30px; }
  .kpi .k-value .k-unit { font-size: 15px; top: -0.55em; }
  .kpi .k-sub { font-size: 11px; }

  /* Calendar: tighter cells, hide the AHI number badge (color-only via a
     bottom bar), pull secondary chrome inward */
  .calendar-card { padding: var(--space-4); }
  .cal-head { margin-bottom: var(--space-4); }
  .cal-title { font-size: 15px; }
  .calendar { gap: 4px; }
  .cal-hd { font-size: 10px; padding: 4px 0 6px; }

  .cal-cell {
    aspect-ratio: 1 / 1;
    min-height: 0;
    padding: 3px 5px 8px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: stretch;
  }
  .cal-cell .cal-day {
    font-size: 10px;
    font-weight: 500;
    line-height: 1;
    color: var(--muted);
    align-self: flex-end;              /* date sits in the top-right */
  }
  .cal-cell.sw-2 .cal-day { color: var(--text-2); }
  .cal-cell.sw-3 .cal-day { color: white; }
  .cal-metric {
    position: static;                  /* flex layout owns positioning now */
    text-align: center;
    font-size: 13px;
    font-weight: 600;
    line-height: 1;
    padding-bottom: 2px;
  }
  .cal-metric .cal-unit { display: none; }

  /* AHI: convert the top-right pill into a 3px status stripe along the
     bottom of the cell — visible at a glance, no text to fight with. */
  .cal-ahi {
    top: auto; bottom: 0; left: 0; right: 0;
    height: 3px;
    padding: 0;
    border-radius: 0;
    font-size: 0;                       /* hide the number, keep the color */
    color: transparent !important;
  }
  .cal-ahi.ahi-ok   { background: var(--ok); }
  .cal-ahi.ahi-warn { background: var(--warn); }
  .cal-ahi.ahi-bad  { background: var(--bad); }
  .cal-cell.sw-3 .cal-ahi { opacity: .95; }

  .cal-legend {
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: var(--space-3);
    font-size: 10px;
    margin-top: var(--space-4);
    padding-top: var(--space-3);
  }

  /* Trends: cards inherit; chart height a bit shorter */
  .chart-card { padding: var(--space-4); margin-bottom: var(--space-3); }
  .chart-card .canvas-box { height: 200px; }
  .chart-card canvas { max-height: 200px; }

  /* Sessions: stack, cap list height so the detail is reachable */
  .sessions-layout { grid-template-columns: 1fr; gap: var(--space-3); }
  .session-list { max-height: 40vh; }
  .session-header h2 { font-size: 18px; }
  .stat { padding: var(--space-2) var(--space-3); }
  .stat .s-val { font-size: 16px; }

  /* Settings popover uses more room on phones */
  .settings-pop {
    top: auto;
    left: var(--space-4);
    right: var(--space-4);
    bottom: calc(72px + env(safe-area-inset-bottom, 0));
    min-width: 0;
  }
}
