:root {
  --graphite: #1a2230;
  --graphite-deep: #121820;
  --navy: #243044;
  --navy-light: #2e3d54;
  --bone: #f2ead8;
  --bone-dim: #c8bfb0;
  --bone-muted: #8f98a6;
  --ink: #1c2430;
  --ink-soft: #4a5568;
  --mint: #5ec4a8;
  --mint-bright: #7ddfc4;
  --cobalt: #4a8fd4;
  --cobalt-bright: #6aadf0;
  --tile-face: #f5eedc;
  --danger: #e07070;
  --danger-soft: #f5b0a8;

  --prem-dl: #2a4a6e;
  --prem-dl-text: #9ed4f0;
  --prem-tl: #1e5a78;
  --prem-tl-text: #b8ecff;
  --prem-dw: #5a3a68;
  --prem-dw-text: #d8b8e8;
  --prem-tw: #7a2e52;
  --prem-tw-text: #f0a8c8;

  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "Source Code Pro", Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  background:
    radial-gradient(1100px 650px at 75% -5%, #2a3850 0%, transparent 58%),
    radial-gradient(800px 500px at -5% 105%, #1e2a3c 0%, transparent 52%),
    var(--graphite-deep);
  color: var(--bone);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.45;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: -100px;
  left: 16px;
  z-index: 200;
  padding: 8px 14px;
  background: var(--cobalt);
  color: #f0f6ff;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 600;
}
.skip-link:focus {
  top: 12px;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid rgba(242, 234, 216, 0.1);
  background: rgba(18, 24, 32, 0.6);
}

.wordmark { display: flex; align-items: center; gap: 12px; }

.wm-tile {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  background: var(--tile-face);
  color: var(--ink);
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 24px;
  border-radius: 7px;
  box-shadow: inset 0 -3px 0 rgba(28, 36, 48, 0.2);
}
.wm-tile.alt {
  background: var(--cobalt);
  color: #f0f6ff;
  margin-left: -8px;
  transform: rotate(-6deg);
}

.wm-text h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 0.02em;
  color: var(--bone);
}
.wm-text p {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--bone-muted);
  letter-spacing: 0.08em;
}

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

.variant-picker { display: flex; align-items: center; gap: 8px; }
.variant-picker span {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--bone-muted);
}
.variant-picker select {
  background: var(--navy);
  color: var(--bone);
  border: 1px solid rgba(242, 234, 216, 0.2);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: var(--font-ui);
  font-size: 14px;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  appearance: none;
  border: none;
  border-radius: 9px;
  padding: 9px 16px;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease, background 120ms ease;
}
.btn:focus-visible { outline: 2px solid var(--mint-bright); outline-offset: 2px; }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }

.btn.primary {
  background: linear-gradient(180deg, var(--mint-bright), var(--mint));
  color: var(--ink);
  box-shadow: 0 2px 12px rgba(94, 196, 168, 0.3);
}
.btn.primary:hover:not([disabled]) { filter: brightness(1.07); }

.btn.ghost {
  background: transparent;
  color: var(--bone);
  border: 1px solid rgba(242, 234, 216, 0.28);
}
.btn.ghost:hover:not([disabled]) { background: rgba(242, 234, 216, 0.07); }
.btn.ghost.danger {
  color: var(--danger-soft);
  border-color: rgba(224, 112, 112, 0.45);
}
.btn.ghost.danger:hover:not([disabled]) {
  background: rgba(224, 112, 112, 0.12);
}
.btn.small { padding: 5px 12px; font-size: 13px; }

/* ---------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: minmax(280px, 620px) minmax(280px, 400px);
  justify-content: center;
  gap: clamp(18px, 3vw, 44px);
  padding: clamp(16px, 3vw, 36px);
}

/* ---------------------------------------------------------------- board frame (3×3 grid) */

.board-zone {
  min-width: 0;
}

.board-frame {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "corner-tl coords-top corner-tr"
    "coords-left board coords-right"
    "corner-bl coords-bottom corner-br";
  gap: 4px;
  width: 100%;
  max-width: 620px;
  padding: 8px;
  background: var(--navy);
  border: 1px solid rgba(242, 234, 216, 0.12);
  border-radius: 16px;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(242, 234, 216, 0.06);
}

.board-corner {
  width: 22px;
  height: 22px;
  background: var(--graphite-deep);
  border: 1px solid rgba(242, 234, 216, 0.15);
  border-radius: 4px;
}
.corner-tl { grid-area: corner-tl; }
.corner-tr { grid-area: corner-tr; }
.corner-bl { grid-area: corner-bl; }
.corner-br { grid-area: corner-br; }

.coords {
  display: grid;
  font-family: var(--font-mono);
  font-size: clamp(9px, 1.4vw, 11px);
  font-weight: 600;
  color: var(--bone-muted);
  user-select: none;
}
.coords span { display: grid; place-items: center; }

.coords-top {
  grid-area: coords-top;
  grid-template-columns: repeat(15, 1fr);
}
.coords-bottom {
  grid-area: coords-bottom;
  grid-template-columns: repeat(15, 1fr);
}
.coords-left {
  grid-area: coords-left;
  grid-template-rows: repeat(15, 1fr);
}
.coords-right {
  grid-area: coords-right;
  grid-template-rows: repeat(15, 1fr);
}

/* ---------------------------------------------------------------- board */

.board {
  grid-area: board;
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  aspect-ratio: 1;
  width: 100%;
  background: var(--graphite);
  border-radius: 10px;
  padding: 4px;
  gap: 2px;
  touch-action: manipulation;
}

.sq {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--navy-light);
  border-radius: 3px;
  font-family: var(--font-mono);
  user-select: none;
  border: none;
  padding: 0;
  color: inherit;
  font: inherit;
  cursor: default;
}
.sq:focus-visible {
  outline: 2px solid var(--cobalt-bright);
  outline-offset: -1px;
  z-index: 3;
}
.sq.focused {
  outline: 2px solid var(--mint);
  outline-offset: -1px;
  z-index: 3;
}

.sq .bonus-label {
  font-size: clamp(5px, 0.58vw, 8px);
  font-weight: 700;
  letter-spacing: 0.04em;
  opacity: 0.85;
  pointer-events: none;
}
.sq.dl { background: var(--prem-dl); color: var(--prem-dl-text); }
.sq.tl { background: var(--prem-tl); color: var(--prem-tl-text); }
.sq.dw { background: var(--prem-dw); color: var(--prem-dw-text); }
.sq.tw { background: var(--prem-tw); color: var(--prem-tw-text); }
.sq.star::after {
  content: "★";
  position: absolute;
  font-size: clamp(8px, 0.95vw, 13px);
  color: rgba(242, 234, 216, 0.45);
}

.sq.staged { outline: 2px solid var(--mint-bright); outline-offset: -2px; z-index: 2; }
.sq.drop-target { outline: 2px dashed var(--cobalt); outline-offset: -2px; z-index: 2; }

.sq .tile {
  position: absolute;
  inset: 5%;
  z-index: 1;
}

/* ---------------------------------------------------------------- tiles */

.tile {
  display: grid;
  place-items: center;
  background: linear-gradient(170deg, #faf6ec, var(--tile-face) 55%, #e4dac8);
  color: var(--ink);
  border-radius: 4px;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(11px, 1.65vw, 22px);
  line-height: 1;
  cursor: grab;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4), inset 0 -2px 0 rgba(28, 36, 48, 0.18);
}
.tile .sub {
  position: absolute;
  right: 7%;
  bottom: 4%;
  font-family: var(--font-mono);
  font-weight: 400;
  font-size: clamp(6px, 0.6vw, 9px);
  color: var(--ink-soft);
}
.tile.blank-rack { color: var(--cobalt); }
.tile.blank-letter { color: var(--cobalt); }
.tile.dragging { opacity: 0.45; cursor: grabbing; }
.tile.selected { outline: 3px solid var(--mint-bright); outline-offset: -1px; }

.board .tile { cursor: default; }

/* ---------------------------------------------------------------- panel */

.panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.scoreboard {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 10px;
  background: rgba(36, 48, 68, 0.85);
  border: 1px solid rgba(242, 234, 216, 0.12);
  border-radius: 14px;
  padding: 12px 16px;
}

.seat { display: flex; flex-direction: column; gap: 2px; min-width: 64px; }
.seat.bot { align-items: flex-end; }
.seat-name {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--bone-muted);
}
.seat-score {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(28px, 6vw, 34px);
  line-height: 1;
  color: var(--bone);
}
.seat.turn .seat-score { color: var(--mint-bright); }
.seat.turn .seat-name::after { content: " ●"; color: var(--mint-bright); }

.vs {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 0 10px;
}
.vs > span:first-child {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 20px;
  color: var(--cobalt-bright);
}
.vs-label {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--bone-muted);
}

.bot-status {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--bone-muted);
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(36, 48, 68, 0.55);
  border: 1px solid rgba(242, 234, 216, 0.08);
}
.bot-status strong { color: var(--mint); font-weight: 600; }
.bot-status.warn strong { color: var(--cobalt-bright); }

.onboarding {
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 13px;
  background: rgba(94, 196, 168, 0.08);
  border: 1px solid rgba(94, 196, 168, 0.3);
  color: var(--bone);
}
.onboarding strong { color: var(--mint); }

.banner {
  display: flex;
  flex-direction: column;
  gap: 12px;
  border-radius: 12px;
  padding: 14px 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  background: rgba(94, 196, 168, 0.1);
  border: 1px solid rgba(94, 196, 168, 0.35);
  color: var(--mint);
}
.banner.win {
  background: rgba(74, 143, 212, 0.12);
  border-color: rgba(74, 143, 212, 0.4);
  color: var(--cobalt-bright);
}
.banner.loss {
  background: rgba(224, 112, 112, 0.1);
  border-color: rgba(224, 112, 112, 0.4);
  color: var(--danger-soft);
}
.banner-text { margin: 0; line-height: 1.5; }
.banner .btn.primary { align-self: stretch; font-size: 15px; padding: 12px 16px; }

/* ---------------------------------------------------------------- rack */

.rack-zone {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.rack {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  min-height: 58px;
  padding: 8px;
  background: linear-gradient(180deg, var(--graphite-deep), var(--graphite));
  border-radius: 12px;
  border: 1px solid rgba(242, 234, 216, 0.12);
  box-shadow: inset 0 3px 10px rgba(0, 0, 0, 0.5);
}

.rack .tile {
  width: clamp(42px, 11vw, 52px);
  height: clamp(42px, 11vw, 52px);
  position: relative;
  font-size: clamp(20px, 5vw, 26px);
  transition: transform 120ms ease;
}
.rack .tile:hover { transform: translateY(-3px); }
.rack .tile.dragging { transform: scale(1.05); }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.actions .btn.primary { flex: 1 1 140px; }

.hint-line {
  margin: 0;
  font-size: 12px;
  color: var(--bone-muted);
}

/* ---------------------------------------------------------------- evidence card */

.evidence-card {
  background: linear-gradient(165deg, rgba(36, 48, 68, 0.95), rgba(26, 34, 48, 0.9));
  border: 1px solid rgba(94, 196, 168, 0.22);
  border-radius: 14px;
  padding: 14px 16px 16px;
  box-shadow: inset 3px 0 0 var(--mint);
}

.evidence-card h2 {
  margin: 0 0 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--mint);
}

.evidence-disclaimer {
  margin: 0 0 12px;
  font-size: 11px;
  line-height: 1.45;
  color: var(--bone-muted);
}

.evidence-stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 0 0 14px;
  font-family: var(--font-mono);
  font-size: 12px;
}
.evidence-stats dt {
  color: var(--bone-muted);
  margin: 0;
}
.evidence-stats dd {
  margin: 0;
  color: var(--bone);
  font-weight: 500;
  text-align: right;
}

.evidence-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.chart-figure {
  margin: 0;
  padding: 8px;
  background: rgba(18, 24, 32, 0.55);
  border: 1px solid rgba(242, 234, 216, 0.08);
  border-radius: 10px;
}
.chart-figure figcaption {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--bone-muted);
  margin-bottom: 6px;
}

.chart-host {
  height: 100px;
  min-height: 80px;
}
.chart-empty {
  margin: 0;
  padding: 36px 8px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--bone-muted);
}

.chart-axis { stroke: rgba(242, 234, 216, 0.2); stroke-width: 1; }
.chart-line-you {
  stroke: var(--mint);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.chart-line-bot {
  stroke: var(--cobalt);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
  stroke-dasharray: 4 3;
}
.chart-bar { fill: rgba(74, 143, 212, 0.55); }
.chart-bar-top { fill: var(--mint); }

/* ---------------------------------------------------------------- cards */

.log-card, .hints-card {
  background: rgba(36, 48, 68, 0.7);
  border: 1px solid rgba(242, 234, 216, 0.1);
  border-radius: 14px;
  padding: 12px 16px 14px;
}
.log-card h2, .hints-card h2 {
  margin: 0 0 8px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--bone-muted);
}
.hints-head { display: flex; align-items: center; justify-content: space-between; }
.hints-head h2 { margin-bottom: 0; }
.hints-card .hints-head { margin-bottom: 8px; }

.hints-actions { margin-bottom: 8px; }
.hints-actions .btn.primary {
  width: 100%;
  font-size: 15px;
  padding: 11px 16px;
}

.hints-status {
  margin: 0 0 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--mint);
}
.hints-note {
  margin: 0 0 8px;
  font-size: 12px;
  color: var(--bone-muted);
}

.history, .hints-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 220px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 13px;
}

.history li {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 4px 0;
  border-bottom: 1px dotted rgba(242, 234, 216, 0.1);
}
.history li:last-child { border-bottom: none; }
.hist-player { color: var(--bone-muted); width: 42px; flex-shrink: 0; }
.hist-word { color: var(--bone); font-weight: 500; }
.hist-note { color: var(--bone-muted); }
.hist-score { margin-left: auto; color: var(--cobalt-bright); font-weight: 700; }
.hist-score.negative { color: var(--danger); }

.hints-list li {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 6px 8px;
  cursor: pointer;
  border-radius: 6px;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  color: inherit;
  font: inherit;
}
.hints-list li.hint-top {
  background: rgba(94, 196, 168, 0.08);
  border: 1px solid rgba(94, 196, 168, 0.2);
}
.hints-list li:hover,
.hints-list li:focus-visible {
  background: rgba(242, 234, 216, 0.06);
  outline: 2px solid var(--mint-bright);
  outline-offset: -2px;
}
.hints-list li.staging { opacity: 0.55; pointer-events: none; }
.hint-eq { color: var(--mint); width: 48px; flex-shrink: 0; font-weight: 600; }
.hint-score { margin-left: auto; color: var(--bone-muted); }
.hints-empty { color: var(--bone-muted); font-family: var(--font-mono); font-size: 13px; margin: 0; padding: 6px 8px; }

/* ---------------------------------------------------------------- toast + modal */

.toast {
  position: fixed;
  top: 74px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  background: var(--navy);
  color: var(--bone);
  border: 1px solid rgba(94, 196, 168, 0.4);
  padding: 10px 18px;
  border-radius: 10px;
  font-family: var(--font-mono);
  font-size: 13px;
  max-width: min(90vw, 480px);
  text-align: center;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(12, 16, 22, 0.78);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 16px;
}
.modal {
  background: var(--navy);
  border: 1px solid rgba(242, 234, 216, 0.18);
  border-radius: 16px;
  padding: 20px 22px;
  width: min(92vw, 360px);
  max-height: min(92vh, 520px);
  overflow-y: auto;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
}
.modal h3 {
  margin: 0 0 10px;
  font-family: var(--font-display);
  font-weight: 800;
  color: var(--bone);
}
.modal-note { margin: 0 0 12px; font-size: 13px; color: var(--bone-muted); }
.modal-actions { display: flex; gap: 10px; margin-top: 14px; }

.blank-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
.blank-grid button {
  aspect-ratio: 1;
  border: none;
  border-radius: 7px;
  background: var(--tile-face);
  color: var(--ink);
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 17px;
  cursor: pointer;
}
.blank-grid button:hover { background: var(--cobalt); color: #f0f6ff; }
.blank-grid button:focus-visible {
  outline: 2px solid var(--mint-bright);
  outline-offset: 2px;
}

.exchange-rack { background: transparent; border: none; box-shadow: none; }
.exchange-rack .tile.chosen {
  outline: 3px solid var(--mint-bright);
  outline-offset: -1px;
  transform: translateY(-4px);
}

/* ---------------------------------------------------------------- responsive */

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    max-width: 560px;
    margin: 0 auto;
  }
  .panel { order: 2; }
  body { font-size: 14px; }
  .topbar { flex-wrap: wrap; }
  .topbar-actions { width: 100%; justify-content: flex-end; }
  .evidence-charts {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .actions .btn { flex: 1 1 calc(50% - 4px); }
  .actions .btn.primary { flex: 1 1 100%; }
  .board-frame { padding: 6px; gap: 3px; }
  .board-corner { width: 16px; height: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
