/* Math Scrabble — English Room Games */

:root {
  --bg: #1d2452;
  --bg-2: #252d63;
  --ink: #fff8e7;
  --muted: #b9c0ea;
  --muted-2: #8790c4;
  --line: rgba(255, 255, 255, .14);
  --gold: #ffc93c;
  --gold-dark: #c99a1c;
  --red: #ef6461;
  --red-dark: #b8393a;
  --yellow: #ffc93c;
  --yellow-dark: #c99a1c;
  --blue: #2f6fd8;
  --blue-dark: #1d4ea3;
  --display: 'Fredoka', 'Nunito', sans-serif;
  --body: 'Nunito', sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--body); color: var(--ink); background-color: var(--bg);
  background-image:
    linear-gradient(45deg, rgba(255,255,255,.03) 25%, transparent 25%, transparent 75%, rgba(255,255,255,.03) 75%),
    linear-gradient(45deg, rgba(255,255,255,.03) 25%, transparent 25%, transparent 75%, rgba(255,255,255,.03) 75%);
  background-size: 64px 64px; background-position: 0 0, 32px 32px;
  min-height: 100dvh;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 14px; padding-top: calc(8px + env(safe-area-inset-top, 0px)); border-bottom: 1px solid var(--line); background: rgba(20, 26, 64, .8); }
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand h1 { margin: 0; font: 700 26px var(--display); white-space: nowrap; }
.brand .site { font-size: 11.5px; font-weight: 800; letter-spacing: .1em; text-transform: uppercase; color: var(--muted-2); text-decoration: none; white-space: nowrap; }
.brand .site:hover { color: var(--gold); }
.spacer { flex: 1; }
.iconbtn { width: 38px; height: 38px; flex-shrink: 0; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 10px; background: rgba(255,255,255,.05); color: var(--muted); cursor: pointer; text-decoration: none; }
.iconbtn:hover { color: var(--ink); border-color: var(--gold); }
.iconbtn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.iconbtn[aria-pressed="false"] .wave { display: none; }

.controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: rgba(255,255,255,.07); border-radius: 12px; padding: 10px 16px; font-weight: 800; font-size: 1rem; cursor: pointer; min-height: 44px; }
.btn:hover:not(:disabled) { background: rgba(255,255,255,.13); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--gold); color: var(--bg); border-color: transparent; box-shadow: 0 4px 0 var(--gold-dark); font-family: var(--display); font-weight: 600; font-size: 1.12rem; }
.btn.primary:hover { background: #ffd35e; }
.btn.primary:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--gold-dark); }
.btn.wide { width: 100%; margin-top: 8px; }
.tip { margin: 0; color: var(--muted-2); font-size: .9rem; text-align: center; }

/* overlays */
.overlay { position: fixed; inset: 0; z-index: 100; background: rgba(10, 14, 40, .75); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; padding: 14px; }
.card { position: relative; width: min(480px, 100%); max-height: calc(100dvh - 28px); overflow: auto; background: var(--bg-2); border: 1px solid var(--line); border-radius: 20px; padding: 22px; box-shadow: 0 30px 70px rgba(0,0,0,.5); }
.card h2 { margin: 0 0 6px; font: 700 2rem var(--display); }
.card h3 { margin: 16px 0 6px; font: 600 1.15rem var(--display); color: var(--gold); }
.card p { line-height: 1.5; margin: 0 0 12px; }
.card .big { font-size: 1.08rem; }
.kicker { font: 600 .8rem var(--display); letter-spacing: .12em; text-transform: uppercase; color: var(--gold); }
.kicker a { color: inherit; text-decoration: none; }
.close { position: absolute; top: 10px; right: 12px; width: 36px; height: 36px; border: 0; border-radius: 50%; background: rgba(255,255,255,.08); font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); }
.field { margin-bottom: 14px; }
.label { display: flex; align-items: center; gap: 6px; font: 600 .8rem var(--display); letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.dot.red { background: var(--red); } .dot.yellow { background: var(--yellow); }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice button { text-align: left; padding: 11px 14px; border-radius: 14px; cursor: pointer; border: 2px solid var(--line); background: rgba(0,0,0,.25); }
.choice button b { display: block; font-size: 1.05rem; }
.choice button span { color: var(--muted); font-size: .88rem; }
.choice button[aria-pressed="true"] { border-color: var(--gold); background: rgba(255, 201, 60, .1); }
.names { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.names input { width: 100%; font: 700 1rem var(--body); color: var(--ink); background: rgba(0,0,0,.3); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.names input:focus { outline: none; border-color: var(--gold); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; background: rgba(0,0,0,.25); padding: 4px; border-radius: 12px; border: 1px solid var(--line); }
.seg button { border: 0; background: transparent; border-radius: 9px; padding: 9px 6px; font-weight: 800; cursor: pointer; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--gold); color: var(--bg); }
.steps { padding-left: 20px; margin: 4px 0; line-height: 1.55; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 150; overflow: hidden; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 14px; border-radius: 2px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (max-width: 520px) {
  .brand .site { display: none; }
  .brand h1 { font-size: 22px; }
  .names { grid-template-columns: 1fr; }
  .card { padding: 18px 16px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ---------- Math Scrabble ---------- */
:root { --p0: #5b8def; --p1: #ef6461; --tile: #f6e7c1; --tile-dark: #c9ae6e; --tile-ink: #2a2140; --N: 13; }
.layout {
  --cell: max(26px, min(46px, calc((100vw - 28px) / var(--N)), calc((100dvh - 320px) / var(--N))));
  display: flex; justify-content: center; align-items: flex-start; gap: 20px; padding: 12px 12px 22px;
}
.play-area { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.players { width: calc(var(--cell) * var(--N) + 12px); max-width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.player { display: flex; align-items: center; gap: 10px; padding: 7px 14px; border-radius: 999px; border: 2px solid var(--line); background: rgba(0,0,0,.2); min-width: 0; transition: border-color .2s, box-shadow .2s; }
.player.p1 { justify-content: flex-end; }
.player .name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.player .score { font: 700 1.35rem var(--display); color: var(--gold); }
.player.p0 .score { margin-left: auto; } .player.p1 .score { margin-right: auto; }
.player.turn.p0 { border-color: var(--p0); box-shadow: 0 0 0 4px rgba(91,141,239,.22); }
.player.turn.p1 { border-color: var(--p1); box-shadow: 0 0 0 4px rgba(239,100,97,.22); }

.board-wrap { padding: 6px; border-radius: 14px; background: #14193d; box-shadow: 0 16px 34px rgba(0,0,0,.4); }
.board { display: grid; grid-template-columns: repeat(var(--N), var(--cell)); grid-template-rows: repeat(var(--N), var(--cell)); gap: 0; user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.sq { position: relative; border: 1px solid #14193d; background: #2c3570; display: grid; place-items: center; cursor: pointer; }
.sq .lbl { font: 800 calc(var(--cell) * .2)/1 var(--body); text-align: center; color: rgba(255,255,255,.85); pointer-events: none; letter-spacing: .02em; }
.sq.dt { background: #3a8fd0; } .sq.tt { background: #1f5aa6; }
.sq.de { background: #d7678f; } .sq.te { background: #c2364a; }
.sq.star { background: #d7678f; }
.sq.star .lbl { font-size: calc(var(--cell) * .55); color: #fff; }
.sq.hint { animation: hint .8s ease-in-out 4; }
@keyframes hint { 50% { box-shadow: inset 0 0 0 4px #6ee7a8; background: #2f7a5a; } }
.sq.drop { box-shadow: inset 0 0 0 3px var(--gold); }
.sq:hover:not(.has) { filter: brightness(1.15); }

.tile {
  position: relative; display: grid; place-items: center; width: 100%; height: 100%;
  background: linear-gradient(180deg, #fbf0d3, var(--tile)); color: var(--tile-ink); border-radius: calc(var(--cell) * .14);
  box-shadow: inset 0 -3px 0 var(--tile-dark), 0 1px 2px rgba(0,0,0,.4);
  font: 700 calc(var(--cell) * .58)/1 var(--display);
}
.tile .pts { position: absolute; right: 9%; bottom: 7%; font: 800 calc(var(--cell) * .2)/1 var(--body); opacity: .75; }
.tile.op { color: #8a2d3b; }
.tile.eqs { color: #1d4ea3; }
.sq .tile { width: calc(100% - 2px); height: calc(100% - 2px); }
.tile.pending { background: linear-gradient(180deg, #fff6c9, #ffe27a); box-shadow: inset 0 -3px 0 #d7a91e, 0 0 0 2px var(--gold); }
.tile.pending.bad { box-shadow: inset 0 -3px 0 #d7a91e, 0 0 0 2px var(--red); }
.tile.fresh { animation: fresh 1.6s ease-out; }
@keyframes fresh { 0% { background: #a7f3c6; } 100% { background: linear-gradient(180deg, #fbf0d3, var(--tile)); } }

.preview { min-height: 1.6em; font: 600 1.1rem var(--display); text-align: center; color: var(--muted); max-width: calc(var(--cell) * var(--N) + 12px); }
.preview .ok { color: #6ee7a8; } .preview .no { color: var(--red); }
.preview .words { display: block; font: 700 .92rem var(--body); color: var(--muted); font-style: italic; }

.rack-row { display: flex; justify-content: center; }
.rack { --rt: min(52px, calc((100vw - 60px) / 8)); display: flex; gap: 6px; padding: 8px 10px; min-height: calc(var(--rt) + 16px); border-radius: 14px; background: #6b4a2a; box-shadow: inset 0 -4px 0 rgba(0,0,0,.3), 0 8px 18px rgba(0,0,0,.35); touch-action: none; }
.rack .tile { --cell: var(--rt); width: var(--rt); height: var(--rt); cursor: grab; transition: transform .12s; }
.rack .tile.sel { transform: translateY(-8px); box-shadow: inset 0 -3px 0 var(--tile-dark), 0 0 0 3px var(--gold), 0 6px 10px rgba(0,0,0,.4); }
.rack .tile.used { visibility: hidden; }
.rack .tile.mark { background: #ffd0cf; box-shadow: inset 0 -3px 0 #d98a88, 0 0 0 3px var(--red); }
.rack.locked .tile { cursor: default; }
.rack .tile.hidden-face { color: transparent; background: #c9ae6e; }
.rack .tile.hidden-face .pts { display: none; }
.drag-ghost { position: fixed; z-index: 300; pointer-events: none; --cell: 52px; width: 52px; height: 52px; transform: translate(-50%, -60%) scale(1.1); }
.swap-rack { justify-content: center; margin: 10px auto 14px; width: max-content; max-width: 100%; flex-wrap: wrap; }

.controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.controls .btn { padding: 9px 14px; }

.side { width: 280px; display: flex; flex-direction: column; gap: 12px; position: sticky; top: 12px; }
.side-card { background: rgba(0,0,0,.22); border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px; }
.side-head { display: flex; justify-content: space-between; align-items: center; font: 600 .8rem var(--display); letter-spacing: .1em; text-transform: uppercase; color: var(--gold); margin-bottom: 8px; }
.bag { color: var(--muted); letter-spacing: 0; text-transform: none; font-family: var(--body); font-weight: 800; }
.log { list-style: none; margin: 0; padding: 0; max-height: 300px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.log li { padding: 7px 10px; border-radius: 10px; background: rgba(255,255,255,.05); border-left: 4px solid var(--line); font-size: .92rem; }
.log li.p0 { border-left-color: var(--p0); } .log li.p1 { border-left-color: var(--p1); }
.log .who { font-weight: 800; }
.log .pts { float: right; font: 700 1rem var(--display); color: var(--gold); }
.log .eqn { display: block; font: 600 1.1rem var(--display); margin-top: 2px; }
.log .words { display: block; color: var(--muted); font-style: italic; }
.log .empty { background: none; border: 0; color: var(--muted-2); padding: 4px 0; }
.values { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; margin-bottom: 10px; }
.values span { --cell: 40px; display: block; aspect-ratio: 1; }
.values .tile { font-size: 22px; }
.values .tile .pts { font-size: 9px; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-size: .82rem; font-weight: 800; color: var(--muted); }
.legend .sw { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: -2px; margin-right: 6px; }
.sw.dt { background: #3a8fd0; } .sw.tt { background: #1f5aa6; } .sw.de { background: #d7678f; } .sw.te { background: #c2364a; }
.eq { font-family: var(--display); font-weight: 600; color: var(--gold); white-space: nowrap; }
.card.center { text-align: center; }

@media (max-width: 980px) {
  .layout { flex-direction: column; align-items: center; }
  .side { width: min(520px, 100%); position: static; }
  .keyinfo .values { grid-template-columns: repeat(10, 1fr); }
  .values span { --cell: 32px; }
  .values .tile { font-size: 18px; }
}
@media (max-width: 520px) {
  .layout { padding: 8px 6px 18px; --cell: min(46px, calc((100vw - 20px) / var(--N))); }
  .controls .btn { padding: 8px 11px; font-size: .92rem; min-height: 40px; }
  .player { padding: 5px 10px; }
  .rack { gap: 4px; padding: 6px; }
  .sq:not(.star) .lbl { display: none; }
}
