/* ---------- base ---------- */
:root {
  --bg: #2c2620;
  --bg-panel: #3a332b;
  --ink: #f2ead9;
  --ink-dim: #b9ad99;
  --accent: #e8a33d;
  --danger: #d05b4b;
  --ok: #7fb069;
  --tile-face: #f3e5c0;
  --tile-ink: #3b2f1e;
  --board-bg: #22303a;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--ink);
  overscroll-behavior: none;
}
button { font: inherit; cursor: pointer; }
a { color: var(--accent); }

.btn {
  border: 1px solid #57493a; border-radius: 8px;
  background: #4a4034; color: var(--ink);
  padding: 8px 14px; text-decoration: none;
}
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #241a08; font-weight: 700; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.special { background: var(--ok); border-color: var(--ok); color: #17240f; font-weight: 700; }
.icon-btn {
  background: none; border: none; color: var(--ink);
  font-size: 20px; padding: 6px 8px; text-decoration: none; border-radius: 8px;
}
.icon-btn:hover { background: #ffffff14; }
.icon-btn svg { width: 20px; height: 20px; display: block; }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(360px, 100%); background: var(--bg-panel);
  padding: 32px 28px; border-radius: 16px; box-shadow: 0 12px 40px #0006;
  display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { margin: 0; font-size: 26px; }
.login-card .tagline { margin: 0 0 8px; color: var(--ink-dim); font-size: 14px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.login-card input {
  padding: 10px 12px; border-radius: 8px; border: 1px solid #57493a;
  background: #2c2620; color: var(--ink); font-size: 16px;
}
.login-card button {
  margin-top: 8px; padding: 12px; border: none; border-radius: 8px;
  background: var(--accent); color: #241a08; font-weight: 700; font-size: 16px;
}
.flash { color: var(--danger); margin: 0; font-size: 14px; }

/* ---------- game layout ---------- */
#app { height: 100dvh; display: flex; flex-direction: column; }
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; background: var(--bg-panel); z-index: 5;
  /* keep clear of the status bar / nav bar when drawn edge-to-edge */
  padding: calc(6px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right))
           6px calc(10px + env(safe-area-inset-left));
}
#scores { display: flex; gap: 8px; align-items: center; min-width: 0; }
.player-chip {
  display: flex; gap: 8px; align-items: baseline;
  padding: 4px 12px; border-radius: 999px; background: #2c2620;
  border: 2px solid transparent; white-space: nowrap;
}
.player-chip.turn { border-color: var(--accent); }
.player-chip .dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; align-self: center; }
.player-chip .name { font-size: 13px; color: var(--ink-dim); overflow: hidden; text-overflow: ellipsis; }
.player-chip .you-tag { font-size: 11px; color: var(--ink-dim); }
.player-chip.turn .name { color: var(--ink); }
.player-chip .pts { font-weight: 700; font-size: 16px; }
#topnav { position: relative; flex-shrink: 0; }
#menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  display: flex; flex-direction: column; min-width: 200px;
  background: var(--bg-panel); border: 1px solid #57493a; border-radius: 12px;
  box-shadow: 0 12px 32px #000a; padding: 6px; z-index: 25;
}
.menu-item {
  display: flex; align-items: center; gap: 12px;
  background: none; border: none; color: var(--ink);
  padding: 11px 12px; border-radius: 8px; font-size: 14px;
  text-decoration: none; text-align: left; cursor: pointer;
}
.menu-item:hover { background: #ffffff14; }
.menu-item svg { width: 18px; height: 18px; flex: 0 0 auto; color: var(--ink-dim); }
.menu-item.on, .menu-item.on svg { color: var(--accent); }

#banner {
  padding: 10px 14px; background: #574a2b; text-align: center; font-size: 15px;
  display: flex; gap: 10px; justify-content: center; align-items: center; flex-wrap: wrap;
}
#banner.over { background: #2f4a33; }

#board-wrap { position: relative; flex: 1; min-height: 0; background: var(--board-bg); }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
#preview {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%);
  background: #000000b0; padding: 8px 14px; border-radius: 10px;
  font-size: 15px; pointer-events: none; max-width: 92%;
}
#preview.bad { color: #ffb4a8; }
#preview .total { color: var(--accent); font-weight: 700; }
.float-btn {
  position: absolute; right: 10px; bottom: 10px;
  background: #000000a0; border-radius: 12px;
  display: grid; place-items: center; width: 40px; height: 40px;
}
.float-btn svg { width: 20px; height: 20px; }

/* ---------- rack & actions ---------- */
#controls { background: var(--bg-panel); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
#rack {
  display: flex; gap: 6px; justify-content: center; min-height: 54px;
  padding-bottom: 8px; flex-wrap: nowrap;
}
.rack-tile {
  width: 48px; height: 52px; border-radius: 8px; border: 2px solid #c9b787;
  background: var(--tile-face); color: var(--tile-ink);
  font-weight: 800; font-size: 22px; position: relative;
  display: grid; place-items: center; user-select: none; flex: 0 0 auto;
  touch-action: none; /* allow touch drags to the board */
  transition: transform .12s ease;
}
.rack-tile.dragging { opacity: .35; }
.rack-tile .pts { position: absolute; right: 4px; bottom: 2px; font-size: 10px; font-weight: 600; }
.rack-tile.selected { transform: translateY(-6px); border-color: var(--accent); box-shadow: 0 6px 14px #0008; }
.rack-tile.exchange-pick { border-color: var(--danger); background: #f3d1c0; }
#actions { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
#exchange-bar {
  display: flex; gap: 10px; align-items: center; justify-content: center;
  padding-top: 8px; font-size: 14px; color: var(--ink-dim); flex-wrap: wrap;
}

/* ---------- side panel ---------- */
#panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(340px, 92vw);
  background: var(--bg-panel); z-index: 20; box-shadow: -10px 0 30px #0008;
  display: flex; flex-direction: column;
}
#panel header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; }
#panel h2 { margin: 0; font-size: 18px; }
#panel-body { overflow-y: auto; padding: 0 16px 16px; flex: 1; }

.bag-total { color: var(--ink-dim); margin: 0 0 12px; }
.bag-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.bag-cell {
  background: #2c2620; border-radius: 8px; padding: 8px 4px; text-align: center;
}
.bag-cell .l { font-weight: 800; font-size: 16px; }
.bag-cell .n { color: var(--ink-dim); font-size: 13px; }
.bag-cell .pct { color: var(--accent); font-size: 11px; opacity: .85; }
.bag-cell.empty { opacity: .35; }

.log-entry { padding: 8px 0; border-bottom: 1px solid #ffffff14; font-size: 14px; }
.log-entry.clickable { cursor: pointer; }
.log-entry.clickable:hover { background: #ffffff0a; }
.log-entry .who { font-weight: 700; }
.log-entry .what { color: var(--ink-dim); }
.log-entry .score { float: right; font-weight: 700; color: var(--accent); }
.log-entry .all-tiles { color: var(--ok); font-weight: 700; }

/* ---------- modal & toast ---------- */
.modal {
  position: fixed; inset: 0; background: #000a; z-index: 30;
  display: grid; place-items: center; padding: 20px;
}
.modal-card {
  background: var(--bg-panel); border-radius: 16px; padding: 20px;
  width: min(400px, 100%);
}
.modal-card h2 { margin: 0 0 14px; font-size: 17px; }
#blank-letters { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
#blank-letters button {
  padding: 10px 0; border-radius: 8px; border: 1px solid #57493a;
  background: var(--tile-face); color: var(--tile-ink); font-weight: 800; font-size: 16px;
}

#toast {
  position: fixed; bottom: 130px; left: 50%; transform: translateX(-50%);
  background: #000000d0; color: #ffb4a8; padding: 10px 18px; border-radius: 10px;
  z-index: 40; max-width: 90%; text-align: center; font-size: 14px;
}
#toast.info { color: var(--ink); }

/* ---------- history page ---------- */
.history-wrap { max-width: 640px; margin: 0 auto; padding: 20px; }
.history-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
.history-header h1 { font-size: 24px; margin: 0; }
.muted { color: var(--ink-dim); }
.game-list { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.game-list a {
  display: flex; gap: 14px; align-items: center; flex-wrap: wrap;
  background: var(--bg-panel); border-radius: 12px; padding: 14px 16px;
  color: var(--ink); text-decoration: none;
}
.game-list .game-no { font-weight: 700; }
.game-list .game-scores { display: flex; gap: 12px; color: var(--ink-dim); }
.game-list .game-scores strong { color: var(--ink); }
.game-list .game-winner { margin-left: auto; display: flex; align-items: center; gap: 6px; color: var(--accent); }
.game-list .game-winner svg { width: 16px; height: 16px; }
.game-list .game-date { color: var(--ink-dim); font-size: 13px; }

/* ---------- rules & stats pages ---------- */
.history-wrap h2 { font-size: 18px; margin: 26px 0 10px; }
.prose { background: var(--bg-panel); border-radius: 12px; padding: 4px 18px 14px; margin-bottom: 14px; }
.prose h3 { margin: 18px 0 8px; font-size: 16px; color: var(--accent); }
.prose p, .prose li { font-size: 14px; line-height: 1.6; }
.prose ol, .prose ul { padding-left: 22px; }
.stats-table {
  width: 100%; border-collapse: collapse; background: var(--bg-panel);
  border-radius: 12px; overflow: hidden;
}
.stats-table th, .stats-table td {
  padding: 9px 12px; text-align: left; font-size: 14px;
  border-bottom: 1px solid #ffffff10;
}
.stats-table th { color: var(--ink); font-size: 15px; }
.stats-table td:first-child { color: var(--ink-dim); }
.stats-table .dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  margin-right: 7px; vertical-align: baseline;
}

/* ---------- responsive ---------- */
@media (max-width: 480px) {
  .rack-tile { width: 42px; height: 46px; font-size: 19px; }
  .btn { padding: 8px 10px; font-size: 14px; }
  .player-chip { padding: 3px 9px; gap: 5px; }
  .player-chip .pts { font-size: 14px; }
  .player-chip .name { max-width: 14ch; }
  .player-chip .you-tag { display: none; }
}
