:root {
  --bg: #0b1620; --surface: #13212e; --surface-2: #1a2c3b; --line: #263c4e; --text: #e9f1f6; --muted: #8fa5b5;
  --accent: #35c6dc; --accent-ink: #04222c; --ok: #3fcf8e; --low: #f4b740; --high: #ff7a66;
  --hero-a: #0f5c78; --hero-b: #0b2a44; --radius: 18px;
  color-scheme: dark;
}
/* Light theme: when the phone is in light mode (unless Dark is forced in Settings), or when Light is forced. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef4f7; --surface: #ffffff; --surface-2: #f2f6f9; --line: #dae4ea; --text: #10202c; --muted: #5a6f7f;
    --accent: #0892aa; --accent-ink: #ffffff; --ok: #14895a; --low: #a66a00; --high: #cf3f2b;
    --hero-a: #19a6c4; --hero-b: #0d6a94;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #eef4f7; --surface: #ffffff; --surface-2: #f2f6f9; --line: #dae4ea; --text: #10202c; --muted: #5a6f7f;
  --accent: #0892aa; --accent-ink: #ffffff; --ok: #14895a; --low: #a66a00; --high: #cf3f2b;
  --hero-a: #19a6c4; --hero-b: #0d6a94;
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* The two file pickers are hidden elements; no later rule may bring them back on screen. */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }

/* App shell. The page itself never scrolls: only .view does, and the tab bar
   sits below it in normal flow. A position:fixed bar is placed above the
   bottom of the screen by an installed iPhone web app whenever the page is
   shorter than the screen (History with one test), which is what this avoids.
   Same fix as 43 Links Power, where it was proven on the same phone. */
html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
/* Installed on the Home Screen, dvh can be shorter than the screen by a
   safe-area strip, so every clipping ancestor uses vh there. */
html.standalone-pwa, html.standalone-pwa body, html.standalone-pwa #app { height: 100vh; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.65rem; line-height: 1.15; letter-spacing: -0.01em; text-wrap: balance; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.98rem; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
small { font-size: 0.8rem; }
.muted { color: var(--muted); font-size: 0.88rem; }

.view { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: calc(env(safe-area-inset-top) + 14px) max(16px, calc((100% - 720px) / 2)) 24px; display: grid; grid-auto-rows: max-content; align-content: start; gap: 14px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; display: grid; gap: 12px; }
.page { padding: 8px 4px 0; }

/* Hero */
.hero { position: relative; overflow: hidden; border-radius: 24px; padding: 26px 22px 30px; color: #fff; background: linear-gradient(160deg, var(--hero-a), var(--hero-b)); display: grid; gap: 8px; }
.hero.attention { background: linear-gradient(160deg, #a8641a, #5a2a1c); }
.hero p { color: rgba(255, 255, 255, 0.82); font-size: 0.92rem; }
.hero .badge { justify-self: start; background: rgba(255, 255, 255, 0.18); padding: 4px 11px; border-radius: 99px; font-size: 0.8rem; font-weight: 600; }
.hero.empty { padding-bottom: 26px; gap: 14px; }
.wave { position: absolute; inset: auto -20% -46px -20%; height: 90px; border-radius: 45%; background: rgba(255, 255, 255, 0.09); transform: rotate(-3deg); pointer-events: none; }

/* Buttons */
.primary, .ghost, .danger, .button { display: inline-flex; align-items: center; justify-content: center; min-height: 46px; padding: 0 18px; border-radius: 14px; font-weight: 600; text-decoration: none; }
.primary { background: var(--accent); color: var(--accent-ink); }
.hero .primary { background: #fff; color: #0b3550; }
.ghost { border: 1px solid var(--line); background: var(--surface-2); color: var(--text); }
.danger { border: 1px solid var(--high); color: var(--high); width: 100%; }
.big { min-height: 54px; font-size: 1.05rem; width: 100%; }
.small { min-height: 38px; padding: 0 14px; font-size: 0.9rem; border-radius: 11px; }
.link { color: var(--accent); text-decoration: underline; padding: 0; font-size: inherit; }
.icon { width: 40px; height: 40px; border-radius: 50%; background: var(--surface-2); }
button:disabled { opacity: 0.6; }
.pair { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; align-items: start; }

/* Readings */
.readings { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 330px) { .readings { grid-template-columns: 1fr; } }
.reading { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 14px 12px; display: grid; gap: 8px; min-width: 0; }
.reading-head { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.reading-head h2 { font-size: 0.9rem; color: var(--muted); font-weight: 600; }
.reading-value { font-size: 2.1rem; font-weight: 700; line-height: 1; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.reading-value small { font-size: 0.85rem; font-weight: 500; color: var(--muted); margin-left: 4px; letter-spacing: 0; }
.chip { font-size: 0.72rem; font-weight: 700; padding: 3px 9px; border-radius: 99px; white-space: nowrap; }
.chip.ok, i.ok { background: color-mix(in srgb, var(--ok) 20%, transparent); color: var(--ok); }
.chip.low, i.low { background: color-mix(in srgb, var(--low) 22%, transparent); color: var(--low); }
.chip.high, i.high { background: color-mix(in srgb, var(--high) 22%, transparent); color: var(--high); }
.chip.unsure, i.unsure { background: color-mix(in srgb, var(--muted) 22%, transparent); color: var(--muted); }
.reading.unsure .reading-value, .match-value.unsure, b.unsure { color: var(--muted); }
.match.weak { border-color: var(--low); }
b.ok, .match-value.ok { color: var(--ok); } b.low, .match-value.low { color: var(--low); } b.high, .match-value.high { color: var(--high); }

.scale { position: relative; padding-top: 15px; }
.scale-track { height: 12px; border-radius: 6px; border: 1px solid rgba(128, 128, 128, 0.35); }
.scale-ideal { position: absolute; top: 0; height: 12px; border: 1.5px solid var(--muted); border-bottom: 0; border-radius: 4px 4px 0 0; }
.scale-ideal span { position: absolute; top: -3px; left: 50%; transform: translate(-50%, -50%); background: var(--surface); padding: 0 4px; font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.scale-range { position: absolute; top: 12px; height: 18px; border-radius: 9px; border: 2px solid var(--text); opacity: 0.35; }
.scale-mark { position: absolute; top: 11px; width: 8px; height: 20px; margin-left: -4px; border-radius: 4px; background: var(--text); box-shadow: 0 0 0 2.5px var(--surface); }
.scale-ends { display: flex; justify-content: space-between; font-size: 0.68rem; color: var(--muted); margin-top: 5px; font-variant-numeric: tabular-nums; }

/* Plan */
.step { display: grid; grid-template-columns: 34px 1fr; gap: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.plan h2 + .step { border-top: 0; padding-top: 0; }
.step-num { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; background: var(--accent); color: var(--accent-ink); }
.step.done .step-num { background: var(--ok); color: #06281a; }
.step.done .step-amount, .step.done h3 { opacity: 0.6; }
.step-body { display: grid; gap: 6px; justify-items: start; min-width: 0; }
.step-body p { font-size: 0.92rem; }
.step-amount { font-size: 1.5rem; font-weight: 700; line-height: 1.15; letter-spacing: -0.01em; }
.step-amount span { display: block; font-size: 0.92rem; font-weight: 500; color: var(--muted); letter-spacing: 0; }
details summary { color: var(--accent); font-size: 0.9rem; cursor: pointer; }
details p { color: var(--muted); margin-top: 4px; }
.note, .safety, .warning, .nudge { border-radius: 12px; padding: 11px 13px; font-size: 0.92rem; }
.note { background: var(--surface-2); }
.safety { border: 1px dashed var(--line); color: var(--muted); font-size: 0.85rem; }
.warning { background: color-mix(in srgb, var(--low) 16%, transparent); border: 1px solid color-mix(in srgb, var(--low) 45%, transparent); }
.nudge { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--accent); display: grid; gap: 2px; }
.nudge.action { border-left-color: var(--low); }
.insight { display: grid; gap: 5px; }
.insight + .insight { border-top: 1px solid var(--line); padding-top: 12px; }
.insight h3 { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Tabs */
.tabs { position: relative; flex-shrink: 0; z-index: 5; display: grid; grid-template-columns: repeat(3, 1fr) 96px; align-items: center; gap: 4px; padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-top: 1px solid var(--line); }
html.standalone-pwa .tabs { padding-bottom: max(8px, calc(env(safe-area-inset-bottom) - 12px)); }
.tab { display: grid; justify-items: center; gap: 2px; padding: 6px 0; color: var(--muted); font-size: 0.72rem; font-weight: 600; border-radius: 12px; }
.tab.on { color: var(--accent); }
.tab svg, .fab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.fab { display: flex; align-items: center; justify-content: center; gap: 4px; height: 50px; border-radius: 16px; background: var(--accent); color: var(--accent-ink); font-weight: 700; }

/* History */
.charts > div { display: grid; gap: 6px; }
.charts h3 { display: flex; justify-content: space-between; align-items: baseline; }
.trend { width: 100%; height: 96px; display: block; background: var(--surface-2); border-radius: 10px; }
.trend .band { fill: color-mix(in srgb, var(--ok) 18%, transparent); }
.trend polyline { fill: none; stroke: var(--muted); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.trend circle, .trend path { stroke: var(--surface); stroke-width: 1; vector-effect: non-scaling-stroke; }
.trend .ok { fill: var(--ok); } .trend .low { fill: var(--low); } .trend .high { fill: var(--high); }
.trend circle.unsure { fill: var(--surface-2); stroke: var(--muted); stroke-width: 2; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 10px; width: 100%; padding: 11px 0; border-top: 1px solid var(--line); text-align: left; }
.row-when { display: grid; font-size: 0.92rem; }
.row-when small { color: var(--muted); }
.row-values { display: flex; gap: 5px; align-items: center; }
.row-values i { font-style: normal; font-weight: 700; font-size: 0.82rem; min-width: 40px; text-align: center; padding: 4px 6px; border-radius: 8px; font-variant-numeric: tabular-nums; }
.legend { text-align: right; font-size: 0.75rem; }

/* Settings */
.field { display: grid; gap: 5px; min-width: 0; }
.field > span { font-size: 0.82rem; color: var(--muted); font-weight: 600; }
.field small { color: var(--muted); }
input:not([type=checkbox]), select { display: block; width: 100%; max-width: 100%; height: 46px; padding: 0 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); min-width: 0; text-align: left; }
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.product { display: grid; gap: 10px; padding-top: 14px; border-top: 1px solid var(--line); }
.switch { display: flex; align-items: center; gap: 12px; }
.switch input { width: 22px; height: 22px; accent-color: var(--accent); }
.about { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; font-size: 0.92rem; }
.about dt { color: var(--muted); } .about dd { margin: 0; overflow-wrap: anywhere; }

/* Sheet */
dialog { border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 720px; max-height: 94dvh; border-radius: 24px 24px 0 0; background: var(--bg); color: var(--text); overflow: hidden; }
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(3, 10, 16, 0.6); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px 8px; }
.sheet-body { flex: 1; min-height: 0; overflow-y: auto; padding: 8px 18px calc(env(safe-area-inset-bottom) + 18px); display: grid; grid-auto-rows: max-content; gap: 14px; }
.sheet-body.busy { justify-items: center; padding: 70px 18px; }
.prep { margin: 0; padding: 0; list-style: none; counter-reset: prep; display: grid; gap: 10px; }
.prep li { counter-increment: prep; position: relative; padding: 14px 14px 14px 54px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; display: grid; gap: 8px; justify-items: start; }
.prep li::before { content: counter(prep); position: absolute; left: 14px; top: 13px; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; background: var(--accent); color: var(--accent-ink); }
.stage { background: #000; border-radius: 16px; overflow: hidden; display: grid; place-items: center; }
#stage { display: block; max-width: 100%; max-height: 46dvh; touch-action: none; }
.hint { margin-top: -6px; }
.matches { display: grid; gap: 12px; }
.match { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 13px; display: grid; gap: 8px; }
.match-head { display: flex; justify-content: space-between; align-items: baseline; }
.match-value { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.match-value small { font-size: 0.8rem; font-weight: 500; color: var(--muted); margin-left: 3px; }
.swatches { position: relative; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding-bottom: 12px; }
.swatch { height: 44px; border-radius: 8px; border: 1px solid rgba(128, 128, 128, 0.4); display: grid; place-items: end center; padding-bottom: 3px; }
.swatch span { font-size: 0.72rem; font-weight: 700; color: #111; background: rgba(255, 255, 255, 0.78); padding: 0 5px; border-radius: 6px; }
.swatch.picked { outline: 3px solid var(--text); outline-offset: 1px; }
.pointer { position: absolute; bottom: 0; width: 0; height: 0; margin-left: -7px; border: 7px solid transparent; border-bottom: 10px solid var(--text); border-top: 0; }
.pointer.manual { border-bottom-color: var(--accent); }
.sticky { position: sticky; bottom: calc(-1 * env(safe-area-inset-bottom) - 18px); margin: 0 -18px calc(-1 * env(safe-area-inset-bottom) - 18px); padding: 12px 18px calc(env(safe-area-inset-bottom) + 14px); background: var(--bg); border-top: 1px solid var(--line); grid-template-columns: 1fr 1.5fr; }
.photo { width: 100%; border-radius: 16px; max-height: 50dvh; object-fit: contain; background: #000; }
.detail { display: grid; gap: 2px; }
.detail > div { display: grid; grid-template-columns: 1fr auto; padding: 10px 0; border-top: 1px solid var(--line); }
.detail small { grid-column: 1 / -1; color: var(--muted); }

/* Boot, login, toast */
.boot, .login { flex: 1; min-height: 0; display: grid; place-content: center; justify-items: center; gap: 16px; padding: 24px; text-align: center; color: var(--muted); }
.login { justify-items: stretch; width: min(100%, 360px); margin: 0 auto; color: var(--text); text-align: left; }
.login h1, .login .boot-drop { justify-self: center; }
.login .link { justify-self: center; }
form.sheet-body .primary, form.card .primary { justify-self: stretch; }
.boot-drop { width: 34px; height: 34px; border-radius: 0 50% 50% 50%; transform: rotate(45deg); background: var(--accent); animation: bob 1.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: rotate(45deg) scale(0.82); opacity: 0.6; } }
@media (prefers-reduced-motion: reduce) { .boot-drop { animation: none; } }
#toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 92px); transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 30;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 99px; font-weight: 600; font-size: 0.9rem; transition: 0.2s; max-width: 90vw; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: var(--high); color: #fff; }

/* Light box */
.live { position: relative; display: inline-block; max-width: 100%; line-height: 0; }
.live video { display: block; max-width: 100%; max-height: 46dvh; }
.live canvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.calib { display: grid; gap: 8px; }
.calib > div { display: grid; grid-template-columns: 5.4em minmax(0, 1fr); gap: 8px; align-items: center; font-weight: 600; }
.calib .chips { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 4px; }
.calib i { height: 40px; border-radius: 8px; border: 1px solid rgba(128, 128, 128, 0.4); display: grid; place-items: end center; font-style: normal; }
.calib i b { font-size: 11px; font-weight: 600; color: #fff; text-shadow: 0 0 3px rgba(0, 0, 0, 0.9), 0 0 1px #000; padding-bottom: 2px; }
.row-values i.dot { min-width: 0; width: 16px; height: 16px; padding: 0; border-radius: 50%; border: 1px solid rgba(128, 128, 128, 0.4); }

/* Appearance choice */
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.segmented .on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

/* Sources */
.source { border-top: 1px solid var(--line); padding-top: 10px; }
.source summary { color: var(--text); font-size: 0.92rem; font-weight: 600; }
.source p { font-size: 0.9rem; }
.source a { color: var(--accent); font-size: 0.9rem; }
