:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #6a7280;
  --line: #dfe3e8;
  --accent: #1e88e5;
  --accent-ink: #ffffff;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --bad: #b42318;
  --bad-bg: #fee4e2;
  --good: #067647;
  --good-bg: #d1fadf;
  /* The chart. `--chart-band` is the app's own target-range green, which is
     the one colour worth reproducing exactly: it is what makes a glance at the
     curve mean anything. */
  --chart-curve: #3b3b3b;
  --chart-band: #f0f7e8;
  --chart-grid: #e6e9ee;
  --chart-agp-inner: #a8c8ec;
  --chart-agp-outer: #dce8f7;
  --chart-agp-median: #1a4f8f;
  --band-very-low: #7f1d1d;
  --band-low: #ef4444;
  --band-in-range: #67a52b;
  --band-high: #fbbf24;
  --band-very-high: #c2410c;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f25;
    --ink: #e8eaed;
    --muted: #9aa3ad;
    --line: #2c313a;
    --accent: #4ea1f0;
    --accent-ink: #0b0d10;
    --warn: #fbbf24;
    --warn-bg: #3a2f10;
    --bad: #fda29b;
    --bad-bg: #43201d;
    --good: #6ce9a6;
    --good-bg: #10301f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);

    /* The curve has to read against a dark panel, and the band has to stay
       recognisably the same green without glowing. */
    --chart-curve: #e8eaed;
    --chart-band: #1e2a17;
    --chart-grid: #2c313a;
    --chart-agp-inner: #2d4f7c;
    --chart-agp-outer: #1c2f47;
    --chart-agp-median: #8ec1f5;
    --band-very-low: #f87171;
    --band-low: #fb923c;
    --band-in-range: #86c94a;
    --band-high: #fbbf24;
    --band-very-high: #ea580c;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

/* ---- top bar ---- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand { font-weight: 650; letter-spacing: -.01em; text-decoration: none; color: var(--ink); }

.topbar nav { display: flex; gap: 4px; margin-right: auto; }

.topbar nav a {
  padding: 5px 11px;
  border-radius: 7px;
  text-decoration: none;
  color: var(--muted);
  font-weight: 500;
}

.topbar nav a:hover { background: var(--bg); color: var(--ink); }
.topbar nav a.here { background: var(--accent); color: var(--accent-ink); }

/* ---- account menu ---- */

.menu { position: relative; }

.menu summary {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 5px 11px;
  border: 1px solid var(--line);
  border-radius: 7px;
  cursor: pointer;
  list-style: none;
  color: var(--muted);
  font-size: 13px;
}

.menu summary::-webkit-details-marker { display: none; }
.menu summary:hover, .menu[open] summary { color: var(--ink); border-color: var(--muted); }

.burger { display: block; width: 15px; height: 11px; position: relative; }

.burger::before,
.burger::after,
.burger { background: none; }

.burger::before {
  content: "";
  position: absolute;
  inset: 0;
  border-top: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
}

.burger::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  margin-top: -1px;
  border-top: 2px solid currentColor;
}

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 210px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px;
  box-shadow: 0 8px 28px rgba(16, 24, 40, .16);
  z-index: 10;
}

.menu-panel a,
.menu-panel button[type=submit] {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: none;
  border-radius: 7px;
  background: none;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  text-decoration: none;
  cursor: pointer;
}

.menu-panel a:hover, .menu-panel button[type=submit]:hover { background: var(--bg); }
.menu-panel form { margin: 0; }
.menu-panel hr { border: none; border-top: 1px solid var(--line); margin: 6px 4px; }

/* Not a control -- just says which build you are looking at. */
.menu-version {
  margin: 6px 0 2px;
  padding: 0 10px;
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  user-select: text;
}

.add-password {
  font-size: 13px;
  font-weight: 550;
  text-decoration: none;
  padding: 5px 11px;
  border-radius: 7px;
  background: var(--good-bg);
  color: var(--good);
}

.add-password:hover { filter: brightness(.96); }

main { max-width: 1240px; margin: 0 auto; padding: 24px; }

/* Narrow screens: the brand, the account menu and four nav links do not fit on
   one row, so the nav drops to its own. */
@media (max-width: 620px) {
  .topbar { flex-wrap: wrap; row-gap: 8px; gap: 10px; padding: 10px 16px; }
  .topbar nav { order: 3; flex-basis: 100%; margin-right: 0; overflow-x: auto; }
  .add-password, .menu { margin-left: auto; }

  .menu summary .who {
    max-width: 44vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  main { padding: 16px; }
}

/* ---- flashes ---- */

/* Shown to a link user once they have been around five days. Deliberately
   loud-ish: it is an offer, not a warning. */
.banner {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}

.banner div { flex: 1; min-width: 260px; color: var(--muted); font-size: 14px; }
.banner strong { display: block; color: var(--ink); font-size: 15px; margin-bottom: 2px; }

.flash { padding: 10px 14px; border-radius: var(--radius); margin: 0 0 16px; }
.flash.notice { background: var(--good-bg); color: var(--good); }
.flash.error { background: var(--bad-bg); color: var(--bad); }

.page-head { margin-bottom: 24px; }
.page-head h1 { margin: 0 0 4px; font-size: 22px; letter-spacing: -.015em; }
.summary { margin: 0; color: var(--muted); font-size: 14px; }
.pending { color: var(--accent); }
.warn { color: var(--warn); }

.empty { padding: 60px 20px; text-align: center; color: var(--muted); }

.button {
  display: inline-block;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 9px 16px;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 550;
}

/* ---- gallery ---- */

.report { margin-bottom: 36px; }

.report h2 {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 650;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--muted);
}

.count {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 1px 8px;
  font-size: 11px;
  letter-spacing: 0;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(146px, 1fr));
  gap: 14px;
}

.shot { margin: 0; }

.shot .open {
  display: block;
  width: 100%;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  overflow: hidden;
  cursor: zoom-in;
  box-shadow: var(--shadow);
  transition: transform .12s ease, border-color .12s ease;
}

.shot .open:hover { transform: translateY(-2px); border-color: var(--accent); }
.shot .open:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.shot img { display: block; width: 100%; height: auto; }

.shot figcaption {
  margin-top: 7px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  line-height: 1.35;
}

/* ---- viewer ---- */

#viewer {
  border: none;
  padding: 0;
  background: transparent;
  width: 100vw;
  max-width: 100vw;
  height: 100vh;
  max-height: 100vh;
  overflow: hidden;
}

/* Scoped to [open] on purpose. A bare `display: flex` here would override the
   browser's own `dialog:not([open]) { display: none }`, leaving an invisible
   full-viewport dialog on top of the page swallowing every click after the
   viewer is closed. */
#viewer[open] {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 14px;
}

#viewer::backdrop { background: rgba(8, 10, 14, .88); }

/* The whole section lies in one track; the track slides, the slides scale. */
.viewer-stage { width: 100%; overflow: hidden; }

.viewer-track {
  display: flex;
  align-items: center;
  will-change: transform;
  transition: transform .32s cubic-bezier(.4, 0, .2, 1);
}

.slide {
  flex: 0 0 var(--slot, min(46vw, 300px));
  display: flex;
  justify-content: center;
  margin: 0;
  padding: 0 10px;
  cursor: pointer;
  transform: scale(.8);
  opacity: .45;
  transition: transform .32s cubic-bezier(.4, 0, .2, 1), opacity .32s ease;
}

.slide.active { transform: scale(1); opacity: 1; cursor: default; }
.slide:not(.active):hover { opacity: .75; }

/* Slides outside the loaded window have no src; without this they would show
   their alt text as a bare box. */
.slide img:not([src]) { visibility: hidden; }

.slide img {
  width: 100%;
  height: auto;
  max-height: 78vh;
  object-fit: contain;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 12px 48px rgba(0, 0, 0, .5);
}

.viewer-caption {
  display: flex;
  gap: 10px;
  align-items: baseline;
  justify-content: center;
  color: #fff;
  font-size: 13px;
  margin: 0;
}

#viewer-report { font-weight: 600; }
#viewer-label { opacity: .75; }

#viewer .close,
#viewer .nav {
  position: fixed;
  border: none;
  background: rgba(255, 255, 255, .12);
  color: #fff;
  cursor: pointer;
  border-radius: 50%;
  line-height: 1;
  z-index: 1;
}

#viewer .close { top: 18px; right: 18px; width: 38px; height: 38px; font-size: 22px; }

#viewer .nav {
  top: 50%;
  transform: translateY(-50%);
  width: 46px;
  height: 46px;
  font-size: 26px;
}

#viewer .prev { left: 16px; }
#viewer .next { right: 16px; }
#viewer .nav:hover, #viewer .close:hover { background: rgba(255, 255, 255, .24); }
#viewer .nav:disabled { opacity: .2; cursor: default; }

/* ---- forms / cards ---- */

body.centred { display: grid; place-items: center; min-height: 100vh; padding: 24px; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
  width: min(100%, 380px);
  box-shadow: var(--shadow);
}

.card h1 { margin: 0 0 2px; font-size: 15px; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }
.card h2 { margin: 0 0 18px; font-size: 20px; letter-spacing: -.015em; }

label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); font-weight: 500; }

input[type=text], input[type=password], input[type=email], input[type=date], select {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}

input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

button[type=submit] {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 550;
  cursor: pointer;
}

.hint { color: var(--muted); font-size: 13px; margin: 0 0 18px; }

/* ---- panels, tables ---- */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}

.panel h2 { margin: 0 0 14px; font-size: 16px; }

/* Wide tables scroll inside their panel rather than pushing the page sideways. */
.table-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }
.table-wrap table { min-width: 560px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); }
tr:last-child td { border-bottom: none; }

code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }

.token-box {
  background: var(--good-bg);
  border: 1px solid var(--good);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 20px;
}

.token-box p { margin: 0 0 8px; color: var(--good); font-size: 13px; }

.token-box input {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
}

/* ---- upload ---- */

/* Sticky, above the dropzone: with 50+ tiles below it, progress is the one
   thing that must not scroll away. */
.progress {
  position: sticky;
  top: 61px;
  z-index: 4;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.progress-bar {
  height: 6px;
  border-radius: 3px;
  background: var(--bg);
  overflow: hidden;
  opacity: 0;
  transition: opacity .2s ease;
}

.progress.busy .progress-bar { opacity: 1; }

.progress-bar span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 3px;
  background: var(--accent);
  transition: width .25s ease;
}

.progress-line { margin: 0; color: var(--muted); font-size: 14px; }
.progress.busy .progress-line { margin-top: 9px; color: var(--ink); font-weight: 550; }

.dropzone {
  border: 2px dashed var(--line);
  border-radius: 14px;
  background: var(--panel);
  min-height: 200px;
  padding: 26px;
  cursor: pointer;
}

.dropzone.dz-drag-hover { border-color: var(--accent); background: var(--bg); }
.dz-message { text-align: center; color: var(--muted); }
.dz-message strong { display: block; color: var(--ink); font-size: 16px; margin-bottom: 4px; }

.dz-preview {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 104px;
  margin: 12px 10px 0 0;
  vertical-align: top;
  font-size: 11px;
  color: var(--muted);
  word-break: break-all;
  text-align: center;
}

.dz-preview img { width: 100%; border-radius: 7px; border: 1px solid var(--line); background: var(--bg); }
.dz-preview .name, .dz-preview .state { display: block; width: 100%; }
.dz-preview .state { font-weight: 600; }
.dz-preview.done .state { color: var(--good); }
.dz-preview.dupe .state { color: var(--muted); }
.dz-preview.bad .state { color: var(--bad); }

/* ---- people ---- */

.modes { border: none; padding: 0; margin: 0 0 14px; }
.modes legend { font-size: 13px; color: var(--muted); font-weight: 500; padding: 0; margin-bottom: 8px; }

.mode {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin-bottom: 8px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 400;
}

.mode:has(input:checked) { border-color: var(--accent); background: var(--bg); }
.mode input { margin-top: 3px; }
.mode strong { display: block; color: var(--ink); font-weight: 600; margin-bottom: 1px; }

.link-days { display: flex; align-items: baseline; gap: 8px; }
.link-days[hidden] { display: none; }
.link-days input { width: 90px; margin-top: 0; }

.people td { vertical-align: top; }
.people tr.disabled td:first-child { opacity: .55; }

.tag {
  display: inline-block;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 0 8px;
  font-size: 11px;
  color: var(--muted);
  vertical-align: 1px;
}

.tag.bad { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }

.row-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.row-actions .inline { margin: 0; display: flex; gap: 4px; align-items: center; }

.row-actions button {
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 7px;
  padding: 4px 10px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.row-actions button:hover { color: var(--ink); border-color: var(--muted); }
.row-actions .days { width: 62px; margin: 0; padding: 4px 7px; font-size: 12px; }

/* ---- review ---- */

.review-item { display: flex; gap: 20px; align-items: flex-start; }
.review-item img { width: 150px; border-radius: 8px; border: 1px solid var(--line); }
.review-fields { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.review-fields label { margin-bottom: 0; }
.review-fields label[hidden] { display: none; }

/* Its own form under the fields, so a discard is a deliberate second gesture
   rather than a button sitting next to "File it". */
.review-discard { margin: 10px 0 0; }

.review-discard button {
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 7px;
  padding: 4px 10px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.review-discard button:hover { color: var(--bad); border-color: var(--bad); }

.ocr { background: var(--bg); border-radius: 7px; padding: 9px 11px; margin: 0 0 14px; }


/* ---- glucose panel ---- */

.glucose { display: flex; flex-direction: column; gap: 18px; }
.glucose.loading { opacity: .55; transition: opacity .15s; }

.filters {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: var(--shadow);
}

.presets { display: flex; gap: 4px; }

.presets button {
  padding: 5px 13px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

.presets button:hover { background: var(--bg); color: var(--ink); }
.presets button.here { background: var(--accent); color: var(--accent-ink); }

.filters .dates { display: flex; gap: 12px; margin-left: auto; align-items: center; }
.filters .dates label { display: flex; gap: 6px; align-items: center; margin: 0; color: var(--muted); font-size: 13px; }

.filters .dates input {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--bg);
  color: var(--ink);
  padding: 4px 8px;
  font: inherit;
  font-size: 13px;
}

/* ---- the numbers ---- */

.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.stat .label {
  display: block;
  font-size: 11px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 4px;
}

.stat .value { display: block; font-size: 21px; font-weight: 600; letter-spacing: -.02em; }
.stat.trend .value { font-size: 14px; font-weight: 500; color: var(--muted); }
.stat.trend .value.up { color: var(--warn); }
.stat.trend .value.down { color: var(--good); }

/* ---- time in range ---- */

.tir {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.tir-bar { display: flex; height: 22px; border-radius: 6px; overflow: hidden; background: var(--bg); }
.tir-bar .seg { display: block; height: 100%; }
.tir-bar .seg.very_low { background: var(--band-very-low); }
.tir-bar .seg.low { background: var(--band-low); }
.tir-bar .seg.in_range { background: var(--band-in-range); }
.tir-bar .seg.high { background: var(--band-high); }
.tir-bar .seg.very_high { background: var(--band-very-high); }

.tir-key { display: flex; gap: 18px; flex-wrap: wrap; list-style: none; margin: 12px 0 0; padding: 0; font-size: 13px; }
.tir-key li { display: flex; gap: 6px; align-items: baseline; color: var(--muted); }
.tir-key li::before { content: ''; width: 9px; height: 9px; border-radius: 2px; }
.tir-key li.very_low::before { background: var(--band-very-low); }
.tir-key li.low::before { background: var(--band-low); }
.tir-key li.in_range::before { background: var(--band-in-range); }
.tir-key li.high::before { background: var(--band-high); }
.tir-key li.very_high::before { background: var(--band-very-high); }
.tir-key b { color: var(--ink); font-weight: 600; }

.coverage { margin: 12px 0 0; color: var(--muted); font-size: 13px; }

/* ---- the chart ---- */

.chart-head { display: flex; align-items: center; gap: 12px; }

.tabs { display: flex; gap: 4px; }

.tabs button {
  padding: 6px 15px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

.tabs button[aria-selected="true"] { background: var(--ink); border-color: var(--ink); color: var(--panel); }

.reset-zoom {
  margin-left: auto;
  padding: 6px 13px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.reset-zoom:hover { color: var(--ink); border-color: var(--muted); }
.reset-zoom[hidden] { display: none; }

.chart-note { margin: 0; color: var(--muted); font-size: 13px; }

.chart {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px 4px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* uPlot styles its own legend and axes through these. */
.chart .u-legend { font-size: 13px; color: var(--muted); }
.chart .u-legend .u-marker { border-radius: 2px; }
.chart .u-select { background: rgba(30, 136, 229, .12); }

@media (max-width: 860px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .filters .dates { margin-left: 0; width: 100%; }
  .stat .value { font-size: 18px; }
}

@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .stat.trend { grid-column: 1 / -1; }
  .presets { width: 100%; }
  .presets button { flex: 1; padding: 5px 6px; }
  .filters .dates { flex-direction: column; align-items: stretch; }
  .filters .dates label { justify-content: space-between; }
}
