:root {
  color-scheme: light;
  --bg: #f7f8fb;
  --bg-2: #e8edf5;
  --surface: #ffffff;
  --surface-2: #f1f4f9;
  --text: #172033;
  --muted: #5d6a7d;
  --accent: #2563eb;
  --accent-2: #ef476f;
  --line: #d7deea;
  --shadow: 0 18px 48px rgba(31, 41, 55, 0.14);
  --shadow-strong: 0 28px 80px rgba(15, 23, 42, 0.22);
  --tile: #ffffff;
  --good: #14b86a;
  --bad: #e63946;
  --theme-image: linear-gradient(135deg, #eef2ff, #ffffff 42%, #fce7f3);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow-x: hidden;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #f7f8fb;
  color: var(--text);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.22) 1px, transparent 1px),
    linear-gradient(rgba(255, 255, 255, 0.22) 1px, transparent 1px);
  background-size: 46px 46px;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.45), transparent 58%);
}

button {
  font: inherit;
}

input {
  font: inherit;
}

.app {
  width: 100%;
  min-height: 100vh;
  padding: clamp(12px, 2.4vw, 28px);
  background:
    radial-gradient(circle at 78% 12%, color-mix(in srgb, var(--accent-2) 24%, transparent), transparent 20rem),
    radial-gradient(circle at 8% 72%, color-mix(in srgb, var(--accent) 20%, transparent), transparent 22rem),
    radial-gradient(circle at 20% 10%, var(--bg-2), transparent 34rem),
    linear-gradient(135deg, var(--bg), color-mix(in srgb, var(--surface) 70%, var(--bg)) 55%, var(--bg-2));
  transition: background 220ms ease, color 220ms ease;
}

.setup-view {
  min-height: calc(100vh - clamp(24px, 4.8vw, 56px));
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 400px);
  gap: clamp(18px, 3vw, 38px);
  align-items: center;
  max-width: 1180px;
  margin: 0 auto;
}

.setup-main,
.preview-card,
.board-wrap {
  min-width: 0;
}

.setup-main {
  position: relative;
  padding: clamp(18px, 3vw, 34px);
  border: 1px solid color-mix(in srgb, var(--line) 82%, transparent);
  border-radius: 8px;
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  box-shadow: var(--shadow-strong);
  backdrop-filter: blur(16px);
}

.setup-main::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  background:
    linear-gradient(135deg, color-mix(in srgb, var(--accent) 14%, transparent), transparent 36%),
    linear-gradient(315deg, color-mix(in srgb, var(--accent-2) 16%, transparent), transparent 42%);
  pointer-events: none;
}

.setup-main > * {
  position: relative;
  z-index: 1;
}

.setup-main h1 {
  margin: 0;
  max-width: 760px;
  font-size: clamp(2.4rem, 6.4vw, 5.7rem);
  line-height: 0.96;
  letter-spacing: 0;
  background: linear-gradient(135deg, var(--text), var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.eyebrow,
.hud-label {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.eyebrow {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 12px;
  border: 1px solid color-mix(in srgb, var(--accent) 26%, var(--line));
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface) 74%, transparent);
  color: color-mix(in srgb, var(--accent) 78%, var(--text));
}

.intro {
  max-width: 680px;
  color: var(--muted);
  font-size: clamp(1rem, 2vw, 1.25rem);
  line-height: 1.55;
}

.setup-grid {
  display: grid;
  grid-template-columns: minmax(250px, 1.35fr) minmax(132px, 0.65fr) minmax(132px, 0.75fr) minmax(170px, 0.9fr);
  gap: 14px;
  margin: 24px 0 14px;
}

.control-panel,
.preview-card,
.scoreboard,
.hud,
.result-dialog {
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 84%, transparent);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.control-panel {
  margin: 0;
  min-width: 0;
  padding: 16px;
  border-radius: 8px;
  align-self: stretch;
}

.control-panel legend {
  color: var(--text);
}

legend {
  padding: 0 6px;
  font-weight: 900;
}

.player-name-panel {
  margin-top: 0;
}

.name-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.name-grid label {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.name-grid label.inactive {
  display: none;
}

.name-grid span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.name-grid input {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  color: var(--text);
  font-weight: 850;
  outline: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.48);
}

.name-grid input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

.setup-actions {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-top: 18px;
}

.theme-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
  gap: 10px;
}

.theme-choice,
.segmented button,
.primary-action,
.secondary-action,
.icon-button {
  cursor: pointer;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--surface);
  transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.theme-choice {
  position: relative;
  min-height: 86px;
  overflow: hidden;
  display: grid;
  grid-template-columns: 26px 1fr;
  gap: 10px;
  align-items: end;
  padding: 12px;
  text-align: left;
  font-weight: 850;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0.92)),
    var(--choice-image, linear-gradient(135deg, #eef2ff, #ffffff));
  background-size: cover;
  background-position: center;
  isolation: isolate;
}

.theme-choice::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 18%, transparent), transparent 46%);
}

.theme-choice.active,
.segmented button.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent), 0 16px 34px rgba(15, 23, 42, 0.16);
}

.theme-swatch {
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: 2px solid rgba(255, 255, 255, 0.82);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.18);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

#roundOptions {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.difficulty-options {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.segmented button {
  min-height: 46px;
  min-width: 0;
  font-weight: 900;
}

.difficulty-options button {
  min-height: 58px;
  line-height: 1.05;
}

.difficulty-options span {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 900;
}

.primary-action,
.secondary-action {
  min-height: 46px;
  padding: 0 20px;
  font-weight: 900;
}

.primary-action {
  min-width: min(100%, 210px);
  min-height: 54px;
  border-color: transparent;
  color: #ffffff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 18px 32px color-mix(in srgb, var(--accent) 24%, transparent);
}

.secondary-action {
  background: color-mix(in srgb, var(--surface) 84%, var(--accent) 8%);
}

.theme-choice:hover,
.segmented button:hover,
.primary-action:hover,
.secondary-action:hover,
.icon-button:hover,
.tile:hover {
  transform: translateY(-1px);
}

.preview-card {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  padding: clamp(14px, 2vw, 18px);
  box-shadow: var(--shadow-strong);
}

.preview-card > * {
  position: relative;
  z-index: 1;
}

.preview-card::before,
.board-wrap::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background-image: var(--theme-image);
  background-size: cover;
  background-position: center;
  opacity: 0.95;
}

.preview-card::after,
.board-wrap::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(circle at center, rgba(255, 255, 255, 0.72), rgba(255, 255, 255, 0.38) 42%, rgba(255, 255, 255, 0.12)),
    linear-gradient(180deg, rgba(255, 255, 255, 0.58), rgba(255, 255, 255, 0.18));
}

.preview-topline {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  font-weight: 900;
}

.icon-button {
  min-width: 54px;
  width: auto;
  height: 38px;
  padding: 0 10px;
  font-size: 0.82rem;
  font-weight: 900;
  line-height: 1;
}

.mini-board,
.board {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  gap: clamp(2px, 0.5vw, 4px);
}

.mini-board {
  width: min(100%, 360px);
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 6px;
  border-radius: 8px;
  background:
    linear-gradient(rgba(255, 255, 255, 0.74), rgba(255, 255, 255, 0.74)),
    var(--theme-image, linear-gradient(135deg, #eef2ff, #ffffff));
  background-size: cover;
  background-position: center;
  backdrop-filter: blur(2px);
}

.preview-note {
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.game-view {
  max-width: 1180px;
  margin: 0 auto;
}

.hidden {
  display: none;
}

.hud {
  position: sticky;
  top: 8px;
  z-index: 3;
  display: grid;
  grid-template-columns: 104px 76px 68px minmax(170px, 1fr) max-content max-content;
  gap: 8px;
  align-items: center;
  padding: 8px;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(15, 23, 42, 0.92), rgba(30, 41, 59, 0.86)),
    color-mix(in srgb, var(--accent) 18%, transparent);
  border-color: rgba(255, 255, 255, 0.18);
  box-shadow: 0 18px 42px rgba(15, 23, 42, 0.26);
}

.hud > div {
  min-width: 0;
}

.hud > div:not(:first-child),
.equation {
  min-height: 46px;
  padding: 6px 9px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.target {
  width: 88px;
  min-height: 66px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: #ffffff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  font-size: 2.35rem;
  font-weight: 950;
  box-shadow: 0 12px 28px color-mix(in srgb, var(--accent) 34%, transparent);
}

.hud-value {
  color: #ffffff;
  font-size: 1rem;
  font-weight: 950;
  line-height: 1;
}

.hud .hud-label {
  margin-bottom: 4px;
  color: rgba(255, 255, 255, 0.62);
  font-size: 0.62rem;
  letter-spacing: 0.08em;
}

.equation {
  display: flex;
  align-items: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.92rem;
  font-weight: 850;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud .secondary-action {
  min-height: 38px;
  padding: 0 12px;
  border-color: rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.11);
  color: #ffffff;
  font-size: 0.82rem;
  letter-spacing: 0.02em;
  box-shadow: none;
}

.hud #quitGame {
  background: rgba(239, 68, 68, 0.18);
  border-color: rgba(248, 113, 113, 0.28);
}

.play-area {
  display: grid;
  grid-template-columns: minmax(170px, 210px) minmax(0, 1fr);
  gap: 14px;
  margin-top: 14px;
}

.board-wrap {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: clamp(12px, 2vw, 20px);
  box-shadow: var(--shadow);
}

.board-wrap > * {
  position: relative;
  z-index: 1;
}

.scoreboard {
  align-self: start;
  border-radius: 8px;
  padding: 14px;
}

.player-row {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  min-height: 48px;
  border-bottom: 1px solid var(--line);
  font-weight: 850;
}

.player-row span:nth-child(2) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-row:last-child {
  border-bottom: 0;
}

.player-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
}

.player-row.active {
  color: var(--accent);
}

.status-line {
  min-height: 34px;
  margin-bottom: 10px;
  color: var(--text);
  font-weight: 850;
  text-shadow: 0 1px 8px rgba(255, 255, 255, 0.84);
}

.board {
  width: min(100%, calc(100vh - 156px), 690px);
  min-width: 0;
  aspect-ratio: 1;
  margin: 0 auto;
  padding: clamp(4px, 0.8vw, 8px);
  border-radius: 8px;
  background:
    linear-gradient(rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.7)),
    var(--theme-image, linear-gradient(135deg, #eef2ff, #ffffff));
  background-size: cover;
  background-position: center;
  backdrop-filter: blur(2px);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.42);
}

.tile {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border: 3px solid var(--tile-border);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.9);
  color: #121826;
  font-size: clamp(0.92rem, 2.3vw, 1.75rem);
  font-weight: 950;
  box-shadow: 0 5px 12px rgba(25, 31, 45, 0.1);
  user-select: none;
  aspect-ratio: 1;
}

.mini-board .tile {
  border-width: 2px;
  border-radius: 5px;
  font-size: clamp(0.7rem, 1.4vw, 1.05rem);
  box-shadow: none;
}

.tile.selected {
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--player-color) 35%, transparent), 0 10px 22px rgba(20, 26, 38, 0.18);
  transform: translateY(-2px) scale(1.02);
}

.tile.correct {
  background: color-mix(in srgb, var(--good) 18%, white);
  border-color: var(--good);
}

.tile.incorrect {
  background: color-mix(in srgb, var(--bad) 16%, white);
  border-color: var(--bad);
}

.result-dialog {
  width: min(480px, calc(100vw - 28px));
  border-radius: 8px;
  color: var(--text);
  padding: 24px;
}

.result-dialog::backdrop {
  background: rgba(8, 12, 20, 0.48);
}

.result-dialog h2 {
  margin: 0 0 8px;
  font-size: 2rem;
}

.dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

[data-theme="wizard"] {
  --bg: #211539;
  --bg-2: #4c2a77;
  --surface: #fff8e8;
  --surface-2: #f1e6ff;
  --text: #211539;
  --muted: #675677;
  --accent: #7c3aed;
  --accent-2: #f2b84b;
  --line: #d8c3ff;
  --theme-image: url("assets/themes/wizard.png");
}

[data-theme="unicorn"] {
  --bg: #fff1f8;
  --bg-2: #c5f1ff;
  --surface: #ffffff;
  --surface-2: #fff4fc;
  --text: #30324f;
  --muted: #6d7193;
  --accent: #ff73ba;
  --accent-2: #6bd8ff;
  --line: #ffd0e8;
  --theme-image: url("assets/themes/unicorn.png");
}

[data-theme="dino"] {
  --bg: #eef8e7;
  --bg-2: #cfe3bd;
  --surface: #fffdf5;
  --surface-2: #edf5e4;
  --text: #20301f;
  --muted: #5f705c;
  --accent: #2f8d46;
  --accent-2: #d9922e;
  --line: #c6d8ba;
  --theme-image: url("assets/themes/dino.png");
}

[data-theme="space"] {
  --bg: #081120;
  --bg-2: #1b2d55;
  --surface: #f8fbff;
  --surface-2: #e8f0ff;
  --text: #111827;
  --muted: #5f6f88;
  --accent: #2563eb;
  --accent-2: #29d3c2;
  --line: #bed2ff;
  --theme-image: url("assets/themes/space.png");
}

@media (min-width: 981px) and (max-width: 1180px) {
  .app {
    padding: 18px;
  }

  .setup-view {
    grid-template-columns: minmax(0, 1fr) minmax(260px, 330px);
    gap: 18px;
    align-items: start;
  }

  .setup-main {
    padding: 22px;
  }

  .setup-main h1 {
    font-size: clamp(3rem, 6vw, 4.3rem);
  }

  .intro {
    font-size: 1rem;
    line-height: 1.45;
  }

  .setup-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  .setup-grid .control-panel:first-child {
    grid-column: 1 / -1;
  }

  .theme-options {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .theme-choice {
    min-height: 72px;
    grid-template-columns: 1fr;
    align-items: end;
    gap: 6px;
    padding: 10px;
  }

  .theme-swatch {
    width: 20px;
    height: 20px;
  }

  .name-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .preview-card {
    position: sticky;
    top: 18px;
    padding: 14px;
  }

  .mini-board {
    width: min(100%, 292px);
  }

  .play-area {
    grid-template-columns: 178px minmax(0, 1fr);
  }

  .board {
    width: min(calc(100vw - 250px), calc(100vh - 144px), 620px);
  }
}

@media (max-width: 980px) {
  .setup-view,
  .play-area {
    grid-template-columns: 1fr;
  }

  .setup-view {
    align-items: start;
    gap: 16px;
    min-height: auto;
  }

  .setup-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }

  .name-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .preview-card {
    max-width: 720px;
    margin: 0 auto;
    width: 100%;
  }

  .preview-card {
    display: grid;
    grid-template-columns: minmax(0, 280px) minmax(0, 1fr);
    gap: 14px;
    align-items: center;
  }

  .preview-topline,
  .preview-note {
    grid-column: 2;
  }

  .preview-topline {
    align-self: end;
    margin-bottom: 0;
  }

  .preview-note {
    align-self: start;
    margin: 0;
  }

  .mini-board {
    grid-row: 1 / span 2;
    width: min(100%, 280px);
  }

  .hud {
    position: static;
    grid-template-columns: 104px repeat(2, minmax(70px, 1fr)) repeat(2, minmax(86px, max-content));
  }

  .equation {
    grid-column: 1 / -1;
    order: 4;
  }

  .hud .secondary-action {
    min-height: 38px;
  }

  .hud .secondary-action {
    min-width: 0;
  }

  .board {
    width: min(100%, calc(100vh - 244px), 680px);
  }

  .scoreboard {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 8px;
  }

  .player-row {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0 10px;
  }
}

@media (min-width: 700px) and (max-width: 980px) {
  .app {
    padding: 18px;
  }

  .setup-main {
    padding: 22px;
  }

  .setup-main h1 {
    font-size: clamp(3.2rem, 9vw, 5rem);
  }

  .setup-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .setup-grid .control-panel:first-child {
    grid-column: 1 / -1;
  }

  .theme-options {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .theme-choice {
    min-height: 76px;
    grid-template-columns: 1fr;
    align-items: end;
    gap: 6px;
    padding: 10px;
  }

  .theme-choice span:last-child {
    font-size: 0.9rem;
  }

  .play-area {
    gap: 12px;
  }

  .board-wrap {
    padding: 14px;
  }

  .status-line {
    min-height: 28px;
    margin-bottom: 8px;
  }
}

@media (max-width: 540px) {
  .app {
    padding: 10px;
  }

  .setup-main h1 {
    font-size: clamp(2.2rem, 15vw, 4.4rem);
  }

  .theme-options {
    grid-template-columns: 1fr;
  }

  .preview-card {
    display: block;
    max-width: 520px;
  }

  .preview-topline {
    margin-bottom: 16px;
  }

  .mini-board {
    width: min(100%, 360px);
  }

  .preview-note {
    margin: 16px 0 0;
  }

  .control-panel {
    padding: 12px;
  }

  .name-grid {
    grid-template-columns: 1fr;
  }

  .setup-actions,
  .primary-action {
    width: 100%;
  }

  .hud {
    grid-template-columns: 1fr 1fr;
  }

  .hud > div:first-child {
    grid-column: 1 / -1;
  }

  .target {
    width: 100%;
    min-height: 62px;
  }

  .hud .secondary-action {
    padding: 0 12px;
  }

  .equation {
    grid-column: 1 / -1;
  }

  .tile {
    border-width: 2px;
    border-radius: 5px;
    font-size: clamp(0.78rem, 5vw, 1.25rem);
  }

  .board {
    gap: 2px;
  }
}
