:root {
  --bg: #f4efe4;
  --panel: #fbf7ef;
  --panel-2: #efe8d8;
  --text: #1f1b17;
  --muted: #6d6257;
  --accent: #0e7a6d;
  --accent-strong: #09594f;
  --warn: #a35429;
  --danger: #a12626;
  --light-square: #f0dfbf;
  --dark-square: #9a774c;
  --selected: #f5c85b;
  --legal: #0e7a6d66;
  --border: #d5cab7;
  --shadow: 0 16px 40px rgba(63, 43, 23, 0.12);
  --board-size: min(680px, calc(100vw - 540px), calc(100dvh - 215px));
  font-family: "Segoe UI", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(14, 122, 109, 0.08), transparent 28%),
    radial-gradient(circle at bottom right, rgba(163, 84, 41, 0.12), transparent 24%),
    var(--bg);
  color: var(--text);
}

button,
select,
input {
  font: inherit;
}

.app-shell {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px 20px 28px;
}

.topbar,
.panel-header,
.board-footer,
.actions,
.clock-card,
.cost-grid,
.layout,
.topbar-right,
.sync-strip {
  display: flex;
}

.topbar,
.panel-header,
.board-footer,
.actions {
  align-items: center;
  justify-content: space-between;
}

.topbar {
  margin-bottom: 16px;
  gap: 16px;
}

.topbar-right {
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  text-transform: uppercase;
  font-size: 12px;
  letter-spacing: 0.08em;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 2.6vw, 3.1rem);
  line-height: 1;
}

h2 {
  font-size: 1rem;
}

.phase-pill,
.seat-pill,
.engine-status,
.side-banner {
  border-radius: 999px;
  padding: 10px 14px;
  font-size: 0.9rem;
}

.phase-pill {
  background: rgba(14, 122, 109, 0.12);
  color: var(--accent-strong);
}

.seat-pill {
  background: rgba(163, 84, 41, 0.12);
  color: var(--warn);
}

.layout {
  gap: 18px;
  align-items: stretch;
}

.hidden {
  display: none !important;
}

.lobby-panel {
  max-width: 720px;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  border-radius: 18px;
  padding: 20px;
  display: grid;
  gap: 18px;
}

.board-panel,
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.board-panel {
  flex: 1 1 auto;
  min-width: 0;
  padding: 16px;
  border-radius: 20px;
}

.control-panel {
  flex: 0 0 360px;
  min-width: 320px;
  display: grid;
  gap: 12px;
  align-content: start;
}

.panel {
  border-radius: 18px;
  padding: 16px;
}

.side-banner {
  display: inline-flex;
  background: var(--panel-2);
  margin-bottom: 16px;
}

.sync-strip {
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 14px;
}

.sync-label {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}

.match-note {
  color: var(--muted);
  font-size: 0.92rem;
}

.board-wrap {
  display: grid;
  gap: 10px;
}

.coordinates {
  width: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0;
  padding-left: 42px;
  color: var(--muted);
  font-size: 0.84rem;
}

.board-frame {
  display: grid;
  grid-template-columns: 34px var(--board-size);
  align-items: stretch;
}

.ranks {
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  align-items: center;
  justify-items: center;
  color: var(--muted);
  font-size: 0.84rem;
}

.board {
  width: var(--board-size);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, minmax(0, 1fr));
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.14);
}

.square {
  position: relative;
  border: 0;
  padding: 0;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  font-size: clamp(1.9rem, 4vw, 3.45rem);
  line-height: 1;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease;
}

.square:hover {
  filter: brightness(1.05);
}

.square.light {
  background: var(--light-square);
}

.square.dark {
  background: var(--dark-square);
}

.square.selected {
  box-shadow: inset 0 0 0 4px var(--selected);
}

.square.last-move-from,
.square.last-move-to {
  box-shadow: inset 0 0 0 4px rgba(255, 246, 145, 0.78);
}

.square.last-move-to::before {
  content: "";
  position: absolute;
  inset: 12%;
  border: 3px solid rgba(255, 246, 145, 0.88);
  border-radius: 16px;
  pointer-events: none;
}

.square.selected.last-move-from,
.square.selected.last-move-to {
  box-shadow:
    inset 0 0 0 4px var(--selected),
    inset 0 0 0 8px rgba(255, 246, 145, 0.78);
}

.square.legal::after {
  content: "";
  width: 28%;
  height: 28%;
  border-radius: 999px;
  background: var(--legal);
  position: absolute;
}

.square.occupied-legal::after {
  content: "";
  position: absolute;
  inset: 10%;
  border: 4px solid var(--legal);
  border-radius: 999px;
}

.piece {
  pointer-events: none;
  line-height: 0.95;
  font-family: "Apple Symbols", "Arial Unicode MS", "Segoe UI Symbol", "Noto Sans Symbols 2", serif;
  font-weight: 400;
  font-variant-ligatures: none;
  text-rendering: geometricPrecision;
  transform: translateY(-1%);
}

.piece.white {
  color: #fff8ec;
  text-shadow: none;
  -webkit-text-stroke: 1.15px #1f1b17;
}

.piece.black {
  color: #14100d;
  text-shadow: none;
  -webkit-text-stroke: 0.35px rgba(255, 248, 236, 0.2);
}

.board-footer {
  margin-top: 10px;
  gap: 12px;
}

.legend {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  font-size: 0.88rem;
  flex-wrap: wrap;
}

.dot {
  width: 12px;
  height: 12px;
  display: inline-block;
  border-radius: 999px;
  margin-right: 6px;
}

.dot.legal {
  background: var(--legal);
}

.dot.selected {
  background: var(--selected);
}

.clocks {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

.clock-card {
  flex-direction: column;
  gap: 4px;
  background: var(--panel-2);
  border-radius: 16px;
  padding: 12px;
  border: 1px solid transparent;
}

.clock-card.active {
  border-color: rgba(14, 122, 109, 0.45);
  box-shadow: 0 0 0 3px rgba(14, 122, 109, 0.12);
}

.clock-card.mine {
  background: #fffaf0;
}

.clock-card.mine .card-title::before {
  content: "local ";
  color: var(--accent-strong);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.clock-card.opponent {
  opacity: 0.88;
}

.clock-card.low-resource {
  border-color: rgba(161, 38, 38, 0.42);
}

.card-title {
  color: var(--muted);
  font-size: 0.9rem;
}

.clock {
  font-size: clamp(1.8rem, 3vw, 2.6rem);
  font-variant-numeric: tabular-nums;
}

.points {
  font-weight: 700;
  color: var(--accent-strong);
}

.controls-panel {
  display: grid;
  gap: 12px;
}

.engine-status {
  background: rgba(14, 122, 109, 0.12);
  color: var(--accent-strong);
}

.field {
  display: grid;
  gap: 6px;
}

.field label,
.label,
.microcopy,
.hint {
  color: var(--muted);
}

.field select {
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fffdfa;
}

.slider-field {
  display: grid;
  gap: 10px;
}

.slider-field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.slider-readout,
.engine-meta {
  color: var(--muted);
  font-size: 0.92rem;
}

.range-labels {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.profile-field {
  background: rgba(14, 122, 109, 0.07);
  border: 1px solid rgba(14, 122, 109, 0.12);
  border-radius: 14px;
  padding: 12px;
}

.profile-field strong {
  color: var(--accent-strong);
}

.checkbox-field {
  gap: 8px;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
}

.toggle-label input {
  width: 18px;
  height: 18px;
}

.cost-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  background: var(--panel-2);
  border-radius: 14px;
  padding: 14px;
}

.cost-grid.insufficient {
  background: rgba(161, 38, 38, 0.1);
  outline: 2px solid rgba(161, 38, 38, 0.24);
}

.cost-grid.insufficient #move-cost {
  color: var(--danger);
}

.cost-grid div {
  display: grid;
  gap: 2px;
}

.cost-grid strong {
  font-size: 1.15rem;
}

.actions {
  gap: 10px;
}

.button {
  min-height: 46px;
  padding: 0 16px;
  border-radius: 12px;
  border: 0;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease;
}

.button:hover {
  transform: translateY(-1px);
}

.button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.button.primary {
  background: var(--accent);
  color: white;
  flex: 1;
}

.button.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.pricing-panel table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.pricing-panel th,
.pricing-panel td {
  padding: 10px 8px;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.pricing-panel tbody tr:last-child td {
  border-bottom: 0;
}

.history-panel ol {
  margin: 12px 0 0;
  padding-left: 20px;
  max-height: 240px;
  overflow: auto;
  display: grid;
  gap: 8px;
}

.history-panel li {
  color: var(--muted);
}

.history-panel li strong {
  color: var(--text);
}

.game-over {
  color: var(--danger);
}

.result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(24, 18, 14, 0.5);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 40;
}

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

.result-modal {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 24px;
  display: grid;
  gap: 14px;
}

.result-modal h2 {
  font-size: 1.4rem;
}

.result-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

@media (max-height: 860px) and (min-width: 721px) {
  :root {
    --board-size: min(620px, calc(100vw - 500px), calc(100dvh - 180px));
  }

  .app-shell {
    padding: 16px 16px 18px;
  }

  .topbar {
    margin-bottom: 12px;
  }

  .board-panel,
  .panel {
    border-radius: 16px;
  }

  .board-panel,
  .panel {
    padding: 12px;
  }

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

  .clock {
    font-size: clamp(1.45rem, 2.6vw, 2rem);
  }

  .card-title,
  .points,
  .slider-readout,
  .engine-meta,
  .hint {
    font-size: 0.84rem;
  }

  .field {
    gap: 4px;
  }

  .slider-field {
    gap: 6px;
  }

  .pricing-panel,
  .history-panel {
    display: none;
  }
}

@media (max-width: 920px) {
  .layout {
    flex-direction: column;
  }

  .control-panel {
    min-width: 0;
  }

  .board-frame {
    grid-template-columns: 28px min(680px, calc(100vw - 82px));
  }

  .coordinates {
    width: min(680px, calc(100vw - 82px));
    padding-left: 36px;
  }

  .board {
    width: min(680px, calc(100vw - 82px));
  }
}

@media (max-width: 720px) {
  :root {
    --board-size: min(calc(100vw - 56px), calc(100dvh - 342px));
  }

  body {
    min-height: 100dvh;
  }

  .app-shell {
    padding: 8px 8px calc(220px + env(safe-area-inset-bottom));
  }

  .topbar {
    margin-bottom: 8px;
    align-items: center;
    gap: 8px;
  }

  .eyebrow,
  .phase-pill,
  .seat-pill,
  .legend,
  .profile-field,
  .hint,
  .engine-meta,
  .pricing-panel,
  .history-panel {
    display: none;
  }

  h1 {
    font-size: 1.45rem;
  }

  .topbar-right {
    margin-left: auto;
  }

  .layout {
    gap: 8px;
  }

  .board-panel,
  .panel {
    border-radius: 12px;
    box-shadow: none;
  }

  .board-panel {
    padding: 8px;
  }

  .sync-strip {
    margin-bottom: 6px;
  }

  .sync-label {
    display: none;
  }

  .match-note {
    font-size: 0.8rem;
  }

  .engine-status,
  .side-banner {
    padding: 6px 10px;
    font-size: 0.78rem;
  }

  .side-banner {
    margin-bottom: 8px;
  }

  .board-wrap {
    gap: 4px;
  }

  .board-frame {
    grid-template-columns: 20px var(--board-size);
  }

  .coordinates {
    width: var(--board-size);
    padding-left: 20px;
    font-size: 0.72rem;
  }

  .board {
    width: var(--board-size);
    border-radius: 10px;
  }

  .ranks {
    font-size: 0.72rem;
  }

  .square {
    font-size: clamp(1.3rem, 7.4vw, 1.9rem);
  }

  .board-footer {
    display: none;
  }

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

  .clock-card {
    border-radius: 10px;
    padding: 8px 10px;
    gap: 2px;
  }

  .clock {
    font-size: 1.45rem;
  }

  .card-title,
  .points {
    font-size: 0.78rem;
  }

  .controls-panel {
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: env(safe-area-inset-bottom);
    z-index: 12;
    max-height: 214px;
    overflow: hidden;
    padding: 10px 12px 12px;
    gap: 6px;
  }

  .controls-panel .panel-header {
    gap: 8px;
    order: 0;
  }

  .controls-panel h2 {
    font-size: 0.9rem;
  }

  #active-side-label {
    font-size: 0.8rem;
  }

  .field {
    gap: 3px;
  }

  .field:has(#depth-slider) {
    order: 3;
  }

  .field:has(#time-slider) {
    order: 4;
  }

  .checkbox-field {
    display: grid;
    order: 2;
    gap: 2px;
  }

  .checkbox-field .microcopy {
    display: none;
  }

  .toggle-label {
    justify-content: space-between;
    gap: 8px;
    font-size: 0.8rem;
  }

  .toggle-label input {
    width: 16px;
    height: 16px;
    margin-left: auto;
  }

  .field label,
  .label {
    font-size: 0.78rem;
  }

  .slider-field {
    gap: 3px;
  }

  .slider-readout {
    display: none;
  }

  .range-labels {
    display: none;
  }

  .cost-grid {
    display: none;
  }

  .cost-grid .label {
    font-size: 0.68rem;
  }

  .cost-grid strong {
    font-size: 0.84rem;
  }

  .actions {
    gap: 8px;
    order: 1;
  }

  .button {
    min-height: 38px;
    border-radius: 10px;
    padding: 0 10px;
    font-size: 0.88rem;
  }

  #cancel-selection-button {
    display: none;
  }

  .actions {
    flex-wrap: nowrap;
  }

  .result-modal {
    padding: 18px;
    gap: 10px;
  }
}
