:root {
    --bg: #000000;
    --panel: rgba(28, 28, 30, 0.96);
    --panel-soft: #2c2c2e;
    --key: #333333;
    --key-hover: #4a4a4a;
    --function: #a5a5a5;
    --function-hover: #d4d4d2;
    --operator: #ff9f0a;
    --operator-hover: #ffb340;
    --text: #ffffff;
    --muted: #8e8e93;
    --blue: #0a84ff;
    --red: #ff453a;
}

* {
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    margin: 0;
    padding: 40px 20px;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
        "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
    color: var(--text);
    background:
        radial-gradient(900px 500px at 50% -10%, rgba(10, 132, 255, 0.12), transparent 60%),
        radial-gradient(700px 500px at 100% 100%, rgba(255, 159, 10, 0.08), transparent 60%),
        var(--bg);
    background-attachment: fixed;
}

.app-layout {
    width: min(100%, 760px);
    margin: 0 auto;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 22px;
}

.calculator {
    width: min(100%, 380px);
    flex: 0 0 380px;
    padding: 18px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 34px;
    background: var(--panel);
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(24px);
}

.screen {
    padding: 22px 18px;
    margin-bottom: 16px;
    border-radius: 26px;
    background: #000000;
    text-align: right;
    overflow: hidden;
}

.previous-command {
    min-height: 22px;
    margin-bottom: 6px;
    color: var(--muted);
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.expression {
    width: 100%;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--text);
    font-size: clamp(36px, 8vw, 52px);
    font-weight: 300;
    letter-spacing: -1px;
    text-align: right;
    caret-color: var(--operator);
}

.expression::placeholder {
    color: #48484a;
}

.result {
    min-height: 26px;
    margin-top: 6px;
    color: var(--muted);
    font-size: 17px;
    word-break: break-all;
}

.result.error {
    color: var(--red);
}

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

.key {
    aspect-ratio: 1;
    border: 0;
    border-radius: 50%;
    background: var(--key);
    color: var(--text);
    font-size: 24px;
    font-weight: 400;
    cursor: pointer;
    transition: transform 0.08s ease, background 0.15s ease, filter 0.15s ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.key:hover {
    background: var(--key-hover);
}

.key:active {
    transform: scale(0.94);
    filter: brightness(1.15);
}

.key-function {
    color: #000000;
    background: var(--function);
}

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

.key-operator {
    color: #ffffff;
    background: var(--operator);
    font-size: 28px;
}

.key-operator:hover {
    background: var(--operator-hover);
}

.key-equals {
    color: #ffffff;
    background: var(--operator);
    font-size: 28px;
}

.key-equals:hover {
    background: var(--operator-hover);
}

.history-panel {
    width: 340px;
    max-height: 520px;
    padding: 18px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 34px;
    background: var(--panel);
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(24px);
    display: flex;
    flex-direction: column;
}

.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.history-header h2 {
    margin: 0;
    font-size: 17px;
    font-weight: 600;
    color: #ffffff;
    letter-spacing: 0.2px;
}

.history-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.history-refresh,
.history-clear {
    border: 0;
    border-radius: 999px;
    padding: 8px 14px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.08s ease;
}

.history-refresh {
    background: #2c2c2e;
    color: var(--blue);
}

.history-clear {
    background: rgba(255, 69, 58, 0.14);
    color: var(--red);
}

.history-refresh:hover {
    background: #3a3a3c;
}

.history-clear:hover {
    background: rgba(255, 69, 58, 0.24);
}

.history-refresh:active,
.history-clear:active {
    transform: scale(0.96);
}

.history-list {
    display: grid;
    gap: 10px;
    max-height: 420px;
    overflow-y: auto;
    margin: 0;
    padding: 2px;
    list-style: none;
}

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

.history-list::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: #3a3a3c;
}

.history-item {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 5px 10px;
    align-items: center;
    padding: 12px 14px;
    border-radius: 18px;
    background: var(--panel-soft);
    cursor: pointer;
    transition: background 0.15s ease, transform 0.08s ease;
}

.history-item:hover {
    background: #3a3a3c;
}

.history-item:active {
    transform: scale(0.985);
}

.history-expression {
    color: #ffffff;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.history-meta {
    grid-column: 1 / 2;
    color: var(--muted);
    font-size: 12px;
}

.history-delete {
    grid-column: 2 / 3;
    grid-row: 1 / 3;
    border: 0;
    border-radius: 999px;
    padding: 8px 11px;
    background: rgba(255, 69, 58, 0.14);
    color: var(--red);
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.08s ease;
}

.history-delete:hover {
    background: rgba(255, 69, 58, 0.24);
}

.history-delete:active {
    transform: scale(0.95);
}

.history-empty {
    padding: 18px;
    border-radius: 18px;
    background: var(--panel-soft);
    color: var(--muted);
    text-align: center;
    font-size: 14px;
}

@media (max-width: 760px) {
    body {
        padding: 20px 12px;
    }

    .app-layout {
        flex-direction: column;
        align-items: center;
    }

    .calculator,
    .history-panel {
        width: min(100%, 420px);
        flex: none;
    }

    .history-panel {
        max-height: none;
    }
}

@media (max-width: 420px) {
    .calculator {
        padding: 14px;
        border-radius: 28px;
    }

    .screen {
        padding: 18px 15px;
        border-radius: 22px;
    }

    .keys {
        gap: 9px;
    }

    .key {
        font-size: 21px;
    }

    .key-operator,
    .key-equals {
        font-size: 24px;
    }

    .history-panel {
        padding: 14px;
        border-radius: 28px;
    }
}

/* ---------- 模式切换 ---------- */
.mode-tabs {
    display: flex;
    gap: 6px;
    margin-bottom: 14px;
    padding: 4px;
    border-radius: 999px;
    background: #1c1c1e;
}

.mode-tab {
    flex: 1;
    border: 0;
    border-radius: 999px;
    padding: 9px 10px;
    background: transparent;
    color: var(--muted);
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}

.mode-tab.active {
    background: #3a3a3c;
    color: #ffffff;
}

.mode-panel.hidden {
    display: none;
}

/* ---------- 进制转换 ---------- */
.converter {
    display: grid;
    gap: 14px;
    padding: 6px 2px 2px;
}

.converter h2 {
    margin: 0 0 4px;
    color: #ffffff;
    font-size: 18px;
    font-weight: 600;
}

.converter-field {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
}

.converter-field input,
.converter-field select {
    width: 100%;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 14px;
    padding: 12px 14px;
    background: #1c1c1e;
    color: #ffffff;
    font-size: 16px;
    outline: 0;
}

.converter-field input:focus,
.converter-field select:focus {
    border-color: var(--operator);
}

.converter-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.converter-button {
    border: 0;
    border-radius: 16px;
    padding: 14px;
    background: var(--operator);
    color: #ffffff;
    font-size: 17px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.08s ease;
}

.converter-button:hover {
    background: var(--operator-hover);
}

.converter-button:active {
    transform: scale(0.98);
}

.converter-result {
    min-height: 58px;
    padding: 14px;
    border-radius: 16px;
    background: #1c1c1e;
    color: #ffffff;
    font-size: 22px;
    text-align: right;
    word-break: break-all;
}

.converter-result.error {
    color: var(--red);
}

@media (max-width: 420px) {
    .converter-row {
        grid-template-columns: 1fr;
    }
}

/* ---------- 科学计算 ---------- */
.sci-keys .key {
    aspect-ratio: 1.25;
    font-size: 15px;
}

.sci-keys .key-function {
    font-size: 14px;
}

.sci-keys .key-operator,
.sci-keys .key-equals {
    font-size: 20px;
}

@media (max-width: 420px) {
    .sci-keys .key {
        aspect-ratio: 1.35;
        font-size: 13px;
    }

    .sci-keys .key-function {
        font-size: 12px;
    }

    .sci-keys .key-operator,
    .sci-keys .key-equals {
        font-size: 17px;
    }
}
