:root {
  --bg: #1b1c22;
  --panel: #24262e;
  --ink: #e9e6df;
  --muted: #9a9aa4;
  --accent: #f2b705;
  --white-tile: #f0e2c0;
  --white-edge: #cbb888;
  --black-tile: #363a46;
  --black-edge: #1d2029;
  --ghost: #f2b705;
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* excludes mobile browser chrome so trays aren't pushed off */
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid #00000040;
}

.status {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  letter-spacing: 0.2px;
  text-transform: capitalize;
}

.status.over {
  color: var(--accent);
}

/* Equal flex on the two sides lets the auto-width brand sit dead centre. */
.brand {
  flex: none;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: 3px;
  color: var(--accent);
  text-decoration: none;
  cursor: pointer;
}

.brand:hover {
  color: #ffd758;
}

.controls {
  display: flex;
  flex: 1;
  justify-content: flex-end;
  gap: 8px;
}

button {
  font: inherit;
  color: var(--ink);
  background: #313440;
  border: 1px solid #00000040;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: #3a3e4c;
}

button:disabled {
  opacity: 0.4;
  cursor: default;
}

.board {
  flex: 1;
  width: 100%;
  min-height: 0;
  background: radial-gradient(circle at 50% 40%, #22242c, #16171c);
  cursor: grab;
  touch-action: none;
}

.board:active {
  cursor: grabbing;
}

.tile-hex {
  stroke-width: 2;
}

.tile.white .tile-hex {
  fill: var(--white-tile);
  stroke: var(--white-edge);
}

.tile.black .tile-hex {
  fill: var(--black-tile);
  stroke: var(--black-edge);
}

.tile {
  cursor: pointer;
}

/* The outer `.tile` owns the final hex position. Only this inner artwork group
   moves, so piece motion composes cleanly with the world's pan/zoom transform. */
.tile-body {
  transform-box: fill-box;
  transform-origin: center;
}

.tile.tile-moving .tile-body {
  animation: tile-move var(--tile-move-duration, 260ms)
    cubic-bezier(0.2, 0.82, 0.25, 1) both;
  will-change: transform, filter;
}

.tile.tile-entering .tile-body {
  animation: tile-enter 240ms cubic-bezier(0.2, 0.82, 0.25, 1) both;
  will-change: transform, opacity, filter;
}

@keyframes tile-move {
  0% {
    transform: translate(var(--tile-from-x), var(--tile-from-y)) scale(0.96);
    filter: drop-shadow(0 2px 1px #0005);
  }
  72% {
    transform: translate(0, 0) scale(1.045);
    filter: drop-shadow(0 7px 5px #0008);
  }
  100% {
    transform: translate(0, 0) scale(1);
    filter: drop-shadow(0 2px 1px #0003);
  }
}

@keyframes tile-enter {
  0% {
    opacity: 0;
    transform: translateY(12px) rotate(-6deg) scale(0.58);
    filter: drop-shadow(0 1px 1px #0000);
  }
  68% {
    opacity: 1;
    transform: translateY(-2px) rotate(1deg) scale(1.08);
    filter: drop-shadow(0 7px 5px #0008);
  }
  100% {
    opacity: 1;
    transform: translateY(0) rotate(0) scale(1);
    filter: drop-shadow(0 2px 1px #0003);
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile.tile-moving .tile-body,
  .tile.tile-entering .tile-body {
    animation: none;
  }
}

.glyph {
  font-size: 30px;
  text-anchor: middle;
  dominant-baseline: middle;
}

.tile-shadow {
  fill: #00000055;
}

.piece-num {
  font-size: 12px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}

.tile.white .piece-num {
  fill: #6b5a2a;
}

.tile.black .piece-num {
  fill: #c9c6bf;
}

.depth {
  font-size: 13px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
  fill: var(--accent);
  paint-order: stroke;
  stroke: #000;
  stroke-width: 3px;
}

.ghost {
  fill: var(--ghost);
  fill-opacity: 0.16;
  stroke: var(--ghost);
  stroke-width: 2.5;
  stroke-dasharray: 5 4;
  cursor: pointer;
}

.ghost:hover {
  fill-opacity: 0.32;
}

.selected {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3.5;
  pointer-events: none;
}

.trays {
  display: flex;
  gap: 12px;
  padding: 10px 16px 16px;
  background: var(--panel);
  border-top: 1px solid #00000040;
  flex-wrap: wrap;
}

.tray {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: #1e2027;
}

.tray.active {
  border-color: var(--accent);
}

.tray-chips {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.tray-label {
  display: flex;
  flex-direction: column;
  min-width: 64px;
  font-weight: 600;
  text-transform: capitalize;
  color: var(--muted);
}

.tray.active .tray-label {
  color: var(--ink);
}

.tray-owner {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.3px;
  color: var(--accent);
}

.chip {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  border-radius: 8px;
}

.chip-glyph {
  font-size: 20px;
}

.chip-count {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  min-width: 10px;
}

.chip.chosen {
  outline: 2px solid var(--accent);
  background: #3a3e4c;
}

.hint {
  display: none;
}

@media (min-width: 720px) {
  .hint {
    display: block;
    padding: 8px 16px;
    color: var(--muted);
    font-size: 13px;
    background: var(--panel);
  }
}

/* ---- pre-game setup panel ---- */
.setup[hidden] {
  display: none;
}

/* The setup form used to live inline on the lobby. It now only exists inside
   the new-game modal, so these rules only ever apply there. */
.setup {
  display: flex;
  justify-content: center;
  padding: 20px 16px;
  background: var(--bg);
}

/* ---- lobby ("your games" list) ---- */
.lobby[hidden] {
  display: none;
}

.lobby {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 32px 24px;
  background: var(--bg);
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.lobby-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.lobby-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--muted);
}

.lobby-new-btn {
  font-weight: 600;
  padding: 8px 14px;
}

.lobby-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.lobby-row {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 14px 18px;
  background: var(--panel);
  border: 1px solid #00000040;
  border-radius: 10px;
  text-align: left;
  cursor: pointer;
  transition: background 80ms;
}

.lobby-row:hover:not(:disabled) {
  background: #2c2f38;
}

.lobby-dot {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1.5px solid #00000060;
}

.lobby-dot.white {
  background: var(--white-tile);
}

.lobby-dot.black {
  background: var(--black-tile);
}

/* Local games have no seat colour — a hollow ring reads as "neutral / yours". */
.lobby-dot-local {
  background: transparent;
  border-color: var(--accent);
  border-style: dashed;
}

.lobby-middle {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.lobby-opp {
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lobby-meta {
  font-size: 12px;
  color: var(--muted);
}

.lobby-badge {
  flex: none;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.3px;
  text-transform: lowercase;
  background: #313440;
  color: var(--muted);
  border: 1px solid #00000040;
}

.lobby-badge-your-turn {
  background: #4d3c00;
  color: var(--accent);
  border-color: #6a5400;
}

.lobby-badge-in-progress {
  background: #2a303b;
  color: var(--ink);
}

.lobby-badge-their-turn {
  background: #2a303b;
  color: var(--ink);
}

.lobby-badge-finished {
  background: #2a2a30;
  color: var(--muted);
}

.lobby-placeholder {
  padding: 24px 18px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  background: var(--panel);
  border: 1px dashed #00000060;
  border-radius: 10px;
}

.lobby-panel {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: flex-start;
  padding: 24px;
  background: var(--panel);
  border: 1px solid #00000040;
  border-radius: 12px;
}

.lobby-panel .lobby-title {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  color: var(--ink);
}

@media (max-width: 719px) {
  .lobby {
    padding: 18px 14px;
    gap: 14px;
  }

  .lobby-row {
    padding: 12px 14px;
    gap: 10px;
  }

  .lobby-opp {
    font-size: 14px;
  }
}

/* ---- new-game modal ---- */
.new-game-modal .modal-panel {
  min-width: 280px;
  max-width: 460px;
}

.new-game-modal .modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.new-game-modal .modal-title {
  font-weight: 600;
  font-size: 16px;
}

.setup-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
  min-width: 220px;
  padding: 20px;
  background: var(--panel);
  border: 1px solid #00000040;
  border-radius: 12px;
}

.setup-title {
  font-weight: 600;
  text-transform: capitalize;
  color: var(--muted);
}

.setup-panel .range {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.setup-panel .range input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.setup-modes {
  display: flex;
  gap: 6px;
}

.setup-modes button {
  flex: 1;
  text-transform: capitalize;
}

.setup-modes button.on {
  border-color: var(--accent);
  color: var(--accent);
}

.name-input {
  font: inherit;
  color: var(--ink);
  background: #1b1d24;
  border: 1px solid #00000060;
  border-radius: 8px;
  padding: 7px 10px;
  width: 100%;
}

.name-input:focus {
  outline: none;
  border-color: var(--accent);
}

.setup-expansions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 4px;
  border-top: 1px solid #00000026;
}

.setup-subtitle {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 2px;
}

.setup-panel .check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  text-transform: capitalize;
  cursor: pointer;
}

.setup-panel .check input[type="checkbox"] {
  accent-color: var(--accent);
  cursor: pointer;
}

/* ---- layout: history sidebar beside the board ---- */
.main {
  display: flex;
  flex: 1;
  min-height: 0;
  min-width: 0;
}

.stage {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  /* Without this a flex item won't shrink below its content, so the wide tray
     row would stretch the whole page instead of scrolling inside itself. */
  min-width: 0;
}

/* The board and its floating controls, above the trays. */
.board-wrap {
  position: relative; /* anchor for the floating zoom controls */
  flex: 1;
  min-height: 0;
  display: flex;
}

/* ---- floating zoom controls (over the board) ---- */
.zoom-controls {
  position: absolute;
  right: 12px;
  bottom: 12px;
  display: none; /* shown only where a mouse wheel isn't the primary zoom */
  flex-direction: column;
  gap: 6px;
  z-index: 5;
}

.zoom-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
  border-radius: 10px;
  background: #313440e6;
  box-shadow: 0 2px 8px #0006;
}

@media (pointer: coarse), (max-width: 719px) {
  .zoom-controls {
    display: flex;
  }
}

.history {
  flex: none;
  width: 208px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid #00000040;
  overflow-y: auto;
}

.history[hidden] {
  display: none;
}

/* Tap-catcher behind the mobile history drawer; inert (display:none) elsewhere. */
.history-backdrop {
  display: none;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px 6px 12px;
}

.history-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--muted);
}

.legend-btn {
  padding: 1px 8px;
  font-size: 12px;
  line-height: 1.4;
}

.history-legend {
  padding: 6px 12px 10px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--muted);
  border-bottom: 1px solid #00000030;
}

.legend-sec {
  margin-top: 8px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: var(--muted);
}

.legend-row {
  margin-top: 3px;
}

.legend-row b {
  color: var(--ink);
  font-weight: 700;
}

.legend-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  margin-top: 4px;
}

.legend-grid span:nth-child(odd) {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--accent);
}

.history-list {
  display: flex;
  flex-direction: column;
}

.history-row {
  text-align: left;
  border: none;
  border-radius: 0;
  background: transparent;
  padding: 5px 12px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.history-row:hover:not(:disabled) {
  background: #2f323c;
}

.history-row.current {
  background: #3a3e4c;
  color: var(--accent);
  font-weight: 700;
}

.resume-btn {
  margin: 10px 12px;
}

.controls button.on {
  border-color: var(--accent);
  color: var(--accent);
}

.notify-btn {
  /* The bell button is contextual (only appears in online seated games), so
     it borrows the same accent treatment as a `.on` toggle to make its active
     state visible without adding an extra modifier. */
  min-width: 76px;
}

.notify-btn[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- join modal (online) ---- */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0009;
  z-index: 10;
}

.modal[hidden] {
  display: none;
}

.modal-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 260px;
  padding: 22px;
  background: var(--panel);
  border: 1px solid #00000060;
  border-radius: 12px;
  box-shadow: 0 12px 40px #0007;
}

.modal-title {
  font-weight: 600;
  font-size: 16px;
}

.modal-panel .link-btn {
  background: none;
  border: none;
  color: var(--muted);
  padding: 2px;
  font-size: 13px;
}

.modal-panel .link-btn:hover:not(:disabled) {
  background: none;
  color: var(--ink);
}

/* ---- piece help ---- */
.help-btn {
  flex: none;
  padding: 0;
  width: 22px;
  height: 22px;
  line-height: 1;
  border-radius: 50%;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.help-btn.on {
  border-color: var(--accent);
  color: var(--accent);
  background: #3a3e4c;
}

.tray.help-mode .chip {
  cursor: help;
}

.help-panel {
  max-width: 340px;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.help-title {
  font-weight: 600;
  font-size: 17px;
}

.help-body {
  color: var(--muted);
  line-height: 1.5;
  user-select: text;
}

/* ---- seat link modal (online) ---- */
.seat-link-panel {
  min-width: 320px;
  max-width: 480px;
}

.seat-link-panel .modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.seat-link-panel .modal-title {
  font-weight: 600;
  font-size: 17px;
}

.modal-hint {
  color: var(--muted);
  line-height: 1.4;
  user-select: text;
  font-size: 14px;
}

.seat-link-input {
  font: 13px ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--ink);
  background: #1a1c24;
  border: 1px solid #00000060;
  border-radius: 6px;
  padding: 8px 10px;
  width: 100%;
  user-select: text;
  -webkit-user-select: text;
}

.seat-link-input:focus {
  outline: none;
  border-color: var(--accent);
}

.icon-btn {
  padding: 2px 8px;
  line-height: 1;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 16px;
}

.icon-btn:hover:not(:disabled) {
  background: none;
  color: var(--ink);
}

/* ---- touch: fatten tap targets so fingers don't miss ---- */
@media (pointer: coarse) {
  button {
    padding: 10px 14px;
  }
  .chip {
    padding: 10px 12px;
  }
  .chip-glyph {
    font-size: 24px;
  }
  .help-btn {
    width: 32px;
    height: 32px;
    font-size: 15px;
  }
  .history-row {
    padding: 11px 12px;
  }
}

/* ---- compact mode: hide the page chrome so the board gets more room ---- */
/* Always-visible floating toggle (mirrors .zoom-controls bottom-right). */
.chrome-toggle {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 5;
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  border-radius: 10px;
  background: #313440e6;
  box-shadow: 0 2px 8px #0006;
}

/* Mirror of .chrome-toggle for the bottom bar: pinned bottom-left so it
   can't collide with the bottom-right zoom controls, and stays reachable even
   when the trays themselves are hidden (the whole point of the toggle). */
.tray-toggle {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 5;
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  border-radius: 10px;
  background: #313440e6;
  box-shadow: 0 2px 8px #0006;
}

@media (pointer: coarse), (max-width: 719px) {
  .chrome-toggle {
    top: 16px;
    right: 16px;
  }

  .tray-toggle {
    bottom: 16px;
    left: 16px;
  }
}

/* The whole point: shrink the topbar to a thin status strip. The status text
   stays — players need to know whose turn it is — but the brand and the
   button row (undo, history, notifications, new game) disappear. */
#app.compact .brand,
#app.compact .controls {
  display: none;
}

/* Hide the bottom piece trays on demand so the board wins back the height.
   The .tray-toggle button sits over the board (not inside the trays), so
   hiding this row can never trap the user. */
#app.trays-hidden .trays {
  display: none;
}

#app.compact .topbar {
  padding: 6px 14px;
  gap: 0;
}

/* On phones the topbar stacks into three rows; compact collapses it back to
   a single status line so the board wins the height it stole. */
@media (max-width: 719px) {
  #app.compact .topbar {
    padding: 6px 12px;
    padding-top: calc(6px + env(safe-area-inset-top));
  }

  #app.compact .status {
    flex: 1 1 100%;
    order: 0;
    text-align: center;
    font-size: 13px;
  }
}

/* ---- phones: single-column, touch-first layout ---- */
@media (max-width: 719px) {
  .topbar {
    padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top));
    gap: 8px;
    flex-wrap: wrap;
  }

  .brand {
    flex: 1 1 100%;
    order: -2; /* title row on top */
    text-align: center;
  }

  .status {
    flex: 1 1 100%;
    order: -1; /* status below the title, above the buttons */
    font-size: 14px;
    text-align: center;
  }

  .controls {
    flex: 1 1 100%;
    flex-wrap: wrap;
    justify-content: center;
  }

  /* History becomes a slide-over drawer instead of stealing board width. */
  .main {
    position: relative;
  }

  .history {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(300px, 84vw);
    border-right: 1px solid #00000060;
    box-shadow: 8px 0 32px #000a;
    z-index: 30;
  }

  .history-backdrop:not([hidden]) {
    display: block;
    position: absolute;
    inset: 0;
    background: #0007;
    z-index: 20;
  }

  /* Each player gets their own full-width row so Black is never scrolled
     off-screen; the chips within a row scroll sideways if expansions make
     them overflow, keeping the colour label pinned. */
  .trays {
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 8px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  }

  .tray {
    width: 100%;
  }

  .tray-label {
    min-width: 56px;
  }

  .tray-chips {
    flex: 1 1 auto;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .tray-chips::-webkit-scrollbar {
    display: none;
  }

  /* Trim chip width (not height) so more fit per view before scrolling. */
  .tray-chips .chip {
    padding: 10px 8px;
  }

  .tray {
    gap: 6px;
    padding: 8px;
  }

  .tray-label {
    min-width: 48px;
    font-size: 13px;
  }

  /* Keep the zoom buttons clear of the trays. */
  .zoom-controls {
    bottom: 16px;
    right: 16px;
  }
}

/* ---- concede: give up the current game ---- */

/* The concede button lives in the controls bar; muted red text on the normal
   button chrome reads as "destructive" without screaming. The matching modal
   uses the same accent so the affordance stays consistent from button to
   confirmation. */
.controls .concede-btn {
  color: #d97a7a;
  border-color: #5a3030;
}

.controls .concede-btn:hover:not(:disabled) {
  background: #4a3030;
  color: #f0a8a8;
}

.concede-modal .modal-panel {
  min-width: 300px;
  max-width: 440px;
}

.concede-modal .modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.concede-modal .modal-title {
  font-weight: 600;
  font-size: 17px;
  color: #d97a7a;
}

.concede-panel .modal-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  margin-top: 4px;
}

.concede-panel .concede-confirm {
  background: #5a3030;
  border-color: #803838;
  color: #f0d4d4;
}

.concede-panel .concede-confirm:hover:not(:disabled) {
  background: #6e3a3a;
  border-color: #9a4444;
  color: #fff;
}
