:root {
    --bg-color: #0f172a;
    --calc-bg: #1e293b;
    --text-color: #f8fafc;
    --accent-color: #38bdf8;
    --operator-color: #f59e0b;
    --danger-color: #ef4444;
}

body {
    background-color: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    font-family: 'Segoe UI', Roboto, sans-serif;
    overflow: hidden; /* Prevents scrollbars if everything fits */
}

.app-container {
    display: flex;
    gap: 20px;
    align-items: center; /* Centers both panels vertically */
}

/* --- Minimized Calculator Layout --- */
.calculator {
    display: grid;
    grid-template-columns: repeat(4, 70px); 
    /* Precise Row Locking: Display(1) + Controls(2) + Keypad(3-7) */
    grid-template-rows: 100px 55px repeat(5, 60px); 
    gap: 10px;
    padding: 20px;
    background: var(--calc-bg);
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
    height: auto; /* Removes bottom "dead space" */
    align-content: start;
}

/* Fixes the "Covering Screen" issue */
.display {
    grid-column: 1 / -1;
    grid-row: 1; /* Force display to stay in the first row only */
    height: 90px;
    background: rgba(0, 0, 0, 0.25);
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    padding: 12px;
    border-radius: 12px;
    pointer-events: none;
    overflow: hidden;
}

.current-operand { 
    color: var(--text-color); 
    font-size: 1.6rem; 
    word-break: break-all;
    line-height: 1.2;
}

.previous-operand { 
    color: rgba(255, 255, 255, 0.4); 
    font-size: 0.9rem; 
    min-height: 1.1rem;
}

/* --- Button Styling --- */
button {
    width: 100%;
    height: 100%;
    cursor: pointer;
    font-size: 1.1rem;
    border: none;
    border-radius: 12px;
    background: #334155;
    color: var(--text-color);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    justify-content: center;
    align-items: center;
}

button:hover { 
    background: #475569; 
    transform: translateY(-1px);
}

button:active { 
    transform: scale(0.95); 
}

/* Red Clear Button with Hover Effect */
/* Updated Small Red Clear Button */
.clear-history {
    background: transparent;
    border: 1px solid var(--danger-color);
    color: var(--danger-color);
    /* Minimalist sizing */
    padding: 2px 6px; 
    font-size: 10px; 
    line-height: 1;
    border-radius: 4px;
    font-weight: 700;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.2s ease;
    /* Prevents any stretching */
    width: fit-content;
    height: fit-content;
}

.clear-history:hover {
    background: var(--danger-color);
    color: #ffffff;
    box-shadow: 0 2px 5px rgba(239, 68, 68, 0.2);
}
/* Special Button Types */
.operator { color: var(--operator-color); font-weight: bold; }
.equals { background: var(--accent-color) !important; color: #0f172a !important; font-weight: bold; }
.span-two { grid-column: span 2; }
.small-btn { font-size: 0.9rem; font-weight: bold; background: #475569; }
.mode-btn { color: var(--accent-color); font-size: 1.3rem; }

/* Advanced Keys Logic */
.keypad-grid { display: contents; }
.hidden { display: none !important; }
.adv { 
    background: rgba(56, 189, 248, 0.08); 
    border: 1px solid rgba(56, 189, 248, 0.2); 
    color: var(--accent-color);
    font-size: 1rem;
}

/* --- History Panel (Auto-scaling) --- */
.history-panel {
    width: 260px;
    height: 485px; /* Matches the vertical height of the minimized calculator */
    background: var(--calc-bg);
    border-radius: 20px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #334155;
    padding-bottom: 12px;
    margin-bottom: 15px;
    flex-shrink: 0;
}

.history-panel h3 { margin: 0; color: var(--accent-color); font-size: 1.1rem; }

#history-list {
    list-style: none;
    padding: 0;
    margin: 0;
    overflow-y: auto;
    flex-grow: 1;
    scrollbar-width: thin;
    scrollbar-color: #475569 transparent;
}

.history-item {
    padding: 10px;
    border-bottom: 1px solid rgba(51, 65, 85, 0.5);
    border-radius: 6px;
    margin-bottom: 5px;
}

.history-equation { font-size: 0.8rem; color: rgba(255, 255, 255, 0.5); }
.history-result { font-weight: bold; font-size: 1rem; color: var(--accent-color); margin-top: 2px; }