:root {
  --bg: #0e1420; --bg2: #141d2e; --panel: #172136; --panel2: #1f2c47; --line: rgba(255,255,255,.1);
  --ink: #eef1f7; --muted: #8e9ab0; --gold: #e9b949; --teal: #38c2b0; --danger: #e5646b;
  --ds: min(13.5vw, 66px);
  --nr: 18; --rh: clamp(22px, calc((100dvh - 262px) / var(--nr)), 34px);
}
@supports not (height: 100dvh) { :root { --rh: clamp(22px, calc((100vh - 262px) / var(--nr)), 34px); } }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 30%, #1a2742 0%, var(--bg) 70%) fixed;
  color: var(--ink); font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; user-select: none; -webkit-user-select: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#app { height: 100%; display: flex; flex-direction: column; align-items: center; }

/* header */
#top { width: 100%; max-width: 560px; display: flex; align-items: center; gap: 10px; padding: 8px 12px; }
.brand { font-weight: 800; letter-spacing: .02em; font-size: 18px; }
.brand span, h1 span { color: var(--gold); }
.status { flex: 1; font-size: 0; min-height: 0; }   /* the status text is for screen readers; the arrow and the buttons show the state */
.tools { display: flex; gap: 6px; }
button.icon { width: 36px; height: 36px; padding: 0; border-radius: 10px; font-size: 18px; line-height: 1; }
button.icon.off { opacity: .45; }

/* board */
#board { width: 100%; max-width: 560px; padding: 0 12px 8px; display: flex; flex-direction: column; align-items: center; gap: 8px; min-height: 0; }
.tray { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 100%; }
.dice { display: flex; gap: calc(var(--ds) * .16); justify-content: center; min-height: calc(var(--ds) * 1.25); align-items: flex-end; }
.trayinfo { font-size: 13px; color: var(--muted); min-height: 18px; text-align: center; }
.trayinfo b { color: var(--ink); }

/* a die: pips drawn with a 3x3 grid */
.die {
  width: var(--ds); height: var(--ds); border-radius: calc(var(--ds) * .2); position: relative; flex: none;
  background: linear-gradient(145deg, #ffffff, #d9dde7); box-shadow: 0 3px 0 #9aa3b6, 0 6px 10px rgba(0,0,0,.5);
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: calc(var(--ds) * .13); gap: calc(var(--ds) * .02);
  transition: transform .12s ease, box-shadow .12s ease; cursor: default; touch-action: manipulation;
}
.die i { border-radius: 50%; background: transparent; margin: 8%; }
.die i.on { background: radial-gradient(circle at 35% 30%, #3a4256, #10141f); }
.die.fresh:not(.rollingnow) { cursor: default; }
.die.can { cursor: pointer; }
.die.held { transform: translateY(calc(var(--ds) * -.22)); box-shadow: 0 0 0 3px var(--gold), 0 3px 0 #9aa3b6, 0 10px 14px rgba(0,0,0,.55); background: linear-gradient(145deg, #fff6d8, #eed48a); }
.die.blank { background: rgba(255,255,255,.06); box-shadow: none; }
.die.rollingnow { animation: tumble .18s linear infinite; }
@keyframes tumble { 0% { transform: rotate(-9deg) translateY(-3px); } 50% { transform: rotate(8deg) translateY(2px); } 100% { transform: rotate(-9deg) translateY(-3px); } }
@media (prefers-reduced-motion: reduce) { .die.rollingnow { animation: none; } }

#actions { display: flex; gap: 8px; width: 100%; justify-content: center; min-height: 44px; }
#actions button { min-width: 150px; }
#btnScore { background: var(--teal); color: #04201c; border-color: var(--teal); font-weight: 700; }

/* scorecard */
.card-wrap { width: 100%; max-width: 440px; }
.sc { display: grid; width: 100%; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.sc > div { height: var(--rh); display: flex; align-items: center; padding: 0 8px; border-bottom: 1px solid rgba(255,255,255,.06); font-size: clamp(12px, calc(var(--rh) * .46), 15px); white-space: nowrap; min-width: 0; }
.sc .lab { color: var(--ink); overflow: hidden; text-overflow: ellipsis; }
.sc .lab small { color: var(--muted); margin-left: 6px; font-size: .82em; }
.sc .head { background: var(--panel2); font-weight: 700; justify-content: center; position: relative; overflow: hidden; text-overflow: ellipsis; }
.sc .head.me { color: var(--gold); }
.sc .head .nm { overflow: hidden; text-overflow: ellipsis; }
.sc .val { justify-content: center; font-variant-numeric: tabular-nums; border-left: 1px solid rgba(255,255,255,.06); }
.sc .val.filled { font-weight: 600; }
.sc .val.zero { color: var(--muted); }
.sc .val.pot { color: var(--teal); opacity: .75; font-weight: 600; }
.sc .val.pot.mine { cursor: pointer; background: rgba(56,194,176,.1); opacity: 1; }
.sc .val.pot.mine.sel { outline: 2px solid var(--gold); outline-offset: -2px; background: rgba(233,185,73,.22); color: var(--gold); }
.sc .val.pot.best { box-shadow: inset 0 0 0 1px rgba(56,194,176,.5); }
.sc .sum { background: rgba(255,255,255,.04); color: var(--muted); }
.sc .sum.val { color: var(--ink); }
.sc .total { background: var(--panel2); font-weight: 800; color: var(--ink); }
.sc .total.me { color: var(--gold); }
.sc .total.val { font-size: 1.1em; }
.sc .gap { grid-column: 1 / -1; height: 3px; padding: 0; background: rgba(233,185,73,.35); }
.sc .lastrow { border-bottom: 0; }
.turnarrow { width: 0; height: 0; border-left: 9px solid var(--gold); border-top: 6px solid transparent; border-bottom: 6px solid transparent; margin-right: 5px; flex: none; filter: drop-shadow(0 0 4px rgba(233,185,73,.7)); }
.sc .val.flash { animation: flash .9s ease-out; }
@keyframes flash { 0% { background: rgba(233,185,73,.7); transform: scale(1.15); } 100% { background: transparent; transform: none; } }

/* buttons & inputs */
button { font: inherit; color: var(--ink); background: var(--panel2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; cursor: pointer; }
button:hover { border-color: rgba(255,255,255,.3); }
button.primary { background: var(--gold); color: #1b1405; border-color: var(--gold); font-weight: 700; }
button:disabled { opacity: .5; cursor: default; }
input[type=text] { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; user-select: text; -webkit-user-select: text; }
input[type=text]:focus { outline: 2px solid var(--teal); outline-offset: 1px; }

/* overlays */
.overlay { position: fixed; inset: 0; z-index: 40; background: rgba(6,9,16,.78); display: flex; align-items: center; justify-content: center; padding: 16px; overflow-y: auto; }
.overlay[hidden], #toast[hidden], button[hidden], .note[hidden], #btnInstall[hidden] { display: none; }
.dialog { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 22px 20px; display: flex; flex-direction: column; gap: 10px; box-shadow: 0 20px 60px rgba(0,0,0,.5); max-height: 100%; overflow-y: auto; }
.dialog.wide { max-width: 560px; }
.dialog h1 { margin: 0; font-size: 32px; letter-spacing: .01em; }
.dialog h2 { margin: 0; font-size: 22px; }
.dialog h3 { margin: 6px 0 0; font-size: 15px; color: var(--gold); }
.dialog label { font-size: 13px; color: var(--muted); }
.tag { margin: 0 0 4px; color: var(--muted); font-size: 14px; }
.note { padding: 8px 12px; border-radius: 10px; background: rgba(233,185,73,.12); color: var(--gold); font-size: 14px; }
.chips { display: flex; gap: 8px; }
.chips label { flex: 1; color: var(--ink); }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span { display: block; text-align: center; padding: 9px 4px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg2); cursor: pointer; font-size: 14px; }
.chips input:checked + span { border-color: var(--gold); color: var(--gold); background: rgba(233,185,73,.1); }
.chips input:focus-visible + span { outline: 2px solid var(--teal); }
.joinrow, .row { display: flex; gap: 8px; }
.joinrow input { text-transform: uppercase; letter-spacing: .15em; }
.code { font-size: 20px; letter-spacing: .2em; color: var(--gold); }
button small { opacity: .7; }
#newGameBlock { display: flex; flex-direction: column; gap: 10px; }
#newGameBlock[hidden] { display: none; }

.rules p, .rules ul { margin: 4px 0 0; font-size: 14px; color: var(--ink); }
.rules ul { padding-left: 18px; }
.rules li { margin-bottom: 4px; }
.rules button { margin-top: 10px; }

#toast { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 70; max-width: min(92vw, 420px); background: #2a1519; color: #ffd9dc; border: 1px solid var(--danger); border-radius: 12px; padding: 10px 16px; font-size: 14px; text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.5); }
#toast.info { background: var(--panel2); color: var(--ink); border-color: var(--line); }

/* result: a banner at the bottom, the finished board stays visible; a win gets a golden glow (plus confetti from game.js) */
#result.overlay { background: none; pointer-events: none; align-items: flex-start; padding: calc(50px + env(safe-area-inset-top)) 12px 12px; }
#result .dialog { pointer-events: auto; max-width: 440px; text-align: center; padding: 12px 16px; gap: 6px; background: rgba(23,33,54,.97); animation: rise .45s ease-out; }
#result .dialog h2 { font-size: 24px; } #result .tag { margin: 0; }
#result .row { justify-content: center; }
#result .dialog.party { border-color: var(--gold); box-shadow: 0 0 34px rgba(233,185,73,.6), 0 10px 40px rgba(0,0,0,.5); }
#result .dialog.party h2 { color: var(--gold); animation: bounce 1s ease-in-out infinite; }
@keyframes rise { from { transform: translateY(-30px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes bounce { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@media (prefers-reduced-motion: reduce) { #result .dialog, #result .dialog.party h2 { animation: none; } }

/* smiley (reaction) buttons next to the tray (human games) */
.react { display: flex; gap: 6px; justify-content: center; }
.react .rb { width: 38px; height: 28px; padding: 0; font-size: 17px; line-height: 1; border-radius: 9px; }
.react .rb[disabled] { opacity: .35; cursor: default; }

/* ranking overlay */
.ranking .rankwrap { max-height: 52vh; overflow: auto; border-radius: 10px; }
.ranking table { width: 100%; border-collapse: collapse; font-size: 14px; }
.ranking th, .ranking td { padding: 6px 8px; text-align: left; border-bottom: 1px solid rgba(255,255,255,.07); }
.ranking th { position: sticky; top: 0; background: var(--panel2); font-weight: 600; font-size: 12px; color: var(--muted); }
.ranking td.n, .ranking th.n { text-align: right; white-space: nowrap; }
.ranking tr.me td { background: rgba(233,185,73,.14); }
.ranking tr.top1 td:first-child { color: #ffd34d; font-weight: 800; } .ranking tr.top2 td:first-child { color: #d4d9e2; font-weight: 800; } .ranking tr.top3 td:first-child { color: #d99a63; font-weight: 800; }
.ranking .up { color: #54d18a; } .ranking .down { color: #ef7a80; } .ranking .muted { color: var(--muted); }
.ranking small { color: var(--gold); }
.tabs { margin-bottom: 4px; }
#pidShow { font-family: ui-monospace, Consolas, monospace; font-size: 11px; letter-spacing: 0; padding-left: 8px; padding-right: 6px; }
#pidInput { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }

@media (min-width: 700px) { :root { --ds: 66px; } }
#actions .rb { min-width: 0; }
#actions { align-items: center; }

/* the boxes where the points can be written blink gently */
.sc .val.pot.mine { animation: blinkpot 1.1s ease-in-out infinite; }
@keyframes blinkpot { 0%, 100% { background: rgba(56,194,176,.08); color: var(--teal); } 50% { background: rgba(56,194,176,.42); color: #fff; } }
@media (prefers-reduced-motion: reduce) { .sc .val.pot.mine { animation: none; } }

/* several scorecards at once: narrower columns, short labels */
.sc.multi > div { padding: 0 3px; font-size: clamp(10px, calc(var(--rh) * .42), 13px); }
.sc.multi .lab { padding-left: 6px; }
.sc .head.sub { font-size: 11px; font-weight: 600; color: var(--muted); }
.sc .head.sub.me { color: var(--gold); }
.sc .grand { font-size: 1.15em; }
.sc.multi .val.pot { font-size: 1em; }
.sc.multi .head .nm { max-width: 100%; }
