:root {
  --bg: #f7f5ef;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --ink: #18202a;
  --muted: #627081;
  --line: #d7dde4;
  --button-hover-bg: #f8fafc;
  --button-hover-border: #aeb8c3;
  --toggle-active-bg: #26313d;
  --toggle-active-border: #26313d;
  --toggle-active-ink: #ffffff;
  --board-frame: #26313d;
  --board-grid: rgba(38, 49, 61, 0.55);
  --board-cell: #fdfdfb;
  --start-contrast: #18202a;
  --panel-shadow: 0 8px 24px rgba(24, 32, 42, 0.08);
  --row-bg: #fbfcfd;
  --active-line: #26313d;
  --swatch-border: rgba(24, 32, 42, 0.18);
  --difficulty-bg: #f5f7fa;
  --difficulty-ink: #455364;
  --status-bg: #eceff3;
  --status-ink: #596879;
  --piece-selected-bg: #ffffff;
  --mini-cell-border: rgba(24, 32, 42, 0.18);
  --game-over: #8a4b15;
  --recent-pulse: rgba(255, 255, 255, 0.62);
  --recent-line: rgba(255, 255, 255, 0.98);
  --recent-clear: rgba(255, 255, 255, 0);
  --valid: #1f9d62;
  --invalid: #cb3b3b;
  --red: #d84f4f;
  --blue: #3f78d6;
  --yellow: #e5b849;
  --green: #42a96b;
  --shadow: 0 16px 35px rgba(24, 32, 42, 0.12);
}

body[data-theme="dark"] {
  color-scheme: dark;
  --bg: #11120f;
  --surface: #1b1e1c;
  --surface-2: #242823;
  --ink: #f1eee5;
  --muted: #aaa79c;
  --line: #3d443f;
  --button-hover-bg: #252a25;
  --button-hover-border: #647064;
  --toggle-active-bg: #183d31;
  --toggle-active-border: #7dd6aa;
  --toggle-active-ink: #eafff3;
  --board-frame: #4b544d;
  --board-grid: rgba(112, 124, 114, 0.58);
  --board-cell: #171a17;
  --start-contrast: #f5f0df;
  --panel-shadow: 0 10px 26px rgba(0, 0, 0, 0.34);
  --row-bg: #20241f;
  --active-line: #9ed9bd;
  --swatch-border: rgba(241, 238, 229, 0.22);
  --difficulty-bg: #2c332d;
  --difficulty-ink: #d7d2c6;
  --status-bg: #2b312d;
  --status-ink: #c7c1b4;
  --piece-selected-bg: #2b312d;
  --mini-cell-border: rgba(241, 238, 229, 0.22);
  --game-over: #f1c76d;
  --recent-pulse: rgba(255, 244, 188, 0.5);
  --recent-line: rgba(255, 248, 204, 0.98);
  --recent-clear: rgba(255, 248, 204, 0);
  --shadow: 0 16px 35px rgba(0, 0, 0, 0.46);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  color-scheme: light;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font-weight: 700;
  transition:
    background 120ms ease,
    border-color 120ms ease,
    transform 120ms ease;
}

button:hover {
  border-color: var(--button-hover-border);
  background: var(--button-hover-bg);
}

button:active {
  transform: translateY(1px);
}

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

.toggle-button[aria-pressed="true"] {
  border-color: var(--toggle-active-border);
  background: var(--toggle-active-bg);
  color: var(--toggle-active-ink);
}

.theme-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: 72px;
  padding-inline: 10px;
}

.theme-button::before {
  content: "";
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #f0bd4e;
  box-shadow: inset -3px -3px 0 rgba(24, 32, 42, 0.16);
}

body[data-theme="dark"] .theme-button::before {
  background: #d9e6ff;
  box-shadow: inset 4px -2px 0 #8797b7;
}

input,
select {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 34px 0 12px;
  font-weight: 700;
}

input {
  width: 100%;
  padding: 0 12px;
}

.app-shell {
  width: min(1580px, 100%);
  margin: 0 auto;
  padding: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.room-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 0 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--panel-shadow);
  padding: 8px 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.room-banner strong {
  color: var(--ink);
  letter-spacing: 0.06em;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.3rem, 1.9vw, 1.9rem);
  line-height: 1.05;
}

h2 {
  font-size: 0.95rem;
  line-height: 1.2;
}

#statusText {
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.95rem;
}

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

.select-label,
.meta-label {
  color: var(--muted);
  font-size: 0.77rem;
  font-weight: 800;
  text-transform: uppercase;
}

.layout {
  display: grid;
  grid-template-columns: 205px minmax(500px, 1fr) 320px;
  gap: 14px;
  align-items: start;
}

.board-panel,
.players-panel,
.side-panel {
  min-width: 0;
}

.board-wrap {
  width: min(100%, calc(100vh - 136px));
  min-width: 320px;
  margin: 0 auto;
}

.board-instructions {
  display: flex;
  justify-content: center;
  gap: 10px 18px;
  flex-wrap: nowrap;
  width: min(100%, calc(100vh - 136px));
  min-width: 320px;
  margin: 10px auto 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 800;
  white-space: nowrap;
}

.board {
  display: grid;
  grid-template-columns: repeat(20, 1fr);
  width: 100%;
  aspect-ratio: 1;
  border: 2px solid var(--board-frame);
  background: var(--board-frame);
  box-shadow: var(--shadow);
  touch-action: manipulation;
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 1px solid var(--board-grid);
  border-radius: 0;
  background: var(--board-cell);
  appearance: none;
}

.cell.start::after {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--start-color) 74%, var(--start-contrast));
}

.cell.player-red {
  background: var(--red);
}

.cell.player-blue {
  background: var(--blue);
}

.cell.player-yellow {
  background: var(--yellow);
}

.cell.player-green {
  background: var(--green);
}

.cell.preview {
  background: var(--preview-bg);
  outline: 2px solid var(--preview-outline);
  outline-offset: -3px;
}

.cell.preview-valid {
  outline-style: solid;
}

.cell.preview-invalid {
  outline-style: dashed;
}

.cell.recent-move::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background: var(--recent-pulse);
  pointer-events: none;
  animation: recentMovePulse 1.05s ease-in-out infinite;
}

.recent-edge {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  background-image: repeating-linear-gradient(
    90deg,
    var(--recent-line) 0 6px,
    var(--recent-clear) 6px 11px
  );
  background-size: 22px 3px;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.95));
  animation: walkingEdge 0.65s linear infinite;
}

.recent-edge.top,
.recent-edge.bottom {
  right: -1px;
  left: -1px;
  height: 3px;
}

.recent-edge.top {
  top: -2px;
}

.recent-edge.bottom {
  bottom: -2px;
}

.recent-edge.left,
.recent-edge.right {
  top: -1px;
  bottom: -1px;
  width: 3px;
  background-image: repeating-linear-gradient(
    180deg,
    var(--recent-line) 0 6px,
    var(--recent-clear) 6px 11px
  );
  background-size: 3px 22px;
}

.recent-edge.left {
  left: -2px;
}

.recent-edge.right {
  right: -2px;
}

.players-panel,
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.turn-panel,
.scores-panel,
.pieces-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--panel-shadow);
}

.turn-panel {
  padding: 9px;
}

.current-player {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 9px;
}

.player-swatch {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  border: 2px solid var(--swatch-border);
  background: var(--red);
}

#currentPlayerName {
  display: block;
  margin-top: 2px;
  font-size: 1.05rem;
}

.scores-panel,
.pieces-panel {
  padding: 11px;
}

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

.turn-panel .piece-actions {
  grid-template-columns: 1fr;
}

.turn-panel button {
  min-height: 34px;
  font-size: 0.88rem;
}

.score-list {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.score-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: start;
  gap: 8px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 8px;
  background: var(--row-bg);
}

.score-row.active {
  border-color: var(--active-line);
}

.score-swatch {
  width: 15px;
  height: 15px;
  border-radius: 5px;
}

.score-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 800;
}

.score-name em {
  color: var(--muted);
  font-size: 0.7rem;
  font-style: normal;
  font-weight: 900;
  text-transform: uppercase;
}

.difficulty-button {
  min-height: 20px;
  margin-left: 5px;
  border-radius: 999px;
  padding: 1px 7px;
  background: var(--difficulty-bg);
  color: var(--difficulty-ink);
  font-size: 0.68rem;
  line-height: 1;
  text-transform: uppercase;
}

.score-value {
  grid-column: 2;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
}

.score-status {
  grid-column: 2;
  width: fit-content;
  border-radius: 999px;
  background: var(--status-bg);
  color: var(--status-ink);
  padding: 2px 7px;
  font-size: 0.68rem;
  font-weight: 900;
  text-transform: uppercase;
}

.score-status:empty {
  display: none;
}

.pieces-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.piece-tray {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  max-height: calc(100vh - 216px);
  min-height: 290px;
  overflow: auto;
  padding-right: 2px;
}

.piece-button {
  display: grid;
  place-items: center;
  min-height: 56px;
  padding: 6px;
  background: var(--surface-2);
}

.piece-button.selected {
  border-color: var(--active-line);
  background: var(--piece-selected-bg);
  box-shadow: inset 0 0 0 2px var(--active-line);
}

.piece-button.used {
  opacity: 0.35;
}

.undo-button {
  width: 100%;
  margin-top: 9px;
  min-height: 36px;
}

.piece-mini {
  display: grid;
  gap: 2px;
  width: auto;
}

.mini-cell {
  border-radius: 3px;
  background: transparent;
}

.mini-cell.filled {
  background: currentColor;
  border: 1px solid var(--mini-cell-border);
}

.game-over {
  color: var(--game-over);
  font-weight: 800;
}

.confetti-layer {
  position: fixed;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  overflow: hidden;
}

.confetti-piece {
  position: absolute;
  top: -16px;
  width: 9px;
  height: 14px;
  border-radius: 2px;
  opacity: 0.95;
  animation: confettiFall linear forwards;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.38);
}

.modal-panel {
  width: min(520px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 14px;
}

.modal-header,
.mp-room-header,
.mp-room-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 34px;
  min-height: 34px;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
}

.mp-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  margin-top: 12px;
}

.form-field {
  display: grid;
  gap: 5px;
}

.form-field span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.mp-grid .form-field:first-child {
  grid-column: 1 / -1;
}

.mp-room-card {
  display: grid;
  gap: 10px;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--row-bg);
  padding: 10px;
}

.mp-room-header strong {
  letter-spacing: 0.08em;
}

.mp-room-header span,
.mp-status {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.mp-seat-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.mp-seat {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 7px;
  font-size: 0.78rem;
  font-weight: 800;
}

.mp-seat-dot {
  width: 12px;
  height: 12px;
  border-radius: 4px;
}

.mp-seat em {
  color: var(--muted);
  font-size: 0.68rem;
  font-style: normal;
  text-transform: uppercase;
}

.mp-status {
  margin-top: 10px;
}

[hidden] {
  display: none !important;
}

@keyframes recentMovePulse {
  0%,
  100% {
    opacity: 0.18;
  }

  50% {
    opacity: 0.72;
  }
}

@keyframes walkingEdge {
  to {
    background-position: 22px 0;
  }
}

@keyframes confettiFall {
  to {
    transform: translate3d(var(--fall-x), calc(100vh + 40px), 0) rotate(var(--spin));
    opacity: 0;
  }
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 300px;
  }

  .board-wrap {
    width: min(100%, 720px);
    min-width: 0;
  }

  .players-panel {
    grid-column: 1 / -1;
    order: 2;
  }

  .board-panel {
    grid-column: 1;
  }

  .side-panel {
    grid-column: 2;
  }

  .score-list {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .piece-tray {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    max-height: none;
  }
}

@media (max-width: 760px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .players-panel,
  .board-panel,
  .side-panel {
    grid-column: 1;
  }

  .players-panel {
    order: 1;
  }

  .board-panel {
    order: 2;
  }

  .side-panel {
    order: 3;
  }

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

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

  .board-instructions {
    flex-wrap: wrap;
    white-space: normal;
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 12px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .toolbar {
    justify-content: flex-start;
    width: 100%;
  }

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

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

  .room-banner,
  .mp-room-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .mp-grid,
  .mp-seat-list {
    grid-template-columns: 1fr;
  }
}
