:root {
  color-scheme: light;
  --bg: #eef2ef;
  --ink: #172026;
  --muted: #65727d;
  --panel: #fffefa;
  --line: #d7ded8;
  --accent: #266b5d;
  --accent-2: #b5483e;
  --mark-x: #1f6fb2;
  --mark-o: #c0562f;
  --win: #e1b44f;
  --shadow: 0 18px 40px rgba(23, 32, 38, 0.12);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(38, 107, 93, 0.15), transparent 40%),
    linear-gradient(225deg, rgba(181, 72, 62, 0.12), transparent 34%),
    var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-weight: 800;
  padding: 0 18px;
}

button:hover {
  filter: brightness(0.95);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

input,
select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: white;
  color: var(--ink);
  padding: 10px 12px;
}

label {
  color: var(--muted);
  display: grid;
  gap: 7px;
  font-size: 0.9rem;
  font-weight: 800;
}

fieldset {
  border: 0;
  margin: 0;
  padding: 0;
}

legend {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
  margin-bottom: 7px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2.4rem, 6vw, 5.2rem);
  line-height: 0.95;
}

h2 {
  font-size: 1.25rem;
}

.shell {
  margin: 0 auto;
  max-width: 1120px;
  padding: 28px;
}

body.in-game .shell {
  padding-top: 18px;
}

.topbar {
  align-items: end;
  display: flex;
  gap: 18px;
  justify-content: space-between;
  margin-bottom: 24px;
}

body.in-game .topbar {
  margin-bottom: 10px;
}

body.in-game h1 {
  font-size: clamp(1.35rem, 3vw, 2.55rem);
}

body.in-game .game-code {
  min-width: 96px;
  padding: 9px 12px;
}

body.in-game .game-code span {
  font-size: 0.6rem;
}

body.in-game .game-code strong {
  font-size: 1.15rem;
}

.eyebrow {
  color: var(--accent-2);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.game-code {
  background: var(--ink);
  border-radius: 8px;
  color: white;
  min-width: 128px;
  padding: 13px 16px;
  text-align: right;
}

.game-code span {
  color: rgba(255, 255, 255, 0.68);
  display: block;
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.game-code strong {
  font-size: 1.55rem;
  letter-spacing: 0.06em;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 24px;
}

.stack {
  display: grid;
  gap: 16px;
}

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

.play {
  display: grid;
  gap: 18px;
}

.status {
  align-items: center;
  display: flex;
  gap: 20px;
  justify-content: space-between;
}

.status > div:first-child {
  min-width: 0;
}

body.in-game .status {
  padding: 12px 16px;
}

.status.your-turn {
  border-color: var(--win);
  box-shadow: 0 0 0 5px rgba(225, 180, 79, 0.25), var(--shadow);
}

.actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.status-players {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
  list-style: none;
  margin: 0;
  padding: 0;
}

.status-player {
  align-items: center;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  gap: 7px;
  min-height: 36px;
  padding: 5px 7px 5px 10px;
}

.status-player.active {
  border-color: var(--win);
  box-shadow: 0 0 0 3px rgba(225, 180, 79, 0.22);
}

.status-player-name {
  color: var(--ink);
  font-size: 0.88rem;
  font-weight: 900;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mini-badge {
  border-radius: 999px;
  color: white;
  display: inline-grid;
  font-size: 0.78rem;
  font-weight: 900;
  height: 26px;
  min-width: 26px;
  place-items: center;
  padding: 0 8px;
}

.mini-badge.x {
  background: var(--mark-x);
}

.mini-badge.o {
  background: var(--mark-o);
}

.complete-settings {
  align-items: center;
  display: flex;
  gap: 8px;
}

.complete-settings[hidden] {
  display: none !important;
}

.complete-settings select {
  min-height: 36px;
  width: 168px;
}

.mini-segmented {
  gap: 5px;
  width: 184px;
}

.mode-segmented {
  gap: 5px;
  width: 170px;
}

.mini-segmented span,
.mode-segmented span {
  font-size: 0.78rem;
  min-height: 36px;
  padding: 0 7px;
}

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

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

.segmented label {
  display: block;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  align-items: center;
  background: white;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--ink);
  display: flex;
  font-weight: 900;
  justify-content: center;
  min-height: 44px;
  padding: 0 8px;
}

.segmented input:checked + span {
  background: var(--ink);
  border-color: var(--ink);
  color: white;
}

.board-layout {
  align-items: start;
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(0, 1fr) 340px;
}

body.board-active .board-layout {
  grid-template-columns: 1fr;
}

body.board-active .side {
  display: none;
}

.board-wrap {
  display: grid;
  justify-items: center;
}

.board {
  aspect-ratio: 1;
  display: grid;
  width: min(100%, 620px, calc(100svh - 230px));
}

.classic-board {
  gap: 10px;
  grid-template-columns: repeat(3, 1fr);
}

.ultimate-board {
  gap: 8px;
  grid-template-columns: repeat(3, 1fr);
}

.cell {
  aspect-ratio: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  color: var(--ink);
  display: grid;
  font-size: clamp(3rem, 12vw, 6.2rem);
  font-weight: 900;
  line-height: 1;
  place-items: center;
  padding: 0;
}

.cell.mark-x {
  color: var(--mark-x);
}

.cell.mark-o {
  color: var(--mark-o);
}

.cell.win {
  background: #fff5cc;
  border-color: var(--win);
}

.ultimate-small-board {
  aspect-ratio: 1;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 3px;
  grid-template-columns: repeat(3, 1fr);
  padding: 5px;
  position: relative;
}

.ultimate-small-board.active {
  border-color: var(--win);
  box-shadow: 0 0 0 3px rgba(225, 180, 79, 0.22);
}

.ultimate-small-board.overall-win {
  background: #fff5cc;
  border-color: var(--win);
}

.ultimate-cell {
  aspect-ratio: 1;
  background: white;
  border: 1px solid var(--line);
  border-radius: 5px;
  color: var(--ink);
  display: grid;
  font-size: clamp(0.85rem, 3.2vw, 1.7rem);
  font-weight: 900;
  line-height: 1;
  min-height: 0;
  padding: 0;
  place-items: center;
}

.ultimate-cell.win {
  background: #fff5cc;
  border-color: var(--win);
}

.ultimate-winner {
  align-items: center;
  background: rgba(255, 254, 250, 0.82);
  border-radius: 8px;
  display: flex;
  font-size: clamp(2rem, 8vw, 4.3rem);
  font-weight: 900;
  inset: 5px;
  justify-content: center;
  pointer-events: none;
  position: absolute;
}

.ultimate-winner.x,
.mark-x {
  color: var(--mark-x);
}

.ultimate-winner.o,
.mark-o {
  color: var(--mark-o);
}

.players {
  display: grid;
  gap: 10px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.player {
  align-items: center;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  gap: 10px;
  justify-content: space-between;
  min-height: 52px;
  padding: 10px 12px;
}

.player.active {
  border-color: var(--win);
  box-shadow: 0 0 0 4px rgba(225, 180, 79, 0.22);
}

.player-name {
  font-weight: 900;
}

.player-meta {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.badge {
  border-radius: 999px;
  color: white;
  display: inline-grid;
  font-weight: 900;
  height: 34px;
  min-width: 34px;
  place-items: center;
  padding: 0 10px;
}

.badge.x {
  background: var(--mark-x);
}

.badge.o {
  background: var(--mark-o);
}

.toast {
  background: var(--ink);
  border-radius: 8px;
  bottom: 20px;
  color: white;
  font-weight: 800;
  left: 50%;
  max-width: min(92vw, 520px);
  padding: 13px 16px;
  position: fixed;
  transform: translateX(-50%);
  z-index: 10;
}

@media (max-width: 840px) {
  .home,
  .board-layout {
    grid-template-columns: 1fr;
  }

  .side {
    order: -1;
  }

  body.board-active .side {
    order: initial;
  }

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

@media (max-width: 560px) {
  .shell {
    padding: 18px;
  }

  body.in-game .shell {
    padding: 12px;
  }

  .topbar,
  .status {
    align-items: stretch;
    flex-direction: column;
  }

  body.in-game .topbar {
    flex-direction: row;
  }

  body.in-game .topbar h1 {
    font-size: 1.25rem;
  }

  body.in-game .eyebrow {
    font-size: 0.68rem;
  }

  .game-code {
    text-align: left;
  }

  body.in-game .game-code {
    min-width: 82px;
    padding: 6px 8px;
  }

  body.in-game .game-code strong {
    font-size: 0.95rem;
  }

  .panel {
    padding: 18px;
  }

  body.in-game .panel,
  body.in-game .status {
    padding: 12px;
  }

  body.in-game .status {
    gap: 10px;
  }

  body.board-active .status {
    align-items: center;
    flex-direction: row;
  }

  body.board-active .status > div:first-child {
    flex: 1 1 auto;
  }

  body.board-active .status h2,
  body.board-active .status p:not(.eyebrow) {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.board-active .actions {
    flex: 0 0 auto;
    max-width: 48%;
  }

  body.game-ended .actions {
    flex: 1 1 auto;
    max-width: none;
  }

  body.board-active .complete-settings {
    flex-wrap: wrap;
    gap: 5px;
    justify-content: flex-end;
    max-width: 100%;
  }

  body.board-active .complete-settings select {
    min-height: 32px;
    width: 142px;
  }

  body.board-active .mini-segmented {
    width: 150px;
  }

  body.board-active .mini-segmented span {
    font-size: 0.68rem;
    min-height: 32px;
    padding: 0 5px;
  }

  body.board-active .status-players {
    flex-wrap: nowrap;
    gap: 5px;
  }

  body.board-active .status-player {
    min-height: 31px;
    padding: 4px 5px 4px 7px;
  }

  body.board-active .status-player-name {
    font-size: 0.76rem;
    max-width: 74px;
  }

  body.board-active .mini-badge {
    font-size: 0.68rem;
    height: 23px;
    min-width: 23px;
    padding: 0 6px;
  }

  body.in-game .status h2 {
    font-size: 1rem;
  }

  body.in-game .status p:not(.eyebrow) {
    font-size: 0.9rem;
  }

body.in-game .board-layout,
body.in-game .play,
body.in-game .side {
  gap: 10px;
}

  .board {
    gap: 8px;
    width: min(100%, calc(100svh - 330px));
  }

  .cell {
    font-size: clamp(2.3rem, 16vw, 4.4rem);
  }
}

@media (max-width: 560px) and (max-height: 720px) {
  body.in-game .side .panel {
    padding: 10px;
  }

  .player {
    min-height: 44px;
    padding: 7px 9px;
  }

  .badge {
    height: 30px;
    min-width: 30px;
  }

  .board {
    width: min(100%, calc(100svh - 285px));
  }
}
