:root {
  --bg: #f6f8f7;
  --surface: #ffffff;
  --surface-2: #eef2f0;
  --ink: #16211e;
  --ink-soft: #4d5c58;
  --ink-faint: #7b8985;
  --rule: #dfe6e3;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --danger: #b42318;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --c-teal: #0f766e;
  --c-blue: #2563eb;
  --c-violet: #7c3aed;
  --c-rose: #e11d48;
  --c-amber: #d97706;
  --c-green: #15803d;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1413;
    --surface: #161e1c;
    --surface-2: #1e2826;
    --ink: #e7eeec;
    --ink-soft: #a8b6b2;
    --ink-faint: #7d8b87;
    --rule: #26312e;
    --accent: #2dd4bf;
    --accent-ink: #062622;
    --danger: #f97066;

    --c-teal: #2dd4bf;
    --c-blue: #60a5fa;
    --c-violet: #a78bfa;
    --c-rose: #fb7185;
    --c-amber: #fbbf24;
    --c-green: #4ade80;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }

.app { max-width: 560px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; }
main {
  flex: 1;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
  outline: none;
}

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 20px; }
.top h1 { font-size: 26px; line-height: 1.15; margin: 0; letter-spacing: -0.01em; }
.top .sub { margin: 2px 0 0; color: var(--ink-soft); font-size: 15px; }
.back { display: inline-flex; align-items: center; gap: 4px; text-decoration: none; font-size: 15px; margin-bottom: 8px; }

.date-nav { display: flex; align-items: center; gap: 4px; }
.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 1px solid var(--rule); border-radius: 50%;
  background: var(--surface); cursor: pointer;
}
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.today-link { font-size: 14px; padding: 0 8px; background: none; border: 0; color: var(--accent); cursor: pointer; }

/* Progress */
.progress { display: flex; align-items: center; gap: 16px; padding: 16px; background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius); margin-bottom: 16px; }
.ring { width: 64px; height: 64px; flex: none; }
.ring circle { fill: none; stroke-width: 7; }
.ring .track { stroke: var(--surface-2); }
.ring .fill { stroke: var(--accent); stroke-linecap: round; transform: rotate(-90deg); transform-origin: center; transition: stroke-dashoffset .4s ease; }
.progress strong { display: block; font-size: 18px; }
.progress span { color: var(--ink-soft); font-size: 15px; }

/* Habit rows (Today) */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.row {
  --c: var(--c-teal);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 12px 12px 12px 14px;
}
.row-main { display: flex; align-items: center; gap: 12px; }
.row .emoji { font-size: 24px; width: 32px; text-align: center; flex: none; }
.row .info { flex: 1; min-width: 0; }
.row .name { font-weight: 600; margin: 0; overflow-wrap: anywhere; }
.row .meta { margin: 0; font-size: 14px; color: var(--ink-faint); }
.row .meta .flame { color: var(--c); font-weight: 600; }
.check {
  width: 48px; height: 48px; flex: none;
  border-radius: 50%;
  border: 2px solid var(--c);
  background: transparent;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background .15s, transform .1s;
}
.check:active { transform: scale(0.94); }
.check svg { width: 24px; height: 24px; fill: none; stroke: var(--c); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: 0; }
.check[aria-pressed="true"] { background: var(--c); }
.check[aria-pressed="true"] svg { stroke: var(--surface); opacity: 1; }
.row.done .name { color: var(--ink-soft); }

.note-toggle { background: none; border: 0; padding: 4px 0 0; font-size: 14px; color: var(--ink-faint); cursor: pointer; }
.note-toggle:hover { color: var(--accent); }
.note-box { margin-top: 8px; }
.note-box textarea {
  width: 100%; min-height: 64px; resize: vertical;
  border: 1px solid var(--rule); border-radius: 10px;
  background: var(--bg); padding: 8px 10px; font: inherit; color: inherit;
}

/* Habits list */
.habit-link {
  --c: var(--c-teal);
  display: flex; align-items: center; gap: 12px;
  padding: 14px; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius);
}
.habit-link:hover { border-color: var(--c); }
.habit-link .bar { width: 4px; align-self: stretch; border-radius: 2px; background: var(--c); }
.habit-link .chev { color: var(--ink-faint); }
.section-title { font-size: 14px; font-weight: 600; color: var(--ink-faint); margin: 28px 0 10px; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius); padding: 14px; }
.stat b { display: block; font-size: 26px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { font-size: 14px; color: var(--ink-soft); }

.card { background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.card h2 { font-size: 16px; margin: 0 0 12px; }

/* Heatmap */
.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 3px; overflow-x: auto; }
.heatmap i { width: 100%; aspect-ratio: 1; min-width: 12px; border-radius: 3px; background: var(--surface-2); }
.heatmap i.done { background: var(--c); }
.heatmap i.missed { background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--rule); }
.heatmap i.off { background: var(--surface-2); opacity: 0.45; }
.heatmap i.future { background: transparent; box-shadow: inset 0 0 0 1px var(--surface-2); }
.legend { display: flex; gap: 14px; margin-top: 10px; font-size: 13px; color: var(--ink-faint); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

.notes { list-style: none; margin: 0; padding: 0; }
.notes li { padding: 10px 0; border-top: 1px solid var(--rule); }
.notes li:first-child { border-top: 0; padding-top: 0; }
.notes time { display: block; font-size: 13px; color: var(--ink-faint); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px;
  border-radius: 999px; border: 1px solid var(--rule);
  background: var(--surface); cursor: pointer; font-weight: 600; font-size: 15px;
  text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; }

.fab {
  position: fixed;
  right: max(16px, calc(50% - 280px + 16px));
  bottom: calc(84px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 18px;
  background: var(--accent); color: var(--accent-ink);
  border: 0; cursor: pointer;
  display: grid; place-items: center;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.18);
}
.fab svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }

/* Empty state */
.empty { text-align: center; padding: 48px 16px; color: var(--ink-soft); }
.empty .big { font-size: 44px; }
.empty h2 { color: var(--ink); font-size: 20px; margin: 8px 0 6px; }
.empty p { margin: 0 0 20px; }

/* Tab bar */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: center;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--rule);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  flex: 1; max-width: 186px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px 0 8px;
  color: var(--ink-faint); text-decoration: none; font-size: 12px; font-weight: 500;
}
.tabs a[aria-current="page"] { color: var(--accent); }
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Dialog */
dialog {
  width: min(520px, calc(100% - 24px));
  max-height: calc(100dvh - 24px);
  border: 0; border-radius: 20px; padding: 0;
  background: var(--surface); color: var(--ink);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog form { padding: 20px; }
dialog h2 { margin: 0 0 16px; font-size: 20px; }
.field { display: block; margin: 0 0 16px; padding: 0; border: 0; min-width: 0; }
.field > span, .field > legend { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; padding: 0; }
.field small { font-weight: 400; color: var(--ink-faint); }
.field input[name] {
  width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--rule); border-radius: 10px; background: var(--bg);
}
.hint { display: block; margin-top: 6px; font-size: 13px; }
.field-row { display: grid; grid-template-columns: 88px 1fr; gap: 12px; }
.emoji-field input { text-align: center; font-size: 20px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatches label { position: relative; }
.swatches input { position: absolute; opacity: 0; inset: 0; }
.swatches span {
  display: block; width: 36px; height: 36px; border-radius: 50%;
  background: var(--c); cursor: pointer;
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 2px transparent;
}
.swatches input:checked + span { box-shadow: 0 0 0 3px var(--surface), 0 0 0 5px var(--c); }
.swatches input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 5px; }
.days { display: flex; gap: 6px; }
.days label { flex: 1; }
.days input { position: absolute; opacity: 0; pointer-events: none; }
.days span {
  display: grid; place-items: center; height: 40px;
  border-radius: 10px; border: 1px solid var(--rule);
  font-size: 13px; font-weight: 600; cursor: pointer; user-select: none;
}
.days input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.days input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.form-error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* Settings */
.setting p { margin: 0 0 12px; color: var(--ink-soft); font-size: 15px; }
.setting .actions { margin-top: 4px; }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(88px + env(safe-area-inset-bottom));
  background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 999px; font-size: 14px;
  max-width: calc(100% - 32px);
  display: flex; align-items: center; gap: 12px;
}
.toast button { background: none; border: 0; color: inherit; font-weight: 700; text-decoration: underline; cursor: pointer; padding: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
