:root {
  --bg: #f4f3ef;
  --card: #ffffff;
  --text: #23262c;
  --muted: #6c717a;
  --faint: #9a9ea6;
  --line: rgb(0 0 0 / 0.07);
  --grid: rgb(0 0 0 / 0.06);
  --accent: 88 104 204;
  --now: #d0605e;
  --danger: #b4443f;
  --radius: 18px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.04);

  /* Per-pill hues as RGB triplets so shading can vary only in opacity. */
  --xanax: 124 106 214;
  --klonopin: 38 146 134;
  --valium: 206 128 60;
  --other: 120 126 138;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --card: #1b1e24;
    --text: #e6e7ea;
    --muted: #a0a5ae;
    --faint: #737882;
    --line: rgb(255 255 255 / 0.08);
    --grid: rgb(255 255 255 / 0.06);
    --accent: 140 154 240;
    --now: #ef8a86;
    --danger: #ef8a86;
    --shadow: none;
    --xanax: 164 148 245;
    --klonopin: 88 196 180;
    --valium: 232 164 100;
    --other: 150 156 168;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, sans-serif;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

[hidden] { display: none !important; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid rgb(var(--accent)); outline-offset: 2px; }

/* ---------- Header / tabs ---------- */
.top { display: flex; align-items: center; gap: 12px; padding: 0 2px 10px; }
.wordmark {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--muted);
}
.tabs { display: flex; gap: 2px; margin-left: auto; }
.tabs a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  color: var(--muted);
  text-decoration: none;
  font-size: 15px;
  font-weight: 500;
}
.tabs a[aria-current="page"] { color: var(--text); background: var(--card); box-shadow: var(--shadow); }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 {
  margin: 0 0 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.02em;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.card-head h2 { margin: 0; }

.empty { margin: 6px 0 4px; color: var(--muted); }
.empty.small { font-size: 15px; }
.hint { margin: -4px 0 12px; font-size: 14px; color: var(--muted); }

/* Segmented control (chart window) */
.seg-control {
  display: flex;
  background: var(--bg);
  border-radius: 12px;
  padding: 2px;
}
.seg-control button {
  min-width: 40px;
  min-height: 36px;
  padding: 0 6px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--muted);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.seg-control button[aria-pressed="true"] { background: var(--card); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

/* ---------- Login ---------- */
.login form { display: grid; gap: 10px; }
.login label { color: var(--muted); font-size: 15px; }
.error { color: var(--danger); margin: 0; min-height: 1.4em; font-size: 15px; }

input[type="text"], input[type="password"], input[type="time"], input[type="number"] {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-size: 17px; /* >=16px avoids iOS zoom-on-focus */
}

.primary, .secondary {
  min-height: 52px;
  border: 0;
  border-radius: 14px;
  font-weight: 600;
  padding: 0 18px;
}
.primary { background: rgb(var(--accent)); color: #fff; }
.secondary { background: var(--bg); color: var(--text); }
.primary:disabled, .secondary:disabled { opacity: 0.4; }

/* ---------- Chart ---------- */
.plot { position: relative; }

.row { --c: var(--accent); }
.drug-xanax { --c: var(--xanax); }
.drug-klonopin { --c: var(--klonopin); }
.drug-valium { --c: var(--valium); }
.kind-other { --c: var(--other); }

.grid, .now-line { position: absolute; top: 22px; bottom: 22px; left: 0; right: 0; pointer-events: none; }
.gridline { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--grid); }
.gridline.day { background: var(--line); width: 1.5px; }
.now-line {
  left: auto; right: auto;
  width: 2px;
  margin-left: -1px;
  background: var(--now);
  opacity: 0.55;
  border-radius: 1px;
}

.now-strip { position: relative; height: 22px; }
.now-pill {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  font-size: 12px;
  font-weight: 600;
  color: var(--now);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.now-pill.edge-l { transform: translateX(-4px); }
.now-pill.edge-r { transform: translateX(calc(-100% + 4px)); }

.row { position: relative; padding: 8px 0 10px; }
.row + .row { border-top: 1px dashed var(--line); }

.row-head {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  font-variant-numeric: tabular-nums;
}
.row-name { font-weight: 600; }
.row-time { color: var(--muted); }
.row-head .del { margin-left: auto; }

.track { position: relative; height: 40px; margin: 4px 0 6px; overflow: hidden; }
.curve { display: block; width: 100%; height: 100%; overflow: visible; }
.curve .baseline { stroke: var(--line); stroke-width: 1; vector-effect: non-scaling-stroke; }
.curve .stop-top { stop-color: rgb(var(--c)); stop-opacity: 0.55; }
.curve .stop-bottom { stop-color: rgb(var(--c)); stop-opacity: 0.12; }
.curve .outline {
  fill: none;
  stroke: rgb(var(--c));
  stroke-width: 1.75;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}
/* Curves running off either edge fade out rather than stopping abruptly. */
.track.off-r { -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); }
.track.off-l { -webkit-mask-image: linear-gradient(to left, #000 calc(100% - 20px), transparent); mask-image: linear-gradient(to left, #000 calc(100% - 20px), transparent); }
.track.off-l.off-r {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 20px, #000 calc(100% - 28px), transparent);
  mask-image: linear-gradient(to right, transparent, #000 20px, #000 calc(100% - 28px), transparent);
}
.runoff { position: absolute; right: 0; bottom: 2px; font-size: 14px; line-height: 1; color: rgb(var(--c)); }
.taken-dot {
  position: absolute;
  bottom: -3px;
  width: 7px;
  height: 7px;
  margin-left: -3.5px;
  border-radius: 50%;
  background: rgb(var(--c));
}

.row-text { margin: 0; font-size: 15px; line-height: 1.4; }
.row-note { margin: 2px 0 0; font-size: 14px; color: var(--muted); }

.axis { position: relative; height: 22px; margin-top: 2px; }
.tick {
  position: absolute;
  top: 4px;
  transform: translateX(-50%);
  font-size: 12px;
  color: var(--faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tick.day { color: var(--muted); font-weight: 600; }
.tick.edge-l { transform: none; }
.tick.edge-r { transform: translateX(-100%); }
.other-mark {
  position: absolute;
  top: -3px;
  width: 2px;
  height: 8px;
  margin-left: -1px;
  border-radius: 1px;
  background: rgb(var(--other));
}

.others-now:empty { display: none; }
.others-now { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line); }
.other-line { margin: 2px 0; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.other-dot {
  display: inline-block;
  width: 2px;
  height: 10px;
  margin-right: 8px;
  vertical-align: -1px;
  border-radius: 1px;
  background: rgb(var(--other));
}

/* Delete buttons: quiet but a full 44px tap target. */
.del {
  flex: none;
  width: 44px;
  height: 44px;
  margin: -6px -10px -6px 0;
  border: 0;
  background: none;
  color: var(--faint);
  font-size: 22px;
  line-height: 1;
  border-radius: 12px;
}
.del:not(:disabled):hover, .del:not(:disabled):active { color: var(--danger); background: var(--line); }
.del:disabled { opacity: 0.3; }

/* ---------- Log form ---------- */
.drug-picks { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.drug-pick {
  min-height: 60px;
  border: 1.5px solid rgb(var(--c) / 0.35);
  border-radius: 16px;
  background: rgb(var(--c) / 0.08);
  font-weight: 600;
  font-size: 16px;
  padding: 0 4px;
  transition: background 0.15s, border-color 0.15s;
}
.drug-pick[aria-pressed="true"] {
  background: rgb(var(--c) / 0.85);
  border-color: transparent;
  color: #fff;
}

fieldset { border: 0; margin: 16px 0 0; padding: 0; min-width: 0; }
legend { padding: 0; margin-bottom: 8px; font-size: 14px; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:empty { display: none; }
.chip {
  min-height: 44px;
  min-width: 64px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}
.chip[aria-pressed="true"] {
  background: rgb(var(--accent) / 0.14);
  border-color: rgb(var(--accent) / 0.6);
  font-weight: 600;
}
#other-fields input { margin-top: 10px; }
.amount-chips { margin-top: 10px; }

.inline-field { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.inline-field span { color: var(--muted); font-size: 15px; }
.inline-field input { max-width: 150px; }
.when-summary { margin: 8px 0 0; font-size: 15px; color: var(--muted); font-variant-numeric: tabular-nums; }

#note { margin-top: 16px; }
#log-submit { width: 100%; margin-top: 14px; }

/* ---------- History ---------- */
.hist-list { list-style: none; margin: 0; padding: 0; }
.date-head {
  margin: 14px 0 2px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.date-head:first-child { margin-top: 0; }
.hist-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  border-top: 1px solid var(--line);
}
.date-head + .hist-item { border-top: 0; }
.hist-time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; width: 3.2em; }
.hist-what { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.kind-other .hist-what { color: var(--muted); }
.swatch {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: 2px;
  background: rgb(var(--c));
}
.hist-note { display: block; font-size: 14px; color: var(--muted); }
.hist-item .del { margin-right: -10px; }

/* ---------- Timings ---------- */
.profile-form { padding: 14px 0 16px; border-top: 1px solid var(--line); }
.profile-form h3 { margin: 0 0 10px; font-size: 17px; display: flex; align-items: center; }
.badge {
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 999px;
  background: rgb(var(--c) / 0.15);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.pair, .decline-row { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.pair-label { width: 4.2em; flex: none; color: var(--muted); font-size: 15px; }
.pair input, .decline-row input { width: 5em; min-width: 0; flex: 1 1 5em; text-align: right; padding: 10px; }
.dash, .unit { color: var(--muted); font-size: 15px; flex: none; }
.decline { margin-top: 12px; }
.decline-head { margin-bottom: 6px; font-size: 14px; color: var(--muted); }
.decline-row .del { margin: 0 -10px 0 0; }
.add-point { margin-top: 2px; }
.preview { margin-top: 10px; transition: opacity 0.2s; }
.preview.stale { opacity: 0.35; }
.preview .plot .now-strip { height: 4px; }
.preview .grid { top: 4px; }
.actions { display: flex; gap: 8px; margin-top: 6px; }
.actions button { flex: 1; }

/* ---------- Footer / toast ---------- */
.foot {
  text-align: center;
  font-size: 13px;
  color: var(--faint);
  padding: 4px 8px 12px;
}
.foot p { margin: 0 0 4px; }
.linkish {
  border: 0;
  background: none;
  color: var(--faint);
  text-decoration: underline;
  font-size: 13px;
  min-height: 44px;
  padding: 0 12px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 14px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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