:root {
  --bg: #f4f1e8;
  --bg-grad-start: #f7f3ea;
  --bg-grad-end: #ece4d8;
  --panel: #fffef9;
  --text: #222;
  --muted: #666;
  --line: #d7d0c2;
  --board-bg: #f0e9d7;
  --board-border: #765a2c;
  --cell-border: #c9c1af;
  --tile: #f6d57a;
  --tile-border: #8b6f2f;
  --pending: #b5e48c;
  --empty: #f8f7f2;
  --dl: #9fd5ff;
  --tl: #4da3e2;
  --dw: #ffb3b3;
  --tw: #e85a5a;
  --accent: #254f6d;
}

:root[data-theme="dark"] {
  --bg: #0f1318;
  --bg-grad-start: #131922;
  --bg-grad-end: #0c1119;
  --panel: #1a232f;
  --text: #f1f6ff;
  --muted: #b8c4d6;
  --line: #304154;
  --board-bg: #1c2938;
  --board-border: #4f6277;
  --cell-border: #4a5a6a;
  --tile: #e2bf68;
  --tile-border: #8b6f2f;
  --pending: #7aa558;
  --empty: #101722;
  --dl: #215174;
  --tl: #1970ae;
  --dw: #8d3f3f;
  --tw: #ba4a4a;
  --accent: #7ac3f4;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, var(--bg-grad-start), var(--bg-grad-end));
}

body {
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

.page {
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px;
}

h1,
h2,
h3 {
  margin: 0 0 12px;
}

p {
  margin: 0 0 10px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}

.board-layout {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 320px;
  gap: 16px;
}

.board-panel {
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.board-shell {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  border: 2px solid var(--board-border);
  background: var(--board-bg);
  width: min(100%, 900px, calc(100vh - 170px));
  max-width: 900px;
  max-height: calc(100vh - 170px);
  aspect-ratio: 1 / 1;
}

.mini-board-shell {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  border: 2px solid var(--board-border);
  width: 100%;
  max-width: 420px;
  aspect-ratio: 1 / 1;
  background: var(--board-bg);
}

.cell {
  border: 1px solid var(--cell-border);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  color: #333;
  font-size: 12px;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
}

.board-shell .cell {
  font-size: 16px;
}

.cell.empty {
  background: var(--empty);
}

.cell.dl {
  background: var(--dl);
}

.cell.tl {
  background: var(--tl);
  color: white;
}

.cell.dw {
  background: var(--dw);
}

.cell.tw {
  background: var(--tw);
  color: white;
}

.cell.pending-cell {
  box-shadow: inset 0 0 0 3px #5d7f30;
}

.premium-label {
  font-size: 9px;
  opacity: 0.7;
}

.tile {
  width: 90%;
  height: 90%;
  border-radius: 4px;
  border: 1px solid var(--tile-border);
  background: var(--tile);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95em;
  position: relative;
}

.tile .points {
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 10px;
  font-weight: bold;
}

.tile.pending {
  background: var(--pending);
  border-color: #4f6e24;
}

.sidebar {
  display: grid;
  gap: 12px;
  align-content: start;
}

.score-table {
  width: 100%;
  border-collapse: collapse;
}

.score-table th,
.score-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  text-align: left;
}

.turn-chip {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  background: #e6f1f8;
  color: #12405b;
  font-weight: bold;
}

.log-list {
  margin: 0;
  padding-left: 18px;
  max-height: 260px;
  overflow: auto;
}

.log-list li {
  margin-bottom: 6px;
}

.rack {
  display: grid;
  grid-template-columns: repeat(7, minmax(42px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.rack button {
  border: 1px solid var(--tile-border);
  border-radius: 6px;
  background: var(--tile);
  min-height: 56px;
  font-size: 22px;
  font-weight: bold;
  position: relative;
}

.rack button .points {
  position: absolute;
  right: 5px;
  bottom: 3px;
  font-size: 11px;
}

.rack button.selected {
  outline: 3px solid #2f6d95;
}

.rack button.exchange-selected {
  outline: 3px solid #9c3524;
  transform: translateY(-1px);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

button,
input {
  font-size: 16px;
}

button {
  border: 1px solid #1d425b;
  background: #2e5c79;
  color: white;
  border-radius: 6px;
  padding: 8px 12px;
}

button.secondary {
  background: #eef4f8;
  color: #1c3d53;
}

:root[data-theme="dark"] button.secondary {
  background: #2a3b4f;
  color: #d6e8f8;
  border-color: #3f5b78;
}

button.warn {
  background: #9c3524;
  border-color: #7e2518;
}

input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  width: 100%;
}

.status-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}

.error {
  color: #a2281a;
  font-weight: bold;
  min-height: 1.25em;
}

.small {
  color: var(--muted);
  font-size: 13px;
}

.qr-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

.qr-wrap img,
.qr-wrap svg {
  width: 220px;
  height: 220px;
  border: 1px solid var(--line);
  background: white;
  padding: 8px;
}

.qr-image {
  width: 220px;
  height: 220px;
  border: 1px solid var(--line);
  background: white;
  padding: 8px;
}

.qr-image img {
  width: 100%;
  height: 100%;
}

.join-form {
  max-width: 420px;
  margin-top: 18px;
  display: grid;
  gap: 10px;
}

.theme-toggle {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 1100;
  border-radius: 999px;
  padding: 10px 14px;
  border: 1px solid #1d425b;
  background: #2e5c79;
  color: #fff;
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
}

:root[data-theme="dark"] .theme-toggle {
  background: #1f3346;
  border-color: #446487;
}

.exchange-panel {
  margin-top: 12px;
}

.exchange-preview {
  min-height: 1.2em;
  font-weight: bold;
  letter-spacing: 0.04em;
}

.exchange-animating {
  pointer-events: none;
}

.blank-panel {
  margin-top: 12px;
}

.blank-grid {
  display: grid;
  grid-template-columns: repeat(8, minmax(32px, 1fr));
  gap: 6px;
}

.blank-grid button {
  min-height: 40px;
  font-size: 20px;
  font-weight: 900;
  padding: 4px 0;
}

.rack button.exchange-fly {
  animation: exchange-fly 0.45s ease-in forwards;
}

@keyframes exchange-fly {
  0% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translateY(-24px) scale(0.7) rotate(-8deg);
    opacity: 0;
  }
}

/* Phone hand view: hide tiny point values so letters stay legible. */
.hand-page .tile .points,
.hand-page .rack button .points {
  display: none;
}

.hand-page .rack button {
  font-size: 27px;
  font-weight: 900;
}

/* Laptop board "TV mode" for across-the-room readability. */
@media (min-width: 1200px) {
  .board-page .page {
    max-width: none;
    width: 100vw;
    min-height: 100vh;
    padding: 10px;
  }

  .board-page h1 {
    font-size: 34px;
    font-weight: 900;
    margin-bottom: 10px;
  }

  .board-page .board-layout {
    grid-template-columns: minmax(480px, 1fr) 420px;
    gap: 10px;
    align-items: start;
  }

  .board-page .panel {
    border-width: 2px;
  }

  .board-page .board-shell {
    width: min(calc(100vw - 470px), calc(100vh - 66px));
    max-width: none;
    max-height: none;
    border: 4px solid var(--board-border);
  }

  .board-page .cell {
    border-width: 2px;
    font-size: 18px;
    font-weight: 900;
  }

  .board-page .tile {
    border-width: 2px;
    font-size: 1.22em;
    font-weight: 900;
  }

  .board-page .tile .points {
    font-size: 14px;
  }

  .board-page .premium-label {
    font-size: 11px;
    font-weight: 800;
  }

  .board-page .score-table th,
  .board-page .score-table td {
    font-size: 24px;
    font-weight: 800;
    padding: 10px;
  }

  .board-page .turn-chip {
    font-size: 26px;
    font-weight: 900;
    padding: 6px 14px;
  }

  .board-page #bag-count,
  .board-page #practice-chip {
    font-size: 24px;
    font-weight: 800;
  }

  .board-page .log-list li {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 8px;
  }
}

@media (max-width: 1024px) {
  .board-layout {
    grid-template-columns: 1fr;
  }

  .board-shell {
    width: min(100%, 900px);
    max-width: 100%;
    max-height: none;
  }

  .sidebar {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  }
}

@media (max-width: 768px) {
  .page {
    padding: 10px;
  }

  .rack {
    grid-template-columns: repeat(7, minmax(36px, 1fr));
  }

  .rack button {
    min-height: 48px;
    font-size: 20px;
  }

  button {
    flex: 1 1 45%;
  }

  .theme-toggle {
    right: 10px;
    bottom: 10px;
    font-size: 13px;
    padding: 8px 12px;
  }
}
