:root {
  color-scheme: light;
  --bg: #fff5f5;
  --ink: #171717;
  --muted: #5f5b5b;
  --line: #e7d4d4;
  --panel: #ffffff;
  --accent: #d71920;
  --accent-strong: #8f1116;
  --warn: #b7791f;
  --active: #facc15;
  --active-soft: #fef3c7;
  --x: #1e63d6;
  --o: #c43d5d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top, #fff 0, var(--bg) 36%, #f7dada 100%);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 14px;
  font-weight: 700;
}

button.primary {
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.secondary {
  width: 100%;
  background: #e8f3ef;
  border-color: #b7d8cd;
  color: var(--accent-strong);
}

button.ghost {
  background: transparent;
}

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

input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
}

.emoji-input {
  width: 58px;
  text-align: center;
  font-size: 24px;
  line-height: 1;
}

.app {
  width: min(100%, 720px);
  margin: 0 auto;
  padding: 16px;
}

.screen {
  display: none;
  min-height: 100vh;
}

.screen.active {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#intro {
  --intro-control-width: min(100%, 420px);
  min-height: calc(100dvh - 32px);
  padding: clamp(12px, 4vw, 18px);
  border-radius: 18px;
  background: linear-gradient(160deg, #d71920 0%, #8f1116 45%, #080808 100%);
  color: #fff;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

#intro p {
  color: #fff;
}

#intro .primary {
  width: var(--intro-control-width);
  background: #fff;
  border-color: #fff;
  color: var(--accent-strong);
  align-self: center;
}

.brand {
  width: var(--intro-control-width);
  display: grid;
  gap: 12px;
  justify-items: center;
  text-align: center;
}

.brand p {
  max-width: var(--intro-control-width);
  line-height: 1.35;
}

.revision-summary {
  justify-self: center;
  margin-top: 6px;
  padding: 8px 10px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  background: rgba(5, 5, 5, 0.38);
  color: rgba(255, 255, 255, 0.94);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
}

.intro-art {
  width: min(90%, 378px);
  aspect-ratio: 1;
  object-fit: contain;
  border: 4px solid #111;
  border-radius: 28px;
  background: #050505;
  box-shadow: 0 18px 42px rgba(143, 17, 22, 0.34);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 42px;
  line-height: 1;
  color: var(--accent-strong);
}

h2 {
  font-size: 22px;
}

p,
.meta,
.label {
  color: var(--muted);
}

.topbar,
.game-header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
}

.topbar h2 {
  text-align: center;
}

.game-header > div {
  min-width: 0;
  text-align: center;
}

.panel {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.player-create-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 58px minmax(160px, 220px);
  gap: 8px;
  align-items: end;
}

.color-picker-shell {
  display: grid;
  grid-template-columns: minmax(78px, 1fr) 46px;
  gap: 6px;
  margin-top: 6px;
}

.color-value {
  min-height: 38px;
  padding: 0 8px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 13px;
  text-transform: uppercase;
}

.native-color {
  width: 46px;
  min-height: 38px;
  padding: 3px;
}

.hidden {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 25;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(23, 32, 51, 0.54);
}

.modal.hidden {
  display: none;
}

.modal-panel {
  width: min(100%, 680px);
  max-height: min(90vh, 760px);
  display: grid;
  gap: 12px;
  overflow-y: auto;
  border-radius: 8px;
  background: var(--bg);
  padding: 14px;
  box-shadow: 0 20px 60px rgba(23, 32, 51, 0.28);
}

.modal-header {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.confirm-panel {
  max-width: 360px;
}

.games-list {
  display: grid;
  gap: 10px;
}

.section-heading {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

button.compact {
  min-height: 36px;
  padding: 0 10px;
}

.lobby-players,
.room-list {
  display: grid;
  gap: 8px;
}

.lobby-players {
  max-height: 220px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.notice {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid #f0d17a;
  border-radius: 8px;
  background: #fff7d6;
  color: #4b3710;
  font-weight: 700;
  text-align: center;
}

.notice.hidden {
  display: none;
}

.notice div {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.notice span {
  color: #6b551c;
  font-size: 13px;
  font-weight: 700;
}

.room-summary-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.room-summary-main {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.room-summary-main strong,
.room-summary-main span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-summary-main span {
  color: var(--muted);
  font-weight: 700;
}

.room-summary-players {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
}

.room-summary-players .avatar {
  width: 28px;
  height: 28px;
  font-size: 16px;
}

.room-summary-card button {
  grid-column: 1 / -1;
  width: 100%;
}

.game-card {
  width: 100%;
  min-height: 52px;
  text-align: center;
  font-size: 18px;
  white-space: pre-line;
}

.game-card:disabled {
  background: #eef2f8;
  border-color: #d3dae8;
  color: var(--muted);
}

.game-card strong,
.game-card span,
.game-card em {
  min-width: 0;
}

.game-card strong {
  font-size: 20px;
}

.game-card > span:not(.game-badge) {
  grid-column: 1 / -1;
  color: var(--muted);
  font-weight: 500;
}

.game-card em {
  grid-column: 1 / -1;
  color: var(--accent-strong);
  font-style: normal;
}

.game-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 122, 95, 0.16);
}

.current-player {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 10px;
  align-items: center;
  min-height: 48px;
  font-weight: 800;
}

.current-player:not(:has(.avatar)) {
  display: block;
  color: var(--muted);
  font-weight: 700;
}

.player-select-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
}

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

.player-menu-actions button {
  width: auto;
  padding: 0 12px;
}

.game-badge {
  justify-self: end;
  align-self: start;
  border-radius: 999px;
  background: #e8f3ef;
  color: var(--accent-strong);
  padding: 4px 8px;
  font-size: 12px;
}

.selected-game {
  display: grid;
  gap: 2px;
}

.selected-game span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.roster-scroll {
  display: grid;
  gap: 8px;
  max-height: 190px;
  margin-top: 6px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 2px;
}

.roster-player {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-height: 48px;
  padding: 6px 10px;
  text-align: left;
}

.roster-player strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.roster-player.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 122, 95, 0.16);
}

.player-list,
.room-players {
  display: grid;
  gap: 8px;
}

.player-card {
  display: grid;
  grid-template-columns: 42px 1fr auto;
  gap: 10px;
  align-items: center;
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 6px 10px;
}

.player-card > button.delete-player {
  min-height: 36px;
  padding: 0 10px;
}

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

.player-actions button {
  min-height: 36px;
  padding: 0 10px;
}

.player-icon-edit {
  width: 48px;
  min-height: 36px;
  padding: 0 6px;
  text-align: center;
  font-size: 20px;
  line-height: 1;
}

.delete-player {
  color: #9f1239;
}

.player-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 122, 95, 0.16);
}

.avatar {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
}

.color-palette {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin-top: 6px;
}

.choice {
  width: 42px;
  min-height: 42px;
  padding: 0;
}

.choice.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 122, 95, 0.16);
}

.swatch {
  border-radius: 6px;
  width: 100%;
  min-height: 28px;
}

.room-host-summary {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 4px 10px;
  align-items: center;
  min-height: 48px;
  font-weight: 800;
}

.room-host-summary .label {
  grid-column: 1 / -1;
  font-weight: 700;
}

.room-host-summary strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.section-action {
  display: flex;
  justify-content: flex-end;
}

.section-action button {
  width: auto;
  min-width: 132px;
}

.room-slots {
  display: grid;
  gap: 12px;
}

.room-slot {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 8px 10px;
  font-weight: 800;
}

.room-slot strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-slot > button {
  grid-column: 1 / -1;
  width: 100%;
}

.room-slot.status-only {
  display: flex;
  justify-content: center;
  text-align: center;
}

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

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

.player-switch-button {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  min-width: 0;
  min-height: 48px;
  padding: 6px 8px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: inherit;
  cursor: default;
  user-select: none;
}

.player-switch-button .avatar {
  width: 30px;
  height: 30px;
}

.player-switch-button span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-switch-button.current-turn {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 122, 95, 0.16);
}

.turn-status {
  min-height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-weight: 900;
  text-align: center;
}

.turn-status.your-turn {
  border-color: var(--turn-color, var(--accent));
  background: var(--turn-soft, #e8f3ef);
  color: var(--turn-text, var(--accent-strong));
}

.turn-status.waiting {
  border-color: #facc15;
  background: #fef3c7;
  color: #854d0e;
  animation: waiting-pulse 1.4s ease-in-out infinite;
}

.macro-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  width: 95%;
  aspect-ratio: 1;
  align-self: center;
}

.macro-board.your-turn {
  border-radius: 10px;
  box-shadow: 0 0 0 4px var(--turn-glow, rgba(31, 122, 95, 0.18));
}

.macro-board.waiting {
  border-radius: 10px;
  box-shadow: 0 0 0 4px rgba(250, 204, 21, 0.35);
  animation: waiting-pulse 1.4s ease-in-out infinite;
}

.small-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  padding: 4px;
  border: 2px solid #8a94a8;
  border-radius: 8px;
  background: #eef2f8;
  opacity: 0.48;
}

.small-board.legal {
  border-color: var(--turn-color, var(--accent));
  background: var(--turn-soft, #e6f3ef);
  opacity: 1;
  box-shadow: 0 0 0 2px var(--turn-glow, rgba(31, 122, 95, 0.16));
}

.small-board.flash {
  animation: active-board-flash 0.25s ease-out;
}

.small-board.done {
  opacity: 0.72;
  animation: none;
}

.small-board.macro-win-cell {
  opacity: 1;
}

.cell {
  display: grid;
  place-items: center;
  min-width: 0;
  aspect-ratio: 1;
  border-radius: 5px;
  border: 1px solid #c5ccda;
  background: #fff;
  font-size: clamp(14px, 5vw, 28px);
  font-weight: 900;
  line-height: 1;
  padding: 0;
}

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

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

.cell.small-win-cell {
  border-color: #f59e0b;
  box-shadow: inset 0 0 0 2px rgba(245, 158, 11, 0.36);
}

.macro-win-line {
  position: absolute;
  z-index: 5;
  border-radius: 999px;
  background: var(--accent-strong);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.75);
  pointer-events: none;
}

.macro-win-line.row-0,
.macro-win-line.row-1,
.macro-win-line.row-2 {
  left: 2%;
  width: 96%;
  height: 6px;
}

.macro-win-line.row-0 {
  top: 16.666%;
}

.macro-win-line.row-1 {
  top: 50%;
}

.macro-win-line.row-2 {
  top: 83.333%;
}

.macro-win-line.col-0,
.macro-win-line.col-1,
.macro-win-line.col-2 {
  top: 2%;
  width: 6px;
  height: 96%;
}

.macro-win-line.col-0 {
  left: 16.666%;
}

.macro-win-line.col-1 {
  left: 50%;
}

.macro-win-line.col-2 {
  left: 83.333%;
}

.macro-win-line.diag-down,
.macro-win-line.diag-up {
  left: -16%;
  top: 50%;
  width: 132%;
  height: 6px;
  transform-origin: center;
}

.macro-win-line.diag-down {
  transform: rotate(45deg);
}

.macro-win-line.diag-up {
  transform: rotate(-45deg);
}

.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(23, 32, 51, 0.58);
  overflow: hidden;
}

.win-overlay.hidden {
  display: none;
}

.win-card {
  position: relative;
  z-index: 30;
  width: min(100%, 360px);
  display: grid;
  gap: 14px;
  justify-items: center;
  padding: 24px 18px 18px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 20px 60px rgba(23, 32, 51, 0.32);
  text-align: center;
}

.win-icon {
  width: 74px;
  height: 74px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #e8f3ef;
  color: #fff;
  font-size: 42px;
  font-weight: 950;
  line-height: 1;
}

.win-card h3 {
  margin: 0;
  font-size: 30px;
  line-height: 1.1;
}

.confetti {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}

.confetti span {
  position: absolute;
  top: -24px;
  width: 9px;
  height: 16px;
  border-radius: 2px;
  animation: confetti-fall 1.9s ease-in forwards;
}

@keyframes confetti-fall {
  0% {
    opacity: 1;
    translate: 0 0;
  }

  100% {
    opacity: 0.9;
    translate: 0 110vh;
    rotate: 280deg;
  }
}

.board-winner {
  position: absolute;
  inset: 4px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.78);
  font-size: clamp(36px, 18vw, 82px);
  font-weight: 950;
  pointer-events: none;
}

.board-winner.x {
  color: var(--x);
}

.board-winner.o {
  color: var(--o);
}

.board-winner.d {
  color: var(--muted);
}

@keyframes active-board-flash {
  0% {
    border-color: var(--turn-color, #1f7a5f);
    background: var(--turn-soft-strong, #e6f3ef);
    box-shadow: 0 0 0 4px var(--turn-glow, rgba(31, 122, 95, 0.35));
  }

  100% {
    border-color: var(--turn-color, var(--accent));
    background: var(--turn-soft, #e6f3ef);
    box-shadow: 0 0 0 2px var(--turn-glow, rgba(31, 122, 95, 0.16));
  }
}

@keyframes waiting-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 2px rgba(250, 204, 21, 0.22);
  }

  50% {
    box-shadow: 0 0 0 5px rgba(250, 204, 21, 0.48);
  }
}

@media (min-width: 600px) {
  .app {
    padding-top: 28px;
  }

  .macro-board {
    max-width: 640px;
    width: 95%;
    align-self: center;
  }
}

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

  #intro {
    min-height: calc(100dvh - 20px);
    border-radius: 14px;
  }

  .intro-art {
    border-width: 3px;
    border-radius: 22px;
  }

  .player-select-row {
    grid-template-columns: 1fr;
  }

  .player-menu-actions {
    min-width: 0;
  }

  .player-create-row {
    grid-template-columns: minmax(0, 1fr) 58px;
  }

  .player-create-row > div {
    grid-column: 1 / -1;
  }

  .section-action,
  .section-action button {
    width: 100%;
  }

  .room-summary-card {
    grid-template-columns: 1fr;
  }

  .room-summary-players {
    justify-content: flex-start;
  }

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

  .notice button {
    width: 100%;
  }
}
