:root {
  --navy: #1c2a6b; --navy2: #2b3a8c; --ink: #17204d; --paper: #fffdf5; --gold: #ffd43b; --teal: #20c997; --red: #ff6b6b;
  --muted: #6b7399; --line: #dfe3f5; --radius: 16px;
  font-family: ui-rounded, "Nunito", "Segoe UI", system-ui, sans-serif; font-size: 16px; color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #9bdcff; overscroll-behavior: none; }
[hidden] { display: none !important; }
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
h1, h2, h3 { margin: 0 0 .4em; line-height: 1.15; }
h1 { font-size: 1.9rem; } h2 { font-size: 1.35rem; } h3 { font-size: 1.05rem; }
p { margin: .4em 0; }
.muted { color: var(--muted); } .small { font-size: .85rem; } .err { color: #c92a2a; font-weight: 700; min-height: 1.2em; }
.grow { flex: 1; } .row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; } .between { justify-content: space-between; }

.btn { font: inherit; font-weight: 700; border: 0; border-radius: 999px; padding: .55em 1em; background: #fff; color: var(--ink); cursor: pointer;
  box-shadow: 0 3px 0 rgba(23, 32, 77, .18); transition: transform .08s; }
.btn:hover { transform: translateY(-1px); } .btn:active { transform: translateY(2px); box-shadow: none; }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--gold); } .btn.good { background: var(--teal); color: #fff; } .btn.danger { background: var(--red); color: #fff; }
.btn.tiny { padding: .25em .6em; font-size: .85rem; } .btn.on { background: var(--teal); color: #fff; } .btn.off { background: #ffe3e3; }
.link { font: inherit; background: none; border: 0; color: var(--navy2); text-decoration: underline; cursor: pointer; padding: 0; }
:focus-visible { outline: 4px solid #ff922b; outline-offset: 2px; }
.pill { display: inline-block; color: var(--ink); background: rgba(255, 255, 255, .9); border-radius: 999px; padding: .3em .8em; font-weight: 700; font-size: .9rem; }
.pill.gold { background: var(--gold); } .pill.big { font-size: 1.15rem; padding: .45em 1em; }

input, select, textarea { font: inherit; width: 100%; padding: .6em .8em; border: 2px solid var(--line); border-radius: 12px; background: #fff; color: var(--ink); }
textarea { min-height: 8em; resize: vertical; }
label { display: block; font-weight: 700; margin: .7em 0; } label input, label select, label textarea { margin-top: .3em; font-weight: 400; }

.card { background: var(--paper); border-radius: 24px; padding: 1.6rem; width: min(26rem, 100%); box-shadow: 0 16px 40px rgba(23, 32, 77, .3); max-height: 94vh; overflow: auto; }
.screen { position: fixed; inset: 0; display: grid; place-items: center; padding: 1rem; background: linear-gradient(160deg, #4dabf7, #1c2a6b); z-index: 30; }
.screen .btn.primary { width: 100%; margin-top: .4rem; font-size: 1.1rem; } .links { text-align: center; }
.setup-page { overflow: auto; display: grid; place-items: start center; padding: 2rem 1rem; background: linear-gradient(160deg, #4dabf7, #1c2a6b); height: auto; min-height: 100%; }
.setup { width: min(32rem, 100%); max-height: none; } .setup h2 { margin-top: 1.2em; } code { background: #eef; padding: .1em .35em; border-radius: 6px; }

#topbar { position: fixed; top: 0; left: 0; right: 0; display: flex; gap: .4rem; align-items: center; padding: .5rem .7rem; z-index: 21; flex-wrap: wrap;
  background: linear-gradient(rgba(28, 42, 107, .55), transparent); color: #fff; }
#topbar strong { font-size: 1.15rem; text-shadow: 0 2px 0 rgba(0, 0, 0, .25); }
#help { position: fixed; bottom: .3rem; left: 50%; transform: translateX(-50%); margin: 0; font-size: .8rem; color: #fff; text-shadow: 0 1px 2px #000a; white-space: nowrap; z-index: 5; pointer-events: none; }
#prompt { position: fixed; left: 50%; bottom: 22%; transform: translateX(-50%); background: var(--gold); padding: .7em 1.3em; border-radius: 999px; font-weight: 800; font-size: 1.15rem; z-index: 6; box-shadow: 0 6px 0 rgba(23, 32, 77, .2); cursor: pointer; border: 0; }

#chat { position: fixed; left: .6rem; bottom: 2rem; width: min(21rem, calc(100vw - 1.2rem)); z-index: 22; background: rgba(23, 32, 77, .78); border-radius: var(--radius); padding: .5rem; color: #fff; backdrop-filter: blur(4px); }
#chat.closed { display: none; }
#chat-tabs { display: flex; gap: .3rem; margin-bottom: .4rem; flex-wrap: wrap; } #chat-tabs .btn { background: rgba(255, 255, 255, .25); color: #fff; box-shadow: none; } #chat-tabs .btn.sel { background: var(--gold); color: var(--ink); }
#chat-tabs .badge, #btn-chat .badge { background: var(--red); color: #fff; border-radius: 99px; padding: 0 .45em; margin-left: .35em; font-size: .8em; }
#dm-bar { display: flex; align-items: center; gap: .5rem; margin: .4rem 0 0; font-size: .9rem; } #dm-bar select { margin: 0; padding: .3em .5em; border: 0; }
#chat-log .msg b.who { cursor: pointer; } #chat-log .msg .tag { color: #c5d0ff; }
#chat-log { height: 9.5rem; overflow-y: auto; font-size: .92rem; overflow-wrap: anywhere; }
#chat-log .msg { padding: .12em .2em; } #chat-log .msg b { color: var(--gold); } #chat-log .msg.sys { color: #c5d0ff; font-style: italic; }
#chat-log .msg .x { margin-left: .4em; background: none; border: 0; color: #ffa8a8; cursor: pointer; font-weight: 800; }
#chat-form { display: flex; gap: .4rem; margin-top: .4rem; } #chat-form input { padding: .4em .7em; border: 0; } #chat-form .btn { padding: .4em .9em; }

#touch { position: fixed; inset: 0; z-index: 8; pointer-events: none; }
#joy { position: absolute; left: calc(1.2rem + env(safe-area-inset-left)); bottom: calc(1.6rem + env(safe-area-inset-bottom)); width: 120px; height: 120px; border-radius: 50%; background: rgba(255, 255, 255, .35); pointer-events: auto; touch-action: none; display: grid; place-items: center; }
#knob { width: 52px; height: 52px; border-radius: 50%; background: #fff; box-shadow: 0 3px 0 rgba(23, 32, 77, .25); }
.tbtn { position: absolute; right: calc(1.2rem + env(safe-area-inset-right)); bottom: calc(1.6rem + env(safe-area-inset-bottom)); width: 92px; height: 92px; border-radius: 50%; border: 0; font: inherit; font-weight: 800; background: var(--gold); pointer-events: auto; touch-action: none; box-shadow: 0 5px 0 rgba(23, 32, 77, .25); }
#t-act { bottom: calc(8.4rem + env(safe-area-inset-bottom)); background: var(--teal); color: #fff; }

#toasts { position: fixed; top: calc(var(--topbar-h, 3.4rem) + .6rem); left: 50%; transform: translateX(-50%); z-index: 60; display: grid; gap: .4rem; justify-items: center; pointer-events: none; }
.toast { background: var(--ink); color: #fff; padding: .6em 1.1em; border-radius: 999px; font-weight: 700; box-shadow: 0 6px 20px rgba(0, 0, 0, .3); animation: pop .2s; }
@keyframes pop { from { transform: scale(.8); opacity: 0; } }

.modal-back { position: fixed; inset: 0; background: rgba(23, 32, 77, .55); display: grid; place-items: center; padding: 1rem; z-index: 40; }
.modal { background: var(--paper); border-radius: 24px; padding: 1.2rem 1.3rem; width: min(40rem, 100%); max-height: 88vh; overflow: auto; box-shadow: 0 16px 40px rgba(0, 0, 0, .35); animation: pop .15s; }
.modal.wide { width: min(56rem, 100%); }
.modal-head { display: flex; align-items: center; gap: .6rem; margin-bottom: .7rem; } .modal-head h2 { margin: 0; flex: 1; }
.list { display: grid; gap: .4rem; }
.item { display: flex; align-items: center; gap: .6rem; background: #fff; border: 2px solid var(--line); border-radius: 14px; padding: .5rem .7rem; flex-wrap: wrap; }
.item .name { font-weight: 800; } .item .grow { min-width: 6rem; }
.dot { width: 1.1rem; height: 1.1rem; border-radius: 50%; display: inline-block; flex: none; border: 2px solid rgba(0, 0, 0, .15); }
.games { display: grid; grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr)); gap: .7rem; }
.game-card { background: #fff; border: 2px solid var(--line); border-radius: 18px; padding: .9rem; display: grid; gap: .4rem; align-content: start; }
.game-card .icon { font-size: 2.2rem; } .game-card .diffs { display: flex; gap: .3rem; flex-wrap: wrap; }
.swatches { display: flex; gap: .5rem; flex-wrap: wrap; } .swatch { width: 2.6rem; height: 2.6rem; border-radius: 50%; border: 4px solid #fff; box-shadow: 0 0 0 2px var(--line); cursor: pointer; }
.swatch.sel { box-shadow: 0 0 0 4px var(--ink); }
.tabs { display: flex; gap: .4rem; margin-bottom: .8rem; flex-wrap: wrap; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; } td, th { text-align: left; padding: .35em .4em; border-bottom: 1px solid var(--line); }

#game-root { position: fixed; inset: 0; z-index: 20; display: grid; grid-template-rows: auto 1fr; background: linear-gradient(160deg, #4dabf7, #1c2a6b); overflow: auto; }
#game-root.see { background: none; pointer-events: none; overflow: visible; } #game-root.see > * { pointer-events: auto; }
#game-root.see .room-main { pointer-events: none; }
.room-head { display: flex; gap: .5rem; align-items: center; padding: calc(var(--topbar-h, 3.4rem) + .3rem) .8rem .6rem; color: #fff; flex-wrap: wrap; }
.room-head h2 { margin: 0; text-shadow: 0 2px 0 rgba(0, 0, 0, .25); }
.room-main { display: grid; grid-template-columns: 1fr minmax(11rem, 15rem); gap: 1rem; padding: 0 1rem 13rem; align-items: start; max-width: 70rem; width: 100%; margin: 0 auto; }
.stage { background: var(--paper); border-radius: 24px; padding: 1.2rem; min-height: 14rem; }
#game-root.see .stage { background: none; padding: 0; min-height: 0; }
.scores { background: rgba(255, 255, 255, .92); border-radius: 18px; padding: .8rem; pointer-events: auto; }
.scores .item { padding: .3rem .5rem; border-width: 1px; } .scores .away { opacity: .45; }
@media (max-width: 720px) { .room-main { grid-template-columns: 1fr; } #help { display: none; } #topbar .btn { padding: .45em .7em; font-size: .9rem; } }

.timebar { height: 12px; background: var(--line); border-radius: 99px; overflow: hidden; margin: .7rem 0; } .timebar i { display: block; height: 100%; width: 100%; background: var(--teal); transition: width .1s linear; }
.question { font-size: 1.5rem; margin: .6em 0; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; } .choice { border-radius: 16px; padding: 1em; font-size: 1.1rem; background: #eef2ff; }
.choice.right { background: var(--teal); color: #fff; opacity: 1; } .choice.wrong { background: var(--red); color: #fff; opacity: 1; }
@media (max-width: 520px) { .choices { grid-template-columns: 1fr; } }

.memory-grid { display: grid; gap: .5rem; margin-top: .8rem; max-width: 34rem; margin-inline: auto; }
.card-tile { aspect-ratio: 1; border: 0; border-radius: 14px; background: var(--navy2); font-size: clamp(1.4rem, 5vw, 2.4rem); cursor: pointer; box-shadow: 0 4px 0 rgba(23, 32, 77, .3); }
.card-tile span { visibility: hidden; } .card-tile.up { background: #fff; } .card-tile.up span { visibility: visible; } .card-tile.done { background: #c3fae8; box-shadow: none; }

.four { display: grid; gap: .5rem; justify-items: center; }
.four-drops, .four-board { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; width: min(100%, 28rem); }
.four-board { background: var(--navy2); padding: 8px; border-radius: 16px; } .drop { padding: .3em 0; }
.cell { aspect-ratio: 1; border-radius: 50%; background: #e7ecff; } .cell.red { background: var(--red); } .cell.yellow { background: var(--gold); }
.cell.win { box-shadow: 0 0 0 4px #fff inset, 0 0 12px #fff; }
.starrun-hud { display: flex; gap: .6rem; justify-content: center; pointer-events: none; }
.result-rank { font-size: 1.1rem; } .result-rank .item.me { border-color: var(--gold); background: #fff9db; }

body.in-room #site-name, body.in-room #online-count, body.in-room #btn-games { display: none; }
body.in-room #topbar { background: none; pointer-events: none; } body.in-room #topbar > * { pointer-events: auto; }
body.in-room #chat { left: auto; right: .6rem; width: min(15rem, calc(100vw - 1.2rem)); }
body.in-room #chat-log { height: 7rem; }
.expr { font-size: clamp(1.8rem, 6vw, 2.8rem); font-weight: 800; text-align: center; background: #eef2ff; border-radius: 16px; padding: .5em; margin: .2em 0 .6em; letter-spacing: .03em; }
.guess { display: grid; gap: .3rem; } .guess .question { text-align: center; font-size: 1.3rem; margin: .2em 0; }
.guess-canvas { width: min(100%, 300px); aspect-ratio: 1; margin: 0 auto .6rem; border-radius: 16px; border: 4px solid var(--navy2); background: #fff; display: block; }
.slider { display: grid; grid-template-columns: 11rem 1fr 4rem; gap: .6rem; align-items: center; font-weight: 700; margin: .5em 0; }
.slider input { padding: 0; border: 0; margin: 0; accent-color: var(--navy2); } .slider .pill { text-align: center; background: #eef2ff; }
@media (max-width: 520px) { .slider { grid-template-columns: 1fr 4rem; } .slider span:first-child { grid-column: 1 / -1; } }
#topbar select { width: auto; padding: .45em .6em; border: 0; border-radius: 999px; font-weight: 700; box-shadow: 0 3px 0 rgba(23, 32, 77, .18); }
/* Phones and tablets: chat drops down from the top bar, so the bottom corners stay free for the joystick and buttons. */
@media (pointer: coarse) {
  #chat, body.in-room #chat { left: calc(.6rem + env(safe-area-inset-left)); right: auto; top: calc(var(--topbar-h, 3.4rem) + .3rem); bottom: auto; width: min(21rem, calc(100vw - 1.2rem)); }
  #chat-log, body.in-room #chat-log { height: min(9.5rem, 28vh); }
  #help { display: none; }
}
/* Phones held sideways: less height, so make the controls a little smaller. */
@media (max-height: 480px) {
  #joy { width: 104px; height: 104px; } #knob { width: 46px; height: 46px; }
  .tbtn { width: 76px; height: 76px; } #t-act { bottom: calc(7rem + env(safe-area-inset-bottom)); }
  #topbar { gap: .3rem; padding: .35rem .5rem; } #topbar .btn, #topbar select { padding: .35em .6em; font-size: .85rem; }
  #prompt { bottom: 12%; font-size: 1rem; }
  #chat-log, body.in-room #chat-log { height: 4.4rem; }
}
