:root {
  --bg: #f4f4f6;
  --card: #ffffff;
  --text: #1d1d22;
  --muted: #6b6b75;
  --border: #dcdce2;
  --track: #ececf1;
  --tick: #c8c8d0;
  --tick-major: #85858f;
  --awake: #2f7fd6;
  --asleep: #8a5cc7;
  --sleep-fill: rgba(138, 92, 199, 0.2);
  --btn-active: #ececf1;
  --seg-on: #1d1d22;
  --seg-on-text: #ffffff;
  --ok-fill: rgba(47, 127, 214, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151518;
    --card: #202025;
    --text: #ececf1;
    --muted: #9a9aa5;
    --border: #34343c;
    --track: #2a2a31;
    --tick: #45454e;
    --tick-major: #8a8a95;
    --awake: #5aa2ec;
    --asleep: #b08ae6;
    --sleep-fill: rgba(176, 138, 230, 0.24);
    --btn-active: #2c2c33;
    --seg-on: #ececf1;
    --seg-on-text: #151518;
    --ok-fill: rgba(90, 162, 236, 0.16);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 28px 16px 40px;
}
main { width: 100%; max-width: 360px; display: flex; flex-direction: column; align-items: center; }
#tool { width: 100%; display: flex; flex-direction: column; align-items: center; }
h1 { font-size: 1.3rem; margin: 0 0 4px; text-align: center; }
p.sub { color: var(--muted); margin: 0 0 18px; text-align: center; font-size: 0.92rem; }

.field { width: 100%; margin-bottom: 14px; }
.field-label { font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.04em; }

.seg {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px;
  background: var(--card); border: 1px solid var(--border); border-radius: 9px; padding: 3px;
}
.seg button {
  border: 0; background: transparent; color: var(--text);
  padding: 7px 0; border-radius: 6px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
}
.seg .term { font-size: 0.95rem; font-weight: 600; }
.seg .plain, .seg .freq { font-size: 0.75rem; color: var(--muted); }
.seg .freq { font-size: 0.7rem; }
.seg button.on { background: var(--seg-on); color: var(--seg-on-text); }
.seg button.on .plain, .seg button.on .freq { color: var(--seg-on-text); opacity: 0.8; }

.sleep-row { display: flex; gap: 10px; }
.sleep-row label { flex: 1; display: flex; flex-direction: column; font-size: 0.85rem; color: var(--muted); gap: 4px; }
.sleep-row select {
  font: inherit; color: var(--text); background: var(--card);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 8px;
}

svg { user-select: none; -webkit-user-select: none; }
#timeline { width: 100%; display: block; margin-top: 8px; }
.tick { stroke: var(--tick); stroke-width: 1.5; }
.tick.major { stroke: var(--tick-major); stroke-width: 2; }
.num { font-size: 11px; fill: var(--muted); text-anchor: middle; dominant-baseline: middle; }
.num.major { fill: var(--text); font-weight: 600; }
.marker-label { font-size: 11px; fill: var(--text); text-anchor: middle; font-weight: 600; }
.sleep-label { font-size: 11px; fill: var(--asleep); text-anchor: middle; dominant-baseline: middle; font-weight: 600; }
.hit { stroke: transparent; fill: transparent; cursor: grab; touch-action: none; }
svg.dragging, svg.dragging .hit { cursor: grabbing; }

#controls { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
button.btn {
  height: 44px; padding: 0 16px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  font: inherit; font-size: 0.92rem; font-weight: 500; cursor: pointer;
}
button.btn:active { background: var(--btn-active); }
button.btn.primary { background: var(--seg-on); color: var(--seg-on-text); border-color: var(--seg-on); }
button.btn.primary:active { opacity: 0.85; }
.hint { font-size: 0.8rem; color: var(--muted); margin: 8px 0 0; text-align: center; }

#nudge {
  width: 100%; margin-top: 16px; padding: 10px 12px; border-radius: 10px;
  font-size: 0.88rem; line-height: 1.4;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
#nudge.warn { background: var(--sleep-fill); }
#nudge.ok { background: var(--ok-fill); }
#nudge button {
  flex-shrink: 0; border: 0; border-radius: 7px; padding: 7px 11px; cursor: pointer;
  font: inherit; font-size: 0.82rem; font-weight: 600;
  background: var(--asleep); color: #fff;
}

#legend { display: flex; flex-direction: column; gap: 8px; margin-top: 18px; width: 100%; }
.row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); border-radius: 10px; padding: 9px 14px;
  border: 1px solid var(--border);
}
.row .label { display: flex; align-items: center; gap: 10px; font-weight: 500; }
.swatch { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.row .time { font-variant-numeric: tabular-nums; }
.row .tag { font-size: 0.78rem; color: var(--asleep); margin-left: 8px; }

#actions { width: 100%; margin-top: 18px; }
#actions label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; color: var(--muted); }
#actions input {
  font: inherit; font-size: 1rem; color: var(--text); background: var(--card);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
.action-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }

article { width: 100%; margin-top: 36px; line-height: 1.55; font-size: 0.95rem; }
article h2 { font-size: 1.1rem; margin: 28px 0 8px; }
article h3 { font-size: 0.98rem; margin: 18px 0 4px; }
article p, article ol { margin: 0 0 10px; }
article ol { padding-left: 20px; }
article li { margin-bottom: 6px; }
article table { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 8px; }
article th, article td { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
article th { color: var(--muted); font-weight: 600; }
article td:first-child { font-weight: 600; white-space: nowrap; }

article a, .crumb a { color: var(--awake); }
.crumb { width: 100%; font-size: 0.82rem; color: var(--muted); margin: 0 0 12px; text-align: center; }
.links { list-style: none; padding-left: 0; }

footer { margin-top: 26px; font-size: 0.78rem; color: var(--muted); text-align: center; max-width: 340px; line-height: 1.4; }

/* Desktop: planner in a card on the left, explanations alongside. */
@media (min-width: 860px) {
  body { padding-top: 48px; }
  main {
    max-width: 980px; display: grid; grid-template-columns: 400px minmax(0, 1fr);
    column-gap: 56px; align-items: start;
  }
  #tool { background: var(--card); border: 1px solid var(--border); border-radius: 16px; padding: 24px 20px; }
  #tool .seg, #tool .row, #tool select, #tool input, #tool button.btn:not(.primary) { background: var(--bg); }
  article { margin-top: 0; font-size: 1rem; }
  article h2:first-child { margin-top: 4px; }
  footer { grid-column: 1 / -1; justify-self: center; }
}
