/* ============================================================
   TOKENS
   ============================================================ */
:root {
    --page-bg: #ffffff;
    --shell-bg: #ffffff;
    --display-bg: #c8b4f8;
    --expr-color: #ffffff;
    --result-color: rgba(255, 255, 255, 0.72);

    --btn-num-bg: #ede8ff;
    --btn-num-hover: #ddd5ff;
    --btn-num-txt: #2c1566;

    --btn-ctrl-bg: #c8b4f8;
    --btn-ctrl-hover: #b8a0f0;
    --btn-ctrl-txt: #ffffff;

    --btn-op-bg: #c8b4f8;
    --btn-op-hover: #b8a0f0;
    --btn-op-txt: #ffffff;

    --btn-sci-bg: #ddd0ff;
    --btn-sci-hover: #cdc0f8;
    --btn-sci-txt: #2c1566;
    --btn-sci-active: #a78bfa;

    --btn-eq-bg: #8b5cf6;
    --btn-eq-hover: #7c3aed;
    --btn-eq-txt: #ffffff;

    --op-active-bg: #7c3aed;
    --op-active-txt: #ffffff;

    --sci-panel-bg: #f3eeff;
    --sci-border: rgba(180, 150, 255, 0.18);
    --divider-color: rgba(180, 150, 255, 0.22);
    --toggle-color: #a78bfa;
    --topbar-color: #9974d4;
    --shell-shadow: 0 20px 50px rgba(100, 60, 200, 0.10);

    --hist-bg: rgba(250, 246, 255, 0.98);
    --hist-item-hover: rgba(200, 180, 255, 0.28);
    --hist-txt: #3d1f80;
    --hist-res: #6d3fc9;

    /* Altura de botones estándar — grande en modo sencillo */
    --btn-h: 72px;
    --btn-sci-h: 48px;
    --grid-gap: 10px;
    --sci-gap: 8px;
}

[data-theme="dark"] {
    --page-bg: #18162a;
    --shell-bg: #18162a;
    --display-bg: #2e1a6e;
    --expr-color: #f0e8ff;
    --result-color: #c4a0f5;

    --btn-num-bg: #24213a;
    --btn-num-hover: #2e2a45;
    --btn-num-txt: #e8e0ff;

    --btn-ctrl-bg: #3d2880;
    --btn-ctrl-hover: #4c35a0;
    --btn-ctrl-txt: #e8e0ff;

    --btn-op-bg: #3d2880;
    --btn-op-hover: #4c35a0;
    --btn-op-txt: #e8e0ff;

    --btn-sci-bg: #2d2550;
    --btn-sci-hover: #3a3068;
    --btn-sci-txt: #d4c8ff;
    --btn-sci-active: #7c3aed;

    --btn-eq-bg: #7c3aed;
    --btn-eq-hover: #6d28d9;
    --btn-eq-txt: #ffffff;

    --op-active-bg: #8b5cf6;
    --op-active-txt: #ffffff;

    --sci-panel-bg: #1e1a32;
    --sci-border: rgba(100, 70, 200, 0.20);
    --divider-color: rgba(120, 80, 220, 0.20);
    --toggle-color: #9d78f0;
    --topbar-color: #c4a0f5;
    --shell-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);

    --hist-bg: rgba(22, 18, 42, 0.98);
    --hist-item-hover: rgba(100, 70, 200, 0.22);
    --hist-txt: #c4a0f5;
    --hist-res: #a87af8;
}

/* ============================================================
   BASE
   ============================================================ */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html,
body {
    height: 100%;
    /* NO overflow:hidden aquí — permitir scroll si la calc es más alta */
}

body {
    font-family: 'Figtree', sans-serif;
    background: var(--page-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    transition: background 0.3s;
    padding: 12px 0;
}

/* ============================================================
   SHELL
   ============================================================ */
.calc-shell {
    width: min(360px, 96vw);
    /* SIN altura fija en modo sencillo — crece con su contenido */
    background: var(--shell-bg);
    border-radius: 36px;
    box-shadow: var(--shell-shadow);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: background 0.3s, box-shadow 0.3s;
    animation: popUp .4s cubic-bezier(.34, 1.56, .64, 1) both;
}

@keyframes popUp {
    from {
        transform: translateY(22px) scale(.96);
        opacity: 0;
    }

    to {
        transform: none;
        opacity: 1;
    }
}

/* Cuando la científica está ABIERTA → limitar altura y reducir botones */
.calc-shell.sci-open {
    height: min(820px, 98vh);
    --btn-h: 54px;
    --btn-sci-h: 40px;
    --grid-gap: 8px;
    --sci-gap: 6px;
}

/* En pantallas muy pequeñas en altura */
@media (max-height: 700px) {
    .calc-shell.sci-open {
        --btn-h: 46px;
        --btn-sci-h: 36px;
        --grid-gap: 6px;
        --sci-gap: 5px;
    }
}

/* ============================================================
   TOP BAR
   ============================================================ */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 18px 8px;
    flex-shrink: 0;
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    color: var(--topbar-color);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background .18s, transform .18s;
    -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover {
    background: rgba(140, 80, 255, .10);
    transform: scale(1.1);
}

.icon-btn:active {
    transform: scale(.9);
}

[data-theme="dark"] .icon-sun {
    display: block;
}

[data-theme="dark"] .icon-moon {
    display: none;
}

[data-theme="light"] .icon-moon {
    display: block;
}

[data-theme="light"] .icon-sun {
    display: none;
}

/* ============================================================
   HISTORIAL
   ============================================================ */
.history-panel {
    max-height: 0;
    overflow: hidden;
    transition: max-height .35s cubic-bezier(.4, 0, .2, 1);
    background: var(--hist-bg);
    flex-shrink: 0;
}

.history-panel.open {
    max-height: 160px;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 18px 5px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 1.2px;
    text-transform: uppercase;
    color: var(--hist-txt);
    opacity: .65;
}

.clear-hist {
    background: none;
    border: none;
    cursor: pointer;
    font-family: 'Figtree', sans-serif;
    font-size: 12px;
    font-weight: 600;
    color: var(--hist-res);
    padding: 2px 7px;
    border-radius: 6px;
    transition: background .18s;
}

.clear-hist:hover {
    background: var(--hist-item-hover);
}

.history-list {
    list-style: none;
    overflow-y: auto;
    max-height: 120px;
    padding: 0 0 8px;
    scrollbar-width: thin;
    scrollbar-color: rgba(180, 150, 255, .3) transparent;
}

.hist-empty {
    padding: 12px 18px;
    text-align: center;
    font-size: 13px;
    color: var(--hist-txt);
    opacity: .45;
    font-style: italic;
}

.hist-item {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 7px 18px;
    font-size: 13px;
    color: var(--hist-txt);
    cursor: pointer;
    transition: background .18s;
}

.hist-item:hover {
    background: var(--hist-item-hover);
}

.hist-item .h-res {
    color: var(--hist-res);
    font-weight: 700;
    white-space: nowrap;
}

/* ============================================================
   DISPLAY
   ============================================================ */
.display-area {
    background: var(--display-bg);
    margin: 0 14px;
    border-radius: 22px;
    padding: 20px 20px 16px;
    flex-shrink: 0;
    min-height: 130px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: background .3s, min-height .3s, padding .3s;
    overflow: hidden;
    position: relative;
}

/* Cuando la científica abre, reducir display */
.calc-shell.sci-open .display-area {
    min-height: 100px;
    padding: 14px 20px 12px;
}

.display-area::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 40%;
    background: linear-gradient(180deg, rgba(255, 255, 255, .12) 0%, transparent 100%);
    border-radius: 22px 22px 0 0;
    pointer-events: none;
}

.expr-wrap {
    flex: 1;
    display: flex;
    align-items: flex-start;
    overflow: hidden;
}

.expr-text {
    font-size: clamp(22px, 7vw, 38px);
    font-weight: 600;
    color: var(--expr-color);
    line-height: 1.2;
    word-break: break-all;
    letter-spacing: -0.3px;
    transition: color .3s;
}

.expr-text.long {
    font-size: clamp(15px, 4.5vw, 20px);
}

.expr-text.medium {
    font-size: clamp(18px, 5.8vw, 28px);
}

.result-wrap {
    display: flex;
    justify-content: flex-end;
    min-height: 28px;
}

.result-text {
    font-size: 20px;
    font-weight: 400;
    color: var(--result-color);
    letter-spacing: -0.3px;
    transition: color .3s;
}

.result-text.answer {
    font-size: clamp(28px, 8vw, 44px);
    font-weight: 700;
    color: var(--expr-color);
}

.result-text.error {
    color: #ffaaaa;
    font-size: 15px;
}

/* ============================================================
   TOGGLE CIENTÍFICA
   ============================================================ */
.sci-toggle-row {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 6px 18px 3px;
    flex-shrink: 0;
}

.sci-toggle-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--toggle-color);
    padding: 3px 8px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    transition: background .18s;
    -webkit-tap-highlight-color: transparent;
}

.sci-toggle-btn:hover {
    background: rgba(140, 80, 255, .10);
}

.chevron-icon {
    transition: transform .3s cubic-bezier(.4, 0, .2, 1);
}

.sci-toggle-btn.closed .chevron-icon {
    transform: rotate(180deg);
}

/* ============================================================
   PANEL CIENTÍFICO
   ============================================================ */
.sci-panel {
    overflow: hidden;
    max-height: 0;
    transition: max-height .38s cubic-bezier(.4, 0, .2, 1), opacity .28s ease;
    opacity: 0;
    flex-shrink: 0;
    background: var(--sci-panel-bg);
    border-top: 1px solid var(--sci-border);
    border-bottom: 1px solid var(--sci-border);
}

.sci-panel.open {
    max-height: 185px;
    height: 185px;
    opacity: 1;
}

.sci-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: var(--sci-gap);
    padding: 8px 14px;
    height: 100%;
}

/* ============================================================
   KEYPAD — altura dinámica via variable
   ============================================================ */
.keypad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    /* En modo sencillo: filas con altura fija via variable */
    grid-template-rows: repeat(5, var(--btn-h));
    gap: var(--grid-gap);
    padding: 8px 14px 18px;
    flex-shrink: 0;
}

/* En modo científico: las filas llenan el espacio disponible */
.calc-shell.sci-open .keypad {
    grid-template-rows: repeat(5, 1fr);
    flex: 1;
}

/* ============================================================
   BOTONES
   ============================================================ */
.btn {
    font-family: 'Figtree', sans-serif;
    font-size: 20px;
    font-weight: 600;
    height: 100%;
    min-height: 44px;
    border: none;
    border-radius: 18px;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    transition: background .17s, transform .12s, box-shadow .17s;
    -webkit-tap-highlight-color: transparent;
}

/* Ripple */
.btn::after {
    content: '';
    position: absolute;
    inset: 50%;
    background: rgba(255, 255, 255, .28);
    border-radius: 50%;
    transform: scale(0);
    opacity: 0;
    transition: transform .35s ease, opacity .35s ease;
}

.btn.ripple::after {
    inset: -50%;
    transform: scale(1);
    opacity: 1;
    transition: 0s;
}

/* NÚMEROS */
.btn-num {
    background: var(--btn-num-bg);
    color: var(--btn-num-txt);
}

.btn-num:hover {
    background: var(--btn-num-hover);
    transform: scale(1.04);
}

.btn-num:active {
    transform: scale(.92);
}

/* CONTROL */
.btn-ctrl {
    background: var(--btn-ctrl-bg);
    color: var(--btn-ctrl-txt);
}

.btn-ctrl:hover {
    background: var(--btn-ctrl-hover);
    transform: scale(1.04);
}

.btn-ctrl:active {
    transform: scale(.92);
}

/* OPERADORES */
.btn-op {
    background: var(--btn-op-bg);
    color: var(--btn-op-txt);
    font-size: 23px;
}

.btn-op:hover {
    background: var(--btn-op-hover);
    transform: scale(1.04);
}

.btn-op:active {
    transform: scale(.92);
}

.btn-op.active-op {
    background: var(--op-active-bg) !important;
    color: var(--op-active-txt) !important;
    box-shadow: 0 4px 14px rgba(124, 60, 237, .35);
}

/* CIENTÍFICOS */
.btn-sci {
    background: var(--btn-sci-bg);
    color: var(--btn-sci-txt);
    font-size: 14px;
    font-weight: 600;
    height: 100%;
    min-height: 36px;
    border-radius: 13px;
    transition: background .17s, transform .12s;
}

.btn-sci:hover {
    background: var(--btn-sci-hover);
    transform: scale(1.04);
}

.btn-sci:active {
    transform: scale(.92);
}

.btn-sci.sci-active {
    background: var(--btn-sci-active);
    color: #fff;
    box-shadow: 0 3px 12px rgba(124, 60, 237, .30);
}

/* IGUAL */
.btn-eq {
    background: var(--btn-eq-bg);
    color: var(--btn-eq-txt);
    font-size: 26px;
    box-shadow: 0 4px 16px rgba(124, 60, 237, .28);
}

.btn-eq:hover {
    background: var(--btn-eq-hover);
    transform: scale(1.04);
    box-shadow: 0 6px 20px rgba(124, 60, 237, .40);
}

.btn-eq:active {
    transform: scale(.92);
}

/* Keyboard flash */
.btn.kb-flash {
    outline: 2px solid var(--btn-eq-bg);
    outline-offset: 2px;
}

/* ============================================================
   RESPONSIVE pantallas muy pequeñas en altura
   ============================================================ */
@media (max-height: 600px) {
    :root {
        --btn-h: 56px;
        --grid-gap: 7px;
    }

    .calc-shell {
        border-radius: 24px;
    }

    .calc-shell.sci-open {
        --btn-h: 42px;
        --btn-sci-h: 32px;
        --grid-gap: 5px;
        --sci-gap: 4px;
    }

    .display-area {
        min-height: 100px;
        padding: 12px 16px 10px;
    }

    .top-bar {
        padding: 10px 16px 4px;
    }
}

@media (max-width: 340px) {
    .calc-shell {
        border-radius: 26px;
    }

    .btn {
        font-size: 17px;
        border-radius: 14px;
    }

    .btn-sci {
        font-size: 12px;
        border-radius: 10px;
    }
}