/* ===== CSS VARIABLES & RESET ===== */
:root {
    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-hover: #f8fafc;
    --text: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border: #e2e8f0;
    --primary: #3b82f6;
    --primary-dark: #1d4ed8;
    --primary-light: #dbeafe;
    --success: #10b981;
    --success-light: #d1fae5;
    --warning: #f59e0b;
    --warning-light: #fef3c7;
    --danger: #ef4444;
    --danger-light: #fee2e2;
    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
    --radius: 12px;
    --radius-sm: 8px;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

[data-theme="dark"] {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-hover: #334155;
    --text: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: #334155;
    --primary-light: #1e3a5f;
    --success-light: #064e3b;
    --warning-light: #78350f;
    --danger-light: #7f1d1d;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
}

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

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
}

/* ===== TOP BAR ===== */
.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.logo {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text);
}

.logo span {
    color: var(--primary);
    font-weight: 300;
}

.top-actions { display: flex; gap: 0.5rem; }

.btn-icon {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-icon:hover { background: var(--surface-hover); color: var(--text); border-color: var(--primary); }

[data-theme="light"] .moon-icon { display: none; }
[data-theme="dark"] .sun-icon { display: none; }

/* ===== TAB NAVIGATION ===== */
.tab-nav {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 53px;
    z-index: 99;
}

.tab-scroll {
    display: flex;
    overflow-x: auto;
    padding: 0 1rem;
    gap: 0.25rem;
    scrollbar-width: none;
}

.tab-scroll::-webkit-scrollbar { display: none; }

.tab {
    padding: 0.75rem 1.25rem;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-family: var(--font);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
}

.tab:hover { color: var(--text); }

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

/* ===== MAIN CONTENT ===== */
.main-content {
    max-width: 1280px;
    margin: 0 auto;
    padding: 1.5rem;
}

.calc-section { display: none; }
.calc-section.active { display: block; }

.section-header {
    margin-bottom: 1.5rem;
}

.section-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.section-header p {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* ===== GRID LAYOUTS ===== */
.calc-grid {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 1.5rem;
    align-items: start;
}

.compare-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}

.results-col {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.savings-row, .metrics-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.metrics-row {
    grid-template-columns: repeat(3, 1fr);
}

/* ===== CARDS ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}

.card h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text);
}

.card h3 {
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text-secondary);
}

.card hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1.25rem 0;
}

.card-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.card-header-row h2 { margin-bottom: 0; }

.inputs-card {
    position: sticky;
    top: 110px;
}

/* ===== RESULT CARDS ===== */
.result-highlight {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
    text-align: center;
    padding: 1.5rem;
}

.result-highlight.secondary {
    background: var(--surface);
    border-color: var(--primary);
    color: var(--text);
}

.result-highlight.secondary .result-value { color: var(--primary); }

.result-label {
    font-size: 0.85rem;
    font-weight: 500;
    opacity: 0.9;
    margin-bottom: 0.25rem;
}

.result-value {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1.2;
}

.result-sub {
    font-size: 0.8rem;
    opacity: 0.8;
    margin-top: 0.25rem;
}

.result-highlight.secondary .result-label,
.result-highlight.secondary .result-sub { color: var(--text-secondary); }

/* Metric cards */
.metric-card {
    text-align: center;
    padding: 1rem;
}

.metric-label {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

.metric-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}

/* ===== INPUTS ===== */
.input-group {
    margin-bottom: 0.875rem;
    position: relative;
}

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

.input-group input,
.input-group select {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 0.9rem;
    color: var(--text);
    background: var(--surface);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-group input:focus,
.input-group select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.input-prefix, .input-suffix {
    position: absolute;
    top: calc(0.3rem + 1.2em + 0.6rem);
    font-size: 0.85rem;
    color: var(--text-muted);
    pointer-events: none;
    font-weight: 500;
}

.input-prefix {
    left: 0.75rem;
}

.input-group:has(> .input-prefix) input { padding-left: 1.75rem; }

.input-suffix {
    right: 0.75rem;
}

.input-group:has(> .input-suffix) input { padding-right: 2.25rem; }

.input-row {
    display: flex;
    gap: 0.75rem;
}

.input-row .input-group { flex: 1; }
.input-row .flex-1 { flex: 0.6; }
.input-row .flex-2 { flex: 1; }

.hint {
    display: block;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
}

/* ===== TOOLTIPS ===== */
.tooltip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--border);
    color: var(--text-muted);
    font-size: 0.65rem;
    font-weight: 700;
    cursor: help;
    position: relative;
    vertical-align: middle;
    margin-left: 4px;
}

.tooltip:hover::after {
    content: attr(data-tip);
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: var(--bg);
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    font-weight: 400;
    white-space: normal;
    width: 220px;
    line-height: 1.4;
    z-index: 50;
    box-shadow: var(--shadow-lg);
}

/* ===== TOGGLE BUTTONS ===== */
.toggle-group {
    display: flex;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.toggle-btn {
    padding: 0.35rem 0.75rem;
    border: none;
    background: var(--surface);
    color: var(--text-secondary);
    font-family: var(--font);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn:not(:last-child) { border-right: 1px solid var(--border); }

.toggle-btn.active {
    background: var(--primary);
    color: white;
}

/* ===== CHARTS ===== */
.chart-container {
    position: relative;
    width: 100%;
    max-width: 280px;
    margin: 0 auto 1rem;
}

.chart-container.chart-wide {
    max-width: 100%;
    height: 300px;
}

/* ===== TABLES ===== */
.table-wrap {
    overflow-x: auto;
    max-height: 400px;
    overflow-y: auto;
}

.amort-table, .compare-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.amort-table th, .compare-table th {
    text-align: left;
    padding: 0.6rem 0.75rem;
    border-bottom: 2px solid var(--border);
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    position: sticky;
    top: 0;
    background: var(--surface);
}

.amort-table td, .compare-table td {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border);
    font-variant-numeric: tabular-nums;
}

.amort-table tr:hover td { background: var(--surface-hover); }
.compare-table tr:hover td { background: var(--surface-hover); }

/* ===== BREAKDOWN LIST ===== */
.breakdown-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.breakdown-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.85rem;
}

.breakdown-item:last-child { border-bottom: none; }

.breakdown-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 0.5rem;
    flex-shrink: 0;
}

.breakdown-label {
    display: flex;
    align-items: center;
    color: var(--text-secondary);
}

.breakdown-value {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ===== SUMMARY GRID ===== */
.summary-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.summary-item {
    padding: 0.75rem;
    background: var(--surface-hover);
    border-radius: var(--radius-sm);
}

.summary-item .label {
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.summary-item .value {
    font-size: 1.1rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ===== DTI BARS ===== */
.dti-bars {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.dti-bar-group label {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    font-weight: 500;
    margin-bottom: 0.35rem;
    color: var(--text-secondary);
}

.dti-bar-track {
    height: 24px;
    background: var(--surface-hover);
    border-radius: 12px;
    overflow: hidden;
    position: relative;
}

.dti-bar-fill {
    height: 100%;
    border-radius: 12px;
    transition: width 0.5s ease;
    display: flex;
    align-items: center;
    padding-left: 0.5rem;
    font-size: 0.7rem;
    font-weight: 600;
    color: white;
    min-width: 2rem;
}

.dti-bar-fill.good { background: var(--success); }
.dti-bar-fill.warn { background: var(--warning); }
.dti-bar-fill.bad { background: var(--danger); }

.dti-bar-limit {
    position: absolute;
    top: 0;
    height: 100%;
    width: 2px;
    background: var(--text);
    opacity: 0.3;
}

/* ===== CHECKS LIST ===== */
.checks-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.check-item {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
}

.check-item.pass { background: var(--success-light); }
.check-item.fail { background: var(--danger-light); }
.check-item.warn { background: var(--warning-light); }

.check-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 700;
    color: white;
}

.check-item.pass .check-icon { background: var(--success); }
.check-item.fail .check-icon { background: var(--danger); }
.check-item.warn .check-icon { background: var(--warning); }

.positive { color: var(--success) !important; }
.negative { color: var(--danger) !important; }

/* ===== FOOTER ===== */
.footer {
    text-align: center;
    padding: 2rem 1rem;
    color: var(--text-muted);
    font-size: 0.8rem;
    border-top: 1px solid var(--border);
    margin-top: 3rem;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
    .calc-grid {
        grid-template-columns: 1fr;
    }

    .inputs-card {
        position: static;
    }

    .compare-grid {
        grid-template-columns: 1fr;
    }

    .savings-row {
        grid-template-columns: 1fr;
    }

    .metrics-row {
        grid-template-columns: 1fr 1fr;
    }
}

@media (max-width: 600px) {
    .main-content { padding: 1rem; }

    .tab { padding: 0.6rem 0.875rem; font-size: 0.8rem; }

    .result-value { font-size: 1.5rem; }

    .input-row { flex-direction: column; gap: 0; }

    .metrics-row { grid-template-columns: 1fr; }

    .summary-grid { grid-template-columns: 1fr; }
}

/* ===== PRINT ===== */
@media print {
    .top-bar, .tab-nav, .footer { display: none; }
    .calc-section { display: block !important; page-break-inside: avoid; margin-bottom: 2rem; }
    .calc-section:not(.active) { display: none !important; }
    .calc-section.active { display: block !important; }
    .inputs-card { position: static; }
    .card { box-shadow: none; border: 1px solid #ddd; }
    body { background: white; }
}
