:root {
  color-scheme: dark;
  --bg: #171a1f;
  --panel: #222733;
  --panel-strong: #2d3542;
  --ink: #f7f7f2;
  --muted: #aeb7c4;
  --line: #0b0d10;
  --blue: #2478ff;
  --blue-dark: #1554bd;
  --orange: #ef8f2f;
  --orange-dark: #b76417;
  --green: #78c83b;
  --purple: #8e57d2;
  --good: #2bd39f;
  --danger: #eb5757;
  --winner-color: #88929f;
  --wood-dark: #8a532c;
  --wood-mid: #a86a35;
  --wood-light: #c18446;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
}

body {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.035), transparent 42%),
    var(--bg);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 0;
}

.app {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.matchmaking {
  min-height: 60vh;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 16px;
}

.matchmaking-actions {
  width: min(100%, 640px);
  display: grid;
  gap: 12px;
}

.matchmaking-row {
  display: grid;
  grid-template-columns: minmax(112px, 1fr) repeat(2, minmax(0, 220px));
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.matchmaking-title {
  color: var(--ink);
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: 0;
  text-align: right;
  white-space: nowrap;
}

.matchmaking-button {
  min-width: 220px;
  min-height: 64px;
  padding: 0 28px;
  border-radius: 8px;
  color: white;
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: 0;
  cursor: pointer;
}

@media (max-width: 720px) {
  .matchmaking-actions {
    width: min(100%, 456px);
  }

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

  .matchmaking-title {
    text-align: center;
  }
}

.matchmaking-button:disabled {
  cursor: wait;
  opacity: 0.74;
}

.find-game-button {
  background: var(--blue);
  box-shadow: 0 18px 45px rgba(36, 120, 255, 0.22);
}

.find-game-button:hover:not(:disabled) {
  background: #3287ff;
}

.find-game-button.searching {
  background: var(--panel-strong);
  box-shadow: 0 18px 45px rgba(0, 0, 0, 0.24);
}

.play-bot-button {
  background: var(--orange);
  box-shadow: 0 18px 45px rgba(239, 143, 47, 0.18);
}

.play-bot-button:hover:not(:disabled) {
  background: #f5a246;
}

.connection-status,
.game-status {
  min-height: 24px;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.game-screen {
  width: min(100%, 920px);
  min-height: calc(100vh - 40px);
  display: grid;
  grid-template-rows: auto minmax(0, auto) auto auto;
  gap: 14px;
  justify-items: center;
  align-content: center;
}

.game-header {
  width: min(100%, 760px);
  min-height: 44px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 0 4px;
}

.game-title {
  color: var(--ink);
  font-weight: 800;
  letter-spacing: 0;
}

.round-label,
.phase-label {
  min-width: 96px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  text-align: center;
  font-size: 0.92rem;
  font-weight: 700;
}

.phase-label {
  color: var(--good);
}

.board-frame {
  position: relative;
  width: min(100%, 760px, calc((100vh - 190px) * 1.08));
  max-height: calc(100vh - 190px);
  aspect-ratio: 1.08 / 1;
  border: 2px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.06), transparent 45%),
    #111419;
  overflow: hidden;
  box-shadow: 0 22px 52px rgba(0, 0, 0, 0.35);
}

.board-frame.csel-board-frame {
  width: min(100%, 820px, calc((100vh - 190px) * 0.9));
  aspect-ratio: 0.9 / 1;
}

.board-frame.game-over {
  border-color: var(--winner-color);
  box-shadow:
    0 0 0 3px color-mix(in srgb, var(--winner-color) 42%, transparent),
    0 22px 52px rgba(0, 0, 0, 0.38);
}

.board-frame.game-over .board-svg {
  filter: grayscale(1);
}

.board-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.score-badge {
  position: absolute;
  top: 16px;
  z-index: 3;
  width: 122px;
  height: 76px;
  pointer-events: none;
}

.score-badge-blue {
  left: 18px;
}

.score-badge-orange {
  right: 18px;
}

.score-hex {
  position: absolute;
  top: 0;
  width: 82px;
  height: 72px;
  display: grid;
  place-items: center;
  clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.16), transparent 45%),
    var(--wood-mid);
  color: var(--blue);
  font-size: 2.45rem;
  font-weight: 950;
  line-height: 1;
  text-shadow: 0 2px 0 rgba(255, 255, 255, 0.2);
  filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.35));
}

.score-badge-blue .score-hex {
  left: 0;
}

.score-badge-orange .score-hex {
  right: 0;
  color: var(--orange);
}

.score-circle {
  position: absolute;
  top: 22px;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.9);
  color: white;
  font-size: 0.96rem;
  font-weight: 950;
  line-height: 1;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.34));
}

.score-badge-blue .score-circle {
  left: 66px;
  background: var(--blue);
}

.score-badge-orange .score-circle {
  right: 66px;
  background: var(--orange);
}

.hex-cell {
  stroke: var(--line);
  stroke-width: 1.65;
  stroke-linejoin: round;
}

.stack {
  transform-box: fill-box;
  transform-origin: center;
}

.stack circle {
  stroke: rgba(255, 255, 255, 0.9);
  stroke-width: 3.2;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.38));
}

.stack text {
  fill: #ffffff;
  font-size: 37px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.stack-blue circle {
  fill: var(--blue);
}

.stack-orange circle {
  fill: var(--orange);
}

.csel-flag circle,
.csel-piece circle {
  fill: #3d2a1d;
  stroke-width: 3.2;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.38));
}

.csel-flag-green circle,
.csel-piece-green circle {
  stroke: var(--green);
}

.csel-flag-purple circle,
.csel-piece-purple circle {
  stroke: var(--purple);
}

.csel-flag-pole,
.csel-flag-banner {
  stroke-linecap: square;
}

.csel-flag-green .csel-flag-pole,
.csel-flag-green .csel-flag-banner {
  stroke: var(--green);
  fill: var(--green);
}

.csel-flag-purple .csel-flag-pole,
.csel-flag-purple .csel-flag-banner {
  stroke: var(--purple);
  fill: var(--purple);
}

.csel-flag-pole {
  stroke-width: 3;
}

.csel-flag-banner {
  stroke-width: 1.5;
}

.csel-piece-symbol {
  font-size: 32px;
  font-weight: 950;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
  stroke: #1c130d;
  stroke-width: 1.4;
  paint-order: stroke fill;
}

.csel-piece-green .csel-piece-symbol {
  fill: var(--green);
}

.csel-piece-purple .csel-piece-symbol {
  fill: var(--purple);
}

.stack.selectable {
  cursor: pointer;
}

.stack.selectable:hover circle,
.csel-piece.selectable:hover circle {
  stroke-width: 4;
}

.csel-piece.selectable {
  cursor: pointer;
}

.selection-ring {
  fill: none;
  stroke: #f7f7f2;
  stroke-width: 4.8;
  stroke-dasharray: 9 7;
  pointer-events: none;
}

.arrow-button {
  color: var(--danger);
  cursor: pointer;
}

.arrow-head-path {
  fill: var(--danger);
}

.arrow-button .arrow-hit {
  stroke: transparent;
  stroke-width: 16;
  fill: none;
}

.arrow-button .arrow-line {
  stroke: currentColor;
  stroke-width: 5;
  stroke-linecap: round;
  fill: none;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.55));
}

.arrow-button:hover .arrow-line,
.arrow-button.selected .arrow-line {
  stroke: currentColor;
}

.selection-panel {
  position: absolute;
  width: 58px;
  padding: 0;
  background: transparent;
  z-index: 3;
}

.amount-control {
  display: grid;
  justify-items: center;
  gap: 10px;
}

.amount-slider {
  width: 30px;
  height: 190px;
  accent-color: var(--blue);
  direction: rtl;
  writing-mode: vertical-lr;
}

.amount-slider::-webkit-slider-runnable-track {
  width: 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.7);
}

.amount-slider::-webkit-slider-thumb {
  width: 26px;
  height: 26px;
  margin-left: -7px;
  border-radius: 50%;
}

.amount-slider::-moz-range-track {
  width: 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.7);
}

.amount-slider::-moz-range-thumb {
  width: 26px;
  height: 26px;
  border-radius: 50%;
}

.amount-input {
  width: 58px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: #0f1217;
  color: var(--ink);
  text-align: center;
  font-weight: 900;
}

.board-actions {
  width: min(100%, 700px);
  min-height: 48px;
  display: grid;
  grid-template-columns: auto minmax(150px, 220px) minmax(180px, 280px);
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.board-actions.csel-actions {
  width: min(100%, 500px);
  grid-template-columns: auto minmax(150px, 220px);
}

.history-controls {
  display: grid;
  grid-template-columns: repeat(4, 42px);
  gap: 6px;
}

.history-button {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-size: 1rem;
  font-weight: 950;
  line-height: 1;
  cursor: pointer;
}

.history-button:hover:not(:disabled) {
  background: var(--panel-strong);
}

.history-button:disabled {
  color: rgba(247, 247, 242, 0.34);
  cursor: not-allowed;
}

.submit-button,
.lobby-button {
  width: 100%;
  min-height: 48px;
  border-radius: 8px;
  color: white;
  font-weight: 900;
  cursor: pointer;
}

.submit-button {
  background: var(--blue);
}

.lobby-button {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: var(--ink);
}

.lobby-button:hover {
  background: var(--panel-strong);
}

.lobby-button.forfeit-button {
  background: var(--danger);
  border-color: rgba(255, 255, 255, 0.22);
  color: white;
}

.lobby-button.forfeit-button:hover {
  background: #f06464;
}

.submit-button:disabled {
  background: var(--panel-strong);
  color: var(--muted);
  cursor: not-allowed;
}

.stack.fade-out,
.csel-piece.fade-out {
  animation: fadeStack 680ms ease forwards;
}

@keyframes fadeStack {
  to {
    opacity: 0;
  }
}

.moving-stack {
  pointer-events: none;
}

.end-banner {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  min-height: 64px;
  display: grid;
  place-items: center;
  background: rgba(10, 12, 15, 0.82);
  color: white;
  font-size: clamp(1.3rem, 4vw, 2.1rem);
  font-weight: 950;
  letter-spacing: 0;
  text-transform: uppercase;
  z-index: 4;
}

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

  .matchmaking-actions {
    width: min(100%, 300px);
    grid-template-columns: 1fr;
  }

  .matchmaking-button {
    min-width: 0;
    width: 100%;
  }

  .game-header {
    grid-template-columns: 1fr 1fr;
  }

  .game-title {
    grid-column: 1 / -1;
    text-align: center;
  }

  .board-frame {
    width: min(100%, calc((100vh - 210px) * 0.86));
    max-height: calc(100vh - 210px);
    aspect-ratio: 0.86 / 1;
  }

  .board-frame.csel-board-frame {
    width: min(100%, calc((100vh - 210px) * 0.82));
    aspect-ratio: 0.82 / 1;
  }

  .selection-panel {
    width: 54px;
  }

  .amount-slider {
    height: 150px;
  }

  .board-actions {
    width: min(100%, 360px);
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .history-controls {
    justify-content: center;
  }
}
