:root {
    --primary-bg: #0f172a;
    --glass-bg: rgba(30, 41, 59, 0.55);
    --glass-btn-bg: rgba(255, 255, 255, 0.03);
    --glass-btn-hover: rgba(255, 255, 255, 0.08);
    --glass-border: rgba(255, 255, 255, 0.08);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.36);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --accent-cyan: #22d3ee;
    --accent-cyan-hover: #06b6d4;
    --accent-red: #fb7185;
    --accent-orange: #fbbf24;
    --accent-purple: #c084fc;
}

body {
    margin: 0;
    font-family: 'Inter', -apple-system, sans-serif;
    background-color: var(--primary-bg);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

/* Stunning Animated Gradient Background */
.background-animation {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    overflow: hidden;
    z-index: 0;
    background: 
      radial-gradient(circle at 15% 50%, rgba(34, 211, 238, 0.1), transparent 50%),
      radial-gradient(circle at 85% 30%, rgba(192, 132, 252, 0.15), transparent 50%),
      radial-gradient(circle at 50% 100%, rgba(251, 113, 133, 0.1), transparent 50%);
}

.background-animation::before,
.background-animation::after {
    content: '';
    position: absolute;
    border-radius: 50%;
    filter: blur(90px);
    animation: float 12s infinite alternate cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 0;
}

.background-animation::before {
    width: 450px; height: 450px;
    background: rgba(34, 211, 238, 0.3);
    top: -100px; left: -100px;
}

.background-animation::after {
    width: 400px; height: 400px;
    background: rgba(192, 132, 252, 0.35);
    bottom: -50px; right: -50px;
    animation-delay: -6s;
    animation-duration: 15s;
}

@keyframes float {
    0% { transform: translate(0, 0) scale(1); }
    100% { transform: translate(80px, 80px) scale(1.15); }
}

/* Glassmorphism Calculator Container */
.calculator-container {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 360px;
    margin: 0 16px;
    box-sizing: border-box;
    padding: 24px;
    border-radius: 32px;
    background: var(--glass-bg);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border: 1px solid var(--glass-border);
    box-shadow: var(--glass-shadow), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    gap: 20px;
    transition: max-width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    transform-style: preserve-3d;
    will-change: transform;
}

/* Scale down to ~70% footprint in scientific mode so the 8 rows fit perfectly on PC without overflowing vertical space */
.calculator-container.sci-mode {
    max-width: 380px;
    padding: 20px;
    gap: 14px;
}

.calculator-container.sci-mode .display-area {
    padding: 18px;
    min-height: 85px;
    border-radius: 20px;
}

.calculator-container.sci-mode .current-input {
    font-size: 2rem;
}

.calculator-container.sci-mode .preview {
    font-size: 1rem;
    min-height: 18px;
}

.calculator-container.sci-mode .keypad {
    gap: 8px;
}

.calculator-container.sci-mode .glass-btn {
    padding: 12px 0;
    font-size: 1.1rem;
    border-radius: 14px;
}

.calculator-container.sci-mode .highlight-cyan,
.calculator-container.sci-mode .operator {
    font-size: 1.25rem;
}

.calculator-container.sci-mode .history-panel {
    top: 60px;
}

/* Header & Toggles */
.calculator-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 4px;
}

.mode-toggle {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
}

.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: var(--glass-btn-bg);
    border: 1px solid var(--glass-border);
    transition: 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 24px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px; width: 16px;
    left: 3px; bottom: 3px;
    background-color: var(--accent-cyan);
    transition: 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(34, 211, 238, 0.5);
}

input:checked + .slider {
    background-color: rgba(192, 132, 252, 0.1);
    border-color: rgba(192, 132, 252, 0.3);
}

input:checked + .slider:before {
    transform: translateX(20px);
    background-color: var(--accent-purple);
    box-shadow: 0 0 10px rgba(192, 132, 252, 0.5);
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.3s, transform 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
    border-radius: 50%;
}

.icon-btn:hover {
    color: var(--text-main);
    transform: rotate(15deg);
}

/* Display Area */
.display-area {
    padding: 24px;
    border-radius: 24px;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--glass-border);
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
    min-height: 100px;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s;
    position: relative;
    overflow: hidden;
}

.display-area:active {
    background: rgba(0, 0, 0, 0.4);
}

.display-area::before {
    content: "Tap to copy";
    position: absolute;
    top: 10px;
    left: 16px;
    font-size: 0.75rem;
    color: var(--text-muted);
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

.display-area:hover::before {
    opacity: 0.6;
}

/* Premium Animations */
.flash-copy {
    color: var(--accent-cyan) !important;
    text-shadow: 0 0 15px rgba(34, 211, 238, 0.6);
    transform: scale(0.98);
}

@keyframes errorShake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-6px); }
    40%, 80% { transform: translateX(6px); }
}

.shake-error {
    animation: errorShake 0.4s cubic-bezier(.36,.07,.19,.97) both;
    color: var(--accent-red) !important;
}

.preview {
    font-size: 1.1rem;
    color: var(--text-muted);
    min-height: 24px;
    width: 100%;
    text-align: right;
    white-space: nowrap;
    overflow-x: auto;
}

.preview::-webkit-scrollbar {
    display: none;
}

.current-input {
    font-size: 2.8rem;
    font-weight: 300;
    letter-spacing: -1px;
    color: var(--text-main);
    width: 100%;
    text-align: right;
    overflow-x: auto;
    white-space: nowrap;
    scroll-behavior: smooth;
}

.current-input::-webkit-scrollbar {
    display: none;
}

/* Keypad Grid Layout */
.keypad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    transition: all 0.4s ease;
}

/* Hide Scientific Buttons by default */
.keypad.basic-mode .sci-btn {
    display: none;
}

/* Scientific buttons spans for perfect layout */
.keypad.scientific-mode .sci-btn[data-value="("],
.keypad.scientific-mode .sci-btn[data-value=")"] {
    grid-column: span 2;
}

/* Button Styling */
.glass-btn {
    background: var(--glass-btn-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-main);
    font-size: 1.25rem;
    font-family: inherit;
    font-weight: 400;
    padding: 18px 0;
    border-radius: 18px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    user-select: none;
}

.glass-btn:hover {
    background: var(--glass-btn-hover);
    transform: translateY(-2px);
    border-color: rgba(255, 255, 255, 0.15);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.glass-btn:active, .glass-btn.active-scale {
    transform: translateY(2px) scale(0.95);
}

.btn-zero {
    grid-column: span 2;
}

/* Thematic Accents */
.highlight-red {
    color: var(--accent-red);
    background: rgba(251, 113, 133, 0.05);
    font-weight: 500;
}
.highlight-red:hover { background: rgba(251, 113, 133, 0.12); }

.highlight-orange {
    color: var(--accent-orange);
    background: rgba(251, 191, 36, 0.05);
    font-weight: 500;
}
.highlight-orange:hover { background: rgba(251, 191, 36, 0.12); }

.highlight-cyan {
    color: var(--primary-bg);
    background: var(--accent-cyan);
    font-weight: 600;
    font-size: 1.5rem;
    box-shadow: 0 4px 20px rgba(34, 211, 238, 0.3);
    border: none;
}
.highlight-cyan:hover {
    background: var(--accent-cyan-hover);
    box-shadow: 0 6px 25px rgba(34, 211, 238, 0.4);
}

.operator {
    color: var(--accent-cyan);
    background: rgba(34, 211, 238, 0.05);
    font-size: 1.4rem;
}
.operator:hover { background: rgba(34, 211, 238, 0.12); }

.sci-btn {
    color: var(--accent-purple);
    background: rgba(192, 132, 252, 0.05);
    font-size: 1.05rem;
}
.sci-btn:hover { background: rgba(192, 132, 252, 0.12); }

/* History Panel */
.history-panel {
    position: absolute;
    top: 70px;
    bottom: 24px;
    left: 24px;
    right: 24px;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border-radius: 24px;
    border: 1px solid var(--glass-border);
    z-index: 10;
    padding: 24px;
    display: flex;
    flex-direction: column;
    opacity: 1;
    transform: translateY(0);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: var(--glass-shadow);
}

.history-panel.hidden {
    opacity: 0;
    transform: translateY(20px) scale(0.98);
    pointer-events: none;
}

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

.history-header h3 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 500;
}

#clearHistoryBtn {
    background: transparent;
    border: 1px solid rgba(251, 113, 133, 0.4);
    color: var(--accent-red);
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
}

#clearHistoryBtn:hover {
    background: rgba(251, 113, 133, 0.1);
    transform: translateY(-1px);
}

.history-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-right: 8px;
}

.history-list::-webkit-scrollbar {
    width: 6px;
}
.history-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
}

.history-item {
    background: rgba(255, 255, 255, 0.03);
    padding: 14px;
    border-radius: 14px;
    cursor: pointer;
    transition: background 0.2s;
    border: 1px solid rgba(255, 255, 255, 0.02);
}

.history-item:hover {
    background: rgba(255, 255, 255, 0.06);
}

.history-item-exp {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-bottom: 6px;
    text-align: right;
    word-break: break-all;
}

.history-item-res {
    font-size: 1.3rem;
    color: var(--accent-cyan);
    font-weight: 500;
    text-align: right;
}

/* === Responsive Device Support === */
@media screen and (max-width: 400px) {
    .calculator-container {
        padding: 16px;
        gap: 16px;
        border-radius: 28px;
    }
    
    .display-area {
        padding: 16px;
        min-height: 85px;
        border-radius: 18px;
    }
    
    .current-input {
        font-size: 2.2rem;
    }

    .keypad {
        gap: 8px;
    }
    
    .glass-btn {
        padding: 14px 0;
        font-size: 1.1rem;
        border-radius: 14px;
    }
    
    .history-panel {
        top: 60px;
        left: 16px;
        right: 16px;
        bottom: 16px;
        padding: 16px;
    }
}

/* Very tall/narrow devices like small iPhones */
@media screen and (max-height: 650px) {
    .calculator-container {
        padding: 16px;
        gap: 12px;
    }
    
    .display-area {
        min-height: 75px; 
    }
    
    .glass-btn {
        padding: 10px 0;
    }
    
    .current-input {
        font-size: 2rem;
    }
}

/* Aggressive shrinking for Scientific Mode on mobile/short devices to fit 8 rows of buttons */
@media screen and (max-width: 400px), screen and (max-height: 750px) {
    .calculator-container.sci-mode {
        padding: 14px;
        gap: 10px;
        border-radius: 24px;
    }
    
    .calculator-container.sci-mode .calculator-header {
        margin-bottom: -4px;
    }
    
    .calculator-container.sci-mode .display-area {
        min-height: 65px;
        padding: 12px 16px;
        border-radius: 16px;
    }
    
    .calculator-container.sci-mode .current-input {
        font-size: 1.8rem;
    }

    .calculator-container.sci-mode .preview {
        font-size: 0.95rem;
        min-height: 20px;
    }

    .calculator-container.sci-mode .keypad {
        gap: 6px;
    }
    
    .calculator-container.sci-mode .glass-btn {
        padding: 8px 0;
        font-size: 1rem;
        border-radius: 12px;
    }
    
    .calculator-container.sci-mode .highlight-cyan,
    .calculator-container.sci-mode .operator {
        font-size: 1.25rem;
    }
    
    .calculator-container.sci-mode .sci-btn {
        font-size: 0.9rem;
    }

    .calculator-container.sci-mode .history-panel {
        top: 55px;
        padding: 12px;
    }
}

/* Developer Credit Banner */
.credit-banner {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-muted);
    letter-spacing: 0.5px;
    z-index: 5;
    text-align: center;
    white-space: nowrap;
    opacity: 0.9;
    user-select: none;
    transition: all 0.3s ease;
}

.credit-banner .heart {
    color: var(--accent-red);
    display: inline-block;
    animation: heartbeat 1.5s infinite;
    margin: 0 4px;
}

@keyframes heartbeat {
    0%, 100% { transform: scale(1); }
    15% { transform: scale(1.25); }
    30% { transform: scale(1); }
    45% { transform: scale(1.25); }
}

@media screen and (max-width: 400px), screen and (max-height: 750px) {
    .credit-banner {
        bottom: 8px;
        font-size: 0.8rem;
        opacity: 0.6;
    }
}
