/* ==========================================================================
   Driftword — modern arcade
   Centered, glassy, themeable. Mobile-first. Plain CSS, no build step.
   ========================================================================== */

:root {
  /* structural (theme-independent) */
  --bg: #0d1018;
  --bg-2: #070910;
  --surface: rgba(255, 255, 255, 0.045);
  --surface-2: rgba(255, 255, 255, 0.08);
  --edge: rgba(255, 255, 255, 0.09);
  --edge-strong: rgba(255, 255, 255, 0.16);
  --text: #f5f7fb;
  --text-dim: #9aa3b8;
  --switch-off: rgba(255, 255, 255, 0.16);
  --backlink-bg: rgba(0, 0, 0, 0.28);
  --shadow-soft: 0 10px 30px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --radius-lg: 22px;
  --font-display: "Baloo 2", system-ui, sans-serif;
  --font-body: "Fredoka", system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);

  /* accent tokens — overridden per theme below. default = Aurora */
  --accent: #22d3ee;
  --accent-2: #38bdf8;
  --tile-active: #22d3ee;
  --good: #34d399;
  --bad: #fb7185;
  --on-accent: #04121a;
  --grad-1: rgba(34, 211, 238, 0.20);
  --grad-2: rgba(56, 189, 248, 0.16);
  --grad-3: rgba(45, 212, 191, 0.18);
}

/* ---- themes ------------------------------------------------------------- */

:root[data-theme="aurora"] {
  --accent: #22d3ee; --accent-2: #38bdf8; --tile-active: #22d3ee;
  --good: #34d399; --bad: #fb7185; --on-accent: #04121a;
  --bg: #0b1016; --bg-2: #060a10;
  --grad-1: rgba(34, 211, 238, 0.20); --grad-2: rgba(56, 189, 248, 0.16); --grad-3: rgba(45, 212, 191, 0.20);
}
:root[data-theme="sunset"] {
  --accent: #fb923c; --accent-2: #f43f5e; --tile-active: #fb923c;
  --good: #fbbf24; --bad: #f43f5e; --on-accent: #1a0a04;
  --bg: #140b0d; --bg-2: #0b0608;
  --grad-1: rgba(251, 146, 60, 0.20); --grad-2: rgba(244, 63, 94, 0.16); --grad-3: rgba(251, 191, 36, 0.16);
}
:root[data-theme="grape"] {
  --accent: #a78bfa; --accent-2: #e879f9; --tile-active: #a78bfa;
  --good: #34d399; --bad: #fb7185; --on-accent: #120a1e;
  --bg: #0f0b18; --bg-2: #08060f;
  --grad-1: rgba(167, 139, 250, 0.22); --grad-2: rgba(232, 121, 249, 0.16); --grad-3: rgba(99, 102, 241, 0.18);
}
:root[data-theme="mint"] {
  --accent: #2dd4bf; --accent-2: #4ade80; --tile-active: #2dd4bf;
  --good: #4ade80; --bad: #fb7185; --on-accent: #04140f;
  --bg: #0a1211; --bg-2: #050b0a;
  --grad-1: rgba(45, 212, 191, 0.20); --grad-2: rgba(74, 222, 128, 0.16); --grad-3: rgba(20, 184, 166, 0.18);
}
:root[data-theme="rose"] {
  --accent: #fb7185; --accent-2: #f472b6; --tile-active: #fb7185;
  --good: #34d399; --bad: #f43f5e; --on-accent: #1a060c;
  --bg: #140a10; --bg-2: #0b050a;
  --grad-1: rgba(251, 113, 133, 0.20); --grad-2: rgba(244, 114, 182, 0.16); --grad-3: rgba(236, 72, 153, 0.16);
}
:root[data-theme="mono"] {
  --accent: #60a5fa; --accent-2: #818cf8; --tile-active: #60a5fa;
  --good: #34d399; --bad: #fb7185; --on-accent: #05101f;
  --bg: #0c0e12; --bg-2: #06070a;
  --grad-1: rgba(96, 165, 250, 0.16); --grad-2: rgba(129, 140, 248, 0.12); --grad-3: rgba(148, 163, 184, 0.10);
}

/* Light surface mode — overrides shared surface tokens on top of any accent
   theme. MUST come after the theme blocks so it wins the --bg/--bg-2 cascade. */
:root[data-mode="light"] {
  --bg: #eef1fb; --bg-2: #f7f5f0;
  --surface: rgba(18, 22, 48, 0.05);
  --surface-2: rgba(18, 22, 48, 0.09);
  --edge: rgba(18, 22, 48, 0.12);
  --edge-strong: rgba(18, 22, 48, 0.22);
  --text: #16182f;
  --text-dim: #59608a;
  --switch-off: rgba(18, 22, 48, 0.20);
  --backlink-bg: rgba(255, 255, 255, 0.66);
  --shadow-soft: 0 10px 30px rgba(20, 22, 46, 0.14);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  position: relative;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { font-family: var(--font-display); margin: 0; }
p { margin: 0; }
button { font-family: var(--font-body); -webkit-user-select: none; user-select: none; }

/* ---- animated background ------------------------------------------------ */

.bg-gradient {
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(55vmax 55vmax at 18% 12%, var(--grad-1), transparent 62%),
    radial-gradient(65vmax 65vmax at 84% 26%, var(--grad-2), transparent 62%),
    radial-gradient(75vmax 75vmax at 50% 108%, var(--grad-3), transparent 66%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
  background-size: 130% 130%, 130% 130%, 130% 130%, 100% 100%;
  animation: drift 24s ease-in-out infinite alternate;
  transition: background 0.5s ease;
}

@keyframes drift {
  0% { background-position: 0% 0%, 100% 0%, 50% 100%, 0 0; }
  50% { background-position: 24% 16%, 74% 26%, 44% 84%, 0 0; }
  100% { background-position: 10% 34%, 90% 12%, 58% 92%, 0 0; }
}

#particles, #confetti {
  position: fixed;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#confetti { z-index: 50; }

/* ---- back link ---------------------------------------------------------- */

.back-link {
  position: fixed;
  top: max(12px, var(--safe-t));
  left: max(12px, var(--safe-l));
  z-index: 100;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  background: var(--backlink-bg);
  border: 1px solid var(--edge);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: background 0.2s ease, transform 0.15s ease;
}
.back-link:active { transform: scale(0.95); }

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

.screen { display: none; }

.screen.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: screenIn 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes screenIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- setup screen: centered card ---------------------------------------- */

#screen-setup.active { padding: 0; }

.setup-card {
  margin: auto;                 /* vertical + horizontal centering, scroll-safe */
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 22px;
  padding: calc(64px + var(--safe-t)) max(20px, var(--safe-l))
           calc(32px + var(--safe-b)) max(20px, var(--safe-r));
}

.logo {
  font-size: clamp(2.6rem, 11vw, 3.4rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1;
  background: linear-gradient(120deg, var(--accent) 10%, var(--accent-2) 90%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 18px color-mix(in srgb, var(--accent) 35%, transparent));
}
.logo b { font-weight: 800; }

.tagline {
  margin-top: -14px;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.setup-group { width: 100%; }

.setup-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.name-inputs {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.name-inputs input {
  min-height: 48px;
  padding: 0 16px;
  border-radius: 14px;
  border: 1px solid var(--edge);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  text-align: center;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.name-inputs input:focus {
  border-color: var(--accent);
  background: var(--surface-2);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 26%, transparent);
}

.muted {
  color: var(--text-dim);
  font-size: 13px;
  min-height: 1em;
  text-align: center;
}

.best-note {
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  min-height: 1em;
  text-align: center;
  letter-spacing: 0.02em;
}
.best-note b { color: var(--accent); }

.results-best {
  text-align: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  margin: 14px 0 2px;
  min-height: 1em;
}
.results-best .new-best {
  color: var(--good);
}

/* ---- chip rows (players / board / time) --------------------------------- */

.pill-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.pill {
  min-height: 52px;
  min-width: 52px;
  padding: 0 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  border: 1px solid var(--edge);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 16px;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.18s ease,
              border-color 0.18s ease, box-shadow 0.18s ease, color 0.18s ease;
}
.pill:active { transform: scale(0.93); }

.pill.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: var(--on-accent);
  box-shadow: 0 8px 22px color-mix(in srgb, var(--accent) 40%, transparent);
}

/* ---- switch rows (toggles) ---------------------------------------------- */

.settings-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 54px;
  padding: 0 18px;
  border-radius: 14px;
  border: 1px solid var(--edge);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.12s ease;
}
.switch-row:active { transform: scale(0.985); }
.switch-row[aria-pressed="true"] { border-color: color-mix(in srgb, var(--accent) 45%, var(--edge)); }

.switch {
  flex: none;
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: var(--switch-off);
  position: relative;
  transition: background 0.2s ease;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.switch-row[aria-pressed="true"] .switch {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.switch-row[aria-pressed="true"] .switch::after { transform: translateX(20px); }

/* ---- theme swatches ----------------------------------------------------- */

.theme-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.theme-swatch {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  padding: 0;
  cursor: pointer;
  position: relative;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.12), var(--shadow-soft);
  transition: transform 0.15s ease;
}
.theme-swatch:active { transform: scale(0.9); }
.theme-swatch.active {
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--text);
}
.theme-swatch.active::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

/* ---- buttons ------------------------------------------------------------ */

.btn-primary, .btn-secondary {
  min-height: 54px;
  padding: 0 34px;
  border-radius: 16px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, filter 0.2s ease;
}

.btn-primary {
  width: 100%;
  max-width: 260px;
  border: none;
  color: var(--on-accent);
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 10px 28px color-mix(in srgb, var(--accent) 40%, transparent);
}
.btn-primary:active { transform: scale(0.96); filter: brightness(1.06); }
.btn-primary:disabled { opacity: 0.5; filter: grayscale(0.3); box-shadow: none; cursor: default; }

.btn-secondary {
  border: 1px solid var(--edge-strong);
  background: var(--surface);
  color: var(--text);
}
.btn-secondary:active { transform: scale(0.96); background: var(--surface-2); }

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--edge);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, background 0.15s ease;
}
.icon-btn:active { transform: scale(0.92); }

/* ---- handoff screen ----------------------------------------------------- */

#screen-handoff.active { justify-content: center; text-align: center; gap: 8px; padding: 24px; }

.pass-label {
  color: var(--text-dim);
  font-size: 14px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
#handoff-name {
  font-size: clamp(2.2rem, 10vw, 3.2rem);
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin: 8px 0 22px;
}

/* ---- play screen: HUD --------------------------------------------------- */

#screen-play.active {
  /* top padding clears the fixed corner buttons (back-link left, fullscreen
     right) so the HUD row — timer included — never sits under them */
  padding: calc(66px + var(--safe-t)) max(14px, var(--safe-l))
           calc(16px + var(--safe-b)) max(14px, var(--safe-r));
  justify-content: flex-start;
}

.hud {
  width: 100%;
  max-width: 640px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 4px;
  margin-bottom: 4px;
}

.hud-pill {
  min-height: 42px;
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--edge);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
}

#hud-timer { color: var(--accent); font-variant-numeric: tabular-nums; }
#hud-timer.low-time {
  color: var(--bad);
  animation: pulseWarn 0.6s ease-in-out infinite;
}
@keyframes pulseWarn {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}
#hud-score { color: var(--good); }

.current-word {
  min-height: 40px;
  font-family: var(--font-display);
  font-size: clamp(1.3rem, 6vw, 1.9rem);
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text);
  margin: 8px 0 12px;
  text-align: center;
  min-width: 2ch;
}

/* ---- board -------------------------------------------------------------- */

.board-wrap {
  position: relative;
  width: min(92vw, 68vh, 560px);
  aspect-ratio: 1;
  margin: 0 auto;
}

.board {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(var(--n, 4), 1fr);
  grid-template-rows: repeat(var(--n, 4), 1fr);
  --gap: clamp(7px, 2.2vw, 13px);
  gap: var(--gap);
  width: 100%;
  height: 100%;
  /* actual rendered tile edge (board width minus the gaps, divided by n); drives
     letter size so letters stay big on every grid size rather than shrinking on
     larger boards */
  --tilepx: calc((min(92vw, 68vh, 560px) - (var(--n, 4) - 1) * var(--gap)) / var(--n, 4));
}

.trail {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}
.trail polyline {
  fill: none;
  stroke: var(--accent);
  stroke-width: 11;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 0 7px color-mix(in srgb, var(--accent) 70%, transparent));
  opacity: 0.9;
}

/* ---- tiles -------------------------------------------------------------- */

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  border-radius: clamp(12px, 3vw, 18px);
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border: 1px solid var(--edge);
  color: var(--text);
  font-family: var(--font-display);
  font-weight: 700;
  /* letter size = a fraction of the real tile, growing with grid size so 5x5/6x6
     get the biggest boost (their tiles are smallest) */
  font-size: calc(var(--tilepx, 80px) * (0.6 + (var(--n, 4) - 4) * 0.05));
  line-height: 1;
  text-transform: uppercase;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 2px 6px rgba(0, 0, 0, 0.25);
  transition: transform 0.12s ease, background 0.12s ease, color 0.12s ease, box-shadow 0.12s ease;
}

/* "Qu" tiles carry two glyphs — smaller factor so they don't overflow */
.tile.is-qu { font-size: calc(var(--tilepx, 80px) * (0.42 + (var(--n, 4) - 4) * 0.04)); }

.tile .tile-mult {
  position: absolute;
  bottom: 5px;
  right: 7px;
  font-size: 0.3em;
  font-weight: 600;
  color: var(--accent);
  opacity: 0.9;
}

.tile.sel {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: var(--on-accent);
  transform: scale(1.07);
  box-shadow: 0 8px 22px color-mix(in srgb, var(--accent) 50%, transparent);
  animation: pop 0.18s ease;
  z-index: 3;
}
.tile.good { animation: flashGood 0.5s ease; }
.tile.bad { animation: shake 0.4s ease; }

@keyframes pop {
  0% { transform: scale(0.92); }
  60% { transform: scale(1.13); }
  100% { transform: scale(1.07); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}
@keyframes flashGood {
  0% { background: var(--good); color: #04241a; box-shadow: 0 0 0 rgba(0, 0, 0, 0); }
  40% { background: var(--good); color: #04241a; box-shadow: 0 0 26px color-mix(in srgb, var(--good) 70%, transparent); }
  100% { background: linear-gradient(160deg, var(--surface-2), var(--surface)); color: var(--text); box-shadow: none; }
}

/* ---- score pop ---------------------------------------------------------- */

.score-pop {
  position: absolute;
  z-index: 60;
  pointer-events: none;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 22px;
  color: var(--good);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  white-space: nowrap;
}

/* ---- results screen ----------------------------------------------------- */

#screen-results.active {
  padding: calc(56px + var(--safe-t)) max(20px, var(--safe-l)) calc(28px + var(--safe-b)) max(20px, var(--safe-r));
  justify-content: center;
}

.results-title {
  font-size: clamp(2rem, 8vw, 2.8rem);
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 20px;
  text-align: center;
}

.results-list {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.results-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 54px;
  padding: 0 18px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--edge);
  font-weight: 600;
}
.results-list li.winner {
  border-color: transparent;
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 22%, transparent), color-mix(in srgb, var(--accent-2) 12%, transparent));
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 26%, transparent);
}

.missed { width: 100%; max-width: 480px; margin-top: 24px; }
.missed h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  margin-bottom: 12px;
  text-align: center;
}
#missed-words { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
#missed-words span {
  padding: 5px 13px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--edge);
  font-size: 13px;
  color: var(--text-dim);
}

.results-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 28px;
}

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

@media (max-width: 380px) {
  .btn-primary, .btn-secondary { padding: 0 24px; font-size: 16px; }
  .hud-pill { font-size: 14px; padding: 0 12px; }
  .pill { min-height: 48px; min-width: 48px; font-size: 15px; }
}

/* ---- reduced motion ----------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .bg-gradient { animation: none; }
  .screen.active { animation: none; }
  .tile, .pill, .switch-row, .btn-primary, .btn-secondary, .icon-btn, .back-link, .theme-swatch {
    transition: none;
  }
}
