:root {
    color-scheme: dark;

    --background: #0f1115;
    --surface: #171a21;
    --surface-hover: #1d222b;
    --border: #2a303a;

    --text: #f1f4f8;
    --text-secondary: #9da6b3;
    --text-muted: #6f7885;

    --blue: #3b82f6;
    --blue-hover: #2563eb;
    --blue-soft: rgba(59, 130, 246, 0.12);

    --operator: #202631;
    --function: #1b2028;

    --shadow: 0 18px 45px rgba(0, 0, 0, 0.25);

    --radius: 12px;
    --transition: 160ms ease;
}

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

html {
    min-height: 100%;
}

body {
    min-height: 100vh;
    background: var(--background);
    color: var(--text);

    font-family:
        Inter,
        system-ui,
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        sans-serif;

    display: flex;
    justify-content: center;

    padding: 48px 20px;

    transition:
        background-color 250ms ease,
        color 250ms ease;
}

.page {
    width: 100%;
    max-width: 460px;

    animation: pageEnter 500ms ease both;
}

.header {
    margin-bottom: 28px;
}

.header h1 {
    color: var(--blue);

    font-size: 32px;
    font-weight: 700;
    letter-spacing: -0.8px;
    line-height: 1;

    animation: logoEnter 500ms ease 50ms both;
}

.header p {
    margin-top: 8px;

    color: var(--text-secondary);

    font-size: 14px;
    line-height: 1.5;

    animation: logoEnter 500ms ease 120ms both;
}

.calculator {
    width: 100%;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);

    padding: 16px;

    box-shadow: var(--shadow);
}

.display {
    min-height: 116px;

    display: flex;
    flex-direction: column;
    justify-content: flex-end;

    padding: 18px 16px 20px;
    margin-bottom: 14px;

    border: 1px solid var(--border);
    border-radius: 10px;

    background: var(--background);

    overflow: hidden;
}

.expression {
    min-height: 22px;

    color: var(--text-secondary);

    font-size: 15px;
    text-align: right;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;

    margin-bottom: 7px;
}

.result {
    color: var(--text);

    font-size: 34px;
    font-weight: 500;
    line-height: 1.1;

    text-align: right;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

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

.key {
    height: 58px;

    border: 1px solid var(--border);
    border-radius: 9px;

    background: var(--surface);
    color: var(--text);

    font-family: inherit;
    font-size: 17px;
    font-weight: 500;

    cursor: pointer;

    transition:
        background-color var(--transition),
        border-color var(--transition),
        transform 80ms ease,
        color var(--transition);
}

.key:hover {
    background: var(--surface-hover);
    border-color: #39414e;
}

.key:active {
    transform: scale(0.96);
}

.key.function {
    background: var(--function);
    color: var(--text-secondary);
}

.key.function:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.key.operator {
    background: var(--operator);
    color: var(--blue);

    font-size: 20px;
    font-weight: 600;
}

.key.operator:hover {
    background: var(--blue-soft);
    border-color: rgba(59, 130, 246, 0.4);
}

.key.zero {
    grid-column: span 2;
}

.key.equals {
    background: var(--blue);
    border-color: var(--blue);

    color: white;

    font-size: 19px;
    font-weight: 600;
}

.key.equals:hover {
    background: var(--blue-hover);
    border-color: var(--blue-hover);
}

.tools {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;

    margin-top: 18px;
    padding: 5px;

    border: 1px solid var(--border);
    border-radius: 10px;

    background: var(--surface);
}

.tool {
    min-height: 38px;

    display: flex;
    align-items: center;
    justify-content: center;

    border: 0;
    border-radius: 7px;

    background: transparent;
    color: var(--text-secondary);

    font-family: inherit;
    font-size: 12px;
    font-weight: 500;

    cursor: pointer;

    text-decoration: none;

    transition:
        background-color var(--transition),
        color var(--transition);
}

.tool:hover:not(:disabled) {
    background: var(--surface-hover);
    color: var(--text);
}

.tool.active {
    background: var(--blue-soft);
    color: var(--blue);
}

.tool:disabled {
    opacity: 0.4;
    cursor: default;
}

.footer {
    display: flex;
    justify-content: center;

    margin-top: 18px;
}

.theme-toggle {
    border: 0;
    background: transparent;

    color: var(--text-muted);

    font-family: inherit;
    font-size: 12px;

    cursor: pointer;

    transition: color var(--transition);
}

.theme-toggle:hover {
    color: var(--text-secondary);
}

body.light {
    color-scheme: light;

    --background: #f5f7fa;
    --surface: #ffffff;
    --surface-hover: #f1f4f8;
    --border: #dce1e8;

    --text: #18202a;
    --text-secondary: #667180;
    --text-muted: #8a94a3;

    --blue: #2563eb;
    --blue-hover: #1d4ed8;
    --blue-soft: rgba(37, 99, 235, 0.09);

    --operator: #f1f4f8;
    --function: #f5f7fa;

    --shadow: 0 14px 35px rgba(24, 32, 42, 0.08);
}

button:focus-visible,
a:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 2px;
}

@keyframes pageEnter {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes logoEnter {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

@media (max-width: 520px) {
    body {
        padding: 28px 14px;
    }

    .header {
        margin-bottom: 20px;
    }

    .header h1 {
        font-size: 28px;
    }

    .calculator {
        padding: 12px;
    }

    .key {
        height: 54px;
    }

    .display {
        min-height: 105px;
    }

    .result {
        font-size: 30px;
    }
}
