/* ===========================
   Design Tokens & Variables
   =========================== */
:root {
    /* Colors */
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-card: #16161f;
    --bg-display: #0d0d14;

    --text-primary: #f0f0f5;
    --text-secondary: #8a8a9a;
    --text-muted: #555566;

    --accent-purple: #8b5cf6;
    --accent-purple-glow: rgba(139, 92, 246, 0.3);
    --accent-blue: #3b82f6;
    --accent-pink: #ec4899;
    --accent-orange: #f97316;
    --accent-green: #22c55e;

    --btn-number-bg: #1e1e2a;
    --btn-number-hover: #2a2a3a;
    --btn-number-active: #333344;

    --btn-utility-bg: #252535;
    --btn-utility-hover: #303045;
    --btn-utility-active: #3a3a50;
    --btn-utility-text: #c0a0ff;

    --btn-operator-bg: linear-gradient(135deg, #6d28d9, #7c3aed);
    --btn-operator-hover: linear-gradient(135deg, #7c3aed, #8b5cf6);
    --btn-operator-active: linear-gradient(135deg, #5b21b6, #6d28d9);
    --btn-operator-text: #ffffff;

    --btn-equals-bg: linear-gradient(135deg, #8b5cf6, #ec4899);
    --btn-equals-hover: linear-gradient(135deg, #a78bfa, #f472b6);
    --btn-equals-active: linear-gradient(135deg, #7c3aed, #db2777);

    --glitch-red: #ff3344;
    --glitch-cyan: #00ffcc;

    /* Spacing */
    --gap: 12px;
    --radius-sm: 12px;
    --radius-md: 16px;
    --radius-lg: 24px;

    /* Shadows */
    --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(255, 255, 255, 0.05);
    --shadow-btn: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-glow: 0 0 40px var(--accent-purple-glow);

    /* Typography */
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    /* Animation */
    --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-smooth: cubic-bezier(0.4, 0, 0.2, 1);
    --transition-fast: 150ms var(--ease-smooth);
    --transition-base: 250ms var(--ease-smooth);
}

/* ===========================
   Reset & Base
   =========================== */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-sans);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    position: relative;
}

/* ===========================
   Background Ambient Glow
   =========================== */
.bg-glow {
    position: fixed;
    border-radius: 50%;
    filter: blur(120px);
    opacity: 0.4;
    pointer-events: none;
    z-index: 0;
    animation: floatGlow 12s ease-in-out infinite;
}

.bg-glow-1 {
    width: 400px;
    height: 400px;
    background: var(--accent-purple);
    top: -10%;
    left: -5%;
    animation-delay: 0s;
}

.bg-glow-2 {
    width: 350px;
    height: 350px;
    background: var(--accent-pink);
    bottom: -10%;
    right: -5%;
    animation-delay: -4s;
}

.bg-glow-3 {
    width: 300px;
    height: 300px;
    background: var(--accent-blue);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation-delay: -8s;
    opacity: 0.15;
}

@keyframes floatGlow {
    0%, 100% { transform: translate(0, 0) scale(1); }
    25% { transform: translate(30px, -20px) scale(1.05); }
    50% { transform: translate(-20px, 30px) scale(0.95); }
    75% { transform: translate(20px, 20px) scale(1.02); }
}

/* ===========================
   App Container
   =========================== */
.app-container {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 20px;
    max-width: 420px;
    width: 100%;
}

/* ===========================
   Header
   =========================== */
.app-header {
    text-align: center;
}

.app-header h1 {
    font-size: 1.8rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink), var(--accent-orange));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: -0.02em;
}

.subtitle {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 4px;
    font-weight: 400;
}

/* ===========================
   Calculator Card
   =========================== */
.calculator {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 100%;
    box-shadow: var(--shadow-card);
    position: relative;
    overflow: hidden;
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.06);
    transition: box-shadow var(--transition-base);
}

.calculator::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(139, 92, 246, 0.4), rgba(236, 72, 153, 0.4), transparent);
}

/* ===========================
   Glitch Indicator
   =========================== */
.glitch-indicator {
    position: absolute;
    top: 12px;
    right: 12px;
    display: flex;
    align-items: center;
    gap: 6px;
    opacity: 0;
    transform: translateY(-8px) scale(0.9);
    transition: all 300ms var(--ease-spring);
    z-index: 10;
    pointer-events: none;
}

.glitch-indicator.active {
    opacity: 1;
    transform: translateY(0) scale(1);
}

.glitch-dot {
    width: 8px;
    height: 8px;
    background: var(--glitch-red);
    border-radius: 50%;
    animation: pulseDot 0.6s ease-in-out infinite;
}

.glitch-text {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--glitch-red);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

@keyframes pulseDot {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.8); }
}

/* ===========================
   Display
   =========================== */
.display-container {
    background: var(--bg-display);
    border-radius: var(--radius-md);
    padding: 20px 24px;
    margin-bottom: 20px;
    min-height: 110px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    border: 1px solid rgba(255, 255, 255, 0.04);
    position: relative;
    overflow: hidden;
}

.display-container::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(180deg, transparent 60%, rgba(139, 92, 246, 0.03));
    pointer-events: none;
}

.display-expression {
    font-family: var(--font-mono);
    font-size: 0.9rem;
    color: var(--text-muted);
    min-height: 24px;
    word-break: break-all;
    text-align: right;
    width: 100%;
    transition: color var(--transition-fast);
}

.display-result {
    font-family: var(--font-mono);
    font-size: 2.8rem;
    font-weight: 600;
    color: var(--text-primary);
    word-break: break-all;
    text-align: right;
    width: 100%;
    line-height: 1.2;
    transition: all var(--transition-base);
}

.display-result.error {
    color: var(--glitch-red);
    font-size: 1.4rem;
}

/* Glitch effect on display */
.display-result.glitch-active {
    animation: glitchText 0.4s ease-in-out;
}

@keyframes glitchText {
    0% { transform: translate(0); }
    10% { transform: translate(-3px, 2px); color: var(--glitch-cyan); }
    20% { transform: translate(3px, -2px); color: var(--glitch-red); }
    30% { transform: translate(-2px, -1px); }
    40% { transform: translate(2px, 1px); color: var(--glitch-cyan); }
    50% { transform: translate(-1px, 2px); }
    60% { transform: translate(1px, -1px); color: var(--glitch-red); }
    70% { transform: translate(-2px, 1px); }
    80% { transform: translate(2px, -2px); }
    100% { transform: translate(0); color: var(--text-primary); }
}

/* Calculator glitch border flash */
.calculator.glitch-flash {
    box-shadow: var(--shadow-card), 0 0 30px rgba(255, 51, 68, 0.3), 0 0 60px rgba(0, 255, 204, 0.1);
}

/* ===========================
   Buttons Grid
   =========================== */
.buttons-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: var(--gap);
}

/* ===========================
   Button Base
   =========================== */
.btn {
    font-family: var(--font-sans);
    font-size: 1.25rem;
    font-weight: 500;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 64px;
    transition: all var(--transition-fast);
    position: relative;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.btn::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.btn:active {
    transform: scale(0.95);
}

/* Ripple effect */
.btn .ripple {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    transform: scale(0);
    animation: rippleEffect 0.5s linear;
    pointer-events: none;
}

@keyframes rippleEffect {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

/* ===========================
   Number Buttons
   =========================== */
.btn-number {
    background: var(--btn-number-bg);
    color: var(--text-primary);
    box-shadow: var(--shadow-btn);
    border: 1px solid rgba(255, 255, 255, 0.04);
}

.btn-number:hover {
    background: var(--btn-number-hover);
    border-color: rgba(255, 255, 255, 0.08);
    transform: translateY(-1px);
}

.btn-number:active {
    background: var(--btn-number-active);
    transform: scale(0.95) translateY(0);
}

/* Zero button spans 2 columns */
.btn-zero {
    grid-column: span 2;
}

/* ===========================
   Utility Buttons
   =========================== */
.btn-utility {
    background: var(--btn-utility-bg);
    color: var(--btn-utility-text);
    box-shadow: var(--shadow-btn);
    border: 1px solid rgba(139, 92, 246, 0.08);
}

.btn-utility:hover {
    background: var(--btn-utility-hover);
    border-color: rgba(139, 92, 246, 0.15);
    transform: translateY(-1px);
}

.btn-utility:active {
    background: var(--btn-utility-active);
    transform: scale(0.95) translateY(0);
}

/* ===========================
   Operator Buttons
   =========================== */
.btn-operator {
    background: var(--btn-operator-bg);
    color: var(--btn-operator-text);
    font-size: 1.4rem;
    font-weight: 600;
    box-shadow: var(--shadow-btn), 0 4px 15px rgba(109, 40, 217, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.btn-operator:hover {
    background: var(--btn-operator-hover);
    box-shadow: var(--shadow-btn), 0 6px 20px rgba(109, 40, 217, 0.4);
    transform: translateY(-2px);
}

.btn-operator:active {
    background: var(--btn-operator-active);
    transform: scale(0.95) translateY(0);
    box-shadow: var(--shadow-btn);
}

.btn-operator.active {
    box-shadow: var(--shadow-btn), 0 0 20px rgba(139, 92, 246, 0.5), inset 0 0 20px rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
}

/* ===========================
   Equals Button
   =========================== */
.btn-equals {
    background: var(--btn-equals-bg);
    color: #ffffff;
    font-size: 1.5rem;
    font-weight: 700;
    box-shadow: var(--shadow-btn), 0 4px 20px rgba(139, 92, 246, 0.3), 0 4px 20px rgba(236, 72, 153, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.15);
}

.btn-equals:hover {
    background: var(--btn-equals-hover);
    box-shadow: var(--shadow-btn), 0 6px 25px rgba(139, 92, 246, 0.4), 0 6px 25px rgba(236, 72, 153, 0.3);
    transform: translateY(-2px);
}

.btn-equals:active {
    background: var(--btn-equals-active);
    transform: scale(0.95) translateY(0);
    box-shadow: var(--shadow-btn);
}

/* ===========================
   Footer
   =========================== */
.app-footer {
    text-align: center;
}

.app-footer p {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 400;
}

/* ===========================
   Responsive
   =========================== */
@media (max-width: 480px) {
    .app-container {
        padding: 16px;
        gap: 16px;
    }

    .calculator {
        padding: 16px;
        border-radius: var(--radius-md);
    }

    .btn {
        height: 56px;
        font-size: 1.15rem;
        border-radius: 10px;
    }

    .display-result {
        font-size: 2.2rem;
    }

    .buttons-grid {
        gap: 10px;
    }

    .app-header h1 {
        font-size: 1.5rem;
    }
}

@media (max-width: 360px) {
    .btn {
        height: 50px;
        font-size: 1.05rem;
    }

    .buttons-grid {
        gap: 8px;
    }

    .display-result {
        font-size: 1.8rem;
    }
}

/* ===========================
   Accessibility
   =========================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

.btn:focus-visible {
    outline: 2px solid var(--accent-purple);
    outline-offset: 2px;
}

/* ===========================
   Scrollbar (for display overflow)
   =========================== */
::-webkit-scrollbar {
    width: 4px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--text-muted);
    border-radius: 4px;
}
