:root {
    --bg: #f0f4f8;
    --calculator-bg: #ffffff;
    --display-bg: #f7f9fb;
    --btn-number: #dce8f5;
    --btn-number-hover: #c2d6f0;
    --btn-zero: #dce8f5;
    --btn-zero-hover: #c2d6f0;
    --btn-operator: #4a8fd4;
    --btn-operator-hover: #6ba5e0;
    --btn-equals: #f0a020;
    --btn-equals-hover: #f5b840;
    --btn-clear: #8a99a8;
    --btn-clear-hover: #a8b5c2;
    --btn-special: #4caf84;
    --btn-special-hover: #6dc9a0;
    --text-primary: #2c3e50;
    --text-secondary: #7a8b9a;
    --text-display: #1a2a3a;
    --shadow: 0 8px 32px rgba(0,0,0,0.08);
    --border-radius: 16px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', 'Inter', system-ui, -apple-system, sans-serif;
    background: linear-gradient(135deg, #e8eef5 0%, #f4f6f9 40%, #ffffff 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.app-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    max-width: 520px;
    width: 100%;
}

.app-title {
    color: var(--text-primary);
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: 1px;
    text-align: center;
    display: flex;
    align-items: center;
    gap: 10px;
}

.app-title .icon {
    font-size: 2rem;
}

.app-subtitle {
    color: var(--text-secondary);
    font-size: 0.85rem;
    text-align: center;
    margin-top: -10px;
    letter-spacing: 0.5px;
}

.calculator {
    background: var(--calculator-bg);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    padding: 24px;
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 16px;
    border: 1px solid rgba(0,0,0,0.06);
}

.display-wrapper {
    background: var(--display-bg);
    border-radius: 12px;
    padding: 16px 20px;
    border: 1px solid rgba(0,0,0,0.06);
    min-height: 100px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
}

.display-expression {
    color: var(--text-secondary);
    font-size: 0.9rem;
    min-height: 24px;
    word-break: break-all;
    text-align: right;
    width: 100%;
    letter-spacing: 0.5px;
    transition: color 0.2s;
}

.display-value {
    color: var(--text-display);
    font-size: 2.8rem;
    font-weight: 600;
    word-break: break-all;
    text-align: right;
    width: 100%;
    line-height: 1.2;
    transition: font-size 0.15s;
    letter-spacing: 1px;
}

.display-value.small {
    font-size: 2rem;
}

.display-value.xsmall {
    font-size: 1.5rem;
}

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

.btn {
    padding: 18px 4px;
    font-size: 1.2rem;
    font-weight: 600;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.15s ease;
    outline: none;
    letter-spacing: 0.5px;
    position: relative;
    overflow: hidden;
    color: var(--text-primary);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.btn:active {
    transform: scale(0.94);
    transition: transform 0.08s;
}

.btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0);
    transition: background 0.2s;
    border-radius: 12px;
}

.btn:hover::after {
    background: rgba(0,0,0,0.05);
}

.btn-number {
    background: var(--btn-number);
    color: #2c3e50;
}
.btn-number:hover {
    background: var(--btn-number-hover);
}

.btn-zero-ext {
    background: #4caf84;
    color: #ffffff;
    font-weight: 700;
    font-size: 1rem;
    letter-spacing: 1px;
}
.btn-zero-ext:hover {
    background: #6dc9a0;
}

.btn-operator {
    background: var(--btn-operator);
    color: #ffffff;
    font-size: 1.3rem;
    font-weight: 700;
}
.btn-operator:hover {
    background: var(--btn-operator-hover);
}
.btn-operator.active {
    background: #3a7ec8;
    box-shadow: 0 0 16px rgba(74, 143, 212, 0.5);
}

.btn-clear {
    background: var(--btn-clear);
    color: #ffffff;
    font-weight: 700;
    font-size: 1rem;
    letter-spacing: 1px;
}
.btn-clear:hover {
    background: var(--btn-clear-hover);
}

.btn-equals {
    background: var(--btn-equals);
    color: #ffffff;
    font-weight: 700;
    font-size: 1.5rem;
}
.btn-equals:hover {
    background: var(--btn-equals-hover);
}

.btn-decimal {
    background: var(--btn-number);
    color: #2c3e50;
    font-weight: 700;
    font-size: 1.3rem;
}
.btn-decimal:hover {
    background: var(--btn-number-hover);
}

.btn-function {
    background: var(--btn-special);
    color: #ffffff;
    font-size: 0.85rem;
    font-weight: 600;
}
.btn-function:hover {
    background: var(--btn-special-hover);
}

.history-panel {
    background: var(--display-bg);
    border-radius: 12px;
    padding: 12px 16px;
    border: 1px solid rgba(0,0,0,0.06);
    max-height: 150px;
    overflow-y: auto;
    display: none;
}

.history-panel.show {
    display: block;
}

.history-panel h3 {
    color: var(--text-secondary);
    font-size: 0.8rem;
    margin-bottom: 8px;
    letter-spacing: 1px;
    text-transform: uppercase;
}

.history-item {
    color: var(--text-display);
    font-size: 0.85rem;
    padding: 4px 8px;
    border-bottom: 1px solid rgba(0,0,0,0.04);
    display: flex;
    justify-content: space-between;
    cursor: pointer;
    transition: background 0.15s;
    border-radius: 4px;
}

.history-item:hover {
    background: rgba(0,0,0,0.03);
}

.history-item .hist-expr {
    color: var(--text-secondary);
}
.history-item .hist-result {
    font-weight: 600;
    color: #d4850a;
}

.btn-history-toggle {
    width: 100%;
    background: transparent;
    border: 1px solid rgba(0,0,0,0.1);
    color: var(--text-secondary);
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.8rem;
    letter-spacing: 1px;
    transition: all 0.2s;
}
.btn-history-toggle:hover {
    background: rgba(0,0,0,0.03);
    color: var(--text-primary);
}

.footer-note {
    color: var(--text-secondary);
    font-size: 0.7rem;
    text-align: center;
    opacity: 0.6;
}

/* Loading screen */
.loading-overlay {
    display: flex;
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 10000;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 16px;
    font-family: 'Segoe UI', sans-serif;
    color: var(--text-secondary);
    transition: opacity 0.3s;
}
.loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}
.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #dce8f5;
    border-top: 4px solid #4a8fd4;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}

@media (max-width: 400px) {
    .calculator { padding: 14px; gap: 10px; }
    .buttons-grid { gap: 6px; }
    .btn { padding: 14px 2px; font-size: 1rem; border-radius: 10px; }
    .display-value { font-size: 2.2rem; }
    .display-value.small { font-size: 1.6rem; }
    .display-value.xsmall { font-size: 1.2rem; }
}

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

.ripple-effect {
    position: absolute;
    border-radius: 50%;
    background: rgba(0,0,0,0.15);
    width: 20px;
    height: 20px;
    animation: ripple 0.6s linear;
    pointer-events: none;
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 9999;
    justify-content: center;
    align-items: center;
}
.modal-overlay.show {
    display: flex;
}

.modal-box {
    background: #ffffff;
    border-radius: 16px;
    padding: 28px 24px 20px;
    width: 380px;
    max-width: 90vw;
    box-shadow: 0 12px 40px rgba(0,0,0,0.2);
    text-align: center;
}

.modal-message {
    color: #2c3e50;
    font-size: 1.05rem;
    margin-bottom: 20px;
    line-height: 1.5;
}

.modal-input {
    width: 100%;
    padding: 12px 16px;
    font-size: 1.1rem;
    border: 2px solid #dce8f5;
    border-radius: 10px;
    outline: none;
    text-align: center;
    margin-bottom: 20px;
    box-sizing: border-box;
    color: #2c3e50;
}
.modal-input:focus {
    border-color: #4a8fd4;
}

.modal-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.modal-btn {
    padding: 10px 32px;
    font-size: 0.95rem;
    font-weight: 600;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.15s;
}

.modal-btn-ok {
    background: #4a8fd4;
    color: #ffffff;
}
.modal-btn-ok:hover {
    background: #6ba5e0;
}

.modal-btn-cancel {
    background: #e0e5ec;
    color: #5a6a7a;
}
.modal-btn-cancel:hover {
    background: #cdd3db;
}
