:root {
    --amarillo: #ffd400;
    --rojo: #e8202a;
    --azul: #1660d8;
    --tinta: #0b0f17;
    --blanco: #ffffff;
    --vidrio: rgba(8, 14, 28, 0.78);
    --hud: "Russo One", "Arial Black", system-ui, sans-serif;
    --titulo: "Bungee", "Russo One", "Arial Black", sans-serif;
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    overflow-x: clip;
    background: #0b0f17;
    color: var(--blanco);
    font-family: var(--hud);
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
}

#gl {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}

button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* Texto de fichín: relleno fuerte, borde negro y sombra corrida */
.arcade, .reloj b, .caja b, .velo b, .mensaje, .aviso, .grande, .logo span {
    font-style: italic;
    paint-order: stroke fill;
    -webkit-text-stroke: 0.14em var(--tinta);
    text-shadow: 0.06em 0.08em 0 var(--tinta);
}

/* ——— HUD ——— */
#hud {
    position: fixed;
    inset: 0;
    pointer-events: none;
    padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.hud-arriba {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: start;
    gap: 12px;
}

.hud-arriba small, .hud-abajo small, .crono small {
    display: block;
    font-size: 13px;
    letter-spacing: 0.12em;
    color: var(--amarillo);
    text-shadow: 2px 2px 0 var(--tinta);
}

.caja b {
    font-size: clamp(34px, 6vw, 58px);
    line-height: 1;
    color: var(--blanco);
}
.caja b small { font-size: 0.5em; color: var(--amarillo); }
.caja b sup { font-size: 0.5em; vertical-align: top; }
.caja.derecha { text-align: right; }

.reloj { text-align: center; }
.reloj small { text-align: center; font-size: 15px; }
.reloj b {
    display: block;
    font-size: clamp(64px, 12vw, 120px);
    line-height: 0.95;
    color: var(--amarillo);
    min-width: 2ch;
}
.reloj b.poco { color: var(--rojo); animation: latido 0.5s steps(2) infinite; }

.hud-costado {
    position: absolute;
    right: max(16px, env(safe-area-inset-right));
    top: clamp(90px, 15vw, 128px);
    display: grid;
    justify-items: end;
    gap: 6px;
}
#mapa { width: clamp(110px, 16vw, 170px); height: auto; }
.crono { text-align: right; }
.crono span { font-size: 20px; text-shadow: 2px 2px 0 var(--tinta); }
.vueltas { list-style: none; margin: 0; padding: 0; font-size: 15px; text-align: right; text-shadow: 2px 2px 0 var(--tinta); }
.vueltas span { color: var(--amarillo); margin-right: 8px; }
.vueltas .mejor { color: #7dff9a; }

.hud-abajo {
    position: absolute;
    left: max(16px, env(safe-area-inset-left));
    right: max(16px, env(safe-area-inset-right));
    bottom: max(14px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 16px;
}
.turbo { max-width: 280px; }
.barra, .rpm {
    height: 16px;
    border: 3px solid var(--tinta);
    background: rgba(0, 0, 0, 0.45);
    transform: skewX(-18deg);
    overflow: hidden;
}
.barra i, .rpm i {
    display: block;
    height: 100%;
    transform-origin: left;
    background: linear-gradient(90deg, #ff6a00, var(--amarillo) 60%, #fff6a8);
}
.barra.baja i { background: var(--rojo); }
.velo { text-align: right; min-width: 170px; }
.velo b { display: inline-block; font-size: clamp(48px, 8vw, 86px); line-height: 1; color: var(--blanco); }
.velo small { display: inline-block; margin-left: 6px; font-size: 18px; }
.rpm { margin-top: 4px; }
.rpm i { background: linear-gradient(90deg, #20c46a, var(--amarillo) 65%, var(--rojo)); }

.mensaje {
    position: absolute;
    left: 50%;
    top: 36%;
    transform: translate(-50%, -50%) scale(0.6);
    opacity: 0;
    text-align: center;
    font-size: clamp(30px, 7vw, 64px);
    line-height: 1.05;
    color: var(--amarillo);
    white-space: nowrap;
    transition: opacity 0.15s, transform 0.25s cubic-bezier(.2, 1.6, .4, 1);
}
.mensaje.visible { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.mensaje small { display: block; font-size: 0.45em; color: var(--blanco); margin-top: 6px; }
.mensaje.grande { font-size: clamp(40px, 10vw, 96px); }
.mensaje.cuenta { font-size: clamp(90px, 22vw, 200px); color: var(--blanco); }
.mensaje.dorado { color: var(--amarillo); }
.mensaje.rojo { color: var(--rojo); }

.aviso {
    position: absolute;
    left: 50%;
    top: 22%;
    transform: translateX(-50%);
    font-size: clamp(26px, 5vw, 46px);
    color: var(--rojo);
    opacity: 0;
    white-space: nowrap;
}
.aviso.visible { animation: latido 0.36s steps(2) infinite; }

@keyframes latido { 0% { opacity: 1; } 100% { opacity: 0; } }

/* ——— Controles táctiles ——— */
.tactil { display: none; }
body.es-tactil.jugando .tactil {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    justify-content: space-between;
    padding: 0 max(12px, env(safe-area-inset-right)) max(70px, calc(env(safe-area-inset-bottom) + 64px)) max(12px, env(safe-area-inset-left));
    pointer-events: none;
}
.tactil-izq, .tactil-der { display: flex; gap: 12px; pointer-events: auto; }
.tactil button {
    width: 76px;
    height: 76px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.55);
    background: rgba(8, 14, 28, 0.45);
    font-size: 26px;
    touch-action: none;
}
.tactil .freno, .tactil .volar { font-size: 13px; letter-spacing: 0.05em; }
.tactil .volar { background: rgba(255, 106, 0, 0.6); border-color: var(--amarillo); width: 92px; height: 92px; }
body.es-tactil .hud-abajo { bottom: max(170px, calc(env(safe-area-inset-bottom) + 166px)); }

/* ——— Pantallas ——— */
.pantalla {
    position: fixed;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 16px;
    overflow-y: auto;
    touch-action: pan-y;
    background: radial-gradient(ellipse at center, rgba(5, 10, 25, 0.15), rgba(5, 10, 25, 0.7));
}
.panel {
    width: min(760px, 100%);
    min-width: 0;
    padding: 24px clamp(16px, 4vw, 36px);
    border: 3px solid var(--amarillo);
    border-radius: 6px;
    background: var(--vidrio);
    box-shadow: 8px 8px 0 var(--tinta);
    text-align: center;
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
}
.panel.chico { width: min(380px, 100%); display: grid; gap: 14px; }

.logo { margin: 0 0 18px; line-height: 1; }
.logo-auto { width: 120px; height: 48px; color: var(--rojo); display: block; margin: 0 auto 6px; }
.logo span {
    display: block;
    font-family: var(--titulo);
    font-size: clamp(44px, 11vw, 92px);
    color: var(--amarillo);
    letter-spacing: 0.02em;
    transform: skewX(-8deg);
}
.logo em {
    display: block;
    margin-top: 10px;
    font-size: clamp(14px, 3vw, 18px);
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: #9fd6ff;
}

.grande { margin: 0 0 12px; font-size: clamp(40px, 9vw, 72px); color: var(--amarillo); line-height: 1.05; }
#fin.gano .grande { animation: latido 0.5s steps(2) 6; }

.elegir small { display: block; font-size: 13px; letter-spacing: 0.15em; color: #9fd6ff; margin-bottom: 8px; }
.colores { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-bottom: 18px; }
.colores button {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.35);
    background: var(--c);
    transition: transform 0.15s;
}
.colores button.activo { border-color: var(--amarillo); transform: scale(1.2); box-shadow: 0 0 0 3px var(--tinta); }

.boton-grande {
    display: inline-block;
    padding: 14px 34px;
    font-family: var(--titulo);
    font-size: clamp(22px, 5vw, 32px);
    color: var(--tinta);
    background: linear-gradient(180deg, #fff38a, var(--amarillo) 55%, #ffaa00);
    border: 3px solid var(--tinta);
    border-radius: 4px;
    box-shadow: 5px 5px 0 var(--tinta);
    transform: skewX(-8deg);
    transition: transform 0.1s, box-shadow 0.1s;
}
.boton-grande:hover, .boton-grande:focus-visible { transform: skewX(-8deg) translate(-2px, -2px); box-shadow: 7px 7px 0 var(--tinta); outline: none; }
.boton-grande:active { transform: skewX(-8deg) translate(3px, 3px); box-shadow: 2px 2px 0 var(--tinta); }
.boton {
    padding: 10px 20px;
    background: rgba(255, 255, 255, 0.08);
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-radius: 4px;
}
.boton:hover { border-color: var(--amarillo); color: var(--amarillo); }
.link { background: none; border: 0; padding: 0; color: #9fd6ff; text-decoration: underline; }

.parpadea { margin: 12px 0 18px; font-size: 13px; letter-spacing: 0.2em; color: #9fd6ff; animation: latido 1s steps(2) infinite; }

.info {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 20px;
    text-align: left;
}
.info > * { min-width: 0; }
.controles { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; font-family: system-ui, sans-serif; font-size: 14px; color: #dbe7ff; }
.controles b { color: var(--amarillo); font-weight: 600; }
kbd {
    display: inline-block;
    min-width: 26px;
    padding: 2px 6px;
    margin-right: 4px;
    font-family: var(--hud);
    font-size: 12px;
    text-align: center;
    color: var(--tinta);
    background: #e8eefc;
    border-radius: 4px;
    box-shadow: 0 2px 0 #8a96b0;
}
body.es-tactil .controles { display: none; }

.ranking h2 { margin: 0 0 8px; font-size: 16px; letter-spacing: 0.15em; color: var(--amarillo); }
.ranking ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; }
.ranking li {
    display: grid;
    grid-template-columns: 22px 12px minmax(0, 1fr) auto 30px;
    align-items: center;
    gap: 8px;
    padding: 3px 8px;
    font-size: 15px;
    background: rgba(255, 255, 255, 0.05);
}
.ranking li:nth-child(1) { color: var(--amarillo); }
.ranking li.yo { background: rgba(255, 212, 0, 0.25); }
.ranking li.vacio { display: block; color: #9fb3d6; font-family: system-ui, sans-serif; font-size: 14px; }
.ranking .n { color: #9fb3d6; }
.ranking i { width: 12px; height: 12px; border-radius: 50%; background: var(--c); border: 1px solid #000; }
.ranking .p { color: #9fb3d6; text-align: right; }
#fin .ranking { max-width: 380px; margin: 16px auto; text-align: left; }

.pie { margin: 18px 0 0; font-family: system-ui, sans-serif; font-size: 13px; color: #9fb3d6; }

.fin-datos { display: flex; justify-content: center; gap: 28px; flex-wrap: wrap; margin-bottom: 12px; }
.fin-datos small { display: block; font-size: 12px; letter-spacing: 0.15em; color: #9fd6ff; }
.fin-datos b { font-size: 30px; }
.fin-datos p { font-family: system-ui, sans-serif; color: #dbe7ff; max-width: 440px; margin: 0 auto; }

.marca { display: flex; justify-content: center; align-items: center; gap: 10px; flex-wrap: wrap; }
.marca label { font-size: 13px; letter-spacing: 0.15em; color: var(--amarillo); }
.marca input {
    width: 4.2ch;
    padding: 4px 8px;
    font-family: var(--titulo);
    font-size: 30px;
    letter-spacing: 0.15em;
    text-align: center;
    text-transform: uppercase;
    color: var(--amarillo);
    background: var(--tinta);
    border: 3px solid var(--amarillo);
    border-radius: 4px;
    user-select: text;
}
.marca-msg { min-height: 1.2em; margin: 8px 0 0; color: #7dff9a; font-family: system-ui, sans-serif; }
.acciones { display: flex; justify-content: center; align-items: center; gap: 14px; flex-wrap: wrap; }

.sin-js { position: fixed; inset: 0; display: grid; place-items: center; font-family: system-ui, sans-serif; }

@media (max-width: 640px) {
    .info { grid-template-columns: minmax(0, 1fr); }
    .hud-costado { top: 96px; }
    .velo { min-width: 0; }
    .crono, .vueltas { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    .parpadea, .aviso.visible, .reloj b.poco { animation: none; }
}

/* ——— Multijugador ——— */
.modos { display: flex; justify-content: center; gap: 16px; flex-wrap: wrap; }
.boton-grande.azul { color: var(--blanco); background: linear-gradient(180deg, #5aa2ff, var(--azul) 55%, #0c3f9c); text-shadow: 2px 2px 0 var(--tinta); }
.sala-entrar, .sala-lobby { display: grid; justify-items: center; gap: 14px; }
.campo { display: grid; gap: 6px; }
.campo small { font-size: 13px; letter-spacing: 0.15em; color: var(--amarillo); }
.campo input, .unirse input {
    width: min(280px, 100%);
    padding: 8px 12px;
    font-family: var(--hud);
    font-size: 22px;
    text-align: center;
    color: var(--blanco);
    background: var(--tinta);
    border: 3px solid var(--amarillo);
    border-radius: 4px;
    user-select: text;
}
.unirse { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.unirse input { width: 7ch; letter-spacing: 0.2em; text-transform: uppercase; }
.o { margin: 0; font-family: system-ui, sans-serif; color: #9fb3d6; }
.codigo-sala { margin: 0; font-size: 18px; letter-spacing: 0.15em; color: #9fd6ff; }
.codigo-sala b { display: block; font-family: var(--titulo); font-size: clamp(44px, 12vw, 72px); color: var(--amarillo); letter-spacing: 0.25em; text-shadow: 4px 4px 0 var(--tinta); }
.pilotos { list-style: none; margin: 0; padding: 0; width: min(380px, 100%); display: grid; gap: 4px; }
.pilotos li { display: flex; align-items: center; gap: 10px; padding: 6px 10px; background: rgba(255, 255, 255, 0.06); text-align: left; min-width: 0; }
.pilotos i { flex: none; width: 16px; height: 16px; border-radius: 50%; background: var(--c); border: 2px solid #000; }
.pilotos b { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pilotos span { font-family: system-ui, sans-serif; font-size: 12px; padding: 2px 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.15); }
.pilotos .anf { background: var(--amarillo); color: var(--tinta); }
.cupo { margin: 0; font-family: system-ui, sans-serif; font-size: 14px; color: #9fb3d6; }
.check { display: flex; align-items: center; gap: 8px; font-family: system-ui, sans-serif; font-size: 14px; color: #dbe7ff; }
.check input { width: 18px; height: 18px; accent-color: var(--amarillo); }
#sala .boton { margin-top: 4px; }
.posiciones li { grid-template-columns: 30px 12px minmax(0, 1fr) auto; }
.posiciones b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
