/* ===== Word Tiles ===== */
@font-face {
  font-family: 'Atomic Age';
  src: url('fonts/atomic-age.woff2') format('woff2');
  font-display: swap;
}

:root {
  --cell: 40px;
  --bs: 600px;
  --rt: 60px;
  --side: 420px;
  --gap: 16px;
  --ui-font: "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  --tile-font: "Arial Black", "Arial Bold", "Helvetica Neue", Arial, sans-serif;
  --title-font: Georgia, "Times New Roman", serif;
}

/* ---------- Classic theme (default) ---------- */
body[data-theme="classic"] {
  --bg-base: #8a5a33;
  --bg:
    radial-gradient(ellipse at 50% 40%, rgba(255, 220, 170, .18), transparent 65%),
    repeating-linear-gradient(97deg, rgba(255, 255, 255, .035) 0 2px, transparent 2px 7px),
    repeating-linear-gradient(93deg, rgba(60, 30, 10, .07) 0 1px, transparent 1px 19px),
    repeating-linear-gradient(91deg, rgba(60, 30, 10, .06) 0 3px, transparent 3px 53px),
    linear-gradient(180deg, #a26b3f 0%, #8d5b34 55%, #75482a 100%);
  --ink: #2b1a0d;
  --ink-soft: #6b4a2c;
  --panel: #fbf3e2;
  --panel-2: #f1e3c4;
  --panel-edge: #c9a978;
  --on-bg: #fff6e4;
  --frame: #4a2b15;
  --frame-hi: #6d4224;
  --sq: #f3e9cf;
  --sq-line: #cdb68a;
  --tw: #b8322a; --tw-ink: #fff;
  --dw: #eb9a86; --dw-ink: #5a1d12;
  --tl: #2d64a3; --tl-ink: #fff;
  --dl: #a9d1ea; --dl-ink: #12385a;
  --st: #eb9a86; --st-ink: #8a2a18;
  --tile-top: #fbefcf;
  --tile-bot: #efd9a6;
  --tile-edge: #b48e55;
  --tile-ink: #2b1a0d;
  --tile-blank-ink: #9a6a2c;
  --pending: #ffd34d;
  --last: #2e8b57;
  --accent: #2e7d4f;
  --accent-ink: #fff;
  --accent-2: #b8322a;
  --bad: #c62828;
  --good: #1e7a3c;
  --btn: #fff8ea;
  --btn-edge: #c4a06c;
  --btn-ink: #3a2312;
  --active-glow: rgba(46, 125, 79, .35);
}

/* ---------- Atomic '57 theme ---------- */
body[data-theme="atomic"] {
  --bg-base: #2c2c30;
  --bg:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240' viewBox='0 0 240 240'%3E%3Cg fill='none' stroke='%239ed9c5' stroke-opacity='.10' stroke-width='2.2'%3E%3Cellipse cx='60' cy='60' rx='34' ry='12'/%3E%3Cellipse cx='60' cy='60' rx='34' ry='12' transform='rotate(60 60 60)'/%3E%3Cellipse cx='60' cy='60' rx='34' ry='12' transform='rotate(-60 60 60)'/%3E%3C/g%3E%3Ccircle cx='60' cy='60' r='4.5' fill='%23d9622b' fill-opacity='.22'/%3E%3Cg stroke='%23e8b23a' stroke-opacity='.13' stroke-width='2.2' stroke-linecap='round'%3E%3Cpath d='M180 160v-22M180 160v22M180 160h-22M180 160h22M180 160l-14-14M180 160l14 14M180 160l14-14M180 160l-14 14'/%3E%3C/g%3E%3Cpath d='M150 40q20-18 46 0q-24-6-46 0z' fill='%239ed9c5' fill-opacity='.08'/%3E%3Cpath d='M40 190q14 20 40 8q-26 0-40-8z' fill='%23d9622b' fill-opacity='.09'/%3E%3Ccircle cx='120' cy='120' r='2' fill='%23f4ead2' fill-opacity='.15'/%3E%3Ccircle cx='215' cy='95' r='2' fill='%23f4ead2' fill-opacity='.15'/%3E%3Ccircle cx='100' cy='215' r='2' fill='%23f4ead2' fill-opacity='.15'/%3E%3C/svg%3E"),
    radial-gradient(ellipse at 30% 20%, #3a3a40 0%, #2c2c30 55%, #222226 100%);
  --ink: #2a2a2e;
  --ink-soft: #5d5a52;
  --panel: #f6eedb;
  --panel-2: #e9dfc4;
  --panel-edge: #2a2a2e;
  --on-bg: #f6eedb;
  --frame: #1f1f22;
  --frame-hi: #3d3d42;
  --sq: #d7eee3;
  --sq-line: #a8cfc0;
  --tw: #d9622b; --tw-ink: #fff;
  --dw: #f4a98f; --dw-ink: #5b2410;
  --tl: #23797c; --tl-ink: #fff;
  --dl: #8ed6c0; --dl-ink: #134a42;
  --st: #e8b23a; --st-ink: #4a3405;
  --tile-top: #fdf5e1;
  --tile-bot: #f1e1bb;
  --tile-edge: #8f7a52;
  --tile-ink: #2a2a2e;
  --tile-blank-ink: #c4541f;
  --pending: #e8b23a;
  --last: #d9622b;
  --accent: #d9622b;
  --accent-ink: #fff;
  --accent-2: #23797c;
  --bad: #c0392b;
  --good: #23797c;
  --btn: #f6eedb;
  --btn-edge: #2a2a2e;
  --btn-ink: #2a2a2e;
  --active-glow: rgba(217, 98, 43, .4);
  --title-font: 'Atomic Age', Georgia, serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  background-color: var(--bg-base);
  color: var(--ink);
  font-family: var(--ui-font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
button { font-family: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--pending); outline-offset: 2px; }
svg { display: block; }

#app { position: fixed; inset: 0; }
.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: block; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--btn); color: var(--btn-ink);
  border: 2px solid var(--btn-edge); border-radius: 14px;
  font-size: 18px; font-weight: 700; padding: 10px 18px; min-height: 52px;
  box-shadow: 0 3px 0 var(--btn-edge), 0 6px 14px rgba(0, 0, 0, .18);
  transition: transform .08s ease, box-shadow .08s ease, filter .15s;
}
.btn:hover { filter: brightness(1.04); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--btn-edge), 0 3px 8px rgba(0, 0, 0, .2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: rgba(0, 0, 0, .25); box-shadow: 0 3px 0 rgba(0, 0, 0, .28), 0 6px 14px rgba(0, 0, 0, .2); }
.btn.big { font-size: 22px; min-height: 64px; width: 100%; }
.btn.small { font-size: 16px; min-height: 46px; padding: 6px 14px; flex-direction: row; gap: 6px; }
.btn.small b { font-weight: 800; }
.btn.danger { color: var(--bad); }
.btn-sub { font-size: 14px; font-weight: 600; opacity: .85; margin-top: 2px; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.btn.icon-only { width: 46px; padding: 0; }
.icon-only svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Title screen ---------- */
#title { overflow-y: auto; }
.title-inner {
  position: relative; min-height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 26px; padding: 24px 16px 54px;
}
.logo { text-align: center; }
.logo-row { display: flex; justify-content: center; gap: 6px; margin: 6px 0; }
.logo-row .tile { --ts: clamp(46px, 12vw, 74px); width: var(--ts); height: var(--ts); position: relative; transform: rotate(var(--rot, 0deg)); animation: logoDrop .6s cubic-bezier(.2, 1.4, .4, 1) both; animation-delay: var(--d, 0s); }
.logo-sub { margin-top: 12px; font-size: 20px; color: var(--on-bg); opacity: .92; letter-spacing: .06em; font-weight: 600; text-shadow: 0 1px 2px rgba(0, 0, 0, .35); }
body[data-theme="atomic"] .logo-sub { font-family: var(--title-font); font-weight: 400; letter-spacing: .12em; font-size: 22px; color: #9ed9c5; }
@keyframes logoDrop { from { opacity: 0; transform: translateY(-30px) rotate(var(--rot, 0deg)); } to { opacity: 1; transform: translateY(0) rotate(var(--rot, 0deg)); } }
.title-actions { width: min(440px, 100%); display: flex; flex-direction: column; gap: 14px; }
.card { background: var(--panel); border: 2px solid var(--panel-edge); border-radius: 18px; padding: 12px; box-shadow: 0 8px 24px rgba(0, 0, 0, .22); }
.play-card { display: flex; flex-direction: column; gap: 10px; }
.seg { display: flex; background: var(--panel-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; font-size: 18px; font-weight: 700; padding: 10px 4px; border-radius: 9px; color: var(--ink-soft); min-height: 48px; }
.seg button[aria-checked="true"] { background: var(--panel); color: var(--ink); box-shadow: 0 2px 6px rgba(0, 0, 0, .15); outline: 2px solid var(--accent); }
.title-links { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.goat-link { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); color: var(--on-bg); opacity: .8; font-size: 15px; text-decoration: none; font-weight: 600; white-space: nowrap; }
.goat-link:hover { opacity: 1; text-decoration: underline; }
.title-deco { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
body[data-theme="atomic"] .title-deco::before {
  content: ""; position: absolute; width: 520px; height: 520px; left: 50%; top: 50%; transform: translate(-50%, -62%);
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 200'%3E%3Cg fill='none' stroke='%239ed9c5' stroke-width='1.6' stroke-opacity='.35'%3E%3Cellipse cx='100' cy='100' rx='92' ry='30'/%3E%3Cellipse cx='100' cy='100' rx='92' ry='30' transform='rotate(60 100 100)'/%3E%3Cellipse cx='100' cy='100' rx='92' ry='30' transform='rotate(-60 100 100)'/%3E%3C/g%3E%3Ccircle cx='192' cy='100' r='5' fill='%23e8b23a' fill-opacity='.7'/%3E%3Ccircle cx='54' cy='20' r='5' fill='%23d9622b' fill-opacity='.7'/%3E%3Ccircle cx='54' cy='180' r='5' fill='%239ed9c5' fill-opacity='.7'/%3E%3C/svg%3E") center/contain no-repeat;
  animation: spin 80s linear infinite;
}
body[data-theme="atomic"] .title-deco::after {
  content: ""; position: absolute; right: -60px; top: -60px; width: 260px; height: 260px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-50 -50 100 100'%3E%3Cg stroke='%23e8b23a' stroke-opacity='.35' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M0 -46V46M-46 0H46M-32 -32L32 32M32 -32L-32 32M-18 -42L18 42M18 -42L-18 42M-42 -18L42 18M42 -18L-42 18'/%3E%3C/g%3E%3Ccircle r='7' fill='%23d9622b' fill-opacity='.5'/%3E%3C/svg%3E") center/contain no-repeat;
}
@keyframes spin { to { transform: translate(-50%, -62%) rotate(360deg); } }

/* ---------- Tiles ---------- */
.tile {
  position: relative;
  background: linear-gradient(170deg, var(--tile-top) 0%, var(--tile-bot) 100%);
  border-radius: 12%;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .14), inset 0 2px 0 rgba(255, 255, 255, .7), 0 2px 3px rgba(0, 0, 0, .35);
  color: var(--tile-ink);
  display: flex; align-items: center; justify-content: center;
}
.tile .L { font-family: var(--tile-font); font-weight: 900; font-size: calc(var(--ts) * .58); line-height: 1; margin-top: -4%; margin-left: -6%; }
.tile .v { position: absolute; right: 8%; bottom: 5%; font-family: var(--ui-font); font-weight: 800; font-size: max(9px, calc(var(--ts) * .26)); line-height: 1; }
.tile.blank .L { color: var(--tile-blank-ink); }
.tile.blank.unset .L { opacity: .35; }

/* ---------- Game layout ---------- */
#game.active { display: grid; padding: var(--pad, 10px); gap: var(--rgap, 8px) var(--gap); justify-content: center; align-content: center; }
#game.wide {
  grid-template-columns: var(--bs) var(--side);
  grid-template-rows: auto auto auto auto auto minmax(0, 1fr);
  grid-template-areas: "board top" "board scores" "board status" "board rack" "board actions" "board log";
}
#game.tall {
  grid-template-columns: var(--bs);
  grid-template-rows: auto auto auto auto auto auto;
  grid-template-areas: "top" "scores" "board" "status" "rack" "actions";
}
#game.tall #logPanel { display: none; }
#game.wide #btnLog { display: none; }
#top { grid-area: top; display: flex; gap: 8px; align-items: center; }
#scores { grid-area: scores; }
#boardWrap { grid-area: board; }
#status { grid-area: status; }
#rackArea { grid-area: rack; }
#actions { grid-area: actions; }
#logPanel { grid-area: log; }

.top-title { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; gap: 8px; color: var(--on-bg); font-family: var(--title-font); font-size: 24px; font-weight: 700; text-shadow: 0 2px 3px rgba(0, 0, 0, .35); white-space: nowrap; overflow: hidden; }
.top-title .atom { width: 30px; height: 30px; color: #9ed9c5; display: none; flex: none; }
body[data-theme="atomic"] .top-title { font-weight: 400; color: #f6eedb; letter-spacing: .04em; }
body[data-theme="atomic"] .top-title .atom { display: block; }
#game.tall .top-title span { display: none; }
#game.tall .top-title .atom { display: none; }
#game.narrow #btnZoom span, #game.narrow #btnLog span { display: none; }
#game.narrow .tbtn { min-width: 50px; justify-content: center; }
.tbtn {
  display: flex; align-items: center; gap: 6px; height: 46px; padding: 0 12px;
  background: var(--btn); color: var(--btn-ink); border: 2px solid var(--btn-edge); border-radius: 12px;
  font-size: 16px; font-weight: 700; box-shadow: 0 2px 0 var(--btn-edge);
}
.tbtn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.tbtn:active { transform: translateY(2px); box-shadow: none; }
.tbtn.on { background: var(--accent); color: var(--accent-ink); }
#btnZoom { display: none; }
#game.canzoom #btnZoom { display: flex; }

#scores { display: flex; gap: 10px; }
.pcard {
  flex: 1; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--panel); border: 2px solid var(--panel-edge); border-radius: 14px; padding: 6px 14px; min-height: 54px;
  transition: box-shadow .25s, transform .25s;
  position: relative;
}
.pcard .pname { font-size: 18px; font-weight: 700; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pcard .pscore { font-size: 30px; font-weight: 900; font-variant-numeric: tabular-nums; color: var(--ink); }
.pcard.turn { box-shadow: 0 0 0 4px var(--accent), 0 6px 16px var(--active-glow); }
.pcard.turn .pname { color: var(--ink); }
.pcard.turn::before { content: ""; position: absolute; left: 50%; top: -9px; transform: translateX(-50%); border: 7px solid transparent; border-top-color: var(--accent); border-bottom: 0; display: none; }
body[data-theme="atomic"] .pcard .pscore { font-family: var(--title-font); font-weight: 400; }

/* ---------- Board ---------- */
#boardWrap { width: var(--bs); height: var(--bs); position: relative; overflow: hidden; border-radius: 10px; touch-action: none; }
#boardWrap.zoomed { overflow: auto; touch-action: pan-x pan-y; -webkit-overflow-scrolling: touch; }
#boardFrame {
  width: var(--bsz, var(--bs)); height: var(--bsz, var(--bs));
  background: linear-gradient(145deg, var(--frame-hi), var(--frame));
  padding: var(--fp, 6px); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4), inset 0 1px 0 rgba(255, 255, 255, .15);
}
#board {
  width: 100%; height: 100%;
  display: grid; grid-template-columns: repeat(15, 1fr); grid-template-rows: repeat(15, 1fr);
  gap: var(--bg-gap, 2px);
  background: var(--sq-line); border-radius: 4px; padding: var(--bg-gap, 2px);
}
.sq {
  position: relative; background: var(--sq); border-radius: 3px;
  display: flex; align-items: center; justify-content: center;
  --ts: var(--cell);
  min-width: 0; min-height: 0;
}
.sq .pl { font-weight: 800; text-align: center; line-height: 1.02; font-size: max(8px, calc(var(--cell) * .22)); letter-spacing: .01em; pointer-events: none; }
.sq .pl b { display: block; font-size: max(10px, calc(var(--cell) * .34)); font-weight: 900; }
.sq.small .pl { font-size: max(9px, calc(var(--cell) * .36)); }
.sq.TW { background: var(--tw); color: var(--tw-ink); }
.sq.DW { background: var(--dw); color: var(--dw-ink); }
.sq.TL { background: var(--tl); color: var(--tl-ink); }
.sq.DL { background: var(--dl); color: var(--dl-ink); }
.sq.ST { background: var(--st); color: var(--st-ink); }
.sq.ST .pl { font-size: calc(var(--cell) * .62); }
.sq.hover { box-shadow: inset 0 0 0 3px var(--pending); filter: brightness(1.08); }
.sq.cursor::after {
  content: ""; position: absolute; inset: 2px; border: 3px solid var(--accent); border-radius: 4px; pointer-events: none;
  animation: blink 1s steps(2) infinite;
}
.sq.cursor .arrow { position: absolute; font-size: calc(var(--cell) * .5); font-weight: 900; color: var(--accent); pointer-events: none; }
@keyframes blink { 50% { opacity: .35; } }
.sq > .tile { position: absolute; inset: 0; border-radius: 12%; z-index: 1; }
.sq > .tile.pending { box-shadow: 0 0 0 3px var(--pending), 0 3px 6px rgba(0, 0, 0, .35); cursor: grab; z-index: 2; touch-action: none; }
.sq > .tile.last::after { content: ""; position: absolute; inset: 0; border-radius: 12%; box-shadow: inset 0 0 0 max(2px, calc(var(--cell) * .07)) var(--last); pointer-events: none; }
.sq > .tile.bad { box-shadow: 0 0 0 3px var(--bad), 0 3px 6px rgba(0, 0, 0, .35); background: linear-gradient(170deg, #ffe3dc, #f6c4b8); }
.sq > .tile.pop { animation: pop .28s cubic-bezier(.2, 1.6, .4, 1); }
.sq > .tile.drop { animation: drop .42s cubic-bezier(.2, 1.3, .45, 1) both; }
.sq > .tile.glow { animation: glow 1s ease; }
@keyframes pop { from { transform: scale(.6); } to { transform: scale(1); } }
@keyframes drop { from { transform: translateY(-40%) scale(1.5); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes glow { 0% { filter: brightness(1.0); } 30% { filter: brightness(1.25) drop-shadow(0 0 8px var(--pending)); } 100% { filter: none; } }

/* ---------- Status ---------- */
#status {
  min-height: 44px; display: flex; align-items: center; justify-content: center; text-align: center;
  background: var(--panel); border: 2px solid var(--panel-edge); border-radius: 12px; padding: 6px 12px;
  font-size: 18px; font-weight: 600; color: var(--ink);
}
#status.err { color: var(--bad); }
#status.good { color: var(--good); }
#status.shake { animation: shake .4s; }
@keyframes shake { 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(3px); } }
.dots::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

/* ---------- Rack ---------- */
#rackArea { position: relative; }
#rack {
  display: flex; justify-content: center; align-items: center; gap: calc(var(--rt) * .1);
  min-height: calc(var(--rt) + 20px); padding: 10px;
  background: linear-gradient(180deg, var(--frame-hi), var(--frame)); border-radius: 14px;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, .45), 0 2px 0 rgba(255, 255, 255, .12);
  touch-action: none;
}
#rack .tile { --ts: var(--rt); width: var(--rt); height: var(--rt); flex: none; cursor: grab; transition: transform .15s ease, box-shadow .15s, opacity .15s; touch-action: none; }
#rack .tile.sel { transform: translateY(-9px); box-shadow: 0 0 0 4px var(--pending), 0 8px 14px rgba(0, 0, 0, .4); }
#rack .tile.lift { opacity: .25; }
#rack .tile.newt { animation: drop .4s cubic-bezier(.2, 1.3, .45, 1) both; }
#rack .gapper { width: var(--rt); height: var(--rt); flex: none; border-radius: 12%; border: 2px dashed rgba(255, 255, 255, .35); }
#rack.hidden-tiles .tile { visibility: hidden; }
#rack .empty-msg { color: var(--on-bg); opacity: .8; font-size: 16px; }
#handoff { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 6px; }
#handoff[hidden] { display: none; }
#handoff .btn { height: 100%; min-height: 0; }
.ghost { position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none; --ts: var(--rt); width: var(--ts); height: var(--ts); box-shadow: 0 12px 24px rgba(0, 0, 0, .45); transition: width .1s, height .1s; }

/* ---------- Actions ---------- */
#actions { display: grid; grid-template-columns: repeat(5, 1fr) 1.6fr; gap: 8px; }
.abtn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 62px; padding: 4px 2px;
  background: var(--btn); color: var(--btn-ink); border: 2px solid var(--btn-edge); border-radius: 14px;
  font-size: 15px; font-weight: 700; box-shadow: 0 3px 0 var(--btn-edge);
  transition: transform .08s, box-shadow .08s, opacity .2s;
}
.abtn svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.abtn:active { transform: translateY(3px); box-shadow: none; }
.abtn[disabled] { opacity: .4; pointer-events: none; }
.abtn.play { background: var(--accent); color: var(--accent-ink); border-color: rgba(0, 0, 0, .3); box-shadow: 0 3px 0 rgba(0, 0, 0, .3); }
.abtn.play .play-label { font-size: 22px; font-weight: 900; letter-spacing: .02em; }
.abtn.play .play-score { font-size: 17px; font-weight: 800; opacity: .95; min-height: 1em; }
.abtn.play.invalid { background: #8f8a80; }
body[data-theme="atomic"] .abtn.play .play-label { font-family: var(--title-font); font-weight: 400; font-size: 24px; }

/* ---------- Log ---------- */
#logPanel { background: var(--panel); border: 2px solid var(--panel-edge); border-radius: 14px; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.log-title { font-weight: 800; font-size: 16px; padding: 8px 14px 4px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .08em; }
#log, .log-list { list-style: none; margin: 0; padding: 0 10px 8px; overflow-y: auto; flex: 1; min-height: 0; }
.log-list { max-height: 60vh; }
.log-item { display: flex; align-items: center; gap: 10px; padding: 7px 4px; border-bottom: 1px solid rgba(0, 0, 0, .08); font-size: 17px; }
.log-item:last-child { border-bottom: 0; }
.log-item .who { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.log-item .who.p0 { background: var(--accent); }
.log-item .who.p1 { background: var(--accent-2); }
.log-item .what { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-item .what b { letter-spacing: .04em; }
.log-item .pts { font-weight: 900; font-variant-numeric: tabular-nums; }
.log-item .badge { font-size: 12px; font-weight: 800; background: var(--pending); color: #3a2a00; border-radius: 6px; padding: 2px 6px; margin-left: 6px; }
.log-empty { color: var(--ink-soft); font-size: 16px; padding: 8px 4px; }

/* ---------- Modal ---------- */
#modalBack { position: fixed; inset: 0; background: rgba(20, 12, 6, .55); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 14px; animation: fade .2s; }
#modalBack[hidden] { display: none; }
#modal {
  background: var(--panel); color: var(--ink); border: 3px solid var(--panel-edge); border-radius: 20px;
  width: min(520px, 100%); max-height: calc(100% - 10px); overflow-y: auto; padding: 20px 22px; box-shadow: 0 20px 50px rgba(0, 0, 0, .5);
  animation: rise .25s cubic-bezier(.2, 1.2, .4, 1);
}
#modal h2 { margin: 0 0 12px; font-size: 28px; text-align: center; font-family: var(--title-font); }
body[data-theme="atomic"] #modal h2 { font-weight: 400; letter-spacing: .03em; }
#modalBody { font-size: 18px; line-height: 1.45; }
#modalBody p { margin: 8px 0; }
#modalBtns { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
#modalBtns .btn { flex: 1 1 140px; }
#modal.wide-modal { width: min(640px, 100%); }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px) scale(.97); opacity: 0; } }
.menu-list { display: flex; flex-direction: column; gap: 10px; }
.menu-list .btn { width: 100%; }
.row-toggle { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid rgba(0, 0, 0, .1); font-size: 18px; font-weight: 600; }
.row-toggle .seg { min-width: 220px; }
.letter-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(54px, 1fr)); gap: 8px; }
.letter-grid button { --ts: 54px; height: 58px; border: 0; padding: 0; }
.letter-grid button .L { font-size: 30px; }
.swap-rack { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 10px 0; }
.swap-rack button { --ts: 58px; width: 58px; height: 58px; border: 0; padding: 0; transition: transform .12s; }
.swap-rack button.on { transform: translateY(-8px); box-shadow: 0 0 0 4px var(--accent), 0 8px 14px rgba(0, 0, 0, .3); }
.bag-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; margin-top: 10px; }
.bag-cell { display: flex; align-items: center; gap: 6px; }
.bag-cell .tile { --ts: 42px; width: 42px; height: 42px; flex: none; }
.bag-cell .n { font-weight: 800; font-size: 18px; }
.bag-cell.zero { opacity: .3; }
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat { background: var(--panel-2); border-radius: 12px; padding: 10px 12px; text-align: center; }
.stat .n { font-size: 30px; font-weight: 900; line-height: 1.1; }
.stat .k { font-size: 15px; color: var(--ink-soft); font-weight: 700; }
.stat.full { grid-column: 1 / -1; }
.lvl-table { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 17px; }
.lvl-table td, .lvl-table th { padding: 6px; text-align: center; border-bottom: 1px solid rgba(0, 0, 0, .1); }
.final { display: flex; gap: 12px; margin: 8px 0 12px; }
.final .fs { flex: 1; text-align: center; background: var(--panel-2); border-radius: 14px; padding: 10px; }
.final .fs.win { box-shadow: 0 0 0 3px var(--accent); }
.final .fs .n { font-size: 40px; font-weight: 900; }
.final .fs .k { font-weight: 700; color: var(--ink-soft); }
.final .fs .adj { font-size: 15px; color: var(--ink-soft); }
.legend { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 10px 0; }
.legend div { display: flex; align-items: center; gap: 8px; font-size: 16px; font-weight: 600; }
.legend span.sw { width: 40px; height: 40px; border-radius: 6px; display: flex; align-items: center; justify-content: center; font-weight: 900; font-size: 12px; text-align: center; line-height: 1; flex: none; }
.how h3 { margin: 14px 0 4px; font-size: 19px; }
.how ul { margin: 4px 0; padding-left: 22px; }
.how li { margin: 4px 0; }

/* ---------- Toast & banner & floats ---------- */
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0; z-index: 60;
  background: var(--frame); color: #fff; padding: 12px 20px; border-radius: 14px; font-size: 18px; font-weight: 700;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4); transition: opacity .25s, transform .25s; pointer-events: none; max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#banner { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; z-index: 55; pointer-events: none; }
#banner.show { display: flex; }
.banner-inner { position: relative; text-align: center; animation: bannerIn 1.8s ease forwards; }
.banner-text { position: relative; font-size: clamp(48px, 12vw, 110px); font-weight: 900; color: #fff; font-family: var(--title-font); text-shadow: 0 4px 0 rgba(0, 0, 0, .3), 0 0 30px rgba(0, 0, 0, .4); -webkit-text-stroke: 2px rgba(0, 0, 0, .25); }
.banner-sub { position: relative; font-size: clamp(22px, 5vw, 40px); font-weight: 800; color: #fff; text-shadow: 0 2px 6px rgba(0, 0, 0, .5); }
.banner-burst { position: absolute; left: 50%; top: 50%; width: 140%; aspect-ratio: 1; transform: translate(-50%, -50%);
  background: repeating-conic-gradient(var(--pending) 0 10deg, transparent 10deg 20deg); border-radius: 50%;
  -webkit-mask: radial-gradient(circle, #000 20%, transparent 70%); mask: radial-gradient(circle, #000 20%, transparent 70%); opacity: .8; animation: spinb 2s linear; }
@keyframes spinb { to { transform: translate(-50%, -50%) rotate(90deg); } }
@keyframes bannerIn { 0% { transform: scale(.3); opacity: 0; } 15% { transform: scale(1.08); opacity: 1; } 25% { transform: scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: scale(1.04); } }
.float-score {
  position: fixed; z-index: 45; pointer-events: none; transform: translate(-50%, -50%);
  background: var(--accent); color: #fff; font-weight: 900; font-size: 26px; padding: 6px 14px; border-radius: 999px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, .35); border: 3px solid #fff; animation: floatUp 1.6s ease forwards; white-space: nowrap;
}
.float-score.p1 { background: var(--accent-2); }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, -20%) scale(.6); } 15% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); } 25% { transform: translate(-50%, -50%) scale(1); } 75% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -140%); } }

/* ---------- Small screens ---------- */
#game.compact .abtn { min-height: 52px; font-size: 13px; }
#game.compact .abtn svg { width: 22px; height: 22px; }
#game.compact .abtn.play .play-label { font-size: 19px; }
#game.compact .abtn.play .play-score { font-size: 15px; }
#game.compact #status { min-height: 38px; font-size: 16px; padding: 4px 10px; }
#game.compact .pcard { min-height: 46px; padding: 4px 12px; }
#game.compact .pcard .pscore { font-size: 26px; }
#game.compact .pcard .pname { font-size: 16px; }
#game.compact .lvl { display: none; }
#game.compact .tbtn { height: 42px; font-size: 15px; padding: 0 10px; }
#game.compact #rack { padding: 6px; min-height: calc(var(--rt) + 12px); }
#game.tiny .tbtn span { display: none; }
#game.tiny .tbtn #bagCount { display: inline; }
#game.tiny .abtn span:not(.play-label):not(.play-score) { display: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001s !important; animation-iteration-count: 1 !important; transition-duration: .001s !important; }
  .sq.cursor::after { animation: none; }
}

/* ---------- Fine tuning ---------- */
.sq.small > .tile .v { font-size: max(7px, calc(var(--cell) * .27)); right: 5%; bottom: 3%; }
.sq.small > .tile .L { margin-left: -10%; }
body[data-theme="atomic"] #boardFrame { box-shadow: 0 10px 30px rgba(0, 0, 0, .45), inset 0 0 0 2px #e8b23a, inset 0 0 0 4px #1f1f22; }
body[data-theme="atomic"] #rack { box-shadow: inset 0 2px 6px rgba(0, 0, 0, .5), inset 0 0 0 2px rgba(232, 178, 58, .55); }
body[data-theme="atomic"] .pcard { border-width: 2px; }
body[data-theme="atomic"] .pcard::after { content: ""; position: absolute; left: 12px; right: 12px; bottom: 5px; height: 3px; border-radius: 3px; background: linear-gradient(90deg, #d9622b 0 33%, #e8b23a 33% 66%, #23797c 66%); opacity: .55; }
body[data-theme="atomic"] .btn.primary, body[data-theme="atomic"] .abtn.play { background: linear-gradient(180deg, #e2733c, #d0581f); }
body[data-theme="classic"] .top-title { font-style: italic; }
.banner-inner::before { content: ""; position: absolute; left: 50%; top: 50%; width: 130%; height: 170%; transform: translate(-50%, -50%); background: radial-gradient(ellipse at center, rgba(0, 0, 0, .5) 0%, rgba(0, 0, 0, .25) 45%, transparent 70%); pointer-events: none; }
.banner-text { -webkit-text-stroke: 0; letter-spacing: .02em; }
body[data-theme="classic"] .banner-text { font-family: var(--tile-font); }
@media (max-height: 520px) {
  #modal { padding: 12px 16px; }
  #modal h2 { font-size: 22px; margin-bottom: 8px; }
  .menu-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .menu-list .btn { min-height: 48px; }
  .row-toggle { padding: 6px 0; }
  #modalBtns { margin-top: 10px; }
  .letter-grid { grid-template-columns: repeat(9, 1fr); gap: 5px; }
  .letter-grid button { height: 44px; }
}
