:root {
  color-scheme: light dark;
  --bg: #fff;
  --text: #111;
  --muted: #6b6f7a;
  --border: #ddd;
  --border-subtle: #eee;
  --input-bg: #fff;
  --card-bg: #fafafa;
  --wash: rgba(0, 0, 0, 0.05);
  --accent: #2563eb;
  --accent-text: #fff;
  --notice-bg: #eef4ff;
  /* Chart series (validated pair, see README "Chart"). */
  --headache: #2a78d6;
  --migraine: #eb6834;
  --grid: #e8e8e4;
  --axis: #c3c2b7;
}

/* System preference is the default signal (no explicit choice stored)... */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171d;
    --text: #e7e7ea;
    --muted: #9a9fae;
    --border: #33353f;
    --border-subtle: #2a2c35;
    --input-bg: #1e2027;
    --card-bg: #1b1c23;
    --wash: rgba(255, 255, 255, 0.06);
    --accent: #3b76f0;
    --notice-bg: #1c2438;
    --headache: #3987e5;
    --migraine: #d95926;
    --grid: #2a2c35;
    --axis: #3a3c46;
  }
}

/* ...but an explicit toggle choice (stamped as data-theme on <html>) always wins. */
:root[data-theme="dark"] {
  --bg: #16171d;
  --text: #e7e7ea;
  --muted: #9a9fae;
  --border: #33353f;
  --border-subtle: #2a2c35;
  --input-bg: #1e2027;
  --card-bg: #1b1c23;
  --wash: rgba(255, 255, 255, 0.06);
  --accent: #3b76f0;
  --notice-bg: #1c2438;
  --headache: #3987e5;
  --migraine: #d95926;
  --grid: #2a2c35;
  --axis: #3a3c46;
}

/* See notes/public/style.css: pins native form controls to the toggle too. */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
/* Beat the display rules below (label, .tz-banner, ...) for [hidden]. */
[hidden] { display: none !important; }
body { margin: 0; font-family: system-ui, sans-serif; color: var(--text); background: var(--bg); line-height: 1.4; }
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0; }

.topnav {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}
.topnav a { text-decoration: none; font-weight: 600; color: var(--text); }
.topnav-brand { display: flex; align-items: center; gap: 0.5rem; }
.topnav-right { display: flex; align-items: center; gap: 0.75rem; }
.nav-logo { height: 1.5rem; width: auto; display: block; }
.nav-user { font-weight: 500 !important; color: var(--muted) !important; }

.theme-toggle { display: flex; gap: 0.25rem; }
.theme-toggle button {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2rem 0.45rem;
  font-size: 0.9rem;
  line-height: 1.4;
  color: var(--text);
  cursor: pointer;
}
.theme-toggle button.active {
  background: var(--border-subtle);
  border-color: var(--muted);
}

.container { max-width: 900px; margin: 0 auto; padding: 1.5rem 1rem; }
.container.narrow { max-width: 480px; }
.container.medium { max-width: 640px; }

.notice {
  max-width: 900px;
  margin: 1rem auto 0;
  padding: 0.6rem 1rem;
  background: var(--notice-bg);
  border-radius: 6px;
  width: calc(100% - 2rem);
}
.error { color: #ef4444; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }
.muted { color: var(--muted); }
.meta { color: var(--muted); font-size: 0.85rem; }
.optional { color: var(--muted); font-weight: 400; }
.links { font-size: 0.9rem; }

.button {
  display: inline-block;
  padding: 0.5rem 0.9rem;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 6px;
  text-decoration: none;
  border: 1px solid transparent;
  font-size: 0.95rem;
  font-family: inherit;
  cursor: pointer;
  text-align: center;
}
.button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
.button.secondary:hover { background: var(--wash); }
.button.danger { background: #b91c1c; color: #fff; }
.button.big { padding: 0.75rem 1.2rem; font-size: 1rem; font-weight: 600; }
.link-button {
  background: none; border: none; padding: 0; font: inherit; font-size: 0.9rem;
  color: var(--muted); cursor: pointer; text-decoration: underline;
}

form { display: flex; flex-direction: column; gap: 1rem; }
form.inline { display: inline; }
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.9rem; font-weight: 500; }
label.checkbox { flex-direction: row; align-items: center; gap: 0.5rem; }
input[type="text"], input[type="password"], input[type="email"], input[type="datetime-local"], textarea {
  padding: 0.5rem;
  font-size: 1rem;
  font-family: inherit;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 100%;
}
input[type="checkbox"] { width: 1.1rem; height: 1.1rem; }
textarea { resize: vertical; }
fieldset { border: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
legend { font-size: 0.9rem; font-weight: 500; padding: 0; margin-bottom: 0.4rem; }
.form-actions { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.hp { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }

/* Strength: a 1–10 row of radio buttons (no JS needed, easy to tap). */
.scale { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; }
.scale label, .chip { position: relative; }
.scale input, .chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.scale span {
  display: block;
  text-align: center;
  padding: 0.6rem 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  background: var(--input-bg);
}
.scale label.high span { border-bottom: 3px solid var(--migraine); }
.scale input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.scale input:focus-visible + span, .chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.scale-ends { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--muted); }

/* Multi-select chips (locations, symptoms, triggers). */
.chip-groups { display: flex; flex-direction: column; gap: 0.6rem; }
.chip-group-label { font-size: 0.8rem; color: var(--muted); margin-bottom: 0.25rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip span {
  display: inline-block;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 400;
  cursor: pointer;
  background: var(--input-bg);
  user-select: none;
}
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

details.optional-section {
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 0.75rem 1rem;
}
details.optional-section summary { cursor: pointer; font-weight: 500; }
details.optional-section[open] summary { margin-bottom: 1rem; }
details.optional-section .section-body { display: flex; flex-direction: column; gap: 1rem; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 1rem;
}
section + section { margin-top: 1.5rem; }

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 500;
  padding: 0.1rem 0.45rem;
  margin-left: 0.3rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  vertical-align: middle;
}
.badge.migraine { border-color: var(--migraine); }
.badge.migraine::before, .badge.ongoing::before {
  content: '';
  display: inline-block;
  width: 0.5rem; height: 0.5rem;
  border-radius: 50%;
  margin-right: 0.3rem;
  background: var(--migraine);
}
.badge.ongoing::before { background: var(--accent); }

/* Dashboard */
.current-title { display: flex; align-items: center; flex-wrap: wrap; gap: 0.25rem; margin-bottom: 0.25rem; }
.action-row { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 0.9rem; }
.tz-banner { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 1rem; font-size: 0.9rem; }

.section-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.75rem; }
.tabs { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.tabs a { padding: 0.3rem 0.75rem; text-decoration: none; color: var(--text); font-size: 0.85rem; }
.tabs a + a { border-left: 1px solid var(--border); }
.tabs a[aria-current="page"] { background: var(--wash); font-weight: 600; }

.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; margin-bottom: 1rem; }
.stat { display: flex; flex-direction: column; }
.stat-label { font-size: 0.8rem; color: var(--muted); }
.stat-value { font-size: 1.6rem; font-weight: 600; }

/* Chart: HTML bars rather than a scaled SVG so the text stays at real
   size on a phone. Heights are percentages of .plot, from src/chart.js. */
.chart { margin: 0; }
.legend { display: flex; gap: 1rem; font-size: 0.8rem; color: var(--muted); margin: 0.25rem 0 0.75rem; }
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.legend i { width: 0.7rem; height: 0.7rem; border-radius: 2px; display: inline-block; }
.key-headache { background: var(--headache); }
.key-migraine { background: var(--migraine); }
.plot-wrap { display: grid; grid-template-columns: 1.75rem 1fr; }
.y-axis, .plot { position: relative; height: 180px; }
.y-axis span {
  position: absolute; right: 0.4rem; transform: translateY(50%);
  font-size: 0.7rem; color: var(--muted); font-variant-numeric: tabular-nums;
}
.plot { display: flex; align-items: stretch; border-bottom: 1px solid var(--axis); }
.gridline { position: absolute; left: 0; right: 0; border-top: 1px solid var(--grid); }
.col {
  position: relative;
  flex: 1;
  display: flex;
  justify-content: center;
  border-radius: 4px 4px 0 0;
  outline: none;
}
.col:hover, .col:focus-visible { background: var(--wash); }
.stack {
  position: relative;
  width: 60%;
  max-width: 24px;
  height: 100%;
  display: flex;
  flex-direction: column-reverse;
  gap: 2px;
}
.seg { width: 100%; flex: none; }
.seg.headache { background: var(--headache); }
.seg.migraine { background: var(--migraine); }
.stack .seg:last-child { border-radius: 4px 4px 0 0; }
.col[data-tip]:hover::after, .col[data-tip]:focus-visible::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  z-index: 2;
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  pointer-events: none;
}
.col.tip-start[data-tip]:hover::after, .col.tip-start[data-tip]:focus-visible::after { left: 0; transform: none; }
.col.tip-end[data-tip]:hover::after, .col.tip-end[data-tip]:focus-visible::after { left: auto; right: 0; transform: none; }
.x-axis { display: flex; margin-left: 1.75rem; height: 1.2rem; }
.x-axis span { flex: 1; position: relative; }
.x-axis span b {
  position: absolute; left: 50%; transform: translateX(-50%);
  font-weight: 400; font-size: 0.7rem; color: var(--muted); white-space: nowrap; top: 0.2rem;
}
.x-axis span:last-child b { left: auto; right: 0; transform: none; }
.x-axis span:first-child b { left: 0; transform: none; }
.chart-table { margin-top: 0.75rem; font-size: 0.85rem; }
.chart-table summary { cursor: pointer; color: var(--muted); }
table { border-collapse: collapse; width: 100%; margin-top: 0.5rem; }
th, td { text-align: left; padding: 0.3rem 0.5rem; border-bottom: 1px solid var(--border-subtle); }
th { font-weight: 500; color: var(--muted); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Headache list + timeline */
.h-list { list-style: none; padding: 0; margin: 0; }
.h-list li + li { border-top: 1px solid var(--border-subtle); }
.h-link { display: flex; gap: 0.8rem; align-items: center; padding: 0.7rem 0.25rem; color: var(--text); text-decoration: none; }
.h-link:hover { background: var(--wash); }
.strength-chip {
  flex: none;
  min-width: 2.4rem; height: 2.4rem;
  padding: 0 0.3rem;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  color: #fff;
  background: var(--headache);
}
/* Ink rather than white on orange: white only reaches ~3:1 there. */
.strength-chip.migraine { background: var(--migraine); color: #111; }
.strength-chip small { font-size: 0.6rem; font-weight: 500; opacity: 0.85; margin-left: 1px; }
.h-main { display: flex; flex-direction: column; gap: 0.15rem; min-width: 0; }
.h-title { font-weight: 500; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 0.75rem; font-size: 0.9rem; }
.empty { color: var(--muted); }

.detail-head { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; align-items: flex-start; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 0.75rem; margin: 1rem 0; }
.tag-list { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.3rem; }
.tag { font-size: 0.8rem; padding: 0.15rem 0.55rem; border-radius: 999px; background: var(--wash); }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { display: flex; gap: 0.8rem; padding: 0.75rem 0; border-top: 1px solid var(--border-subtle); }
.timeline .entry-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.2rem; }
.timeline .entry-actions { display: flex; gap: 0.75rem; font-size: 0.85rem; align-items: baseline; }
.notes-text { white-space: pre-wrap; margin: 0.3rem 0 0; }

.account-section h2 { margin-bottom: 0.5rem; }
.account-section + .account-section { margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid var(--border-subtle); }

.admin-table { font-size: 0.85rem; }
.admin-table td { vertical-align: top; }
.admin-table .actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.admin-table .button { padding: 0.25rem 0.55rem; font-size: 0.8rem; }
.match-match { color: #16a34a; }
.match-mismatch, .match-no-account { color: #ef4444; }
.match-no-email { color: #ca8a04; }
.generated { word-break: break-all; }
.table-scroll { overflow-x: auto; }

@media (max-width: 480px) {
  .stat-value { font-size: 1.3rem; }
  .action-row .button { flex: 1 1 100%; }
  .action-row form.inline { display: contents; }
}
