/* LinX Gateway Dashboard — mobile-first stylesheet (PHASE 7B rewrite).

PHASE 6 found the original token set Apple-HIG-aligned and kept it.
PHASE 7B extends it (more sizes, more semantic colors, semantic chart
bands) and replaces the layout system so the dashboard is usable on
320 px. The system is structured so a future
@media (prefers-color-scheme: dark) override only needs to swap the
semantic palette — no class rename, no DOM change.

Tokens:
  Spacing    — 4-pt baseline, --space-1..--space-6
  Type       — fluid hero (clamp), body, captions, tabular numerics
  Radius     --r-1..--r-4
  Color      — semantic only: --ok, --warn, --crit, --stale, --neutral, --interactive
              plus 6 chart band tints (very-low, low, in-range, high, very-high, stale)
  Shadow     --shadow-1, --shadow-2
  Layout     — fixed topbar (≥768) + bottom-tab-bar (<768); hamburger inside mobile drawer
*/

:root {
    --bg:        #f6f7fb;
    --bg-elev:  #ffffff;
    --text:      #1c1c1e;
    --muted:     #6e6e73;
    --accent:    #0a84ff;
    --accent-fg: #ffffff;

    --ok:        #34c759;
    --warn:      #ff9500;
    --crit:      #ff3b30;
    --stale:     #8e8e93;
    --neutral:   #aeaeb2;
    --interactive: #0a84ff;

    --band-vlow:  rgba(255,  59,  48, 0.18);
    --band-low:   rgba(255, 149,   0, 0.14);
    --band-range: rgba( 52, 199,  89, 0.14);
    --band-high:  rgba(255, 149,   0, 0.14);
    --band-vhigh: rgba(255,  59,  48, 0.18);
    --band-stale: rgba(142, 142, 147, 0.18);

    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 1rem;
    --space-4: 1.5rem;
    --space-5: 2rem;
    --space-6: 3rem;

    --r-1: 6px;
    --r-2: 10px;
    --r-3: 14px;
    --r-4: 22px;

    --shadow-1: 0 1px 2px rgba(0,0,0,0.06);
    --shadow-2: 0 8px 24px rgba(0,0,0,0.12);

    --font-body: -apple-system, BlinkMacSystemFont, "SF Pro Text",
                  "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, "Roboto Mono", monospace;

    --fs-hero:    clamp(3rem, 12vw, 6rem);
    --fs-display: clamp(1.5rem, 5vw, 2.5rem);
    --fs-h1:      1.5rem;
    --fs-h2:      1.25rem;
    --fs-h3:      1.0625rem;
    --fs-body:    1rem;
    --fs-caption: 0.875rem;
    --fs-micro:   0.75rem;

    --tap-min: 44px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg:        #000000;
        --bg-elev:  #1c1c1e;
        --text:      #ffffff;
        --muted:     #98989d;
        --accent:    #409cff;
        --ok:        #30d158;
        --warn:      #ff9f0a;
        --crit:      #ff453a;
        --stale:     #98989d;
        --shadow-1: 0 1px 2px rgba(0,0,0,0.4);
        --shadow-2: 0 8px 24px rgba(0,0,0,0.6);
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    font-size: var(--fs-body);
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    /* Reserve space for the mobile bottom tab bar */
    padding-bottom: env(safe-area-inset-bottom);
}

/* === Layout === */
.container {
    max-width: 1100px;
    margin: 0 auto;
    padding: var(--space-3) var(--space-3) calc(var(--space-6) + env(safe-area-inset-bottom));
}

/* === Card === */
.card {
    background: var(--bg-elev);
    border-radius: var(--r-3);
    padding: var(--space-3);
    margin: var(--space-3) 0;
    box-shadow: var(--shadow-1);
    overflow-wrap: break-word;
    border: 1px solid transparent;
}
.card.is-stale { opacity: 0.78; }
.card.is-stale .big-number { color: var(--stale); }
.card.severity-ok      { border-color: rgba(52,199,89,0.35); }
.card.severity-warn    { border-color: rgba(255,149,0,0.45); }
.card.severity-crit    { border-color: rgba(255,59,48,0.55); box-shadow: var(--shadow-2); }
.card.hero { text-align: center; padding: var(--space-5) var(--space-3); }
.card.hero.is-stale { background: rgba(142,142,147,0.06); }

/* === Numeric glucose display === */
.glucose {
    font-family: var(--font-body);
    font-feature-settings: "tnum" 1, "lnum" 1;
    font-size: var(--fs-hero);
    font-weight: 700;
    line-height: 1.0;
    margin: 0;
    letter-spacing: -0.02em;
}
.glucose .unit {
    font-size: 0.28em;
    color: var(--muted);
    font-weight: 500;
    margin-left: 0.35em;
    vertical-align: middle;
    letter-spacing: 0;
}

/* === Trend arrow + rate (mandatory per PHASE 7B §9) === */
.trend {
    display: inline-flex;
    align-items: baseline;
    gap: 0.35em;
    margin-top: var(--space-2);
    font-size: var(--fs-h3);
    font-weight: 600;
}
.trend .arrow {
    display: inline-block;
    font-size: 1.5em;
    line-height: 1;
    vertical-align: -0.05em;
}
.trend .rate {
    color: var(--muted);
    font-size: 0.85em;
    font-weight: 500;
}
.trend.up-fast      .arrow { color: var(--crit); }
.trend.up          .arrow { color: var(--warn); }
.trend.flat        .arrow { color: var(--ok); }
.trend.down        .arrow { color: var(--warn); }
.trend.down-fast   .arrow { color: var(--crit); }
.trend.unknown    .arrow { color: var(--muted); }

/* === Status / badge === */
.badge {
    display: inline-block;
    padding: 0.15em 0.7em;
    border-radius: 999px;
    font-size: var(--fs-caption);
    font-weight: 600;
    white-space: nowrap;
    letter-spacing: 0.02em;
}
.badge.ok { background: rgba(52,199,89,0.18); color: var(--ok); }
.badge.warn { background: rgba(255,149,0,0.18); color: var(--warn); }
.badge.crit { background: rgba(255,59,48,0.18); color: var(--crit); }
.badge.stale { background: rgba(142,142,147,0.18); color: var(--stale); }
.badge.test { background: rgba(175,82,222,0.18); color: #af52de; }
.badge.neutral { background: rgba(174,174,178,0.18); color: var(--neutral); }

/* === Freshness strip (live age) === */
.freshness {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    color: var(--muted);
    font-size: var(--fs-caption);
    margin-top: var(--space-2);
}
.freshness .dot {
    width: 0.5em; height: 0.5em; border-radius: 50%;
    background: var(--ok); display: inline-block;
}
.freshness.is-stale .dot { background: var(--stale); }
.freshness.is-old    .dot { background: var(--warn); }
.freshness.is-very-old .dot { background: var(--crit); }

/* === Status row (hero card meta) === */
.status-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    justify-content: center;
    align-items: center;
    margin-top: var(--space-3);
}

/* === Chart card === */
.chart-card { padding: var(--space-3); }
.chart-card h3 { margin: 0 0 var(--space-2); font-size: var(--fs-h2); }
.chart-card .legend {
    display: flex; flex-wrap: wrap; gap: var(--space-3);
    margin-top: var(--space-2); font-size: var(--fs-micro); color: var(--muted);
}
.chart-card .legend span::before {
    content: ""; display: inline-block;
    width: 0.7em; height: 0.7em; border-radius: 2px;
    margin-right: 0.35em; vertical-align: -0.05em;
}
.chart-card .legend .l-ok    ::before { background: var(--ok); }
.chart-card .legend .l-warn  ::before { background: var(--warn); }
.chart-card .legend .l-crit  ::before { background: var(--crit); }
.chart-card .legend .l-stale ::before { background: var(--stale); }
.chart-card canvas { width: 100% !important; height: 180px !important; }

/* === TIR strip === */
.tir {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-2);
}
.tir-bar {
    display: flex;
    height: 14px;
    border-radius: 7px;
    overflow: hidden;
    background: rgba(142,142,147,0.18);
}
.tir-bar > span { display: block; height: 100%; }
.tir-bar .vlow  { background: var(--crit); }
.tir-bar .low   { background: var(--warn); }
.tir-bar .range { background: var(--ok); }
.tir-bar .high  { background: var(--warn); }
.tir-bar .vhigh { background: var(--crit); }
.tir-bar .stale { background: var(--stale); }
.tir-legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    font-size: var(--fs-micro);
    color: var(--muted);
}

/* === Topbar (desktop ≥768) === */
.topbar {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    background: var(--bg-elev);
    border-bottom: 1px solid rgba(142,142,147,0.18);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar .brand {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    font-weight: 700;
    font-size: var(--fs-h3);
    text-decoration: none;
    color: var(--text);
    white-space: nowrap;
}
.topbar .brand .icon {
    display: inline-block;
    width: 1.4em; height: 1.4em; border-radius: 50%;
    background: var(--crit);
    color: var(--accent-fg);
    text-align: center;
    line-height: 1.4em;
    font-size: 0.9em;
}
.topbar nav {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    margin-left: auto;
}
.topbar nav a {
    color: var(--text);
    text-decoration: none;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--r-1);
    min-height: var(--tap-min);
    display: inline-flex;
    align-items: center;
    font-weight: 500;
}
.topbar nav a:hover { background: rgba(142,142,147,0.10); }
.topbar nav a.is-active { color: var(--accent); background: rgba(10,132,255,0.10); }
.topbar nav .btn-link {
    background: none; border: none; color: var(--muted);
    cursor: pointer; font: inherit;
    padding: var(--space-2) var(--space-3);
    min-height: var(--tap-min);
}

/* === Mobile drawer & bottom tab bar (≤767) === */
.menu-toggle {
    display: none;
    background: none; border: none;
    color: var(--text);
    width: var(--tap-min); height: var(--tap-min);
    border-radius: var(--r-1);
    font-size: 1.25rem;
    cursor: pointer;
    align-items: center; justify-content: center;
}
.menu-toggle:hover { background: rgba(142,142,147,0.10); }

.drawer {
    position: fixed; inset: 0 0 0 0;
    background: rgba(0,0,0,0.4);
    z-index: 50;
    display: none;
    align-items: stretch;
}
.drawer.is-open { display: flex; }
.drawer-panel {
    width: min(86vw, 320px);
    background: var(--bg-elev);
    margin-left: auto;
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}
.drawer-panel a {
    color: var(--text);
    text-decoration: none;
    padding: var(--space-3);
    border-radius: var(--r-2);
    min-height: var(--tap-min);
    display: flex;
    align-items: center;
    font-size: var(--fs-h3);
}
.drawer-panel a.is-active { background: rgba(10,132,255,0.10); color: var(--accent); }
.drawer-panel .btn-link {
    margin-top: auto;
    background: none; border: none;
    color: var(--muted); cursor: pointer;
    padding: var(--space-3);
    text-align: left; font: inherit;
}

.bottom-bar {
    display: none;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: var(--bg-elev);
    border-top: 1px solid rgba(142,142,147,0.18);
    z-index: 9;
    padding-bottom: env(safe-area-inset-bottom);
}
.bottom-bar ul {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    margin: 0; padding: 0; list-style: none;
}
.bottom-bar a {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.2em;
    min-height: var(--tap-min);
    padding: 0.4em 0;
    color: var(--muted);
    text-decoration: none;
    font-size: var(--fs-micro);
}
.bottom-bar a.is-active { color: var(--accent); }
.bottom-bar .glyph { font-size: 1.25em; line-height: 1; }
@media (max-width: 767px) {
    .topbar nav, .topbar .btn-link { display: none; }
    .topbar .brand { margin-right: auto; }
    .menu-toggle { display: inline-flex; }
    .bottom-bar { display: block; }
    .container { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
}
@media (min-width: 768px) {
    .menu-toggle { display: none; }
    .drawer { display: none !important; }
}

/* === Alert cards (replaces /alerts table-only render) === */
.alert-card {
    border-left: 6px solid var(--neutral);
    background: var(--bg-elev);
    border-radius: var(--r-3);
    padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
    margin: var(--space-3) 0;
    box-shadow: var(--shadow-1);
}
.alert-card.severity-ok      { border-left-color: var(--ok); }
.alert-card.severity-warn    { border-left-color: var(--warn); }
.alert-card.severity-crit    { border-left-color: var(--crit); box-shadow: var(--shadow-2); }
.alert-card.severity-cleared { opacity: 0.7; }
.alert-card .alert-head {
    display: flex; flex-wrap: wrap; align-items: center;
    gap: var(--space-2);
    margin-bottom: var(--space-2);
}
.alert-card .alert-head .value {
    font-family: var(--font-body);
    font-feature-settings: "tnum" 1, "lnum" 1;
    font-size: var(--fs-h1);
    font-weight: 700;
    margin-right: auto;
}
.alert-card dl {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.35em var(--space-3);
    margin: var(--space-2) 0 0;
    font-size: var(--fs-caption);
}
.alert-card dt { color: var(--muted); }
.alert-card dd { margin: 0; color: var(--text); }
.alert-card .delivery {
    margin-top: var(--space-2);
    display: flex; flex-wrap: wrap; gap: var(--space-2);
}
.alert-card .delivery .badge.delivery-sent { background: rgba(52,199,89,0.18); color: var(--ok); }
.alert-card .delivery .badge.delivery-failed { background: rgba(255,59,48,0.18); color: var(--crit); }
.alert-card .delivery .badge.delivery-skipped { background: rgba(142,142,147,0.18); color: var(--stale); }

.alert-empty {
    text-align: center;
    color: var(--muted);
    padding: var(--space-5);
}

/* === History cards (replaces raw 200-row table) === */
.history-list {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-2);
    list-style: none;
    padding: 0;
    margin: 0;
}
.history-card {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: var(--space-3);
    background: var(--bg-elev);
    border-radius: var(--r-3);
    padding: var(--space-3);
    box-shadow: var(--shadow-1);
    border-left: 6px solid var(--neutral);
}
.history-card.severity-ok    { border-left-color: var(--ok); }
.history-card.severity-warn  { border-left-color: var(--warn); }
.history-card.severity-crit  { border-left-color: var(--crit); }
.history-card .value {
    font-family: var(--font-body);
    font-feature-settings: "tnum" 1;
    font-size: var(--fs-h2);
    font-weight: 700;
}
.history-card .value .unit { font-size: 0.5em; color: var(--muted); font-weight: 500; }
.history-card .meta {
    color: var(--muted);
    font-size: var(--fs-micro);
    display: flex; gap: var(--space-2); flex-wrap: wrap;
}
.history-card .trend-mini {
    font-size: 1.25em;
    color: var(--muted);
}

/* === Forms === */
.field {
    display: block;
    margin: var(--space-3) 0;
}
.field > label {
    display: block;
    margin-bottom: var(--space-1);
    color: var(--muted);
    font-size: var(--fs-caption);
}
.field input, .field select {
    width: 100%;
    max-width: 320px;
    padding: var(--space-2) var(--space-3);
    border: 1px solid rgba(142,142,147,0.4);
    border-radius: var(--r-2);
    font: inherit;
    background: var(--bg-elev);
    color: var(--text);
    min-height: var(--tap-min);
    box-sizing: border-box;
}
.field input:focus, .field select:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--tap-min);
    padding: 0.6em 1.2em;
    background: var(--accent);
    color: var(--accent-fg);
    border: none;
    border-radius: var(--r-2);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--crit); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn-row { display: flex; flex-wrap: wrap; gap: var(--space-2); }

/* === Settings layout === */
.settings-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-3);
}
@media (min-width: 768px) {
    .settings-grid { grid-template-columns: 1fr 1fr; }
}
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 0.35em var(--space-3); margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* === Login === */
.login {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: var(--space-3);
}
.login .card { width: 100%; max-width: 360px; }
.login h2 { margin: 0 0 var(--space-3); text-align: center; }
.login .btn { width: 100%; }
.error {
    color: var(--crit);
    font-size: var(--fs-caption);
    margin: var(--space-2) 0;
}

/* === Section headings === */
h1 { font-size: var(--fs-display); margin: 0 0 var(--space-3); }
h2 { font-size: var(--fs-h1); margin: var(--space-5) 0 var(--space-2); }
h3 { font-size: var(--fs-h3); margin: var(--space-3) 0 var(--space-2); }

/* === Helpers === */
.muted { color: var(--muted); }
.center { text-align: center; }
.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.row { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.row.between { justify-content: space-between; }

/* === Charts stay readable at every viewport === */
@media (min-width: 768px) {
    .chart-card canvas { height: 220px !important; }
}
@media (min-width: 1024px) {
    .chart-card canvas { height: 260px !important; }
}

/* === Tables stay inside their container on mobile === */
.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(-1 * var(--space-3));
    padding: 0 var(--space-3);
}
.table-wrap table.readings {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg-elev);
    border-radius: var(--r-3);
    overflow: hidden;
    table-layout: auto;
}
.table-wrap table.readings th,
.table-wrap table.readings td {
    padding: 0.55em 0.7em;
    text-align: left;
    word-break: break-word;
}
.table-wrap table.readings thead { background: rgba(142,142,147,0.10); }
.table-wrap table.readings tr + tr td { border-top: 1px solid rgba(142,142,147,0.18); }
.fp {
    font-family: var(--font-mono);
    color: var(--muted);
    font-size: var(--fs-micro);
    word-break: break-all;
}

/* === Skip link for keyboard nav === */
.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
}
.skip-link:focus {
    left: var(--space-3);
    top: var(--space-3);
    z-index: 100;
    background: var(--accent);
    color: var(--accent-fg);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--r-2);
}

/* PHASE: Caregiver — responsive table wrapper so data-tables
   (Caregivers screen, My Patients screen, history) scroll
   horizontally INSIDE the table container without overflowing
   the viewport on mobile. */
.data-table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-collapse: collapse;
}
.data-table thead,
.data-table tbody,
.data-table tr {
    display: table;
    width: 100%;
    table-layout: auto;
}
