@font-face {
  font-family: "Fredoka";
  src: url("fonts/fredoka-latin.woff2") format("woff2");
  font-weight: 300 700;
  font-display: swap;
}

:root {
  --ink: #23364a;
  --sky-top: #9fdcff;
  --sky-bottom: #e6f7ff;
  --water: #3aa7e0;
  --water-dark: #1f86c2;
  --teal: #1fb5a8;
  --teal-dark: #158a80;
  --sun: #ffc93c;
  --sun-dark: #e0a400;
  --coral: #ff7a6b;
  --coral-dark: #d9574a;
  --green: #36b37e;
  --card: #ffffff;
  --shadow: 0 8px 0 rgba(35, 54, 74, 0.12), 0 18px 40px rgba(35, 54, 74, 0.12);
  --radius: 28px;
}

* { box-sizing: border-box; }

html, body { height: 100%; overscroll-behavior: none; }

body {
  margin: 0;
  font-family: "Fredoka", ui-rounded, "Arial Rounded MT Bold", "Trebuchet MS", sans-serif;
  font-weight: 500;
  color: var(--ink);
  background: linear-gradient(#78c9f5, var(--sky-top) 120px, var(--sky-bottom) 70%);
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

input { -webkit-user-select: text; user-select: text; }
[hidden] { display: none !important; }

button { font-family: inherit; }

/* ---------- Background ---------- */
.sky { position: fixed; inset: 0; pointer-events: none; z-index: 0; }
.cloud {
  position: absolute; background: #fff; border-radius: 999px; opacity: 0.85;
  width: 160px; height: 46px;
  animation: drift 60s linear infinite;
}
.cloud::before, .cloud::after { content: ""; position: absolute; background: #fff; border-radius: 50%; }
.cloud::before { width: 70px; height: 70px; left: 24px; top: -34px; }
.cloud::after { width: 52px; height: 52px; left: 80px; top: -22px; }
.c1 { top: 9%; left: -10%; }
.c2 { top: 22%; left: 40%; transform: scale(0.7); animation-duration: 80s; animation-delay: -30s; }
.c3 { top: 5%; left: 75%; transform: scale(0.9); animation-duration: 70s; animation-delay: -50s; }
@keyframes drift { from { translate: -20vw 0; } to { translate: 120vw 0; } }

.water { position: fixed; left: 0; right: 0; bottom: 0; height: 90px; pointer-events: none; z-index: 0; }
.water svg { width: 100%; height: 100%; display: block; }
.water path { fill: var(--water); opacity: 0.55; }

/* ---------- Screens ---------- */
.screen {
  display: none; position: relative; z-index: 1; min-height: 100vh;
  padding: calc(24px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 110px calc(20px + env(safe-area-inset-left));
}
.screen.active { display: block; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 4px solid #fff;
  outline: 3px solid rgba(35, 54, 74, 0.08);
}

/* ---------- Buttons ---------- */
.btn {
  border: 0; cursor: pointer; font-size: 1.4rem; font-weight: 700;
  padding: 14px 28px; border-radius: 999px; color: #fff;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
  touch-action: manipulation;
}
.btn:active { transform: translateY(4px); }
.btn:focus-visible, .key:focus-visible, .mute:focus-visible { outline: 4px solid var(--sun); outline-offset: 3px; }
.btn-go { background: var(--teal); box-shadow: 0 6px 0 var(--teal-dark); }
.btn-go:active { box-shadow: 0 2px 0 var(--teal-dark); }
.btn-skip { background: var(--sun); color: #5b4300; box-shadow: 0 6px 0 var(--sun-dark); }
.btn-skip:active { box-shadow: 0 2px 0 var(--sun-dark); }
.btn-ghost { background: #eef4f9; color: var(--ink); box-shadow: 0 6px 0 #cfdbe6; }
.btn-ghost:active { box-shadow: 0 2px 0 #cfdbe6; }

.mute {
  position: fixed; top: calc(14px + env(safe-area-inset-top)); right: calc(14px + env(safe-area-inset-right)); z-index: 20;
  width: 56px; height: 56px; border-radius: 50%; border: 0; cursor: pointer;
  background: #fff; box-shadow: 0 4px 0 rgba(35,54,74,.15); font-size: 1.6rem;
}
.mute .mute-off { display: none; }
.mute[aria-pressed="true"] .mute-on { display: none; }
.mute[aria-pressed="true"] .mute-off { display: inline; }

/* ---------- Otter ---------- */
.otter svg { width: 100%; height: 100%; display: block; overflow: visible; }
.otter.big { width: 210px; height: 240px; }
.otter.mid { width: 130px; height: 150px; margin: 0 auto; }
.otter.tiny { width: 40px; height: 46px; }
.otter .arm-wave { transform-origin: 62px 140px; animation: wave 1.6s ease-in-out infinite; }
@keyframes wave { 0%, 100% { rotate: 0deg; } 50% { rotate: -22deg; } }
.otter.big, .otter.mid { animation: bob 3s ease-in-out infinite; }
@keyframes bob { 0%, 100% { translate: 0 0; } 50% { translate: 0 -6px; } }

/* ---------- Start ---------- */
.start-wrap {
  max-width: 980px; margin: 0 auto; min-height: calc(100vh - 140px);
  display: flex; align-items: center; justify-content: center; gap: 32px; flex-wrap: wrap;
}
.otter-hero { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.bubble {
  position: relative; background: #fff; border-radius: 22px; padding: 14px 18px; max-width: 260px;
  font-size: 1.15rem; text-align: center; box-shadow: 0 4px 0 rgba(35,54,74,.12);
}
.bubble::after {
  content: ""; position: absolute; bottom: -14px; left: 50%; translate: -50% 0;
  border: 14px solid transparent; border-top-color: #fff; border-bottom: 0;
}
.start-card { padding: 28px 32px; width: min(480px, 100%); }
.start-card h1 { margin: 0 0 12px; font-size: 2.6rem; color: var(--water-dark); text-align: center; letter-spacing: .5px; }
.rules { list-style: none; padding: 0; margin: 0 0 20px; display: grid; gap: 10px; font-size: 1.15rem; }
.rules li { display: flex; gap: 12px; align-items: center; background: #f1f9ff; border-radius: 16px; padding: 8px 12px; }
.rule-icon { font-size: 1.5rem; }
.name-label { display: block; font-size: 1.3rem; font-weight: 700; margin-bottom: 8px; }
#name {
  width: 100%; font: inherit; font-size: 1.6rem; padding: 12px 18px; border-radius: 18px;
  border: 3px solid #cfe3f2; color: var(--ink); background: #fbfdff;
}
#name:focus { outline: none; border-color: var(--teal); }
.name-error { min-height: 1.4em; margin: 6px 0 8px; color: var(--coral-dark); font-size: 1.05rem; }
.start-card .btn-go { width: 100%; font-size: 1.7rem; padding: 16px; }
.shake { animation: shake .35s; }
@keyframes shake { 25% { translate: -8px 0; } 75% { translate: 8px 0; } }

/* ---------- Game ---------- */
#screen-game { max-width: 640px; margin: 0 auto; padding-top: calc(14px + env(safe-area-inset-top)); }
.game-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-right: 70px; }
.player { display: flex; align-items: center; gap: 8px; font-size: 1.3rem; font-weight: 700; background: #fff; padding: 4px 16px 4px 8px; border-radius: 999px; box-shadow: 0 4px 0 rgba(35,54,74,.12); max-width: 55%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.timer {
  display: flex; align-items: center; gap: 6px; font-size: 1.9rem; font-weight: 700;
  background: #fff; padding: 4px 18px; border-radius: 999px; box-shadow: 0 4px 0 rgba(35,54,74,.12);
  font-variant-numeric: tabular-nums;
}
.timer.low { color: var(--coral-dark); }
.timer.over { background: var(--coral); color: #fff; }

.progress { margin: 18px 0 4px; padding: 0 20px; }
.river { position: relative; height: 22px; background: #d8eefb; border-radius: 999px; overflow: visible; }
.river-fill { height: 100%; width: 0%; border-radius: 999px; background: repeating-linear-gradient(-45deg, var(--water) 0 12px, #55b8ea 12px 24px); transition: width .25s ease; }
.otter.swim { position: absolute; top: -22px; width: 40px; height: 46px; left: 0%; translate: -50% 0; transition: left .25s ease; }
.progress-text { text-align: center; margin: 6px 0 12px; font-size: 1.1rem; }

.problem-card { padding: 18px 20px 22px; }
.comeback { margin: 0 0 4px; text-align: center; background: #fff5d6; color: #7a5b00; border-radius: 14px; padding: 6px 10px; font-size: 1.05rem; }
.problem {
  display: flex; justify-content: center; align-items: center; gap: 14px;
  font-size: clamp(3.6rem, 13vw, 6rem); font-weight: 700; line-height: 1.1; margin: 4px 0 16px;
  font-variant-numeric: tabular-nums;
}
.op { color: var(--water-dark); }
.answer {
  min-width: 2.2ch; padding: 0 10px; border-radius: 20px; text-align: center;
  background: #f1f9ff; border: 4px dashed #9fcbe8; color: var(--teal-dark);
  display: inline-flex; justify-content: center; align-items: center; height: 1.25em;
}
.answer.filled { border-style: solid; border-color: var(--teal); }
.caret { width: 4px; height: 0.8em; background: var(--teal); border-radius: 2px; animation: blink 1s steps(1) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }
.answer.pop { animation: pop .18s ease; }
@keyframes pop { 50% { scale: 1.1; } }

.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; max-width: 360px; margin: 0 auto; }
.key {
  border: 0; cursor: pointer; font-size: 2rem; font-weight: 700; color: var(--ink);
  background: #eef6fc; border-radius: 18px; height: 64px; box-shadow: 0 5px 0 #c6dcec;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.key:active, .key.pressed { transform: translateY(4px); box-shadow: 0 1px 0 #c6dcec; }
.key-back { background: #ffe9e6; box-shadow: 0 5px 0 #f3c4bd; color: var(--coral-dark); }
.key-enter { background: var(--teal); box-shadow: 0 5px 0 var(--teal-dark); color: #fff; font-size: 1.4rem; }
.key-enter:active, .key-enter.pressed { box-shadow: 0 1px 0 var(--teal-dark); }

.game-actions { display: flex; justify-content: center; align-items: center; gap: 14px; margin-top: 18px; flex-wrap: wrap; }
.skipped-count { font-size: 1rem; color: #6b7f92; }
.cheer { text-align: center; font-size: 1.2rem; min-height: 1.5em; margin-top: 12px; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 30; background: rgba(20, 50, 80, 0.45); display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal[hidden] { display: none; }
.modal-card { padding: 22px 28px 28px; max-width: 460px; text-align: center; animation: pop .3s ease; }
.modal-card h2 { font-size: 2.4rem; margin: 6px 0; color: var(--coral-dark); }
.modal-card p { font-size: 1.2rem; }
.modal-actions { display: flex; flex-direction: column; gap: 14px; margin-top: 14px; }

.late-badge { display: inline-block; background: #fff1d1; color: #8a5a00; border-radius: 999px; padding: 0 8px; font-size: .9em; white-space: nowrap; }

/* ---------- Results ---------- */
#screen-results { max-width: 900px; margin: 0 auto; }
.results-card { padding: 22px 28px 28px; }
.results-head { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.results-head .otter.big { width: 160px; height: 185px; }
.results-head h1 { margin: 0; font-size: 2.4rem; color: var(--water-dark); }
.results-msg { margin: 4px 0; font-size: 1.2rem; }
.score { margin: 4px 0; font-size: 2rem; font-weight: 700; }
.score b { color: var(--teal-dark); font-size: 1.3em; }
.results-date { margin: 0; color: #6b7f92; }
.stats { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0 4px; }
.stat { background: #f1f9ff; border-radius: 16px; padding: 8px 14px; font-size: 1.1rem; }
.stat b { font-size: 1.3em; }
.section-title { font-size: 1.5rem; margin: 22px 0 10px; }
.wrong-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.wrong-item { background: #fff3f1; border: 3px solid #ffd2cc; border-radius: 18px; padding: 10px 14px; }
.wrong-item .eq { font-size: 1.6rem; font-weight: 700; }
.wrong-item .eq .right { color: var(--green); }
.wrong-item .you { color: var(--coral-dark); font-size: 1.05rem; }
.all-right { background: #e9f9f1; border-radius: 18px; padding: 14px; font-size: 1.2rem; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; font-size: 1rem; margin: 0 0 10px; }
.all-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 6px; }
.cell { border-radius: 10px; padding: 4px 8px; font-size: 1rem; font-variant-numeric: tabular-nums; display: flex; justify-content: space-between; gap: 4px; }
.cell .n { color: #8295a8; font-size: .8em; align-self: center; }
.cell.ok { background: #e3f7ec; }
.cell.bad { background: #ffe4e0; }
.cell.blank { background: #eef1f4; }
.cell.mini { display: inline-block; padding: 0 8px; }
.cell .tag { font-size: .85em; }
.results-actions { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; margin-top: 26px; }

/* ---------- Resume + My Scores ---------- */
.resume { background: #fff7dc; border: 3px solid #ffe08a; border-radius: 22px; padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 12px; }
.resume-text { margin: 0; font-size: 1.25rem; text-align: center; }
.resume .btn { width: 100%; }
.start-card .btn-scores { width: 100%; margin-top: 14px; font-size: 1.3rem; }
#screen-scores { max-width: 900px; margin: 0 auto; }
#screen-results, #screen-scores { padding-top: calc(84px + env(safe-area-inset-top)); }
.results-head .otter.mid { margin: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0 0; }
.chip { border: 3px solid #cfe6f7; background: #fff; color: var(--ink); border-radius: 999px; padding: 6px 16px; font-size: 1.1rem; font-weight: 600; cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--water-dark); border-color: var(--water-dark); color: #fff; }
.chart { display: flex; align-items: flex-end; gap: 8px; height: 170px; margin: 18px 0 6px; padding: 0 4px; border-bottom: 3px solid #dbe9f4; }
.chart:empty { display: none; }
.bar-col { flex: 1; max-width: 56px; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; position: relative; }
.bar { width: 100%; background: linear-gradient(var(--water), var(--water-dark)); border-radius: 10px 10px 0 0; }
.bar.best { background: linear-gradient(var(--sun), var(--sun-dark)); }
.bar-num { font-weight: 700; font-size: 1rem; }
.bar-date { position: absolute; bottom: -24px; font-size: .8rem; color: #6b7f92; white-space: nowrap; }
.scores-list { display: flex; flex-direction: column; gap: 10px; margin-top: 34px; }
.score-row { background: #f1f9ff; border-radius: 18px; }
.score-row summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 12px; padding: 12px 16px; font-size: 1.1rem; }
.score-row summary::-webkit-details-marker { display: none; }
.score-row summary::after { content: "▾"; color: #6b7f92; transition: transform .15s; }
.score-row[open] summary::after { transform: rotate(180deg); }
.sr-when { flex: 1; }
.sr-score b { font-size: 1.4em; color: var(--teal-dark); }
.sr-time { color: #4e6378; white-space: nowrap; }
.sr-missed { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 14px; align-items: center; }
.sr-label { font-weight: 700; margin-right: 4px; }
.miss { background: #fff3f1; border: 2px solid #ffd2cc; border-radius: 12px; padding: 2px 10px; }
.miss b { color: var(--green); }
.miss.perfect { background: #e9f9f1; border-color: #bfeed6; }

/* ---------- Small screens ---------- */
@media (max-width: 520px) {
  .screen { padding: calc(16px + env(safe-area-inset-top)) 12px 100px; }
  #screen-start { padding-top: calc(72px + env(safe-area-inset-top)); }
  .otter.big { width: 150px; height: 172px; }
  .start-card { padding: 22px 18px; }
  .start-card h1 { font-size: 2.1rem; }
  .key { height: 58px; }
  .timer { font-size: 1.5rem; }
  .chart { gap: 5px; }
  .bar-date { display: none; }
  .scores-list { margin-top: 16px; }
}
@media (max-height: 760px) and (min-width: 521px) {
  .key { height: 56px; }
  .problem { margin-bottom: 10px; }
}

/* ---------- Opening splash ---------- */
.splash {
  position: fixed; inset: 0; z-index: 100; overflow: hidden; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: linear-gradient(#78c9f5, #9fdcff 18%, #e6f7ff 75%);
  transition: opacity 0.35s ease;
}
.splash.splash-out { opacity: 0; pointer-events: none; }
.splash:focus { outline: none; }
.splash-stage { position: relative; width: min(420px, 62vw, 42vh); aspect-ratio: 420 / 500; }
.splash-otter { position: absolute; left: 14.3%; top: 18%; width: 71.4%; height: 69%; }
.splash-otter svg, .splash-fx { width: 100%; height: 100%; overflow: visible; }
.splash-fx { position: absolute; inset: 0; }
.splash-fx text { font-family: inherit; font-weight: 700; font-size: 44px; fill: #fff; }
.splash-title { margin: 1.5vh 0 0; font-size: clamp(56px, 14vw, 118px); line-height: 1; font-weight: 700; color: #1f86c2; letter-spacing: 1px; text-shadow: 0 5px 0 #fff; }
.splash-sub { margin: 1.4vh 0 0; font-size: clamp(22px, 5vw, 40px); font-weight: 500; color: var(--ink, #23364a); }
.splash-water { position: absolute; left: 0; bottom: 0; width: 100%; height: 13vh; }
.splash-water path { fill: #3aa7e0; opacity: 0.55; }

.splash[data-kind="balance"] .splash-stage { margin-top: 5vh; }
.splash[data-kind="juggle"] .splash-otter { animation: bob 0.6s ease-in-out infinite; }
.splash .toss { animation: toss 0.6s ease-in-out infinite alternate; }
.splash .toss-2 { animation-delay: -0.2s; }
.splash .toss-3 { animation-delay: -0.4s; }
@keyframes toss { from { translate: 0 12px; } to { translate: 0 -30px; } }

.splash[data-kind="splash"] .splash-otter { animation: dive 0.8s cubic-bezier(0.3, 1.5, 0.6, 1) both; }
.splash .splash-pool { transform-box: fill-box; transform-origin: 50% 100%; animation: ripple 0.6s 0.45s ease-out both; }
.splash .drop { fill: #3aa7e0; opacity: 0.8; animation: fling 0.7s 0.5s ease-out both; }
.splash .drop-1 { animation-delay: 0.6s; }
.splash .drop-2 { animation-delay: 0.7s; }
@keyframes dive { from { translate: 0 -140%; rotate: -20deg; } to { translate: 0 0; rotate: 0deg; } }
@keyframes ripple { from { scale: 0.3 0.2; } to { scale: 1 1; } }
@keyframes fling { from { translate: 0 60px; opacity: 0; } to { translate: 0 0; opacity: 0.8; } }

.splash .tower { transform-box: fill-box; transform-origin: 50% 100%; animation: wobble 0.9s ease-in-out infinite; }
.splash[data-kind="balance"] .splash-otter { transform-origin: 50% 100%; animation: sway 0.9s ease-in-out infinite; }
@keyframes wobble { 0%, 100% { rotate: -7deg; } 50% { rotate: 7deg; } }
@keyframes sway { 0%, 100% { rotate: 2deg; } 50% { rotate: -2deg; } }

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

/* ---------- Print ---------- */
@media print {
  body { background: #fff; }
  .splash, .sky, .water, .mute, .no-print, #screen-start, #screen-game, #screen-scores, .modal { display: none !important; }
  #screen-results { display: block !important; padding: 0; min-height: 0; }
  .card { box-shadow: none; border: 0; outline: 0; }
  .otter { animation: none !important; }
  .results-head .otter.big { width: 90px; height: 104px; }
  .cell, .wrong-item, .stat { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .wrong-item, .cell { break-inside: avoid; }
}
