:root {
  --ink: #3b2a1c;
  --ink-soft: #6d5640;
  --cream: #fbf3e4;
  --paper: rgba(255, 248, 236, 0.9);
  --paper-solid: #fff8ec;
  --line: rgba(110, 80, 50, 0.18);
  --accent: #c8684a;
  --accent-dark: #a24f35;
  --sage: #5f8f6b;
  --honey: #e2a947;
  --wood: #d8b98a;
  --wood-dark: #9b7148;
  --tile: #fbf0d9;
  --tile-edge: #d9c19a;
  --p2L: #b9d2b0;
  --p3L: #6f9e78;
  --p2W: #f1c27a;
  --p3W: #d2765a;
  --st: #e8b468;
  --radius: 16px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --shadow: 0 10px 30px rgba(40, 22, 8, 0.25), 0 2px 6px rgba(40, 22, 8, 0.15);
  --top: 54px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.45 var(--sans);
  color: var(--ink);
  background: #2a1a10;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
#bg {
  position: fixed; inset: -20px; z-index: -2;
  background: #3b2416 center / cover no-repeat;
  filter: blur(2px) saturate(1.05);
  transition: background-image 0.8s ease;
  animation: breathe 40s ease-in-out infinite alternate;
}
#glow {
  position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 40%, rgba(255, 220, 160, 0.10), transparent 60%),
    linear-gradient(180deg, rgba(40, 20, 8, 0.25), rgba(40, 20, 8, 0.45));
}
body.in-game #glow { background: linear-gradient(180deg, rgba(40, 20, 8, 0.35), rgba(40, 20, 8, 0.55)); }
@keyframes breathe { from { transform: scale(1); } to { transform: scale(1.04); } }
@media (prefers-reduced-motion: reduce) { #bg { animation: none; } * { transition: none !important; animation: none !important; } }

a { color: var(--accent-dark); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0 0 0.5em; line-height: 1.2; }
h1 { font-size: clamp(2rem, 5vw, 3.2rem); }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.05rem; color: var(--ink-soft); }
.muted { color: var(--ink-soft); }
.small { font-size: 0.88rem; }
.center { text-align: center; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--top);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px;
  background: linear-gradient(180deg, rgba(43, 25, 13, 0.85), rgba(43, 25, 13, 0.55));
  backdrop-filter: blur(6px);
  color: var(--cream);
}
.home-link { color: var(--cream); text-decoration: none; font-family: var(--serif); font-size: 1.05rem; opacity: 0.9; }
.home-link:hover { opacity: 1; text-decoration: underline; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.icon {
  width: 38px; height: 38px; border-radius: 50%;
  border: 1px solid rgba(255, 240, 220, 0.3);
  background: rgba(255, 240, 220, 0.12); color: var(--cream);
  font-size: 1.05rem; cursor: pointer;
}
.icon:hover { background: rgba(255, 240, 220, 0.25); }

#app { padding: 18px clamp(10px, 3vw, 28px) 40px; max-width: 1280px; margin: 0 auto; }
.loading { color: var(--cream); text-align: center; padding: 30vh 0; font-family: var(--serif); font-size: 1.3rem; text-shadow: 0 2px 8px #0008; }

.card {
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  backdrop-filter: blur(8px);
}

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 600;
  border: 1px solid var(--line);
  background: #fffaf1; color: var(--ink);
  border-radius: 999px; padding: 9px 18px; cursor: pointer;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset, 0 2px 4px rgba(60, 30, 10, 0.12);
  transition: transform 0.08s, background 0.2s;
}
.btn:hover:not(:disabled) { background: #fff3df; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: linear-gradient(180deg, #d77a5a, var(--accent-dark)); color: #fff; border-color: #8c412b; }
.btn.primary:hover:not(:disabled) { background: linear-gradient(180deg, #e0886a, #ad573c); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.danger { color: #9c2f22; }
.btn.small { padding: 5px 12px; font-size: 0.9rem; }
.btn.big { padding: 14px 26px; font-size: 1.08rem; display: flex; flex-direction: column; align-items: center; }
.btn.big small { font-weight: 400; opacity: 0.85; font-size: 0.8rem; }
.btn.on { background: var(--honey); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 16px; }
.row.end { justify-content: flex-end; }

/* ---------- home ---------- */
.home { display: grid; gap: 18px; max-width: 860px; margin: 0 auto; }
.hero { text-align: center; padding: 30px 24px; background: linear-gradient(180deg, rgba(255, 249, 238, 0.94), rgba(250, 236, 212, 0.9)); }
.hero-tiles { display: flex; gap: 18px; justify-content: center; margin-bottom: 12px; flex-wrap: wrap; }
.hero-tiles .word { display: flex; gap: 5px; }
.hero-tiles .tile:nth-child(odd) { transform: rotate(-3deg); }
.hero-tiles .tile:nth-child(even) { transform: rotate(2deg) translateY(2px); }
.tagline { font-family: var(--serif); font-size: 1.15rem; color: var(--ink-soft); margin: 0 0 20px; }
.hero-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-bottom: 10px; }
.notice { background: #fff1d6; border-radius: 10px; padding: 8px 12px; }
.games { list-style: none; margin: 0; padding: 0; }
.game-row { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 12px 0; border-top: 1px solid var(--line); }
.game-row:first-child { border-top: 0; }
.g-title { font-weight: 600; }
.g-sub { color: var(--ink-soft); font-size: 0.92rem; }
.g-btns { display: flex; gap: 6px; flex-shrink: 0; }
.yours { color: var(--accent-dark); }
.invites ul { list-style: none; padding: 0; margin: 0; }
.invites li { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 8px 0; flex-wrap: wrap; }
.foot { text-align: center; color: rgba(255, 245, 230, 0.85); text-shadow: 0 1px 4px #0009; }
.foot a { color: #ffe2b8; }

/* ---------- setup ---------- */
.setup { max-width: 620px; margin: 0 auto; }
.seats { list-style: none; padding: 0; margin: 14px 0; display: grid; gap: 10px; }
.seats li { display: flex; gap: 10px; align-items: center; }
.seats input, .seats select, .chat-form input {
  font: inherit; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: #fffdf8; color: var(--ink);
}
.seats input { flex: 1; min-width: 0; }
.dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; box-shadow: 0 0 0 2px #fff8; }

/* ---------- tiles ---------- */
.tile {
  position: relative;
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, #fff8e8 0%, var(--tile) 55%, #efdcb8 100%);
  border-radius: 14%;
  box-shadow: 0 1px 0 #fff inset, 0 -2px 0 rgba(140, 100, 50, 0.25) inset, 0 2px 3px rgba(60, 30, 5, 0.35);
  color: #3a2614;
  font-family: var(--serif); font-weight: 700;
  user-select: none; -webkit-user-select: none; touch-action: none;
  container-type: size;
}
.tile .l { font-size: 62cqh; line-height: 1; transform: translateY(-3%); }
.tile .v { position: absolute; right: 9%; bottom: 5%; font-size: 26cqh; font-family: var(--sans); font-weight: 600; opacity: 0.8; }
.tile.blank .l { color: #8a5a2a; font-style: italic; }
.tile.blank .l:empty::after { content: '✦'; font-style: normal; color: #b48a55; opacity: 0.55; font-size: 0.7em; }
.tile.mini { width: 40px; height: 40px; display: inline-flex; border: 0; cursor: default; }
button.tile.mini { cursor: pointer; }
.tile.pending { background: linear-gradient(160deg, #fffbe9, #fbe7b3); box-shadow: 0 0 0 2px var(--honey), 0 3px 6px rgba(60, 30, 5, 0.4); cursor: grab; }
.tile.pending.bad { box-shadow: 0 0 0 2px #c4473a, 0 3px 6px rgba(60, 30, 5, 0.4); }
.tile.recent { background: linear-gradient(160deg, #fff6dc, #f6dc9f); }
.tile.selected { transform: translateY(-8px); box-shadow: 0 0 0 3px var(--honey), 0 8px 14px rgba(60, 30, 5, 0.4); }
.tile.swap { transform: translateY(-8px); box-shadow: 0 0 0 3px var(--sage), 0 8px 14px rgba(60, 30, 5, 0.4); }
.tile.ghost { position: fixed; left: 0; top: 0; z-index: 100; pointer-events: none; box-shadow: 0 14px 24px rgba(40, 20, 5, 0.45); }

/* ---------- game layout ---------- */
.game {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  grid-template-areas: "board side" "rack side";
  grid-template-rows: auto 1fr;
  align-items: start;
  gap: 16px;
  position: relative;
}
.board-wrap { grid-area: board; display: flex; justify-content: center; }
.side { grid-area: side; display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.rack-bar { grid-area: rack; display: flex; flex-direction: column; align-items: center; gap: 10px; }

.board {
  --bs: min(100%, calc(100vh - var(--top) - 230px), 760px);
  width: var(--bs); aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(15, 1fr); grid-template-rows: repeat(15, 1fr);
  gap: 2px; padding: 8px;
  background: linear-gradient(135deg, #b88a5a, #8f6239);
  border-radius: 14px;
  box-shadow: 0 0 0 4px #6e4a2c, var(--shadow), 0 0 0 1px #c9a173 inset;
  min-width: 300px;
}
.cell {
  position: relative; border-radius: 4px;
  background: #f3e4c6;
  display: flex; align-items: center; justify-content: center;
  container-type: size;
  box-shadow: 0 1px 1px rgba(80, 50, 20, 0.25) inset;
}
.cell .pl { font-size: 30cqh; font-weight: 700; color: rgba(255, 255, 255, 0.95); text-align: center; line-height: 1; font-family: var(--sans); text-shadow: 0 1px 1px rgba(0, 0, 0, 0.15); }
.cell .pl small { display: block; font-size: 20cqh; font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em; }
.cell.p2L { background: var(--p2L); } .cell.p2L .pl { color: #3f6046; }
.cell.p3L { background: var(--p3L); }
.cell.p2W { background: var(--p2W); } .cell.p2W .pl { color: #7a4c12; }
.cell.p3W { background: var(--p3W); }
.cell.pST { background: var(--st); } .cell.pST .pl { font-size: 70cqh; color: #fff8e6; }
.cell.filled { background: transparent; box-shadow: none; }
.cell.target:hover { outline: 2px dashed var(--accent); outline-offset: -2px; cursor: pointer; }
.cell > .tile { border-radius: 12%; }

.rack {
  display: flex; gap: 8px; padding: 12px 16px;
  background: linear-gradient(180deg, #a77a4d, #7b5432);
  border-radius: 14px; box-shadow: 0 0 0 3px #5f3f24, var(--shadow);
  min-height: 74px;
}
.slot { width: clamp(42px, 9vw, 56px); height: clamp(42px, 9vw, 56px); }
.slot .tile { cursor: grab; transition: transform 0.15s; }
.slot.empty { border-radius: 10px; background: rgba(0, 0, 0, 0.12); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset; }
.rack.swapping .tile { cursor: pointer; }
.preview {
  min-height: 28px; color: var(--cream); text-shadow: 0 1px 4px #000a; font-weight: 600; text-align: center;
}
.preview .ok { color: #ffe9b3; }
.preview .bad, .preview .warn { color: #ffb3a3; }
.preview .btn { text-shadow: none; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.actions .btn.ghost { color: var(--cream); border-color: rgba(255, 240, 220, 0.4); background: rgba(255, 240, 220, 0.12); }

.plist { list-style: none; padding: 0; margin: 0 0 8px; }
.plist li { display: flex; gap: 10px; align-items: center; padding: 7px 8px; border-radius: 10px; }
.plist li.turn { background: #fbe6c2; box-shadow: 0 0 0 1px #edc98b inset; }
.plist li.out { opacity: 0.5; }
.pname { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.pname small { font-weight: 400; color: var(--ink-soft); }
.pname .away { color: #a24f35; }
.pscore { font-family: var(--serif); font-size: 1.25rem; font-weight: 700; }
.bag { color: var(--ink-soft); font-size: 0.92rem; padding: 0 8px; }
.status { font-size: 0.98rem; }
.thinking::after { content: ' …'; animation: dots 1.4s steps(4) infinite; display: inline-block; width: 1.4em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0; } to { width: 1.4em; } }
.moves { list-style: none; padding: 0; margin: 0; font-size: 0.92rem; }
.moves li { padding: 3px 0; border-top: 1px dashed var(--line); }
.moves li:first-child { border-top: 0; }

.chat { display: flex; flex-direction: column; }
.chat-log { max-height: 180px; min-height: 80px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding: 4px 2px 8px; }
.msg { background: #fff; border-radius: 12px 12px 12px 4px; padding: 6px 10px; align-self: flex-start; max-width: 92%; word-wrap: break-word; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08); }
.msg.mine { align-self: flex-end; background: #fbe3c4; border-radius: 12px 12px 4px 12px; }
.msg b { display: block; font-size: 0.78rem; color: var(--ink-soft); }
.chat-form { display: flex; gap: 6px; }
.chat-form input { flex: 1; min-width: 0; padding: 7px 10px; }

.cover { position: absolute; inset: 0; z-index: 5; display: flex; align-items: center; justify-content: center; background: rgba(40, 22, 10, 0.55); backdrop-filter: blur(10px); border-radius: var(--radius); }
.cover .card { max-width: 420px; }

/* ---------- dialogs ---------- */
.modal-wrap { position: fixed; inset: 0; z-index: 50; background: rgba(30, 16, 6, 0.55); display: flex; align-items: center; justify-content: center; padding: 16px; animation: fade 0.2s; }
.modal { background: var(--paper-solid); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px 24px; width: min(440px, 100%); max-height: 90vh; overflow-y: auto; }
.modal.wide { width: min(640px, 100%); }
@keyframes fade { from { opacity: 0; } }
.letters { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.menu { display: grid; gap: 8px; }
.rules { padding-left: 1.2em; } .rules li { margin-bottom: 8px; }
.chip { display: inline-block; padding: 1px 8px; border-radius: 6px; font-size: 0.85rem; font-weight: 600; }
.chip.p2L { background: var(--p2L); color: #3f6046; } .chip.p3L { background: var(--p3L); color: #fff; }
.chip.p2W { background: var(--p2W); color: #7a4c12; } .chip.p3W { background: var(--p3W); color: #fff; }
.results { list-style: none; padding: 0; }
.results li { display: grid; grid-template-columns: auto 1fr auto; gap: 4px 10px; align-items: center; padding: 8px; border-radius: 10px; }
.results li:first-child { background: #fbe6c2; }
.results li:first-child .pname::before { content: '🏆 '; }
.results .adj { grid-column: 2 / 4; color: var(--ink-soft); }
.scenes { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.scene { padding: 0; border: 2px solid transparent; border-radius: 12px; overflow: hidden; background: #fff; cursor: pointer; text-align: left; font: inherit; }
.scene img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; display: block; }
.scene span { display: block; padding: 5px 8px; font-size: 0.9rem; }
.scene b { font-size: 0.72rem; background: var(--honey); border-radius: 6px; padding: 1px 6px; margin-left: 4px; }
.scene.on { border-color: var(--accent); }
.scene.locked img { filter: grayscale(0.5) brightness(0.8); }
.toggle, .slider { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.slider input { flex: 1; accent-color: var(--accent); }
.toggle input { accent-color: var(--accent); width: 18px; height: 18px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0; z-index: 60;
  background: #3b2416; color: var(--cream); padding: 10px 18px; border-radius: 999px; box-shadow: var(--shadow);
  transition: all 0.3s; pointer-events: none; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- smaller screens ---------- */
@media (max-width: 980px) {
  .game { grid-template-columns: 1fr; grid-template-areas: "board" "rack" "side"; }
  .board { --bs: min(100%, 640px); min-width: 0; padding: 5px; gap: 1px; }
  .side { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
}
@media (max-width: 560px) {
  #app { padding: 10px 6px 30px; }
  .cell .pl small { display: none; }
  .cell .pl { font-size: 38cqh; }
  .rack { gap: 5px; padding: 8px 10px; }
  .actions .btn { padding: 8px 12px; }
  .home-link { font-size: 0.9rem; }
}

.pick { list-style: none; padding: 0; margin: 10px 0; max-height: 260px; overflow-y: auto; }
.pick li { padding: 6px 4px; border-top: 1px solid var(--line); }
.pick label { display: flex; gap: 10px; align-items: center; cursor: pointer; }
.pick input { width: 18px; height: 18px; accent-color: var(--accent); }
.pick small { color: var(--ink-soft); }
.players .btn.small { margin: 8px 8px 0; }
.players p.small { margin: 6px 8px 0; }

/* ---------- dark mode (the platform sets data-iht-theme on <html>) ---------- */
[data-iht-theme="dark"] {
  color-scheme: dark;
  --ink: #efe6d8;
  --ink-soft: #c2b2a0;
  --paper: rgba(36, 29, 23, 0.92);
  --paper-solid: #241d17;
  --line: rgba(239, 230, 216, 0.14);
  --accent-dark: #e8a07f;
  --btn: #3a3027;
  --btn-hover: #463a2f;
  --field: #1e1914;
  --field-line: #43382c;
  --highlight: #4a3524;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.35);
}
[data-iht-theme="dark"] #bg { filter: blur(2px) saturate(0.9) brightness(0.62); }
[data-iht-theme="dark"] #glow { background: linear-gradient(180deg, rgba(15, 9, 5, 0.45), rgba(15, 9, 5, 0.7)); }
[data-iht-theme="dark"] .btn { background: var(--btn); color: var(--ink); border-color: var(--field-line); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3); }
[data-iht-theme="dark"] .btn:hover:not(:disabled) { background: var(--btn-hover); }
[data-iht-theme="dark"] .btn.primary { background: linear-gradient(180deg, #b8603f, #8f442c); color: #fff; border-color: #6e3220; }
[data-iht-theme="dark"] .btn.primary:hover:not(:disabled) { background: linear-gradient(180deg, #c96d4b, #9c4c32); }
[data-iht-theme="dark"] .btn.ghost { background: transparent; }
[data-iht-theme="dark"] .btn.danger { color: #ff9c8c; }
[data-iht-theme="dark"] .btn.on { background: #8a6420; }
[data-iht-theme="dark"] .hero { background: linear-gradient(180deg, rgba(42, 33, 26, 0.95), rgba(30, 24, 19, 0.92)); }
[data-iht-theme="dark"] .seats input, [data-iht-theme="dark"] .seats select, [data-iht-theme="dark"] .chat-form input { background: var(--field); color: var(--ink); border-color: var(--field-line); }
[data-iht-theme="dark"] .msg { background: #2e261f; box-shadow: none; }
[data-iht-theme="dark"] .msg.mine { background: var(--highlight); }
[data-iht-theme="dark"] .plist li.turn { background: var(--highlight); box-shadow: 0 0 0 1px #6b4e33 inset; }
[data-iht-theme="dark"] .results li:first-child { background: var(--highlight); }
[data-iht-theme="dark"] .notice { background: #3a2e20; }
[data-iht-theme="dark"] .scene { background: #2e261f; }
[data-iht-theme="dark"] .yours, [data-iht-theme="dark"] .pname .away { color: #f0a585; }
[data-iht-theme="dark"] .board { box-shadow: 0 0 0 4px #4f3420, var(--shadow), 0 0 0 1px #a07c55 inset; }
[data-iht-theme="dark"] .cell { background: #e6d3ae; }
[data-iht-theme="dark"] .cell.filled { background: transparent; }
[data-iht-theme="dark"] .cell.p2L { background: #a9c2a0; } [data-iht-theme="dark"] .cell.p3L { background: #5f8a67; }
[data-iht-theme="dark"] .cell.p2W { background: #e0b26c; } [data-iht-theme="dark"] .cell.p3W { background: #bf6a4f; }
[data-iht-theme="dark"] .cell.pST { background: #d8a45c; }
[data-iht-theme="dark"] .topbar { background: linear-gradient(180deg, rgba(20, 12, 7, 0.9), rgba(20, 12, 7, 0.6)); }
[data-iht-theme="dark"] #toast { background: #efe6d8; color: #2a1a10; }
[data-iht-theme="dark"] .appearance .btn.on { background: #6b4e33; }
.appearance { display: flex; gap: 8px; flex-wrap: wrap; }
.appearance .btn.on { background: var(--honey); }
.modal h3 { margin-top: 16px; }

/* ---------- phones ---------- */
.scorebar, .phone-only { display: none; }
.actions .ic { margin-right: 6px; }
body { touch-action: manipulation; }
/* Score bar parts (shown on phones) */
.scorebar .chips { display: flex; gap: 5px; overflow-x: auto; flex: 1; min-width: 0; scrollbar-width: none; padding: 2px 0; }
.scorebar .chips::-webkit-scrollbar { display: none; }
.pchip { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; flex-shrink: 0;
  background: var(--paper); color: var(--ink); border-radius: 999px; padding: 4px 10px; font-size: 0.84rem; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }
.pchip b { font-family: var(--serif); font-size: 0.98rem; }
.pchip .dot { width: 9px; height: 9px; box-shadow: none; }
.pchip.turn { box-shadow: 0 0 0 2px var(--honey), 0 1px 3px rgba(0, 0, 0, 0.25); }
.pchip.out { opacity: 0.5; }
.bagchip { color: var(--ink-soft); }
.sbtn { position: relative; flex-shrink: 0; width: 38px; height: 34px; border-radius: 999px; border: 0; font-size: 1rem;
  background: var(--paper); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); cursor: pointer; }
.sbtn.on { background: var(--honey); }
.sbtn .badge { position: absolute; top: -4px; right: -4px; background: var(--accent); color: #fff; font-size: 0.68rem; font-weight: 700;
  min-width: 17px; height: 17px; border-radius: 9px; display: flex; align-items: center; justify-content: center; padding: 0 4px; }


@media (max-width: 700px) {
  :root { --top: 46px; }
  .topbar { padding: 0 8px; }
  .icon { width: 34px; height: 34px; font-size: 0.95rem; }
  .home-link { font-size: 0.92rem; }
  #app { padding: 6px 4px 0; }
  .card { padding: 12px 14px; }

  /* Game screen: scores on top, board, then the rack and buttons docked at the bottom. */
  .game {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "scores" "status" "board" "rack" "players" "chat" "log";
    gap: 6px;
  }
  .side { display: contents; }
  #players { grid-area: players; }
  .status { grid-area: status; }
  .chat { grid-area: chat; }
  .log { grid-area: log; margin-bottom: 12px; }
  .status.card { padding: 6px 12px; font-size: 0.9rem; border-radius: 12px; }
  .status .btn.small { padding: 3px 10px; }

  .scorebar { grid-area: scores; display: flex; align-items: center; gap: 6px; }
  .board-wrap { position: relative; min-width: 0; }
  .scorebar { position: sticky; top: var(--top); z-index: 7; margin: -6px -4px 0; padding: 6px 6px; background: rgba(43, 25, 13, 0.88); backdrop-filter: blur(4px); border-radius: 0 0 12px 12px; }
  .board { --bs: 100%; width: 100%; min-width: 0; padding: 3px; gap: 1px; border-radius: 8px; box-shadow: 0 0 0 2px #6e4a2c, var(--shadow); }
  .cell { border-radius: 2px; }
  .cell .pl small { display: none; }
  .cell .pl { font-size: 40cqh; }
  .cell.pST .pl { font-size: 70cqh; }
  /* Zoomed: bigger squares; drag the board around with a finger. */
  .board-wrap.zoomed { display: block; width: 100%; max-width: calc(100vw - 8px); overflow: auto; aspect-ratio: 1; border-radius: 8px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
    box-shadow: 0 0 0 2px #6e4a2c; }
  .board-wrap.zoomed .board { width: 700px; max-width: none; padding: 5px; gap: 2px; }
  .board-wrap.zoomed .cell .pl small { display: block; }
  .board-wrap.zoomed .cell .pl { font-size: 30cqh; }

  .rack-bar {
    position: sticky; bottom: 0; z-index: 6;
    margin: 0 -4px; padding: 14px 6px calc(8px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, rgba(43, 25, 13, 0), rgba(43, 25, 13, 0.9) 14px, rgba(43, 25, 13, 0.94));
    backdrop-filter: blur(3px);
    gap: 6px;
  }
  .preview { min-height: 20px; font-size: 0.86rem; line-height: 1.25; padding: 0 4px; }
  .rack { width: 100%; justify-content: center; gap: 4px; padding: 6px; min-height: 0; border-radius: 10px; box-shadow: 0 0 0 2px #5f3f24, var(--shadow); }
  .slot { width: min(52px, calc((100vw - 52px) / 7)); height: min(52px, calc((100vw - 52px) / 7)); }
  .actions { display: grid; grid-template-columns: 1.35fr repeat(5, 1fr); gap: 4px; width: 100%; }
  .actions .btn { padding: 2px 0; min-height: 46px; font-size: 0.74rem; font-weight: 600; display: flex; flex-direction: column;
    align-items: center; justify-content: center; line-height: 1.1; border-radius: 12px; gap: 1px; }
  .actions .ic { margin: 0; font-size: 1.1rem; line-height: 1; }
  .actions .long { display: none; }
  .phone-only { display: inline; }

  .chat-log { max-height: 45vh; }
  .chat-form input { font-size: 16px; } /* 16px stops iPhones zooming in on the field */
  .seats input, .seats select { font-size: 16px; }

  /* Dialogs slide up from the bottom. */
  .modal-wrap { align-items: flex-end; padding: 0; }
  .modal, .modal.wide { width: 100%; max-height: 88vh; border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  .modal .row.end .btn { flex: 1 1 auto; }
  .letters { grid-template-columns: repeat(6, 1fr); }
  .letters .tile.mini { width: 100%; height: auto; aspect-ratio: 1; }
  .scenes { grid-template-columns: repeat(2, 1fr); }
  .menu .btn { min-height: 46px; }

  .cover { position: fixed; inset: 0; z-index: 30; border-radius: 0; padding: 16px; }

  /* Home and setup */
  .home { gap: 12px; }
  .hero { padding: 22px 14px; }
  .hero-tiles { gap: 10px; }
  .hero-tiles .word { gap: 3px; }
  .hero-tiles .tile.mini { width: 30px; height: 30px; }
  .hero-actions { flex-direction: column; align-items: stretch; }
  .hero-actions .btn.big { width: 100%; }
  .game-row { flex-wrap: wrap; }
  .g-main { flex: 1 1 200px; }
  .seats li { flex-wrap: wrap; }
  .seats input { flex: 1 1 150px; }
  .seats select { flex: 1 1 100%; }
  .setup .row.end .btn { flex: 1; }

  #toast { bottom: calc(180px + env(safe-area-inset-bottom)); }
}

/* Phones turned sideways: board on the left, everything else on the right. */
@media (max-height: 520px) and (orientation: landscape) and (max-width: 1000px) {
  :root { --top: 42px; }
  #app { padding: 4px 8px 0; max-width: none; }
  .game {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "board scores" "board status" "board rack" "board players" "board chat" "board log";
    align-items: start; gap: 6px 12px;
  }
  .side { display: contents; }
  #players { grid-area: players; } .status { grid-area: status; } .chat { grid-area: chat; } .log { grid-area: log; }
  .scorebar { grid-area: scores; display: flex; align-items: center; gap: 6px; }
  .board-wrap { position: sticky; top: calc(var(--top) + 4px); }
  .board { --bs: calc(100vh - var(--top) - 14px); width: var(--bs); min-width: 0; padding: 3px; gap: 1px; }
  .cell .pl small { display: none; }
  .cell .pl { font-size: 40cqh; }
  .rack-bar { position: static; background: none; padding: 0; }
  #sb-zoom { display: none; }
  .slot { width: 44px; height: 44px; }
  .actions .btn { padding: 6px 10px; }
}
