:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --ink: #1d1d1b;
  --muted: #84837d;
  --faint: #bdbcb5;
  --line: #e6e4de;
  --accent: #23a061;
  --accent-ink: #ffffff;
  --danger: #c8483f;
  --r: 14px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111110;
    --card: #1a1a19;
    --ink: #efeee9;
    --muted: #8a8984;
    --faint: #55544f;
    --line: #2a2a28;
    --accent: #38c27a;
    --accent-ink: #0c1a12;
    --danger: #e0675e;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 20px calc(32px + env(safe-area-inset-bottom));
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.top { display: flex; justify-content: space-between; align-items: center; height: 40px; }
.brand { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.link { color: var(--muted); font-size: 15px; padding: 8px 0; }
.link:hover { color: var(--ink); }
main { flex: 1; }

h1 { font-size: 34px; line-height: 1.1; letter-spacing: -0.03em; font-weight: 700; margin: 0; }
.sub { margin: 4px 0 0; color: var(--muted); }

/* ---------- Today ---------- */

.day-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin: 24px 0 20px; }

.ring { position: relative; width: 64px; height: 64px; flex: none; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 5; }
.ring .track { stroke: var(--line); }
.ring .bar {
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
  transition: stroke-dashoffset 0.6s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s;
}
.ring-label {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  font-size: 15px; font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.ring-label small { color: var(--muted); font-weight: 500; font-size: 13px; }
.ring-label svg { width: 26px; height: 26px; transform: none; fill: none; stroke: var(--accent); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.ring.celebrate { animation: ringpop 0.7s cubic-bezier(0.2, 0.8, 0.2, 1); }

.week { display: grid; grid-template-columns: 24px repeat(7, 1fr) 24px; align-items: center; margin-bottom: 28px; }
.arrow { color: var(--faint); font-size: 22px; height: 60px; }
.arrow:hover { color: var(--ink); }
.wd { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 4px 0; }
.wd .l { font-size: 11px; color: var(--muted); font-weight: 500; }
.wd .n {
  width: 32px; height: 32px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 15px; font-variant-numeric: tabular-nums;
  transition: background 0.2s, color 0.2s;
}
.wd.today .n { color: var(--accent); font-weight: 700; }
.wd.sel .n { background: var(--ink); color: var(--bg); font-weight: 600; }
.wd:hover:not(.sel) .n { background: var(--line); }
.dot { width: 6px; height: 6px; border-radius: 50%; }
.dot.full { background: var(--accent); }
.dot.part { box-shadow: inset 0 0 0 1.5px var(--accent); }
.dot.missed, .dot.todo { background: var(--line); }

.group { margin-bottom: 24px; }
.group h2 {
  font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--muted); margin: 0 0 8px 4px;
}
.group ul {
  list-style: none; margin: 0; padding: 0;
  background: var(--card); border-radius: var(--r); overflow: hidden;
  box-shadow: 0 0 0 1px var(--line);
}
.group li + li { border-top: 1px solid var(--line); }

.item { width: 100%; display: flex; align-items: center; gap: 14px; padding: 16px; text-align: left; transition: background 0.15s; }
.item:hover:not(:disabled) { background: color-mix(in srgb, var(--ink) 3%, transparent); }
.item:active:not(:disabled) .check { transform: scale(0.9); }
.item:disabled { cursor: default; }

.check {
  flex: none; width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--faint);
  display: grid; place-items: center;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
}
.check svg { width: 18px; height: 18px; fill: none; stroke: var(--accent-ink); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.check path { stroke-dasharray: 20; stroke-dashoffset: 20; transition: stroke-dashoffset 0.25s 0.08s ease-out; }
.item.done .check { background: var(--accent); border-color: var(--accent); }
.item.done .check path { stroke-dashoffset: 0; }
.item.just .check { animation: pop 0.4s cubic-bezier(0.2, 0.8, 0.2, 1); }
.future .check { border-style: dashed; }

.body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.name {
  align-self: flex-start;
  font-weight: 500;
  background: linear-gradient(currentColor, currentColor) no-repeat 0 55% / 0% 1.5px;
  transition: background-size 0.35s ease, color 0.3s;
}
.item.done .name { color: var(--muted); background-size: 100% 1.5px; }
.notes { font-size: 13px; color: var(--muted); margin-top: 2px; }
.target { flex: none; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; transition: color 0.3s; }
.item.done .target { color: var(--faint); }

.banner {
  text-align: center; padding: 8px 16px 24px; color: var(--muted);
  opacity: 0; transform: translateY(6px);
  transition: opacity 0.4s, transform 0.4s;
}
.banner.show { opacity: 1; transform: none; }
.banner strong { display: block; color: var(--ink); font-size: 18px; margin-bottom: 2px; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty strong { display: block; color: var(--ink); font-size: 18px; margin-bottom: 4px; }
.empty .btn { margin-top: 16px; }

.foot { display: flex; justify-content: center; gap: 20px; color: var(--muted); font-size: 14px; padding-top: 24px; }
.foot b { color: var(--ink); font-variant-numeric: tabular-nums; }
.foot:empty { display: none; }

/* ---------- Routines ---------- */

.edit-head { margin: 24px 0 20px; }
.routines { display: flex; flex-direction: column; gap: 10px; }

.card {
  width: 100%; text-align: left;
  background: var(--card); border-radius: var(--r); box-shadow: 0 0 0 1px var(--line);
  padding: 16px; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  transition: box-shadow 0.15s;
}
.card:hover { box-shadow: 0 0 0 1px var(--faint); }
.card .rc-name, .card .meta { display: block; }
.card .rc-name { font-weight: 600; }
.card .meta { color: var(--muted); font-size: 14px; margin-top: 2px; }
.card .chev { color: var(--faint); font-size: 20px; }

.add-routine {
  width: 100%; margin-top: 10px; padding: 14px;
  border: 1.5px dashed var(--line); border-radius: var(--r); color: var(--muted);
}
.add-routine:hover { color: var(--ink); border-color: var(--faint); }

.editor {
  background: var(--card); border-radius: var(--r);
  box-shadow: 0 0 0 1px var(--line), 0 8px 24px -12px rgba(0, 0, 0, 0.18);
  padding: 16px; display: flex; flex-direction: column; gap: 14px;
}
input, textarea {
  width: 100%; background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  outline: none; transition: border-color 0.15s;
}
input:focus, textarea:focus { border-color: var(--accent); }
input::placeholder, textarea::placeholder { color: var(--faint); }
.ed-name { font-size: 18px; font-weight: 600; background: none; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; padding: 4px 0 8px; }
.ed-name:focus-visible { outline: none; }
.ed-days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.day-pill {
  padding: 8px 0; border-radius: 999px; font-size: 13px;
  background: var(--bg); color: var(--muted); box-shadow: inset 0 0 0 1px var(--line);
  transition: background 0.15s, color 0.15s;
}
.day-pill.on { background: var(--ink); color: var(--bg); box-shadow: none; }
.ed-label { font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin-bottom: -6px; }
.ed-ex { display: flex; flex-direction: column; gap: 12px; }
.ex-row { display: grid; grid-template-columns: 1fr 6.5rem 32px; gap: 6px; }
.ex-row .ex-notes { grid-column: 1 / 3; font-size: 13px; padding: 6px 12px; }
.add-ex { align-self: flex-start; }
.icon { color: var(--faint); font-size: 20px; border-radius: 8px; }
.icon:hover { color: var(--danger); }
.ed-actions { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; }

.btn { padding: 10px 16px; border-radius: 10px; box-shadow: inset 0 0 0 1px var(--line); font-weight: 500; }
.btn:hover { box-shadow: inset 0 0 0 1px var(--faint); }
.btn.primary { background: var(--ink); color: var(--bg); box-shadow: none; }
.btn.primary:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.4; cursor: default; }
.danger { color: var(--danger); }

.coach { margin-top: 40px; padding-top: 24px; border-top: 1px solid var(--line); }
.coach h2 { font-size: 20px; letter-spacing: -0.02em; margin: 0 0 4px; }
.steps { list-style: none; padding: 0; margin: 20px 0 0; counter-reset: s; }
.steps > li { counter-increment: s; position: relative; padding-left: 36px; margin-bottom: 22px; }
.steps > li::before {
  content: counter(s);
  position: absolute; left: 0; top: 0;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--line); display: grid; place-items: center;
  font-size: 13px; font-weight: 600;
}
.steps p { margin: 0 0 10px; }
textarea { min-height: 120px; resize: vertical; font: 13px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; }
.preview { font-size: 14px; color: var(--muted); margin-top: 10px; }
.preview ul { margin: 6px 0 0; padding-left: 18px; }
.preview .err { color: var(--danger); }
.mode { display: flex; flex-wrap: wrap; gap: 16px; font-size: 14px; margin: 12px 0; }
.mode label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.mode input { width: auto; accent-color: var(--accent); }

details { margin-top: 8px; color: var(--muted); font-size: 14px; }
summary { cursor: pointer; color: var(--ink); }
details p { margin: 10px 0; }
code {
  font: 13px ui-monospace, "Cascadia Mono", Consolas, monospace;
  background: var(--card); box-shadow: 0 0 0 1px var(--line);
  padding: 2px 6px; border-radius: 6px; word-break: break-all;
}

.key-form { display: flex; flex-direction: column; gap: 12px; margin-top: 64px; }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 16px);
  background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 40px); z-index: 20;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; }

@keyframes pop {
  0% { transform: scale(1); }
  35% { transform: scale(0.82); }
  70% { transform: scale(1.14); }
  100% { transform: scale(1); }
}
@keyframes ringpop {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

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