:root {
  color-scheme: light;
  --canvas: #e9e7e0;
  --surface: #f8f7f3;
  --ink: #171b19;
  --muted: #686e6a;
  --line: #d4d5cf;
  --soft: #eeefea;
  --accent: #176c4e;
  --accent-ink: #fff;
  --accent-soft: #d9ebe2;
  --blue: #397591;
  --coral: #c06a4e;
  --focus: #0878a4;
  --font: "Avenir Next", Avenir, "Segoe UI", system-ui, sans-serif;
  --display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --canvas: #111412;
  --surface: #1a1e1b;
  --ink: #f1f3ef;
  --muted: #a6aea8;
  --line: #3a403c;
  --soft: #252a27;
  --accent: #86c8a8;
  --accent-ink: #102219;
  --accent-soft: #263e32;
  --blue: #87bdd4;
  --coral: #e39578;
  --focus: #b8e8ff;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--canvas); font-family: var(--font); }
body {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  color: var(--ink);
  background: var(--canvas);
}
button, input, textarea { font: inherit; color: inherit; }
button { min-height: 44px; cursor: pointer; }
button:disabled { cursor: default; opacity: .4; }
button:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible, .skip-link:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}
[hidden] { display: none !important; }

.skip-link {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 20;
  padding: .75rem 1rem;
  color: var(--surface);
  background: var(--ink);
  transform: translateY(-120%);
}
.skip-link:focus { transform: translateY(0); }

.shell {
  width: min(100%, 46rem);
  min-height: 100dvh;
  margin: 0 auto;
  padding: 1.1rem 1rem calc(5rem + env(safe-area-inset-bottom));
  background: var(--surface);
}
.topbar { padding: .75rem .2rem 1rem; }
h1, h2, p { margin-top: 0; }
h1 {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(2.35rem, 10vw, 3.65rem);
  font-weight: 500;
  letter-spacing: -.055em;
  line-height: .95;
}
h2 { margin-bottom: .35rem; font-size: 1.35rem; font-weight: 650; letter-spacing: -.025em; }
.eyebrow { margin-bottom: .35rem; color: var(--accent); font-size: .68rem; font-weight: 800; letter-spacing: .14em; }

.tabs {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: .25rem;
  margin: 0 -.25rem 1.25rem;
  padding: .25rem;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--line);
  border-radius: .8rem;
  backdrop-filter: blur(14px);
}
.tabs button {
  padding: .65rem .45rem;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: .55rem;
  font-size: .82rem;
  font-weight: 700;
}
.tabs button[aria-selected="true"] { color: var(--ink); background: var(--soft); }

.view { padding-top: .25rem; }
.phase-switch {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-bottom: 1.25rem;
  border-bottom: 1px solid var(--line);
}
.phase-switch button {
  position: relative;
  padding: .65rem .25rem;
  color: var(--muted);
  background: transparent;
  border: 0;
  font-size: .82rem;
  font-weight: 700;
}
.phase-switch button[aria-pressed="true"] { color: var(--ink); }
.phase-switch button[aria-pressed="true"]::after {
  position: absolute;
  right: .25rem;
  bottom: -1px;
  left: .25rem;
  height: 3px;
  background: var(--accent);
  border-radius: 3px 3px 0 0;
  content: "";
}
#checkin-view .phase-switch { margin-bottom: 0; }
.input-mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .35rem;
  margin: 1.15rem 0 .3rem;
  padding: .3rem;
  background: var(--soft);
  border-radius: .75rem;
}
.input-mode-switch button {
  min-height: 48px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: .52rem;
  font-size: .9rem;
  font-weight: 800;
}
.input-mode-switch button[aria-pressed="true"] {
  color: var(--accent-ink);
  background: var(--accent);
}
.question-form { border-top: 0; }
.form-question {
  min-width: 0;
  margin: 0;
  padding: 1.8rem .2rem 1.7rem;
  border: 0;
  border-bottom: 1px solid var(--line);
}
.question-prompt {
  margin: 0 0 1.15rem;
  padding: 0;
  font-size: clamp(1.08rem, 4.5vw, 1.28rem);
  font-weight: 700;
  letter-spacing: -.02em;
  line-height: 1.25;
}
.choice-list { display: grid; gap: .45rem; }
.choice-option, .question-toggle, .toggle-label {
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
.choice-option {
  display: flex;
  align-items: center;
  min-height: 50px;
  padding: .65rem .8rem;
  background: var(--soft);
  border: 1px solid transparent;
  border-radius: .65rem;
}
.choice-option:has(input:checked) { background: var(--accent-soft); border-color: var(--accent); }
.choice-option input, .question-toggle input, .toggle-label input { width: 1.25rem; height: 1.25rem; margin: 0 .75rem 0 0; accent-color: var(--accent); flex: 0 0 auto; }
.choice-option span { font-size: .9rem; font-weight: 600; }
.plain-input { display: flex; align-items: center; gap: .65rem; }
.plain-input input, .plain-input textarea, .plain-input select {
  width: 100%;
  min-height: 50px;
  padding: .8rem .9rem;
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: .65rem;
  font-size: 1rem;
}
.plain-input textarea { min-height: 76px; resize: vertical; line-height: 1.4; }
.plain-input span { flex: 0 0 auto; color: var(--muted); font-size: .78rem; }
.scale-input { display: grid; grid-template-columns: 2.5rem 1fr; align-items: center; gap: .75rem; }
.scale-input output { font-size: 1.5rem; font-weight: 750; font-variant-numeric: tabular-nums; text-align: center; }
.scale-input input { width: 100%; accent-color: var(--accent); }
.scale-ends { grid-column: 2; display: flex; justify-content: space-between; color: var(--muted); font-size: .7rem; }

.primary-action {
  width: 100%;
  min-height: 54px;
  margin-top: 1.25rem;
  padding: .8rem 1rem;
  color: var(--accent-ink);
  background: var(--accent);
  border: 0;
  border-radius: .75rem;
  box-shadow: 0 8px 24px rgb(0 0 0 / 18%);
  font-size: .95rem;
  font-weight: 800;
}
.text-action, .list-actions button, .week-nav button {
  color: var(--accent);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: .65rem;
  font-size: .82rem;
  font-weight: 700;
}
.empty-state { padding: 2rem .2rem; color: var(--muted); }
.empty-state p { margin-bottom: 1rem; }
.empty-state .text-action { padding: .55rem .8rem; }

.rant-view { padding: 1.8rem .2rem 0; }
.rant-view blockquote {
  margin: .35rem 0 2rem;
  font-family: var(--display);
  font-size: clamp(2rem, 9vw, 3rem);
  letter-spacing: -.045em;
  line-height: 1.05;
}
.rant-view h2 { margin-bottom: .75rem; }
.rant-checklist { margin: 0 0 1.6rem; padding: 0; list-style: none; border-top: 1px solid var(--line); }
.rant-checklist li {
  position: relative;
  min-height: 48px;
  padding: .85rem .2rem .85rem 1.7rem;
  border-bottom: 1px solid var(--line);
  font-size: .9rem;
  font-weight: 600;
  line-height: 1.35;
}
.rant-checklist li::before {
  position: absolute;
  top: 1.05rem;
  left: .25rem;
  width: .62rem;
  height: .62rem;
  border: 1.5px solid var(--accent);
  content: "";
}
.rant-footnote { margin: .7rem 0 0; color: var(--muted); font-size: .75rem; text-align: center; }

.setup-section { margin-bottom: 2rem; }
.schedule-list { margin-top: .9rem; border-top: 1px solid var(--line); }
.schedule-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 1rem;
  min-height: 64px;
  border-bottom: 1px solid var(--line);
}
.toggle-label { display: flex; align-items: center; min-height: 50px; font-size: .95rem; font-weight: 700; }
.schedule-time { min-height: 44px; padding: .5rem .65rem; background: var(--soft); border: 1px solid var(--line); border-radius: .55rem; }
.question-heading { margin-bottom: .8rem; }
.question-heading > p, .date-range, .section-heading > p { margin-bottom: 0; color: var(--muted); font-size: .78rem; }
.list-actions { display: flex; justify-content: flex-end; gap: .45rem; margin: -.45rem 0 .65rem; }
.list-actions button { min-height: 44px; padding: .4rem .7rem; }
.question-list { border-top: 1px solid var(--line); }
.question-toggle { display: flex; align-items: center; min-height: 58px; padding: .65rem .15rem; border-bottom: 1px solid var(--line); }
.question-toggle span { font-size: .9rem; font-weight: 600; line-height: 1.3; }

.review-header { display: grid; gap: .65rem; }
.date-range { padding-left: .15rem; }
.week-nav { display: grid; grid-template-columns: 48px 1fr 48px; gap: .5rem; margin-bottom: 1rem; }
.week-nav button { font-size: .85rem; }
.metric-strip { display: grid; grid-template-columns: repeat(2, 1fr); margin-bottom: 1.4rem; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.metric-strip > div { min-height: 5.8rem; padding: 1rem .75rem; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.metric-strip > div:nth-child(2n) { border-right: 0; }
.metric-strip > div:nth-child(n + 3) { border-bottom: 0; }
.metric-strip strong { display: block; margin-bottom: .45rem; font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.metric-strip span { color: var(--muted); font-size: .75rem; }
.review-block { margin: 1.6rem 0; padding: 1.3rem .2rem; border-top: 1px solid var(--line); }
.section-heading { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.completion-grid { display: grid; grid-template-columns: 2rem repeat(3, 1fr); gap: .4rem; }
.grid-cell { display: grid; min-height: 2.6rem; place-items: center; border-radius: .55rem; font-size: .75rem; }
.grid-label { color: var(--muted); font-size: .65rem; font-weight: 700; }
.done { color: var(--accent-ink); background: var(--accent); font-size: .9rem; }
.missing { border: 1px dashed var(--line); }
.future, .off { background: var(--soft); }
.off { opacity: .45; }
.legend { display: flex; align-items: center; gap: .35rem; color: var(--muted); font-size: .68rem; white-space: nowrap; }
.legend span { width: .55rem; height: .55rem; margin-left: .35rem; border-radius: 50%; }
.sleep-dot { background: var(--blue); }
.function-dot { background: var(--accent); }
.chart-wrap { width: 100%; overflow: hidden; }
.chart-wrap svg { display: block; width: 100%; height: auto; }
.chart-gridline { stroke: var(--line); stroke-width: 1; }
.chart-label, .chart-day { fill: var(--muted); font: 11px var(--font); }
.chart-day { text-anchor: middle; }
.sleep-line, .function-line { fill: none; stroke-linecap: round; stroke-linejoin: round; stroke-width: 4; vector-effect: non-scaling-stroke; }
.sleep-line { stroke: var(--blue); }
.function-line { stroke: var(--accent); }
.dose-chart {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: clamp(.28rem, 2vw, .7rem);
  min-height: 11rem;
}
.dose-chart > .review-empty { grid-column: 1 / -1; align-self: center; }
.dose-column {
  display: grid;
  grid-template-rows: 1.35rem 1fr 1rem;
  min-width: 0;
  text-align: center;
}
.dose-value, .dose-day { color: var(--muted); font-size: .65rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.dose-track {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-height: 8rem;
  border-bottom: 1px solid var(--line);
}
.dose-bar {
  display: block;
  width: min(62%, 2rem);
  background: var(--accent);
  border-radius: .28rem .28rem 0 0;
}
.dose-day { padding-top: .45rem; }
.bar-chart { display: grid; gap: .65rem; }
.bar-row { display: grid; grid-template-columns: 2.4rem 1fr; align-items: center; gap: .5rem; }
.bar-label { color: var(--muted); font-size: .7rem; }
.bar-tracks { display: grid; gap: .2rem; }
.bar-track { height: .55rem; overflow: hidden; background: var(--soft); border-radius: 1rem; }
.bar-track span { display: block; height: 100%; border-radius: inherit; }
.work-bar { background: var(--accent); }
.scroll-bar { background: var(--coral); }
.insight-points { margin: 0; padding-left: 1.2rem; }
.insight-points li { margin-bottom: .65rem; padding-left: .15rem; font-size: .88rem; line-height: 1.45; }
.effect-list { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .8rem; }
.effect-list span { padding: .4rem .65rem; color: var(--muted); background: var(--soft); border-radius: 2rem; font-size: .7rem; }
.daily-section { padding: 1rem .2rem 0; }
.review-empty {
  position: relative;
  display: grid;
  grid-template-columns: 3.2rem 1fr;
  align-items: center;
  gap: 1rem;
  min-height: 7.25rem;
  padding: 1.15rem 1rem;
  overflow: hidden;
  background: var(--soft);
  border: 1px solid color-mix(in srgb, var(--line) 72%, transparent);
  border-radius: .8rem;
}
.review-empty strong { display: block; margin-bottom: .3rem; font-size: .92rem; }
.review-empty p { margin: 0; color: var(--muted); font-size: .78rem; line-height: 1.45; }
.empty-orbit {
  position: relative;
  display: block;
  width: 2.9rem;
  height: 2.9rem;
  border: 1px solid var(--line);
  border-radius: 50%;
}
.empty-orbit::before {
  position: absolute;
  top: 50%;
  right: -.24rem;
  width: .48rem;
  height: .48rem;
  background: var(--accent);
  border: 2px solid var(--soft);
  border-radius: 50%;
  transform: translateY(-50%);
  content: "";
}
.empty-orbit::after {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1px;
  height: 1.5rem;
  background: var(--line);
  transform: translate(-50%, -50%) rotate(35deg);
  content: "";
}
.day-detail { border-top: 1px solid var(--line); }
.day-detail summary { min-height: 48px; padding: .9rem 0; font-size: .88rem; cursor: pointer; }
.day-detail ul { margin: 0 0 1rem; padding-left: 1.2rem; color: var(--muted); font-size: .82rem; line-height: 1.5; }
.fatal-state { padding: 4rem .2rem; }
.fatal-state p { color: var(--muted); line-height: 1.5; }
.toast {
  position: fixed;
  right: 1rem;
  bottom: max(1rem, env(safe-area-inset-bottom));
  left: 1rem;
  z-index: 10;
  width: min(calc(100% - 2rem), 30rem);
  min-height: 3.5rem;
  margin: auto;
  padding: 1rem;
  color: var(--surface);
  background: var(--ink);
  border-radius: .8rem;
  font-size: .85rem;
}

@media (min-width: 40rem) {
  body { padding: 2rem 1rem; }
  .shell { min-height: calc(100dvh - 4rem); padding: 2rem 2.5rem 5rem; border: 1px solid var(--line); border-radius: 1.5rem; }
  .metric-strip { grid-template-columns: repeat(4, 1fr); }
  .metric-strip > div { border-right: 1px solid var(--line); border-bottom: 0; }
  .metric-strip > div:nth-child(2n) { border-right: 1px solid var(--line); }
  .metric-strip > div:last-child { border-right: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
