/* How many sleeps? Light and dark mode via CSS variables. */

:root {
  --bg: #eef3ff;
  --bg-2: #dbe6ff;
  --surface: #ffffff;
  --ink: #232a4d;
  --ink-soft: #666e94;
  --line: #d5dcf2;
  --go: #20a868;
  --go-ink: #fff;
  --accent: #5c7cfa;
  --moon: #f59f00;
  --moon-dim: #c9d1ee;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151a3a;
    --bg-2: #1f2656;
    --surface: #242c63;
    --ink: #f3f5ff;
    --ink-soft: #aab3e0;
    --line: #36407f;
    --go: #2fcf83;
    --go-ink: #08281a;
    --accent: #91a7ff;
    --moon: #ffe066;
    --moon-dim: #3a4580;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0; min-height: 100%;
  background: var(--bg); color: var(--ink); font-family: var(--font);
  -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;
}
body { background: radial-gradient(120% 70% at 50% 0%, var(--bg-2), var(--bg) 70%) fixed; }

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.app {
  min-height: 100vh; min-height: 100dvh;
  max-width: 640px; margin: 0 auto;
  display: grid; grid-template-rows: auto auto minmax(220px, 1fr) auto; gap: 10px;
  padding:
    max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 52px; }
.brand { margin: 0; font-size: 1.3rem; font-weight: 900; }
.top-actions { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  width: 52px; height: 52px; border-radius: 50%; flex: none;
  border: 2px solid var(--line); background: var(--surface); box-shadow: 0 2px 0 var(--line);
  display: grid; place-items: center;
}
.icon-btn svg { width: 26px; height: 26px; fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; }
.pill-btn {
  min-height: 52px; padding: 0 20px 0 14px; border-radius: 999px;
  border: 2px solid var(--line); background: var(--surface); box-shadow: 0 2px 0 var(--line);
  font-weight: 800; font-size: 1rem; display: inline-flex; align-items: center; gap: 6px;
}
.pill-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.8; stroke-linecap: round; }

/* ---------- Welcome ---------- */
.welcome { grid-row: 2 / -1; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.hero { display: flex; justify-content: center; margin-top: 6px; }
.hero svg { width: min(30vw, 120px); height: auto; margin: 0 -6px; }
.hero svg:nth-child(2) { transform: translateY(-10px) scale(1.15); }
.lead { margin: 0; font-size: 1.4rem; font-weight: 800; text-align: center; }
.fine { color: var(--ink-soft); font-size: 0.9rem; text-align: center; margin: 4px 0 0; }

.templates { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; }
.tpl {
  border: 2px solid var(--line); border-radius: 22px; background: var(--surface); box-shadow: 0 4px 0 var(--line);
  padding: 10px 4px 8px; display: grid; justify-items: center; gap: 4px; font-weight: 800; font-size: 0.95rem;
}
.tpl svg { width: 64%; height: auto; }
.tpl:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.templates.small { gap: 8px; margin-bottom: 14px; }
.templates.small .tpl { padding: 6px 2px 5px; font-size: 0.8rem; border-radius: 16px; box-shadow: 0 3px 0 var(--line); }
.templates.small .tpl svg { width: 46%; }

/* ---------- Chips ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding: 2px 2px 6px; scroll-snap-type: x proximity; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; scroll-snap-align: start; min-height: 56px; max-width: 220px;
  display: flex; align-items: center; gap: 8px; padding: 4px 14px 4px 8px;
  border: 2px solid var(--line); border-radius: 999px; background: var(--surface); font-weight: 800;
}
.chip svg { width: 36px; height: 36px; flex: none; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-n { min-width: 28px; text-align: center; padding: 2px 9px; border-radius: 999px; background: var(--bg-2); font-size: 0.95rem; }
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip[aria-pressed="true"] .chip-n { background: var(--accent); color: #fff; }

/* ---------- Big picture ---------- */
.stage { position: relative; min-height: 0; }
.stage svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 8px 14px rgba(30, 30, 80, 0.16)); }
.stage.party svg { animation: hop 0.8s ease-in-out infinite alternate; }
@keyframes hop { to { transform: translateY(-12px) rotate(1.5deg); } }

/* ---------- Count ---------- */
.panel { display: grid; justify-items: center; gap: 4px; text-align: center; }
.count { font-size: clamp(4.5rem, 24vw, 8rem); font-weight: 900; line-height: 0.95; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
[data-state="today"] .count, [data-state="past"] .count { font-size: clamp(3rem, 15vw, 5.2rem); color: var(--accent); }
.unit { font-size: 1.7rem; font-weight: 800; }
.when { margin: 4px 0 6px; color: var(--ink-soft); font-weight: 700; font-size: 1.05rem; max-width: 30ch; }
.moons { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px 4px; max-width: 420px; min-height: 0; }
.moon { width: 26px; height: 26px; display: block; }
.moon svg { width: 100%; height: 100%; fill: var(--moon); display: block; }
.moon.next svg { animation: glow 1.8s ease-in-out infinite alternate; filter: drop-shadow(0 0 6px var(--moon)); }
@keyframes glow { to { transform: scale(1.18) rotate(-8deg); } }

.controls { margin-top: 8px; }
.btn { min-height: 60px; border-radius: 999px; border: 0; font-size: 1.2rem; font-weight: 900; padding: 0 26px; }
.btn:active { transform: translateY(3px); }
.btn-go { background: var(--go); color: var(--go-ink); box-shadow: 0 5px 0 color-mix(in srgb, var(--go) 70%, #000); }
.btn-ghost { background: var(--surface); border: 2px solid var(--line); box-shadow: 0 5px 0 var(--line); min-width: 140px; }

@media (orientation: landscape) and (min-width: 700px) {
  .app { max-width: 1000px; grid-template-columns: 1.1fr 1fr; grid-template-rows: auto auto minmax(0, 1fr); column-gap: 28px; }
  .topbar { grid-column: 1 / -1; }
  .chips { grid-column: 1 / -1; }
  .stage { grid-row: 3; grid-column: 1; }
  .panel { grid-row: 3; grid-column: 2; align-content: center; }
  .welcome { grid-column: 1 / -1; }
}

/* ---------- Editor ---------- */
.sheet { border: 0; border-radius: 28px; padding: 22px; width: min(94vw, 440px); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35); }
.sheet::backdrop { background: rgba(15, 18, 50, 0.6); }
.sheet-tall { max-height: 90dvh; overflow-y: auto; overscroll-behavior: contain; }
.sheet h2 { margin: 0 0 14px; text-align: center; font-size: 1.5rem; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; margin-top: 16px; }
.sheet-actions .btn { min-height: 58px; font-size: 1.2rem; }

.field { display: grid; gap: 4px; margin: 0 0 12px; padding: 0; border: 0; font-weight: 800; }
.field input[type="text"], .field input[type="date"] {
  font: inherit; font-weight: 700; color: var(--ink); background: var(--bg);
  border: 2px solid var(--line); border-radius: 14px; min-height: 52px; padding: 0 14px; width: 100%;
}
.datepick { display: flex; gap: 8px; }
.dp {
  font: inherit; font-weight: 700; color: var(--ink); background: var(--bg);
  border: 2px solid var(--line); border-radius: 14px; min-height: 52px; padding: 0 8px; min-width: 0;
}
.dp-day { flex: 1 1 0; }
.dp-month { flex: 2 1 0; }
.dp-year { flex: 1.4 1 0; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 4px 0 14px; }
.seg-btn {
  display: grid; gap: 2px; justify-items: center; text-align: center; padding: 10px 6px;
  border: 2px solid var(--line); border-radius: 18px; background: var(--bg); box-shadow: 0 3px 0 var(--line);
}
.seg-btn strong { font-size: 1rem; }
.seg-btn span { font-weight: 700; color: var(--ink-soft); font-size: 0.95rem; }
.seg-btn[aria-checked="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.seg-btn[aria-checked="true"] span { color: inherit; opacity: 0.85; }
.sheet-actions.one { grid-template-columns: 1fr; }
.sheet .fine { margin-top: 12px; }

.pic-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 4px; }
.pic { border: 2px solid var(--line); border-radius: 16px; background: var(--bg); padding: 6px; aspect-ratio: 1; }
.pic svg { width: 100%; height: 100%; display: block; }
.pic[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent); background: var(--surface); }

.switch { display: grid; grid-template-columns: 52px 1fr; gap: 12px; align-items: center; margin: 4px 0; font-weight: 800; position: relative; }
.switch input { position: absolute; opacity: 0; width: 52px; height: 32px; margin: 0; }
.track { width: 52px; height: 32px; border-radius: 999px; background: var(--moon-dim); position: relative; transition: background 0.2s; }
.track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.2s; }
.switch input:checked + .track { background: var(--go); }
.switch input:checked + .track::after { transform: translateX(20px); }
.switch input:focus-visible + .track { outline: 3px solid var(--accent); outline-offset: 3px; }
.switch-text small { display: block; font-weight: 600; color: var(--ink-soft); margin-top: 1px; }
.link-btn { background: none; border: 0; font-weight: 800; margin-top: 12px; padding: 8px; text-decoration: underline; width: 100%; }
.link-btn.danger { color: #e03131; }

/* ---------- Confetti ---------- */
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 10; }
.confetti i { position: absolute; top: -20px; left: var(--x); width: 10px; height: 6px; border-radius: 2px; animation: fall var(--d) ease-in var(--delay) forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(var(--r)); } }

.noscript { padding: 24px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
