/* ==========================================================================
   Rayah — "Sungging" Design System
   An inlaid writing-tablet, not a dashboard. The board is the hero;
   everything else is quiet chrome. Depth comes from contrast and space,
   not borders. Borders survive only on small tactile objects (tiles).
   ========================================================================== */

@font-face {
  font-family: 'Ngayogyan';
  src: url('nykngayogyannew.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}


:root {
  /* ---- radius: sharp on structure, soft only where the hand touches ---- */
  --radius-panel: 2px;
  --radius-tile: 10px;
  --radius-cell: 3px;
  --radius-button: 999px;
  --radius-input: 3px;
  --radius-dialog: 4px;
  --radius-badge: 999px;

  /* ---- spacing (8pt system, generous) ---- */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --sp-7: 40px;
  --sp-8: 56px;
  --sp-9: 72px;
  --sp-10: 96px;
  --sp-11: 128px;

  /* ---- type scale: Fraunces for voice, Inter for utility ---- */
  --font-display: 'Fraunces', 'Iowan Old Style', serif;
  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
  --fs-display: 64px;
  --fs-h1: 44px;
  --fs-h2: 32px;
  --fs-h3: 24px;
  --fs-h4: 19px;
  --fs-body: 16px;
  --fs-small: 14px;
  --fs-caption: 12px;
  --fs-micro: 10.5px;

  /* ---- motion ---- */
  --dur-1: 140ms;
  --dur-2: 220ms;
  --dur-3: 320ms;
  --dur-4: 480ms;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

:root[data-theme="dark"] {
  --bg: #100D0A;
  --bg-deep: #0A0908;
  --surface: #1B1611;
  --surface-2: #221B15;
  --ink: #E9E1D2;
  --ink-soft: #A99C86;
  --ink-faint: #6E6252;
  --hairline: rgba(233, 225, 210, 0.09);
  --gold: #D8B565;
  --gold-bright: #F0CE86;
  --gold-dim: rgba(216, 181, 101, 0.14);
  --rust: #B4694A;
  --jade: #6E9E8C;
  --danger: #D9776A;

  --shadow-soft: 0 1px 0 rgba(0,0,0,0.4);
  --shadow-lift: 0 12px 32px rgba(0,0,0,0.45);
  --shadow-deep: 0 28px 64px rgba(0,0,0,0.55);

  --board-bg: #171310;
  --board-inlay: #0C0A08;
  --cell-normal: #1D1712;
  --cell-tw: #7A5A24;
  --cell-tw-text: #F5E3B8;
  --cell-dw: #4A3320;
  --cell-dw-text: #E7C79E;
  --cell-tl: #244138;
  --cell-tl-text: #BFE3D3;
  --cell-dl: #1D3630;
  --cell-dl-text: #9CC2B4;
  --cell-center: #D8B565;
  --cell-center-text: #171310;

  --tile-bg: #EFE6D2;
  --tile-bg-alt: #E4D8BE;
  --tile-ink: #241C12;
  --tile-border: rgba(36, 28, 18, 0.18);
  --tile-score: #7A5A24;
}

:root[data-theme="light"] {
  --bg: #F1E9D8;
  --bg-deep: #E9DFC9;
  --surface: #FBF7EC;
  --surface-2: #F3ECDC;
  --ink: #2A2118;
  --ink-soft: #6B5D48;
  --ink-faint: #9C8E77;
  --hairline: rgba(42, 33, 24, 0.1);
  --gold: #93701F;
  --gold-bright: #7A5A17;
  --gold-dim: rgba(147, 112, 31, 0.12);
  --rust: #8C4A30;
  --jade: #3F6B58;
  --danger: #A6402E;

  --shadow-soft: 0 1px 0 rgba(42,33,24,0.06);
  --shadow-lift: 0 12px 28px rgba(42,33,24,0.14);
  --shadow-deep: 0 28px 56px rgba(42,33,24,0.2);

  --board-bg: #E3D6B4;
  --board-inlay: #D2C29A;
  --cell-normal: #EBE0C4;
  --cell-tw: #93701F;
  --cell-tw-text: #FBF3DE;
  --cell-dw: #C9A467;
  --cell-dw-text: #4A3417;
  --cell-tl: #6E9585;
  --cell-tl-text: #F4FAF6;
  --cell-dl: #A9C3B6;
  --cell-dl-text: #21382E;
  --cell-center: #93701F;
  --cell-center-text: #FBF3DE;

  --tile-bg: #FFFCF4;
  --tile-bg-alt: #F5EDD9;
  --tile-ink: #2A2118;
  --tile-border: rgba(42, 33, 24, 0.12);
  --tile-score: #93701F;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: var(--fs-body);
  line-height: 1.5;
  transition: background var(--dur-3) var(--ease-out), color var(--dur-3) var(--ease-out);
  -webkit-font-smoothing: antialiased;
}

body { display: flex; flex-direction: column; height: 100dvh; overflow: auto; }
body.in-game { overflow: hidden; }
body.in-game .screen { height: 100dvh; }

h1, h2, h3, h4 { margin: 0; font-family: var(--font-display); font-weight: 500; letter-spacing: -0.01em; }

.hidden { display: none !important; }
button { font-family: inherit; }

:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
  border-radius: 4px;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--hairline); border-radius: 999px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ==========================================================================
   Header — a signature, not a toolbar
   ========================================================================== */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-5) var(--sp-6) var(--sp-2);
  background: transparent;
  flex: 0 0 auto;
  z-index: 20;
}

body.in-game .app-header {
  position: fixed;
  top: var(--sp-4);
  right: var(--sp-4);
  left: auto;
  padding: 0;
  width: auto;
  pointer-events: none;
}
body.in-game .app-header .brand { display: none; }
body.in-game .app-header .header-actions { pointer-events: auto; }

.brand { display: flex; align-items: center; gap: var(--sp-3); }
.brand-glyph {
  font-family: 'Ngayogyan', 'Noto Sans Javanese', serif;
  font-size: 30px;
  color: var(--gold);
  line-height: 1;
}
.brand-text h1 {
  font-family: var(--font-display);
  font-size: var(--fs-h4);
  font-weight: 500;
  margin: 0;
}
.brand-text span {
  font-size: var(--fs-micro);
  color: var(--ink-faint);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-weight: 500;
}

.header-actions { display: flex; gap: var(--sp-2); }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--surface);
  color: var(--ink);
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-soft);
  transition: transform var(--dur-2) var(--ease-out), color var(--dur-2) var(--ease-out), box-shadow var(--dur-2) var(--ease-out);
}
.icon-btn:hover { color: var(--gold); box-shadow: var(--shadow-lift); }
.icon-btn:active { transform: scale(0.92); }
body.in-game .icon-btn { box-shadow: var(--shadow-lift); opacity: 0.94; }
body.in-game .icon-btn:hover { opacity: 1; }

/* ==========================================================================
   Screens & shells
   ========================================================================== */

.screen {
  flex: 1 1 auto;
  min-height: 0;
  padding: var(--sp-5);
  overflow: auto;
  display: flex;
  flex-direction: column;
  position: relative;
}
#setup-screen, #end-screen, #lobby-screen { align-items: center; justify-content: center; }
#table-screen { padding: var(--sp-3) var(--sp-4) 0; align-items: stretch; justify-content: flex-start; overflow: hidden; }

/* Hero watermark on the setup screen — atmosphere, not decoration */
.hero-mark {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Ngayogyan', 'Noto Sans Javanese', serif;
  font-size: min(46vw, 560px);
  color: var(--ink);
  opacity: 0.035;
  line-height: 1;
  pointer-events: none;
  user-select: none;
  overflow: hidden;
}

.setup-card {
  max-width: 420px;
  width: 100%;
  background: transparent;
  padding: 0;
  position: relative;
  z-index: 1;
  animation: card-in var(--dur-4) var(--ease-out);
}
.setup-kicker {
  font-size: var(--fs-micro);
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: var(--gold);
  font-weight: 600;
  margin-bottom: var(--sp-3);
}
.setup-card h2 {
  font-size: var(--fs-display);
  margin-bottom: var(--sp-7);
  color: var(--ink);
}
.setup-card.wide { max-width: 420px; }

@keyframes card-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ==========================================================================
   Mode tabs — quiet underline, not a segmented box
   ========================================================================== */

.mode-tabs {
  display: flex;
  gap: var(--sp-5);
  margin-bottom: var(--sp-7);
  border-bottom: 1px solid var(--hairline);
}
.mode-tab {
  padding: 0 0 var(--sp-3);
  border: none;
  background: transparent;
  color: var(--ink-faint);
  font-size: var(--fs-small);
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  position: relative;
  transition: color var(--dur-2) var(--ease-out);
}
.mode-tab::after {
  content: '';
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 2px;
  background: var(--gold);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform var(--dur-2) var(--ease-out);
}
.mode-tab:hover { color: var(--ink); }
.mode-tab.active { color: var(--ink); }
.mode-tab.active::after { transform: scaleX(1); }

.mode-panel input[type="text"] {
  width: 100%;
  padding: var(--sp-3) 0;
  border: none;
  border-bottom: 1px solid var(--hairline);
  background: transparent;
  color: var(--ink);
  font-size: var(--fs-body);
  margin-bottom: var(--sp-5);
  border-radius: 0;
  transition: border-color var(--dur-2) var(--ease-out);
}
.mode-panel input[type="text"]:focus {
  outline: none;
  border-color: var(--gold);
}

.or-divider {
  text-align: center;
  font-size: var(--fs-micro);
  color: var(--ink-faint);
  margin: var(--sp-4) 0 var(--sp-5);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-weight: 500;
}
.ghost-btn.full { width: 100%; }
#lobby-invite-btn { margin: var(--sp-2) 0 var(--sp-6); }
.lobby-code {
  font-family: var(--font-display);
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--gold);
}
.hand-hint { font-size: var(--fs-small); color: var(--ink-soft); margin-bottom: var(--sp-4); }
.move-msg { min-height: 16px; font-size: var(--fs-caption); color: var(--danger); margin-bottom: var(--sp-2); text-align: center; }
#lobby-start-btn { margin-bottom: var(--sp-3); }

.fame-list { display: flex; flex-direction: column; max-height: 360px; overflow-y: auto; }
.fame-row {
  display: flex; align-items: center; gap: var(--sp-4);
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--hairline);
  font-size: var(--fs-small);
}
.fame-row:last-child { border-bottom: none; }
.fame-row .fame-rank { width: 28px; font-family: var(--font-display); font-weight: 500; color: var(--ink-faint); }
.fame-row .fame-name { flex: 1; font-weight: 600; }
.fame-row .fame-score { color: var(--gold); font-weight: 700; font-variant-numeric: tabular-nums; }

.field-label { display: block; margin-bottom: var(--sp-3); color: var(--ink-soft); font-size: var(--fs-small); font-weight: 500; }
#player-count { width: 100%; margin-bottom: var(--sp-6); accent-color: var(--gold); }
.name-inputs { display: flex; flex-direction: column; gap: var(--sp-4); margin-bottom: var(--sp-6); }
.name-field label { display: block; font-size: var(--fs-caption); color: var(--ink-faint); margin-bottom: var(--sp-1); font-weight: 500; }
.name-field input {
  width: 100%;
  padding: var(--sp-2) 0;
  border: none;
  border-bottom: 1px solid var(--hairline);
  background: transparent;
  color: var(--ink);
  font-size: var(--fs-small);
  border-radius: 0;
  transition: border-color var(--dur-2) var(--ease-out);
}
.name-field input:focus { outline: none; border-color: var(--gold); }

/* ==========================================================================
   Buttons
   ========================================================================== */

.primary-btn {
  width: 100%;
  padding: var(--sp-4) var(--sp-5);
  border-radius: var(--radius-button);
  border: none;
  background: var(--gold);
  color: var(--bg-deep);
  font-weight: 700;
  font-size: var(--fs-small);
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: background var(--dur-2) var(--ease-out), transform var(--dur-1) var(--ease-out), box-shadow var(--dur-2) var(--ease-out);
  box-shadow: var(--shadow-soft);
}
.primary-btn.small { width: auto; padding: var(--sp-3) var(--sp-6); }
.primary-btn:disabled { opacity: 0.35; cursor: not-allowed; box-shadow: none; }
.primary-btn:not(:disabled):hover { background: var(--gold-bright); box-shadow: var(--shadow-lift); }
.primary-btn:not(:disabled):active { transform: scale(0.98); }

.ghost-btn {
  padding: var(--sp-3) var(--sp-5);
  border-radius: var(--radius-button);
  border: 1px solid var(--hairline);
  background: transparent;
  color: var(--ink);
  font-size: var(--fs-caption);
  font-weight: 600;
  cursor: pointer;
  transition: border-color var(--dur-2) var(--ease-out), color var(--dur-2) var(--ease-out), transform var(--dur-1) var(--ease-out);
}
.ghost-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.ghost-btn:not(:disabled):hover { border-color: var(--gold); color: var(--gold); }
.ghost-btn:not(:disabled):active { transform: scale(0.98); }
.ghost-btn.small { padding: var(--sp-2) var(--sp-4); font-size: var(--fs-micro); flex: 0 0 auto; }

/* ==========================================================================
   Open rooms — browsable list instead of a blind code field
   ========================================================================== */

.open-rooms-list { display: flex; flex-direction: column; margin-bottom: var(--sp-2); }
.open-room-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--hairline);
}
.open-room-row:last-child { border-bottom: none; }
.open-room-code {
  font-family: var(--font-display);
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--gold-bright);
  font-size: var(--fs-h4);
  flex: 0 0 auto;
}
.open-room-meta {
  flex: 1 1 auto;
  font-size: var(--fs-caption);
  color: var(--ink-faint);
  min-width: 0;
}
button.open-rooms-toggle {
  margin-top: var(--sp-2);
  text-align: center;
  color: var(--gold);
  font-weight: 600;
  font-size: var(--fs-caption);
  border: none;
  background: transparent;
  padding: var(--sp-2) 0;
}
button.open-rooms-toggle:hover { color: var(--gold-bright); }

/* ==========================================================================
   Table / board topbar — a caption above the board, not a toolbar
   ========================================================================== */

.game-shell {
  width: 100%;
  max-width: 900px;
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  margin: 0 auto;
}

.board-topbar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--sp-4);
  padding: 0 var(--sp-2) var(--sp-3);
  flex: 0 0 auto;
  flex-wrap: wrap;
}
.turn-info { display: flex; flex-direction: column; gap: 2px; }
.turn-label { font-size: var(--fs-micro); text-transform: uppercase; letter-spacing: 0.14em; color: var(--ink-faint); font-weight: 600; }
.turn-name { font-family: var(--font-display); font-size: var(--fs-h3); font-weight: 500; color: var(--gold-bright); }
.deck-info { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; font-size: var(--fs-micro); color: var(--ink-faint); text-transform: uppercase; letter-spacing: 0.1em; }
#deck-count { font-family: var(--font-display); font-weight: 500; color: var(--ink); font-size: var(--fs-h4); letter-spacing: 0; text-transform: none; }

/* ==========================================================================
   Board — the hero. An inlaid slab, ~70% of the frame's attention.
   ========================================================================== */

.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-2) 0;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(15, minmax(0, 1fr));
  grid-template-rows: repeat(15, minmax(0, 1fr));
  width: 640px;
  height: 640px;
  max-width: 100%;
  max-height: 100%;
  background: var(--board-inlay);
  border-radius: var(--radius-panel);
  overflow: hidden;
  box-shadow: var(--shadow-deep);
  margin: 0 auto;
  padding: 3px;
  gap: 2px;
}

.cell {
  position: relative;
  border-radius: 1px;
  background: var(--cell-normal);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--dur-2) var(--ease-out), box-shadow var(--dur-2) var(--ease-out);
}
.cell:hover:not(.filled) { background: color-mix(in srgb, var(--gold) 16%, var(--cell-normal)); }
.cell.bonus-tw { background: var(--cell-tw); color: var(--cell-tw-text); }
.cell.bonus-dw { background: var(--cell-dw); color: var(--cell-dw-text); }
.cell.bonus-tl { background: var(--cell-tl); color: var(--cell-tl-text); }
.cell.bonus-dl { background: var(--cell-dl); color: var(--cell-dl-text); }
.cell.bonus-center { background: var(--cell-center); color: var(--cell-center-text); }
.cell::before {
  content: attr(data-label);
  font-size: clamp(6px, 1.05vmin, 9px);
  font-weight: 700;
  color: currentColor;
  opacity: 0.85;
  letter-spacing: 0.02em;
}
.cell.filled::before { content: ''; }
.cell.filled {
  background: linear-gradient(160deg, var(--tile-bg-alt), var(--tile-bg));
  box-shadow: inset 0 0 0 1px var(--tile-border);
  animation: place-pop var(--dur-3) var(--ease-out);
}
.cell.staged {
  background: linear-gradient(160deg, var(--tile-bg-alt), var(--tile-bg));
  box-shadow: inset 0 0 0 2px var(--gold);
}
@keyframes place-pop {
  from { transform: scale(0.85); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}
.cell-glyph {
  font-family: 'Ngayogyan', 'Noto Sans Javanese', serif;
  font-size: clamp(10px, 2.5vmin, 22px);
  color: var(--tile-ink);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.cell-score {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: clamp(5px, 0.85vmin, 8px);
  font-weight: 700;
  color: var(--tile-score);
}

/* ==========================================================================
   Score strip — small ledger entries, not chips in boxes
   ========================================================================== */

.score-strip { display: flex; gap: var(--sp-4); flex-wrap: wrap; align-items: baseline; }
.score-chip {
  display: flex; align-items: baseline; gap: var(--sp-2);
  padding: 0;
  border-bottom: 2px solid transparent;
  font-size: var(--fs-caption);
  transition: border-color var(--dur-2) var(--ease-out), opacity var(--dur-2) var(--ease-out);
  opacity: 0.6;
}
.score-chip.active { border-color: var(--gold); opacity: 1; }
.chip-name { font-weight: 600; }
.chip-score { color: var(--ink-faint); font-weight: 600; font-variant-numeric: tabular-nums; }

/* ==========================================================================
   Rack & tiles — a floating dock, translucent, minimal chrome
   ========================================================================== */

.rack-area {
  flex: 0 0 auto;
  width: 100%;
  padding: var(--sp-4) var(--sp-2) max(var(--sp-4), env(safe-area-inset-bottom));
  border-top: 1px solid var(--hairline);
}
.rack { display: flex; gap: var(--sp-2); flex-wrap: wrap; justify-content: center; margin-bottom: var(--sp-4); }

.rack-tile {
  width: 60px; height: 84px;
  border-radius: var(--radius-tile);
  border: 1px solid var(--tile-border);
  background: linear-gradient(160deg, var(--tile-bg-alt), var(--tile-bg));
  color: var(--tile-ink);
  display: flex; flex-direction: column; align-items: stretch; justify-content: flex-start;
  cursor: grab;
  user-select: none;
  touch-action: none;
  box-shadow: var(--shadow-lift);
  position: relative;
  flex-shrink: 0;
  padding: var(--sp-1) 2px var(--sp-1);
  transition: transform var(--dur-2) var(--ease-out), box-shadow var(--dur-2) var(--ease-out), opacity var(--dur-2) var(--ease-out);
}
.rack-tile:hover:not(.dragging-source) { transform: translateY(-6px); box-shadow: var(--shadow-deep); }
.rack-tile.selected { outline: 2px solid var(--gold); transform: translateY(-8px); box-shadow: var(--shadow-deep); }
.rack-tile.dragging-source { opacity: 0.3; }
.rack-tile.hint { outline: 2px solid var(--jade); animation: hint-pulse 1.1s ease-in-out infinite alternate; }
.rack-tile.hint .hint-order {
  position: absolute; top: -8px; left: -8px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--jade);
  color: #0E1712;
  font-size: 10px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-soft);
}
@keyframes hint-pulse {
  from { box-shadow: 0 0 0 0 color-mix(in srgb, var(--jade) 45%, transparent); }
  to { box-shadow: 0 0 0 6px transparent; }
}

.tile-glyph {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  font-family: 'Ngayogyan', 'Noto Sans Javanese', serif;
  font-size: 23px;
  line-height: 1;
  overflow: visible;
}
.tile-label {
  flex: 0 0 auto;
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: center;
  color: var(--tile-ink);
  opacity: 0.6;
  border-top: 1px solid var(--tile-border);
  padding-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tile-score {
  position: absolute;
  top: 3px;
  right: 5px;
  font-size: 9px; font-weight: 700; color: var(--tile-score);
}

.drag-ghost {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  cursor: grabbing;
  transform: rotate(-3deg);
  box-shadow: var(--shadow-deep);
  opacity: 0.96;
}
.cell.drop-target {
  outline: 2px solid var(--gold);
  outline-offset: -2px;
  box-shadow: 0 0 0 4px var(--gold-dim);
  z-index: 1;
}

.rack-actions { display: flex; gap: var(--sp-2); justify-content: center; }
.tool-btn {
  flex: 1;
  padding: var(--sp-3) var(--sp-2);
  border-radius: var(--radius-button);
  border: 1px solid var(--hairline);
  background: var(--surface);
  color: var(--ink);
  font-size: var(--fs-caption);
  font-weight: 600;
  cursor: pointer;
  transition: border-color var(--dur-2) var(--ease-out), background var(--dur-2) var(--ease-out), transform var(--dur-1) var(--ease-out), color var(--dur-2) var(--ease-out);
}
.tool-btn.primary { background: var(--gold); color: var(--bg-deep); border-color: var(--gold); flex: 1.4; }
.tool-btn.primary:not(:disabled):hover { background: var(--gold-bright); }
.tool-btn.icon-only {
  flex: 0 0 44px;
  padding: 0;
  height: 44px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tool-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.tool-btn:not(:disabled):hover { border-color: var(--gold); color: var(--gold); }
.tool-btn.primary:not(:disabled):hover { color: var(--bg-deep); }
.tool-btn:not(:disabled):active { transform: scale(0.97); }

/* ==========================================================================
   Toast
   ========================================================================== */

.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  background: var(--ink);
  color: var(--bg-deep);
  padding: var(--sp-3) var(--sp-6);
  border-radius: var(--radius-button);
  font-size: var(--fs-small);
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-3) var(--ease-out), transform var(--dur-3) var(--ease-out);
  z-index: 90;
  max-width: 90vw;
  text-align: center;
  box-shadow: var(--shadow-deep);
}
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* ==========================================================================
   End screen / results — a ledger, not cards
   ========================================================================== */

.end-body { display: flex; flex-direction: column; margin: var(--sp-6) 0 var(--sp-7); }
.result-row {
  display: flex; align-items: center; gap: var(--sp-4);
  padding: var(--sp-4) 0;
  border-bottom: 1px solid var(--hairline);
  transition: opacity var(--dur-2) var(--ease-out);
}
.result-row.winner { border-color: var(--gold); }
.result-rank { font-family: var(--font-display); font-weight: 500; color: var(--ink-faint); width: 32px; }
.result-name { flex: 1; font-weight: 600; }
.result-score { font-family: var(--font-display); font-size: var(--fs-h3); font-weight: 500; color: var(--gold-bright); }

/* ==========================================================================
   Modals — a sheet of paper, not a card with a shadow stack
   ========================================================================== */

.modal {
  position: fixed; inset: 0;
  background: rgba(10, 9, 8, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  z-index: 60;
  padding: var(--sp-5);
  animation: fade-in var(--dur-3) var(--ease-out);
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.modal-card {
  background: var(--surface);
  border-radius: var(--radius-dialog);
  padding: var(--sp-7);
  width: 100%;
  max-width: 340px;
  box-shadow: var(--shadow-deep);
  position: relative;
  animation: modal-in var(--dur-3) var(--ease-out);
}
@keyframes modal-in {
  from { opacity: 0; transform: scale(0.97) translateY(8px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-card h3 { font-size: var(--fs-h4); margin-bottom: var(--sp-4); }
.modal-card.wide { max-width: 460px; }
.modal-hint { font-size: var(--fs-caption); color: var(--ink-soft); margin: 0 0 var(--sp-4); line-height: 1.5; }
.exchange-count {
  font-size: var(--fs-small);
  font-weight: 600;
  color: var(--gold);
  text-align: center;
  margin-bottom: var(--sp-4);
}
.exchange-tiles {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-3);
  justify-content: center;
  margin-bottom: var(--sp-1);
}
.exchange-pick { cursor: pointer; transition: transform var(--dur-2) var(--ease-out); }
.exchange-pick:hover { transform: translateY(-3px); }
.exchange-pick.selected {
  outline: 2px solid var(--gold);
  transform: translateY(-6px);
  box-shadow: 0 0 0 4px var(--gold-dim);
}
.exchange-pick.selected::after {
  content: '\2713';
  position: absolute;
  bottom: 3px;
  left: 4px;
  width: 15px; height: 15px;
  border-radius: 50%;
  background: var(--gold);
  color: var(--bg-deep);
  font-size: 10px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.modal-card label { display: block; font-size: var(--fs-caption); color: var(--ink-faint); margin: var(--sp-4) 0 var(--sp-2); font-weight: 500; text-transform: uppercase; letter-spacing: 0.08em; }
.modal-card select {
  width: 100%;
  padding: var(--sp-3) 0;
  border-radius: 0;
  border: none;
  border-bottom: 1px solid var(--hairline);
  background: transparent;
  color: var(--ink);
  font-size: var(--fs-small);
  transition: border-color var(--dur-2) var(--ease-out);
}
.modal-card select:focus { outline: none; border-color: var(--gold); }
.modal-actions { display: flex; justify-content: flex-end; gap: var(--sp-3); margin-top: var(--sp-6); }

/* ==========================================================================
   Menu drawer
   ========================================================================== */

.menu-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 9, 8, 0.55);
  backdrop-filter: blur(3px);
  z-index: 70;
  animation: fade-in var(--dur-3) var(--ease-out);
}
.menu-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(360px, 88vw);
  background: var(--surface);
  box-shadow: var(--shadow-deep);
  z-index: 71;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  animation: drawer-in var(--dur-3) var(--ease-out);
}
@keyframes drawer-in {
  from { transform: translateX(28px); opacity: 0.6; }
  to { transform: translateX(0); opacity: 1; }
}
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-5) var(--sp-6);
  border-bottom: 1px solid var(--hairline);
  flex: 0 0 auto;
}
.drawer-head h3 { font-size: var(--fs-h4); }
.drawer-body { padding: var(--sp-5) var(--sp-6) var(--sp-7); display: flex; flex-direction: column; gap: var(--sp-1); }
.drawer-btn {
  padding: var(--sp-3) 0;
  border: none;
  background: transparent;
  color: var(--ink);
  font-size: var(--fs-small);
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  border-bottom: 1px solid var(--hairline);
  transition: color var(--dur-2) var(--ease-out);
}
.drawer-btn:hover { color: var(--gold); }
.drawer-section { margin-top: var(--sp-5); padding-top: var(--sp-5); border-top: 1px solid var(--hairline); }
.drawer-section:first-of-type { margin-top: var(--sp-2); }
.drawer-label {
  display: block;
  font-size: var(--fs-micro);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-faint);
  margin-bottom: var(--sp-2);
  font-weight: 600;
}
.drawer-value { font-family: var(--font-display); font-size: var(--fs-h3); font-weight: 500; color: var(--gold-bright); }
.turn-history { display: flex; flex-direction: column; max-height: 220px; overflow-y: auto; }
.history-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--sp-2) 0;
  border-bottom: 1px solid var(--hairline);
  font-size: var(--fs-caption);
}
.history-player { font-weight: 600; }
.history-word { color: var(--ink-soft); }
.drawer-rules { margin: 0; padding-left: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-2); font-size: var(--fs-small); color: var(--ink-soft); line-height: 1.5; }
.drawer-rules b { color: var(--ink); font-weight: 600; }

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 900px) {
  .score-strip { order: 3; flex-basis: 100%; }
  .board-topbar { flex-direction: row; }
}

@media (max-width: 640px) {
  .app-header { padding: var(--sp-4) var(--sp-4) 0; }
  #table-screen { padding: var(--sp-2) var(--sp-2) 0; }
  .rack-tile { width: 46px; height: 64px; padding: 2px 1px 3px; }
  .tile-glyph { font-size: 16px; min-height: 34px; }
  .tile-label { font-size: 7px; padding-top: 2px; }
  .rack-actions { gap: var(--sp-1); flex-wrap: wrap; }
  .rack-actions button { padding: var(--sp-2) var(--sp-3); font-size: var(--fs-caption); }
  .tool-btn { flex: 1 1 auto; min-width: 64px; }
  .tool-btn.icon-only { flex: 0 0 40px; height: 40px; }

  body.in-game .app-header { top: var(--sp-3); right: var(--sp-3); }
  body.in-game .app-header .header-actions { gap: var(--sp-1); }
  body.in-game .app-header .icon-btn { width: 34px; height: 34px; font-size: 13px; }

  .turn-info, .deck-info { display: none; }
  .board-topbar { padding: 0 var(--sp-2) var(--sp-2); justify-content: center; }
  .score-strip { gap: var(--sp-3); }
  .setup-card h2 { font-size: var(--fs-h1); }
  .hero-mark { font-size: 70vw; }
}
