:root {
  --bg: #171512;
  --panel-bg: #262421;
  --panel-border: #3a3733;
  --text: #e9e6e0;
  --text-dim: #a3a09a;
  --accent: #81b64c;
  --accent-hover: #97cc5f;
  --danger: #c0392b;
  --danger-hover: #d64536;
  --square-light: #eeeed2;
  --square-dark: #769656;
  --square-highlight: #f6f66977;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* Board color palettes — selected via the Customize panel (data-board on <html>). */
[data-board="brown"] { --square-light: #f0d9b5; --square-dark: #b58863; }
[data-board="blue"] { --square-light: #dee3e6; --square-dark: #8ca2ad; }
[data-board="gray"] { --square-light: #dcdcdc; --square-dark: #7d7d7d; }
[data-board="purple"] { --square-light: #e8dff0; --square-dark: #9771b5; }
[data-board="midnight"] { --square-light: #7d8796; --square-dark: #3d4753; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
}

.brand {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent);
}

.conn-status {
  font-size: 0.8rem;
  color: var(--text-dim);
}
.conn-status.ok { color: var(--accent); }
.conn-status.bad { color: var(--danger-hover); }

.view { flex: 1; padding: 1.25rem; }
.view-hidden { display: none; }

/* --- Lobby --- */
.lobby-grid {
  display: grid;
  grid-template-columns: minmax(260px, 380px) minmax(260px, 480px);
  gap: 1.25rem;
  max-width: 900px;
  margin: 0 auto;
}
@media (max-width: 720px) {
  .lobby-grid { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
}

.panel h2 { margin-top: 0; font-size: 1rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.panel h3 { font-size: 0.85rem; color: var(--text-dim); margin: 1rem 0 0.5rem; text-transform: uppercase; letter-spacing: 0.04em; }

.create-panel label {
  display: block;
  margin-bottom: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}
.create-panel input[type="text"],
.create-panel select {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem;
  background: #1e1c19;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  color: var(--text);
  font-size: 0.95rem;
}
.checkbox-row { display: flex; align-items: center; gap: 0.5rem; }
.checkbox-row input { width: auto; margin: 0; }

.btn {
  padding: 0.55rem 1rem;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  background: #33312d;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}
.btn:hover { background: #3d3a35; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #14260a; font-weight: 700; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }

.join-code-row { display: flex; gap: 0.5rem; }
.join-code-row input {
  flex: 1;
  padding: 0.5rem;
  background: #1e1c19;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  color: var(--text);
}

.open-games-list { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow-y: auto; }
.open-games-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--panel-border);
  font-size: 0.9rem;
}
.open-games-list li.empty-hint { color: var(--text-dim); justify-content: center; }
.open-games-list .btn { padding: 0.3rem 0.7rem; font-size: 0.8rem; }

/* --- Game view --- */
.game-grid {
  display: grid;
  grid-template-columns: minmax(300px, 640px) minmax(260px, 360px);
  gap: 1.25rem;
  max-width: 1040px;
  margin: 0 auto;
  align-items: start;
}
@media (max-width: 820px) {
  .game-grid { grid-template-columns: 1fr; }
}

.player-strip {
  display: flex;
  justify-content: space-between;
  padding: 0.4rem 0.2rem;
  font-size: 0.95rem;
}
.player-name { font-weight: 600; }
.clock {
  font-variant-numeric: tabular-nums;
  background: #1e1c19;
  border-radius: 4px;
  padding: 0.15rem 0.55rem;
  min-width: 4.5em;
  text-align: center;
}
.clock.low { color: var(--danger-hover); }
.clock:empty { display: none; } /* untimed games: no empty gray pill */

/* Keep the whole board on screen: the column may never be wider than the
   viewport height minus the surrounding chrome (matters in phone landscape,
   where the single-column layout would otherwise make the board ~2 screens
   tall). dvh with a vh fallback for older browsers. */
.board-column {
  width: 100%;
  max-width: min(640px, calc(100vh - 190px));
  max-width: min(640px, calc(100dvh - 190px));
  margin: 0 auto;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 2px solid var(--panel-border);
  border-radius: 4px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
}

.square { position: relative; display: flex; align-items: center; justify-content: center; }
.square.light { background: var(--square-light); }
.square.dark { background: var(--square-dark); }
.square.selected::before {
  content: ""; position: absolute; inset: 0; background: var(--square-highlight);
}
.square.last-move::before {
  content: ""; position: absolute; inset: 0; background: #ffdd0055;
}
.square .move-dot {
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: #00000030;
  pointer-events: none;
}
.square .move-dot.capture {
  width: 92%;
  height: 92%;
  background: transparent;
  border: 5px solid #00000030;
  border-radius: 50%;
}

.piece {
  width: 88%;
  height: 88%;
  cursor: grab;
  touch-action: none;
}
.piece.dragging {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  width: 64px;
  height: 64px;
  filter: drop-shadow(0 2px 6px rgba(0,0,0,0.5));
}

.rank-label, .file-label {
  position: absolute;
  font-size: 0.6rem;
  font-weight: 700;
  opacity: 0.55;
  pointer-events: none;
}
.rank-label { top: 2px; left: 3px; }
.file-label { bottom: 1px; right: 3px; }
.hide-coords .rank-label, .hide-coords .file-label { display: none; }
.square.light .rank-label, .square.light .file-label { color: var(--square-dark); }
.square.dark .rank-label, .square.dark .file-label { color: var(--square-light); }

.game-actions { display: flex; gap: 0.5rem; margin-top: 0.75rem; flex-wrap: wrap; }

.side-column { display: flex; flex-direction: column; gap: 1rem; }
.moves-panel { max-height: 220px; overflow-y: auto; }
.move-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 0.15rem 0.5rem; font-size: 0.9rem; }
.move-list li { padding: 0.1rem 0; }
.move-list .move-num { color: var(--text-dim); margin-right: 0.35rem; }

.chat-panel { display: flex; flex-direction: column; flex: 1; min-height: 260px; }
.chat-log { flex: 1; overflow-y: auto; margin-bottom: 0.5rem; font-size: 0.88rem; max-height: 320px; }
.chat-log .msg { margin-bottom: 0.35rem; word-wrap: break-word; }
.chat-log .msg .nick { font-weight: 700; color: var(--accent); }
.chat-log .msg.spectator .nick { color: var(--text-dim); }
.chat-log .msg.system { color: var(--text-dim); font-style: italic; }
.chat-form { display: flex; gap: 0.5rem; }
.chat-form input {
  flex: 1;
  padding: 0.5rem;
  background: #1e1c19;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  color: var(--text);
}

/* --- Modal / toast --- */
.modal {
  position: fixed; inset: 0; background: #000000a0;
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-hidden { display: none; }
.modal-box { background: var(--panel-bg); border: 1px solid var(--panel-border); border-radius: 8px; padding: 1.25rem; text-align: center; }
.promotion-choices { display: flex; gap: 0.75rem; margin-top: 0.75rem; }
.promotion-choices img { width: 56px; height: 56px; cursor: pointer; background: #33312d; border-radius: 6px; padding: 4px; }
.promotion-choices img:hover { background: var(--accent); }

/* --- Customize panel --- */
.topbar-right { display: flex; align-items: center; gap: 0.75rem; }
.btn-small { padding: 0.3rem 0.7rem; font-size: 0.8rem; }

.settings-box {
  text-align: left;
  width: min(92vw, 440px);
  max-height: 86vh;
  overflow-y: auto;
}
.settings-box h3 { margin: 0 0 0.25rem; }
.settings-box h4 {
  margin: 1rem 0 0.5rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}
.theme-choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  padding: 0.4rem 0.2rem;
  background: #1e1c19;
  border: 2px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 0.68rem;
  cursor: pointer;
}
.theme-choice img { width: 38px; height: 38px; }
.theme-choice:hover { border-color: var(--accent-hover); }
.theme-choice.active { border-color: var(--accent); color: var(--text); }

.swatch-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.board-swatch {
  width: 44px;
  height: 44px;
  border: 2px solid var(--panel-border);
  border-radius: 6px;
  cursor: pointer;
}
.board-swatch:hover { border-color: var(--accent-hover); }
.board-swatch.active { border-color: var(--accent); }

.settings-colors { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.color-row { display: flex; align-items: center; gap: 0.4rem; font-size: 0.85rem; color: var(--text-dim); }
.color-row input[type="color"] {
  width: 34px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  background: #1e1c19;
  cursor: pointer;
}

.settings-box .checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  font-size: 0.9rem;
}
.settings-footer { margin-top: 1rem; text-align: right; }

.toast {
  position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%);
  background: #2b2926; border: 1px solid var(--panel-border); border-radius: 6px;
  padding: 0.6rem 1.1rem; font-size: 0.9rem; z-index: 200;
}
.toast-hidden { display: none; }

/* --- Wizard's Gambit (arcane mode) --- */
:root {
  --spell-fire: #ff7a45;
  --spell-frost: #7cc7ff;
  --spell-arcane: #b48cff;
  --spell-shield: #ffd75e;
}

.mode-hint {
  margin: -0.35rem 0 0.75rem;
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--spell-arcane);
}
.mode-hint-hidden { display: none; }

.mode-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  align-self: flex-start;
  margin-bottom: 0.35rem;
  padding: 0.3rem 0.6rem;
  background: none;
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--spell-arcane);
  border-radius: 999px;
  color: var(--spell-arcane);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-shadow: 0 0 10px #b48cff55;
  transition: background 0.18s ease, box-shadow 0.18s ease;
}
.mode-badge:hover { background: #b48cff1a; box-shadow: 0 0 10px #b48cff44; }
.mode-badge:focus-visible { outline: 2px solid var(--spell-arcane); outline-offset: 2px; }
.mode-badge svg { width: 14px; height: 14px; }
.mode-badge .badge-rules-link {
  margin-left: 0.15rem;
  padding: 0.05rem 0.45rem;
  border-radius: 999px;
  background: #b48cff22;
  font-weight: 600;
  text-transform: lowercase;
}
.mode-badge-hidden { display: none; }

.link-btn {
  background: none;
  border: none;
  padding: 0;
  margin-left: 0.25rem;
  color: var(--spell-arcane);
  font: inherit;
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
}
.link-btn:hover { color: #cbb0ff; }

/* Rules modal */
.rules-box {
  text-align: left;
  width: min(92vw, 480px);
  max-height: 86vh;
  max-height: 86dvh;
  overflow-y: auto;
}
.rules-box h3 { display: flex; align-items: center; gap: 0.5rem; margin: 0 0 0.25rem; color: var(--spell-arcane); }
.rules-title-icon { width: 20px; height: 20px; }
.rules-box h4 {
  margin: 1rem 0 0.35rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.rules-box p, .rules-box li { font-size: 0.88rem; line-height: 1.55; }
.rules-intro { color: var(--text-dim); margin: 0; }
.rules-spells { list-style: none; margin: 0; padding: 0; }
.rules-spells li {
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  padding: 0.45rem 0;
  border-bottom: 1px solid var(--panel-border);
}
.rules-spells li:last-child { border-bottom: none; }
.rules-spells em { color: var(--spell-arcane); font-style: normal; font-weight: 700; }
.rules-icon { width: 20px; height: 20px; flex-shrink: 0; margin-top: 2px; }
.spell-fireball-c { color: var(--spell-fire); }
.spell-freeze-c { color: var(--spell-frost); }
.spell-teleport-c { color: var(--spell-arcane); }
.spell-shield-c { color: var(--spell-shield); }
.rules-fineprint { margin: 0; padding-left: 1.1rem; color: var(--text-dim); }
.rules-fineprint li { margin-bottom: 0.3rem; }

.player-id { display: flex; align-items: center; gap: 0.6rem; min-width: 0; }

.mana-pips { display: inline-flex; align-items: center; gap: 2px; }
.mana-pip {
  width: 7px;
  height: 12px;
  border-radius: 2px;
  background: #1e1c19;
  border: 1px solid var(--panel-border);
}
.mana-pip.filled {
  background: var(--spell-arcane);
  border-color: var(--spell-arcane);
  box-shadow: 0 0 6px #b48cff66;
}
.mana-count {
  margin-left: 0.35rem;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: var(--spell-arcane);
  font-weight: 700;
}

.spell-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin-top: 0.75rem;
}
.spell-bar-hidden { display: none; }

.spell-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 44px;
  padding: 0.45rem 0.5rem;
  background: #1e1c19;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}
.spell-btn svg { width: 18px; height: 18px; flex-shrink: 0; }
.spell-btn .spell-cost {
  padding: 0.05rem 0.4rem;
  border-radius: 999px;
  background: #12101d;
  border: 1px solid var(--spell-arcane);
  color: var(--spell-arcane);
  font-size: 0.72rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.spell-fireball svg { color: var(--spell-fire); }
.spell-freeze svg { color: var(--spell-frost); }
.spell-teleport svg { color: var(--spell-arcane); }
.spell-shield svg { color: var(--spell-shield); }

.spell-btn:hover:not(:disabled) { border-color: var(--spell-arcane); background: #262138; }
.spell-btn:focus-visible { outline: 2px solid var(--spell-arcane); outline-offset: 2px; }
.spell-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.spell-btn.casting {
  border-color: var(--spell-arcane);
  box-shadow: 0 0 10px #b48cff66;
  background: #2c2545;
}

.targeting-hint {
  margin: 0.45rem 0 0;
  font-size: 0.8rem;
  color: var(--spell-arcane);
  text-align: center;
}
.targeting-hint-hidden { display: none; }

@media (max-width: 480px) {
  .spell-bar { grid-template-columns: repeat(2, 1fr); }
  .mana-pip { width: 5px; height: 10px; }
  /* Compact topbar: keep the brand on one line, drop the "connected" chip
     (disconnection warnings still show). */
  .topbar { padding: 0.5rem 0.75rem; gap: 0.5rem; }
  .brand { font-size: 0.95rem; white-space: nowrap; }
  .conn-status.ok { display: none; }
  .view { padding: 0.75rem; }
}

/* Targeting highlights */
.board.targeting .square { cursor: crosshair; }
.square.targetable::after {
  content: "";
  position: absolute;
  inset: 6%;
  border: 2px solid var(--spell-arcane);
  border-radius: 6px;
  box-shadow: 0 0 8px #b48cff88 inset;
  pointer-events: none;
  animation: fx-target-pulse 1.2s ease-in-out infinite;
}
@keyframes fx-target-pulse {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}

/* Persistent overlays */
.fx-frost {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  padding: 6%;
  background: linear-gradient(135deg, #7cc7ff33, #b7e3ff55);
  color: var(--spell-frost);
  font-size: 0.95rem;
  text-shadow: 0 0 6px #7cc7ffcc;
  pointer-events: none;
}
.fx-frost .frost-turns {
  font-size: 0.62rem;
  font-weight: 800;
  margin-left: 2px;
  color: #eaf6ff;
}
.square.shielded::after {
  content: "";
  position: absolute;
  inset: 4%;
  border: 2px solid var(--spell-shield);
  border-radius: 50%;
  box-shadow: 0 0 10px #ffd75e88, 0 0 10px #ffd75e44 inset;
  pointer-events: none;
}

/* Transient FX */
.fx-burst { position: absolute; inset: 0; pointer-events: none; z-index: 30; }
.fx-particle {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: radial-gradient(circle, #ffe08a, var(--spell-fire) 60%, transparent);
  animation: fx-scatter 0.55s ease-out forwards;
}
@keyframes fx-scatter {
  0% { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
  100% { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(0.2); opacity: 0; }
}
.square.fx-scorch::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, #ff7a45aa 0%, #3a1a0888 65%, transparent 100%);
  animation: fx-fade 0.65s ease-out forwards;
  pointer-events: none;
}
.square.fx-freeze-flash::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, #b7e3ffcc, #7cc7ff55 70%, transparent);
  animation: fx-fade 0.6s ease-out forwards;
  pointer-events: none;
}
.square.fx-teleport-flash::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, #d8c5ffcc, #b48cff55 70%, transparent);
  animation: fx-fade 0.55s ease-out forwards;
  pointer-events: none;
}
.square.fx-shield-flash::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, #ffe9a8cc, #ffd75e55 70%, transparent);
  animation: fx-fade 0.6s ease-out forwards;
  pointer-events: none;
}
@keyframes fx-fade {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .square.targetable::after { animation: none; opacity: 0.85; }
  .fx-particle,
  .square.fx-scorch::before,
  .square.fx-freeze-flash::before,
  .square.fx-teleport-flash::before,
  .square.fx-shield-flash::before { animation: none; opacity: 0; }
}
