:root {
    --primary-purple: #6e48aa;
    --primary-green: #28a745;
    --carry-orange: #fd7e14;
    --result-dark: #343a40;
    --line-color: #dee2e6;
    --text-light: #6c757d;
    --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #e3e8f0 100%);
    --container-bg: rgba(255, 255, 255, 0.95);
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    --font-family: 'Nunito', sans-serif;
}

body {
    font-family: var(--font-family);
    background-image: var(--bg-gradient);
    color: var(--result-dark);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 2rem 1rem;
    min-height: 100vh;
}

.container {
    background: var(--container-bg);
    border-radius: 24px;
    box-shadow: var(--shadow);
    padding: 2rem;
    width: 100%;
    max-width: 600px; /* Default max-width for mobile/tablet */
    transition: max-width 0.4s ease-in-out;
    /* Flexbox to correctly stack header and layout wrapper */
    display: flex;
    flex-direction: column;
    align-items: center;
}

header {
    width: 100%;
    text-align: center;
}

.subtitle { margin: 0.2rem 0 1.5rem; color: var(--text-light); }

#whiteboard {
    background: #fff;
    border: 1px solid #e9ecef;
    border-radius: 12px;
    min-height: 200px;
    margin-bottom: 1.5rem;
    padding: 20px;
    position: relative;
    font-size: 2.2rem;
    font-weight: 700;
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.05);
}

#operands-container {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.operand-item {
    display: flex;
    align-items: center;
    gap: 15px;
    width: 100%;
    justify-content: flex-end;
    font-family: 'Courier New', Courier, monospace;
    animation: pop-in 0.4s ease-out;
}
.operand-item:last-child::before {
    content: '+';
    color: var(--text-light);
    font-family: var(--font-family);
    font-weight: 400;
}
.operand-item:first-child:last-child::before,
.operand-item:nth-last-child(2):first-child::before {
    content: '';
}

.operand-text {
    cursor: pointer;
    padding: 2px 8px;
    border-radius: 8px;
    border: 1px solid transparent;
    transition: background-color 0.2s, border-color 0.2s;
}
.operand-text:hover {
    background-color: #f8f9fa;
    border-color: #dee2e6;
}
.edit-input {
    font-family: 'Courier New', Courier, monospace;
    font-size: 2.2rem;
    font-weight: 700;
    color: var(--result-dark);
    text-align: right;
    border: 2px solid var(--primary-purple);
    outline: none;
    background-color: #f3e5f54d;
    border-radius: 8px;
    padding: 0 6px;
    flex-grow: 1;
    width: 10px;
}

.delete-btn {
    background: #ffebee;
    color: #f44336;
    border: none; border-radius: 50%;
    width: 24px; height: 24px;
    font-size: 14px; font-weight: bold;
    cursor: pointer; opacity: 0;
    transition: all 0.2s;
    line-height: 24px; padding: 0;
}
.operand-item:hover .delete-btn { opacity: 1; transform: scale(1.1); }

#calculation-svg {
    width: 100%; height: 100%;
    position: absolute; top: 0; left: 0;
}

.input-section { display: flex; gap: 0.75rem; margin-bottom: 1.5rem; }
#numberInput {
    flex-grow: 1; padding: 0.8rem 1rem;
    border: 2px solid var(--line-color); border-radius: 10px;
    font-size: 1.2rem; font-weight: 700;
    transition: all 0.3s ease;
}
#numberInput:focus {
    outline: none; border-color: var(--primary-purple);
    box-shadow: 0 0 10px rgba(110, 72, 170, 0.2);
}

.controls { display: flex; justify-content: center; gap: 1rem; margin-bottom: 1.5rem; }
.btn-add, .btn-calculate, .btn-reset, .btn-replay {
    padding: 0.8rem 1.5rem;
    font-size: 1rem; font-weight: 700;
    border: none; border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s ease;
}
.btn-add:hover, .btn-calculate:hover, .btn-reset:hover, .btn-replay:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.btn-add { background: var(--primary-purple); color: white; }
.btn-calculate { background-color: var(--primary-green); color: white; }
.btn-calculate:disabled { background-color: #a5d6a7; cursor: not-allowed; transform: none; box-shadow: none; }
.btn-replay { background-color: var(--primary-purple); color: white; }
.btn-reset { background-color: var(--text-light); color: white; }

#explanation-box {
    background-color: #f3e5f5;
    border-left: 5px solid var(--primary-purple);
    padding: 1rem; border-radius: 8px;
    text-align: left; transition: all 0.3s ease;
    margin-bottom: 1rem;
}
#explanation-text { margin: 0; font-size: 1rem; color: #5e35b1; }

#procedure-section, #history-section { 
    margin-top: 1.5rem; 
    text-align: left; 
}
#procedure-section h2, #history-section h2 { 
    font-size: 1.2rem; 
    color: var(--text-light); 
    border-bottom: 2px solid #e9ecef;
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}

#procedure-list, #history-list { list-style: none; padding: 0; margin: 0; }
#procedure-list li {
    padding: 0.5rem 0.8rem;
    margin-bottom: 0.5rem;
    border-radius: 6px;
    background-color: #fff;
    border: 1px solid #e9ecef;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    position: relative;
}

#procedure-list li::after {
    content: '🔊';
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.5;
    font-size: 1.2rem;
}

#procedure-list li:hover::after {
    opacity: 1;
}
#procedure-list li:hover {
    background-color: #f3e5f5;
    border-color: var(--primary-purple);
    transform: scale(1.02);
}

#history-list li { 
    background: #f1f3f5; 
    padding: 0.5rem 1rem; 
    border-radius: 5px; 
    margin-bottom: 0.5rem;
    cursor: pointer;
    transition: background-color 0.2s ease;
    border: 1px solid transparent;
}
#history-list li:hover {
    background-color: #e3e8f0;
    border-color: var(--primary-purple);
}

.hidden { display: none !important; }
@keyframes pop-in { from { opacity: 0; transform: translateY(10px) scale(0.95); } to { opacity: 1; transform: translateY(0) scale(1); } }
.digit { font-size: 32px; font-weight: 700; text-anchor: middle; }
.num1-text { fill: #1565c0; } .num2-text { fill: #00695c; }
.carry-text { fill: var(--carry-orange); font-size: 20px; font-weight: 900; }
.result-text { fill: var(--result-dark); font-weight: 900; }
.plus-sign-svg { fill: var(--text-light); font-weight: 700; }
.sum-line-svg { stroke: var(--result-dark); stroke-width: 2.5; }
.highlight-rect { fill: #fff176; opacity: 0.5; transition: all 0.5s ease-in-out; }


/* --- LAYOUT STYLES --- */
.calculator-layout-wrapper {
    display: flex;
    flex-direction: column; /* Mobile-first: stacks columns */
    width: 100%;
    margin-top: 1.5rem;
}

/* On mobile, reorder for better flow: main content is first */
.main-content { order: 1; }
#procedure-section { order: 2; }
#history-section { order: 3; }


/* DESKTOP: 3-Column Flexbox Layout */
@media (min-width: 1024px) {
    .container {
        max-width: 1200px; /* Widen container for desktop */
    }

    .calculator-layout-wrapper {
        flex-direction: row; /* Switch to side-by-side columns */
        align-items: flex-start;
        gap: 3rem; /* Increased gap for better visual separation */
    }
    
    /* Reset mobile ordering so columns appear as defined in the HTML */
    .main-content, #procedure-section, #history-section {
        order: 0;
        margin-top: 0;
    }

    /* Define column sizes */
    #procedure-section {
        flex: 1 1 25%; /* Left column */
    }
    .main-content {
        flex: 1 1 50%; /* Center column (wider) */
    }
    #history-section {
        flex: 1 1 25%; /* Right column */
    }
}


/* RESPONSIVE STYLES FOR SMALLER SCREENS */
@media (max-width: 576px) {
    body { padding: 1rem 0.5rem; }
    .container { padding: 1.5rem 1rem; max-width: 100%; }
    header h1 { font-size: 1.8rem; }
    #whiteboard { min-height: 180px; font-size: 1.8rem; padding: 15px; }
    .edit-input { font-size: 1.8rem; }
    .digit { font-size: 28px; }
    .carry-text { font-size: 18px; }
    .plus-sign-svg { font-size: 28px; }
    .input-section { flex-direction: column; gap: 0.5rem; }
    .controls { flex-direction: column; gap: 0.5rem; }
    .btn-calculate, .btn-replay, .btn-reset { width: 100%; }
}




/* --- ESTILOS DEL FOOTER MOTIVACIONAL --- */
.motivational-footer {
    width: 100%;
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--line-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap; /* Para que se adapte en pantallas pequeñas */
    gap: 1rem;
}

.footer-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-weight: 700;
    color: var(--primary-purple);
}

.logo-svg {
    width: 40px;
    height: 40px;
}

.footer-buttons {
    display: flex;
    gap: 0.75rem;
}

.footer-btn {
    background-color: #f1f3f5;
    color: var(--text-light);
    border: 1px solid var(--line-color);
    border-radius: 8px;
    padding: 0.6rem 1rem;
    font-family: var(--font-family);
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s ease;
}

.footer-btn:hover {
    background-color: var(--primary-purple);
    color: white;
    border-color: var(--primary-purple);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

/* Adaptar footer para móviles */
@media (max-width: 600px) {
    .motivational-footer {
        flex-direction: column;
        justify-content: center;
        text-align: center;
        gap: 1.5rem;
    }
    .footer-buttons {
        width: 100%;
        display: flex;
        flex-direction: column;
    }
}


/* --- ESTILOS DEL FOOTER MOTIVACIONAL --- */
.motivational-footer {
    width: 100%;
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--line-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap; /* Para que se adapte en pantallas pequeñas */
    gap: 1rem;
}

.footer-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-weight: 700;
    color: var(--primary-purple);
}

.logo-svg {
    width: 40px;
    height: 40px;
}

.footer-buttons {
    display: flex;
    gap: 0.75rem;
}

.footer-btn {
    background-color: #f1f3f5;
    color: var(--text-light);
    border: 1px solid var(--line-color);
    border-radius: 8px;
    padding: 0.6rem 1rem;
    font-family: var(--font-family);
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s ease;
}

.footer-btn:hover {
    background-color: var(--primary-purple);
    color: white;
    border-color: var(--primary-purple);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

/* Adaptar footer para móviles */
@media (max-width: 600px) {
    .motivational-footer {
        flex-direction: column;
        justify-content: center;
        text-align: center;
        gap: 1.5rem;
    }
    .footer-buttons {
        width: 100%;
        display: flex;
        flex-direction: column;
    }
}

/* --- ESTILOS DE PROCEDIMIENTO Y HISTORIAL --- */
#procedure-section, #history-section { 
    margin-top: 1.5rem; 
    text-align: left; 
}

/* --- ESTA ES LA SECCIÓN MODIFICADA PARA EL SCROLL --- */
#procedure-section {
    display: flex;
    flex-direction: column;
    /* Establecemos una altura máxima. El contenido no podrá superar este alto. */
    /* Este valor funciona bien en la vista de escritorio de 3 columnas. */
    max-height: 450px; 
}

#procedure-section h2, #history-section h2 { 
    font-size: 1.2rem; 
    color: var(--text-light); 
    border-bottom: 2px solid #e9ecef;
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
    /* Hacemos que el título no se mueva con el scroll */
    flex-shrink: 0;
}

#procedure-list, #history-list { 
    list-style: none; 
    padding: 0; 
    margin: 0; 
}

/* --- ESTA ES LA SEGUNDA PARTE CLAVE PARA EL SCROLL --- */
#procedure-list {
    /* Si el contenido de la lista supera el espacio disponible, muestra un scroll vertical */
    overflow-y: auto; 
    /* Pequeño padding para que el scroll no se pegue al borde */
    padding-right: 0.5rem; 
}

#procedure-list li {
    padding: 0.5rem 0.8rem;
    margin-bottom: 0.5rem;
    border-radius: 6px;
    background-color: #fff;
    border: 1px solid #e9ecef;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    position: relative;
}


/* ==============================================
   ===      ESTILOS PARA MODO OSCURO          ===
   ============================================== */

/* --- 1. Estilos para el botón de cambio de tema --- */
.header-top {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    position: relative; /* Para alinear el título y el botón */
    width: 100%;
}

.theme-btn {
    background: #f1f3f5;
    border: 1px solid var(--line-color);
    color: var(--result-dark);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    justify-content: center;
    align-items: center;
}

.theme-btn:hover {
    background-color: var(--primary-purple);
    color: white;
    transform: translateY(-2px) scale(1.1);
}


/* --- 2. Variables y estilos para el modo oscuro --- */
body.dark-mode {
    --bg-gradient: linear-gradient(135deg, #2c3e50 0%, #1a222a 100%);
    --container-bg: rgba(44, 62, 80, 0.9);
    --result-dark: #ecf0f1; /* Texto claro para fondos oscuros */
    --text-light: #95a5a6;
    --line-color: #3b5066;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

/* Sobrescribir estilos de componentes específicos */
body.dark-mode #whiteboard {
    background: #2c3e50;
    border-color: var(--line-color);
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.25);
}

body.dark-mode .operand-item:last-child::before {
     color: var(--text-light);
}

body.dark-mode .edit-input {
    background-color: #34495e80;
    color: #fff;
}

body.dark-mode .delete-btn {
    background: #5e3d43;
    color: #ff8a80;
}

body.dark-mode #numberInput {
    background-color: #34495e;
    border-color: var(--line-color);
    color: var(--result-dark);
}

body.dark-mode #numberInput::placeholder {
    color: var(--text-light);
}

body.dark-mode #numberInput:focus {
    border-color: var(--primary-purple);
    box-shadow: 0 0 10px rgba(110, 72, 170, 0.4);
}

body.dark-mode #explanation-box {
    background-color: rgba(94, 53, 177, 0.2);
    border-left-color: #9575cd;
}

body.dark-mode #explanation-text {
    color: #d1c4e9;
}

body.dark-mode #procedure-list li,
body.dark-mode #history-list li {
    background-color: #34495e;
    border-color: var(--line-color);
}

body.dark-mode #procedure-list li:hover {
    background-color: rgba(110, 72, 170, 0.3);
    border-color: var(--primary-purple);
}

body.dark-mode #history-list li:hover {
    background-color: #4b6680;
    border-color: var(--primary-purple);
}

body.dark-mode .footer-btn,
body.dark-mode .theme-btn {
    background-color: #34495e;
    color: var(--text-light);
    border-color: var(--line-color);
}

body.dark-mode .footer-btn:hover,
body.dark-mode .theme-btn:hover {
    background-color: var(--primary-purple);
    color: white;
    border-color: var(--primary-purple);
}

body.dark-mode .logo-svg rect {
    fill: #9575cd;
}

body.dark-mode .footer-logo span {
    color: #b39ddb;
}

body.dark-mode .sum-line-svg {
    stroke: var(--result-dark);
}