/* --- Fuentes Personalizadas (CORREGIDO) --- */
@import url('https://fonts.googleapis.com/css2?family=Fredoka+One&family=Pixelify+Sans:wght@400;700&display=swap');

/* --- Variables Globales de Color --- */
:root {
    --bg-main: #121212;
    --bg-grid-lines: rgba(200, 200, 200, 0.04);
    --bg-display: #1a1a1a;
    --bg-keyboard: #000;
    --color-title-1: #e84d4d;
    --color-title-2: #4de89d;
    --color-display-text: #f0e68c;
    --btn-num-bg: #FFFF66;
    --btn-op-bg: red;
    --btn-special-bg: #6666FF;
    --btn-equal-bg: #66FF66;
    --btn-text-color: #000;
    --btn-disabled-bg: #333;
    --btn-disabled-text: #666;
    --output-caja-1: #e88d8d;
    --output-caja-2: #8de89d;
    --output-caja-3: #8d8de8;
    --output-caja-4: #e8e88d;
    --history-panel-bg: #1e1e1e;
    --history-header-bg: #2a2a2a;
    --history-border-color: #444;
}

/* --- Estilos Base y Reseteo --- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    cursor: default;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100dvh;
    background-color: var(--bg-main);
    background-image:
        repeating-linear-gradient(90deg, var(--bg-grid-lines) 0 1px, transparent 1px 96px),
        repeating-linear-gradient(0deg, var(--bg-grid-lines) 0 1px, transparent 1px 96px);
    padding: 20px;
    font-family: 'kurzetstypekurzetstype', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

button {
    outline: none;
    border: none;
    font-family: inherit;
}

/* --- Estructura Principal de la Calculadora --- */
#contenedor {
    position: relative;
    opacity: 0;
    transition: opacity 1s;
    width: 100%;
    max-width: 480px;
    padding-left: 1em;
}

/* --- Header Mejorado --- */
header {
    margin-bottom: 25px;
    text-align: center;
}
header h1 {
    font-family: 'Pixelify Sans', "Arial Black", "Helvetica Neue", Helvetica, sans-serif;
    font-size: 1.5rem; font-weight: bold; line-height: 1; letter-spacing: 2px; color: var(--color-title-1);
    text-transform: uppercase; text-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
}
header h1 span { color: var(--color-title-2); }
header .subtitle { font-size: 0.8rem; color: var(--color-title-1); margin-top: 15px; opacity: 0.8; }
header .subtitle a { color: var(--btn-special-bg); text-decoration: underline; font-weight: bold; transition: color 0.2s ease; }
header .subtitle a:hover { color: var(--color-title-2); }

/* --- Display y Contenedor del Teclado --- */
#display {
    font-family: 'Fredoka One', cursive, monospace;
    background-color: var(--bg-display); color: var(--color-display-text); text-align: right; width: 100%;
    padding: 20px; font-size: 2.2rem; overflow: hidden; border-radius: 10px; margin-bottom: 15px;
    min-height: 65px; word-wrap: break-word;
    margin-left: 0;
}
#cuerpoteclado {
    position: relative; background-color: var(--bg-keyboard); width: 100%; aspect-ratio: 1 / 1;
    overflow: hidden; border-radius: 10px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

/* --- Teclado y Botones --- */
#teclado {
    position: absolute; top: 0; width: 100%; height: 100%; transition: top 0.5s ease-in-out;
    display: grid; grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(5, 1fr); gap: 2px;
}
#teclado.keyboard--hidden {
    top: 100%;
}

#teclado button {
    font-size: 1.4rem; color: var(--btn-text-color); cursor: pointer; display: flex; justify-content: center;
    align-items: center; padding: 0; border: 2px solid #000; transition: transform 0.1s ease;
}
@keyframes arcoiris {
    0%{background-position:0% 50%} 50%{background-position:100% 50%} 100%{background-position:0% 50%}
}
#teclado button:not(:disabled):hover {
    background: linear-gradient(90deg, #ff0000, #ff8000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff);
    background-size: 200% 200%;
    animation: arcoiris 2s linear infinite;
    transform: scale(1.05);
}
#teclado button:disabled { background-color: var(--btn-disabled-bg) !important; color: var(--btn-disabled-text); cursor: not-allowed; }

/* Colores y tamaños específicos de botones */
#tpan { grid-column: span 2; background-color: var(--btn-special-bg); font-size: 0.8em; font-family: 'jellyregular'; }
#tcal { grid-column: span 3; background-color: var(--btn-equal-bg); font-family: 'jellyregular'; }
#t0 { grid-column: span 2; }
#t1, #t2, #t3, #t4, #t5, #t6, #t7, #t8, #t9, #t0, #tcom { background-color: var(--btn-num-bg); font-family: 'Fredoka One', cursive; }
#tmas, #tmen, #tpor, #tdiv, #tc, #tdel { background-color: var(--btn-op-bg); font-family: 'jellyregular'; }
#tpri, #trai { background-color: var(--btn-equal-bg); font-family: 'jellyregular'; }

/* --- Pantalla de Salida (Algoritmos) --- */
#salida {
    position: absolute; top: -100%; background-color: #111; width: 100%; height: 100%;
    transition: top 0.5s ease-in-out; padding: 15px; overflow: auto;
}
#salida.output-screen--visible {
    top: 0;
}

#divvolver {
    position: absolute; top: -100%; left: 0; width: 100%; height: 100%;
    transition: top 0.5s ease-in-out; display: flex; justify-content: space-between;
    align-items: flex-end; padding: 10px; pointer-events: none;
}
#divvolver.bottom-nav--visible {
    top: 0;
}

#volver, #botexp, #botnor {
    pointer-events: all; color: rgba(255, 255, 255, 0.4); background-color: rgba(255, 255, 255, 0.05);
    padding: 10px; border-radius: 5px; cursor: pointer; transition: all 0.2s;
}
#volver:hover, #botexp:hover, #botnor:hover { background-color: rgba(255, 255, 255, 0.1); color: white; }
#volver { font-family: 'arrow_7regular'; font-size: 1.5rem; }
#botexp, #botnor { font-family: 'jellyregular'; display: none; }


/* --- ESTILOS DE SALIDA MAPEADOS --- */
.output-grid__cell {
    position: absolute;
    text-align: center;
    font-family: 'Fredoka One', cursive;
    display: flex;
    justify-content: center;
    align-items: center;
}
.output-grid__cell--dividendo { color: var(--output-caja-1); }
.output-grid__cell--divisor { color: var(--output-caja-2); }
.output-grid__cell--cociente { color: var(--output-caja-3); }
.output-grid__cell--producto { color: var(--output-caja-3); }
.output-grid__cell--resto { color: var(--output-caja-4); }

.output-grid__line { position: absolute; background-color: #444; }

.output-screen__error-message {
    color: var(--output-caja-1);
    font-size: 1.2rem;
    text-align: center;
    padding-top: 20%;
}


/* ============================================== */
/* --- SECCIÓN DEL HISTORIAL --- */
/* ============================================== */
#history-toggle-btn {
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 20px);
    right: calc(env(safe-area-inset-right, 0px) + 20px);
    z-index: 1001;
    background-color: #333;
    border: 1px solid #555;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background-color 0.3s, transform 0.2s;
}
#history-toggle-btn:hover { background-color: #4CAF50; transform: scale(1.1); }
#history-toggle-btn svg { width: 28px; height: 28px; fill: #eee; }

#history-panel {
    position: fixed;
    top: 0;
    right: -100%;
    width: 350px;
    max-width: 100vw;
    height: 100dvh;
    background-color: var(--history-panel-bg);
    box-shadow: -5px 0 15px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: right 0.4s cubic-bezier(0.25, 1, 0.5, 1);
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-sizing: border-box;
}
#history-panel.history-panel--open { right: 0; }

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px env(safe-area-inset-right, 20px) 15px env(safe-area-inset-left, 20px);
    background-color: var(--history-header-bg);
    border-bottom: 1px solid var(--history-border-color);
    padding-right: 90px;
}
.history-header h2 { margin: 0; font-size: 1.2em; color: #eee; }

#clear-history-btn {
    background: #c0392b; border: none; color: white; padding: 8px 15px; border-radius: 5px;
    cursor: pointer; font-size: 0.9em; transition: background-color 0.2s;
}
#clear-history-btn:hover { background: #e74c3c; }

#history-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; flex-grow: 1; }
.history-panel__item {
    padding: 15px 20px;
    border-bottom: 1px solid #333;
    cursor: pointer;
    transition: background-color 0.2s;
}
.history-panel__item:hover { background-color: #2c3e50; }

.history-panel__input {
    display: block; font-size: 1.1em; color: #ecf0f1; margin-bottom: 5px;
    font-family: 'Fredoka One', cursive, monospace;
    word-wrap: break-word;
}
.history-panel__result {
    display: block; font-size: 1.3em; color: #2ecc71; font-weight: bold; text-align: right;
    font-family: 'Fredoka One', cursive, monospace;
}

/* --- ANIMACIONES --- */
@keyframes flash-animation {
    0% { background-color: #2c3e50; }
    50% { background-color: #3498db; }
    100% { background-color: #2c3e50; }
}
.history-item-highlight {
    animation: flash-animation 1s ease-in-out;
}


/* ============================================== */
/* --- DISEÑO RESPONSIVE PARA MÓVILES MEJORADO --- */
/* ============================================== */
@media (max-width: 600px) {
    body { 
        padding: 0;
        display: block;
        min-height: 100dvh;
        height: 100dvh;
        overflow: hidden;
    }
    
    #contenedor {
        width: 100% !important;
        max-width: 100% !important;
        height: 100dvh;
        padding: 0 !important;
        margin: 0 !important;
        display: flex;
        flex-direction: column;
        opacity: 1 !important;
    }
    
    header {
        padding: 10px 15px 0;
        margin-bottom: 15px;
        flex-shrink: 0;
    }
    
    header h1 {
        font-size: 2rem !important;
        line-height: 1.2;
        margin-bottom: 5px;
    }
    
    header .subtitle {
        font-size: 0.7rem !important;
        margin-top: 5px;
    }
    
    #display {
        width: calc(100% - 20px);
        margin: 0 auto 15px;
        font-size: 2.5rem !important;
        min-height: 70px;
        padding: 15px;
        flex-shrink: 0;
    }
    
    #cuerpoteclado {
        width: calc(100% - 20px) !important;
        margin: 0 auto;
        flex-grow: 1;
        max-height: 60vh;
        border-radius: 15px;
    }
    
    #teclado button {
        font-size: 1.5rem !important;
        border-radius: 8px;
    }
    
    /* Ajustar botones de navegación en pantallas de algoritmos */
    #divvolver {
        padding: 15px;
    }
    
    #volver {
        font-size: 2rem !important;
        width: 50px;
        height: 50px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    #botexp, #botnor {
        font-size: 0.9rem !important;
        padding: 12px 8px;
        height: 50px;
    }
    
    /* Mejoras para el historial en móviles */
    #history-panel {
        width: 100%;
        max-width: 100vw;
    }
    
    .history-header {
        padding: 15px 70px 15px 20px;
    }
    
    #history-toggle-btn {
        top: 10px;
        right: 10px;
        width: 40px;
        height: 40px;
    }
    
    #history-toggle-btn svg {
        width: 22px;
        height: 22px;
    }
}

/* Ajustes adicionales para dispositivos muy pequeños */
@media (max-width: 350px) {
    header h1 {
        font-size: 1.8rem !important;
    }
    
    #display {
        font-size: 2rem !important;
    }
    
    #teclado button {
        font-size: 1.3rem !important;
    }
}

/* Ajuste para evitar que el teclado virtual de iOS afecte el diseño */
@supports (-webkit-touch-callout: none) {
    #contenedor {
        min-height: -webkit-fill-available;
        height: -webkit-fill-available;
    }
}

/* --- FIN DEL CSS --- */