/* Base = dark mode neutrals + default accent. Two independent axes:
   data-mode (light|dark) sets the neutrals; data-theme sets the accent. */
:root {
  --bg: #0f1115;
  --panel: #171a21;
  --line: #262b35;
  --text: #e7e9ee;
  --dim: #6b7280;
  --correct: #3ddc97;
  --accent: #7c9cff;
  --cw: 44px; /* wheel/cell width; shrinks on mobile */
}

:root[data-mode="light"] {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --line: #e2e5ec;
  --text: #1a1d24;
  --dim: #8a90a0;
  --correct: #16a06a;
}

/* Accent-only themes; each works in both light and dark mode. */
:root[data-theme="indigo"] { --accent: #7c9cff; }
:root[data-theme="blue"]   { --accent: #4f6bed; }
:root[data-theme="cyan"]   { --accent: #06b6d4; }
:root[data-theme="teal"]   { --accent: #14b8a6; }
:root[data-theme="green"]  { --accent: #22c55e; }
:root[data-theme="lime"]   { --accent: #65a30d; }
:root[data-theme="amber"]  { --accent: #f59e0b; }
:root[data-theme="orange"] { --accent: #f97316; }
:root[data-theme="red"]     { --accent: #ef4444; }
:root[data-theme="rose"]    { --accent: #f43f5e; }
:root[data-theme="coral"]   { --accent: #ff7a5c; }
:root[data-theme="pink"]    { --accent: #ec4899; }
:root[data-theme="fuchsia"] { --accent: #d946ef; }
:root[data-theme="purple"]  { --accent: #a855f7; }
:root[data-theme="violet"]  { --accent: #8b5cf6; }
:root[data-theme="sky"]     { --accent: #0ea5e9; }
:root[data-theme="emerald"] { --accent: #10b981; }
:root[data-theme="gold"]    { --accent: #d4af37; }
:root[data-theme="steel"]   { --accent: #64748b; }
:root[data-theme="mono"]    { --accent: #94a3b8; }

* { box-sizing: border-box; }

/* Remove the 300ms tap delay and double-tap zoom on interactive controls. */
button, input, .wheel, .arrow { touch-action: manipulation; }

/* Visible keyboard focus on every interactive control (accessibility). */
button:focus-visible,
input:focus-visible,
a:focus-visible,
.wheel:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  padding: 2rem 1rem 3rem;
  padding-bottom: max(3rem, env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
}

header { text-align: center; }

h1 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.subtitle { margin: 0.35rem 0 0; color: var(--dim); font-size: 0.85rem; }

#stats {
  display: flex;
  gap: 0.5rem;
}
.stat {
  min-width: 68px;
  padding: 0.5rem 0.75rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  text-align: center;
}
.stat span {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stat.warn span { color: #ff5a5a; }
.stat.warn { border-color: #ff5a5a; }
.stat label {
  display: block;
  font-size: 0.65rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--dim);
  margin-top: 0.15rem;
}

#unlock {
  background: var(--accent);
  color: #0f1115;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1.6rem;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: transform 0.1s, filter 0.15s, opacity 0.15s;
}
#unlock:hover { filter: brightness(1.1); }
#unlock:active { transform: scale(0.96); }
#unlock:disabled { opacity: 0.4; cursor: default; }
#unlock[hidden] { display: none; }
#unlock .hint {
  font-size: 0.7rem;
  font-weight: 600;
  opacity: 0.6;
  margin-left: 0.4rem;
}

#board {
  display: flex;
  gap: 0.5rem;
  flex-wrap: nowrap;
  justify-content: center;
  padding: 1rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  position: relative;
  max-width: 100%;
  overflow-x: auto;
}

/* solution line across the center row */
#board::before {
  content: "";
  position: absolute;
  left: 0.75rem;
  right: 0.75rem;
  top: 50%;
  height: 46px;
  transform: translateY(-50%);
  border-top: 2px solid var(--accent);
  border-bottom: 2px solid var(--accent);
  opacity: 0.25;
  pointer-events: none;
}

.wheel {
  display: flex;
  flex-direction: column;
  align-items: center;
  border-radius: 10px;
  padding: 0.25rem;
  cursor: pointer;
  transition: background 0.15s;
}

.wheel.selected {
  background: var(--line);
  box-shadow: inset 0 0 0 2px var(--accent);
}
.wheel.correct {
  box-shadow: inset 0 0 0 2px var(--correct);
}
.wheel.correct .viewport { cursor: default; }

/* 5-row window; the strip inside holds 7 rows and is offset up by one row
   so the center letter sits in the middle. */
.viewport {
  height: 230px;
  overflow: hidden;
  cursor: grab;
  mask-image: linear-gradient(transparent, #000 20%, #000 80%, transparent);
  -webkit-mask-image: linear-gradient(transparent, #000 20%, #000 80%, transparent);
}
.viewport.dragging { cursor: grabbing; }

.cells {
  display: flex;
  flex-direction: column;
  align-items: center;
  transform: translateY(-46px);
  user-select: none;
  -webkit-user-select: none;
}

@keyframes slideUp {
  from { transform: translateY(0); }
  to { transform: translateY(-46px); }
}
@keyframes slideDown {
  from { transform: translateY(-92px); }
  to { transform: translateY(-46px); }
}
.cells.slide-up { animation: slideUp 0.16s cubic-bezier(0.22, 1, 0.36, 1); }
.cells.slide-down { animation: slideDown 0.16s cubic-bezier(0.22, 1, 0.36, 1); }

.cell {
  width: var(--cw);
  height: 46px;
  flex: 0 0 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 600;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  transition: color 0.2s, transform 0.2s;
}

.cell.dim { color: var(--dim); opacity: 0.5; font-size: 1.15rem; }
.cell.center { color: var(--text); transform: scale(1.06); }

/* Per-letter reveal on Unlock: green pops and stays, red flashes and fades. */
@keyframes flashCorrect {
  0% { transform: scale(1.06); }
  35% { transform: translateY(-4px) scale(1.28); color: var(--correct); }
  100% { transform: scale(1.06); color: var(--correct); }
}
@keyframes flashWrong {
  0% { transform: scale(1.06); color: var(--text); }
  25% { transform: translateY(-4px) scale(1.2); color: #ff5a5a; }
  55% { color: #ff5a5a; }
  100% { transform: scale(1.06); color: var(--text); }
}
.cell.flash-correct { animation: flashCorrect 0.55s cubic-bezier(0.22, 1, 0.36, 1) both; }
.cell.flash-wrong { animation: flashWrong 0.6s ease; }

.wheel.correct .cell.center { color: var(--correct); transform: scale(1.12); }

.arrow {
  background: none;
  border: none;
  color: var(--dim);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.2rem 0.6rem;
  border-radius: 6px;
}
.arrow:hover:not(:disabled) { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.arrow:disabled { cursor: default; opacity: 0.55; }
.wheel.correct .arrow:disabled { color: var(--correct); opacity: 0.8; }

#status { color: var(--dim); font-size: 0.9rem; }

#win {
  opacity: 0;
  transform: translateY(10px) scale(0.96);
  transition: opacity 0.3s ease, transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
  text-align: center;
  background: rgba(61, 220, 151, 0.1);
  border: 1px solid var(--correct);
  color: var(--text);
  padding: 0.85rem 1.25rem;
  border-radius: 10px;
  max-width: 420px;
  pointer-events: none;
}
#win.show { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; }
/* Collapse the win panel entirely while a round is in progress, so it leaves no
   empty gap below the board until it actually appears on finish. */
#win:not(.show) { display: none; }
#win.fail {
  background: rgba(255, 90, 90, 0.08);
  border-color: #ff5a5a;
}
.countdown {
  margin-top: 0.75rem;
  font-size: 0.8rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
#win .word { display: block; font-size: 1.4rem; letter-spacing: 0.08em; }
#win .detail { color: var(--dim); font-size: 0.85rem; margin-top: 0.35rem; }
.definition-text {
  margin: 0.6rem 0 0;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--text);
  opacity: 0.85;
  max-width: 360px;
}
#share {
  margin-top: 0.75rem;
  background: var(--correct);
  color: #06281c;
  border: none;
  border-radius: 8px;
  padding: 0.55rem 1.2rem;
  font-weight: 700;
  cursor: pointer;
}
#share:active { transform: scale(0.96); }

.help {
  color: var(--dim);
  font-size: 0.8rem;
  text-align: center;
  max-width: 460px;
  line-height: 1.5;
}
kbd {
  background: var(--line);
  border-radius: 4px;
  padding: 0.05rem 0.35rem;
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
}

/* ---------- toolbar ---------- */
#toolbar {
  position: fixed;
  top: max(0.75rem, env(safe-area-inset-top));
  right: max(0.75rem, env(safe-area-inset-right));
  display: flex;
  gap: 0.4rem;
  z-index: 10;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }

.menu-wrap { position: relative; }
#theme-menu {
  position: absolute;
  top: 46px;
  right: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  z-index: 20;
  min-width: 160px;
  max-height: 70vh;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}
#theme-menu[hidden] { display: none; }
.swatch {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: transparent;
  border: none;
  border-radius: 7px;
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}
.swatch:hover { background: var(--line); }
.swatch.active { outline: 2px solid var(--accent); }
.sw-dot {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid transparent;
  flex: 0 0 auto;
}
.sw-label { margin-left: 2px; }

/* ---------- controls ---------- */
#controls {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}
.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7rem 1.2rem;
  font-size: 0.9rem;
  cursor: pointer;
}
.secondary:hover { border-color: var(--accent); }
.secondary:disabled { opacity: 0.4; cursor: default; border-color: var(--line); }
.secondary[hidden] { display: none; }

/* ---------- controls legend ---------- */
#legend {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0.4rem 0.6rem;
  color: var(--dim);
  font-size: 0.8rem;
}
#legend kbd { margin: 0 1px; }
#legend .sep { opacity: 0.5; }
@media (max-width: 560px) {
  #legend .sep { display: none; }
  #legend { flex-direction: column; gap: 0.3rem; }
}

/* ---------- history ---------- */
#history {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}
.hist-row { display: flex; gap: 4px; }
.hist-cell {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  box-sizing: border-box;
  display: grid;
  place-items: center;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.72rem;
  font-weight: 700;
}
.hist-cell.hit { background: var(--correct); color: var(--bg); border: 1px solid var(--correct); }
.hist-cell.miss { background: transparent; color: var(--dim); border: 1px solid var(--line); }

/* ---------- leaderboard ---------- */
#leaderboard {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1rem 1.1rem 1.25rem;
}
#leaderboard h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--dim);
}
#lb-name {
  min-width: 0;
  padding: 0.6rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  /* 16px keeps iOS Safari from auto-zooming when the field is focused. */
  font-size: 16px;
}
#lb-name::placeholder { color: var(--dim); }
#lb-name:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 30%, transparent);
}
#lb-tabs {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
}
.lb-tab {
  flex: 1;
  padding: 0.45rem 0.3rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--dim);
  font-size: 0.78rem;
  cursor: pointer;
}
.lb-tab.active {
  color: var(--text);
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}
#lb-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.lb-row {
  display: grid;
  grid-template-columns: 2.2rem 1fr auto;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.55rem;
  border-radius: 8px;
  background: var(--bg);
  font-size: 0.9rem;
}
.lb-row.me { border: 1px solid var(--accent); }
.lb-rank { font-weight: 700; }
.lb-name-cell { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-val { font-variant-numeric: tabular-nums; color: var(--dim); }
.lb-empty { color: var(--dim); font-size: 0.85rem; padding: 0.5rem; }
.lb-me { margin-top: 0.75rem; font-size: 0.82rem; color: var(--text); line-height: 1.5; }
#lb-note { margin-top: 0.5rem; }

/* ---------- how-to modal ---------- */
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.8rem;
  cursor: pointer;
  text-decoration: underline;
  margin-top: 0.5rem;
  padding: 0.35rem;
}
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.5rem;
  max-width: 400px;
  width: 100%;
  color: var(--text);
}
.modal-card h2 { margin: 0 0 1rem; font-size: 1.15rem; }
.modal-card ol { margin: 0 0 1.25rem; padding-left: 1.2rem; line-height: 1.6; font-size: 0.9rem; }
.modal-card li { margin-bottom: 0.5rem; }
.modal-card .k {
  display: inline-block;
  background: var(--line);
  border-radius: 4px;
  padding: 0 0.35rem;
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  margin: 0 1px;
}
#howto-close {
  width: 100%;
  background: var(--accent);
  color: #0f1115;
  border: none;
  border-radius: 10px;
  padding: 0.8rem;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
}

/* ---------- reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .cells.slide-up, .cells.slide-down { animation: none; }
  .cell, #win, #unlock { transition: none; }
}

/* ---------- mobile ---------- */
@media (max-width: 560px) {
  body { padding: 3.25rem 0.5rem 3rem; gap: 0.9rem; }
  h1 { font-size: 1.25rem; }
  :root { --cw: 32px; }
  #toolbar {
    top: max(0.5rem, env(safe-area-inset-top));
    right: max(0.5rem, env(safe-area-inset-right));
  }
  .icon-btn { width: 40px; height: 40px; font-size: 1rem; }
  #stats { flex-wrap: nowrap; justify-content: center; gap: 0.35rem; width: 100%; }
  .stat { min-width: 0; flex: 1 1 0; padding: 0.4rem 0.2rem; }
  .stat span { font-size: 1.1rem; }
  #board {
    max-width: 100%;
    overflow-x: auto;
    justify-content: flex-start;
    gap: 0.25rem;
    padding: 0.6rem 0.4rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #board::-webkit-scrollbar { display: none; }
  .wheel { padding: 0.15rem; }
  .cell { font-size: 1.15rem; }
  .cell.dim { font-size: 0.9rem; }
  /* Bigger touch targets for the scroll arrows. */
  .arrow { padding: 0.55rem 0.4rem; font-size: 0.95rem; min-height: 42px; }
  #controls { width: 100%; }
  #unlock { flex: 1 1 100%; padding: 0.9rem; min-height: 48px; }
  .secondary { flex: 1 1 40%; min-height: 44px; }
  #leaderboard { border-radius: 12px; padding: 0.9rem; }
  .subtitle { font-size: 0.78rem; }
  /* Comfortable tap targets on the leaderboard tabs. */
  .lb-tab { min-height: 44px; display: flex; align-items: center; justify-content: center; }
  #lb-tabs { gap: 0.4rem; }
}

@media (max-width: 380px) {
  :root { --cw: 28px; }
  .cell { font-size: 1rem; }
  .cell.dim { font-size: 0.8rem; }
}

/* ---------- stats modal ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin-bottom: 1.1rem;
}
.stat-tile {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem 0.25rem;
  text-align: center;
}
.stat-tile span {
  display: block;
  font-size: 1.4rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stat-tile label {
  display: block;
  font-size: 0.6rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--dim);
  margin-top: 0.15rem;
}
.hist-title { margin: 0 0 0.6rem; font-size: 0.9rem; color: var(--dim); font-weight: 600; }
.hist { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 1.1rem; }
.hist-bar-row { display: flex; align-items: center; gap: 0.5rem; }
.hb-label {
  width: 1.1rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  color: var(--dim);
}
.hb-track { flex: 1; }
.hb-fill {
  display: inline-block;
  background: var(--accent);
  color: #0f1115;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.1rem 0.4rem;
  border-radius: 5px;
  min-width: 1.2rem;
  text-align: right;
  box-sizing: border-box;
}

/* ---------- first-run coach hint ---------- */
.coach {
  position: fixed;
  left: 50%;
  bottom: max(1.1rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  max-width: min(92vw, 440px);
  padding: 0.7rem 0.85rem;
  background: var(--accent);
  color: #0f1115;
  border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  font-size: 0.85rem;
  line-height: 1.35;
  animation: coach-in 0.35s ease;
}
.coach[hidden] { display: none; }
.coach-text strong { font-weight: 800; }
.coach-x {
  flex-shrink: 0;
  background: rgba(15, 17, 21, 0.15);
  color: #0f1115;
  border: none;
  border-radius: 999px;
  width: 22px;
  height: 22px;
  font-size: 0.75rem;
  cursor: pointer;
  line-height: 1;
}
.coach-x:hover { background: rgba(15, 17, 21, 0.3); }
@keyframes coach-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- footer ---------- */
.site-footer {
  margin-top: 1.5rem;
  padding-top: 1rem;
  color: var(--dim);
  font-size: 0.8rem;
  text-align: center;
}
.site-footer a { color: var(--dim); text-decoration: underline; }
.site-footer a:hover { color: var(--accent); }
.site-footer .sep { margin: 0 0.5rem; opacity: 0.6; }

@media (prefers-reduced-motion: reduce) {
  .coach { animation: none; }
}

@media (max-width: 480px) {
  .stat-tile span { font-size: 1.15rem; }
}

/* ---------- page load screen ---------- */
#app-loading {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  background: var(--bg);
  transition: opacity 0.35s ease;
}
#app-loading img { border-radius: 22%; }
#app-loading.ready { opacity: 0; pointer-events: none; }
.app-loading-spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: app-spin 0.7s linear infinite;
}
@keyframes app-spin { to { transform: rotate(360deg); } }

/* Unlock button while a server grade is in flight. */
#unlock.checking { opacity: 0.7; cursor: progress; }

@media (prefers-reduced-motion: reduce) {
  .app-loading-spinner { animation-duration: 1.6s; }
}

/* ---------- stats caption + win-screen name ---------- */
.hist-caption {
  color: var(--dim);
  font-size: 0.75rem;
  line-height: 1.45;
  margin: 0 0 0.85rem;
}
/* ---------- name field with explicit Save (mobile-friendly) ---------- */
.name-field {
  display: flex;
  gap: 0.4rem;
  align-items: stretch;
  width: 100%;
}
.name-field input { flex: 1; margin-bottom: 0; }
.name-field button {
  flex-shrink: 0;
  padding: 0 1rem;
  min-height: 44px;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 8px;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
}
.name-field button:active { transform: scale(0.97); }

/* Subtle cue: a wheel moved since the last unlock tints its current letter. */
.wheel.changed .cell.center { color: var(--accent); }

/* Leaderboard name row spacing (name input + Save sit flush on one line). */
#lb-name-row { margin-bottom: 0.75rem; }

#leaderboard[hidden] { display: none; }
