/* CSS Variables - 简洁配色 */
:root {
    --bg-color: #f5f5f5;
    --card-bg: #ffffff;
    --text-primary: #333333;
    --text-secondary: #666666;
    --divider: #e0e0e0;
    --input-bg: #ffffff;
    --input-border: #e0e0e0;
    --btn-bg: #f0f0f0;
    --btn-hover: #e4e4e4;
    --btn-active: #d8d8d8;
    --operator-bg: #e94560;
    --operator-hover: #ff6b6b;
    --operator-active: #c73e54;
    --clear-bg: #d0d0d0;
    --clear-hover: #c4c4c4;
    --clear-active: #b8b8b8;
    --equals-bg: #4f8ef7;
    --equals-hover: #6fa3f9;
    --equals-active: #3a7be0;
    --fund-color: #4f8ef7;
    --comm-color: #e94560;
    --card-shadow: rgba(0,0,0,0.08);
    --card-shadow-hover: rgba(0,0,0,0.12);
    --border-radius: 16px;
    --btn-radius: 12px;
}

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

html {
    overflow-x: hidden;
    width: 100%;
}

body {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--bg-color);
    font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
    overflow-x: hidden;
    padding: 0;
    color: var(--text-primary);
}

/* ==================== 模式切换 ==================== */
.mode-switch {
    position: sticky;
    top: 0;
    z-index: 100;
    width: min(94vw, 480px);
    display: flex;
    justify-content: center;
    padding: 10px 12px 8px;
    background: var(--bg-color);
    gap: 8px;
}

.mode-btn {
    flex: 1;
    padding: 9px 16px;
    border: 1px solid var(--divider);
    border-radius: 10px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    background: var(--card-bg);
    color: var(--text-secondary);
    font-family: inherit;
    transition: all 0.2s;
}

.mode-btn.active {
    background: var(--fund-color);
    color: white;
    border-color: var(--fund-color);
}

/* ==================== 计算器主面板 ==================== */
.calculator {
    background: var(--card-bg);
    border-radius: var(--border-radius);
    padding: 16px;
    width: min(94vw, 460px);
    position: relative;
    z-index: 1;
    overflow-y: auto;
    max-height: calc(100vh - 80px);
}

.calculator.hidden {
    display: none;
}

/* ==================== 显示屏 ==================== */
.display {
    background: var(--input-bg);
    border-radius: var(--btn-radius);
    padding: 16px;
    margin-bottom: 16px;
    text-align: right;
    border: 1px solid var(--divider);
}

.previous-operand {
    color: var(--text-secondary);
    font-size: 1.1rem;
    min-height: 1.5rem;
    word-break: break-all;
}

.current-operand {
    color: var(--text-primary);
    font-size: 2.2rem;
    font-weight: 300;
    word-break: break-all;
}

/* ==================== 按钮网格 ==================== */
.buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

.btn {
    border: none;
    outline: none;
    background: var(--btn-bg);
    color: var(--text-primary);
    font-size: 1.4rem;
    padding: 18px;
    border-radius: var(--btn-radius);
    cursor: pointer;
    font-weight: 500;
    font-family: inherit;
    transition: all 0.15s;
}

.btn:hover { background: var(--btn-hover); }
.btn:active { background: var(--btn-active); }

.btn.operator { background: var(--operator-bg); color: white; }
.btn.operator:hover { background: var(--operator-hover); }
.btn.operator:active { background: var(--operator-active); }

.btn.clear { background: var(--clear-bg); }
.btn.clear:hover { background: var(--clear-hover); }
.btn.clear:active { background: var(--clear-active); }

.btn.delete { background: var(--clear-bg); font-size: 1.2rem; }
.btn.delete:hover { background: var(--clear-hover); }
.btn.delete:active { background: var(--clear-active); }

.btn.equals { background: var(--equals-bg); color: white; font-weight: bold; font-size: 1.8rem; }
.btn.equals:hover { background: var(--equals-hover); }
.btn.equals:active { background: var(--equals-active); }

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

/* ==================== 房贷计算器 ==================== */
.mortgage-panel {
    width: min(94vw, 480px);
    padding: 14px;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: calc(100vh - 76px);
    border-radius: var(--border-radius);
    background: var(--bg-color);
}

/* ---- 标题 ---- */
.mortgage-header {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--divider);
}

/* ---- 输入行 ---- */
.mortgage-row-top {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 8px;
}

.mortgage-input-group {
    margin-bottom: 0;
}

.mortgage-input-group label {
    display: block;
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 5px;
    font-weight: 500;
}

.input-wrapper {
    display: flex;
    align-items: center;
    background: var(--input-bg);
    border-radius: 8px;
    border: 1px solid var(--input-border);
    overflow: hidden;
}

.input-wrapper input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1rem;
    padding: 8px 10px;
    font-family: inherit;
    outline: none;
    min-width: 0;
}

.input-wrapper .input-unit {
    padding: 0 8px;
    color: var(--text-secondary);
    font-size: 0.8rem;
    background: #f8f8f8;
    border-left: 1px solid var(--divider);
    align-self: stretch;
    display: flex;
    align-items: center;
}

/* 隐藏 number 输入框原生 spinner */
input[type="number"] {
    -moz-appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* 滑块 */
.slider-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
}

.slider-wrapper input[type="range"] {
    flex: 1;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: #e0e0e0;
    border-radius: 2px;
    outline: none;
    cursor: pointer;
}

.slider-wrapper input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--fund-color);
    cursor: pointer;
}

#fundRatioValue {
    color: var(--fund-color);
    font-weight: 700;
    font-size: 0.95rem;
    min-width: 36px;
    text-align: right;
}

/* ---- 年限选择 ---- */
.years-section {
    margin-bottom: 8px;
}

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

.year-btn {
    padding: 8px 4px;
    border: 1px solid var(--divider);
    border-radius: 8px;
    background: var(--card-bg);
    color: var(--text-secondary);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    text-align: center;
    font-weight: 500;
}

.year-btn:hover {
    background: var(--btn-hover);
    color: var(--text-primary);
}

.year-btn.active {
    background: var(--fund-color);
    border-color: var(--fund-color);
    color: white;
}

/* ---- 利率信息 ---- */
.rate-info {
    background: var(--card-bg);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
    border: 1px solid var(--divider);
}

.rate-loading {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

.rate-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid #e0e0e0;
    border-top-color: var(--fund-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.rate-loaded, .rate-fallback {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.rate-source-line {
    font-size: 0.72rem;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.rate-source-line a {
    color: var(--fund-color);
    text-decoration: underline;
}

.rate-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 0;
}

.rate-label { color: var(--text-secondary); font-size: 0.78rem; }

.rate-value { font-weight: 700; font-size: 0.88rem; }
.fund-rate { color: var(--fund-color); }
.comm-rate { color: var(--comm-color); }

.rate-source-warning {
    font-size: 0.72rem;
    color: #f59e0b;
    margin-top: 2px;
}

/* ---- 结果卡片 ---- */
.mortgage-results {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 8px;
}

.result-card {
    border-radius: 10px;
    padding: 14px 12px;
    background: var(--card-bg);
    border: 1px solid var(--divider);
}

.result-title {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.result-hint {
    font-size: 0.68rem;
    color: var(--text-secondary);
    font-weight: 400;
    margin-left: auto;
}

.result-main {
    display: flex;
    align-items: baseline;
    gap: 3px;
    margin-bottom: 5px;
}

.result-monthly {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--text-primary);
}

.card-bx .result-monthly { color: var(--fund-color); }
.card-bj .result-monthly { color: var(--comm-color); }

.result-unit { font-size: 0.7rem; color: var(--text-secondary); }

.result-details { display: flex; flex-direction: column; gap: 3px; }

.result-detail-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.result-detail-row span:last-child {
    color: var(--text-primary);
    font-weight: 600;
}

.result-detail-row.highlight span { color: #f59e0b; }

/* ---- 对比摘要 ---- */
.compare-summary {
    text-align: center;
    margin-bottom: 6px;
}

.compare-tip {
    font-size: 0.78rem;
    color: var(--text-secondary);
    padding: 7px 12px;
    background: var(--card-bg);
    border-radius: 8px;
    border: 1px dashed var(--divider);
}

/* ---- 明细按钮 ---- */
.btn-detail-schedule {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--divider);
    border-radius: 8px;
    background: var(--card-bg);
    color: var(--text-secondary);
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.btn-detail-schedule:hover {
    background: var(--btn-hover);
    color: var(--text-primary);
}

/* ---- 还款明细表格 ---- */
.schedule-container {
    border: 1px solid var(--divider);
    border-radius: 10px;
    overflow: hidden;
}

.schedule-tabs {
    display: flex;
    background: #f8f8f8;
}

.schedule-tab {
    flex: 1;
    padding: 8px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    border-bottom: 2px solid transparent;
}

.schedule-tab.active {
    color: var(--fund-color);
    border-bottom-color: var(--fund-color);
}

.schedule-table-wrapper {
    max-height: 260px;
    overflow-y: auto;
}

.schedule-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.75rem;
}

.schedule-table thead th {
    position: sticky;
    top: 0;
    background: #f5f5f5;
    color: var(--text-secondary);
    padding: 6px 5px;
    text-align: right;
    font-weight: 600;
    border-bottom: 1px solid var(--divider);
}

.schedule-table thead th:first-child { text-align: center; }

.schedule-table tbody tr {
    border-bottom: 1px solid #f0f0f0;
}

.schedule-table tbody td {
    padding: 5px 5px;
    text-align: right;
    color: var(--text-primary);
}

.schedule-table tbody td:first-child { text-align: center; color: var(--text-secondary); }

/* ==================== 通用 ==================== */
.hidden { display: none !important; }

/* ==================== 响应式 ==================== */
@media (max-width: 480px) {
    .mode-switch {
        width: 95vw;
        padding: 8px 10px 6px;
    }
    .mode-btn {
        padding: 8px 12px;
        font-size: 0.85rem;
    }
    .calculator {
        width: 95vw;
        padding: 12px;
    }
    .mortgage-panel {
        width: 95vw;
        padding: 10px;
    }
    .mortgage-row-top {
        gap: 8px;
        margin-bottom: 6px;
    }
    .years-grid {
        gap: 5px;
    }
    .year-btn {
        padding: 7px 3px;
        font-size: 0.75rem;
    }
    .years-section {
        margin-bottom: 6px;
    }
    .rate-info {
        margin-bottom: 6px;
    }
    .mortgage-results {
        gap: 8px;
        margin-bottom: 6px;
    }
    .result-card {
        padding: 10px;
    }
    .result-monthly {
        font-size: 1.2rem;
    }
    .compare-summary {
        margin-bottom: 4px;
    }
    .btn-detail-schedule {
        margin-bottom: 4px;
    }
    .buttons {
        gap: 8px;
    }
    .btn {
        padding: 15px;
        font-size: 1.3rem;
    }
}

@media (max-width: 360px) {
    .mortgage-results {
        grid-template-columns: 1fr;
    }
}
