:root {
  --bg: #0a0a0f;
  --card: #15151d;
  --text: #f2f2f7;
  --muted: #9a9aa8;
  --accent: #7c5cff;
  --night-accent: #b89bff;
  --night-btn: #5a49c8;
  --danger: #ff453a;
  --radius: 16px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.title { font-size: 22px; font-weight: 700; text-align: center; margin: 8px 0 0; }

.status { text-align: center; }
.status-line { font-size: 18px; color: var(--muted); margin: 0; }
.status.sleeping .status-line { color: var(--night-accent); }

.primary {
  width: 100%;
  padding: 28px;
  font-size: 22px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.2s ease, opacity 0.2s ease;
}
.primary:active { transform: scale(0.98); }
.primary:disabled { opacity: 0.5; cursor: default; }
.primary.sleeping { background: var(--night-btn); }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 10px; }
.last-duration { font-size: 26px; font-weight: 700; margin: 0; }
.last-times { color: var(--muted); margin: 4px 0 0; font-size: 14px; }
.stats { margin: 0 0 8px; font-size: 15px; }

.history { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.history li { display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); }
.history li .dur { color: var(--text); font-weight: 600; }

.actions { display: flex; gap: 12px; }
.secondary {
  flex: 1;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  background: var(--card);
  border: 1px solid #2a2a36;
  border-radius: 12px;
  cursor: pointer;
}
.secondary:active { background: #1e1e28; }
.secondary:disabled { opacity: 0.4; cursor: default; }

dialog {
  border: none;
  border-radius: var(--radius);
  background: var(--card);
  color: var(--text);
  padding: 0;
  width: min(90vw, 360px);
}
dialog::backdrop { background: rgba(0,0,0,0.6); }
.dialog-form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog-form h2 { margin: 0; font-size: 18px; }
.dialog-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.dialog-form input, .dialog-form textarea {
  background: #0d0d14; color: var(--text); border: 1px solid #2a2a36;
  border-radius: 10px; padding: 10px; font-size: 16px; font-family: inherit;
}
.rating { display: flex; gap: 8px; justify-content: space-between; }
.rating button {
  flex: 1; padding: 12px 0; font-size: 16px; border-radius: 10px;
  border: 1px solid #2a2a36; background: #0d0d14; color: var(--text); cursor: pointer;
}
.rating button.selected { background: var(--accent); border-color: var(--accent); color: #fff; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; padding: 0; margin: 0; }
.dialog-actions button {
  padding: 10px 18px; font-size: 15px; font-weight: 600; border-radius: 10px;
  border: none; background: var(--accent); color: #fff; cursor: pointer;
}
.dialog-actions button#editCancel { background: #2a2a36; }
.error { color: var(--danger); font-size: 13px; margin: 0; }

.toast {
  position: fixed; left: 50%; bottom: 32px; transform: translateX(-50%);
  background: #2a2a36; color: var(--text); padding: 12px 18px; border-radius: 12px;
  font-size: 14px; max-width: 90vw; text-align: center; z-index: 10;
}

/* ---- Night view ---- */
.night {
  position: relative;
  height: 300px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #07070c;
  display: flex;
  align-items: center;
  justify-content: center;
}
.night .liquid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.night-readout { position: relative; text-align: center; z-index: 1; }
.clock {
  font-size: 64px;
  font-weight: 200;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  margin: 0;
  text-shadow: 0 2px 24px rgba(0,0,0,0.6);
}
.countdown {
  font-size: 18px;
  font-weight: 600;
  margin: 6px 0 0;
  color: var(--text);
  text-shadow: 0 1px 12px rgba(0,0,0,0.6);
}
.alarm {
  font-size: 13px;
  color: var(--muted);
  margin: 8px 0 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ---- Sleep score ---- */
.last-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.score { display: flex; flex-direction: column; align-items: center; min-width: 72px; }
.score-num { font-size: 34px; font-weight: 700; line-height: 1; color: var(--accent); font-variant-numeric: tabular-nums; }
.score-band { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-top: 2px; }
.history li .score-mini { color: var(--accent); font-weight: 600; margin-left: 10px; }

/* ---- Sleep report ---- */
.report { display: flex; gap: 10px; margin: 14px 0 0; padding: 0; }
.report-item { flex: 1; }
.report dt { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.report dd { margin: 4px 0 0; font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }

.awake-bar { display: flex; height: 10px; border-radius: 6px; overflow: hidden; margin-top: 14px; background: #2a2a36; }
.awake-bar-asleep { background: var(--accent); }
.awake-bar-awake { background: #3a3a48; }
.awake-legend { font-size: 11px; color: var(--muted); margin: 6px 0 0; text-transform: uppercase; letter-spacing: 0.06em; }

/* ---------- v4: tabs + trends ---------- */
.tabs { display: flex; gap: 8px; justify-content: center; margin: 4px 0 16px; }
.tab { flex: 1; padding: 11px; border-radius: 12px; background: var(--card); color: var(--muted); border: none; font: inherit; font-weight: 600; cursor: pointer; }
.tab.active { background: var(--accent); color: #fff; }
.view[hidden] { display: none; }

.range-toggle { display: flex; gap: 6px; justify-content: flex-end; margin-bottom: 12px; }
.range { padding: 6px 14px; border-radius: 9px; background: var(--card); color: var(--muted); border: none; font: inherit; cursor: pointer; }
.range.active { background: var(--accent); color: #fff; }

.goal-row { display: flex; justify-content: space-between; align-items: center; }
.link { background: none; border: none; color: var(--accent); font-size: 1.15rem; cursor: pointer; padding: 0 4px; }
.streak { margin: 10px 0 0; font-size: 1.15rem; font-weight: 600; }
.summary { line-height: 1.55; margin: 0; }

.bars { display: flex; align-items: flex-end; gap: 4px; height: 120px; position: relative; padding-top: 4px; }
.bars .bar { flex: 1; background: var(--accent); border-radius: 5px 5px 0 0; min-height: 2px; opacity: 0.4; }
.bars .bar.hit { opacity: 1; }
.bars .goal-line { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--night-accent); pointer-events: none; }

.drift svg { width: 100%; height: 140px; display: block; }
.drift .dot-bed { fill: var(--night-accent); }
.drift .dot-wake { fill: var(--accent); }
.drift .link-seg { stroke: #3a3a48; stroke-width: 1.5; }
.drift:empty::after, .drift > :not(svg) { color: var(--muted); font-size: 14px; }

/* ---------- v5: badges, shelf, brain dump ---------- */
.rating { flex-wrap: wrap; }
.rating button { flex: 1 1 30%; }

.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.badge-tag { display: inline-flex; align-items: center; gap: 5px; padding: 5px 10px; border-radius: 999px; background: #221c3a; color: var(--text); font-size: 13px; }
.hist-badges { margin-left: 6px; }

.shelf { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.shelf li { display: flex; align-items: center; gap: 10px; font-size: 15px; color: var(--muted); }
.shelf li.earned { color: var(--text); }
.shelf li .mark { margin-left: auto; }
.shelf li.earned .mark { color: var(--accent); }

.debt { margin: 8px 0 0; color: var(--muted); }
.brain-btn { width: 100%; margin-top: 10px; }
.brain-reveal { white-space: pre-wrap; line-height: 1.5; margin: 0 0 12px; }
.dump-history { margin-top: 6px; text-align: left; max-height: 38vh; overflow-y: auto; }
.dump-history-title { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 10px 0 4px; }
.dump-item { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 8px 0; border-top: 1px solid #2a2a36; }
.dump-date { grid-column: 1; color: var(--muted); font-size: 12px; margin: 0; }
.dump-body { grid-column: 1; white-space: pre-wrap; line-height: 1.4; margin: 0; }
.dump-del { grid-column: 2; grid-row: 1 / span 2; background: none; border: none; color: var(--muted); font-size: 16px; cursor: pointer; align-self: start; padding: 0 4px; }
