/* ============================================================================
   Word Puzzle — style.css (Game 04, plan/games/04-word-puzzle.md §4)
   Mobile-first, one-thumb layout; the level screen fits 360 px with the HUD
   and word chips visible and no scroll. The board is a CSS grid of letter
   cells; selection, found locks, wrong flashes and hint rings are classes.
   Theme follows the site's `ai-quiz-theme` setting (resolved to
   [data-theme="dark"] / "light" by an inline script in index.html).
   ========================================================================== */

:root,
[data-theme='light'] {
  --bg-a: #e8e4f3;
  --bg-b: #d4c5e8;
  --card: #ffffff;
  --card-border: rgba(148, 163, 184, 0.35);
  --text: #1e293b;
  --muted: #64748b;
  --board: #94a3b8;
  --socket: #cbd5e1;
  --tile: #f8fafc;
  --tile-hover: #eef2ff;
  --primary-a: #2563eb;
  --primary-b: #4f46e5;
  --gold: #b45309;
  --sel: rgba(37, 99, 235, 0.28);
  --wrong: #dc2626;
  --shadow: 0 10px 30px rgba(30, 27, 75, 0.12);
  --board-w: min(92vw, calc(100vh - 16.5rem), 24rem);
  --board-w: min(92vw, calc(100dvh - 16.5rem), 32rem); /* BUG-016 sweep: bigger on large screens */
}

[data-theme='dark'] {
  --bg-a: #1e1b4a;
  --bg-b: #3f1229;
  --card: #1e293b;
  --card-border: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --board: #475569;
  --socket: #334155;
  --tile: #16202f;
  --tile-hover: #1c2a3f;
  --primary-a: #3b82f6;
  --primary-b: #6366f1;
  --gold: #fbbf24;
  --sel: rgba(59, 130, 246, 0.38);
  --wrong: #ef4444;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --board-w: min(92vw, calc(100vh - 16.5rem), 24rem);
  --board-w: min(92vw, calc(100dvh - 16.5rem), 32rem); /* BUG-016 sweep: bigger on large screens */
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    'Helvetica Neue',
    Arial,
    sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, var(--bg-a), var(--bg-b)) fixed;
  -webkit-tap-highlight-color: transparent;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  max-width: none; /* BUG-016 sweep: full-screen per owner request */
  margin: 0 auto;
  padding: 0.75rem 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.back-link {
  align-self: flex-start;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  padding: 0.25rem 0.5rem;
  margin-left: -0.5rem;
  border-radius: 8px;
}

.back-link:hover,
.back-link:focus-visible {
  color: var(--text);
  background: var(--tile-hover);
}

.hidden {
  display: none !important;
}

/* ---- screens -------------------------------------------------------------- */

.screen {
  display: none;
  flex-direction: column;
  gap: 0.75rem;
  flex: 1;
  min-height: 0;
}

.screen--active {
  display: flex;
}

/* ---- menu ----------------------------------------------------------------- */

.menu-title {
  margin: 0.25rem 0 0;
  text-align: center;
  font-size: clamp(1.6rem, 7vw, 2.1rem);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 0;
  text-align: center;
  font-size: 0.875rem;
  color: var(--muted);
}

/* continue banner — jumps into the first unfinished level */

button.continue {
  width: 100%;
  border: 2px solid var(--primary-a);
  border-radius: 14px;
  padding: 0.65rem 1rem;
  font: inherit;
  color: var(--text);
  background: var(--card);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  box-shadow: var(--shadow);
  transition:
    transform 0.12s ease,
    border-color 0.12s ease;
}

button.continue:hover,
button.continue:focus-visible {
  transform: scale(0.99);
}

.continue-title {
  font-size: 1rem;
  font-weight: 700;
}

.continue-sub {
  font-size: 0.75rem;
  color: var(--muted);
}

/* theme cards: 2×2 grid, each with its three level chips */

.themes {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.625rem;
}

.theme-card {
  border: 2px solid var(--card-border);
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  padding: 0.75rem 0.5rem 0.625rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition:
    border-color 0.12s ease,
    background 0.12s ease;
}

.theme-card:hover,
.theme-card:focus-visible {
  background: var(--tile-hover);
}

.theme-card.is-complete {
  border-color: var(--gold);
}

.theme-emoji {
  font-size: 1.75rem;
  line-height: 1.1;
}

.theme-name {
  font-size: 1rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.theme-stars {
  font-size: 0.6875rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.theme-card.is-complete .theme-stars {
  color: var(--gold);
}

.level-chips {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.25rem;
  width: 100%;
  margin-top: 0.3rem;
}

.level-chip {
  border: 1px solid var(--card-border);
  border-radius: 9px;
  background: var(--tile);
  font-size: 0.6875rem;
  font-weight: 700;
  padding: 0.3rem 0.1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.05rem;
  font-variant-numeric: tabular-nums;
}

.level-chip .chip-stars {
  font-size: 0.5625rem;
  letter-spacing: -0.05em;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.625rem;
  font-size: 0.875rem;
  cursor: pointer;
  padding: 0.25rem 0;
}

.toggle-row input {
  width: 1.125rem;
  height: 1.125rem;
  accent-color: var(--primary-a);
  flex-shrink: 0;
}

/* buttons */

button.ghost {
  border: 1px solid var(--card-border);
  border-radius: 12px;
  padding: 0.625rem 0.875rem;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text);
  background: var(--card);
  cursor: pointer;
  touch-action: manipulation;
}

button.ghost:hover:not(:disabled),
button.ghost:focus-visible {
  background: var(--tile-hover);
}

button.ghost:disabled {
  opacity: 0.45;
  cursor: default;
}

button.primary {
  border: 0;
  border-radius: 14px;
  padding: 0.875rem 1rem;
  font: inherit;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--primary-a), var(--primary-b));
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(37, 99, 235, 0.35);
  transition:
    transform 0.12s ease,
    box-shadow 0.12s ease,
    filter 0.12s ease;
}

button.primary:hover:not(:disabled) {
  filter: brightness(1.06);
}

button.primary:active:not(:disabled) {
  transform: scale(0.98);
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--primary-a);
  outline-offset: 2px;
}

/* ---- level screen ----------------------------------------------------------- */

#screen-level {
  justify-content: center;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  flex-wrap: wrap; /* safety on very narrow screens */
}

.topbar .ghost {
  padding: 0.4rem 0.65rem;
}

.hud {
  display: flex;
  justify-content: center;
  gap: 0.4rem;
}

.chip {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  padding: 0.4rem 0.7rem;
  font-size: 0.875rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.chip .chip-icon {
  margin-right: 0.3rem;
}

/* the board: a square CSS grid; cells are plain buttons laid out row-major */

.board-wrap {
  position: relative;
  flex: 0 0 auto;
  align-self: center; /* overlays cover the board's exact box */
}

.board {
  --n: 8;
  width: var(--board-w);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: 2px;
  padding: 4px;
  background: var(--socket);
  border: 1px solid var(--board);
  border-radius: 18px;
  touch-action: none; /* the drag belongs to the board, never to scroll */
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  border: 0;
  padding: 0;
  border-radius: 7px;
  background: var(--tile);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  font-size: calc(var(--board-w) / var(--n) * 0.48);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: none;
}

.cell:focus-visible {
  outline: 3px solid var(--primary-a);
  outline-offset: -3px;
  z-index: 1;
}

/* live drag highlight */
.cell--sel {
  background: var(--sel);
}

/* tap-tap anchor pulse */
.cell--anchor {
  animation: anchor-pulse 1s ease-in-out infinite;
  background: var(--sel);
}

@keyframes anchor-pulse {
  0%,
  100% {
    box-shadow: inset 0 0 0 2px var(--primary-a);
  }
  50% {
    box-shadow: inset 0 0 0 5px var(--primary-a);
  }
}

/* found words lock in with their own color pill (--wc / --wc-d set inline) */
.cell--found {
  /* Neutral tint fallback when color-mix is unavailable — the found state is
     also carried by the letter color. */
  background: rgba(148, 163, 184, 0.22);
  background: color-mix(in srgb, var(--wc) 22%, transparent);
  color: var(--wc);
  font-weight: 800;
}

[data-theme='dark'] .cell--found {
  background: rgba(148, 163, 184, 0.26);
  background: color-mix(in srgb, var(--wc-d) 26%, transparent);
  color: var(--wc-d);
}

/* fresh lock-in pop (plan §2) */
.cell--pop {
  animation: pop-in 0.32s ease-out;
}

@keyframes pop-in {
  0% {
    transform: scale(0.6);
  }
  60% {
    transform: scale(1.25);
  }
  100% {
    transform: scale(1);
  }
}

/* wrong pick: red flash, 300 ms (plan §2/§7.4) */
.cell--wrong {
  animation: wrong-flash 0.3s ease;
}

@keyframes wrong-flash {
  0%,
  100% {
    background: var(--tile);
  }
  40% {
    background: var(--wrong);
    color: #fff;
  }
}

/* hint: ring the first letter of an unfound word until it is found */
.cell--hinted {
  box-shadow: inset 0 0 0 3px var(--gold);
  animation: hint-breathe 1.2s ease-in-out infinite;
}

@keyframes hint-breathe {
  0%,
  100% {
    box-shadow: inset 0 0 0 3px var(--gold);
  }
  50% {
    box-shadow: inset 0 0 0 5px var(--gold);
  }
}

/* ---- word chips ------------------------------------------------------------- */

.words {
  list-style: none;
  margin: 0;
  padding: 0.25rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: flex-start;
  gap: 0.375rem;
}

.word-chip {
  font-size: 0.8125rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--card);
  color: var(--text);
}

.word-chip--found {
  border-color: transparent;
  background: rgba(148, 163, 184, 0.16);
  background: color-mix(in srgb, var(--wc) 16%, transparent);
  color: var(--wc);
  text-decoration: line-through;
  text-decoration-thickness: 2px;
}

[data-theme='dark'] .word-chip--found {
  background: rgba(148, 163, 184, 0.2);
  background: color-mix(in srgb, var(--wc-d) 20%, transparent);
  color: var(--wc-d);
}

/* ---- result overlay ----------------------------------------------------------- */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  border-radius: 18px;
  /* Fallback first: without color-mix the scrim must still be visible. */
  background: rgba(15, 23, 42, 0.55);
  background: color-mix(in srgb, var(--bg-a) 55%, transparent);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  z-index: 2;
}

.overlay-card {
  width: 100%;
  max-width: 19rem;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1.25rem 1rem;
  text-align: center;
  animation: card-in 0.25s ease-out;
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.overlay-emoji {
  font-size: 2rem;
  line-height: 1;
}

.overlay-title {
  margin: 0.375rem 0 0.25rem;
  font-size: 1.5rem;
  font-weight: 800;
}

.result-stars {
  font-size: 1.6rem;
  letter-spacing: 0.15em;
  margin: 0.25rem 0;
}

.result-stars .star {
  filter: grayscale(1);
  opacity: 0.35;
}

.result-stars .star--on {
  filter: none;
  opacity: 1;
}

.win-stats {
  font-size: 1.125rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  margin: 0.25rem 0 0.5rem;
}

.overlay-sub {
  margin: 0 0 1rem;
  font-size: 0.875rem;
  color: var(--muted);
}

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.overlay-actions .ghost-row {
  display: flex;
  gap: 0.5rem;
}

.overlay-actions .ghost-row > button {
  flex: 1;
}

/* ---- wide screens: board left, word list right (plan §4) ---------------------- */

@media (min-width: 768px) {
  .app {
    max-width: 46rem;
  }

  #screen-level.screen--active {
    display: grid;
    grid-template-columns: auto minmax(13rem, 19rem);
    grid-template-rows: auto 1fr;
    grid-template-areas:
      'top top'
      'board words';
    justify-content: center;
    column-gap: 1.75rem;
    align-content: start;
  }

  #screen-level .topbar {
    grid-area: top;
  }

  #screen-level .board-wrap {
    grid-area: board;
    align-self: start;
  }

  #screen-level .words {
    grid-area: words;
    align-content: start;
    justify-content: flex-start;
    padding: 0.5rem;
  }
}

/* ---- toast ------------------------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translate(-50%, 200%);
  background: var(--text);
  color: var(--card);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 0.5rem 0.9rem;
  border-radius: 999px;
  transition: transform 0.25s ease;
  pointer-events: none;
  z-index: 10;
}

.toast--in {
  transform: translate(-50%, 0);
}

/* ---- small viewports ----------------------------------------------------------- */

@media (max-height: 620px) {
  :root,
  [data-theme='dark'] {
    --board-w: min(92vw, calc(100vh - 13.5rem), 24rem);
    --board-w: min(92vw, calc(100dvh - 13.5rem), 24rem);
  }

  .app {
    gap: 0.5rem;
  }

  .menu-title {
    font-size: 1.4rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell--pop,
  .cell--wrong,
  .cell--anchor,
  .cell--hinted,
  .overlay-card {
    animation: none;
  }

  .cell--anchor {
    box-shadow: inset 0 0 0 4px var(--primary-a);
  }

  .cell--hinted {
    box-shadow: inset 0 0 0 3px var(--gold);
  }

  button.primary,
  button.ghost,
  .toast {
    transition: none;
  }
}

/* ---- suggestion 03 items 1 + 3 ------------------------------------------- */

/* Live "invalid direction" drag state: the held highlight desaturates while
   the path bends — distinct from the solid live highlight and from the red
   release-time wrong-pick flash. */
.board--drag-invalid .cell--sel {
  opacity: 0.4;
  outline: 2px dashed var(--muted, #64748b);
  outline-offset: -2px;
}

/* Theme-complete momentum strip: compact chips for the other themes. */
.theme-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
  margin: 0.4rem 0 0.2rem;
}

.theme-strip.hidden {
  display: none;
}

.theme-strip-chip {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid var(--card-border, #cbd5e1);
  border-radius: 999px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.8rem;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
}

.theme-strip-chip:hover {
  border-color: currentColor;
}

.theme-strip-chip .ts-emoji {
  font-size: 0.95rem;
}

.theme-strip-chip .ts-stars {
  font-weight: 700;
  opacity: 0.75;
}
