:root {
    --bg-color: #0f172a;
    --text-color: #f8fafc;
    --primary-color: #38bdf8;
    --secondary-color: #64748b;
    --accent-color: #f472b6;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    --font-main: 'Outfit', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    overscroll-behavior: none;
}

#app {
    position: relative;
    width: 100%;
    height: 100%;
}

canvas {
    display: block;
    touch-action: none;
    width: 100%;
    height: 100%;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    /* Let clicks pass through to canvas when not on UI elements */
}

.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg-color);
    transition: opacity 0.3s ease;
    pointer-events: auto;
}

.screen.hidden {
    opacity: 0;
    pointer-events: none;
    z-index: -1;
}

.screen.active {
    opacity: 1;
    z-index: 10;
}

.screen.overlay {
    background: transparent;
    justify-content: space-between;
    pointer-events: none;
    /* HUD container shouldn't block canvas */
}

.screen.overlay>* {
    pointer-events: auto;
    /* HUD elements should be clickable */
}

/* Typography */
h1.title {
    font-size: clamp(3rem, 14vw, 5rem);
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 3rem;
    text-shadow: 0 0 30px rgba(56, 189, 248, 0.3);
}

h2 {
    font-size: clamp(1.6rem, 6vw, 2.5rem);
    margin-bottom: 2rem;
}

/* Buttons */
button {
    font-family: var(--font-main);
    cursor: pointer;
    border: none;
    outline: none;
    transition: all 0.2s ease;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), #0ea5e9);
    color: white;
    padding: 1rem 3rem;
    font-size: 1.5rem;
    border-radius: 50px;
    font-weight: 700;
    box-shadow: 0 4px 15px rgba(14, 165, 233, 0.4);
    margin: 0.5rem;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(14, 165, 233, 0.6);
}

.btn-primary:active {
    transform: translateY(1px);
}

.btn-secondary {
    background: transparent;
    border: 2px solid var(--secondary-color);
    color: var(--secondary-color);
    padding: 0.8rem 2.5rem;
    font-size: 1.2rem;
    border-radius: 50px;
    font-weight: 500;
    margin: 0.5rem;
}

.btn-secondary:hover {
    border-color: var(--text-color);
    color: var(--text-color);
    background: rgba(255, 255, 255, 0.05);
}

.btn-text {
    background: transparent;
    color: var(--secondary-color);
    font-size: 1rem;
    margin-top: 2rem;
}

.btn-text:hover {
    color: var(--text-color);
    text-decoration: underline;
}

.icon-btn {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-color);
    width: 50px;
    height: 50px;
    border-radius: 12px;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Menu Layout */
.menu-options {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Level Select */
.levels-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 1rem;
    width: 80%;
    max-width: 800px;
    max-height: 60vh;
    max-height: 60dvh;
    overflow-y: auto;
    padding: 1rem;
}

.level-btn {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-color);
    aspect-ratio: 1;
    border-radius: 12px;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    transition: all 0.2s;
}
.level-btn small { font-size: 0.6rem; opacity: 0.8; margin-top: -0.2rem; }
.level-btn.done { border-color: #22c55e; }

.level-btn:hover {
    background: var(--primary-color);
    color: #000;
    transform: scale(1.05);
}

/* HUD */
.screen.overlay { --item: 80px; --panel-w: 160px; }

.hud-top {
    width: 100%;
    padding: 0.8rem 1.2rem;
    padding-right: calc(var(--panel-w) + 1.2rem);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.6rem;
}

.level-info {
    font-size: 1.3rem;
    font-weight: 700;
    white-space: nowrap;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.moves { font-weight: 400; font-size: 1rem; color: var(--secondary-color); margin-left: 0.3rem; }
.hud-actions { display: flex; gap: 0.5rem; }
.hud-top .icon-btn { width: 44px; height: 44px; font-size: 1.3rem; }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn.apagado { opacity: 0.55; }
.hidden-btn { display: none; }

.commutators-panel {
    position: absolute;
    right: 0; top: 0; bottom: 0;
    width: var(--panel-w);
    padding: 1rem 0.75rem;
    background: linear-gradient(to left, rgba(15, 23, 42, 0.95), rgba(15, 23, 42, 0.4));
    border-left: 1px solid var(--glass-border);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    pointer-events: auto;
}

.ic { width: 1.3em; height: 1.3em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.icon-btn .ic { width: 55%; height: 55%; }

/* Botones del panel: solo iconos, en cuadrícula de 2 columnas */
.panel-actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.4rem; width: 100%; }
.panel-actions button { display: grid; place-items: center; min-height: 44px; padding: 0; border-radius: 10px; font-size: 1rem; }
.btn-apply .ic { stroke-width: 3; }
.btn-rot { background: var(--glass-bg); border: 1px solid var(--glass-border); color: var(--text-color); }

.btn-apply {
    background: linear-gradient(135deg, var(--primary-color), #0ea5e9);
    color: #04121f;
    font-weight: 700;
    font-size: 1.1rem;
    min-height: 48px;
    border-radius: 12px;
}
.btn-apply:disabled { opacity: 0.35; cursor: default; }
/* con ratón no hace falta: un clic aplica */
@media (hover: hover) and (pointer: fine) { .btn-apply { display: none; } .btn-inverse { grid-column: 1 / -1; } }

.btn-inverse {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-color);
    min-height: 40px;
    padding: 0.4rem;
    border-radius: 10px;
    font-size: 0.9rem;
}
.btn-inverse[aria-pressed="true"] {
    border-color: var(--accent-color);
    color: var(--accent-color);
    box-shadow: 0 0 10px rgba(244, 114, 182, 0.4);
}

.commutators-list {
    display: grid;
    grid-template-columns: var(--item);
    gap: 0.9rem;
    justify-content: center;
    overflow-y: auto;
    max-height: 70vh;
    padding: 4px;
}

.commutator-item {
    width: var(--item);
    height: var(--item);
    background: var(--glass-bg);
    border: 2px solid var(--glass-border);
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.2s, box-shadow 0.2s;
    flex-shrink: 0;
}

.commutator-item.selected {
    border-color: var(--primary-color);
    box-shadow: 0 0 15px var(--primary-color);
}

.commutator-item canvas { width: 100%; height: 100%; border-radius: 10px; }

/* Móvil en horizontal */
@media (max-height: 500px) and (min-aspect-ratio: 1/1) {
    .screen.overlay { --item: 52px; }
    .hud-top { padding: 0.4rem 0.8rem; padding-right: calc(var(--panel-w) + 0.8rem); }
    .commutators-panel { padding: 0.5rem; gap: 0.5rem; }
    .commutators-list { grid-template-columns: repeat(2, var(--item)); gap: 0.5rem; }
    .panel-actions button { min-height: 36px; }
}

/* Móvil en vertical: conmutadores en una barra inferior */
@media (max-aspect-ratio: 1/1) {
    .screen.overlay { --item: 54px; --panel-w: 0px; }
    .hud-top { padding: 0.5rem 0.75rem; gap: 0.4rem; }
    .hud-logo { font-size: 1.05rem; }
    .hud-top .offline-tag { display: none; }      /* en móvil vertical no cabe; al ganar se avisa de que no puntúa */
    .level-info { font-size: 1rem; }
    .hud-top .icon-btn { width: 36px; height: 36px; font-size: 1.1rem; }
    .hud-actions { gap: 0.35rem; }
    .mov-txt { display: none; }
    #btn-full { display: none; }
    #btn-music { display: none; }      /* en móvil vertical no cabe: se activa desde el menú */
    .commutators-panel {
        top: auto; left: 0; right: 0; width: auto;
        flex-direction: row;
        padding: 0.4rem 0.75rem calc(0.4rem + env(safe-area-inset-bottom));
        border-left: none;
        border-top: 1px solid var(--glass-border);
        background: rgba(15, 23, 42, 0.97);
        gap: 0.75rem;
    }
    .panel-actions { order: 2; width: auto; grid-template-columns: repeat(2, 36px); gap: 4px; }
    .panel-actions button { min-height: 36px; width: 36px; }
    .commutators-list {
        display: flex; flex-direction: row;
        overflow-x: auto; overflow-y: hidden;
        max-height: none; gap: 0.5rem; justify-content: safe center; min-width: 0;
    }
}

/* Modal */
.modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(5px);
    pointer-events: auto;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: #1e293b;
    padding: clamp(1.2rem, 5vw, 3rem);
    border-radius: 24px;
    width: min(92vw, 500px);
    max-height: 90vh;
    max-height: 90dvh;
    overflow-y: auto;
    text-align: center;
    border: 1px solid var(--glass-border);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.modal-content h2 {
    color: var(--primary-color);
}

.modal-content p {
    margin-bottom: 1.5rem;
    line-height: 1.6;
    color: #cbd5e1;
}

/* Botón en general */
#win-modal .btn-secondary { display: block; margin: 0.5rem auto 0; }

#btn-home {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    width: auto;
    height: auto;
}

/* Logo dentro del HUD (más pequeño que el título principal) */
.hud-logo {
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 0 30px rgba(56, 189, 248, 0.3);

}

/* Puntuaciones */
.offline-tag { font-size: 0.7rem; font-weight: 400; color: #fbbf24; border: 1px solid #fbbf24; border-radius: 6px; padding: 0 0.3rem; vertical-align: middle; }
.win-score { min-height: 1.4em; font-size: 0.95rem; }
.name-box { display: flex; gap: 0.5rem; justify-content: center; flex-wrap: wrap; margin: 0.6rem 0 1rem; }
.name-box.hidden { display: none; }
.name-box input, .modal-content select {
    background: #0f172a; color: var(--text-color); border: 1px solid var(--glass-border);
    border-radius: 10px; padding: 0.6rem 0.8rem; font: inherit; font-size: 1rem; user-select: text;
}
.name-box .btn-primary, .name-box .btn-secondary { padding: 0.55rem 1.2rem; font-size: 1rem; margin: 0; }
.rank-lista { margin: 1rem 0; max-height: 40vh; overflow-y: auto; color: #cbd5e1; font-size: 0.95rem; }
.rank-lista table { width: 100%; border-collapse: collapse; }
.rank-lista td { padding: 0.25rem 0.4rem; text-align: left; }
.rank-lista td:nth-child(1) { color: var(--secondary-color); width: 2rem; }
.rank-lista td:nth-child(3), .rank-lista td:nth-child(4) { text-align: right; white-space: nowrap; }
.rank-nota { font-size: 0.8rem; color: var(--secondary-color); margin-bottom: 1rem !important; }

.level-btn.locked { opacity: 0.4; cursor: not-allowed; }
.level-btn.locked .ic { width: 1.1em; height: 1.1em; }
