/* Global Reset & System Font */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    user-select: none;
    -webkit-user-select: none;
}

:root {
    /* Color Palette */
    --bg-primary: #0a0b12;
    --card-bg: rgba(20, 22, 37, 0.65);
    --card-border: rgba(255, 255, 255, 0.08);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    
    /* Neon Glow Accents */
    --accent-cyan: #00f2fe;
    --accent-cyan-glow: rgba(0, 242, 254, 0.35);
    --accent-violet: #8b5cf6;
    --accent-violet-glow: rgba(139, 92, 246, 0.3);
    --accent-rose: #f43f5e;
    --accent-rose-glow: rgba(244, 63, 94, 0.35);
    
    /* Button Colors */
    --btn-num-bg: rgba(255, 255, 255, 0.05);
    --btn-num-hover: rgba(255, 255, 255, 0.1);
    --btn-ctrl-bg: rgba(139, 92, 246, 0.15);
    --btn-ctrl-hover: rgba(139, 92, 246, 0.25);
    --btn-sci-bg: rgba(0, 242, 254, 0.08);
    --btn-sci-hover: rgba(0, 242, 254, 0.18);
    
    /* Layout & Effects */
    --blur-val: 24px;
    --transition-speed: 0.25s;
    --shadow-premium: 0 24px 48px -12px rgba(0, 0, 0, 0.5);
}

body {
    background-color: var(--bg-primary);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
    position: relative;
    padding: 20px;
}

/* Background Ambient Orbs */
.stars-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: 
        radial-gradient(white, rgba(255,255,255,.2) 2px, transparent 40px),
        radial-gradient(white, rgba(255,255,255,.15) 1px, transparent 30px),
        radial-gradient(white, rgba(255,255,255,.1) 2px, transparent 40px);
    background-size: 550px 550px, 350px 350px, 250px 250px;
    background-position: 0 0, 40px 60px, 130px 270px;
    opacity: 0.06;
    z-index: -2;
}

.glow-orbs {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: -1;
    pointer-events: none;
}

.orb {
    position: absolute;
    border-radius: 50%;
    filter: blur(100px);
    opacity: 0.18;
}

.orb-1 {
    width: 450px;
    height: 450px;
    background: radial-gradient(circle, var(--accent-cyan) 0%, transparent 70%);
    top: -50px;
    right: 10%;
    animation: floatOrb 12s infinite alternate ease-in-out;
}

.orb-2 {
    width: 550px;
    height: 550px;
    background: radial-gradient(circle, var(--accent-violet) 0%, transparent 70%);
    bottom: -100px;
    left: 10%;
    animation: floatOrb 16s infinite alternate-reverse ease-in-out;
}

@keyframes floatOrb {
    0% { transform: translate(0, 0) scale(1); }
    100% { transform: translate(40px, 50px) scale(1.1); }
}

/* App Layout Container */
.app-container {
    display: flex;
    gap: 24px;
    max-width: 960px;
    width: 100%;
    align-items: stretch;
    justify-content: center;
    position: relative;
    z-index: 10;
}

/* Glassmorphism Styles */
.glass {
    background: var(--card-bg);
    backdrop-filter: blur(var(--blur-val));
    -webkit-backdrop-filter: blur(var(--blur-val));
    border: 1px solid var(--card-border);
    box-shadow: var(--shadow-premium);
    border-radius: 28px;
}

/* Calculator Main Card */
.calculator-card {
    flex: 1 1 450px;
    max-width: 500px;
    padding: 28px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    transition: max-width var(--transition-speed) cubic-bezier(0.16, 1, 0.3, 1),
                flex var(--transition-speed) cubic-bezier(0.16, 1, 0.3, 1);
}

/* Standard vs Scientific Size transition */
.calculator-card.scientific-active {
    max-width: 900px;
    flex: 2 1 750px;
}

/* Header UI Elements */
.calc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    padding-bottom: 16px;
}

.logo {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.logo-accent {
    background: linear-gradient(135deg, var(--accent-cyan), var(--accent-violet));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-right: 2px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* Button & Tab Styling */
.icon-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: var(--text-muted);
    border-radius: 50%;
    width: 38px;
    height: 38px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: all var(--transition-speed) ease;
}

.icon-btn:hover {
    color: var(--text-main);
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(255, 255, 255, 0.15);
}

.icon-btn.active-glow {
    color: var(--accent-cyan);
    border-color: var(--accent-cyan);
    box-shadow: 0 0 10px var(--accent-cyan-glow);
}

.mode-tabs {
    background: rgba(0, 0, 0, 0.25);
    padding: 4px;
    border-radius: 20px;
    display: flex;
    border: 1px solid rgba(255, 255, 255, 0.03);
}

.tab-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 6px 14px;
    font-size: 0.85rem;
    font-weight: 500;
    border-radius: 16px;
    cursor: pointer;
    transition: all var(--transition-speed) ease;
}

.tab-btn:hover {
    color: var(--text-main);
}

.tab-btn.active {
    background: linear-gradient(135deg, var(--btn-ctrl-bg), rgba(139, 92, 246, 0.3));
    color: var(--text-main);
    box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.05);
}

/* Display Panel Custom Layout */
.display-container {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 20px;
    padding: 24px;
    border: 1px solid rgba(255, 255, 255, 0.02);
    box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.3);
}

.display-wrapper {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: flex-end;
    min-height: 90px;
    overflow: hidden;
}

.display-expression {
    font-size: 1.05rem;
    color: var(--text-muted);
    min-height: 1.5rem;
    word-break: break-all;
    text-align: right;
    margin-bottom: 6px;
    letter-spacing: 0.5px;
    font-weight: 300;
}

.display-value {
    font-size: 2.75rem;
    font-weight: 500;
    width: 100%;
    text-align: right;
    white-space: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: smooth;
    letter-spacing: -0.5px;
}

/* Hide scrollbar for display */
.display-value::-webkit-scrollbar {
    height: 4px;
}
.display-value::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.08);
    border-radius: 2px;
}

/* Keypad Grid Control */
.keypad-grid {
    display: grid;
    gap: 12px;
    transition: all var(--transition-speed) ease;
}

.keypad-grid.standard-mode {
    grid-template-columns: 1fr;
}

.keypad-grid.scientific-mode {
    grid-template-columns: 1.1fr 1fr; /* Scientific left, standard right */
}

.scientific-keys {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    opacity: 0;
    max-height: 0;
    overflow: hidden;
    transform: translateY(-10px);
    transition: opacity var(--transition-speed) ease,
                transform var(--transition-speed) ease;
}

.keypad-grid.scientific-mode .scientific-keys {
    opacity: 1;
    max-height: 500px;
    transform: translateY(0);
}

.standard-keys {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 12px;
}

/* Individual Keys Styling */
.key {
    background: var(--btn-num-bg);
    border: 1px solid rgba(255, 255, 255, 0.04);
    color: var(--text-main);
    font-size: 1.35rem;
    font-weight: 400;
    padding: 16px 0;
    border-radius: 18px;
    cursor: pointer;
    transition: transform 0.08s ease, 
                background-color var(--transition-speed) ease,
                box-shadow var(--transition-speed) ease;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.key:hover {
    background: var(--btn-num-hover);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.key:active, .key.keyboard-active {
    transform: scale(0.93);
    background: rgba(255, 255, 255, 0.15);
}

/* Scientific buttons */
.btn-sci {
    background: var(--btn-sci-bg);
    color: var(--accent-cyan);
    font-size: 1.1rem;
    font-weight: 400;
    border-radius: 16px;
}
.btn-sci:hover {
    background: var(--btn-sci-hover);
    box-shadow: 0 0 10px rgba(0, 242, 254, 0.12);
}
.btn-sci:active, .btn-sci.keyboard-active {
    background: rgba(0, 242, 254, 0.25);
}

/* Operator/Control keys */
.btn-ctrl {
    background: var(--btn-ctrl-bg);
    color: #c084fc;
    font-weight: 500;
}
.btn-ctrl:hover {
    background: var(--btn-ctrl-hover);
    box-shadow: 0 0 10px rgba(139, 92, 246, 0.15);
}
.btn-ctrl:active, .btn-ctrl.keyboard-active {
    background: rgba(139, 92, 246, 0.4);
}

/* Clear key (Red highlight) */
.btn-clear {
    color: #f43f5e !important;
    background: rgba(244, 63, 94, 0.08) !important;
}
.btn-clear:hover {
    background: rgba(244, 63, 94, 0.16) !important;
    box-shadow: 0 0 10px rgba(244, 63, 94, 0.15) !important;
}
.btn-clear:active, .btn-clear.keyboard-active {
    background: rgba(244, 63, 94, 0.3) !important;
}

/* Equals Button (Rose-to-Purple Gradient Accent) */
.btn-equals {
    grid-column: 4;
    grid-row: 4 / span 2;
    background: linear-gradient(135deg, var(--accent-violet), var(--accent-rose));
    color: #fff;
    font-weight: 700;
    font-size: 1.6rem;
    box-shadow: 0 8px 20px -4px var(--accent-rose-glow);
    border: none;
}
.btn-equals:hover {
    box-shadow: 0 8px 24px -2px var(--accent-rose-glow),
                0 0 15px rgba(244, 63, 94, 0.25);
    filter: brightness(1.1);
}
.btn-equals:active, .btn-equals.keyboard-active {
    transform: scale(0.95);
    filter: brightness(0.9);
}

.double-width {
    grid-column: span 2;
}

/* History Drawer Card */
.history-drawer {
    flex: 0 0 280px;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-height: 620px;
    overflow: hidden;
    transition: all var(--transition-speed) ease;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    padding-bottom: 12px;
}

.history-header h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-main);
}

.text-btn {
    background: transparent;
    border: none;
    color: var(--accent-rose);
    font-size: 0.85rem;
    cursor: pointer;
    font-weight: 500;
    padding: 4px 8px;
    border-radius: 8px;
    transition: background-color var(--transition-speed) ease;
}
.text-btn:hover {
    background: rgba(244, 63, 94, 0.08);
}

.history-list-container {
    flex: 1;
    overflow-y: auto;
    padding-right: 4px;
}

/* Custom Scrollbar for History */
.history-list-container::-webkit-scrollbar {
    width: 5px;
}
.history-list-container::-webkit-scrollbar-track {
    background: transparent;
}
.history-list-container::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 10px;
}
.history-list-container::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.15);
}

.history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.history-item {
    padding: 10px 12px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid rgba(255, 255, 255, 0.02);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    transition: all var(--transition-speed) ease;
}

.history-item:hover {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(255, 255, 255, 0.06);
    transform: translateY(-2px);
}

.hist-expr {
    font-size: 0.8rem;
    color: var(--text-muted);
    word-break: break-all;
}

.hist-val {
    font-size: 1.05rem;
    font-weight: 500;
    color: var(--accent-cyan);
    margin-top: 4px;
}

.empty-state {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
    padding: 40px 0;
    font-style: italic;
}

.history-footer {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    padding-top: 12px;
}

/* Tooltip & Visibility Utilities */
.hidden {
    display: none !important;
}

.tooltip {
    position: relative;
}

.tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: -36px;
    left: 50%;
    transform: translateX(-50%) scale(0.9);
    background: rgba(10, 11, 18, 0.9);
    color: #fff;
    padding: 4px 8px;
    font-size: 0.75rem;
    border-radius: 6px;
    white-space: nowrap;
    border: 1px solid rgba(255, 255, 255, 0.08);
    opacity: 0;
    pointer-events: none;
    transition: all 0.15s ease;
    z-index: 100;
}

.tooltip:hover::after {
    opacity: 1;
    transform: translateX(-50%) scale(1);
    bottom: -40px;
}

/* Responsive Breakpoints */
@media (max-width: 820px) {
    .app-container {
        flex-direction: column;
        align-items: center;
    }
    
    .calculator-card {
        width: 100%;
        max-width: 500px;
    }

    .calculator-card.scientific-active {
        max-width: 500px; /* Force stacked layout width */
    }

    .keypad-grid.scientific-mode {
        grid-template-columns: 1fr; /* Stack scientific keys vertically */
    }

    .scientific-keys {
        order: -1; /* Place scientific keys on top when stacked */
        margin-bottom: 8px;
    }

    .history-drawer {
        width: 100%;
        max-width: 500px;
        height: 250px;
    }
}

@media (max-width: 420px) {
    body {
        padding: 10px;
    }
    
    .calculator-card {
        padding: 16px;
        border-radius: 20px;
    }
    
    .display-value {
        font-size: 2.2rem;
    }

    .key {
        font-size: 1.15rem;
        padding: 12px 0;
        border-radius: 12px;
    }

    .btn-sci {
        font-size: 0.95rem;
        border-radius: 12px;
    }
    
    .btn-equals {
        font-size: 1.4rem;
    }
}
