/* LoopSight web client.
 *
 * Color sources, in priority order (see docs/colors.md):
 *   1. Clinical: the AGP report's time-in-range colors, for anything that
 *      summarizes glucose by range.
 *   2. Loop: glucose blue, insulin orange, carbs green, for the live chart.
 *   3. Trio: UAM magenta for Trio's extra forecast line. ZT is neutral ink.
 */

:root {
  color-scheme: light;
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f1;
  --fg: #131918;
  --muted: #56615f;
  --faint: #8c9795;
  --line: #dde3e1;
  --grid: rgba(19, 25, 24, 0.07);
  --accent: #0f6e66;
  --accent-fg: #ffffff;

  /* Clinical (AGP report v5) */
  --agp-vhigh: #d2823d;
  --agp-high: #fff051;
  --agp-target: #5ba151;
  --agp-low: #ac3432;
  --agp-vlow: #832828;
  --band: rgba(91, 161, 81, 0.12);
  /* AGP report percentile bands and median */
  --agp-outer: #dedee4;
  --agp-inner: #aaaac2;
  --agp-median: #2a3773;

  /* Loop (LoopKit DerivedAssetsBase); carbs darkened from #63da38 to read on white */
  --glucose: #00b0ff;
  --insulin: #ff9500;
  --carbs: #3d9a22;
  /* Trio */
  --uam: #d12bf7;
  --zt: var(--muted);

  /* Status, for text with an icon or label beside it */
  --ok: #2f8a3e;
  --warn: #b07800;
  --bad: #c0362f;
  --stale: var(--faint);

  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --agp-outer: rgba(170, 170, 194, 0.18);
    --agp-inner: rgba(170, 170, 194, 0.42);
    --agp-median: #c3c8ff;
    color-scheme: dark;
    --bg: #0e1312;
    --surface: #151c1b;
    --surface-2: #1c2524;
    --fg: #e8efed;
    --muted: #9eaba8;
    --faint: #6e7b78;
    --line: #283230;
    --grid: rgba(232, 239, 237, 0.07);
    --accent: #5cc7b8;
    --accent-fg: #06201d;
    --band: rgba(91, 161, 81, 0.16);
    --glucose: #63baff;
    --insulin: #ff9f0a;
    --carbs: #59e433;
    --uam: #d12bf7;
    --ok: #4cc77a;
    --warn: #e8b53a;
    --bad: #ef6363;
  }
}
:root[data-theme="dark"] {
    --agp-outer: rgba(170, 170, 194, 0.18);
    --agp-inner: rgba(170, 170, 194, 0.42);
    --agp-median: #c3c8ff;
  color-scheme: dark;
  --bg: #0e1312;
  --surface: #151c1b;
  --surface-2: #1c2524;
  --fg: #e8efed;
  --muted: #9eaba8;
  --faint: #6e7b78;
  --line: #283230;
  --grid: rgba(232, 239, 237, 0.07);
  --accent: #5cc7b8;
  --accent-fg: #06201d;
  --band: rgba(91, 161, 81, 0.16);
  --glucose: #63baff;
  --insulin: #ff9f0a;
  --carbs: #59e433;
  --uam: #d12bf7;
  --ok: #4cc77a;
  --warn: #e8b53a;
  --bad: #ef6363;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.45 var(--font-ui);
  padding: env(safe-area-inset-top) 16px calc(24px + env(safe-area-inset-bottom));
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.shell { max-width: 1320px; margin: 0 auto; }
.num { font-variant-numeric: tabular-nums; }

/* ---- top bar ---- */
.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; padding-block: 12px; }
.sitename { font-weight: 800; letter-spacing: -0.02em; font-size: 26px; line-height: 1.1; margin: 0 auto 0 0; overflow-wrap: anywhere; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 11px; border-radius: 7px; font-size: 13.5px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--fg); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08); }
.iconbtn { border: 1px solid var(--line); background: var(--surface); border-radius: 9px; padding: 5px 10px; font-size: 13.5px; font-weight: 600; cursor: pointer; }

/* ---- live layout ---- */
.live { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
@media (min-width: 768px) { .live { grid-template-columns: minmax(0, 1fr) 290px; } }
@media (min-width: 1200px) { .live { grid-template-columns: minmax(0, 1fr) 330px; } }

.bghead { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; }
/* The hero number uses proportional digits on purpose. */
.bgval { font-size: clamp(64px, 17vw, 88px); font-weight: 800; line-height: 0.9; letter-spacing: -0.04em; }
.bgval.stale { color: var(--stale); }
.bgside { display: flex; flex-direction: column; gap: 2px; padding-bottom: 6px; }
.arrow { font-size: 34px; line-height: 1; font-weight: 700; }
.delta { font-size: 15px; color: var(--muted); }
.unitbtn { border: 0; background: none; padding: 0; color: var(--muted); font-size: 15px; cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }
.unitbtn:hover { color: var(--fg); }
.rangetag { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); }
.rangetag i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.headright { margin-left: auto; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; padding-bottom: 6px; }
.ago { font-size: 13px; color: var(--muted); }
.pill { display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; padding: 4px 10px; font-size: 13px; font-weight: 600; background: var(--surface); border: 1px solid var(--line); cursor: pointer; }
.pill i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; margin-top: 12px; }
.chip { flex: 0 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; min-width: 74px; }
.chip .k { font-size: 11px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.chip .v { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; }
.chip.missing { opacity: 0.45; }

.chartcard { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 8px 0 4px; position: relative; margin-top: 12px; }
.charttools { display: flex; align-items: center; gap: 8px; padding: 0 10px 6px; flex-wrap: wrap; }
.charttools .seg button { padding: 4px 9px; font-size: 12.5px; }
.livebtn { border: 0; background: var(--accent); color: var(--accent-fg); font-weight: 700; font-size: 13px; border-radius: 8px; padding: 5px 11px; cursor: pointer; margin-left: auto; }
.range-label { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; margin-left: auto; }
.livebtn:not([hidden]) + .range-label { margin-left: 0; }
.loading { font-size: 12px; color: var(--faint); }
canvas.chart { display: block; width: 100%; height: 440px; touch-action: none; cursor: grab; }
canvas.chart.dragging { cursor: grabbing; }
/* 24-hour overview strip with a brush, on wide screens only. */
canvas.overview { display: none; width: calc(100% - 20px); height: 56px; margin: 4px 10px 6px; touch-action: none; cursor: ew-resize; border-top: 1px solid var(--line); }
@media (min-width: 1200px) { canvas.overview { display: block; } }
@media (min-width: 768px) { canvas.chart { height: 520px; } }
.readout { position: absolute; top: 44px; left: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; font-size: 12.5px; pointer-events: none; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08); min-width: 160px; font-variant-numeric: tabular-nums; }
.readout b { font-size: 13px; }
.readout .r { display: flex; justify-content: space-between; gap: 14px; }
.readout .r span:first-child { color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 4px 12px 6px; font-size: 12px; color: var(--muted); }
.legend label, .legend span { display: inline-flex; gap: 6px; align-items: center; }
.legend label { cursor: pointer; }
.hint { font-size: 12px; color: var(--faint); padding: 0 12px 6px; }

.side { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.panel h3 { margin: 0 0 8px; font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.kv { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; font-size: 14px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.kv:last-child { border-bottom: 0; }
.kv span:first-child { color: var(--muted); }
.reason { font-size: 12.5px; color: var(--muted); margin-top: 6px; overflow-wrap: anywhere; }
/* Time-in-range bar: 2px surface gaps between segments. */
.tirbar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; gap: 2px; margin: 6px 0; }
.tirbar div { height: 100%; }
.tirleg { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tx { display: grid; grid-template-columns: 66px 1fr auto; gap: 8px; font-size: 13.5px; padding: 5px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.tx:last-child { border-bottom: 0; }
.tx .t { color: var(--muted); }
.mk { display: inline-block; width: 9px; height: 9px; margin-right: 6px; vertical-align: 1px; }
.mk.ins { background: var(--insulin); clip-path: polygon(0 0, 100% 0, 50% 100%); }
.mk.carb { background: var(--carbs); border-radius: 50%; }
.mk.glu { background: var(--glucose); border-radius: 50%; }
.mk.other { background: var(--muted); border-radius: 2px; }
.sw { display: inline-block; width: 16px; height: 0; border-top: 2px dashed currentColor; }

/* ---- sheets, login ---- */
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--surface); width: 100%; max-width: 440px; border-radius: 18px 18px 0 0; padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 12px; }
@media (min-width: 768px) { .scrim { align-items: center; } .sheet { border-radius: 18px; } }
.sheet h2 { margin: 0; font-size: 19px; }
.note { font-size: 13px; color: var(--muted); }
.btns { display: flex; gap: 8px; }
.btn { flex: 1; padding: 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); font-weight: 700; cursor: pointer; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.field input { font: inherit; font-size: 16px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); color: var(--fg); }
.error { color: var(--bad); font-size: 13.5px; }
.login { max-width: 380px; margin: 12vh auto 0; display: flex; flex-direction: column; gap: 14px; }
.login h1 { margin: 0; font-size: 28px; font-weight: 800; letter-spacing: -0.02em; }
.or { text-align: center; font-size: 12px; color: var(--faint); }
.foot { margin-top: 28px; font-size: 12px; color: var(--faint); text-align: center; }
.foot button { border: 0; background: none; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 12px; }

/* ---- alarms ---- */
.alarms { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.alarms:empty { display: none; }
.alarm { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 10px 12px; border-radius: 12px; background: var(--surface); border: 2px solid var(--sev); }
.alarm-icon { width: 26px; height: 26px; border-radius: 50%; background: var(--sev); color: var(--surface); display: grid; place-items: center; font-weight: 800; flex: 0 0 auto; }
.alarm-text { font-size: 16px; margin-right: auto; }
.alarm-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.alarm-snoozed { font-size: 13px; color: var(--muted); }
.enable-sound { align-self: flex-start; border: 1px dashed var(--line); background: var(--surface); border-radius: 9px; padding: 6px 12px; font-weight: 600; cursor: pointer; }
.alarm-setting { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 6px 12px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.toggle { display: inline-flex; gap: 8px; align-items: center; font-weight: 600; }
.field.inline { flex-direction: row; align-items: center; gap: 6px; }
.field.inline input { width: 80px; font-size: 15px; padding: 6px 8px; }

/* ---- bedside ---- */
.bedside { position: fixed; inset: 0; z-index: 30; background: #000; color: #9aa6a3; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; cursor: pointer; padding: 24px; text-align: center; }
.bedside-bg { font-size: min(38vw, 50vh); font-weight: 800; line-height: 0.9; letter-spacing: -0.04em; color: #c9d3d0; }
.bedside-bg.stale { color: #4d5755; }
.bedside-arrow { font-size: 0.4em; margin-left: 0.15em; vertical-align: 0.6em; }
.bedside-sub { font-size: clamp(16px, 4vw, 28px); display: flex; align-items: center; gap: 8px; }
.bedside .dot { width: 14px; height: 14px; border-radius: 50%; display: inline-block; }
.bedside-hint { position: absolute; bottom: 20px; font-size: 13px; color: #4d5755; }

/* ---- review ---- */
.rhead { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding-block: 6px 12px; }
.rtitle { margin: 0; font-size: 22px; font-weight: 800; letter-spacing: -0.02em; }
.rhead .sub { color: var(--muted); font-size: 13.5px; font-variant-numeric: tabular-nums; margin-right: auto; }
.review { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
@media (min-width: 900px) { .review { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1.1fr); } .span-all { grid-column: 1 / -1; } }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; min-width: 0; }
.card h2 { margin: 0 0 10px; font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.stat { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.stat:last-child { border-bottom: 0; }
.stat .l { font-size: 14px; }
.stat .l small { display: block; color: var(--muted); font-size: 12px; }
.stat .v { font-size: 20px; font-weight: 700; white-space: nowrap; }
.stat .v small { font-size: 13px; color: var(--muted); font-weight: 500; }
.stat .v small.miss { color: var(--bad); }
small.prev { display: block; font-size: 11.5px; color: var(--muted); font-weight: 500; text-align: right; }
.agplegend .ln.dashed { background: none; border-top: 2px dashed var(--fg); height: 0; opacity: 0.7; }
.tir { display: grid; grid-template-columns: 44px 1fr; gap: 12px; align-items: stretch; }
.tirstack { display: flex; flex-direction: column; gap: 2px; border-radius: 4px; overflow: hidden; min-height: 240px; }
.tirstack div { min-height: 3px; }
.tirrows { display: flex; flex-direction: column; justify-content: space-between; font-variant-numeric: tabular-nums; }
.tirrow { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; padding: 3px 0; }
.tirrow .n { font-size: 14px; }
.tirrow .n small { display: block; font-size: 11.5px; color: var(--muted); }
.tirrow .p { font-size: 20px; font-weight: 700; text-align: right; }
.tirrow .p small { font-size: 11.5px; font-weight: 500; display: block; }
.ok-t { color: var(--ok); }
.miss-t { color: var(--bad); }
.agpwrap { position: relative; }
.agpwrap svg { display: block; touch-action: pan-y; }
.agpwrap text { font-family: var(--font-ui); }
.agptip { position: absolute; top: 8px; pointer-events: none; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px; font-size: 12px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); font-variant-numeric: tabular-nums; white-space: nowrap; }
.agplegend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.agplegend span { display: inline-flex; align-items: center; gap: 6px; }
.agplegend .sq { width: 16px; height: 10px; display: inline-block; border-radius: 2px; }
.agplegend .ln { width: 16px; height: 3px; display: inline-block; border-radius: 2px; }
.days { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
@media (min-width: 560px) { .days { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (min-width: 1000px) { .days { grid-template-columns: repeat(7, minmax(0, 1fr)); } }
.day { border: 1px solid var(--line); border-radius: 8px; padding: 4px 4px 2px; min-width: 0; }
.day .dl { font-size: 11.5px; color: var(--muted); display: flex; justify-content: space-between; padding: 0 2px; font-variant-numeric: tabular-nums; }
.day svg { display: block; width: 100%; height: 64px; }
.linkbtn { border: 0; background: none; padding: 6px 0 0; color: var(--accent); cursor: pointer; text-decoration: underline; font-size: 13px; }
.tblwrap { overflow-x: auto; }
table.ev { border-collapse: collapse; width: 100%; font-size: 13.5px; font-variant-numeric: tabular-nums; }
table.ev th, table.ev td { padding: 6px 8px; text-align: right; border-bottom: 1px solid var(--line); }
table.ev th:first-child, table.ev td:first-child { text-align: left; }
table.ev th { font-size: 11.5px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); font-weight: 600; }

/* Print: the AGP on one landscape page, daily profiles on the next. */
@media print {
  @page { size: landscape; margin: 10mm; }
  :root { color-scheme: light; --bg: #fff; --surface: #fff; --fg: #000; --muted: #444; --line: #ccc; --grid: #e5e5e5;
    --agp-outer: #dedee4; --agp-inner: #aaaac2; --agp-median: #2a3773; --band: rgba(91, 161, 81, 0.12); }
  body { padding: 0; font-size: 11px; }
  .noprint, .foot, .alarms, .scrim { display: none !important; }
  .review { grid-template-columns: 1fr 1fr 1.1fr; gap: 8px; }
  .span-all { grid-column: 1 / -1; }
  .card { break-inside: avoid; padding: 8px 10px; border-radius: 6px; }
  .tirstack { min-height: 150px; }
  .pagebreak { break-before: page; }
  .days { grid-template-columns: repeat(7, minmax(0, 1fr)); }
}

/* ---- setup ---- */
.login.wide { max-width: 460px; margin-top: 6vh; }
.field small { font-size: 12px; color: var(--faint); }
.row2 { display: flex; align-items: center; gap: 8px; }
.row2 input { flex: 1; min-width: 0; }
.secretbox { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.secretbox .k { display: block; font-size: 11px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.secretbox code { font-family: var(--font-mono); font-size: 14px; overflow-wrap: anywhere; }
.secretbox code.big { font-size: 20px; letter-spacing: 0.04em; }
