:root {
  --ink: #2b2030;
  --cream: #efe3c2;
  --paper: #fff4dc;
  --green: #1f4a34;
  --green-2: #2f6347;
  --red: #c8352f;
  --gold: #ffd84a;
  --font: "Inter Display", Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --u: clamp(0.7rem, 1.25vmin + 0.3rem, 1.15rem); /* HUD unit */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fd0f0; font-family: var(--font); color: var(--ink);
  -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; overscroll-behavior: none; }
canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible, summary:focus-visible { outline: 4px solid var(--gold); outline-offset: 3px; }

/* ---------- ticket-stub buttons ---------- */
.ticket {
  --edge: var(--red);
  position: relative; min-width: 56px; min-height: 52px; padding: 0 calc(var(--u) * 1.2);
  background: var(--paper); border: 4px solid var(--edge); border-radius: 12px; color: var(--edge);
  font-weight: 900; letter-spacing: .04em; display: inline-flex; align-items: center; justify-content: center; gap: .4em;
  -webkit-mask: radial-gradient(circle at 0 50%, transparent 9px, #000 10px) left / 51% 100% no-repeat,
                radial-gradient(circle at 100% 50%, transparent 9px, #000 10px) right / 51% 100% no-repeat;
          mask: radial-gradient(circle at 0 50%, transparent 9px, #000 10px) left / 51% 100% no-repeat,
                radial-gradient(circle at 100% 50%, transparent 9px, #000 10px) right / 51% 100% no-repeat;
  transition: transform .08s ease;
}
.ticket::after { content: ""; position: absolute; left: 16px; top: 8px; bottom: 8px; border-left: 2px dashed var(--edge); opacity: .55; }
.ticket:active { transform: scale(.95); }
.ticket svg { width: 26px; height: 26px; fill: currentColor; }
.ticket.big { font-size: calc(var(--u) * 2.1); min-height: 76px; padding: 0 calc(var(--u) * 2.4); }
.ticket.wide { min-width: 200px; font-size: calc(var(--u) * 1.3); }
.ticket.alt { --edge: var(--green); }
.ticket:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- HUD ---------- */
.hud { position: fixed; inset: 0; pointer-events: none; transition: opacity .25s ease; }
.hud.inflight { opacity: .4; }
.hud > div { position: absolute; }
.hud button { pointer-events: auto; }
.hud-tl { top: calc(12px + var(--safe-t)); left: calc(12px + var(--safe-l)); display: flex; gap: 10px; }
.hud-tc { top: calc(14px + var(--safe-t)); left: 50%; transform: translateX(-50%); }
.hud-tr { top: calc(12px + var(--safe-t)); right: calc(12px + var(--safe-r)); display: flex; flex-direction: column; align-items: stretch; gap: 8px; }
.hud-bl { bottom: calc(12px + var(--safe-b)); left: calc(12px + var(--safe-l)); }
body.lefty .hud-tl { left: auto; right: calc(12px + var(--safe-r)); }
body.lefty .hud-tr { right: auto; left: calc(12px + var(--safe-l)); }
body.lefty .hud-bl { left: auto; right: calc(12px + var(--safe-r)); }
body.lefty .hud-tl + .hud-tc + .hud-tr { top: calc(12px + var(--safe-t)); }

.chalk-pill { background: var(--green); color: var(--cream); border: 3px solid var(--cream); border-radius: 999px;
  padding: .35em 1.1em; font-weight: 800; letter-spacing: .12em; font-size: calc(var(--u) * 1.05); white-space: nowrap; }

.scoreboard { background: var(--green); border: 4px solid var(--cream); border-radius: 10px; padding: .5em .8em .45em;
  color: var(--cream); min-width: calc(var(--u) * 15); box-shadow: inset 0 0 0 3px var(--green-2); }
.sb-label { font-weight: 800; font-size: calc(var(--u) * .85); letter-spacing: .2em; }
.sb-digits { display: flex; justify-content: flex-end; gap: 4px; margin: .2em 0 .3em; }
.sb-digits .d { width: calc(var(--u) * 1.75); height: calc(var(--u) * 2.5); background: #121212; border-radius: 4px; color: #fbf7ea;
  display: grid; place-items: center; font-weight: 800; font-size: calc(var(--u) * 1.9); position: relative; font-variant-numeric: tabular-nums; }
.sb-digits .d::after { content: ""; position: absolute; left: 2px; right: 2px; top: 50%; border-top: 2px solid #2c2c2c; }
.sb-digits .c { width: 6px; align-self: flex-end; font-weight: 900; font-size: calc(var(--u) * 1.4); line-height: 1; }
.sb-row { display: flex; justify-content: space-between; align-items: center; gap: 1em; font-weight: 700; font-size: calc(var(--u) * .85); letter-spacing: .06em; }
.sb-dogs { display: inline-flex; align-items: center; gap: .4em; }
.sb-dogs svg { width: calc(var(--u) * 1.1); height: calc(var(--u) * 1.1); }
.starbar { position: relative; height: 14px; margin: 6px 12px 0; background: rgba(0,0,0,.35); border: 3px solid var(--cream); border-radius: 999px; }
.star-fill { position: absolute; left: 2px; top: 2px; bottom: 2px; width: 0; background: var(--gold); border-radius: 999px; transition: width .3s ease; }
.starbar .st { position: absolute; top: 50%; transform: translate(-50%, -52%); font-size: calc(var(--u) * 1.7); color: #3b3b3b;
  -webkit-text-stroke: 2px var(--cream); line-height: 1; transition: color .2s, transform .2s; }
.starbar .st.on { color: var(--gold); transform: translate(-50%, -52%) scale(1.15); }

.dugout { position: relative; width: calc(var(--u) * 15); padding-top: calc(var(--u) * 1.4); }
.dugout-roof { position: absolute; top: 0; left: -6px; right: -6px; height: calc(var(--u) * 1.9); background: var(--green); border: 4px solid var(--cream);
  border-radius: 6px; clip-path: polygon(4% 0, 96% 0, 100% 100%, 0 100%); color: var(--cream); text-align: center;
  font-weight: 800; letter-spacing: .3em; font-size: calc(var(--u) * .8); line-height: calc(var(--u) * 1.9 - 8px); }
.dugout-balls { background: #26292f; border: 4px solid var(--cream); border-radius: 10px; min-height: calc(var(--u) * 4.4);
  display: flex; align-items: center; gap: calc(var(--u) * .8); padding: calc(var(--u) * .9) calc(var(--u) * 1) calc(var(--u) * .6); }
.dugout-balls .b { position: relative; width: calc(var(--u) * 2.4); height: calc(var(--u) * 2.4); }
.dugout-balls .b img { width: 100%; height: 100%; display: block; }
.dugout-balls .b.next::after { content: "NEXT"; position: absolute; left: 50%; bottom: calc(var(--u) * -1.25); transform: translateX(-50%);
  background: var(--gold); color: var(--ink); font-weight: 900; font-size: calc(var(--u) * .62); letter-spacing: .12em; padding: 1px 6px; border-radius: 999px; }
.dugout-balls .empty { color: #8b8f99; font-weight: 700; font-size: calc(var(--u) * .9); }

.tip { left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%); background: rgba(20,22,27,.82); color: #fff;
  border: 3px solid var(--cream); border-radius: 999px; padding: .5em 1.2em; font-weight: 700; font-size: calc(var(--u) * 1.05);
  white-space: nowrap; opacity: 0; transition: opacity .3s; }
.tip.show { opacity: 1; }
.tip kbd { display: inline-block; min-width: 1.6em; padding: 0 .35em; margin: 0 .1em; border: 2px solid var(--cream); border-bottom-width: 4px; border-radius: 6px;
  font: inherit; font-size: .85em; text-align: center; background: #2c2f36; }

.callout { left: 50%; top: 26%; transform: translate(-50%, -50%) rotate(-5deg); text-align: center; white-space: nowrap; pointer-events: none;
  font-weight: 900; font-size: calc(var(--u) * 4.4); color: var(--gold); letter-spacing: .02em; -webkit-text-stroke: 10px var(--ink); paint-order: stroke fill;
  opacity: 0; }
.callout small { display: block; font-size: .55em; color: #fff; -webkit-text-stroke: 7px var(--ink); }
.callout.show { animation: pop 1.6s ease forwards; }
@keyframes pop { 0% { opacity: 0; transform: translate(-50%, -50%) rotate(-5deg) scale(.4); } 12% { opacity: 1; transform: translate(-50%, -50%) rotate(-5deg) scale(1.08); }
  20% { transform: translate(-50%, -50%) rotate(-5deg) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -60%) rotate(-5deg) scale(1); } }

/* ---------- screens ---------- */
.screen { position: fixed; inset: 0; display: grid; place-items: center; padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b)); }
.screen.title { background: radial-gradient(ellipse at 50% 40%, rgba(255,250,235,.78), rgba(255,250,235,.15) 55%, rgba(31,74,52,.25)), url(../assets/bg-sandlot.svg) center 75% / cover no-repeat, #8fd0f0; }
.title-card { text-align: center; display: flex; flex-direction: column; align-items: center; gap: calc(var(--u) * .8); max-width: 760px; }
.title-dog { width: calc(var(--u) * 7); height: auto; margin-bottom: calc(var(--u) * -1.2); }
.title h1 { margin: 0; line-height: .92; display: flex; flex-direction: column; font-weight: 900; color: #fbf3dc; letter-spacing: .02em;
  -webkit-text-stroke: 9px var(--ink); paint-order: stroke fill; text-shadow: 0 8px 0 var(--red); }
.title h1 span:first-child { font-size: calc(var(--u) * 4.6); }
.title h1 span:last-child { font-size: calc(var(--u) * 6.4); color: var(--gold); }
.title-sub { margin: .3em 0 .6em; font-weight: 700; font-size: calc(var(--u) * 1.15); max-width: 40em; background: rgba(255,250,240,.92); padding: .4em .9em; border-radius: 12px; border: 3px solid var(--ink); }
.title-best { margin: 0; font-weight: 800; min-height: 1.2em; }
.title-row { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; justify-content: center; }
.chip { background: var(--green); color: var(--cream); border: 3px solid var(--cream); border-radius: 999px; padding: .45em 1em; font-weight: 800; min-height: 44px; }
.controls-hint { font-weight: 700; font-size: calc(var(--u) * .85); color: #4b3f4e; }
.credits { position: absolute; bottom: calc(10px + var(--safe-b)); left: 0; right: 0; text-align: center; margin: 0; font-size: 12px; color: #5b4630; font-weight: 600; }

.screen.modal { background: rgba(20, 24, 22, .55); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
.card { background: #fffaf0; border: 5px solid var(--ink); border-radius: 24px; box-shadow: 8px 8px 0 var(--ink); padding: 22px 26px; width: min(460px, 100%);
  max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow: auto; text-align: center; user-select: text; -webkit-user-select: text; }
.card h2 { margin: 0 0 12px; font-weight: 900; font-size: calc(var(--u) * 2.4); letter-spacing: .04em; }
.stack { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.row { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 10px; }
.settings { margin: 16px 0 6px; border: 3px dashed #b9a98a; border-radius: 14px; padding: 8px 14px 12px; text-align: left; }
.settings legend { font-weight: 900; letter-spacing: .12em; padding: 0 6px; }
.settings label { display: flex; gap: 10px; align-items: center; min-height: 40px; font-weight: 600; cursor: pointer; }
.settings input { width: 22px; height: 22px; accent-color: var(--green); }
.keys { text-align: left; margin-top: 8px; }
.keys summary { font-weight: 800; cursor: pointer; min-height: 32px; }
.keys table { font-size: 14px; border-collapse: collapse; }
.keys th { text-align: left; padding: 4px 10px 4px 0; vertical-align: top; white-space: nowrap; }
.keys td { padding: 4px 0; }

.res-stars { display: flex; justify-content: center; gap: 6px; margin: -4px 0 10px; }
.res-stars span { font-size: 64px; line-height: 1; color: #d8d2c2; -webkit-text-stroke: 4px var(--ink); paint-order: stroke fill; transform: scale(.6); opacity: .6; transition: transform .35s cubic-bezier(.2,1.6,.4,1), color .2s, opacity .2s; }
.res-stars span:nth-child(2) { font-size: 80px; margin-top: -10px; }
.res-stars span.on { color: var(--gold); transform: scale(1); opacity: 1; }
.res-rows { margin: 0 auto 8px; display: grid; grid-template-columns: 1fr auto; gap: 4px 18px; max-width: 320px; text-align: left; font-weight: 600; }
.res-rows dt { color: #5b5060; } .res-rows dd { margin: 0; text-align: right; font-weight: 800; font-variant-numeric: tabular-nums; }
.res-total { display: flex; justify-content: space-between; align-items: baseline; max-width: 320px; margin: 6px auto; border-top: 3px dashed #b9a98a; padding-top: 8px; }
.res-total span { font-weight: 900; letter-spacing: .15em; } .res-total strong { font-size: 34px; font-variant-numeric: tabular-nums; }
.res-best { margin: 2px 0 6px; font-weight: 700; color: #5b5060; min-height: 1.2em; }
.soon { margin: 10px 0 0; font-size: 13px; color: #6b5f6e; }
.soon:empty { display: none; }
.res-levels { margin-top: 12px; }

/* ---------- level select: a scoreboard-style 5 x 3 grid ---------- */
.card.levels { width: min(640px, 100%); }
.levels-stars { margin: -6px 0 12px; font-weight: 800; letter-spacing: .06em; }
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; background: var(--green); border: 4px solid var(--cream);
  border-radius: 14px; padding: 12px; box-shadow: inset 0 0 0 3px var(--green-2); }
.lv { min-height: 76px; background: #121212; color: #fbf7ea; border: 3px solid #2c2c2c; border-radius: 8px; padding: 6px 2px 4px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-weight: 900; }
.lv .n { font-size: 24px; line-height: 1; font-variant-numeric: tabular-nums; }
.lv .s { font-size: 14px; letter-spacing: 1px; color: #4a4a4a; }
.lv .s .on { color: var(--gold); }
.lv.cur { border-color: var(--gold); }
.lv.boss .n::after { content: " ★"; font-size: 12px; color: var(--gold); vertical-align: top; }
.lv:disabled { opacity: .5; cursor: not-allowed; }
.lv:disabled .s { color: #8b8f99; }
.lv:not(:disabled):hover { border-color: var(--cream); }
@media (max-height: 460px) { .lv { min-height: 58px; } .lv .n { font-size: 20px; } .level-grid { gap: 6px; padding: 8px; } }

/* ---------- leaderboard (results card) ---------- */
.lb { margin: 14px 0 0; border-top: 3px dashed #b9a98a; padding-top: 10px; }
.lb h3 { margin: 0 0 8px; font-weight: 900; letter-spacing: .18em; font-size: 15px; }
.lb-form { display: flex; gap: 10px; justify-content: center; align-items: center; flex-wrap: wrap; }
.lb-form label { font-weight: 700; color: #5b5060; }
.lb-form input { width: 4.2em; min-height: 48px; padding: 0 .3em; font: inherit; font-size: 24px; font-weight: 900; letter-spacing: .25em;
  text-align: center; text-transform: uppercase; border: 4px solid var(--ink); border-radius: 10px; background: #fff; color: var(--ink); }
.lb-form .ticket { min-height: 48px; font-size: 16px; }
.lb-status { margin: 8px 0 4px; min-height: 1.3em; font-weight: 700; color: #5b5060; }
.lb-status.ok { color: var(--green); } .lb-status.bad { color: var(--red); }
.lb-list { list-style: none; margin: 6px auto 0; padding: 0; max-width: 300px; text-align: left; counter-reset: none; }
.lb-list li { display: grid; grid-template-columns: 2.2em 1fr auto; gap: 8px; padding: 3px 8px; border-radius: 8px; font-weight: 700; font-variant-numeric: tabular-nums; }
.lb-list li:nth-child(odd) { background: #f3ead6; }
.lb-list li.me { background: var(--gold); }
.lb-list .r { color: #6b5f6e; } .lb-list .n { letter-spacing: .2em; font-weight: 900; } .lb-list .s { text-align: right; }
.lb-list .empty { display: block; text-align: center; color: #6b5f6e; font-weight: 600; background: none; }

.rotate { position: fixed; left: 50%; bottom: calc(110px + var(--safe-b)); transform: translateX(-50%); background: rgba(20,22,27,.85); color: #fff;
  padding: 8px 16px; border-radius: 999px; font-weight: 700; font-size: 14px; display: none; pointer-events: none; white-space: nowrap; }
@media (orientation: portrait) and (max-width: 820px) {
  .rotate.on { display: block; bottom: calc(var(--u) * 8 + 90px + var(--safe-b)); }
  .hud-tc { display: none; }
  .tip { bottom: calc(var(--u) * 8 + 24px + var(--safe-b)); white-space: normal; text-align: center; max-width: 92vw; }
}

@media (max-height: 460px) {
  :root { --u: clamp(0.6rem, 1.6vmin + 0.2rem, 0.9rem); }
  .ticket { min-height: 48px; min-width: 52px; }
  .title h1 span:first-child { font-size: calc(var(--u) * 3.6); }
  .title h1 span:last-child { font-size: calc(var(--u) * 5); }
  .title-dog { display: none; }
}
@media (prefers-reduced-motion: reduce) { .callout.show { animation-duration: 2s; } .ticket { transition: none; } }
