:root {
    --bg-color: #1a1a2e;
    --calc-body-color: #16213e;
    --display-bg: #0f0f23;
    --btn-bg: #1f4068;
    --btn-hover: #2a5a8a;
    --accent-color: #e94560;
    --accent-hover: #ff6b6b;
    --text-color: #eaeaea;
    --text-muted: rgba(255, 255, 255, 0.6);
    --memory-color: #4ecca3;
    --history-bg: #0d1b2a;
    --border-color: rgba(255, 255, 255, 0.08);
    --ripple-color: rgba(255, 255, 255, 0.3);
    --shadow-color: rgba(0, 0, 0, 0.4);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

body {
    background: linear-gradient(135deg, var(--bg-color) 0%, #0f0f23 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    color: var(--text-color);
    overflow: hidden;
}

/* Calculator Container */
.calculator-container {
    width: 100%;
    max-width: 420px;
    padding: 20px;
}

.calculator {
    background: linear-gradient(145deg, var(--calc-body-color), #1a1a3a);
    border-radius: 24px;
    padding: 20px;
    box-shadow: 
        0 25px 50px var(--shadow-color),
        0 0 0 1px var(--border-color),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
    position: relative;
    overflow: hidden;
}

/* Settings Bar */
.settings-bar {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-bottom: 12px;
}

.settings-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-muted);
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.settings-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-color);
}

.settings-btn.active {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

.hidden {
    display: none !important;
}

/* Display */
.display-container {
    background: var(--display-bg);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 16px;
    text-align: right;
    word-wrap: break-word;
    word-break: break-all;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    min-height: 110px;
    border: 1px solid var(--border-color);
    position: relative;
    overflow: hidden;
    touch-action: pan-y;
    transition: transform 0.2s ease;
}

.display-container.swiping {
    background: linear-gradient(90deg, var(--display-bg) 0%, rgba(233, 69, 96, 0.2) 100%);
}

.previous-operand {
    color: var(--text-muted);
    font-size: 1.1rem;
    min-height: 1.5rem;
    font-weight: 300;
}

.current-operand {
    color: var(--text-color);
    font-size: 2.8rem;
    font-weight: 600;
    letter-spacing: -1px;
    transition: transform 0.15s ease;
}

.current-operand.shake {
    animation: shake 0.4s ease;
}

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

.swipe-hint {
    position: absolute;
    left: 16px;
    bottom: 8px;
    font-size: 0.7rem;
    color: var(--text-muted);
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.display-container.show-hint .swipe-hint {
    opacity: 1;
}

/* History Panel */
.history-panel {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--history-bg);
    border-radius: 24px;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
    display: flex;
    flex-direction: column;
    touch-action: pan-x;
    overflow: hidden;
}

.history-panel.open {
    transform: translateX(0);
}

.history-panel.dragging {
    transition: none;
}

/* Swipe Handle - Left Side (overlay) */
.history-swipe-handle {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 80px;
    cursor: grab;
    touch-action: pan-x;
    z-index: 10;
}

.history-swipe-handle:active {
    cursor: grabbing;
}

.swipe-bar {
    width: 4px;
    height: 50px;
    background: var(--text-muted);
    border-radius: 2px;
    opacity: 0.3;
    transition: opacity 0.2s ease, height 0.2s ease;
}

.history-swipe-handle:hover .swipe-bar,
.history-panel.dragging .swipe-bar {
    opacity: 0.6;
    height: 70px;
}

/* History Content */
.history-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow: hidden;
}

/* Close Button */
.close-history-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-muted);
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.close-history-btn:hover {
    background: rgba(233, 69, 96, 0.2);
    color: var(--accent-color);
    border-color: var(--accent-color);
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 12px;
}

.history-header span {
    flex: 1;
    text-align: center;
    font-size: 1.2rem;
    font-weight: 600;
}

.clear-history-btn {
    background: rgba(233, 69, 96, 0.1);
    border: none;
    border-radius: 8px;
    color: var(--accent-color);
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.clear-history-btn:hover {
    background: var(--accent-color);
    color: white;
}

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

.history-list::-webkit-scrollbar {
    width: 4px;
}

.history-list::-webkit-scrollbar-track {
    background: transparent;
}

.history-list::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.history-empty {
    color: var(--text-muted);
    text-align: center;
    padding: 40px 20px;
    font-size: 0.9rem;
}

.history-item {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 12px 16px;
    cursor: pointer;
    transition: all 0.2s ease;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.history-item:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: var(--accent-color);
}

.history-expression {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-bottom: 4px;
}

.history-result {
    color: var(--text-color);
    font-size: 1.2rem;
    font-weight: 600;
}

/* Memory Buttons */
.memory-buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.memory-btn {
    background: rgba(78, 204, 163, 0.1) !important;
    border: 1px solid rgba(78, 204, 163, 0.2) !important;
    color: var(--memory-color) !important;
    font-size: 0.9rem !important;
    padding: 12px 8px !important;
    font-weight: 600;
}

.memory-btn:hover {
    background: rgba(78, 204, 163, 0.2) !important;
}

.memory-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Memory Indicator */
.memory-indicator {
    position: absolute;
    bottom: 12px;
    left: 20px;
    display: flex;
    align-items: center;
    gap: 6px;
    opacity: 0;
    transform: translateY(10px);
    transition: all 0.3s ease;
    pointer-events: none;
}

.memory-indicator.visible {
    opacity: 1;
    transform: translateY(0);
}

.memory-badge {
    background: var(--memory-color);
    color: var(--calc-body-color);
    font-size: 0.65rem;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 4px;
}

.memory-value {
    color: var(--text-muted);
    font-size: 0.75rem;
}

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

.btn {
    background: var(--btn-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    color: var(--text-color);
    font-size: 1.5rem;
    padding: 20px;
    cursor: pointer;
    transition: all 0.15s ease;
    user-select: none;
    position: relative;
    overflow: hidden;
    font-weight: 500;
}

.btn:hover {
    background: var(--btn-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.btn:active {
    transform: translateY(0) scale(0.97);
}

/* Ripple Effect */
.btn .ripple {
    position: absolute;
    border-radius: 50%;
    background: var(--ripple-color);
    transform: scale(0);
    animation: ripple-animation 0.6s ease-out;
    pointer-events: none;
}

@keyframes ripple-animation {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

/* Operator & Action Buttons */
.btn.operator, 
.btn[data-action="calculate"], 
.btn[data-action="clear"], 
.btn[data-action="delete"] {
    background: linear-gradient(135deg, var(--accent-color), #c73e54);
    border-color: transparent;
    font-weight: 600;
}

.btn.operator:hover, 
.btn[data-action="calculate"]:hover, 
.btn[data-action="clear"]:hover, 
.btn[data-action="delete"]:hover {
    background: linear-gradient(135deg, var(--accent-hover), var(--accent-color));
}

.span-two {
    grid-column: span 2;
}

/* Number Buttons Glow on Press */
.btn.number:active {
    box-shadow: 0 0 20px rgba(31, 64, 104, 0.5);
}

/* Responsive */
@media (max-width: 420px) {
    .calculator-container {
        padding: 10px;
    }

    .calculator {
        border-radius: 20px;
        padding: 16px;
    }

    .btn {
        padding: 18px;
        font-size: 1.3rem;
        border-radius: 12px;
    }
    
    .current-operand {
        font-size: 2.2rem;
    }

    .memory-btn {
        padding: 10px 6px !important;
        font-size: 0.8rem !important;
    }
}

@media (max-width: 360px) {
    .btn {
        padding: 14px;
        font-size: 1.2rem;
    }

    .current-operand {
        font-size: 1.8rem;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    .btn:hover {
        transform: none;
        box-shadow: none;
    }

    .display-container.show-hint .swipe-hint {
        opacity: 0.7;
    }
}

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

/* Footer / Sign */
.sign {
    text-align: center;
    padding: 16px 20px;
    font-size: 0.85rem;
    color: var(--text-muted);
    letter-spacing: 0.3px;
}

.sign a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s ease, text-shadow 0.2s ease;
}

.sign a:hover {
    color: var(--accent-hover);
    text-shadow: 0 0 8px rgba(99, 102, 241, 0.4);
}

.sign a:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
    border-radius: 2px;
}
