* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    background-color: #090d16;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    font-family: 'Courier New', Courier, monospace;
}

#game-container {
    position: relative;
    width: min(100vw, calc(100dvh * 0.5625));
    height: min(100dvh, calc(100vw * 1.7778));
    aspect-ratio: 9 / 16;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #0f172a;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.95);
    overflow: hidden;
    touch-action: none;
}

canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    display: block;
    touch-action: none;
}

.hidden {
    display: none !important;
}

/* --- BARRA SUPERIOR DE UTILIDADES (SONIDO, GIRO Y PAUSA) --- */
#top-utility-bar {
    position: absolute;
    top: calc(env(safe-area-inset-top, 0px) + 106px);
    left: 8px;
    right: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    pointer-events: none;
    z-index: 16;
}

.util-btn {
    pointer-events: auto;
    background: rgba(15, 23, 42, 0.78);
    border: 1.5px solid rgba(255, 255, 255, 0.4);
    color: #f8fafc;
    font-family: inherit;
    font-weight: 900;
    font-size: 11px;
    padding: 6px 10px;
    border-radius: 8px;
    cursor: pointer;
    touch-action: manipulation;
}

.util-btn:active {
    transform: scale(0.95);
}

.tilt-btn.active {
    background: rgba(22, 163, 74, 0.9);
    border-color: #86efac;
    color: #ffffff;
}

.pause-util-btn {
    background: rgba(30, 58, 138, 0.85);
    border-color: #60a5fa;
    color: #facc15;
}

/* --- BOTÓN FLOTANTE DE SALUDO ENCIMA DE PEDALES --- */
.floating-saludo-btn {
    position: absolute;
    right: 10px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 90px);
    width: 146px;
    text-align: center;
    background: rgba(234, 179, 8, 0.92);
    border: 2.5px solid #fef08a;
    color: #0f172a;
    font-family: inherit;
    font-weight: 900;
    font-size: 12px;
    padding: 11px 14px;
    border-radius: 12px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.55);
    z-index: 16;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform 0.1s ease, background 0.15s ease;
}

.floating-saludo-btn:active {
    transform: scale(0.93);
    background: #fde047;
}

.floating-saludo-btn.highlight {
    background: #4ade80;
    border-color: #ffffff;
    color: #052e16;
    animation: pulseSaludo 0.55s infinite alternate;
}

@keyframes pulseSaludo {
    from { transform: scale(1); }
    to { transform: scale(1.08); }
}

/* --- PANTALLA DE ACCESO UNIFICADO (LOGIN / REGISTRO) --- */
#auth-overlay {
    position: absolute;
    inset: 0;
    background: rgba(9, 13, 22, 0.92);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 16px;
    z-index: 30;
}

.auth-card {
    width: 100%;
    max-width: 340px;
    background: #0f172a;
    border: 3px solid #facc15;
    border-radius: 14px;
    padding: 18px 16px;
    color: #f8fafc;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.85);
}

.auth-badge {
    display: inline-block;
    background: #dc2626;
    color: #fff;
    font-size: 10px;
    font-weight: 900;
    padding: 3px 8px;
    border-radius: 6px;
    letter-spacing: 1px;
    margin-bottom: 6px;
}

.auth-card h2 {
    font-size: 21px;
    color: #facc15;
    margin-bottom: 8px;
    text-shadow: 2px 2px 0 #000;
}

.auth-hint {
    font-size: 11px;
    line-height: 1.35;
    color: #cbd5e1;
    margin-bottom: 12px;
}

#auth-form {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

#auth-form label {
    font-size: 11px;
    font-weight: 900;
    color: #38bdf8;
    margin-top: 2px;
}

#auth-form input {
    width: 100%;
    padding: 10px;
    border-radius: 8px;
    border: 2px solid #334155;
    background: #1e293b;
    color: #ffffff;
    font-family: inherit;
    font-size: 16px;
    font-weight: 700;
    outline: none;
    user-select: text;
    -webkit-user-select: text;
    touch-action: auto;
}

#auth-form input:focus {
    border-color: #facc15;
}

#auth-error {
    min-height: 16px;
    font-size: 11px;
    font-weight: 900;
    color: #f87171;
    margin-top: 2px;
}

#btn-auth-submit {
    margin-top: 4px;
    padding: 12px;
    border-radius: 10px;
    border: 2px solid #86efac;
    background: #16a34a;
    color: #ffffff;
    font-family: inherit;
    font-size: 13px;
    font-weight: 900;
    cursor: pointer;
}

#btn-auth-submit:disabled {
    opacity: 0.6;
    cursor: wait;
}

.auth-extra-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.auth-secondary-btn {
    flex: 1;
    padding: 10px 6px;
    border-radius: 8px;
    font-family: inherit;
    font-size: 11px;
    font-weight: 900;
    cursor: pointer;
}

.info-btn {
    background: #1e3a8a;
    border: 2px solid #60a5fa;
    color: #facc15;
}

.rank-btn {
    background: #3f2e00;
    border: 2px solid #facc15;
    color: #fde047;
}

/* --- PANTALLA DE PAUSA + INFO / INSTRUCCIONES --- */
#pause-overlay {
    position: absolute;
    inset: 0;
    background: rgba(9, 13, 22, 0.93);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 14px;
    z-index: 35;
}

.pause-card {
    width: 100%;
    max-width: 348px;
    max-height: 92vh;
    overflow-y: auto;
    touch-action: pan-y;
    background: #0f172a;
    border: 3px solid #60a5fa;
    border-radius: 14px;
    padding: 14px;
    color: #f8fafc;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.9);
}

.pause-badge {
    display: inline-block;
    background: #1d4ed8;
    color: #facc15;
    font-size: 10px;
    font-weight: 900;
    padding: 3px 8px;
    border-radius: 6px;
    margin-bottom: 5px;
}

.pause-card h3 {
    font-size: 16px;
    color: #facc15;
    margin-bottom: 10px;
}

.info-section {
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 8px;
    padding: 8px 10px;
    margin-bottom: 10px;
}

.info-title {
    font-size: 11px;
    font-weight: 900;
    color: #38bdf8;
    margin-bottom: 5px;
}

.info-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 5px;
    font-size: 11px;
    line-height: 1.3;
    color: #e2e8f0;
}

.info-list strong {
    color: #facc15;
}

#btn-resume-game {
    width: 100%;
    padding: 12px;
    border-radius: 10px;
    border: 2px solid #86efac;
    background: #16a34a;
    color: #ffffff;
    font-family: inherit;
    font-size: 13px;
    font-weight: 900;
    cursor: pointer;
}

/* --- OVERLAY DE RANKING OFICIAL TOP 30 --- */
#ranking-overlay {
    position: absolute;
    inset: 0;
    background: rgba(9, 13, 22, 0.94);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 14px;
    z-index: 32;
}

.ranking-card {
    width: 100%;
    max-width: 348px;
    max-height: 92vh;
    display: flex;
    flex-direction: column;
    background: #0f172a;
    border: 3px solid #facc15;
    border-radius: 14px;
    padding: 14px;
    color: #f8fafc;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.9);
}

.end-title {
    font-size: 15px;
    font-weight: 900;
    text-align: center;
    color: #ef4444;
    margin-bottom: 4px;
}

.end-title.win {
    color: #facc15;
}

.end-summary {
    font-size: 11px;
    font-weight: 700;
    text-align: center;
    color: #cbd5e1;
    margin-bottom: 6px;
}

.not-top-banner {
    background: rgba(220, 38, 38, 0.25);
    border: 1px solid #f87171;
    color: #fca5a5;
    font-size: 11px;
    font-weight: 900;
    text-align: center;
    padding: 4px 6px;
    border-radius: 6px;
    margin-bottom: 6px;
}

.ranking-header {
    font-size: 12px;
    font-weight: 900;
    text-align: center;
    color: #facc15;
    padding: 5px 0;
    border-top: 1px solid #334155;
    border-bottom: 1px solid #334155;
    margin-bottom: 6px;
}

.ranking-list {
    flex: 1;
    overflow-y: auto;
    touch-action: pan-y;
    max-height: 265px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding-right: 2px;
    margin-bottom: 10px;
}

.rank-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #1e293b;
    padding: 5px 8px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 700;
    color: #e2e8f0;
}

.rank-row.top3 {
    background: rgba(250, 204, 21, 0.16);
    border: 1px solid rgba(250, 204, 21, 0.45);
    color: #fef08a;
}

.rank-row.me {
    background: rgba(34, 197, 94, 0.25);
    border: 1px solid #4ade80;
    color: #ffffff;
}

.rank-pos {
    width: 34px;
    font-weight: 900;
    color: #facc15;
}

.rank-nick {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.rank-score {
    font-weight: 900;
    color: #38bdf8;
    margin-left: 6px;
}

.rank-empty {
    text-align: center;
    font-size: 11px;
    color: #94a3b8;
    padding: 16px 6px;
}

#btn-play-again {
    width: 100%;
    padding: 12px 8px;
    border-radius: 10px;
    border: 2px solid #fef08a;
    background: #eab308;
    color: #0f172a;
    font-family: inherit;
    font-size: 12px;
    font-weight: 900;
    cursor: pointer;
}

/* --- CONTROLES TÁCTILES ERGONÓMICOS PARA MÓVIL --- */
#touch-controls {
    position: absolute;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
    left: 10px;
    right: 10px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
    z-index: 15;
}

.control-group {
    display: flex;
    gap: 10px;
    pointer-events: auto;
    touch-action: none;
}

.ctrl-btn {
    background: rgba(15, 23, 42, 0.72);
    border: 2.5px solid rgba(255, 255, 255, 0.5);
    color: #fff;
    font-family: inherit;
    font-weight: 900;
    border-radius: 16px;
    height: 68px;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
    cursor: pointer;
    touch-action: none;
}

.ctrl-btn.pressed,
.ctrl-btn:active {
    transform: scale(0.94);
    filter: brightness(1.2);
}

.steer-btn {
    width: 68px;
    font-size: 24px;
    background: rgba(15, 23, 42, 0.76);
}

.steer-btn.pressed {
    background: rgba(56, 189, 248, 0.85);
    border-color: #ffffff;
    color: #0f172a;
}

.brake-btn {
    width: 62px;
    background: rgba(220, 38, 38, 0.78);
    border-color: #fca5a5;
    font-size: 12px;
}

.brake-btn.pressed {
    background: rgba(239, 68, 68, 0.95);
}

.gas-btn {
    width: 74px;
    background: rgba(22, 163, 74, 0.80);
    border-color: #86efac;
    font-size: 15px;
}

.gas-btn.pressed {
    background: rgba(34, 197, 94, 0.96);
}

.auth-logo-header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
}

.auth-game-logo {
    display: block;
    max-width: 85%;
    max-height: 110px;
    width: auto;
    height: auto;
    object-fit: contain;
    filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.55));
}

/* Barra superior libre para no bloquear clics en el canvas */
#top-utility-bar {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 25;
}

/* Botón de sonido más abajo a la izquierda, debajo de ONLINE: 1 */
#btn-mute.util-btn {
    position: absolute;
    top: 186px;
    left: 8px;
    pointer-events: auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    min-width: 96px;
    height: 32px;
    padding: 0 10px;
    border-radius: 8px;
    border: 2px solid #38bdf8;
    background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
    color: #f8fafc;
    font-family: "Courier New", monospace;
    font-size: 11px;
    font-weight: 900;
    letter-spacing: 0.4px;
    box-shadow: 0 3px 0 #020617, 0 4px 10px rgba(0, 0, 0, 0.45);
    cursor: pointer;
}

#btn-mute.util-btn.muted {
    border-color: #ef4444;
    background: linear-gradient(180deg, #450a0a 0%, #7f1d1d 100%);
    color: #fecaca;
}

#btn-mute.util-btn:active {
    transform: translateY(2px);
    box-shadow: 0 1px 0 #020617;
}

/* Botón de pausa mejorado a la derecha, debajo de TRAMO */
#btn-pause-info.pause-util-btn {
    position: absolute;
    top: 120px;
    right: 8px;
    pointer-events: auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: 108px;
    height: 34px;
    padding: 0 12px;
    border-radius: 8px;
    border: 2px solid #facc15;
    background: linear-gradient(180deg, #1d4ed8 0%, #1e3a8a 100%);
    color: #ffffff;
    font-family: "Courier New", monospace;
    font-size: 12px;
    font-weight: 900;
    letter-spacing: 0.6px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.75);
    box-shadow: 0 3px 0 #0f172a, 0 4px 10px rgba(0, 0, 0, 0.45);
    cursor: pointer;
}

#btn-pause-info.pause-util-btn:active {
    transform: translateY(2px);
    box-shadow: 0 1px 0 #0f172a;
}

