:root {
  --ink: #2a1e3f;
  --ink-soft: #5a6b85;
  --cream: #fffbf0;
  --paper: #fffdf6;
  --sun: #ffd23a;
  --coral: #ff5da2;
  --sea-top: #6fe0f2;
  --sea-mid: #2492e0;
  --sea-deep: #173f9e;
  --sand: #ffd98a;
  --blue: #1b6fd0;
  --font: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", Nunito, system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --sea-top: #3fb7d6;
    --sea-mid: #1a6fbd;
    --sea-deep: #0f2c74;
  }
}
:root[data-theme="dark"] {
  --sea-top: #3fb7d6;
  --sea-mid: #1a6fbd;
  --sea-deep: #0f2c74;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(180deg, var(--sea-top) 0%, var(--sea-mid) 48%, var(--sea-deep) 100%) fixed;
  background-color: var(--sea-mid);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 12% 78%, rgba(255,255,255,.18) 0 10px, transparent 11px),
    radial-gradient(circle at 86% 64%, rgba(255,255,255,.16) 0 7px, transparent 8px),
    radial-gradient(circle at 70% 90%, rgba(255,255,255,.14) 0 14px, transparent 15px),
    radial-gradient(circle at 30% 40%, rgba(255,255,255,.12) 0 5px, transparent 6px),
    radial-gradient(ellipse at 50% -10%, rgba(255,255,255,.35), transparent 60%);
  animation: drift 14s ease-in-out infinite alternate;
}
@keyframes drift { to { transform: translateY(-24px); } }
button { font: inherit; color: inherit; }
a { color: inherit; }
svg { display: block; }
[dir="rtl"] .flip-rtl { transform: scaleX(-1); }
:focus-visible { outline: 3px solid var(--sun); outline-offset: 2px; }

#app {
  position: relative;
  max-width: 560px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
}

/* ---------- server-rendered intro (replaced by the app) ---------- */
.ssr { padding: 32px 20px; color: #fff; max-width: 560px; margin: 0 auto; }
.ssr h1 { font-size: 44px; margin: 0 0 8px; font-weight: 900; }
.ssr a { color: #fff; }
body.plain .ssr { text-align: center; padding-top: 20vh; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 20px;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: #fff;
  font-weight: 800;
  font-size: 17px;
  cursor: pointer;
  box-shadow: 0 5px 0 var(--ink);
  transition: transform .08s, box-shadow .08s;
  text-decoration: none;
}
.btn svg { width: 20px; height: 20px; flex: none; }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
.btn.primary { background: var(--sun); }
.btn.big { min-height: 62px; font-size: 21px; border-radius: 20px; width: 100%; }
.btn.big svg { width: 24px; height: 24px; }
.btn.small { min-height: 38px; padding: 6px 14px; font-size: 15px; border-radius: 12px; box-shadow: 0 3px 0 var(--ink); }
.btn.ghost { background: transparent; box-shadow: none; border-color: transparent; text-decoration: underline; text-underline-offset: 3px; min-height: 40px; }
.btn.ghost:active { transform: none; }
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: #fff;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--ink);
  padding: 0;
  flex: none;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.link { background: none; border: 0; padding: 4px; cursor: pointer; text-decoration: underline; color: var(--ink-soft); font-weight: 700; }

/* ---------- home ---------- */
.home { padding: max(12px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 16px; min-height: 100dvh; }
.home-top { display: flex; justify-content: space-between; align-items: center; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  height: 44px; padding: 0 14px;
  border: 3px solid var(--ink); border-radius: 999px; background: #fff;
  font-weight: 800; font-size: 15px; cursor: pointer; box-shadow: 0 3px 0 var(--ink);
}
.chip svg { width: 20px; height: 20px; }
.hero { text-align: center; margin-top: -4px; }
.mascot { width: 170px; height: 170px; margin: 0 auto -10px; animation: bob 2.6s ease-in-out infinite; filter: drop-shadow(0 10px 0 rgba(10,30,80,.25)); }
@keyframes bob { 50% { transform: translateY(-10px) scale(1.04, .97); } }
.logo {
  margin: 0;
  font-size: clamp(46px, 14vw, 68px);
  line-height: .95;
  font-weight: 900;
  letter-spacing: -.01em;
  color: #fff;
  -webkit-text-stroke: 10px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 7px 0 var(--ink);
}
.logo span:last-child { color: var(--sun); }
.tagline { margin: 12px 0 0; color: #fff; font-weight: 800; font-size: 17px; text-shadow: 0 2px 0 rgba(10,30,80,.4); }
.daily-card {
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 26px;
  padding: 18px 18px 16px;
  box-shadow: 0 8px 0 var(--ink), 0 18px 40px rgba(10,30,80,.35);
  display: flex; flex-direction: column; gap: 12px;
}
.daily-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.daily-head strong { font-size: 22px; font-weight: 900; color: var(--blue); }
.daily-head span { color: var(--ink-soft); font-weight: 700; font-size: 14px; }
.daily-hint { margin: 0; font-size: 15px; line-height: 1.35; color: var(--ink-soft); font-weight: 600; }
.daily-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.daily-stats div { background: #eef6fc; border-radius: 14px; padding: 8px 6px; text-align: center; display: flex; flex-direction: column; gap: 2px; }
.daily-stats b { font-size: 20px; font-weight: 900; direction: ltr; unicode-bidi: isolate; }
.daily-stats span { font-size: 12px; color: var(--ink-soft); font-weight: 700; }
.clear-rate { display: flex; align-items: baseline; justify-content: center; gap: 8px; font-size: 14px; font-weight: 700; color: var(--ink-soft); }
.clear-rate b { color: var(--coral); font-size: 18px; }
.first-note { margin: -2px 0 0; text-align: center; font-size: 13px; color: var(--ink-soft); font-weight: 700; }
.next { margin: 0; text-align: center; font-size: 14px; color: var(--ink-soft); font-weight: 700; }
.next b { font-variant-numeric: tabular-nums; color: var(--ink); direction: ltr; unicode-bidi: isolate; }
.refresh { margin: -6px 0 0; text-align: center; font-size: 13px; color: var(--ink-soft); font-weight: 600; }
.refresh bdi { font-weight: 800; color: var(--ink); white-space: nowrap; }
.home-links { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.link-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 999px; border: 0;
  background: rgba(255,255,255,.22); color: #fff; font-weight: 800; font-size: 15px; cursor: pointer;
}
.link-btn svg { width: 18px; height: 18px; }
.home-foot { margin-top: auto; text-align: center; color: rgba(255,255,255,.85); font-size: 12px; }
.home-foot p { margin: 0; }

/* ---------- game ---------- */
.game { height: 100vh; height: 100dvh; display: flex; flex-direction: column; user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.game-top { display: flex; align-items: center; gap: 10px; padding: max(8px, env(safe-area-inset-top)) 12px 4px; }
.game-title { flex: 1; min-width: 0; display: flex; flex-direction: column; color: #fff; text-shadow: 0 2px 0 rgba(10,30,80,.45); }
.game-title strong { font-size: 17px; font-weight: 900; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.game-title span { font-size: 13px; font-weight: 700; opacity: .9; }
.timer {
  font-weight: 900; font-size: 20px; font-variant-numeric: tabular-nums;
  background: var(--ink); color: var(--sun); border-radius: 12px; padding: 6px 12px; direction: ltr; min-width: 92px; text-align: center;
}
.stage { position: relative; flex: 1; min-height: 0; direction: ltr; overflow: hidden; }
.tiles { position: absolute; inset: 0; transition: filter .25s; }
.stage.paused .tiles { filter: blur(16px) saturate(.6); }
.tile {
  position: absolute; left: 0; top: 0;
  width: var(--s); height: var(--s);
  padding: 0; margin: 0; border: 0; background: none;
  cursor: pointer;
  transform-origin: 0 0;
  transition: transform .2s cubic-bezier(.25,.85,.35,1);
  will-change: transform;
  touch-action: manipulation;
}
.tile:disabled { cursor: default; }
.tiles.instant .tile { transition: none; }
.face {
  display: block; width: 100%; height: 100%;
  border-radius: 21%;
  background: var(--cream);
  border: max(1.5px, calc(var(--s) * .035)) solid #24385f;
  box-shadow: 0 calc(var(--s) * .07) 0 #6aa9bd, 0 calc(var(--s) * .1) calc(var(--s) * .14) rgba(5,30,70,.35);
  transition: filter .18s, background-color .18s;
}
.face svg { width: 84%; height: 84%; margin: 8%; pointer-events: none; }
.tile.covered .face { filter: brightness(.56) saturate(.6); }
.tile.back .face { background: repeating-linear-gradient(45deg, #3d8fd8 0 6px, #347fc6 6px 12px); filter: brightness(.85); }
.tile.back .face svg { visibility: hidden; }
.tile:not(:disabled):active .face { transform: scale(.94); }
.tile.hl .face { animation: glow 1s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 0 0 calc(var(--s) * .1) var(--sun), 0 calc(var(--s) * .07) 0 #6aa9bd; } }
.tile.popping .face { animation: pop .34s ease-in forwards; }
@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.22) rotate(-6deg); opacity: 1; }
  100% { transform: scale(.1) rotate(10deg); opacity: 0; }
}
.tile.spin .face { animation: spin .34s ease-in-out; }
@keyframes spin { 50% { transform: rotateY(90deg) scale(.9); } }

.tray-bg {
  position: absolute;
  display: flex; gap: calc(var(--s) * .06); padding: calc(var(--s) * .12);
  background: rgba(12, 36, 96, .55);
  border-radius: calc(var(--s) * .3);
  box-shadow: inset 0 4px 0 rgba(255,255,255,.12), 0 0 0 3px var(--ink), 0 7px 0 3px var(--ink);
  transition: box-shadow .2s;
}
.tray-bg i { flex: none; width: calc(var(--s) * .84); height: calc(var(--s) * .84); border-radius: 21%; background: rgba(255,255,255,.1); }
.tray-bg.danger { box-shadow: inset 0 4px 0 rgba(255,255,255,.12), 0 0 0 3px var(--coral), 0 0 0 8px rgba(255,93,162,.35), 0 7px 0 3px var(--ink); }
.tray-bg.shake { animation: shake .45s; }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
.tray-bg.spot, .tools.spot { box-shadow: 0 0 0 6px var(--sun), 0 0 30px 8px rgba(255,210,58,.7); border-radius: 18px; }
.park-bg {
  position: absolute; border: 3px dashed rgba(255,255,255,.55); border-radius: calc(var(--s) * .26);
  opacity: 0; transition: opacity .2s;
}
.park-bg.show { opacity: 1; }
.fx { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.bubble {
  position: absolute; width: var(--sz); height: var(--sz); margin: calc(var(--sz) / -2) 0 0 calc(var(--sz) / -2);
  border: 2px solid rgba(255,255,255,.95); border-radius: 50%; background: rgba(255,255,255,.25);
  z-index: 3000;
  animation: fly .6s ease-out forwards;
}
@keyframes fly { to { transform: translate(var(--dx), var(--dy)) scale(.4); opacity: 0; } }
.confetti {
  position: absolute; top: -16px; width: 9px; height: 15px; border-radius: 2px; z-index: 3000;
  animation: fall 2.2s cubic-bezier(.3,.6,.5,1) forwards;
}
@keyframes fall { to { transform: translate(var(--drift), 110vh) rotate(var(--rot)); } }
.start-hint {
  position: absolute; left: 50%; top: 4px; transform: translateX(-50%);
  background: rgba(20, 40, 90, .7); color: #fff; font-weight: 800; font-size: 13px;
  padding: 6px 12px; border-radius: 999px; white-space: nowrap; pointer-events: none; z-index: 1500;
  transition: opacity .3s;
}
.start-hint.gone { opacity: 0; }
.coach {
  position: absolute; left: 12px; right: 12px; top: 8px; z-index: 2000;
  background: var(--paper); border: 3px solid var(--ink); border-radius: 18px; padding: 12px 14px;
  box-shadow: 0 5px 0 var(--ink); animation: rise .25s ease-out;
}
[dir="rtl"] .coach { direction: rtl; }
.coach p { margin: 0; font-weight: 800; font-size: 16px; line-height: 1.35; }
.coach-row { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; min-height: 8px; }
@keyframes rise { from { transform: translateY(-10px); opacity: 0; } }
.finger { position: absolute; left: 0; top: 0; width: calc(var(--s) * .9); z-index: 2100; pointer-events: none; transition: transform .3s; }
.finger svg { animation: tap 1s ease-in-out infinite; filter: drop-shadow(0 4px 0 rgba(0,0,0,.25)); }
@keyframes tap { 50% { transform: translate(-4px, -8px); } }
.toast {
  position: absolute; left: 50%; bottom: 18%; transform: translateX(-50%); z-index: 5000;
  background: var(--ink); color: #fff; font-weight: 800; padding: 10px 16px; border-radius: 12px; white-space: nowrap;
  animation: rise .2s ease-out;
}
.toast.global { position: fixed; bottom: 12%; }
.tools { display: flex; gap: 10px; justify-content: center; padding: 6px 12px max(12px, env(safe-area-inset-bottom)); transition: box-shadow .2s; }
.tool {
  position: relative; flex: 1; max-width: 140px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 6px 7px; border: 3px solid var(--ink); border-radius: 16px; background: #fff;
  box-shadow: 0 4px 0 var(--ink); cursor: pointer; font-weight: 800; font-size: 13px;
}
.tool:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.tool-ic svg { width: 24px; height: 24px; }
.tool-n {
  position: absolute; top: -9px; inset-inline-end: -7px;
  min-width: 24px; height: 24px; border-radius: 12px; border: 2.5px solid var(--ink);
  background: var(--coral); color: #fff; font-size: 13px; display: grid; place-items: center;
}
.tool.spent { background: #e4e9f0; color: #8a96a8; border-color: #6b7689; box-shadow: 0 4px 0 #6b7689; }
.tool.spent .tool-n { background: #a9b4c4; }
.tool.idle { background: #f1f4f8; color: var(--ink-soft); }

/* ---------- modals ---------- */
.modal-wrap {
  position: fixed; inset: 0; z-index: 9000;
  display: grid; place-items: center; padding: 16px;
  background: rgba(8, 22, 58, .55);
  opacity: 0; transition: opacity .18s;
}
.modal-wrap.in { opacity: 1; }
.modal {
  position: relative; width: 100%; max-width: 420px; max-height: calc(100dvh - 32px); overflow: auto;
  background: var(--paper); border: 3px solid var(--ink); border-radius: 26px;
  box-shadow: 0 8px 0 var(--ink); padding: 22px 20px 18px; text-align: center;
  transform: translateY(14px) scale(.97); transition: transform .2s cubic-bezier(.3,.8,.4,1.2);
}
.modal-wrap.in .modal { transform: none; }
.modal h2 { margin: 4px 0 10px; font-size: 26px; font-weight: 900; }
.modal p { margin: 0 0 12px; font-weight: 600; line-height: 1.4; color: var(--ink-soft); }
.modal-x { position: absolute; top: 12px; inset-inline-end: 12px; width: 38px; height: 38px; box-shadow: 0 2px 0 var(--ink); }
.modal .stack { display: flex; flex-direction: column; gap: 12px; margin-top: 14px; }
.modal .row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 6px 0 4px; }
.modal .row .btn { flex: 1; min-width: 0; padding: 10px 12px; }
.badge { width: 110px; height: 110px; margin: -6px auto 0; animation: bob 2.4s ease-in-out infinite; }
.badge.sad { width: 96px; height: 96px; filter: saturate(.6); transform: rotate(-12deg); animation: none; }
.big-time {
  font-size: 54px; font-weight: 900; font-variant-numeric: tabular-nums; direction: ltr;
  color: var(--sun); -webkit-text-stroke: 8px var(--ink); paint-order: stroke fill; margin: 0 0 8px;
}
.new-best { color: var(--coral) !important; font-weight: 900 !important; }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 4px 0 12px; }
.win-stats div { background: #eef6fc; border-radius: 14px; padding: 8px 4px; display: flex; flex-direction: column; }
.win-stats b { font-size: 20px; font-weight: 900; }
.win-stats span { font-size: 12px; font-weight: 700; color: var(--ink-soft); }
.rank-line { font-weight: 800 !important; color: var(--blue) !important; min-height: 1.4em; }
.sub { font-weight: 800 !important; color: var(--ink) !important; margin-bottom: 4px !important; }
.tool-btn { background: #eaf6ff; }
.howto { text-align: start; padding-inline-start: 22px; margin: 0 0 12px; line-height: 1.45; font-weight: 600; }
.howto li { margin-bottom: 6px; }
.howto-tools { text-align: start; }
.fine { font-size: 13px; text-align: start; }
.lang-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.lang-grid a {
  display: block; padding: 10px 8px; border: 2.5px solid var(--ink); border-radius: 12px; text-decoration: none; font-weight: 800; background: #fff;
}
.lang-grid a.on { background: var(--sun); }
.card-modal { max-width: 460px; }
.card-preview { display: grid; place-items: center; min-height: 240px; margin-bottom: 12px; }
.card-preview img { width: 100%; max-height: 58dvh; object-fit: contain; border-radius: 14px; border: 3px solid var(--ink); }
.card-actions .btn { flex-direction: column; gap: 3px; font-size: 14px; padding: 8px 6px; min-height: 64px; line-height: 1.15; }
.spinner { width: 40px; height: 40px; border-radius: 50%; border: 5px solid #cfe3f3; border-top-color: var(--blue); animation: rot .8s linear infinite; }
@keyframes rot { to { transform: rotate(360deg); } }


@media (min-height: 760px) and (min-width: 480px) {
  .tool { font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .05s !important; }
}

/* ---------- header audio switches ---------- */
.top-btns { display: flex; gap: 8px; }
.audio-row { margin: 0 0 4px !important; }
.audio-row .icon-btn { width: 52px; height: 48px; }
.icon-btn[aria-pressed="false"] { opacity: .65; }

/* ---------- background reef: sways, bounces on the beat ---------- */
.reef { position: fixed; inset: 0; z-index: 0; pointer-events: none; overflow: hidden; }
#app { z-index: 1; }
.reef .deco { position: absolute; }
.reef .sway { transform-origin: 50% 100%; animation: reef-sway 3.6s ease-in-out infinite; animation-delay: var(--delay); }
.reef .bounce { transform-origin: 50% 100%; transition: transform .26s cubic-bezier(.3, .7, .4, 1); }
.reef.beat .bounce { transform: scale(1.04, 1.09); transition-duration: .06s; }
.reef.beat-strong .bounce { transform: scale(1.07, 1.16); transition-duration: .06s; }
.reef .tuft .bounce { transform-origin: 50% 90%; }
.reef .kelp { opacity: .9; }
@keyframes reef-sway {
  0%, 100% { transform: rotate(calc(var(--amp) * -1)); }
  50% { transform: rotate(var(--amp)); }
}

/* ---------- level intro: layers dealt in from the bottom one up ---------- */
.tiles.dealing .tile .face { animation: deal-in .42s cubic-bezier(.25, .9, .35, 1.25) backwards; animation-delay: var(--d, 0ms); }
@keyframes deal-in {
  from { transform: translateY(calc(var(--s) * -0.9)) scale(.35); opacity: 0; }
  70% { opacity: 1; }
}
/* a tile that just got uncovered opens up */
.tile.reveal .face { animation: reveal .4s ease-out; }
@keyframes reveal {
  0% { transform: scale(.88); filter: brightness(.6); }
  55% { transform: scale(1.1); filter: brightness(1.12); }
  100% { transform: none; filter: none; }
}
@media (prefers-reduced-motion: reduce) {
  .reef.beat .bounce, .reef.beat-strong .bounce { transform: none; }
}
