:root {
  --primary: #1565c0;
  --on-primary: #ffffff;
  --primary-container: #d4e3ff;
  --on-primary-container: #001b3d;
  --secondary: #545f70;
  --secondary-container: #d8e3f8;
  --surface: #f8faff;
  --on-surface: #191c20;
  --outline: #c3c7cf;
  --error: #b3261e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --primary: #a6c8ff;
    --on-primary: #00315c;
    --primary-container: #1f477d;
    --on-primary-container: #d4e3ff;
    --secondary: #bcc7db;
    --secondary-container: #3b4758;
    --surface: #111418;
    --on-surface: #e1e2e8;
    --outline: #43474e;
    --error: #f2b8b5;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--surface); color: var(--on-surface);
  overscroll-behavior: none; touch-action: manipulation;
}
#app {
  /* Leave room each side for the flanking buttons (44px btn + 16px gap + 15px
     margin = 75px → 150px total) so they never run off-screen. */
  --wheel: min(calc(100vw - 150px), 44vh, 320px);
  display: flex; flex-direction: column;
  height: 100dvh; max-width: 600px; margin: 0 auto; position: relative;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#topbar {
  display: flex; align-items: center; gap: 4px;
  padding: 12px 16px; font-size: 20px; font-weight: 600;
}
#topbar .spacer { flex: 1; }
#level-label { cursor: pointer; }

/* Level picker */
.level-picker { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.level-row { display: flex; align-items: center; justify-content: center; gap: 12px; }
.level-input {
  width: 110px; text-align: center; font-size: 26px; font-weight: 700;
  padding: 8px; border-radius: 10px; border: 2px solid var(--outline);
  background: var(--surface); color: var(--on-surface);
}
.step-btn {
  border: none; border-radius: 999px; width: 44px; height: 44px;
  font-size: 18px; cursor: pointer;
  background: var(--secondary-container); color: var(--on-surface);
}
.level-hint { font-size: 13px; opacity: 0.6; }

.icon-btn {
  border: none; background: transparent; color: var(--on-surface);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 20px; padding: 8px; border-radius: 999px; cursor: pointer;
  min-width: 40px; min-height: 40px;
}
.icon-btn:active { background: color-mix(in srgb, var(--on-surface) 12%, transparent); }
#lang-btn { font-size: 15px; font-weight: 700; color: var(--primary); }

/* Board */
#board {
  flex: 1; display: flex; align-items: center; justify-content: center;
  overflow: hidden; padding: 8px;
}
.grid { display: grid; gap: 2px; }
.cell { display: flex; align-items: center; justify-content: center; }
.cell.empty { background: transparent; }
.tile {
  border-radius: 4px; font-weight: 700; line-height: 1;
}
.tile.blank { background: var(--secondary-container); }
.tile.filled { background: var(--primary-container); color: var(--on-primary-container); cursor: pointer; }
.tile.reveal { animation: pop 0.25s ease; }
@keyframes pop { 0% { transform: scale(0.6); opacity: 0.4; } 100% { transform: scale(1); opacity: 1; } }
.error { color: var(--error); font-weight: 600; }

/* Tray (wheel / next button) */
#tray {
  /* Reserve the wheel's footprint so the board doesn't resize (and the grid
     doesn't recenter) when the wheel is swapped for the Next Level button. */
  min-height: calc(var(--wheel) + 100px);
  display: flex; flex-direction: column;
  align-items: center; justify-content: flex-end;
  padding: 8px 0 calc(28px + env(safe-area-inset-bottom));
}
/* Preview + wheel live in one centered column, so the preview is positioned
   relative to the wheel (same center, fixed gap) rather than the whole tray. */
.wheel-stack { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.word-preview {
  min-height: 48px; display: flex; align-items: center;
  font-size: 30px; font-weight: 700; letter-spacing: 2px;
  padding: 6px 18px; border-radius: 10px; opacity: 0;
  background: var(--primary-container); color: var(--on-primary-container);
}
.word-preview.visible { opacity: 1; }
.word-preview.shake { animation: shake 0.4s; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  15% { transform: translateX(-12px); } 30% { transform: translateX(10px); }
  45% { transform: translateX(-7px); } 60% { transform: translateX(5px); } 80% { transform: translateX(-2px); }
}

.wheel-row { position: relative; display: flex; align-items: center; justify-content: center; }
/* Win screen reuses the wheel's footprint so Next Level lands where the wheel was
   and the bonus button keeps its in-play position. */
.win-row { width: var(--wheel); height: var(--wheel); position: relative; z-index: 3; }
/* Once solved, lock the top-bar controls (level + language) until Next Level. */
#app.won #level-label,
#app.won #lang-btn { pointer-events: none; opacity: 0.4; }
/* Shuffle hugs the wheel's right edge, vertically centered on the wheel. Kept a
   sibling of .wheel (not a child) so its taps don't bubble into the drag handler. */
.wheel-row .shuffle {
  position: absolute; width: 44px; height: 44px;
  left: calc(100% + 16px); top: 50%; transform: translateY(-50%);
  background: var(--secondary-container); color: var(--on-surface);
}
.wheel {
  --radius: calc(var(--wheel) * 0.34);   /* must match geom() in app.js */
  --letter: calc(var(--wheel) * 0.27);
  position: relative; width: var(--wheel); height: var(--wheel); flex: 0 0 auto;
  touch-action: none;
}
.wheel::before {
  content: ""; position: absolute; inset: calc(var(--wheel) * 0.05); border-radius: 50%;
  background: color-mix(in srgb, var(--secondary-container) 50%, transparent);
}
.wheel-lines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.wheel-lines line { stroke: var(--primary); stroke-width: 7; stroke-linecap: round; }
.letter {
  position: absolute; top: 50%; left: 50%;
  width: var(--letter); height: var(--letter); margin: calc(var(--letter) / -2);
  border: none; border-radius: 50%;
  background: var(--secondary); color: var(--on-primary);
  font-size: calc(var(--wheel) * 0.13); font-weight: 700; cursor: pointer;
  transform: translate(calc(cos(var(--ang) * 1rad) * var(--radius)), calc(sin(var(--ang) * 1rad) * var(--radius)));
  transition: background 0.1s;
}
.letter.active { background: var(--primary); }
.wheel.shuffling .letter { transition: background 0.1s, transform 0.34s cubic-bezier(.2,.7,.25,1); }
.shuffle { font-size: 24px; }

/* Letters flying from the wheel into the grid on a correct word. */
.fly-layer { position: fixed; inset: 0; pointer-events: none; z-index: 55; }
.fly-letter {
  position: fixed; display: flex; align-items: center; justify-content: center;
  border-radius: 4px; background: var(--primary-container); color: var(--on-primary-container);
  font-weight: 700; will-change: transform; box-sizing: border-box;
}

.primary-btn {
  border: none; border-radius: 999px; padding: 14px 28px;
  background: var(--primary); color: var(--on-primary);
  font-size: 17px; font-weight: 600; cursor: pointer;
}
.primary-btn:disabled { opacity: 0.6; }

#bonus-btn {
  /* Corner fallback (win screen, before first wheel render). */
  position: absolute; width: 44px; height: 44px;
  left: 16px; bottom: calc(20px + env(safe-area-inset-bottom));
  background: var(--secondary-container); font-size: 22px;
}
/* In play it sits inside the wheel-row: mirror of the shuffle button, hugging the
   wheel's left edge (16px gap), vertically centered. margin-top (not transform)
   does the centering so transform is free for the bump/shake animations. */
.wheel-row > #bonus-btn {
  left: auto; bottom: auto;
  right: calc(100% + 16px); top: 50%; margin-top: -22px;
}
#bonus-btn:disabled { opacity: 0.4; }
#bonus-btn.shake { animation: shake 0.4s; }
#bonus-btn.bump { animation: bump 0.35s ease; }
@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.28); }
  100% { transform: scale(1); }
}

/* Dialogs */
.dialog-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 24px;
  animation: backdropIn 0.18s ease;
}
@keyframes backdropIn { from { opacity: 0; } to { opacity: 1; } }
.dialog {
  background: var(--surface); border-radius: 24px; padding: 24px;
  max-width: 360px; width: 100%; max-height: 80dvh; overflow: auto;
  display: flex; flex-direction: column; gap: 12px;
  animation: dialogIn 0.26s cubic-bezier(.2,.7,.25,1);
}
@keyframes dialogIn {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: none; }
}
.dialog h2 { margin: 0; font-size: 22px; }
.dialog-body { white-space: pre-wrap; line-height: 1.5; }
.dialog .primary-btn { align-self: flex-end; }
.word-list, .ach-list { display: flex; flex-direction: column; }
.word-item { padding: 10px 4px; cursor: pointer; border-radius: 6px; }
.word-item:active { background: var(--secondary-container); }
.word-item.current { font-weight: 700; }

.ach-row { padding: 10px 0; opacity: 0.55; border-bottom: 1px solid var(--outline); }
.ach-row.unlocked { opacity: 1; }
.ach-name { font-weight: 600; }
.ach-desc { font-size: 13px; opacity: 0.8; }
.ach-bar { margin-top: 6px; background: var(--secondary-container); border-radius: 6px; height: 18px; overflow: hidden; position: relative; }
.ach-bar span { display: block; height: 100%; background: var(--primary); color: var(--on-primary); font-size: 11px; text-align: right; padding-right: 6px; line-height: 18px; min-width: fit-content; white-space: nowrap; }

/* Achievement toast */
.ach-toast {
  position: fixed; top: calc(12px + env(safe-area-inset-top)); left: 50%;
  transform: translate(-50%, -150%); transition: transform 0.3s ease;
  background: var(--primary); color: var(--on-primary);
  padding: 12px 20px; border-radius: 16px; z-index: 60; max-width: 90%;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}
.ach-toast.show { transform: translate(-50%, 0); }
.ach-toast .ach-title { font-weight: 700; }
.ach-toast .ach-desc { font-size: 13px; opacity: 0.9; }
