/* Colours: chart series are slots 1 to 3 of the validated data-viz palette (blue, orange, aqua), checked
   against both page colours with validate_palette.py --pairs all. The app's own blue (--accent) is the icon's
   blue, so buttons, the icon and the bars read as one thing. */
:root {
  color-scheme: light dark;
  --page: #f5f5f7;
  --bar-bg: rgba(245, 245, 247, 0.82);
  --ink: #1c1c1e;
  --ink-2: #5c5c62;
  --muted: #8a8a8e;
  --grid: #e3e3e8;
  --axis: #c7c7cc;
  --hair: rgba(60, 60, 67, 0.16);
  --bar: #2a78d6;
  --limit: #eb6834;
  --avg: #1baf7a;
  --track: #d6e6fa;
  --over: #ec835a;
  --good: #1a7f37;
  --danger: #d03b3b;
  --danger-tint: #fbe7e7;
  --accent: #1f6bc7;
  --accent-ink: #ffffff;
  --tint: #e2edfb;
  --tint-ink: #1a5aa8;
  --tip: #2c2c2e;
  --tip-ink: #ffffff;
  --field: #ffffff;
  --field-line: #c7c7cc;
  --press: rgba(0, 0, 0, 0.06);
  --seg-on: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #000000;
    --bar-bg: rgba(0, 0, 0, 0.78);
    --ink: #f2f2f7;
    --ink-2: #aeaeb2;
    --muted: #8a8a8e;
    --grid: #26262a;
    --axis: #3a3a3f;
    --hair: rgba(235, 235, 245, 0.16);
    --bar: #3987e5;
    --limit: #d95926;
    --avg: #199e70;
    --track: #17304f;
    --over: #ec835a;
    --good: #30c75a;
    --danger: #ff6b6b;
    --danger-tint: #3a1717;
    --accent: #2f80e0;
    --accent-ink: #ffffff;
    --tint: #142c4a;
    --tint-ink: #7ab3f5;
    --tip: #e5e5ea;
    --tip-ink: #1c1c1e;
    --field: #1c1c1e;
    --field-line: #3a3a3f;
    --press: rgba(255, 255, 255, 0.08);
    --seg-on: #48484e;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; background: var(--page); }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 17px/1.45 -apple-system, system-ui, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

/* App bar: the icon and the name, pinned at the top under the status bar */
.appbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 20px 10px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--hair);
}
.appbar img { width: 30px; height: 30px; border-radius: 7px; display: block; }
.appbar span { font-size: 18px; font-weight: 650; }
.appbar-inner { max-width: 560px; width: 100%; margin: 0 auto; display: flex; align-items: center; gap: 10px; }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 20px calc(env(safe-area-inset-bottom) + 96px);
}

section { padding: 28px 0; border-top: 1px solid var(--hair); }
main > section:first-child,
.first-run #welcome,
html:not(.first-run) #today { border-top: 0; padding-top: 8px; }

h2 { font-size: 24px; font-weight: 700; margin: 0 0 14px; letter-spacing: 0; }
h3 { font-size: 17px; font-weight: 600; margin: 0; }
p { margin: 0; }

.note { color: var(--ink-2); font-size: 15px; }
.error { color: var(--danger); font-size: 15px; flex-basis: 100%; }

/* ---------- buttons ----------
   Main: solid blue, for the one thing each screen is for. Tinted: light blue, for the other actions.
   Plain: blue text, for small ones. Danger: red. Each carries an icon where it helps (.ic, inline SVG). */
button { font: inherit; color: inherit; cursor: pointer; -webkit-user-select: none; user-select: none; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--bar); outline-offset: 2px; }
input, select {
  font: inherit;
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--field-line);
  border-radius: 10px;
  padding: 9px 12px;
  min-height: 44px;
}
input[type="number"] { width: 4.5em; }

.ic { width: 1.15em; height: 1.15em; flex: none; display: block; }

.primary, .solid, .link, .tinted, .danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  border: 0;
  transition: transform 0.12s ease, opacity 0.12s ease, background-color 0.12s ease;
}
.primary:active, .solid:active, .tinted:active, .danger:active { transform: scale(0.97); opacity: 0.85; }
.link:active { opacity: 0.55; }

.primary {
  display: flex;
  width: 100%;
  margin-top: 22px;
  padding: 16px 18px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 19px;
  font-weight: 650;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12), 0 6px 16px rgba(31, 107, 199, 0.28);
}
.primary .ic { width: 24px; height: 24px; }

.solid {
  padding: 10px 18px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.tinted {
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--tint);
  color: var(--tint-ink);
  font-weight: 600;
}

.link {
  padding: 10px 2px;
  background: none;
  color: var(--tint-ink);
  font-weight: 500;
}

.danger {
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--danger-tint);
  color: var(--danger);
  font-weight: 600;
}
.link.danger-text { color: var(--danger); }

.buttons { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; margin-top: 16px; }

/* Welcome: shown alone until a unit is picked. The class is set on <html> before the page draws. */
.first-run main > section:not(#welcome) { display: none; }
html:not(.first-run) #welcome { display: none; }
.welcome-icon { width: 76px; height: 76px; border-radius: 17px; display: block; margin: 18px 0 18px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1), 0 8px 24px rgba(31, 107, 199, 0.25); }
#welcome h1 { font-size: 34px; font-weight: 750; line-height: 1.1; margin: 0 0 16px; }
.lead { font-size: 18px; line-height: 1.5; color: var(--ink-2); margin: 0 0 14px; }
.install-hint { display: flex; gap: 10px; margin: 6px 0 8px; padding: 12px 0 0; border-top: 1px solid var(--hair); }
.install-hint .ic { color: var(--tint-ink); margin-top: 2px; }
.unit-form { margin-top: 26px; }
.unit-form label { display: block; font-weight: 650; margin: 0 0 8px; }
.unit-form input { width: 100%; font-size: 20px; }
.picks { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 0; }
.picks button {
  min-height: 36px;
  padding: 6px 14px;
  border: 0;
  border-radius: 18px;
  background: var(--tint);
  color: var(--tint-ink);
  font-size: 15px;
  font-weight: 500;
  transition: transform 0.12s ease;
}
.picks button:active { transform: scale(0.95); }
#unit-setting { width: 10em; }

/* Today */
.nudge { display: flex; gap: 12px; margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--hair); }
.nudge > .ic { color: var(--limit); width: 22px; height: 22px; margin-top: 1px; }
.nudge .tinted { margin-top: 10px; }
.date { color: var(--ink-2); font-size: 15px; font-weight: 500; }
.hero { display: flex; align-items: baseline; gap: 10px; margin: 2px 0 14px; }
#count { font-size: 80px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
#count-note { color: var(--ink-2); font-size: 20px; }

.meter { display: flex; gap: 3px; height: 10px; margin: 0 0 8px; }
.meter span { flex: 1 1 0; min-width: 2px; border-radius: 3px; background: var(--track); }
.meter .used { background: var(--bar); }
.meter .over { background: var(--over); }
#meter-note { margin-bottom: 4px; }

.missed { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 4px; }
.missed label { color: var(--ink-2); }
#missed-toggle { margin-top: 8px; }

/* Over time */
/* the range picker is an iOS segmented control */
.range { display: flex; padding: 2px; margin: 0 0 22px; border-radius: 10px; background: var(--grid); }
.range button {
  flex: 1 1 0;
  min-height: 36px;
  padding: 6px 4px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--ink);
  font-size: 15px;
  font-weight: 500;
  transition: background-color 0.15s ease;
}
.range button[aria-checked="true"] { background: var(--seg-on); font-weight: 650; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.14); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); margin: 0 0 28px; }
.stat { padding: 0 12px; border-left: 1px solid var(--hair); min-width: 0; }
.stat:first-child { padding-left: 0; border-left: 0; }
.stat-label { color: var(--ink-2); font-size: 14px; }
.stat-value { font-size: 28px; font-weight: 650; line-height: 1.25; }
.stat-value.empty { font-size: 17px; line-height: 1.9; }
.stat-sub { color: var(--ink-2); font-size: 13px; line-height: 1.35; margin-top: 2px; }
.stat-sub.good { color: var(--good); }

.chart { margin: 0 0 30px; }
.chart:last-child { margin-bottom: 0; }
figcaption { margin-bottom: 10px; }
.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 6px 0 0; padding: 0; color: var(--ink-2); font-size: 14px; }
.legend li { display: flex; align-items: center; gap: 6px; }

.key { display: inline-block; flex: none; }
.key-bar { width: 10px; height: 10px; border-radius: 2px; background: var(--bar); }
.key-avg, .key-limit { width: 14px; height: 2px; border-radius: 1px; }
.key-avg { background: var(--avg); }
.key-limit { background: var(--limit); }

.plot { position: relative; }
.plot svg { display: block; touch-action: pan-y; -webkit-user-select: none; user-select: none; }
.plot svg:focus { outline: none; }
.plot svg:focus-visible { outline: 2px solid var(--bar); outline-offset: 2px; }
.plot text { font: 11px system-ui, -apple-system, sans-serif; fill: var(--muted); font-variant-numeric: tabular-nums; }
.plot .end-label { font-size: 12px; fill: var(--ink-2); }
.grid { stroke: var(--grid); stroke-width: 1; }
.baseline { stroke: var(--axis); stroke-width: 1; }
.bar { fill: var(--bar); }
.bar.on { opacity: 0.72; }
/* cigarette columns, when the unit is cigs */
.cig-paper { fill: #ffffff; stroke: var(--axis); stroke-width: 0.75; }
.cig-filter { fill: #dc9a52; }
.cig-band { stroke: #b87332; stroke-width: 1; }
.cig-ember { fill: #f2551f; }
.cig-ash { fill: #9a9aa0; }
.cigs .key-bar { width: 6px; height: 14px; border-radius: 1.5px; border: 0.75px solid var(--axis);
  background: linear-gradient(to top, #dc9a52 0 34%, #ffffff 34% 78%, #f2551f 78% 90%, #9a9aa0 90%); }
@media (prefers-color-scheme: dark) { .cig-paper { stroke: none; } .cigs .key-bar { border-color: transparent; } }
.limit-line { fill: none; stroke: var(--limit); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.avg-line { fill: none; stroke: var(--avg); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.avg-dot { fill: var(--avg); stroke: var(--page); stroke-width: 2; }
.cross { stroke: var(--axis); stroke-width: 1; }
.hit { fill: transparent; }

.tip {
  position: absolute;
  top: 0;
  z-index: 2;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--tip);
  color: var(--tip-ink);
  font-size: 14px;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
}
.tip-title { opacity: 0.72; }
.tip-row { display: flex; align-items: center; gap: 6px; }
.tip-row strong { font-weight: 650; }

details { margin-top: 8px; font-size: 15px; }
summary { color: var(--ink-2); padding: 6px 0; cursor: pointer; }
table { width: 100%; border-collapse: collapse; margin-top: 4px; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 0 6px 12px; text-align: right; border-bottom: 1px solid var(--hair); }
th:first-child, td:first-child { padding-left: 0; text-align: left; }
th { color: var(--ink-2); font-weight: 600; }

/* Plan */
.plan-summary { margin-bottom: 4px; }
.plan-summary + .buttons { margin-top: 14px; }
.plan-form { margin-top: 10px; }
.field { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 12px; }
#plan-preview { min-height: 1.45em; }

/* History */
.days { list-style: none; margin: 8px 0 0; padding: 0; }
.day { padding: 12px 0; border-bottom: 1px solid var(--hair); }
.day-head { display: flex; justify-content: space-between; gap: 12px; font-weight: 600; }
.day-head .of { color: var(--ink-2); font-weight: 400; }
.times { display: flex; flex-wrap: wrap; gap: 0 16px; margin-top: 2px; }
.time { min-height: 36px; padding: 6px 0; border: 0; background: none; color: var(--ink-2); font-size: 15px; font-variant-numeric: tabular-nums; }
.time:active { opacity: 0.55; }
.time.picked { color: var(--tint-ink); font-weight: 650; }
.times .none { color: var(--muted); font-size: 15px; padding: 6px 0; }
.edit { margin-top: 8px; }
.edit label { display: block; color: var(--ink-2); font-size: 15px; margin: 0 0 6px; }
.edit .buttons { margin-top: 10px; }
.edit .error { margin-top: 6px; }

/* Settings */
#settings .field { margin-bottom: 6px; }
.backup-note { margin-top: 18px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  width: min(520px, calc(100% - 32px));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--tip);
  color: var(--tip-ink);
  font-size: 16px;
  z-index: 10;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.25);
}
.toast button { display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 4px 0; border: 0; background: none; color: inherit; font-weight: 650; }
