:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --text: #1c1c1e;
  --muted: #6b6b73;
  --border: #e3e3e8;
  --primary: #6d28d9;
  --on-primary: #ffffff;
  --danger: #dc2626;
  --good: #16a34a;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --glow: none;
  --radius: 16px;
  --font: "Manrope", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

/* Dark ("Midnight Indigo"): follows the phone's setting on "Auto", or forced from the menu */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #100f24;
    --card: #1a1934;
    --border: #2a2850;
    --text: #ecebfa;
    --muted: #9b97c4;
    --primary: #c084fc;
    --on-primary: #1a0f33;
    --danger: #fb7185;
    --good: #4ade80;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --glow: radial-gradient(ellipse 90% 45% at 10% -5%, rgba(168, 85, 247, 0.16), transparent 70%),
            radial-gradient(ellipse 70% 40% at 100% 0%, rgba(99, 102, 241, 0.12), transparent 70%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #100f24;
  --card: #1a1934;
  --border: #2a2850;
  --text: #ecebfa;
  --muted: #9b97c4;
  --primary: #c084fc;
  --on-primary: #1a0f33;
  --danger: #fb7185;
  --good: #4ade80;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --glow: radial-gradient(ellipse 90% 45% at 10% -5%, rgba(168, 85, 247, 0.16), transparent 70%),
          radial-gradient(ellipse 70% 40% at 100% 0%, rgba(99, 102, 241, 0.12), transparent 70%);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  background: var(--glow), var(--bg);
  background-attachment: fixed;
}

button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { letter-spacing: -0.02em; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.4rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.icon-btn {
  border: 0;
  background: none;
  font-size: 1.8rem;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  color: var(--primary);
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  padding: 10px 16px;
  cursor: pointer;
  font-weight: 600;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.danger { color: var(--danger); }
.btn.block { display: block; width: 100%; margin-bottom: 8px; text-align: left; }
.btn:active { transform: scale(0.98); }

/* ---------- Section cards (home) ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.section-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  margin-bottom: 12px;
  border-left: 6px solid var(--accent);
  cursor: pointer;
}
.section-card .emoji {
  font-size: 1.8rem;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  flex-shrink: 0;
}
.section-card .info { flex: 1; min-width: 0; }
.section-card .name {
  font-weight: 700;
  font-size: 1.1rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.section-card .meta { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.nowrap { white-space: nowrap; }
.section-card .live {
  color: var(--accent);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.timer-btn {
  flex-shrink: 0;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  box-shadow: 0 2px 8px color-mix(in srgb, var(--accent) 40%, transparent);
}
.timer-btn.running {
  background: var(--card);
  color: var(--accent);
  border: 3px solid var(--accent);
  animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
  50% { box-shadow: 0 0 0 8px color-mix(in srgb, var(--accent) 15%, transparent); }
}

.add-section {
  width: 100%;
  padding: 16px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: none;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
}
.empty .big { font-size: 3rem; }

/* ---------- Section detail ---------- */
.hero {
  padding: 20px;
  margin-bottom: 12px;
  text-align: center;
  border-top: 6px solid var(--accent);
}
.hero .clock {
  font-size: 2.6rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  margin: 4px 0 14px;
}
.hero .clock.idle { color: var(--muted); }
.hero .timer-btn { width: 96px; height: 96px; font-size: 1.1rem; }
.hero .since { color: var(--muted); font-size: 0.85rem; margin-top: 10px; min-height: 1.2em; }

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 12px;
}
.stat { padding: 12px 14px; }
.stat .label { color: var(--muted); font-size: 0.8rem; }
.stat .value { font-size: 1.3rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }

.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.toolbar .btn { flex: 1; }

.day-group { margin-bottom: 14px; }
.day-head {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 4px 6px;
}
.session {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.session:last-child { border-bottom: 0; }
.session .times { flex: 1; font-variant-numeric: tabular-nums; }
.session .note { color: var(--muted); font-size: 0.85rem; }
.session .dur { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Dialogs ---------- */
dialog {
  border: 0;
  border-radius: 20px;
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  background: var(--card);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 14px; font-size: 1.2rem; }

label, .field-label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 12px;
}
.field-label { margin-bottom: 6px; }
label input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 1rem;
}
.row { display: flex; gap: 10px; }
.row label { flex: 1; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0 0 12px; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
}
.swatch.selected { border-color: var(--text); }

.emoji-picks { display: flex; flex-wrap: wrap; gap: 4px; margin: -4px 0 14px; }
.emoji-picks button {
  border: 0;
  background: var(--bg);
  border-radius: 8px;
  font-size: 1.3rem;
  width: 38px;
  height: 38px;
  cursor: pointer;
}

.position { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.position .field-label { flex: 1; margin: 0; }
.btn.small { padding: 6px 14px; }
.btn:disabled { opacity: 0.35; cursor: default; }

.segmented {
  display: flex;
  background: var(--bg);
  border-radius: 12px;
  padding: 3px;
  margin-bottom: 14px;
}
.segmented button {
  flex: 1;
  border: 0;
  background: none;
  padding: 8px;
  border-radius: 9px;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
}
.segmented button.selected { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.home-summary { color: var(--muted); font-size: 0.9rem; margin: 0 4px 12px; }

.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.spacer { flex: 1; }

/* ---------- v3: goals, categories, nav ---------- */
a.btn { text-decoration: none; color: inherit; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }

.nav-row { display: flex; gap: 8px; margin-bottom: 12px; }
.nav-row .btn { flex: 1; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--primary); display: inline-block; }

.banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  margin-bottom: 12px;
  font-size: 0.9rem;
}
.banner span { flex: 1; }

.group-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 18px 4px 8px;
  font-weight: 700;
}
.group-head span:last-child { color: var(--muted); font-weight: 500; font-size: 0.85rem; }

.goal-line { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.goal-bar {
  height: 6px;
  border-radius: 3px;
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  margin-top: 4px;
  overflow: hidden;
}
.goal-bar div { height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.3s; }
.goal-card { padding: 12px 14px; margin-bottom: 12px; }
.goal-card .goal-line { margin-top: 0; font-size: 0.9rem; }
.goal-card .goal-bar { height: 10px; border-radius: 5px; margin-top: 8px; }

.mini-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--accent);
  font-size: 1rem;
  cursor: pointer;
}
.timer-btn.running.paused { animation: none; opacity: 0.75; }

.hero-actions { display: flex; gap: 8px; justify-content: center; margin-top: 14px; }

.sub-head { font-size: 0.95rem; margin: 18px 4px 8px; color: var(--muted); }

/* ---------- Charts ---------- */
.chart-card { padding: 14px 10px 10px; margin-bottom: 12px; }
.chart { display: flex; gap: 4px; align-items: stretch; height: 150px; }
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.col-value { font-size: 0.62rem; color: var(--muted); height: 14px; white-space: nowrap; }
.bar-track { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; }
.bar, .stack {
  width: 70%;
  max-width: 34px;
  min-height: 0;
  border-radius: 6px 6px 2px 2px;
  background: var(--accent, var(--primary));
}
.stack { display: flex; flex-direction: column-reverse; overflow: hidden; background: none; }
.col-label { font-size: 0.65rem; color: var(--muted); margin-top: 4px; white-space: nowrap; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 10px; font-size: 0.8rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.list-card { padding: 4px 14px; margin-bottom: 12px; }
.stat-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.stat-row:last-child { border-bottom: 0; }
.stat-row.head { font-size: 0.75rem; color: var(--muted); padding: 8px 0 6px; }
.stat-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-num { width: 78px; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.muted { color: var(--muted); }
.change { font-weight: 700; }
.change.up { color: var(--good); }
.change.down { color: var(--danger); }

/* ---------- Focus mode & stopwatch ---------- */
body.focus-mode .topbar h1 { text-align: center; }
.focus {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 12px 0 8px;
}
.focus-emoji { font-size: 3rem; margin-bottom: 8px; }
.ring-wrap { position: relative; width: min(78vw, 300px); aspect-ratio: 1; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 12; }
.ring-track { stroke: color-mix(in srgb, var(--accent) 15%, transparent); }
.ring-fill { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 0.25s linear; }
.focus-clock {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(2.2rem, 12vw, 3.4rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}
.focus-sub { color: var(--muted); margin: 14px 0 18px; min-height: 1.2em; }
.focus-actions { display: flex; gap: 10px; width: 100%; max-width: 360px; }
.focus-actions .btn { flex: 1; }
.btn.big { padding: 16px; font-size: 1.05rem; border-radius: 16px; }
.focus .hint { margin-top: 14px; }
.focus .btn.primary { background: var(--accent); border-color: var(--accent); }

.stopwatch-clock {
  font-size: clamp(3rem, 17vw, 4.6rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  margin: 30px 0 28px;
}
.save-card { padding: 14px; margin: 16px 0 12px; }
.save-card .row { align-items: center; }

/* ---------- Form bits ---------- */
select, .goal-row input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 1rem;
  color: var(--text);
  flex: 1;
  min-width: 0;
}
.goal-row { align-items: center; margin-bottom: 14px; }
.goal-row input { flex: 0 0 90px; }
.goal-row input:disabled { opacity: 0.4; }
.unit { color: var(--muted); font-size: 0.9rem; }

.swatch-list { display: contents; }
.swatch.custom {
  position: relative;
  margin: 0;
  background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red);
  overflow: hidden;
}
.swatch.custom input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; margin: 0; padding: 0; border: 0; }

.warn {
  background: color-mix(in srgb, #f59e0b 18%, transparent);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 0 0 14px;
  font-size: 0.9rem;
}
.warn[hidden] { display: none; }

.presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.presets .btn { padding: 10px 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 18px;
  font-weight: 600;
  font-size: 0.9rem;
  z-index: 20;
  width: max-content;
  max-width: calc(100vw - 32px);
  display: flex;
  align-items: center;
  gap: 14px;
}
.toast button { flex-shrink: 0; }
.toast[hidden] { display: none; }
.toast button {
  border: 0;
  background: none;
  color: inherit;
  font-weight: 800;
  text-decoration: underline;
  padding: 0;
  cursor: pointer;
}

/* ---------- v4: goal days ---------- */
.goal-sub { font-size: 0.75rem; color: var(--muted); margin-top: 8px; }
#goalDaysRow { margin: -4px 0 14px; }
#goalDaysText { font-weight: 500; }
.day-picks { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.day-picks button, .day-presets button {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 8px 0;
  font-weight: 700;
  cursor: pointer;
}
.day-picks button.selected { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.day-presets { display: flex; gap: 6px; margin-top: 6px; }
.day-presets button { flex: 1; font-weight: 600; font-size: 0.8rem; color: var(--muted); }

/* ---------- v4: calendar heatmap ---------- */
.heatmap {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: 12px repeat(7, auto);
  gap: 2px;
}
.hm-label, .hm-month { font-size: 0.6rem; color: var(--muted); line-height: 1; white-space: nowrap; }
.hm-label { display: flex; align-items: center; }
.hm-month { overflow: visible; }
.hm-cell { display: block; aspect-ratio: 1; border-radius: 2px; background: color-mix(in srgb, var(--muted) 14%, transparent); cursor: pointer; }
.hm-cell.l1 { background: color-mix(in srgb, var(--accent) 30%, transparent); }
.hm-cell.l2 { background: color-mix(in srgb, var(--accent) 55%, transparent); }
.hm-cell.l3 { background: color-mix(in srgb, var(--accent) 78%, transparent); }
.hm-cell.l4 { background: var(--accent); }
.hm-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; font-size: 0.75rem; color: var(--muted); }
.hm-scale { display: inline-flex; align-items: center; gap: 2px; }
.hm-scale .hm-cell { width: 10px; cursor: default; }

/* ---------- v4: session search ---------- */
.filter-card { padding: 12px; margin-bottom: 14px; }
.filter-card input[type="search"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 1rem;
  margin-bottom: 10px;
}
.filter-card .row { margin-bottom: 10px; }
.filter-card label { margin-bottom: 0; min-width: 0; }
.filter-card label input { min-width: 0; max-width: 100%; }
.filter-card select { padding: 8px 10px; }
.filter-bottom { align-items: flex-end; margin-bottom: 0 !important; }
.results-card .session { border-left: 4px solid var(--accent); }
.result-title { font-weight: 600; }
.result-when { color: var(--muted); font-size: 0.85rem; }
.center { text-align: center; }

/* ---------- v4: home nav, weekly review ---------- */
.nav-row .nav-btn { flex-direction: column; gap: 2px; padding: 8px 4px; font-size: 0.75rem; position: relative; }
.nav-btn span { font-size: 1.2rem; }
.nav-btn .dot { position: absolute; top: 6px; right: 8px; }
.week-nav { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.week-nav .btn { font-size: 1.2rem; padding: 4px 14px; }
.week-title { flex: 1; text-align: center; font-weight: 700; }
.week-title .hint { margin: 2px 0 0; font-weight: 500; }
.invisible { visibility: hidden; }
.stat-num.good { color: var(--good); }
.stat-name .hint { margin: 0; white-space: normal; }
