:root {
  color-scheme: light;
  --bg: #f7f3eb;
  --paper: #fffaf0;
  --surface: #ffffff;
  --ink: #23303f;
  --muted: #647083;
  --line: #d9d1c3;
  --board: #1767b0;
  --board-dark: #0d4d8a;
  --board-light: #2b83ce;
  --gold: #f0b84f;
  --teal: #187b78;
  --teal-dark: #0f5d5b;
  --danger: #aa2c25;
  --focus: #1c6fe8;
  --shadow: 0 18px 50px rgba(31, 42, 56, 0.16);
  --piece-red: #e64b3c;
  --piece-blue: #2878d7;
  --piece-green: #2f9654;
  --piece-yellow: #f2c94c;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(240, 184, 79, 0.25), transparent 28rem),
    radial-gradient(circle at bottom right, rgba(40, 120, 215, 0.14), transparent 30rem),
    linear-gradient(135deg, #f7f3eb 0%, #e8f0ec 50%, #f3edf7 100%);
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
}

.app-shell {
  width: min(1100px, calc(100% - 32px));
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 28px 0;
  display: grid;
  align-items: center;
}

.screen {
  width: 100%;
}

.is-hidden {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.setup-screen {
  display: grid;
  place-items: center;
}

.setup-panel {
  width: min(100%, 760px);
  padding: 28px;
  background: rgba(255, 250, 240, 0.9);
  border: 1px solid rgba(217, 209, 195, 0.95);
  border-radius: 10px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.setup-copy {
  margin-bottom: 26px;
  text-align: center;
}

.setup-copy h1 {
  margin: 0;
  font-size: clamp(2.7rem, 8vw, 5rem);
  line-height: 0.95;
  letter-spacing: -0.045em;
}

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

.player-field {
  min-width: 0;
  margin: 0;
  padding: 18px;
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid var(--line);
  border-radius: 9px;
}

.player-field legend {
  padding: 0 7px;
  font-size: 1.05rem;
  font-weight: 900;
}

.field {
  display: grid;
  gap: 7px;
  margin-bottom: 14px;
  font-weight: 850;
}

.field:last-child {
  margin-bottom: 0;
}

.field > span:first-child {
  font-size: 0.9rem;
}

.field input,
.field select {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  color: var(--ink);
  background: white;
  border: 2px solid transparent;
  border-radius: 8px;
  outline: 1px solid var(--line);
}

.field input:focus,
.field select:focus {
  border-color: var(--focus);
  outline-color: transparent;
}

.color-select-wrap {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
}

.color-select-wrap select {
  font-weight: 800;
}

.color-swatch {
  width: 28px;
  aspect-ratio: 1;
  justify-self: center;
  border: 2px solid rgba(35, 48, 63, 0.22);
  border-radius: 50%;
  background: var(--swatch-color, var(--piece-red));
  box-shadow: inset 0 -3px 0 rgba(35, 48, 63, 0.12);
  pointer-events: none;
}

.checkbox-field {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 18px;
  font-weight: 900;
}

.checkbox-field input {
  width: 20px;
  height: 20px;
  accent-color: var(--teal);
  flex: 0 0 auto;
}

.error-message {
  min-height: 1.35rem;
  margin: 14px 0 12px;
  color: var(--danger);
  font-weight: 800;
}

.primary-action,
.secondary-action,
.icon-action {
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  font-weight: 900;
}

.primary-action {
  color: white;
  background: var(--teal);
  box-shadow: 0 10px 22px rgba(24, 123, 120, 0.22);
}

.primary-action:hover,
.primary-action:focus-visible {
  background: var(--teal-dark);
}

.secondary-action {
  padding: 0 16px;
  color: var(--ink);
  background: white;
  box-shadow: inset 0 0 0 1px var(--line);
}

.secondary-action:hover,
.secondary-action:focus-visible,
.icon-action:hover,
.icon-action:focus-visible {
  background: #eef7f4;
}

.start-action {
  width: 100%;
}

.is-playing .app-shell {
  padding-top: 0;
  align-items: start;
}

.game-screen {
  display: grid;
  gap: clamp(12px, 2vh, 22px);
  align-items: start;
}

.game-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-areas: "actions players";
  gap: 10px;
  align-items: center;
  padding: 10px;
  background: rgba(255, 250, 240, 0.95);
  border: 1px solid rgba(217, 209, 195, 0.95);
  border-top: 0;
  border-radius: 0 0 9px 9px;
  box-shadow: 0 10px 30px rgba(31, 42, 56, 0.12);
  backdrop-filter: blur(12px);
}

.game-actions {
  grid-area: actions;
  display: flex;
  gap: 8px;
}

.icon-action {
  width: 46px;
  padding: 0;
  color: var(--ink);
  background: white;
  box-shadow: inset 0 0 0 1px var(--line);
  font-size: 1.35rem;
  line-height: 1;
}

.player-status {
  grid-area: players;
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.player-card {
  --player-color: var(--piece-red);
  min-width: 0;
  min-height: 52px;
  padding: 8px 12px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-areas:
    "piece name"
    "piece detail";
  column-gap: 10px;
  align-items: center;
  background: rgba(255, 255, 255, 0.82);
  border: 2px solid transparent;
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px var(--line);
}

.player-card.is-active {
  border-color: var(--player-color);
  background: color-mix(in srgb, var(--player-color) 10%, white);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--player-color) 19%, transparent);
}

.player-card-piece {
  grid-area: piece;
  width: 30px;
  aspect-ratio: 1;
  border: 2px solid rgba(35, 48, 63, 0.2);
  border-radius: 50%;
  background: var(--player-color);
  box-shadow: inset 0 -5px 0 rgba(35, 48, 63, 0.13);
}

.player-card-name {
  grid-area: name;
  min-width: 0;
  overflow: hidden;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-card.is-active .player-card-name {
  font-weight: 900;
}

.player-card-detail {
  grid-area: detail;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 850;
}

.player-card.is-active .player-card-detail {
  color: var(--ink);
}

.board-area {
  --current-color: var(--piece-red);
  width: min(100%, 720px, calc((100dvh - 112px) * 1.18));
  margin: 0 auto;
  display: grid;
  justify-items: center;
}

.turn-piece {
  width: clamp(24px, 6vw, 48px);
  aspect-ratio: 1;
  margin-bottom: 8px;
  border: 2px solid rgba(35, 48, 63, 0.2);
  border-radius: 50%;
  background: var(--current-color);
  box-shadow: inset 0 -7px 0 rgba(35, 48, 63, 0.14), 0 5px 10px rgba(35, 48, 63, 0.14);
  transition: background 160ms ease;
}

.board {
  width: 100%;
  aspect-ratio: 7 / 6;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: clamp(3px, 0.8vw, 8px);
  padding: clamp(7px, 1.5vw, 14px);
  overflow: hidden;
  background: linear-gradient(145deg, var(--board-light), var(--board) 46%, var(--board-dark));
  border: clamp(3px, 0.65vw, 6px) solid var(--board-dark);
  border-radius: clamp(10px, 2vw, 18px);
  box-shadow: 0 20px 38px rgba(13, 77, 138, 0.25), inset 0 4px 0 rgba(255, 255, 255, 0.2);
}

.column-button {
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: 0;
  display: grid;
  grid-template-rows: repeat(6, minmax(0, 1fr));
  gap: clamp(3px, 0.8vw, 8px);
  color: inherit;
  background: transparent;
  border: 0;
  border-radius: 999px;
}

.column-button:not(:disabled):hover .slot.is-empty,
.column-button:not(:disabled):focus-visible .slot.is-empty {
  background: color-mix(in srgb, var(--current-color) 18%, var(--paper));
}

.column-button:not(:disabled):hover .slot.is-drop-target,
.column-button:not(:disabled):focus-visible .slot.is-drop-target {
  background: color-mix(in srgb, var(--current-color) 62%, var(--paper));
  box-shadow: inset 0 7px 11px rgba(35, 48, 63, 0.15), 0 0 0 3px rgba(255, 255, 255, 0.78);
}

.column-button:focus-visible {
  outline: 4px solid var(--gold);
  outline-offset: 3px;
}

.column-button:disabled {
  opacity: 0.88;
}

.slot {
  --piece-color: transparent;
  width: 100%;
  aspect-ratio: 1;
  place-self: center;
  border: clamp(2px, 0.45vw, 5px) solid rgba(9, 59, 106, 0.76);
  border-radius: 50%;
  background: var(--paper);
  box-shadow: inset 0 7px 11px rgba(35, 48, 63, 0.18), 0 2px 0 rgba(255, 255, 255, 0.22);
  transition: background 120ms ease, box-shadow 120ms ease, transform 120ms ease;
}

.slot.has-piece {
  background: var(--piece-color);
  box-shadow: inset 0 -10px 0 rgba(35, 48, 63, 0.14), inset 0 5px 0 rgba(255, 255, 255, 0.2), 0 2px 0 rgba(255, 255, 255, 0.18);
}

.slot.is-last-move {
  animation: piece-drop 360ms cubic-bezier(0.22, 0.9, 0.36, 1.12);
}

.slot.is-winner {
  z-index: 1;
  box-shadow:
    inset 0 -10px 0 rgba(35, 48, 63, 0.12),
    inset 0 5px 0 rgba(255, 255, 255, 0.2),
    0 0 0 4px var(--gold),
    0 0 18px 6px rgba(240, 184, 79, 0.64);
  animation: winner-pulse 900ms ease-in-out infinite alternate;
}

.board-base {
  width: 92%;
  height: clamp(24px, 5vw, 42px);
  display: flex;
  justify-content: space-between;
  margin-top: -4px;
  padding: 0 5%;
  background: linear-gradient(var(--board), var(--board-dark));
  border: clamp(3px, 0.65vw, 6px) solid var(--board-dark);
  border-radius: 0 0 14px 14px;
  box-shadow: 0 16px 26px rgba(13, 77, 138, 0.2);
}

.board-base span {
  width: 12%;
  height: 138%;
  align-self: start;
  background: var(--board-dark);
  border-radius: 0 0 10px 10px;
}

.result-modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(35, 48, 63, 0.42);
  backdrop-filter: blur(4px);
}

.result-panel {
  width: min(100%, 390px);
  padding: 26px;
  color: var(--ink);
  text-align: center;
  background: var(--paper);
  border: 1px solid rgba(217, 209, 195, 0.95);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.result-piece {
  width: 64px;
  aspect-ratio: 1;
  margin: 0 auto 14px;
  border: 3px solid rgba(35, 48, 63, 0.2);
  border-radius: 50%;
  background: var(--result-color, var(--gold));
  box-shadow: inset 0 -10px 0 rgba(35, 48, 63, 0.13), 0 8px 18px rgba(35, 48, 63, 0.16);
}

.result-panel h2 {
  margin: 0 0 8px;
  font-size: 2.15rem;
  line-height: 1.05;
}

.result-panel p {
  min-height: 1.4em;
  margin: 0 0 20px;
  color: var(--muted);
  font-weight: 750;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.result-actions .primary-action,
.result-actions .secondary-action {
  width: 100%;
  min-width: 0;
}

@keyframes piece-drop {
  from {
    opacity: 0.4;
    transform: translateY(-560%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes winner-pulse {
  from {
    transform: scale(0.94);
  }
  to {
    transform: scale(1);
  }
}

@media (max-width: 760px) {
  .app-shell {
    width: min(100% - 20px, 700px);
    padding: 20px 0;
  }

  .setup-panel {
    padding: 20px;
  }

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

  .game-header {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-areas:
      "actions actions"
      "players players";
    gap: 8px;
    padding: 8px;
  }

  .board-area {
    width: min(100%, 650px, calc((100dvh - 160px) * 1.18));
  }
}

@media (max-width: 500px) {
  .app-shell {
    width: min(100% - 12px, 480px);
    padding: 12px 0;
  }

  .setup-panel {
    padding: 16px;
  }

  .setup-copy {
    margin-bottom: 20px;
  }

  .player-field {
    padding: 14px;
  }

  .game-actions {
    gap: 6px;
  }

  .icon-action {
    width: 42px;
    min-height: 42px;
    font-size: 1.18rem;
  }

  .player-status {
    gap: 6px;
  }

  .player-card {
    min-height: 48px;
    padding: 7px 8px;
    column-gap: 7px;
  }

  .player-card-piece {
    width: 24px;
  }

  .player-card-name {
    font-size: 0.9rem;
  }

  .player-card-detail {
    font-size: 0.68rem;
  }

  .game-screen {
    gap: 8px;
  }

  .turn-piece {
    margin-bottom: 5px;
  }

  .result-actions {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
