/* --- Core Variables & Design System --- */
:root {
    /* Theme Glass Dark (Default) */
    --theme-bg-gradient: linear-gradient(135deg, #0e0d16 0%, #151124 50%, #0d0a14 100%);
    --bg-display: rgba(13, 11, 22, 0.45);
    --bg-key: rgba(255, 255, 255, 0.05);
    --bg-key-active: rgba(255, 255, 255, 0.15);
    --bg-function: rgba(255, 255, 255, 0.1);
    --bg-function-active: rgba(255, 255, 255, 0.2);
    --bg-operator: rgba(255, 159, 10, 0.2);
    --bg-operator-active: rgba(255, 159, 10, 0.35);
    --bg-equals: rgba(255, 159, 10, 0.85);
    --bg-equals-active: rgba(255, 159, 10, 1);
    
    --color-text: #ffffff;
    --color-text-muted: rgba(255, 255, 255, 0.5);
    --color-text-operator: #ff9f0a;
    
    --glass-blur: blur(20px);
    --glass-border: 1px solid rgba(255, 255, 255, 0.08);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
    
    --font-primary: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    
    --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-drawer: 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

/* --- Theme overrides --- */

/* Theme Glass Light */
.theme-glass-light {
    --theme-bg-gradient: linear-gradient(135deg, #f0f2f8 0%, #e2e7f3 100%);
    --bg-display: rgba(255, 255, 255, 0.4);
    --bg-key: rgba(255, 255, 255, 0.6);
    --bg-key-active: rgba(0, 0, 0, 0.05);
    --bg-function: rgba(0, 0, 0, 0.05);
    --bg-function-active: rgba(0, 0, 0, 0.12);
    --bg-operator: rgba(0, 122, 255, 0.15);
    --bg-operator-active: rgba(0, 122, 255, 0.25);
    --bg-equals: rgba(0, 122, 255, 0.85);
    --bg-equals-active: rgba(0, 122, 255, 1);
    
    --color-text: #1d1d1f;
    --color-text-muted: rgba(29, 29, 31, 0.5);
    --color-text-operator: #007aff;
    
    --glass-blur: blur(25px);
    --glass-border: 1px solid rgba(255, 255, 255, 0.4);
    --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.06);
}

/* Theme OLED Dark */
.theme-oled {
    --theme-bg-gradient: solid #000000;
    --bg-display: #000000;
    --bg-key: #1c1c1e;
    --bg-key-active: #2c2c2e;
    --bg-function: #3a3a3c;
    --bg-function-active: #48484a;
    --bg-operator: #ff9f0a;
    --bg-operator-active: #ffb340;
    --bg-equals: #30d158;
    --bg-equals-active: #4cd964;
    
    --color-text: #ffffff;
    --color-text-muted: #8e8e93;
    --color-text-operator: #ffffff;
    
    --glass-blur: none;
    --glass-border: 1px solid #2c2c2e;
    --glass-shadow: none;
}

.theme-oled body, .theme-oled .app-container {
    background: #000000 !important;
}

/* Theme Pastel Glow */
.theme-pastel {
    --theme-bg-gradient: linear-gradient(135deg, #ffd1ff 0%, #fad0c4 30%, #ffeed9 60%, #c1e3ff 100%);
    --bg-display: rgba(255, 255, 255, 0.3);
    --bg-key: rgba(255, 255, 255, 0.4);
    --bg-key-active: rgba(255, 255, 255, 0.6);
    --bg-function: rgba(243, 229, 245, 0.6);
    --bg-function-active: rgba(243, 229, 245, 0.85);
    --bg-operator: rgba(144, 202, 249, 0.6);
    --bg-operator-active: rgba(144, 202, 249, 0.85);
    --bg-equals: rgba(179, 157, 219, 0.8);
    --bg-equals-active: rgba(179, 157, 219, 1);
    
    --color-text: #37474f;
    --color-text-muted: rgba(55, 71, 79, 0.6);
    --color-text-operator: #5e35b1;
    
    --glass-blur: blur(20px);
    --glass-border: 1px solid rgba(255, 255, 255, 0.4);
    --glass-shadow: 0 8px 32px 0 rgba(142, 120, 160, 0.12);
}

/* --- Basic Setup & Reset --- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    font-family: var(--font-primary);
    overflow: hidden;
    background: #000000;
}

body {
    background: var(--theme-bg-gradient);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background var(--transition-normal);
}

/* --- App Layout Shell --- */
.app-container {
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-width: 440px; /* Limit width on desktop/tablets */
    display: flex;
    flex-direction: column;
    position: relative;
    padding: 0 20px;
    background: transparent;
    overflow: hidden;
}

/* Safe Area Spacers */
.status-bar-spacer {
    height: env(safe-area-inset-top, 20px);
    min-height: 20px;
    width: 100%;
    flex-shrink: 0;
}

.home-indicator-spacer {
    height: env(safe-area-inset-bottom, 12px);
    min-height: 12px;
    width: 100%;
    flex-shrink: 0;
}

/* --- Header Section --- */
.app-header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    margin-top: 5px;
}

.app-title {
    font-size: 1.1rem;
    font-weight: 500;
    color: var(--color-text);
    opacity: 0.85;
    letter-spacing: 0.5px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.03);
    color: var(--color-text);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.icon-btn:active {
    background: rgba(255, 255, 255, 0.12);
    transform: scale(0.93);
}

/* --- Display Section --- */
.display-area {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    padding: 24px 8px 16px 8px;
    text-align: right;
    word-break: break-all;
    position: relative;
}

.display-history-expr {
    font-size: 1.25rem;
    font-weight: 300;
    color: var(--color-text-muted);
    min-height: 1.7rem;
    margin-bottom: 6px;
    transition: all var(--transition-fast);
    white-space: nowrap;
    overflow-x: auto;
    max-width: 100%;
}

.display-main-wrapper {
    width: 100%;
    display: flex;
    justify-content: flex-end;
    align-items: flex-end;
    overflow: hidden;
}

.display-main-val {
    font-size: 4.8rem;
    font-weight: 200;
    color: var(--color-text);
    line-height: 1;
    white-space: nowrap;
    transition: font-size 0.1s ease;
    direction: ltr;
}

.swipe-hint {
    font-size: 0.7rem;
    color: var(--color-text-muted);
    opacity: 0;
    margin-top: 4px;
    transition: opacity var(--transition-normal);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.display-area:hover .swipe-hint {
    opacity: 0.6;
}

/* --- Calculator Keypad --- */
.keyboard-area {
    flex-shrink: 0;
    padding-bottom: 12px;
}

.calc-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
}

/* Key Base Style */
.key {
    aspect-ratio: 1 / 1;
    border-radius: 24px;
    border: var(--glass-border);
    background: var(--bg-key);
    color: var(--color-text);
    font-size: 1.8rem;
    font-weight: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--glass-shadow);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    transition: transform 0.08s cubic-bezier(0.175, 0.885, 0.32, 1.275), background var(--transition-fast), border var(--transition-fast), box-shadow var(--transition-fast);
}

/* Tap Interaction Dynamics */
.key:active {
    transform: scale(0.9);
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}

.key.digit:active {
    background: var(--bg-key-active);
}

/* Key Categories */
.key.function {
    background: var(--bg-function);
    font-weight: 400;
    font-size: 1.55rem;
}

.key.function:active {
    background: var(--bg-function-active);
}

.key.operator {
    background: var(--bg-operator);
    color: var(--color-text-operator);
    font-weight: 500;
    font-size: 2rem;
}

.key.operator:active {
    background: var(--bg-operator-active);
}

.key.equals {
    background: var(--bg-equals);
    color: #ffffff;
    font-size: 2.1rem;
}

.key.equals:active {
    background: var(--bg-equals-active);
}

/* Zero key spans 2 columns in portrait mode */
.key.zero {
    grid-column: span 2;
    aspect-ratio: auto;
    border-radius: 24px;
    justify-content: flex-start;
    padding-left: 32px;
}

/* Scientific Keys - Hidden in Portrait Mode */
.key.scientific {
    display: none;
    font-size: 1.15rem;
    background: rgba(255, 255, 255, 0.02);
}

/* --- Responsive Landscape Mode (Scientific Grid) --- */
@media (orientation: landscape) {
    .app-container {
        max-width: 900px;
        padding: 0 20px;
    }

    .app-header {
        height: 38px;
        margin-top: 2px;
    }

    .display-area {
        padding: 8px 8px 8px 8px;
    }

    .display-history-expr {
        font-size: 1.1rem;
        margin-bottom: 2px;
        min-height: 1.3rem;
    }

    .display-main-val {
        font-size: 3rem;
    }

    .keyboard-area {
        padding-bottom: 6px;
    }

    .calc-grid {
        grid-template-columns: repeat(10, 1fr);
        gap: 8px;
    }

    .key {
        border-radius: 16px;
        aspect-ratio: auto;
        height: 48px; /* Fixed height to fit multiple landscape screens */
        font-size: 1.25rem;
    }

    .key.scientific {
        display: flex;
        font-weight: 300;
    }

    .key.zero {
        grid-column: span 1;
        justify-content: center;
        padding-left: 0;
    }

    .key.operator {
        font-size: 1.4rem;
    }
    
    .key.equals {
        font-size: 1.4rem;
    }
}

/* For very short devices in portrait, decrease layout spacings */
@media (max-height: 680px) and (orientation: portrait) {
    .calc-grid {
        gap: 8px;
    }
    .key {
        border-radius: 18px;
        font-size: 1.5rem;
    }
    .key.zero {
        border-radius: 18px;
        padding-left: 24px;
    }
    .display-main-val {
        font-size: 4rem;
    }
}

/* --- Sliding Sheet Drawers --- */
.drawer {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, 100%);
    width: 100%;
    max-width: 440px;
    height: 75vh;
    height: 75dvh;
    background: rgba(30, 27, 45, 0.7);
    backdrop-filter: blur(30px);
    -webkit-backdrop-filter: blur(30px);
    border-top-left-radius: 30px;
    border-top-right-radius: 30px;
    border: var(--glass-border);
    border-bottom: none;
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.4);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: transform var(--transition-drawer);
}

.drawer.open {
    transform: translate(-50%, 0);
}

.theme-glass-light .drawer {
    background: rgba(255, 255, 255, 0.75);
    box-shadow: 0 -10px 40px rgba(31, 38, 135, 0.08);
}

.theme-oled .drawer {
    background: #1c1c1e;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border-top: 1px solid #2c2c2e;
}

/* Drawer Header */
.drawer-header {
    padding: 20px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    flex-shrink: 0;
}

.theme-glass-light .drawer-header {
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.drawer-header h3 {
    font-size: 1.3rem;
    font-weight: 500;
    color: var(--color-text);
}

.close-btn {
    font-size: 2rem;
    font-weight: 200;
    color: var(--color-text);
    opacity: 0.6;
    border: none;
    background: transparent;
    cursor: pointer;
    line-height: 1;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all var(--transition-fast);
}

.close-btn:active {
    opacity: 1;
    background: rgba(255, 255, 255, 0.08);
}

/* Drawer Content */
.drawer-content {
    flex-grow: 1;
    padding: 24px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 28px;
    padding-bottom: calc(24px + env(safe-area-inset-bottom, 12px));
}

/* --- Backdrop Overlay --- */
.backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.4);
    opacity: 0;
    visibility: hidden;
    z-index: 999;
    transition: opacity var(--transition-normal), visibility var(--transition-normal);
}

.backdrop.visible {
    opacity: 1;
    visibility: visible;
}

/* --- Settings Components --- */
.setting-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.setting-group h4 {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--color-text-muted);
}

/* Theme Grid Selector */
.theme-selector {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.theme-opt {
    border: var(--glass-border);
    background: rgba(255, 255, 255, 0.02);
    border-radius: 16px;
    padding: 12px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.theme-opt:active {
    transform: scale(0.96);
}

.theme-opt.active {
    background: rgba(255, 255, 255, 0.08);
    border-color: var(--color-text-operator);
}

.theme-preview {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    flex-shrink: 0;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.bg-dark { background: linear-gradient(135deg, #0e0d16 0%, #151124 100%); }
.bg-light { background: linear-gradient(135deg, #f0f2f8 0%, #e2e7f3 100%); }
.bg-oled { background: #000000; border-color: #2c2c2e; }
.bg-pastel { background: linear-gradient(135deg, #ffd1ff 0%, #c1e3ff 100%); }

.theme-name {
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--color-text);
}

/* Switches and items */
.preference-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.preference-item span {
    font-size: 1rem;
    color: var(--color-text);
}

/* Switch styling */
.switch {
    position: relative;
    display: inline-block;
    width: 51px;
    height: 31px;
}

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

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.1);
    transition: .3s;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.slider:before {
    position: absolute;
    content: "";
    height: 27px;
    width: 27px;
    left: 1px;
    bottom: 1px;
    background-color: white;
    transition: .3s;
    box-shadow: 0 3px 8px rgba(0,0,0,0.15);
}

input:checked + .slider {
    background-color: #30d158; /* iOS Green */
}

input:checked + .slider:before {
    transform: translateX(20px);
}

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

/* About / Install Button */
.about p {
    font-size: 0.9rem;
    color: var(--color-text-muted);
    line-height: 1.4;
}

.action-btn {
    width: 100%;
    padding: 14px;
    border-radius: 16px;
    border: none;
    background: var(--bg-equals);
    color: #ffffff;
    font-weight: 500;
    font-size: 1rem;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.action-btn:active {
    background: var(--bg-equals-active);
    transform: scale(0.97);
}

.action-btn-danger {
    width: 100%;
    padding: 14px;
    border-radius: 16px;
    border: none;
    background: rgba(255, 69, 58, 0.15);
    color: #ff453a;
    font-weight: 500;
    font-size: 1rem;
    cursor: pointer;
    transition: all var(--transition-fast);
    margin-top: auto;
}

.action-btn-danger:active {
    background: rgba(255, 69, 58, 0.25);
    transform: scale(0.97);
}

/* --- History Drawer Content --- */
.history-list-wrapper {
    flex-grow: 1;
    overflow-y: auto;
}

#history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

#history-list li {
    padding: 12px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.02);
    border: var(--glass-border);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    cursor: pointer;
    transition: all var(--transition-fast);
}

#history-list li:active {
    background: rgba(255, 255, 255, 0.06);
    transform: scale(0.98);
}

.hist-expr {
    font-size: 0.95rem;
    color: var(--color-text-muted);
}

.hist-val {
    font-size: 1.4rem;
    font-weight: 500;
    color: var(--color-text);
}

.empty-history {
    text-align: center;
    color: var(--color-text-muted);
    font-size: 0.95rem;
    padding: 40px 0 !important;
    background: transparent !important;
    border: none !important;
    cursor: default !important;
}

.empty-history:active {
    transform: none !important;
}
