:root {
  --blue: #2b59c3;
  --blue-dark: #1d3f91;
  --red: #ef4b4b;
  --red-dark: #b8323a;
  --green: #5bd18b;
  --green-dark: #2f9e60;
  --gold: #ffcc33;
  --ink: #3a2e2a;
  --muted: #7a6f6a;
  --cream: #fff7e8;
  --card: #ffffff;
  --line: #ecdcc0;
  --sky-top: #9fd8ff;
  --sky-bottom: #fff3d6;
  --radius: 22px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Fredoka', system-ui, sans-serif;
  color: var(--ink);
  background: var(--cream);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h2, h3 { margin: 0 0 6px; }
h3 { font-size: 1rem; font-weight: 600; }
.hidden { display: none !important; }

/* ---------- Background scene ---------- */
.sky {
  position: fixed; inset: 0; z-index: -1; overflow: hidden;
  background: linear-gradient(var(--sky-top), var(--sky-bottom) 70%);
}
.cloud {
  position: absolute; width: 120px; height: 40px; background: #fff; border-radius: 40px; opacity: .9;
  animation: drift linear infinite;
}
.cloud::before, .cloud::after { content: ''; position: absolute; background: #fff; border-radius: 50%; }
.cloud::before { width: 56px; height: 56px; top: -28px; left: 16px; }
.cloud::after { width: 44px; height: 44px; top: -18px; left: 58px; }
.c1 { top: 8%; animation-duration: 70s; animation-delay: -10s; }
.c2 { top: 22%; transform: scale(.7); animation-duration: 95s; animation-delay: -50s; }
.c3 { top: 40%; transform: scale(.5); animation-duration: 120s; animation-delay: -80s; }
@keyframes drift { from { left: -160px; } to { left: 110vw; } }
.hills {
  position: absolute; left: -10%; right: -10%; bottom: -120px; height: 260px;
  background: radial-gradient(ellipse at 30% 0, #a7e3a0 0 45%, transparent 46%),
              radial-gradient(ellipse at 75% 10%, #8fd889 0 42%, transparent 43%);
}

/* ---------- Layout ---------- */
.app { max-width: 560px; margin: 0 auto; padding: 12px 16px 110px; }
.topbar { display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 5; padding: 8px 0; }
.level-chip {
  flex: 1; display: flex; align-items: center; gap: 10px; text-align: left;
  background: var(--card); border: 3px solid var(--ink); border-radius: 18px; padding: 6px 12px 6px 6px;
  box-shadow: 0 4px 0 var(--ink);
}
.level-num {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center; border-radius: 14px;
  background: var(--gold); border: 3px solid var(--ink); font-weight: 700; font-size: 1.25rem;
}
.level-meta { flex: 1; display: grid; gap: 2px; min-width: 0; }
.rank { font-weight: 600; font-size: .95rem; }
.xpbar { height: 12px; background: #eee3cf; border-radius: 99px; overflow: hidden; border: 2px solid var(--ink); }
.xpfill { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--blue), #6f95ff); border-radius: 99px; transition: width .8s cubic-bezier(.2, .9, .3, 1.2); }
.xptext { font-size: .75rem; color: var(--muted); }
.streak {
  display: flex; align-items: center; gap: 4px; font-weight: 700; font-size: 1.2rem;
  background: var(--card); border: 3px solid var(--ink); border-radius: 18px; padding: 10px 12px; box-shadow: 0 4px 0 var(--ink);
}
.streak .flame { display: inline-block; animation: flicker 1.2s ease-in-out infinite; }
.streak.cold .flame { filter: grayscale(1); opacity: .55; animation: none; }
@keyframes flicker { 50% { transform: scale(1.15) rotate(-6deg); } }
.icon-btn {
  width: 48px; height: 52px; flex: none; border-radius: 18px; border: 3px solid var(--ink); background: var(--card);
  box-shadow: 0 4px 0 var(--ink); font-size: 1.3rem;
}
.icon-btn:active, .level-chip:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }

.tab { display: none; animation: tabIn .35s ease; }
.tab.active { display: block; }
@keyframes tabIn { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Cards ---------- */
.card {
  background: var(--card); border: 3px solid var(--ink); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: 0 5px 0 var(--ink); margin-bottom: 16px;
}
.cards { display: grid; gap: 0; margin-top: 18px; }
.intro-card p { margin: 4px 0; color: var(--muted); }
.intro-card .tour { color: var(--blue); font-weight: 600; }

/* ---------- Stage: mascot + timer ---------- */
.stage { position: relative; text-align: center; padding-top: 70px; }
.bubble {
  position: absolute; top: 0; left: 50%; transform: translateX(-50%); z-index: 2;
  background: #fff; border: 3px solid var(--ink); border-radius: 18px; padding: 6px 14px;
  max-width: min(92%, 380px); width: max-content; box-shadow: 0 4px 0 var(--ink);
}
.bubble::after {
  content: ''; position: absolute; left: 50%; bottom: -12px; width: 18px; height: 18px; background: #fff;
  border-right: 3px solid var(--ink); border-bottom: 3px solid var(--ink); transform: translateX(-50%) rotate(45deg);
}
.bubble b { display: block; font-weight: 600; }
.bubble small { color: var(--muted); }
.bubble.pop { animation: pop .4s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes pop { from { transform: translateX(-50%) scale(.6); opacity: 0; } }

.ring-wrap { position: relative; width: min(78vw, 290px); aspect-ratio: 1; margin: 6px auto 0; }
.ring { position: absolute; inset: 0; transform: rotate(-90deg); }
.ring-bg { fill: rgba(255, 255, 255, .75); stroke: #fff; stroke-width: 16; }
.ring-fg { fill: none; stroke: var(--red); stroke-width: 16; stroke-linecap: round; transition: stroke-dashoffset .3s linear; }
.coco { position: absolute; inset: 14% 16% 10%; cursor: pointer; }
.coco svg { width: 100%; height: 100%; overflow: visible; }
.coco-all { transform-origin: 100px 210px; }
.wing { transform-box: fill-box; }
.wing-l { transform-origin: right top; }
.wing-r { transform-origin: left top; }
.tail { transform-box: fill-box; transform-origin: right bottom; animation: wag 2.4s ease-in-out infinite; }
.eyes { transform-box: fill-box; transform-origin: center; }
.eye-happy, .book, .tear { display: none; }

/* idle: bob + blink */
.coco.idle .coco-all { animation: bob 2.2s ease-in-out infinite; }
.coco.idle .eye-open, .coco.paused .eye-open { transform-box: fill-box; transform-origin: center; animation: blink 4s infinite; }
/* focus: reading a book, gentle sway, eyes down */
.coco.focus .book { display: block; }
.coco.focus .coco-all { animation: sway 3s ease-in-out infinite; }
.coco.focus .eyes { transform: translateY(5px); }
.coco.focus .eye-open { animation: blink 5s infinite; }
.coco.focus .wing-l { transform: rotate(-35deg) translate(8px, 8px); }
.coco.focus .wing-r { transform: rotate(35deg) translate(-8px, 8px); }
/* paused: sleepy */
.coco.paused .coco-all { animation: bob 4s ease-in-out infinite; opacity: .85; }
/* celebrate: jump + flap + happy eyes */
.coco.celebrate .coco-all { animation: jump .7s cubic-bezier(.3, 0, .5, 1) infinite; }
.coco.celebrate .eye-open { display: none; }
.coco.celebrate .eye-happy { display: block; }
.coco.celebrate .wing-l { animation: flapL .25s ease-in-out infinite alternate; }
.coco.celebrate .wing-r { animation: flapR .25s ease-in-out infinite alternate; }
/* sad: droop + tear */
.coco.sad .coco-all { transform: rotate(-6deg) translateY(6px); transition: transform .4s; }
.coco.sad .tear { display: block; animation: tear 1.6s ease-in infinite; }
.coco.sad .eyes { transform: scaleY(.6); }
.coco.poke .coco-all { animation: poke .45s ease; }

@keyframes bob { 50% { transform: translateY(-8px); } }
@keyframes sway { 0%, 100% { transform: rotate(-3deg); } 50% { transform: rotate(3deg); } }
@keyframes blink { 0%, 92%, 100% { transform: scaleY(1); } 95% { transform: scaleY(.1); } }
@keyframes wag { 50% { transform: rotate(-10deg); } }
@keyframes jump { 0%, 100% { transform: translateY(0) scale(1.05, .95); } 50% { transform: translateY(-34px) scale(.97, 1.04); } }
@keyframes flapL { to { transform: rotate(40deg); } }
@keyframes flapR { to { transform: rotate(-40deg); } }
@keyframes tear { from { transform: translateY(0); opacity: 1; } to { transform: translateY(26px); opacity: 0; } }
@keyframes poke { 30% { transform: scale(1.12, .88); } 60% { transform: scale(.94, 1.06); } }

.time { font-size: clamp(3rem, 14vw, 4.4rem); font-weight: 700; line-height: 1; margin-top: 10px; font-variant-numeric: tabular-nums; text-shadow: 0 4px 0 rgba(255, 255, 255, .8); }
.time-sub { color: var(--muted); font-weight: 500; margin-top: 4px; }

/* ---------- Controls ---------- */
.controls { display: grid; gap: 14px; margin-top: 16px; }
#runControls { grid-template-columns: 1fr 1fr; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.chip {
  border: 3px solid var(--ink); background: #fff; border-radius: 99px; padding: 6px 14px; font-weight: 600;
  box-shadow: 0 3px 0 var(--ink); transition: transform .1s;
}
.chip.active { background: var(--blue); color: #fff; }
.chip:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }

.btn {
  border: 3px solid var(--ink); border-radius: 18px; padding: 14px 18px; font-weight: 700; font-size: 1.15rem;
  box-shadow: 0 6px 0 var(--ink); transition: transform .08s, box-shadow .08s; background: #fff;
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
.btn-go { background: var(--green); color: #fff; text-shadow: 0 2px 0 var(--green-dark); font-size: 1.35rem; }
.btn-go:hover { filter: brightness(1.05); }
.btn-blue { background: var(--blue); color: #fff; }
.btn-red { background: var(--red); color: #fff; }
.btn-ghost { background: #fff; }
.btn.small { font-size: .95rem; padding: 10px 14px; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

/* ---------- Today cards ---------- */
.goal-card { display: flex; align-items: center; gap: 14px; }
.goal-card p, .challenge-card p { margin: 0; color: var(--muted); }
.mini-ring { position: relative; width: 64px; height: 64px; flex: none; }
.mini-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.mini-ring span { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 700; font-size: .85rem; }
.mr-bg { fill: none; stroke: #eee3cf; stroke-width: 6; }
.mr-fg { fill: none; stroke: var(--green); stroke-width: 6; stroke-linecap: round; transition: stroke-dashoffset .8s ease; }

.challenge-card { display: flex; gap: 14px; align-items: center; background: #fff8dc; }
.challenge-card.done { background: #e4f8eb; }
.ch-icon { font-size: 2rem; width: 56px; height: 56px; flex: none; display: grid; place-items: center; background: #fff; border: 3px solid var(--ink); border-radius: 16px; }
.ch-body { flex: 1; min-width: 0; }
.reward { font-size: .75rem; background: var(--gold); border: 2px solid var(--ink); border-radius: 99px; padding: 1px 8px; margin-left: 4px; vertical-align: middle; }
.progress { height: 12px; background: #eee3cf; border: 2px solid var(--ink); border-radius: 99px; margin-top: 8px; overflow: hidden; }
.progress span { display: block; height: 100%; width: 0; background: var(--gold); transition: width .8s ease; }
.challenge-card.done .progress span { background: var(--green); }

.word { display: flex; align-items: center; gap: 12px; margin-top: 4px; }
.word-emoji { font-size: 2.4rem; }
.word b { display: block; font-size: 1.4rem; color: var(--blue); }
.word small { color: var(--muted); }
.word-ex { margin: 8px 0 0; font-style: italic; }

/* ---------- Journey ---------- */
.path { list-style: none; padding: 30px 0 0; margin: 0; position: relative; overflow-x: clip; }
.stop { display: flex; flex-direction: column; align-items: center; margin-bottom: 18px; transform: translateX(var(--x)); }
.node {
  position: relative; width: 74px; height: 74px; border-radius: 50%; display: grid; place-items: center; font-size: 2rem;
  border: 4px solid var(--ink); box-shadow: 0 6px 0 var(--ink); background: #ddd;
}
.stop.visited .node { background: var(--gold); }
.stop.current .node { background: var(--blue); animation: pulse 1.6s ease-in-out infinite; }
.stop.locked .node { background: #e8e1d4; filter: grayscale(.4); }
.node .me { position: absolute; top: -26px; right: -22px; font-size: 1.7rem; animation: bob 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 6px 0 var(--ink), 0 0 0 10px rgba(43, 89, 195, .25); } }
.stop-info { text-align: center; margin-top: 8px; background: rgba(255, 255, 255, .9); border-radius: 12px; padding: 4px 10px; max-width: 260px; }
.stop-info b { display: block; }
.stop-info small { color: var(--muted); }
.stop.locked .stop-info { opacity: .7; }

/* ---------- Stats ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 14px; }
.stat { text-align: center; padding: 8px; }
.stat-icon { font-size: 1.5rem; display: block; }
.stat b { display: block; font-size: 1.5rem; }
.stat small { color: var(--muted); }

.bars { position: relative; height: 160px; display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; align-items: end; padding: 0 0 22px; margin-top: 8px; }
.bar-col { height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; position: relative; }
.bar-col span { position: absolute; bottom: -22px; font-size: .8rem; color: var(--muted); text-transform: capitalize; }
.bar-col.today span { color: var(--blue); font-weight: 700; }
.bar { width: 100%; min-height: 4px; background: #c9d6f5; border: 2px solid var(--ink); border-radius: 10px 10px 4px 4px; position: relative; transition: height .8s ease; }
.bar.hit { background: var(--green); }
.bar em { position: absolute; top: -20px; left: 0; right: 0; text-align: center; font-style: normal; font-size: .75rem; font-weight: 600; }
.goal-line { position: absolute; left: 0; right: 0; border-top: 2px dashed var(--red); margin-bottom: 22px; z-index: 1; pointer-events: none; }
.goal-line span { position: absolute; left: 0; top: -18px; font-size: .7rem; color: var(--red); font-weight: 600; }

.heatmap-wrap { overflow-x: auto; }
.heatmap { display: grid; grid-auto-flow: column; grid-template-columns: repeat(12, 1fr); grid-template-rows: repeat(7, auto); gap: 4px; margin-top: 8px; min-width: 260px; }
.heatmap i, .legend i { display: block; aspect-ratio: 1; border-radius: 4px; border: 1.5px solid rgba(58, 46, 42, .25); }
.h0 { background: #f2ece0; }
.h1 { background: #c8e9d3; }
.h2 { background: #8fd8a9; }
.h3 { background: #4dbb78; }
.h4 { background: #2b59c3; }
.heatmap .future { visibility: hidden; }
.heatmap .today { border: 2px solid var(--red); }
.legend { display: flex; align-items: center; gap: 4px; justify-content: flex-end; margin-top: 8px; font-size: .75rem; color: var(--muted); }
.legend i { width: 14px; }

/* ---------- Badges ---------- */
.badge-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0 14px; }
.badge { text-align: center; padding: 14px 10px; background: #f4efe6; }
.badge.got { background: #fff; }
.badge:not(.got) { opacity: .7; }
.b-icon { font-size: 2.2rem; display: block; }
.badge.got .b-icon { animation: bob 2.5s ease-in-out infinite; }
.badge b { display: block; }
.badge small { color: var(--muted); font-size: .8rem; }

/* ---------- Bottom nav ---------- */
.bottomnav {
  position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); z-index: 10;
  width: min(528px, calc(100% - 32px)); display: grid; grid-template-columns: repeat(5, 1fr);
  background: #fff; border: 3px solid var(--ink); border-radius: 22px; box-shadow: 0 5px 0 var(--ink); padding: 6px;
  margin-bottom: env(safe-area-inset-bottom);
}
.nav-btn { background: none; border: 0; border-radius: 16px; padding: 6px 0; font-weight: 600; font-size: .8rem; display: grid; justify-items: center; color: var(--muted); }
.nav-btn span { font-size: 1.4rem; }
.nav-btn.active { background: #e3ebff; color: var(--blue); }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; background: rgba(40, 30, 25, .45); animation: fadeIn .2s; }
@keyframes fadeIn { from { opacity: 0; } }
.modal-card {
  width: min(420px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; background: var(--cream); border: 3px solid var(--ink);
  border-radius: 26px; padding: 22px; box-shadow: 0 8px 0 var(--ink); display: grid; gap: 12px; text-align: center;
  animation: pop2 .4s cubic-bezier(.3, 1.6, .5, 1);
}
@keyframes pop2 { from { transform: scale(.7); opacity: 0; } }
.modal-card p { margin: 0; color: var(--muted); }
.modal-coco { font-size: 3.5rem; animation: bob 2s ease-in-out infinite; }
.modal-card label { display: grid; gap: 4px; text-align: left; font-weight: 600; }
.modal-card input:not([type=checkbox]), .modal-card select {
  border: 3px solid var(--ink); border-radius: 14px; padding: 10px 12px; background: #fff; font-weight: 500;
}
.modal-card label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.modal-card label.check input { width: 20px; height: 20px; accent-color: var(--blue); }
.danger { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; border-top: 2px dashed var(--line); padding-top: 12px; }
.danger .note { grid-column: 1 / -1; font-size: .85rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.result h2 { font-size: 2.2rem; color: var(--blue); }
.xp-lines { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.xp-lines li { display: flex; justify-content: space-between; background: #fff; border: 2px solid var(--ink); border-radius: 12px; padding: 8px 12px; animation: slideIn .4s backwards; }
.xp-lines b { color: var(--green-dark); }
@keyframes slideIn { from { transform: translateX(-20px); opacity: 0; } }
.xp-total { font-size: 1.3rem; font-weight: 700; color: var(--blue); }
.levelup { background: var(--gold); border: 3px solid var(--ink); border-radius: 16px; padding: 10px; animation: pop2 .5s .4s backwards cubic-bezier(.3, 1.6, .5, 1); }
.lu-big { font-size: 1.4rem; font-weight: 700; }
.new-badges { display: grid; gap: 8px; }
.nb { animation: pop2 .4s backwards cubic-bezier(.3, 1.6, .5, 1); display: flex; align-items: center; gap: 10px; text-align: left; background: #fff; border: 2px solid var(--ink); border-radius: 14px; padding: 8px 12px; }
.nb span { font-size: 2rem; }
.nb small { display: block; color: var(--muted); }

/* ---------- Confetti & toast ---------- */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 14px; border-radius: 2px; border: 1px solid rgba(0, 0, 0, .15); animation: fall linear forwards; }
@keyframes fall { to { transform: translate(var(--drift), 110vh) rotate(var(--spin)); } }
.toast {
  position: fixed; left: 50%; bottom: 100px; transform: translate(-50%, 20px); z-index: 70; opacity: 0; pointer-events: none;
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 14px; font-weight: 500; transition: .3s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 520px) {
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* ---------- Leaderboard ---------- */
.seg { display: inline-flex; gap: 6px; margin-top: 8px; background: #f2ece0; border: 3px solid var(--ink); border-radius: 99px; padding: 4px; }
.seg-btn { border: 0; background: none; border-radius: 99px; padding: 6px 14px; font-weight: 600; color: var(--muted); }
.seg-btn.active { background: var(--blue); color: #fff; }
.board { list-style: none; padding: 0; margin: 0; }
.row-entry { display: flex; align-items: center; gap: 12px; padding: 10px 14px; margin-bottom: 12px; }
.row-entry.me { background: #e3ebff; }
.row-entry .pos { width: 42px; flex: none; text-align: center; font-size: 1.5rem; font-weight: 700; }
.row-entry .who { flex: 1; min-width: 0; display: grid; }
.row-entry .who b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-entry .who small { color: var(--muted); }
.row-entry .mins { font-weight: 700; color: var(--blue); white-space: nowrap; }
.board .gap { text-align: center; color: var(--muted); margin: -6px 0 6px; }
.board-note { text-align: center; color: var(--muted); margin: 8px 0 16px; }

/* ---------- Account ---------- */
.account-box { grid-column: 1 / -1; display: grid; gap: 8px; text-align: left; background: #fff; border: 2px solid var(--line); border-radius: 16px; padding: 12px; }
.account-box h3 { margin: 0; }
.account-box .row { grid-template-columns: 1fr 1fr; }
.link-btn { background: none; border: 0; color: var(--blue); font-weight: 600; text-decoration: underline; padding: 4px; }
.note { font-size: .85rem; }
.form-error { color: var(--red-dark) !important; font-weight: 600; min-height: 1em; margin: 0; }
.btn:disabled { opacity: .6; cursor: progress; }
.nav-btn { font-size: .72rem; }
