:root {
  --bg: #EEF6FF;
  --ink: #0F172A;
  --muted: #475569;
  --line: #E2E8F0;
  --card: #FFFFFF;
  --display: 'Baloo 2', 'Nunito', system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Nunito', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}
button, input, select { font-family: inherit; }
button { cursor: pointer; }
input, select { -webkit-user-select: text; user-select: text; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
h1, h2, h3 { font-family: var(--display); margin: 0; }

/* ---------- Portrait warning ---------- */
#rotate { display: none; }
@media (orientation: portrait) and (max-width: 900px) {
  #rotate {
    display: flex; position: fixed; inset: 0; z-index: 50; background: var(--bg);
    flex-direction: column; align-items: center; justify-content: center; gap: 12px;
    color: var(--muted); font-weight: 800; font-size: 20px; text-align: center; padding: 24px;
  }
}

/* ---------- Race view ---------- */
.race {
  height: 100dvh; display: flex; flex-direction: column; gap: 2.2vh;
  padding: max(20px, env(safe-area-inset-top)) max(32px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(32px, env(safe-area-inset-left));
}
.topbar { display: flex; align-items: center; gap: 16px; }
.topbar h1 { font-size: clamp(28px, 3.2vw, 40px); font-weight: 800; letter-spacing: -0.5px; line-height: 1; }
.topbar .date { font-size: 17px; font-weight: 700; color: var(--muted); }
.tabs { margin-left: auto; display: flex; gap: 6px; background: var(--card); border-radius: 18px; padding: 6px; overflow-x: auto; max-width: 50vw; }
.tabs button { min-height: 44px; padding: 0 16px; border: 0; border-radius: 13px; background: transparent; color: #334155; font-size: 16px; font-weight: 800; white-space: nowrap; }
.tabs button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.gear { position: relative; width: 56px; height: 56px; border-radius: 16px; border: 0; background: var(--card); color: #334155; display: flex; align-items: center; justify-content: center; overflow: hidden; flex-shrink: 0; touch-action: none; }
.gear .fill { position: absolute; inset: auto 0 0 0; height: 0; background: #CBD5E1; transition: height 120ms ease-out; }
.gear.holding .fill { height: 100%; transition: height 900ms linear; }
.gear svg { position: relative; }
.toast { position: fixed; left: 50%; top: 92px; transform: translateX(-50%); background: var(--ink); color: #fff; font-weight: 800; padding: 10px 18px; border-radius: 999px; z-index: 30; animation: fadeout 2.2s forwards; }
@keyframes fadeout { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }

.lanes { flex: 1 1 0; min-height: 0; display: flex; flex-direction: column; gap: 2vh; }
.lane {
  flex: 1 1 0; min-height: 0; max-height: 300px;
  background: var(--card); border-radius: 28px; padding: 2vh 28px;
  display: flex; align-items: center; gap: 32px; box-shadow: 0 2px 0 rgba(15,23,42,0.06);
}
.who { width: 200px; flex-shrink: 0; display: flex; flex-direction: column; gap: 10px; }
.who-top { display: flex; align-items: center; gap: 12px; }
.avatar { width: 60px; height: 60px; border-radius: 18px; color: #fff; display: flex; align-items: center; justify-content: center; font-family: var(--display); font-size: 30px; font-weight: 800; flex-shrink: 0; overflow: hidden; background-size: cover; background-position: center; }
.who .name { font-family: var(--display); font-size: 30px; font-weight: 800; line-height: 1; overflow-wrap: anywhere; }
.who .goal { font-size: 15px; font-weight: 700; color: var(--muted); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 14px; font-weight: 800; border-radius: 999px; padding: 4px 10px; }
.chip.streak { color: #9A3412; background: #FFF1E6; }
.chip.today { color: #166534; background: #DCFCE7; }

.track-wrap { flex: 1 1 0; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.track-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.track-head .pg { font-family: var(--display); font-size: 26px; font-weight: 800; }
.track-head .pg span { color: #64748B; font-weight: 700; }
.track-head .left { font-size: 17px; font-weight: 800; }
.track { position: relative; padding: 62px 0 44px; }
.segs { display: flex; gap: 3px; align-items: center; height: 44px; }
.seg { flex: 1 1 0; height: 36px; border-radius: 6px; background: var(--line); transition: background 300ms; }
.seg.now { height: 44px; }
.bar { flex: 1 1 0; height: 36px; border-radius: 10px; background: var(--line); overflow: hidden; }
.bar > div { height: 100%; border-radius: 10px; transition: width 700ms cubic-bezier(.34,1.56,.64,1); }
.runner { position: absolute; top: 0; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; transition: left 700ms cubic-bezier(.34,1.56,.64,1); }
.runner .bubble { width: 48px; height: 48px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 4px 10px rgba(15,23,42,0.2); color: #fff; display: flex; align-items: center; justify-content: center; font-family: var(--display); font-size: 22px; font-weight: 800; background-size: cover; background-position: center; }
.runner .tip { width: 12px; height: 12px; transform: rotate(45deg); margin-top: -8px; }
.runner.bump .bubble { animation: bump 700ms cubic-bezier(.34,1.56,.64,1); }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.35) translateY(-10px); } 100% { transform: scale(1); } }
.marks { position: absolute; left: 0; right: 0; bottom: 0; height: 36px; }
.mark { position: absolute; top: 0; display: flex; align-items: center; gap: 4px; font-size: 13px; font-weight: 800; color: #64748B; white-space: nowrap; }

.act { width: 170px; flex-shrink: 0; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.add {
  position: relative; overflow: hidden; width: 164px; min-height: 64px; border-radius: 18px; border: 2px solid; background: #fff;
  display: flex; align-items: center; justify-content: center; padding: 0 12px; touch-action: none;
}
.add .fill { position: absolute; inset: 0 auto 0 0; width: 0; transition: width 150ms ease-out; }
.add.holding .fill { width: 100%; transition-property: width; transition-timing-function: linear; }
.add .lbl { position: relative; display: flex; align-items: center; gap: 8px; font-size: 17px; font-weight: 800; }
.hint { font-size: 13px; font-weight: 700; color: var(--muted); }
.state { width: 164px; min-height: 92px; border-radius: 22px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-align: center; padding: 8px; }
.state.done { background: #F0FDF4; color: #166534; border: 2px solid #BBF7D0; }
.state b { font-size: 17px; }
.state .big { font-family: var(--display); font-size: 34px; font-weight: 800; line-height: 1; }
.state small { font-size: 13px; font-weight: 700; color: #334155; }
.undo { border: 0; background: transparent; color: var(--muted); font-size: 15px; font-weight: 800; display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 12px; }

.family { display: flex; align-items: center; gap: 20px; background: var(--card); border-radius: 20px; padding: 12px 24px; font-size: 16px; font-weight: 800; }
.family .meter { flex: 1 1 0; height: 14px; border-radius: 999px; background: var(--line); overflow: hidden; }
.family .meter div { height: 100%; background: #16A34A; border-radius: 999px; transition: width 700ms; }
.family .txt { color: #334155; white-space: nowrap; }

.empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; text-align: center; color: var(--muted); font-size: 20px; font-weight: 700; }

/* ---------- Overlays ---------- */
#overlay:empty { display: none; }
.ov { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.ov.dim { background: rgba(15,23,42,0.55); animation: fadein 200ms; }
@keyframes fadein { from { opacity: 0; } }
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti i { position: absolute; top: -20px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }
.cel-card {
  position: relative; width: min(560px, 90vw); background: #fff; border-radius: 36px; padding: 44px 48px 32px;
  display: flex; flex-direction: column; align-items: center; gap: 12px; text-align: center; box-shadow: 0 30px 60px rgba(15,23,42,0.3);
  animation: pop 450ms cubic-bezier(.34,1.56,.64,1);
}
@keyframes pop { from { transform: scale(0.6); opacity: 0; } }
.cel-badge { width: 150px; height: 150px; border-radius: 50%; color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; margin-top: -116px; border: 8px solid #fff; box-shadow: 0 10px 24px rgba(15,23,42,0.25); background-size: cover; background-position: center; }
.cel-badge span { font-size: 18px; font-weight: 800; }
.cel-badge strong { font-family: var(--display); font-size: 72px; font-weight: 800; line-height: 0.9; }
.cel-card h2 { font-size: 44px; font-weight: 800; line-height: 1.05; margin-top: 6px; }
.cel-card p { margin: 0; font-size: 22px; font-weight: 700; color: #334155; }
.stars { display: flex; gap: 8px; color: #F59E0B; }
.stars svg { animation: pop 500ms cubic-bezier(.34,1.56,.64,1) backwards; }
.btn-big { margin-top: 8px; min-height: 64px; padding: 0 40px; border: 0; border-radius: 20px; color: #fff; font-size: 22px; font-weight: 800; }

.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; gap: 64px; padding: 48px; animation: fadein 250ms; }
.screen h1 { font-size: clamp(48px, 6.4vw, 88px); font-weight: 800; line-height: 0.95; letter-spacing: -1px; }
.screen p { margin: 0; font-size: 24px; font-weight: 700; line-height: 1.35; }
.ring { position: relative; width: 340px; height: 340px; flex-shrink: 0; }
.ring .mid { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring .mid b { font-family: var(--display); font-size: 110px; font-weight: 800; line-height: 0.9; }
.ring .mid span { font-size: 20px; font-weight: 800; color: #334155; }
.pill { align-self: flex-start; display: inline-flex; align-items: center; gap: 8px; background: #fff; font-size: 18px; font-weight: 800; border-radius: 999px; padding: 8px 18px; }
.flags { display: flex; align-items: center; gap: 14px; background: #fff; border-radius: 24px; padding: 14px 20px; font-size: 20px; font-weight: 800; align-self: flex-start; }
.done-screen { background: #1E3A8A; color: #fff; flex-direction: column; gap: 20px; text-align: center; }
.trophy { width: 190px; height: 190px; border-radius: 50%; background: #FDE68A; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 0 16px rgba(253,230,138,0.25), 0 0 0 36px rgba(253,230,138,0.12); animation: pop 600ms cubic-bezier(.34,1.56,.64,1); }
.ghost { min-height: 64px; padding: 0 32px; border: 3px solid #BFDBFE; border-radius: 20px; background: transparent; color: #fff; font-size: 22px; font-weight: 800; }

/* ---------- Settings ---------- */
.settings { height: 100dvh; display: flex; flex-direction: column; gap: 16px; padding: 20px 32px; background: #F1F5F9; overflow: hidden; }
.settings header { display: flex; align-items: center; gap: 16px; }
.settings header h1 { font-size: 34px; font-weight: 800; }
.settings header .note { margin-left: auto; font-size: 14px; font-weight: 700; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.iconbtn { width: 52px; height: 52px; border-radius: 16px; border: 0; background: #fff; color: #334155; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.cols { flex: 1 1 0; min-height: 0; display: flex; gap: 20px; }
.side { width: 300px; flex-shrink: 0; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; padding-bottom: 8px; }
.panel { flex: 1 1 0; min-width: 0; background: #fff; border-radius: 24px; padding: 22px 28px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
.panel h2 { font-size: 26px; font-weight: 800; }
.panel h3 { font-size: 19px; font-weight: 800; margin-top: 4px; }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 20px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 800; color: #334155; }
.field input, .field select, .inline select, .inline input {
  min-height: 48px; border: 2px solid #CBD5E1; border-radius: 14px; padding: 0 12px; font-size: 17px; font-weight: 700; color: var(--ink); background: #fff; width: 100%;
}
.inline { display: flex; align-items: center; gap: 12px; min-height: 48px; font-size: 16px; font-weight: 700; }
.inline select, .inline input[type="number"] { width: auto; min-height: 44px; }
.inline input[type="checkbox"] { width: 24px; height: 24px; accent-color: #1D4ED8; }
.pcard { display: flex; align-items: center; gap: 12px; text-align: left; background: #fff; border: 3px solid transparent; border-radius: 20px; padding: 12px 14px; width: 100%; }
.pcard[aria-pressed="true"] { border-color: var(--ink); }
.pcard .avatar { width: 48px; height: 48px; font-size: 24px; border-radius: 15px; }
.pcard b { display: block; font-size: 19px; color: var(--ink); }
.pcard small { font-size: 14px; font-weight: 700; color: var(--muted); }
.dashed { min-height: 56px; border: 2px dashed #94A3B8; border-radius: 18px; background: transparent; color: #334155; font-size: 16px; font-weight: 800; display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; }
.btn { min-height: 46px; padding: 0 18px; border-radius: 14px; border: 2px solid #CBD5E1; background: #fff; color: #334155; font-size: 15px; font-weight: 800; display: inline-flex; align-items: center; gap: 8px; }
.btn.primary { border-color: transparent; background: #1D4ED8; color: #fff; }
.btn.danger { border-color: #FCA5A5; color: #B91C1C; }
.btn input[type="file"] { display: none; }
.stepper { display: flex; align-items: center; gap: 10px; }
.stepper button { width: 52px; height: 52px; border-radius: 14px; border: 2px solid #CBD5E1; background: #fff; font-size: 26px; font-weight: 800; color: #334155; }
.stepper input { width: 96px; text-align: center; font-family: var(--display); font-size: 30px; font-weight: 800; min-height: 52px; border: 2px solid #CBD5E1; border-radius: 14px; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.swatches button { width: 46px; height: 46px; border-radius: 50%; border: 0; }
.swatches button[aria-pressed="true"] { box-shadow: 0 0 0 4px #fff, 0 0 0 7px var(--ink); }
.photo { width: 76px; height: 76px; border-radius: 24px; display: flex; align-items: center; justify-content: center; color: #fff; font-family: var(--display); font-size: 36px; font-weight: 800; background-size: cover; background-position: center; flex-shrink: 0; }
.sep { border: 0; border-top: 2px solid var(--line); margin: 4px 0; width: 100%; }
.trophies { display: flex; flex-direction: column; gap: 4px; background: #fff; border-radius: 18px; padding: 14px 16px; font-size: 15px; font-weight: 700; color: #334155; }
.trophies b { font-size: 14px; color: var(--muted); }
.newgoal { background: #F8FAFC; border: 2px solid var(--line); border-radius: 18px; padding: 16px; display: flex; flex-direction: column; gap: 12px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
  .confetti { display: none; }
}
@media (max-height: 560px) {
  .track { padding: 54px 0 36px; }
  .who .name { font-size: 24px; }
  .avatar { width: 48px; height: 48px; font-size: 24px; }
  .family { padding: 8px 20px; }
}
