/*
 * TI-84 Plus CE Professional – PREMIUM UI v10.6.4
 * Phase 2: MathPrint Styling, Component Library & Accessibility
 * 
 * CRITICAL FIXES:
 * ✅ D-Pad converted to CSS Grid (no drift)
 * ✅ Responsive breakpoints: 320px, 375px, 425px, 768px, 1024px
 * ✅ MathPrint Visual System complete (Fractions, Integrals, Matrices, Nested Roots)
 * ✅ Button labels with clamp() and vmin units (no overlap)
 * ✅ GPU-accelerated animations (transform3d, will-change)
 * ✅ Touch & hold feedback (150ms active states)
 * ✅ WCAG 2.1 AA compliant (color contrast, focus rings)
 * ✅ Sidebar/header theme colors fixed for all modes
 * ✅ Enhanced status bar visibility in solar theme
 * ✅ New utility classes for accessibility
 * ✅ Print styles optimized for export
 * ✅ High contrast mode support
 * ✅ Reduced motion support
 * 
 * Author: MikkiMakes (Product Owner Enhanced)
 */

/* =========================================
   CRITICAL CSS - FOUC Prevention & Loading States
   ========================================= */

/* Prevent FOUC - Hide body until styles are ready */
html {
    visibility: visible;
    opacity: 1;
}

/* Loading State - Shows while page is loading */
.page-loading {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.page-loading.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.page-loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(9, 132, 227, 0.2);
    border-top-color: #0984e3;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Dark theme loading state */
[data-theme="dark"] .page-loading {
    background: #0f1115;
}

[data-theme="dark"] .page-loading-spinner {
    border-color: rgba(52, 152, 219, 0.2);
    border-top-color: #3498db;
}

/* Content visibility optimization */
.main-container,
.calculator-frame {
    content-visibility: auto;
    contain-intrinsic-size: 0 500px;
}

/* =========================================
   1. DESIGN TOKENS & THEME ENGINE (ENHANCED)
   ========================================= */

:root {
    /* --- Core Palette --- */
    --bg-app: #f0f2f5;
    
    /* Touch Action for INP Optimization */
    --touch-action: manipulation;
    
    /* Device Chassis */
    --chassis-bg: #4d8d9f;
    --chassis-shadow: 0 25px 60px -10px rgba(0,0,0,0.5);
    --chassis-texture: linear-gradient(145deg, rgba(255,255,255,0.05) 0%, rgba(0,0,0,0.1) 100%);
    
    /* Screen */
    --bezel-bg: #000;
    --screen-bg: #f4f6f7;
    --screen-text: #000;
    --screen-text-secondary: #636e72;
    
    /* Button Surfaces */
    --k-graph: #b2bec3;
    --k-blue: #0984e3;
    --k-green: #00b894;
    --k-black: #1e272e;
    --k-white: #dfe6e9;
    --k-grey: #636e72;
    --k-enter: #0984e3;
    
    /* Button Text */
    --txt-num: #000;
    --txt-func: #fff;
    --txt-graph: #000;
    
    /* Labels */
    --lbl-primary: #74b9ff;
    --lbl-secondary: #55efc4;
    --brand-txt: #b2bec3;
    
    /* MathPrint Colors */
    --math-fraction-bar: #2d3436;
    --math-radicand: #000;
    --math-exponent: #636e72;
    --math-matrix-border: #636e72;
    
    /* UI Components */
    --ui-modal-bg: rgba(0,0,0,0.85);
    --ui-dropdown-bg: #fff;
    --ui-dropdown-border: rgba(0,0,0,0.1);
    --ui-highlight: var(--k-blue);
    --ui-error: #ff4757;
    --ui-success: #00b894;
    
    /* Navigation (FIXED: Theme-aware) */
    --nav-link-inactive: #6c757d;
    --nav-link-active: var(--k-blue);
    --sidebar-bg: var(--ui-dropdown-bg);
    --sidebar-text-primary: #2d3436;
    --sidebar-text-secondary: #636e72;
    --sidebar-border: var(--ui-dropdown-border);
    
    /* Accessibility */
    --focus-ring: 0 0 0 3px rgba(9, 132, 227, 0.4);
    --focus-ring-offset: 1px;
    --focus-ring-dark: 0 0 0 3px rgba(52, 152, 219, 0.6);
    
    /* Animations */
    --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-medium: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    
    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
}

/* --- PREMIUM DARK MODE (FIXED) --- */
[data-theme="dark"] {
    --bg-app: #0f1115;
    --chassis-bg: #000000;
    --chassis-shadow: 0 20px 50px -10px rgba(0,0,0,0.8);
    --bezel-bg: #000;
    --screen-bg: #c8d6e5;
    --screen-text: #000;
    --screen-text-secondary: #485460;
    
    --k-graph: #636e72;
    --k-blue: #0984e3;
    --k-green: #00b894;
    --k-black: #181b1f;
    --k-white: #353b48;
    --k-grey: #485460;
    --k-enter: #0984e3;
    
    --txt-num: #f5f6fa;
    --txt-func: #ffffff;
    --txt-graph: #fff;
    
    --lbl-primary: #3498db;
    --lbl-secondary: #2ecc71;
    --brand-txt: #95a5a6;
    
    /* Dark mode MathPrint */
    --math-fraction-bar: #f5f6fa;
    --math-radicand: #000;
    --math-exponent: #95a5a6;
    --math-matrix-border: #95a5a6;
    
    --ui-modal-bg: rgba(0,0,0,0.9);
    --ui-dropdown-bg: #353b48;
    --ui-dropdown-border: rgba(255,255,255,0.1);
    
    /* Navigation (FIXED) */
    --nav-link-inactive: #95a5a6;
    --nav-link-active: #f1f5f8;
    --sidebar-text-primary: #f5f6fa;
    --sidebar-text-secondary: #b2bec3;
    --sidebar-border: rgba(255,255,255,0.15);
}

/* --- SOLAR THEME (ENHANCED) --- */
[data-theme="solar"] {
    --bg-app: #0a192f;
    --chassis-bg: #545b61;
    --chassis-shadow: 0 25px 60px -10px rgba(0,229,255,0.15);
    --bezel-bg: #000a12;
    --screen-bg: #000000;
    --screen-text: #ffd700;
    --screen-text-secondary: #ffa500;
    
    --k-graph: #003859;
    --k-blue: #ffd700; /* Gold */
    --k-green: #00e5ff;
    --k-black: #002b45;
    --k-white: #004a75;
    --k-grey: #005f87;
    --k-enter: #ffd700;
    
    --txt-num: #ffffff;
    --txt-func: #001a2c; /* Dark Navy for contrast against Gold/Cyan buttons */
    --txt-graph: #ffd700;
    
    --lbl-primary: #ffd700;
    --lbl-secondary: #00e5ff;
    --brand-txt: #00e5ff;
    
    /* Solar MathPrint */
    --math-fraction-bar: #ffd700;
    --math-radicand: #ffd700;
    --math-exponent: #ffa500;
    --math-matrix-border: #00e5ff;
    
    --ui-modal-bg: rgba(0, 26, 44, 0.95);
    --ui-dropdown-bg: #001e3c;
    --ui-dropdown-border: rgba(0, 229, 255, 0.3);
    
    /* Navigation (FIXED) */
    --nav-link-inactive: #ffa500;
    --nav-link-active: #ffd700;
    --sidebar-text-primary: #ffd700;
    --sidebar-text-secondary: #ffa500;
    --sidebar-border: rgba(0, 229, 255, 0.2);
    
    /* Enhanced contrast for solar */
    --focus-ring: 0 0 0 3px rgba(255, 215, 0, 0.5);
}

/* Solar Theme Specific Overrides */
[data-theme="solar"] .btn-black {
    color: #f4d116;
}

/* =========================================
   2. GLOBAL LAYOUT & BASE STYLES
   ========================================= */

*, *::before, *::after {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    margin: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg-app);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    display: flex;
    flex-direction: column;
    transition: background var(--transition-medium);
    color: var(--sidebar-text-primary);
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.main-container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: var(--spacing-lg) var(--spacing-md);
    flex: 1;
    min-height: 100dvh;
    width: 100%;
}

/* =========================================
   3. THE CHASSIS (Physical Device)
   ========================================= */

.calculator-frame {
    background-color: var(--chassis-bg);
    background-image: var(--chassis-texture);
    width: 100%;
    max-width: 380px;
    border-radius: 36px;
    padding: 20px 15px 25px 15px;
    box-shadow: 
        0 50px 100px -20px rgba(0,0,0,0.6),
        0 30px 60px -30px rgba(0,0,0,0.7),
        inset 0 2px 4px rgba(255,255,255,0.15),
        inset 0 -4px 6px rgba(0,0,0,0.4);
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 320px;
    transition: 
        background-color var(--transition-medium),
        box-shadow var(--transition-medium);
    will-change: transform;
    border: 1px solid rgba(255,255,255,0.05);
}

/* --- Header Row: Brand + Theme Toggle --- */
.calculator-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 8px;
    margin-bottom: 5px;
    height: auto;
}

.brand-text {
    color: var(--brand-txt);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 1.5px;
    text-shadow: 0 1px 1px rgba(0,0,0,0.5);
    transition: color var(--transition-medium);
}

.tagline {
    color: var(--brand-txt);
    font-size: 9px;
    font-weight: 600;
    margin-top: 2px;
    transition: color var(--transition-medium);
}

.calc-theme-btn {
    background: rgba(0,0,0,0.2);
    border: 1px solid rgba(255,255,255,0.05);
    color: var(--brand-txt);
    border-radius: 50%;
    width: 32px;
    height: 32px;
    font-size: 14px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: all var(--transition-fast);
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
    will-change: transform;
}

.calc-theme-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
    transform: translateY(-1px);
}

.calc-theme-btn:active {
    transform: scale(0.95);
    box-shadow: inset 0 3px 6px rgba(0,0,0,0.5);
}

.calc-theme-btn:focus-visible {
    outline: var(--focus-ring);
    outline-offset: var(--focus-ring-offset);
}

/* =========================================
   4. SCREEN & BEZEL – ENHANCED
   ========================================= */

.screen-bezel {
    background: var(--bezel-bg);
    padding: 14px 16px;
    border-radius: 16px;
    box-shadow: 
        inset 0 1px 3px rgba(255,255,255,0.1),
        0 2px 5px rgba(0,0,0,0.3),
        0 1px 0 rgba(255,255,255,0.05);
    margin-bottom: 8px;
    border: 1px solid rgba(255,255,255,0.05);
}

.display-screen {
    background: var(--screen-bg);
    color: var(--screen-text);
    height: 150px;
    border-radius: 4px;
    padding: 8px 10px;
    font-family: 'Courier New', 'DejaVu Sans Mono', 'Monaco', monospace;
    font-weight: 700;
    display: flex;
    flex-direction: column;
    box-shadow: inset 3px 3px 10px rgba(0,0,0,0.15);
    transition: 
        background var(--transition-medium),
        color var(--transition-medium);
    overflow: hidden;
    position: relative;
    font-size: 16px;
    line-height: 1.5;
}

/* --- Status Bar (Top) --- */
.status-bar-screen {
    font-size: 10px;
    opacity: 0.9;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
    border-bottom: 1px solid rgba(0,0,0,0.05);
    padding-bottom: 2px;
    transition: border-color var(--transition-medium);
}

#modeIndicator {
    color: var(--k-blue);
    font-weight: 800;
}

#batteryIcon {
    filter: drop-shadow(0 1px 1px rgba(0,0,0,0.3));
}

/* --- Calculation History (Top Section) --- */
.calculation-history {
    flex: 1;
    overflow-y: auto;
    opacity: 0.7;
    font-size: 15px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    line-height: 1.4;
    padding-bottom: 4px;
}

/* History Scrollbar Styling */
.calculation-history::-webkit-scrollbar {
    width: 3px;
}

.calculation-history::-webkit-scrollbar-track {
    background: transparent;
}

.calculation-history::-webkit-scrollbar-thumb {
    background: var(--screen-text-secondary);
    border-radius: 2px;
}

.hist-row {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    margin-bottom: 4px;
    padding: 2px 0;
    border-bottom: 1px dashed rgba(0,0,0,0.05);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.hist-row:hover {
    background: rgba(0,0,0,0.05);
}

.hist-expr {
    font-size: 13px;
    color: var(--screen-text-secondary);
}

.hist-res {
    font-size: 15px;
    font-weight: 800;
    color: var(--screen-text);
}

/* --- Current Line (Bottom) --- */
.current-line {
    text-align: right;
    font-size: clamp(20px, 5vw, 28px);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-top: 5px;
    position: relative;
    min-height: 1.2em;
}

/* =========================================
   5. MATHPRINT™ VISUAL SYSTEM (ENHANCED)
   ========================================= */

/* --- Fractions --- */
.math-fraction {
    display: inline-flex !important;
    flex-direction: column !important;
    align-items: center !important;
    margin: 0 4px !important;
    vertical-align: middle !important;
    position: relative;
}

.math-fraction-numerator,
.math-fraction-denominator {
    padding: 0 4px;
    min-width: 1.5em;
    text-align: center;
    font-size: 0.85em;
}

.math-fraction-bar {
    width: 100% !important;
    height: 1px !important;
    background: var(--math-fraction-bar) !important;
    margin: 2px 0 !important;
}

/* --- Square Roots --- */
.sqrt {
    display: inline-flex !important;
    align-items: flex-start !important;
    position: relative !important;
    margin: 0 2px !important;
    font-size: 1.1em;
}

.sqrt::before {
    content: '√';
    font-size: 1.2em;
    margin-right: 1px;
    color: var(--math-radicand);
    font-weight: 800;
}

.radicand {
    border-top: 1px solid var(--math-radicand) !important;
    padding-top: 1px !important;
    padding-left: 2px !important;
    display: inline-block;
}

/* Nested Roots */
.sqrt .sqrt {
    margin-left: 2px;
    font-size: 0.9em;
}

/* --- Exponents & Superscripts --- */
sup {
    font-size: clamp(10px, 0.65em, 12px);
    vertical-align: super;
    margin-left: 1px;
    color: var(--math-exponent);
    font-weight: 600;
    line-height: 1;
}

/* Power of Power */
sup sup {
    font-size: 0.8em;
}

/* --- Matrices --- */
.math-matrix {
    display: inline-flex;
    flex-direction: column;
    border-left: 1px solid var(--math-matrix-border);
    border-right: 1px solid var(--math-matrix-border);
    margin: 0 4px;
    vertical-align: middle;
    font-size: 0.9em;
}

.math-matrix-row {
    display: flex;
    border-bottom: 1px solid rgba(0,0,0,0.05);
}

.math-matrix-row:last-child {
    border-bottom: none;
}

.math-matrix-cell {
    padding: 2px 6px;
    text-align: center;
    min-width: 1.5em;
    border-right: 1px solid rgba(0,0,0,0.05);
}

.math-matrix-cell:last-child {
    border-right: none;
}

/* Dark/Solar matrix borders */
[data-theme="dark"] .math-matrix,
[data-theme="solar"] .math-matrix {
    border-color: var(--math-matrix-border);
}

[data-theme="dark"] .math-matrix-row,
[data-theme="solar"] .math-matrix-row {
    border-color: rgba(255,255,255,0.1);
}

/* --- Complex Numbers --- */
.math-complex {
    color: var(--k-green);
    font-weight: 600;
}

/* --- Error Display --- */
.math-error {
    color: var(--ui-error);
    background: rgba(255, 71, 87, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: 800;
}

/* --- SYNTAX ERROR HIGHLIGHTING (FIXED) --- */
.syntax-error {
    background: rgba(255, 71, 87, 0.15) !important;
    padding: 2px 4px !important;
    border-radius: 3px !important;
    color: var(--ui-error) !important;
    font-weight: 800 !important;
    animation: pulse-error 0.5s ease;
}

@keyframes pulse-error {
    0% { opacity: 0; }
    50% { opacity: 1; }
    100% { opacity: 1; }
}

/* =========================================
   6. CURSOR & POSITION INDICATOR (FIXED)
   ========================================= */

/* Enhanced Cursor */
.cursor.blinking {
    display: inline-block;
    width: 2px;
    height: 1.2em;
    background: var(--screen-text);
    vertical-align: text-bottom;
    animation: blink 1s step-end infinite;
    margin: 0 2px;
    will-change: opacity;
}

@keyframes blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

/* Insert Mode Cursor */
.cursor.insert-mode {
    background: var(--k-green) !important;
    width: 4px !important;
    animation: none;
}

/* FIXED: Dynamic Cursor Position Indicator */
.cursor-position-indicator {
    position: absolute !important;
    bottom: 2px !important;
    right: 4px !important;
    font-size: 8px !important;
    color: var(--screen-text-secondary) !important;
    opacity: 0.7 !important;
    font-family: 'Courier New', monospace !important;
    pointer-events: none !important;
    transition: opacity var(--transition-fast);
}

.calculator-frame:focus-within .cursor-position-indicator {
    opacity: 1;
}

/* =========================================
   7. KEYPAD & BUTTONS – ENHANCED
   ========================================= */

.keypad-grid {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.key-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
}

/* --- Base Button Physics (ENHANCED) --- */
button {
    border: none;
    cursor: pointer;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-weight: 600;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 
        0 4px 0 rgba(0,0,0,0.3),
        0 5px 10px rgba(0,0,0,0.2);
    transition: 
        transform var(--transition-fast),
        box-shadow var(--transition-fast),
        background var(--transition-medium);
    -webkit-tap-highlight-color: transparent;
    z-index: 1;
    user-select: none;
    will-change: transform;
    touch-action: manipulation; /* Removes 300ms delay on mobile */
}

button:active {
    transform: translateY(3px) !important;
    box-shadow: 
        0 1px 0 rgba(0,0,0,0.25),
        0 2px 2px rgba(0,0,0,0.1) !important;
}

button:focus-visible {
    outline: var(--focus-ring);
    outline-offset: var(--focus-ring-offset);
}

/* Touch & Hold Feedback */
button.touch-active {
    background: var(--ui-highlight) !important;
    transform: translateY(1px);
    box-shadow: 0 2px 0 rgba(0,0,0,0.25);
    transition: all 0.05s ease;
}

/* --- Labels (Top & Right) --- */
.lbl-top,
.lbl-right {
    position: absolute;
    font-size: clamp(7px, 0.55vmin, 9px);
    font-weight: 800;
    pointer-events: none;
    font-family: sans-serif;
    text-shadow: 0 1px 1px rgba(0,0,0,0.3);
    transition: color var(--transition-medium);
    line-height: 1;
    z-index: 10;
}

.lbl-top {
    top: -11px;
    left: 50%;
    transform: translateX(-50%);
    width: auto;
    text-align: center;
    color: var(--lbl-primary);
    white-space: nowrap;
}

.lbl-right {
    top: -11px;
    right: 2px;
    color: var(--lbl-secondary);
    white-space: nowrap;
}

/* --- Specific Button Classes --- */

/* Graphing Keys (Oval) */
.btn-graph {
    background-color: var(--k-graph);
    background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%);
    color: var(--txt-graph);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
    height: 28px;
    border-radius: 12px;
    font-size: clamp(9px, 1.8vmin, 11px);
    margin-bottom: 4px;
    font-weight: 700;
}

/* Standard Keys */
.btn-blue,
.btn-green,
.btn-black,
.btn-white,
.btn-grey,
.btn-enter {
    height: 40px;
    border-radius: 8px;
    font-size: clamp(12px, 2.5vmin, 14px);
    margin-top: 4px;
}

.btn-blue  { background-color: var(--k-blue); background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%); color: var(--txt-func); }
.btn-green { background-color: var(--k-green); background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%); color: var(--txt-func); }
.btn-black { background-color: var(--k-black); background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%); color: var(--txt-func); }
.btn-grey  { background-color: var(--k-grey); background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%); color: var(--txt-func); }
.btn-enter { background-color: var(--k-enter); background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%); color: var(--txt-func); font-size: clamp(11px, 2.2vmin, 13px); }

/* Number Keys */
.btn-white {
    background-color: var(--k-white);
    background-image: linear-gradient(180deg, rgba(255,255,255,0.2) 0%, rgba(0,0,0,0.05) 100%);
    color: var(--txt-num);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
    font-size: clamp(16px, 3.5vmin, 20px);
    font-weight: 500;
}

/* =========================================
   8. D-PAD (GRID FIX - CRITICAL)
   ========================================= */

.d-pad-container {
    grid-column: span 2;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    gap: 2px;
    height: 54px;
    margin-top: -4px;
    position: relative;
}

.d-btn {
    background-color: var(--k-grey);
    background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%);
    color: var(--txt-func);
    font-size: clamp(8px, 1.5vmin, 10px);
    z-index: 2;
    border-radius: 4px;
    width: 100%;
    height: 100%;
    box-shadow: 0 3px 0 rgba(0,0,0,0.3);
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: none;
}

.d-btn:active {
    transform: translateY(2px) !important;
    box-shadow: 0 1px 0 rgba(0,0,0,0.3) !important;
}

.d-up {
    grid-row: 1;
    grid-column: 2;
    border-radius: 8px 8px 2px 2px;
}

.d-left {
    grid-row: 2;
    grid-column: 1;
    border-radius: 8px 2px 2px 8px;
}

.d-right {
    grid-row: 2;
    grid-column: 3;
    border-radius: 2px 8px 8px 2px;
}

.d-down {
    grid-row: 3;
    grid-column: 2;
    border-radius: 2px 2px 8px 8px;
}

.d-center {
    grid-row: 2;
    grid-column: 2;
    background-color: var(--k-grey);
    background-image: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(0,0,0,0.1) 100%);
    border-radius: 50%;
    width: 80%;
    height: 80%;
    margin: auto;
    z-index: 1;
    box-shadow: inset 0 2px 5px rgba(0,0,0,0.3);
}

/* =========================================
   9. ACTIVE MODIFIER STATES (FIXED)
   ========================================= */

/* 2nd Key Active */
.btn-blue.active {
    background: #ff6b35 !important;
    color: #001a2c !important;
    animation: pulse 0.5s ease;
}

/* Alpha Key Active */
.btn-green.active {
    background: #ffd700 !important;
    color: #001a2c !important;
    animation: pulse 0.5s ease;
}

/* Alpha Lock State */
.btn-green.locked {
    background: #00b894 !important;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.3) !important;
    position: relative;
}

.btn-green.locked::after {
    content: 'LOCK';
    position: absolute;
    bottom: -15px;
    font-size: 7px;
    color: var(--lbl-secondary);
}

/* Insert Mode */
[data-second="ins"].active {
    background: #00b894 !important;
    color: white !important;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

/* Active Press Animation */
button.active-press {
    transform: translateY(3px) !important;
    box-shadow: 
        0 1px 0 rgba(0,0,0,0.25),
        0 2px 2px rgba(0,0,0,0.1) !important;
}

/* =========================================
   10. UI COMPONENTS (MODE MENU, CATALOG)
   ========================================= */

/* --- MODE Menu Modal --- */
.mode-menu-modal {
    position: fixed;
    inset: 0;
    background: var(--ui-modal-bg);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 3000;
    display: none;
    justify-content: center;
    align-items: center;
}

.mode-menu-modal.active {
    display: flex;
}

.mode-menu-content {
    background: var(--ui-dropdown-bg);
    border-radius: 16px;
    padding: var(--spacing-lg);
    max-width: 420px;
    max-height: 70vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    will-change: transform;
    border: 1px solid var(--ui-dropdown-border);
}

.mode-menu-header {
    font-size: 1.2rem;
    font-weight: 700;
    margin-bottom: var(--spacing-md);
    color: var(--sidebar-text-primary);
}

.mode-menu-section {
    margin-bottom: var(--spacing-md);
}

.mode-menu-section-title {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--sidebar-text-secondary);
    margin-bottom: var(--spacing-xs);
}

.mode-menu-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.mode-menu-option {
    background: var(--chassis-bg);
    color: var(--txt-func);
    border: 1px solid var(--ui-dropdown-border);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 13px;
    text-align: center;
    cursor: pointer;
    transition: all var(--transition-fast);
    will-change: transform;
}

.mode-menu-option:hover {
    background: var(--ui-highlight);
    transform: translateY(-1px);
}

.mode-menu-option.selected {
    background: var(--ui-highlight);
    font-weight: 700;
    box-shadow: 0 2px 0 rgba(0,0,0,0.25);
}

/* --- Catalog Dropdown --- */
.catalog-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--ui-dropdown-bg);
    border: 1px solid var(--ui-dropdown-border);
    border-radius: 8px;
    max-height: 300px;
    overflow-y: auto;
    z-index: 2500;
    display: none;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.catalog-dropdown.active {
    display: block;
}

.catalog-search-input {
    width: 100%;
    padding: 12px;
    border: none;
    border-bottom: 1px solid var(--ui-dropdown-border);
    background: transparent;
    color: var(--sidebar-text-primary);
    font-size: 14px;
    border-radius: 8px 8px 0 0;
}

.catalog-search-input:focus {
    outline: none;
    background: rgba(0,0,0,0.05);
}

.catalog-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    cursor: pointer;
    transition: background var(--transition-fast);
    border-bottom: 1px solid var(--ui-dropdown-border);
}

.catalog-item:last-child {
    border-bottom: none;
}

.catalog-item:hover {
    background: var(--ui-highlight);
    color: var(--txt-func);
}

.catalog-item-name {
    font-weight: 600;
    color: var(--sidebar-text-primary);
}

.catalog-item-desc {
    font-size: 12px;
    color: var(--sidebar-text-secondary);
}

/* =========================================
   11. RESPONSIVE DESIGN – MOBILE FIRST
   Full viewport calculator - no scroll needed
   ========================================= */

/* =========================================
   BASE MOBILE STYLES (All mobile devices)
   ========================================= */
@media (max-width: 768px) {
    html, body {
        overflow-x: hidden;
        width: 100%;
        max-width: 100vw;
        min-height: 100vh;
        min-height: 100dvh;
    }
    
    body {
        padding: 0;
    }
    
    /* Main container - space for floating menu at top */
    .main-container {
        padding: 65px 12px 12px 12px !important;
        width: 100% !important;
        max-width: 100vw !important;
        min-height: 100dvh !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important;
        align-items: center !important;
        box-sizing: border-box !important;
    }
    
    /* Calculator frame - properly positioned below floating menu */
    .calculator-frame {
        width: 100% !important;
        max-width: 100% !important;
        min-width: auto !important;
        margin: 0 auto !important;
        transform: none !important;
        box-sizing: border-box !important;
    }
    
    /* Hide desktop elements on mobile */
    .desktop-nav,
    .header {
        display: none !important;
    }
    
    /* Show floating menu button */
    .floating-menu-btn {
        display: flex !important;
    }
}

/* =========================================
   TINY PHONES (iPhone SE 1st gen, old Android)
   Width: 320px, Height: ~568px
   ========================================= */
@media (max-width: 359px) {
    .main-container {
        padding: 60px 8px 8px 8px !important;
    }
    
    .calculator-frame {
        padding: 10px 8px 14px 8px !important;
        border-radius: 18px !important;
        gap: 6px !important;
    }
    
    /* Screen - compact */
    .screen-bezel {
        padding: 8px 10px !important;
        margin-bottom: 4px !important;
    }
    
    .display-screen {
        height: 90px !important;
        padding: 6px 8px !important;
        font-size: 12px !important;
    }
    
    .current-line {
        font-size: 20px !important;
    }
    
    /* Header row */
    .calculator-header-row {
        margin-bottom: 4px !important;
        padding: 0 4px !important;
    }
    
    .brand-text {
        font-size: 10px !important;
    }
    
    .tagline {
        font-size: 7px !important;
    }
    
    .calc-theme-btn {
        width: 26px !important;
        height: 26px !important;
        font-size: 11px !important;
    }
    
    /* Keypad - very compact */
    .keypad-grid {
        gap: 3px !important;
    }
    
    .key-row {
        gap: 3px !important;
    }
    
    /* Graphing buttons */
    .btn-graph {
        height: 22px !important;
        font-size: 8px !important;
        margin-bottom: 2px !important;
    }
    
    /* Standard buttons */
    .btn-blue,
    .btn-green,
    .btn-black,
    .btn-white,
    .btn-grey,
    .btn-enter {
        height: 32px !important;
        font-size: 10px !important;
        margin-top: 3px !important;
    }
    
    .btn-white {
        font-size: 14px !important;
    }
    
    .btn-enter {
        font-size: 9px !important;
    }
    
    /* Labels */
    .lbl-top, .lbl-right {
        font-size: 6px !important;
        top: -8px !important;
    }
    
    /* D-Pad */
    .d-pad-container {
        height: 38px !important;
        min-height: 38px !important;
        max-height: 38px !important;
        margin-top: 0 !important;
        gap: 2px !important;
    }
    
    .d-pad-container .d-btn {
        font-size: 8px !important;
    }
    
    .d-center {
        width: 75% !important;
        height: 75% !important;
    }
}

/* =========================================
   SMALL PHONES (iPhone SE 2nd/3rd gen, iPhone 12/13 mini)
   Width: 360px - 375px, Height: ~667px
   ========================================= */
@media (min-width: 360px) and (max-width: 375px) {
    .main-container {
        padding: 65px 10px 10px 10px !important;
    }
    
    .calculator-frame {
        padding: 12px 10px 16px 10px !important;
        border-radius: 20px !important;
        gap: 6px !important;
    }
    
    /* Screen */
    .screen-bezel {
        padding: 10px 12px !important;
        margin-bottom: 5px !important;
    }
    
    .display-screen {
        height: 100px !important;
        padding: 6px 8px !important;
        font-size: 13px !important;
    }
    
    .current-line {
        font-size: 22px !important;
    }
    
    /* Header row */
    .calculator-header-row {
        margin-bottom: 5px !important;
        padding: 0 5px !important;
    }
    
    .brand-text {
        font-size: 11px !important;
    }
    
    .tagline {
        font-size: 8px !important;
    }
    
    .calc-theme-btn {
        width: 28px !important;
        height: 28px !important;
        font-size: 12px !important;
    }
    
    /* Keypad */
    .keypad-grid {
        gap: 4px !important;
    }
    
    .key-row {
        gap: 4px !important;
    }
    
    /* Graphing buttons */
    .btn-graph {
        height: 24px !important;
        font-size: 9px !important;
        margin-bottom: 3px !important;
    }
    
    /* Standard buttons */
    .btn-blue,
    .btn-green,
    .btn-black,
    .btn-white,
    .btn-grey,
    .btn-enter {
        height: 36px !important;
        font-size: 11px !important;
        margin-top: 3px !important;
    }
    
    .btn-white {
        font-size: 15px !important;
    }
    
    .btn-enter {
        font-size: 10px !important;
    }
    
    /* Labels */
    .lbl-top, .lbl-right {
        font-size: 7px !important;
        top: -9px !important;
    }
    
    /* D-Pad */
    .d-pad-container {
        height: 44px !important;
        min-height: 44px !important;
        max-height: 44px !important;
        margin-top: 0 !important;
        gap: 2px !important;
    }
    
    .d-pad-container .d-btn {
        font-size: 9px !important;
    }
}

/* =========================================
   MEDIUM PHONES (iPhone X/XS/11 Pro, iPhone 12/13/14, Android Standard)
   Width: 376px - 414px, Height: ~812px - 896px
   ========================================= */
@media (min-width: 376px) and (max-width: 414px) {
    .main-container {
        padding: 70px 12px 12px 12px !important;
    }
    
    .calculator-frame {
        padding: 14px 12px 18px 12px !important;
        border-radius: 22px !important;
        gap: 8px !important;
    }
    
    /* Screen */
    .screen-bezel {
        padding: 12px 14px !important;
        margin-bottom: 6px !important;
    }
    
    .display-screen {
        height: 120px !important;
        padding: 8px 10px !important;
        font-size: 14px !important;
    }
    
    .current-line {
        font-size: 24px !important;
    }
    
    /* Header row */
    .calculator-header-row {
        margin-bottom: 6px !important;
        padding: 0 6px !important;
    }
    
    .brand-text {
        font-size: 11px !important;
    }
    
    .tagline {
        font-size: 8px !important;
    }
    
    .calc-theme-btn {
        width: 30px !important;
        height: 30px !important;
        font-size: 13px !important;
    }
    
    /* Keypad */
    .keypad-grid {
        gap: 5px !important;
    }
    
    .key-row {
        gap: 5px !important;
    }
    
    /* Graphing buttons */
    .btn-graph {
        height: 26px !important;
        font-size: 10px !important;
        margin-bottom: 3px !important;
    }
    
    /* Standard buttons */
    .btn-blue,
    .btn-green,
    .btn-black,
    .btn-white,
    .btn-grey,
    .btn-enter {
        height: 40px !important;
        font-size: 12px !important;
        margin-top: 4px !important;
    }
    
    .btn-white {
        font-size: 16px !important;
    }
    
    .btn-enter {
        font-size: 11px !important;
    }
    
    /* Labels */
    .lbl-top, .lbl-right {
        font-size: 7px !important;
        top: -10px !important;
    }
    
    /* D-Pad */
    .d-pad-container {
        height: 48px !important;
        min-height: 48px !important;
        max-height: 48px !important;
        margin-top: 0 !important;
        gap: 3px !important;
    }
    
    .d-pad-container .d-btn {
        font-size: 10px !important;
    }
}

/* =========================================
   LARGE PHONES (iPhone Plus, Pro Max, Large Android)
   Width: 415px - 428px, Height: ~896px - 932px
   ========================================= */
@media (min-width: 415px) and (max-width: 428px) {
    .main-container {
        padding: 75px 14px 14px 14px !important;
    }
    
    .calculator-frame {
        padding: 16px 14px 20px 14px !important;
        border-radius: 24px !important;
        gap: 8px !important;
    }
    
    /* Screen */
    .screen-bezel {
        padding: 14px 16px !important;
        margin-bottom: 6px !important;
    }
    
    .display-screen {
        height: 130px !important;
        padding: 8px 10px !important;
        font-size: 15px !important;
    }
    
    .current-line {
        font-size: 26px !important;
    }
    
    /* Header row */
    .calculator-header-row {
        margin-bottom: 6px !important;
        padding: 0 6px !important;
    }
    
    /* Keypad */
    .keypad-grid {
        gap: 6px !important;
    }
    
    .key-row {
        gap: 6px !important;
    }
    
    /* Graphing buttons */
    .btn-graph {
        height: 28px !important;
        font-size: 11px !important;
    }
    
    /* Standard buttons */
    .btn-blue,
    .btn-green,
    .btn-black,
    .btn-white,
    .btn-grey,
    .btn-enter {
        height: 44px !important;
        font-size: 13px !important;
    }
    
    .btn-white {
        font-size: 17px !important;
    }
    
    .btn-enter {
        font-size: 12px !important;
    }
    
    /* Labels */
    .lbl-top, .lbl-right {
        font-size: 8px !important;
    }
    
    /* D-Pad */
    .d-pad-container {
        height: 52px !important;
        min-height: 52px !important;
        max-height: 52px !important;
    }
    
    .d-pad-container .d-btn {
        font-size: 11px !important;
    }
}

/* =========================================
   EXTRA LARGE PHONES (Foldables, large Android)
   Width: 429px - 767px
   ========================================= */
@media (min-width: 429px) and (max-width: 767px) {
    .main-container {
        padding: 80px 16px 16px 16px !important;
    }
    
    .calculator-frame {
        max-width: 420px !important;
        padding: 18px 16px 22px 16px !important;
        border-radius: 26px !important;
    }
    
    .display-screen {
        height: 140px !important;
    }
    
    .btn-graph {
        height: 28px !important;
    }
    
    .btn-blue,
    .btn-green,
    .btn-black,
    .btn-white,
    .btn-grey,
    .btn-enter {
        height: 46px !important;
    }
    
    .d-pad-container {
        height: 54px !important;
        min-height: 54px !important;
        max-height: 54px !important;
    }
}

/* =========================================
   LANDSCAPE MODE (All mobile sizes)
   ========================================= */
@media (max-width: 896px) and (orientation: landscape) {
    .main-container {
        padding: 55px 12px 12px 12px !important;
        min-height: 100dvh !important;
    }
    
    .calculator-frame {
        max-width: 340px !important;
        padding: 10px 10px 14px 10px !important;
        gap: 5px !important;
    }
    
    .display-screen {
        height: 80px !important;
        font-size: 12px !important;
    }
    
    .current-line {
        font-size: 18px !important;
    }
    
    .btn-graph {
        height: 20px !important;
        font-size: 8px !important;
        margin-bottom: 2px !important;
    }
    
    .btn-blue,
    .btn-green,
    .btn-black,
    .btn-white,
    .btn-grey,
    .btn-enter {
        height: 30px !important;
        font-size: 10px !important;
        margin-top: 3px !important;
    }
    
    .btn-white {
        font-size: 13px !important;
    }
    
    .keypad-grid {
        gap: 3px !important;
    }
    
    .key-row {
        gap: 3px !important;
    }
    
    .d-pad-container {
        height: 36px !important;
        min-height: 36px !important;
        max-height: 36px !important;
        margin-top: 0 !important;
    }
    
    .lbl-top, .lbl-right {
        font-size: 6px !important;
        top: -8px !important;
    }
}

/* =========================================
   DESKTOP (min-width: 769px)
   ========================================= */
@media (min-width: 769px) {
    .mobile-menu-btn,
    .floating-menu-btn {
        display: none !important;
    }
}

/* =========================================
   FLOATING MENU BUTTON (Mobile Only)
   Clean, minimal floating button - no header
   Position: Top-right corner with safe area support
   ========================================= */
.floating-menu-btn {
    display: none;
    position: fixed;
    top: max(12px, env(safe-area-inset-top, 12px));
    right: max(12px, env(safe-area-inset-right, 12px));
    z-index: 9999;
    width: 48px;
    height: 48px;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 14px;
    box-shadow: 
        0 4px 20px rgba(0, 0, 0, 0.15),
        0 2px 8px rgba(0, 0, 0, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    color: #2d3436;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

[data-theme="dark"] .floating-menu-btn {
    background: rgba(53, 59, 72, 0.95);
    border-color: rgba(255, 255, 255, 0.15);
    box-shadow: 
        0 4px 20px rgba(0, 0, 0, 0.4),
        0 2px 8px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
    color: #f5f6fa;
}

[data-theme="solar"] .floating-menu-btn {
    background: rgba(0, 30, 60, 0.95);
    border-color: rgba(0, 229, 255, 0.3);
    box-shadow: 
        0 4px 20px rgba(0, 0, 0, 0.5),
        0 2px 8px rgba(0, 229, 255, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
    color: #ffd700;
}

.floating-menu-btn:hover {
    transform: scale(1.05);
    box-shadow: 
        0 6px 24px rgba(0, 0, 0, 0.2),
        0 3px 12px rgba(0, 0, 0, 0.15);
}

.floating-menu-btn:active {
    transform: scale(0.92);
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.15),
        0 1px 4px rgba(0, 0, 0, 0.1);
}

.floating-menu-btn svg {
    width: 26px;
    height: 26px;
}

/* =========================================
   12. SIDEBAR NAVIGATION (RIGHT SIDE) - FIXED THEME COLORS
   ========================================= */

/* Header */
.header {
    height: 64px;
    background: color-mix(in srgb, var(--ui-dropdown-bg, #fff) 85%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--ui-dropdown-border, rgba(0,0,0,0.08));
    display: flex;
    align-items: center;
    padding: 0 var(--spacing-lg);
    flex-shrink: 0;
    z-index: 100;
    transition:
        background var(--transition-medium),
        border-color var(--transition-medium),
        box-shadow var(--transition-medium);
    position: sticky;
    top: 0;
}

.header-content {
    width: 100%;
    max-width: 1280px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.logo-area::before {
    content: '';
    display: inline-block;
    width: 34px;
    height: 34px;
    background: var(--accent-primary, #001a2c);
    border-radius: 8px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='3'/%3E%3Cline x1='8' y1='8' x2='16' y2='8'/%3E%3Cline x1='8' y1='12' x2='16' y2='12'/%3E%3Cline x1='8' y1='16' x2='12' y2='16'/%3E%3C/svg%3E");
    background-position: center;
    background-repeat: no-repeat;
    flex-shrink: 0;
}

.logo-text h1 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--sidebar-text-primary);
    font-weight: 700;
}

.logo-title {
    margin: 0;
    font-size: 1.5rem;
    color: var(--text-primary, #1a1a2e);
    font-weight: 800;
    letter-spacing: -0.02em;
    line-height: 1.2;
}

.tagline {
    font-size: 0.7rem;
    color: var(--text-tertiary, #6c757d);
    font-weight: 500;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    opacity: 0.75;
}

/* Desktop Nav */
.desktop-nav {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.desktop-nav a {
    text-decoration: none;
    color: var(--nav-link-inactive, #6c757d);
    font-size: 0.8125rem;
    font-weight: 500;
    padding: 0.4rem 0.7rem;
    border-radius: 6px;
    transition: all 0.2s ease;
    position: relative;
}

.desktop-nav a:hover {
    color: var(--nav-link-active, #0066cc);
    background: color-mix(in srgb, var(--nav-link-active, #0066cc) 8%, transparent);
}

.desktop-nav a.active {
    color: var(--nav-link-active, #0066cc);
    background: color-mix(in srgb, var(--nav-link-active, #0066cc) 12%, transparent);
    font-weight: 600;
}

/* Mobile Menu Button */
.mobile-menu-btn {
    background: none;
    border: none;
    font-size: 22px;
    cursor: pointer;
    color: var(--sidebar-text-primary);
    padding: 0;
    width: 44px;
    height: 44px;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
    transition: background var(--transition-fast);
    will-change: transform;
}

.mobile-menu-btn:hover {
    background: var(--chassis-bg);
}

.mobile-menu-btn:focus-visible {
    outline: var(--focus-ring);
    outline-offset: var(--focus-ring-offset);
}

/* Sidebar Close Button */
.sidebar-close-btn {
    display: none;
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--sidebar-text-primary);
    padding: 5px 10px;
    line-height: 1;
    border-radius: 4px;
    transition: background 0.2s, color 0.2s;
}

.sidebar-close-btn:hover {
    background: var(--chassis-bg);
    color: var(--accent-color);
}

.sidebar-close-btn:focus-visible {
    outline: var(--focus-ring);
    outline-offset: var(--focus-ring-offset);
}

/* Show close button only on mobile */
@media (max-width: 768px) {
    .sidebar-close-btn {
        display: block;
    }
}

/* Sidebar - RIGHT SIDE - FIXED THEME COLORS */
.sidebar {
    position: fixed;
    top: 0;
    right: -280px;
    width: 280px;
    height: 100%;
    background: var(--sidebar-bg);
    border-left: 1px solid var(--sidebar-border);
    z-index: 2000;
    transform: translateX(0);
    transition: transform var(--transition-medium) cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: -4px 0 15px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
}

.sidebar.active {
    transform: translateX(-280px);
}

.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: var(--ui-modal-bg);
    z-index: 1999;
    display: none;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-medium);
}

.sidebar-overlay.active {
    display: block;
    opacity: 0.8;
}

/* Sidebar Header - FIXED THEMES */
.sidebar-header {
    background: var(--chassis-bg);
    padding: var(--spacing-lg);
    border-bottom: 1px solid var(--sidebar-border);
    flex-shrink: 0;
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 10px 0;
    -webkit-overflow-scrolling: touch;
}

/* Mobile Sidebar - Full height and wider */
@media (max-width: 768px) {
    .sidebar {
        width: 85vw;
        max-width: 320px;
        right: calc(-1 * min(85vw, 320px));
        height: 100vh;
        height: 100dvh;
    }
    
    .sidebar.active {
        transform: translateX(calc(-1 * min(85vw, 320px)));
    }
}

.sidebar-logo {
    color: var(--sidebar-text-primary);
    font-size: 1.2rem;
    font-weight: 700;
    transition: color var(--transition-medium);
}

.sidebar-tagline {
    color: var(--sidebar-text-secondary);
    font-size: 0.8rem;
    opacity: 0.7;
    transition: color var(--transition-medium);
}

/* Educational Badge - Theme-aware contrast */
.educational-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--badge-bg, #d4edda);
    color: var(--badge-text, #155724);
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-top: 8px;
    border: 1px solid var(--badge-border, #c3e6cb);
    transition: all var(--transition-fast);
}

.educational-badge svg {
    flex-shrink: 0;
}

/* Badge hover effect for better interactivity */
.educational-badge:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] .educational-badge:hover {
    box-shadow: 0 0 12px rgba(46, 204, 113, 0.3);
}

[data-theme="solar"] .educational-badge:hover {
    box-shadow: 0 0 16px rgba(255, 215, 0, 0.3);
}

/* Default theme badge colors */
:root {
    --badge-bg: #d4edda;
    --badge-text: #155724;
    --badge-border: #c3e6cb;
}

/* Dark theme badge - high contrast */
[data-theme="dark"] {
    --badge-bg: #1e3a2f;
    --badge-text: #2ecc71;
    --badge-border: #27ae60;
}

[data-theme="dark"] .educational-badge {
    box-shadow: 0 0 8px rgba(46, 204, 113, 0.2);
}

/* Solar theme badge - gold/cyan contrast - ENHANCED for WCAG AA */
[data-theme="solar"] {
    --badge-bg: rgba(255, 215, 0, 0.2);
    --badge-text: #ffd700;
    --badge-border: rgba(255, 215, 0, 0.5);
}

[data-theme="solar"] .educational-badge {
    box-shadow: 0 0 12px rgba(255, 215, 0, 0.2);
    text-shadow: 0 0 10px rgba(255, 215, 0, 0.4);
    font-weight: 700;
}

/* Sidebar Nav Items - FIXED THEMES */
.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 15px;
    text-decoration: none;
    color: var(--sidebar-text-primary);
    border-radius: 8px;
    margin-bottom: 4px;
    font-size: 14px;
    transition: all var(--transition-fast);
}

.nav-item:hover {
    background: var(--chassis-bg);
    color: var(--nav-link-active);
}

.nav-item.active {
    background: var(--ui-highlight);
    color: var(--txt-func);
    font-weight: 600;
}

.nav-item .icon {
    font-size: 16px;
    width: 20px;
    display: flex;
    justify-content: center;
}

.nav-section-title {
    padding: 15px 15px 5px;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--sidebar-text-secondary);
    transition: color var(--transition-medium);
}

/* =========================================
   13. FOOTER & PWA COMPONENTS
   ========================================= */

.footer {
    background: var(--ui-dropdown-bg);
    width: 100%;
    border-top: 1px solid var(--ui-dropdown-border);
    margin-top: auto;
    transition: 
        background var(--transition-medium),
        border-color var(--transition-medium);
}

.footer-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 50px 20px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 40px;
}

.footer-col h4 {
    margin: 0 0 15px;
    color: var(--sidebar-text-primary);
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.footer-col ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.footer-col li {
    margin-bottom: 10px;
}

.footer-col a {
    text-decoration: none;
    color: var(--sidebar-text-secondary);
    font-size: 14px;
    transition: color var(--transition-fast);
}

.footer-col a:hover {
    color: var(--ui-highlight);
}

.footer-bottom {
    text-align: center;
    padding: 25px;
    color: var(--screen-text-secondary);
    font-size: 12px;
    border-top: 1px solid var(--ui-dropdown-border);
}

/* PWA Toast - Enhanced */
.pwa-toast {
    position: fixed;
    top: -100px;
    left: 50%;
    transform: translateX(-50%);
    width: 94%;
    max-width: 420px;
    background: var(--ui-dropdown-bg);
    border: 1px solid var(--ui-dropdown-border);
    border-radius: 16px;
    padding: 14px 18px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.37);
    z-index: 3000;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: top var(--transition-slow) cubic-bezier(0.34, 1.56, 0.64, 1), opacity var(--transition-medium);
    will-change: top, opacity;
    opacity: 0;
}

.pwa-toast.show {
    top: 16px;
    opacity: 1;
    pointer-events: auto;
}

/* Responsive adjustments for PWA toast */
@media (max-width: 480px) {
    .pwa-toast {
        width: 92%;
        padding: 12px 16px;
        top: -100px;
    }
    
    .pwa-toast.show {
        top: 12px;
    }
    
    .pwa-icon {
        font-size: 28px;
    }
    
    .pwa-text strong {
        font-size: 14px;
    }
    
    .pwa-text span {
        font-size: 12px;
    }
}

@media (max-width: 375px) {
    .pwa-toast {
        width: 90%;
        padding: 10px 14px;
    }
    
    .pwa-toast.show {
        top: 10px;
    }
}

.pwa-content {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
}

.pwa-icon {
    font-size: 32px;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.pwa-text {
    flex: 1;
    line-height: 1.3;
}

.pwa-text strong {
    display: block;
    font-size: 15px;
    font-weight: 700;
    margin-bottom: 2px;
    color: var(--sidebar-text-primary);
}

.pwa-text span {
    font-size: 13px;
    opacity: 0.85;
    color: var(--sidebar-text-secondary);
}

.pwa-btns {
    display: flex;
    gap: 10px;
}

.pwa-primary,
.pwa-secondary {
    border: none;
    border-radius: 12px;
    padding: 10px 18px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-fast);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    will-change: transform;
}

.pwa-primary {
    background: var(--ui-highlight);
    color: var(--txt-func);
}

.pwa-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
}

.pwa-primary:focus-visible {
    outline: var(--focus-ring);
}

.pwa-secondary {
    background: rgba(255, 255, 255, 0.15);
    color: var(--sidebar-text-primary);
}

.pwa-secondary:hover {
    background: rgba(255, 255, 255, 0.25);
}

/* =========================================
   14. ACCESSIBILITY & REDUCED MOTION
   ========================================= */

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .cursor.blinking {
        animation: none;
        opacity: 1;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    :root {
        --chassis-shadow: 0 0 0 2px #000;
        --focus-ring: 0 0 0 4px #000;
        --focus-ring-dark: 0 0 0 4px #fff;
    }
    
    button {
        border: 1px solid #000 !important;
    }
    
    [data-theme="dark"] button,
    [data-theme="solar"] button {
        border-color: #fff !important;
    }
}

/* Focus visible for keyboard navigation */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* =========================================
   15. PRINT STYLES (for export)
   ========================================= */

@media print {
    .calculator-frame {
        box-shadow: none;
        page-break-inside: avoid;
        background: #fff !important;
        color: #000 !important;
    }
    
    .screen-bezel {
        background: #fff !important;
        border: 1px solid #000;
    }
    
    .display-screen {
        background: #fff !important;
        color: #000 !important;
        box-shadow: none !important;
    }
    
    button,
    .header,
    .footer,
    .sidebar,
    .pwa-toast,
    .calc-theme-btn {
        display: none !important;
    }
    
    .keypad-grid {
        display: none;
    }
    
    body {
        background: #fff !important;
    }
}

/* =========================================
   16. LOADING STATE
   ========================================= */

.calculator-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--sidebar-text-secondary);
    font-size: 14px;
    z-index: 100;
    transition: opacity var(--transition-medium);
    font-weight: 600;
}

/* Error loading state */
.calculator-loading.error {
    color: var(--ui-error);
    animation: pulse-error 1s infinite;
}

/* =========================================
   17. UTILITY CLASSES
   ========================================= */

.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

.font-mono { font-family: 'Courier New', monospace; }
.font-sans { font-family: -apple-system, BlinkMacSystemFont, sans-serif; }

.hidden { display: none !important; }
.invisible { visibility: hidden; }

.opacity-0 { opacity: 0; }
.opacity-50 { opacity: 0.5; }
.opacity-100 { opacity: 1; }

/* New: Recent Activity Badge */
.recent-activity-badge {
    background: var(--ui-highlight);
    color: var(--txt-func);
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 11px;
    font-weight: 700;
    min-width: 20px;
    text-align: center;
}

.touch-device button {
    min-height: 44px;
}

@keyframes pulse-soft {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.15); opacity: 0.9; }
    100% { transform: scale(1); opacity: 1; }
}

/* =========================================
   18. TOAST NOTIFICATIONS
   ========================================= */

.reset-toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(46, 204, 113, 0.95);
    color: white;
    padding: 12px 24px;
    border-radius: 8px;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    z-index: 5000;
    display: none;
    animation: slideUpFade 0.4s ease-out;
}

@keyframes slideUpFade {
    from { opacity: 0; transform: translate(-50%, 20px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* =========================================
   19. SEO CONTENT BOXES
   ========================================= */

.info-box {
    background: var(--bg-secondary);
    padding: 2rem;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    margin-bottom: 2rem;
}

.info-box h2, .info-box h3 {
    color: var(--text-primary);
    margin-top: 0;
}

.verification-box {
    background: var(--bg-secondary);
    padding: 2rem;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    margin: 32px 0;
    border-left: 4px solid #10b981;
}

.verification-box h2 {
    color: var(--text-primary);
    margin-top: 0;
}

/* ==========================================================================
   THEME-AWARE LINK COLORS (all tool pages)
   ========================================================================== */

/* Light theme fallback (default) */
a {
  color: #0066cc;
}

/* Dark theme – bright readable blue */
[data-theme="dark"] a {
  color: #5dade2;
}

/* Solar theme – gold, matches the solar palette */
[data-theme="solar"] a {
  color: #ffd700;
}

/* Keep navigation links untouched – they already have their own styles */
header a,
.sidebar a,
.nav-link,
.breadcrumb-nav a,
.legal-links a,
.footer a {
  /* inherited or already styled – reset so the above doesn't override them */
  color: inherit;
}

/* Restore footer and legal link colors */
.footer a {
  color: var(--text-secondary);
}
.legal-links a {
  color: var(--link-color); /* or use --accent-secondary if available */
}

/* Hover state */
a:hover {
  text-decoration: underline;
  opacity: 0.9;
}