:root {
  --ink: #5a4334;
  --paper: #fffaf0;
  --yellow: #ffd84a;
  --yellow-d: #f0b72a;
  --blue: #8fd3ff;
  --blue-d: #5fb3e8;
  --pink: #ff8fb1;
  --green: #5fd35f;
  --red: #ff5a5a;
  --font: "Arial Rounded MT Bold", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #f3e6cf; overflow: hidden; font-family: var(--font); color: var(--ink); }
body { display: flex; justify-content: center; align-items: center; touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; overscroll-behavior: none; position: fixed; inset: 0; }
input { -webkit-user-select: text; user-select: text; font-size: 16px; }
button, input { font-family: inherit; color: inherit; }
.hidden { display: none !important; }

/* portrait stage: full screen on phones, phone-shaped on desktop */
#app { position: relative; width: 100vw; height: 100dvh; overflow: hidden; background: #bfe8ff; }
@media (min-aspect-ratio: 3/4) {
  #app { width: min(100vw, calc(100dvh * 0.5)); border-radius: 28px; box-shadow: 0 0 0 6px var(--ink), 0 20px 60px rgba(90, 67, 52, 0.35); height: min(100dvh - 32px, 960px); }
}
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#touch { position: absolute; inset: 0; z-index: 5; touch-action: none; }

/* ---------- screens ---------- */
.screen { position: absolute; inset: 0; z-index: 10; display: flex; flex-direction: column; justify-content: flex-end; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); pointer-events: none; }
.screen > * { pointer-events: auto; }
.logo { position: absolute; top: calc(18px + env(safe-area-inset-top)); left: 0; right: 0; text-align: center; pointer-events: none; }
.logo h1 { margin: 0; font-size: 44px; letter-spacing: 4px; color: #fff; -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; text-shadow: 0 4px 0 var(--ink); }
.logo p { margin: 4px 0 0; font-weight: bold; color: var(--ink); opacity: .8; }

.panel { background: var(--paper); border: 3px solid var(--ink); border-radius: 24px; padding: 16px; box-shadow: 0 6px 0 var(--ink); max-height: 62%; overflow-y: auto; overscroll-behavior: contain; }
#room .panel { max-height: 68%; }
#results .panel, #join .panel { margin-bottom: auto; margin-top: auto; max-height: 90%; }
.panel h2 { margin: 0 0 12px; text-align: center; font-size: 24px; }
.panel h3 { margin: 14px 0 8px; font-size: 15px; opacity: .75; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

.btn { display: block; width: 100%; border: 3px solid var(--ink); border-radius: 16px; padding: 12px 10px; font-size: 18px; font-weight: bold; background: #fff; box-shadow: 0 4px 0 var(--ink); cursor: pointer; margin-top: 10px; transition: transform .06s, box-shadow .06s; }
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.yellow { background: var(--yellow); }
.btn.blue { background: var(--blue); }
.btn.ghost { background: transparent; box-shadow: none; border-style: dashed; font-size: 16px; padding: 9px; }
.btn.big { font-size: 22px; padding: 14px; }
.btn.small { width: auto; padding: 6px 14px; font-size: 15px; margin: 0; }
.link { display: block; margin: 12px auto 0; background: none; border: none; text-decoration: underline; font-size: 15px; cursor: pointer; opacity: .75; }

.name-field { display: flex; align-items: center; gap: 10px; background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 4px 4px 4px 14px; }
.name-field span { font-weight: bold; white-space: nowrap; font-size: 15px; }
.name-field input { flex: 1; min-width: 0; border: none; outline: none; font-size: 18px; padding: 8px; background: transparent; font-weight: bold; }

.room-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.room-list li { display: flex; align-items: center; gap: 10px; background: #fff; border: 2px solid var(--ink); border-radius: 14px; padding: 8px 12px; cursor: pointer; }
.room-list li.empty { justify-content: center; opacity: .6; cursor: default; border-style: dashed; }
.room-list .code { font-size: 22px; font-weight: bold; letter-spacing: 2px; }
.room-list .meta { flex: 1; font-size: 13px; opacity: .7; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-list .tag { font-size: 13px; padding: 3px 10px; border-radius: 10px; background: var(--yellow); border: 2px solid var(--ink); font-weight: bold; }
.room-list .tag.racing { background: #eee; }

/* join keypad */
.code-slots { display: flex; justify-content: center; gap: 10px; margin-bottom: 14px; }
.code-slots span { width: 64px; height: 72px; border: 3px dashed var(--ink); border-radius: 18px; display: grid; place-items: center; font-size: 38px; font-weight: bold; background: #fff; }
.code-slots span.filled { border-style: solid; background: var(--yellow); animation: pop .25s; }
.keypad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.key { border: 3px solid var(--ink); border-radius: 16px; background: #fff; font-size: 28px; font-weight: bold; padding: 12px 0; box-shadow: 0 4px 0 var(--ink); cursor: pointer; }
.key:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.key.del { grid-column: span 2; background: #ffe1e1; }

/* room */
.room-head { display: flex; justify-content: space-between; align-items: center; }
.room-head small { font-weight: bold; opacity: .7; }
.room-code { font-size: 38px; font-weight: bold; letter-spacing: 6px; }
.player-list { list-style: none; padding: 0; margin: 10px 0 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.player-list li { display: flex; align-items: center; gap: 6px; background: #fff; border: 2px solid var(--ink); border-radius: 14px; padding: 6px 10px; min-width: 0; }
.player-list li.me { background: #fff4c4; }
.player-list li.offline { opacity: .5; }
.player-list li.waiting { border-style: dashed; justify-content: center; opacity: .55; font-size: 13px; }
.player-list .pname { font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.player-list .pbreed { font-size: 12px; opacity: .65; white-space: nowrap; }
.player-list .off { font-size: 11px; color: var(--red); }
.dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ink); flex: none; display: inline-block; }

.breed-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.breed { text-align: left; border: 3px solid var(--ink); border-radius: 16px; background: #fff; padding: 8px 10px; cursor: pointer; display: grid; grid-template-columns: auto 1fr; column-gap: 8px; align-items: center; }
.breed.selected { background: #fff4c4; box-shadow: 0 0 0 3px var(--yellow-d) inset; }
.breed .swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--ink); grid-row: span 2; }
.breed b { font-size: 16px; }
.breed small { font-size: 11px; opacity: .7; line-height: 1.3; }
.breed .stats { grid-column: span 2; display: grid; gap: 2px; margin-top: 6px; }
.breed .stats em { font-style: normal; font-size: 11px; display: grid; grid-template-columns: 30px 1fr; align-items: center; }
.breed .stats u { display: block; height: 6px; border-radius: 3px; background: var(--pink); max-width: 100%; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 2px solid var(--ink); border-radius: 12px; background: #fff; padding: 6px 12px; font-weight: bold; cursor: pointer; font-size: 15px; }
.chip.on { background: var(--yellow); }
.chip:disabled { cursor: default; }
.chip:disabled:not(.on) { opacity: .5; }

/* results */
.result-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.result-list li { display: flex; align-items: center; gap: 10px; background: #fff; border: 2px solid var(--ink); border-radius: 14px; padding: 8px 12px; }
.result-list li.me { background: #fff4c4; }
.result-list .place { font-size: 24px; width: 34px; text-align: center; font-weight: bold; }
.result-list .pname { flex: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-list .score { font-weight: bold; font-size: 14px; opacity: .8; }
#results { background: rgba(255, 250, 240, 0.35); }

/* ---------- HUD ---------- */
#hud { position: absolute; inset: 0; z-index: 6; pointer-events: none; }
#hud .icon-btn { pointer-events: auto; }
.hud-top { position: absolute; top: calc(10px + env(safe-area-inset-top)); left: 10px; right: 10px; display: flex; align-items: center; gap: 8px; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 3px solid var(--ink); background: #fff; font-size: 16px; font-weight: bold; cursor: pointer; flex: none; z-index: 7; position: relative; }
.timer { flex: 1; text-align: center; font-size: 30px; font-weight: bold; color: #fff; -webkit-text-stroke: 1.5px var(--ink); paint-order: stroke fill; text-shadow: 0 3px 0 var(--ink); }
.timer.urgent { color: #ffd0d0; animation: pulse .5s infinite alternate; }
.timer.settling { font-size: 24px; color: #ffe08a; }
.hud-place { position: absolute; left: 56px; top: 50px; background: var(--paper); border: 3px solid var(--ink); border-radius: 14px; padding: 2px 10px; font-weight: bold; font-size: 15px; }
.hud-place b { font-size: 26px; margin: 0 2px; }
.hud-place small { opacity: .6; }
.progress { position: absolute; top: calc(62px + env(safe-area-inset-top)); left: 164px; right: 22px; height: 14px; border: 3px solid var(--ink); border-radius: 10px; background: rgba(255, 255, 255, .8); }
.progress .flag { position: absolute; right: -14px; top: -14px; font-size: 22px; }
.pdot { position: absolute; top: 50%; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ink); transform: translate(-50%, -50%); transition: left .1s linear; }
.pdot.me { width: 22px; height: 22px; z-index: 2; box-shadow: 0 0 0 3px #fff; }
.speed { position: absolute; top: calc(92px + env(safe-area-inset-top)); right: 16px; font-weight: bold; font-size: 14px; background: rgba(255,255,255,.75); border: 2px solid var(--ink); border-radius: 10px; padding: 2px 8px; }

.banner { position: absolute; top: 24%; left: 0; right: 0; text-align: center; font-size: 26px; font-weight: bold; color: #fff; -webkit-text-stroke: 1.5px var(--ink); paint-order: stroke fill; text-shadow: 0 3px 0 var(--ink); opacity: 0; padding: 0 16px; }
.banner.show { animation: banner 2.6s ease-out; }
.countdown { position: absolute; top: 34%; left: 0; right: 0; text-align: center; font-size: 110px; font-weight: bold; color: var(--yellow); -webkit-text-stroke: 4px var(--ink); paint-order: stroke fill; text-shadow: 0 8px 0 var(--ink); }
.countdown.beat { animation: beat .9s ease-out; }
.pops { position: absolute; top: 40%; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; }
.pop { font-size: 26px; font-weight: bold; color: #fff; -webkit-text-stroke: 1.5px var(--ink); paint-order: stroke fill; text-shadow: 0 3px 0 var(--ink); animation: floatUp 1.3s ease-out forwards; position: absolute; white-space: nowrap; }
.pop.good { color: #b7ffb0; }
.pop.warn { color: #ffe08a; }
.pop.bad { color: #ffb0b0; font-size: 30px; }
#hud.wobbly .pads { animation: wobble .18s infinite alternate; }

.getup { position: absolute; top: 54%; left: 50%; transform: translateX(-50%); background: var(--paper); border: 3px solid var(--ink); border-radius: 20px; padding: 12px 20px; text-align: center; box-shadow: 0 5px 0 var(--ink); animation: pop .25s; }
.getup b { display: block; font-size: 22px; }
.getup span { font-size: 15px; }
.getup .dots { display: flex; justify-content: center; gap: 6px; margin-top: 8px; }
.getup .dots i { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--ink); background: #fff; }
.getup .dots i.on { background: var(--yellow); }

.pads { position: absolute; left: 0; right: 0; bottom: calc(20px + env(safe-area-inset-bottom)); display: flex; justify-content: space-between; padding: 0 18px; }
.pad { position: relative; width: 104px; height: 104px; border-radius: 50%; background: rgba(255, 250, 240, .78); border: 3px solid var(--ink); box-shadow: 0 5px 0 var(--ink); transition: transform .08s; }
.pad.held { transform: scale(1.08) translateY(-4px); background: #fff; }
.pad.sweet { background: #e2ffd9; }
.pad.over { background: #ffe0e0; }
.pad svg { position: absolute; inset: 0; transform: rotate(-90deg); }
.pad circle { fill: none; stroke-width: 9; }
.ring-base { stroke: rgba(90, 67, 52, .15); }
.ring-sweet { stroke: rgba(95, 211, 95, .55); }
.ring-over { stroke: rgba(255, 90, 90, .45); }
.ring-fill { stroke: var(--ink); stroke-linecap: round; stroke-width: 6 !important; }
.pad.sweet .ring-fill { stroke: #2e9d2e; }
.pad.over .ring-fill { stroke: #d33; }
.pad-label { position: absolute; inset: 0; display: grid; place-items: center; font-weight: bold; font-size: 20px; }
.pad-key { position: absolute; bottom: 16px; left: 0; right: 0; text-align: center; font-size: 10px; opacity: .5; }
@media (pointer: coarse) { .pad-key { display: none; } }

/* help + toast */
.help { position: absolute; inset: 0; z-index: 30; background: rgba(90, 67, 52, .45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.help .card { background: var(--paper); border: 3px solid var(--ink); border-radius: 24px; padding: 18px; box-shadow: 0 6px 0 var(--ink); max-height: 100%; overflow-y: auto; }
.help h2 { margin: 0 0 10px; text-align: center; }
.help p { margin: 8px 0; font-size: 15px; line-height: 1.5; }
.toast { position: absolute; left: 50%; top: 18%; transform: translate(-50%, -20px); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 14px; font-weight: bold; opacity: 0; transition: .25s; z-index: 40; pointer-events: none; white-space: nowrap; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.net { position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%); background: var(--red); color: #fff; padding: 6px 14px; border-radius: 12px; font-weight: bold; z-index: 40; font-size: 13px; }

@keyframes pop { 0% { transform: scale(.6); } 70% { transform: scale(1.1); } 100% { transform: scale(1); } }
.getup { animation-name: none; }
@keyframes beat { 0% { transform: scale(2); opacity: 0; } 25% { transform: scale(1); opacity: 1; } 80% { opacity: 1; } 100% { transform: scale(.9); opacity: 0; } }
@keyframes floatUp { 0% { transform: translateY(10px) scale(.7); opacity: 0; } 15% { transform: translateY(0) scale(1.1); opacity: 1; } 70% { opacity: 1; } 100% { transform: translateY(-60px) scale(1); opacity: 0; } }
@keyframes banner { 0% { opacity: 0; transform: translateY(-20px); } 12% { opacity: 1; transform: none; } 80% { opacity: 1; } 100% { opacity: 0; } }
@keyframes pulse { to { transform: scale(1.12); } }
@keyframes wobble { from { transform: rotate(-1.5deg); } to { transform: rotate(1.5deg); } }

/* phones held sideways: ask for portrait */
.rotate { display: none; position: fixed; inset: 0; z-index: 100; background: #bfe8ff; color: var(--ink); flex-direction: column; align-items: center; justify-content: center; font-weight: bold; font-size: 20px; gap: 12px; }
.rotate span { font-size: 64px; animation: wobble .6s infinite alternate; }
@media (orientation: landscape) and (pointer: coarse) and (max-height: 600px) { .rotate { display: flex; } }
