:root {
  --bg: #121d10;
  --panel: #1a2618;
  --panel-soft: #202e1d;
  --text: #f2f5ef;
  --muted: #b6c4b1;
  --accent: #f1c84b;
  --accent-ink: #2f2506;
  --accent-2: #7ee081;
  --warn: #f3d59a;
  --danger: #f0a493;
  --key: #2a3a28;
  --key-active: #3d5638;
  --key-center: #7f6a24;
  --focus: #ffe38a;
  --radius: 16px;
  --gap: 12px;
  --hive-size: min(100%, 360px);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html {
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
  /*
   * Slightly zoomed out on small screens, so the five helper buttons sit in
   * one row and more of the board is visible without scrolling. Full size
   * from tablet width up.
   */
  font-size: clamp(13.5px, 3.5vw, 16px);
}

/* Big screen mode: larger type for a classroom TV viewed from a distance. */
html.tv-mode {
  font-size: clamp(18px, 0.6vw + 12px, 26px);
}

body {
  margin: 0;
  font-family: "Inter", "Avenir Next", "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at top, #1a2a17 0%, var(--bg) 45%);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  line-height: 1.35;
}

.skip-link {
  position: absolute;
  left: 12px;
  top: -60px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 8px 12px;
  border-radius: 10px;
  font-weight: 700;
  z-index: 50;
}

.skip-link:focus { top: 12px; }

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.app {
  width: min(760px, 100%);
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px calc(env(safe-area-inset-bottom, 0px) + 18px);
  display: grid;
  gap: var(--gap);
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "topbar"
    "stats"
    "play"
    "keyboard"
    "found";
  align-content: start;
}

.topbar { grid-area: topbar; }
.stats-card { grid-area: stats; }
.play { grid-area: play; }
.keyboard-card { grid-area: keyboard; }
.found-card { grid-area: found; }

.card {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.04), rgba(0, 0, 0, 0.08));
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius);
  padding: 12px;
  min-width: 0;
}

h1, h2, h3 {
  margin: 0;
  letter-spacing: 0.02em;
  line-height: 1.2;
}

h1 { font-size: 1.35rem; }
h2 { font-size: 1rem; }
h3 { font-size: 0.95rem; color: var(--muted); }

p { margin: 0; }

/* Top bar */
.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "brand menu"
    "playing playing";
  align-items: center;
  gap: 8px 14px;
  padding: 4px 2px;
}

.brand {
  grid-area: brand;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand .bee { font-size: 1.5rem; }

#menuButton { grid-area: menu; }

@media (min-width: 640px) {
  .topbar {
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas: "brand playing menu";
  }
}

.now-playing {
  grid-area: playing;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 0.92rem;
  min-width: 0;
}

.now-playing strong {
  color: var(--text);
  font-size: 1.05rem;
}

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: rgba(241, 200, 75, 0.16);
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Buttons and form controls */
select, button {
  font: inherit;
  border: none;
  border-radius: 12px;
  font-size: 0.95rem;
  color: var(--text);
  background: var(--panel-soft);
  padding: 10px 12px;
  min-height: 44px;
  touch-action: manipulation;
}

select {
  flex: 1;
  min-width: 0;
  border: 1px solid rgba(255, 255, 255, 0.12);
}

button {
  cursor: pointer;
  font-weight: 600;
  transition: filter 120ms ease, transform 80ms ease;
}

button:hover { filter: brightness(1.08); }
button:active { transform: translateY(1px); }
button:disabled {
  cursor: default;
  opacity: 0.45;
  filter: none;
  transform: none;
}

button.primary {
  background: #466c41;
}

button.danger {
  background: rgba(240, 164, 147, 0.14);
  color: var(--danger);
}

button.ghost {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.16);
}

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

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
}

.stat {
  background: rgba(0, 0, 0, 0.18);
  border-radius: 12px;
  padding: 10px 8px;
}

.stat .value {
  display: block;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.progress-wrap {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.progress {
  width: 100%;
  height: 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.progress > span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #7ee081, #f1c84b);
  transition: width 300ms ease;
}

.rank {
  font-weight: 700;
  color: #d3e6cf;
  text-align: center;
}

/* Play area */
.play {
  display: grid;
  gap: 10px;
}

.guess {
  text-align: center;
  font-size: clamp(1.3rem, 4vw, 2rem);
  letter-spacing: 0.18em;
  font-weight: 700;
  line-height: 1.2;
  min-height: calc(1.2em + 16px);
  border-radius: 12px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.08);
  /*
   * Inline flow, not grid: the letters and the caret are separate children,
   * and a grid container would put each on its own row.
   */
  display: block;
  overflow-wrap: anywhere;
}

.guess .caret {
  display: inline-block;
  width: 0.08em;
  min-width: 2px;
  height: 1.1em;
  margin-left: 0.1em;
  background: var(--accent);
  vertical-align: middle;
  animation: blink 1.1s steps(2, start) infinite;
}

@keyframes blink { to { visibility: hidden; } }

.guess.good { animation: glowGood 450ms ease; }
.guess.bad { animation: glowBad 350ms ease; }

@keyframes glowGood {
  0% { box-shadow: 0 0 0 rgba(126, 224, 129, 0); }
  40% { box-shadow: 0 0 18px rgba(126, 224, 129, 0.7); }
  100% { box-shadow: 0 0 0 rgba(126, 224, 129, 0); }
}

@keyframes glowBad {
  0% { box-shadow: 0 0 0 rgba(240, 164, 147, 0); transform: translateX(0); }
  25% { transform: translateX(-4px); }
  50% { box-shadow: 0 0 14px rgba(240, 164, 147, 0.5); transform: translateX(4px); }
  75% { transform: translateX(-2px); }
  100% { box-shadow: 0 0 0 rgba(240, 164, 147, 0); transform: translateX(0); }
}

/*
 * Honeycomb: a square box, each hexagon is positioned by its centre as a
 * percentage of the box so the whole hive scales with the viewport.
 */
.honeycomb {
  margin: 0 auto;
  width: var(--hive-size);
  aspect-ratio: 1 / 1;
  position: relative;
  container-type: inline-size;
}

.hex {
  position: absolute;
  width: 31.2%;
  height: 27%;
  left: var(--cx);
  top: var(--cy);
  transform: translate(-50%, -50%);
  padding: 0;
  margin: 0;
  background: none;
  border: 0;
  border-radius: 14px;
  min-height: 0;
}

.hex:active { transform: translate(-50%, -50%) scale(0.96); }

.hex > span {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
  background: var(--key-active);
  color: var(--text);
  font-size: 2rem;
  font-size: clamp(1.3rem, 11cqw, 6rem);
  font-weight: 800;
  user-select: none;
  -webkit-user-select: none;
  transition: background 120ms ease;
}

.hex:hover > span { background: #4a6a45; }
.hex.center > span { background: var(--accent); color: var(--accent-ink); }
.hex.center:hover > span { background: #f7d465; }

.hex.pos-center { --cx: 50%; --cy: 50%; }
.hex.pos-0 { --cx: 50%; --cy: 21.4%; }
.hex.pos-1 { --cx: 74.8%; --cy: 35.7%; }
.hex.pos-2 { --cx: 74.8%; --cy: 64.3%; }
.hex.pos-3 { --cx: 50%; --cy: 78.6%; }
.hex.pos-4 { --cx: 25.2%; --cy: 64.3%; }
.hex.pos-5 { --cx: 25.2%; --cy: 35.7%; }

/*
 * Five helper buttons in one row, with Enter spanning underneath. Five across
 * is the standard look at every size; only a very narrow phone drops to three.
 */
.controls {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.controls button {
  background: #30442c;
  padding-inline: 4px;
  white-space: nowrap;
}

.controls .primary {
  grid-column: 1 / -1;
  background: #466c41;
  font-size: 1.05rem;
  min-height: 50px;
}

.message {
  min-height: 2.4em;
  text-align: center;
  color: var(--muted);
  font-size: 0.98rem;
  padding: 0 4px;
}

.message.good { color: var(--accent-2); font-weight: 700; }
.message.bad { color: var(--warn); }
.message.hint { color: var(--accent); font-weight: 600; }

/* On-screen keyboard */
.keyboard {
  display: grid;
  gap: 8px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

.key {
  flex: 0 1 44px;
  min-width: 26px;
  padding: 12px 4px;
  border-radius: 10px;
  background: var(--key);
  color: var(--text);
  font-weight: 700;
  text-transform: uppercase;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.key.active { background: var(--key-active); }
.key.required { background: var(--key-center); }
.key.disabled { opacity: 0.3; }

/* Found words */
.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.card-head .count {
  color: var(--muted);
  font-size: 0.88rem;
  font-variant-numeric: tabular-nums;
}

.word-list {
  max-height: 200px;
  overflow: auto;
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  grid-auto-rows: max-content;
  align-content: start;
  gap: 6px;
  scrollbar-width: thin;
}

.word-list li {
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.2);
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.word-list li .pts {
  color: var(--muted);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.word-list li.pangram {
  background: rgba(241, 200, 75, 0.18);
  color: var(--accent);
}

.word-list li.pangram .pts { color: var(--accent); }

.word-list.missed li {
  background: rgba(255, 255, 255, 0.05);
  color: var(--muted);
}

.empty {
  color: var(--muted);
  font-size: 0.9rem;
}

.missed-section {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: grid;
  gap: 8px;
}

/* Dialogs (start screen and menu) */
.dialog {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  background: var(--panel);
  color: var(--text);
  padding: 20px;
  width: min(520px, calc(100% - 32px));
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.dialog::backdrop {
  background: rgba(7, 11, 7, 0.86);
}

/* Fallback for browsers without <dialog> support. */
.dialog:not(:modal)[open] {
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 30;
  height: fit-content;
}

.dialog h2 { font-size: 1.3rem; }

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.dialog-head button {
  min-width: 44px;
  padding: 6px 10px;
}

.dialog section {
  display: grid;
  gap: 8px;
  padding: 12px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.dialog section:first-of-type { border-top: 0; }

.dialog label.field {
  font-weight: 600;
  font-size: 0.95rem;
}

.dialog .row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.dialog .note {
  color: var(--muted);
  font-size: 0.88rem;
}

.dialog .start-form {
  display: grid;
  gap: 10px;
  text-align: center;
}

.dialog .start-form p { color: var(--muted); }

.dialog .start-form label {
  text-align: left;
  font-weight: 600;
}

.dialog .start-form select { width: 100%; }

.dialog .start-form .primary {
  width: 100%;
  min-height: 52px;
  font-size: 1.1rem;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.16);
  cursor: pointer;
  min-height: 48px;
}

.toggle input {
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.toggle .text {
  display: grid;
  gap: 2px;
}

.toggle small {
  color: var(--muted);
  font-size: 0.82rem;
}

.menu-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.dialog details summary {
  cursor: pointer;
  font-weight: 600;
  padding: 4px 0;
}

.dialog details ul {
  margin: 8px 0 0;
  padding-left: 20px;
  color: var(--muted);
  font-size: 0.92rem;
  display: grid;
  gap: 4px;
}

.dialog details kbd {
  font-family: inherit;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 0.85em;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  padding-top: 12px;
  margin-top: 4px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-decoration: none;
  opacity: 0.75;
  padding: 4px 8px;
  border-radius: 999px;
}

.badge:hover, .badge:focus-visible { opacity: 1; color: var(--text); }

.badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

/* Confetti */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 15;
}

.confetti span {
  position: absolute;
  top: -30px;
  font-size: 1.2rem;
  animation: drop 1.2s ease-out forwards;
}

@keyframes drop {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(110vh) rotate(540deg); opacity: 0; }
}

/* Small phones */
@media (max-width: 420px) {
  .controls { gap: 6px; }
  .controls button { font-size: 0.86rem; }
  .stats { font-size: 0.82rem; }
}

/* Below this the five labels no longer fit legibly, so wrap to three. */
@media (max-width: 339px) {
  .controls { grid-template-columns: repeat(3, minmax(0, 1fr)); }

  /*
   * The ten-key top row is wider than a 320px screen at the normal key size.
   * Let the keys shrink instead, so the page never scrolls sideways.
   */
  .kb-row { gap: 4px; }
  .key { min-width: 0; padding-inline: 2px; }
}

/*
 * Wide landscape screens (laptops, tablets on their side, a classroom TV):
 * the hive gets the left column and the stats, found words, and keyboard
 * stack on the right so everything fits on one screen without scrolling.
 */
@media (min-width: 900px) and (min-aspect-ratio: 5/4) {
  :root { --hive-size: min(100%, 56vh); }

  .app {
    width: min(1500px, 100%);
    min-height: 100vh;
    min-height: 100dvh;
    grid-template-columns: minmax(0, 1.15fr) minmax(320px, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "topbar topbar"
      "play stats"
      "play found"
      "play keyboard";
    align-content: stretch;
  }

  .play {
    align-content: center;
    padding: 16px;
  }

  .found-card {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .found-card .word-list {
    flex: 1 1 auto;
    max-height: none;
    min-height: 120px;
  }

  .found-card .missed-section { flex-shrink: 0; }

  .controls {
    max-width: 560px;
    margin: 0 auto;
    width: 100%;
  }

  .message { font-size: 1.05rem; }
}

/* Big screen mode: hide the on-screen keyboard and give the hive more room. */
html.tv-mode .keyboard-card { display: none; }

html.tv-mode .app {
  grid-template-areas:
    "topbar"
    "stats"
    "play"
    "found";
}

@media (min-width: 900px) and (min-aspect-ratio: 5/4) {
  html.tv-mode { --hive-size: min(100%, 60vh); }

  html.tv-mode .app {
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "topbar topbar"
      "play stats"
      "play found";
  }

  html.tv-mode .word-list {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }

  html.tv-mode .word-list li { font-size: 1rem; }
  html.tv-mode .message { font-size: 1.2rem; min-height: 2.6em; }
  html.tv-mode .guess { font-size: 2.4rem; }
}

/* Respect users who prefer less motion. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .guess .caret { animation: none; }
  .confetti { display: none; }
  button:active, .hex:active { transform: translate(0, 0); }
  .hex:active { transform: translate(-50%, -50%); }
}
