:root {
    --bg: #06070d;
    --bg-2: #0b0e1a;
    --panel: rgba(16, 20, 34, 0.92);
    --panel-border: rgba(120, 140, 200, 0.18);
    --text: #e9edf8;
    --muted: #8b95ad;
    --cyan: #25e6ff;
    --magenta: #ff3df0;
    --gold: #ffd34d;
    --green: #8affc1;
    --red: #ff5a6a;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", sans-serif;
    overscroll-behavior: none;
}

body {
    background:
        radial-gradient(1200px 700px at 50% -10%, rgba(37, 230, 255, 0.10), transparent 60%),
        radial-gradient(900px 600px at 85% 110%, rgba(255, 61, 240, 0.10), transparent 60%),
        linear-gradient(180deg, #06070d 0%, #080a12 100%);
}

.hidden { display: none !important; }

#app {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    padding: calc(8px + var(--safe-top)) 10px calc(8px + var(--safe-bottom));
    gap: 8px;
}

/* ---------------- HUD ---------------- */
#hud {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 10px;
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
    padding: 6px 4px;
}

.hud-block { min-width: 0; }
.hud-label {
    font-size: 10px;
    letter-spacing: 0.18em;
    color: var(--muted);
    font-weight: 700;
}

.hud-score { text-align: left; }
.score {
    font-size: clamp(26px, 7vw, 40px);
    font-weight: 800;
    line-height: 1;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
    text-shadow: 0 0 24px rgba(37, 230, 255, 0.35);
}

.hud-center { text-align: center; }
.phase {
    font-size: clamp(11px, 3vw, 14px);
    font-weight: 800;
    letter-spacing: 0.16em;
    color: var(--cyan);
    text-transform: uppercase;
    transition: color 0.3s ease;
}
.phase.rush { color: var(--red); animation: pulseText 0.7s ease-in-out infinite; }
.timer { font-size: clamp(18px, 5vw, 26px); font-weight: 800; font-variant-numeric: tabular-nums; }
.timer-sub { font-size: 12px; color: var(--muted); margin-left: 2px; }

.hud-right { text-align: right; }
.chain-row { display: flex; justify-content: flex-end; align-items: baseline; gap: 6px; }
.chain-value { font-size: 18px; font-weight: 800; color: var(--gold); font-variant-numeric: tabular-nums; }
.chainbar {
    height: 5px;
    margin: 4px 0 4px auto;
    width: 100%;
    max-width: 130px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 999px;
    overflow: hidden;
}
.chainbar i {
    display: block;
    height: 100%;
    width: 0%;
    border-radius: 999px;
    background: linear-gradient(90deg, var(--cyan), var(--gold), var(--magenta));
    transition: width 0.18s ease;
}
.partner {
    font-size: 10px;
    letter-spacing: 0.12em;
    color: var(--muted);
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 5px;
}
.partner .dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--green);
    box-shadow: 0 0 8px var(--green);
}
.partner.warn .dot { background: var(--gold); box-shadow: 0 0 8px var(--gold); }
.partner.bad .dot { background: var(--red); box-shadow: 0 0 8px var(--red); }
.beat { font-size: 10px; letter-spacing: 0.12em; color: var(--gold); font-weight: 800; margin-top: 2px; }

/* ---------------- Stage ---------------- */
#stage {
    flex: 1 1 auto;
    position: relative;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

#board {
    display: block;
    border-radius: 18px;
    touch-action: none;
    cursor: crosshair;
    background: #05060b;
    box-shadow:
        0 0 0 1px rgba(120, 150, 220, 0.14),
        0 20px 60px rgba(0, 0, 0, 0.6),
        0 0 90px rgba(37, 230, 255, 0.06);
}

/* ---------------- Overlay ---------------- */
.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    background: radial-gradient(circle at 50% 40%, rgba(8, 10, 20, 0.55), rgba(4, 5, 10, 0.82));
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 5;
    text-align: center;
    pointer-events: none;
}
.overlay.hidden { display: none; }

.panel {
    pointer-events: auto;
    width: min(440px, 94%);
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 20px;
    padding: 26px 22px;
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55);
    animation: rise 0.28s ease;
}

.brand {
    font-size: clamp(28px, 9vw, 42px);
    font-weight: 900;
    letter-spacing: 0.04em;
    line-height: 1;
}
.brand span { color: var(--magenta); text-shadow: 0 0 24px rgba(255, 61, 240, 0.5); }
.brand:not(.small) { color: var(--cyan); text-shadow: 0 0 26px rgba(37, 230, 255, 0.4); }
.brand.small { font-size: 22px; color: var(--cyan); }

.tagline {
    margin: 8px 0 18px;
    color: var(--muted);
    font-size: 14px;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.howto {
    width: 100%;
    max-width: 320px;
    margin: 0 auto 16px;
    text-align: left;
}
.howto-row {
    display: grid;
    grid-template-columns: 14px 1fr;
    align-items: center;
    gap: 9px;
    margin-bottom: 8px;
    font-size: 13.5px;
    color: #c8d0e2;
    line-height: 1.35;
}
.howto-row > span:last-child { min-width: 0; }
.chip { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 auto; }
.chip-you { background: var(--cyan); box-shadow: 0 0 10px var(--cyan); }
.chip-them { background: var(--magenta); box-shadow: 0 0 10px var(--magenta); }
.howto-note {
    margin: 2px 0 0;
    font-size: 13.5px;
    color: #c8d0e2;
    line-height: 1.5;
}
.howto b.t { color: var(--gold); }
.howto b.m { color: var(--red); }
.howto b.g { color: var(--green); }

#lobby .btn.primary { width: 100%; max-width: 320px; }
#lobby .lobby-hint { max-width: 320px; margin-left: auto; margin-right: auto; }

.lobby-status { font-size: 15px; font-weight: 700; letter-spacing: 0.06em; color: var(--text); }
.lobby-hint { margin-top: 6px; font-size: 12px; color: var(--muted); }
.dots i { animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }

.btn {
    appearance: none;
    border: 1px solid rgba(150, 170, 230, 0.28);
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
    font: inherit;
    font-weight: 800;
    letter-spacing: 0.06em;
    padding: 13px 18px;
    border-radius: 13px;
    cursor: pointer;
    transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}
.btn:hover { background: rgba(255, 255, 255, 0.11); }
.btn:active { transform: scale(0.97); }
.btn.primary {
    background: linear-gradient(135deg, rgba(37, 230, 255, 0.95), rgba(120, 110, 255, 0.95));
    border-color: transparent;
    color: #04121a;
    box-shadow: 0 10px 30px rgba(37, 230, 255, 0.25);
}
.btn.primary:hover { box-shadow: 0 12px 36px rgba(37, 230, 255, 0.4); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.tiny { padding: 7px 12px; font-size: 11px; border-radius: 10px; }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }

/* Result */
.result-kicker { font-size: 11px; letter-spacing: 0.24em; color: var(--muted); font-weight: 800; }
.result-tier {
    font-size: clamp(20px, 6vw, 28px);
    font-weight: 900;
    letter-spacing: 0.02em;
    margin: 6px 0 2px;
    color: var(--cyan);
    text-shadow: 0 0 26px rgba(37, 230, 255, 0.4);
}
.result-score {
    font-size: clamp(46px, 16vw, 76px);
    font-weight: 900;
    line-height: 1;
    letter-spacing: -0.03em;
    font-variant-numeric: tabular-nums;
    background: linear-gradient(180deg, #ffffff, #9fd8ff);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}
.result-beat { margin-top: 8px; font-size: 12px; letter-spacing: 0.14em; color: var(--gold); font-weight: 800; }
.result-beat.record { color: var(--green); }

.stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin: 20px 0;
}
.stat {
    background: rgba(255, 255, 255, 0.045);
    border: 1px solid rgba(150, 170, 230, 0.12);
    border-radius: 12px;
    padding: 9px 4px;
}
.stat .v { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat .k { font-size: 9.5px; letter-spacing: 0.1em; color: var(--muted); text-transform: uppercase; margin-top: 2px; }

.result-actions { display: grid; gap: 9px; margin-top: 4px; }
.result-wait { margin-top: 12px; font-size: 13px; color: var(--muted); font-weight: 700; }

.goodbye-msg { margin: 16px 0 20px; color: #c8d0e2; }

/* ---------------- Countdown + toast ---------------- */
.countdown {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(64px, 22vw, 130px);
    font-weight: 900;
    color: #fff;
    text-shadow: 0 0 40px rgba(37, 230, 255, 0.7), 0 0 80px rgba(255, 61, 240, 0.4);
    pointer-events: none;
    z-index: 6;
}
.countdown.go { color: var(--green); text-shadow: 0 0 50px rgba(138, 255, 193, 0.9); }
.countdown .pop { animation: pop 0.4s ease; display: inline-block; }

.wait-banner {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 9px;
    background: rgba(10, 14, 26, 0.9);
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    padding: 7px 8px 7px 14px;
    font-size: 12.5px;
    font-weight: 700;
    color: var(--text);
    z-index: 6;
    white-space: nowrap;
    max-width: 94%;
}
.wait-banner .wb-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--cyan);
    box-shadow: 0 0 10px var(--cyan);
    animation: blink 1.2s infinite;
}

.toast {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    background: rgba(10, 14, 26, 0.92);
    border: 1px solid var(--panel-border);
    color: var(--text);
    font-size: 12.5px;
    font-weight: 700;
    padding: 9px 14px;
    border-radius: 999px;
    z-index: 7;
    white-space: nowrap;
}

/* ---------------- Controls ---------------- */
#controls {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
}
.reactions { display: flex; gap: 7px; }
.reaction {
    appearance: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: 1px solid rgba(150, 170, 230, 0.2);
    background: rgba(255, 255, 255, 0.05);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    transition: transform 0.08s ease, background 0.15s ease;
}
.reaction:active { transform: scale(0.9); }
.reaction:hover { background: rgba(255, 255, 255, 0.12); }

@keyframes blink { 0%, 100% { opacity: 0.2; } 50% { opacity: 1; } }
@keyframes pulseText { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
@keyframes rise { from { opacity: 0; transform: translateY(10px) scale(0.98); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(0.5); opacity: 0; } 60% { transform: scale(1.15); } 100% { transform: scale(1); opacity: 1; } }

@media (max-width: 560px) {
    #hud { gap: 6px; padding: 2px; }
    .chainbar { max-width: 90px; }
    .panel { padding: 22px 16px; }
    .stats { gap: 6px; }
    .stat .v { font-size: 16px; }
    .reaction { width: 38px; height: 38px; font-size: 18px; }
    #app { padding-left: 6px; padding-right: 6px; }
}

@media (max-height: 620px) {
    .howto { display: none; }
    .tagline { margin-bottom: 10px; }
    .stats { margin: 12px 0; }
    .panel { padding: 18px 16px; }
}
