:root {
    --primary-color: #6366f1;
    --primary-hover: #4f46e5;
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --accent-color: #10b981;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --radius: 12px;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 2rem;
    /* Aggressive Reset */
    width: 100%;
    overflow-x: hidden;
    margin: 0;
}

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

.container {
    width: 100%;
    max-width: 1000px;
}

header {
    text-align: center;
    margin-bottom: 2rem;
}

header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--text-main);
    margin-bottom: 0.5rem;
}

header p {
    color: var(--text-muted);
    font-size: 1.1rem;
}

.calculator-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
}

.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border-color);
}

h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    color: var(--text-main);
    border-bottom: 2px solid var(--bg-color);
    padding-bottom: 0.5rem;
}

/* Input Section */
.input-group {
    margin-bottom: 1.5rem;
}

.input-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--text-main);
}

.input-wrapper {
    display: flex;
    align-items: center;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    background: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.input-wrapper input {
    border: none;
    outline: none;
    font-size: 1.1rem;
    width: 100%;
    font-family: inherit;
    color: var(--text-main);
}

.prefix,
.suffix {
    color: var(--text-muted);
    font-weight: 500;
}

.prefix {
    margin-right: 0.5rem;
}

.suffix {
    margin-left: 0.5rem;
}

/* Results Section */
.main-result {
    text-align: center;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    border-radius: var(--radius);
    color: white;
}

.main-result .label {
    display: block;
    font-size: 0.9rem;
    opacity: 0.9;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.main-result .value {
    display: block;
    font-size: 2.5rem;
    font-weight: 700;
}

.secondary-results {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 2rem;
}

.result-item {
    background: var(--bg-color);
    padding: 1rem;
    border-radius: 8px;
    text-align: center;
}

.result-item .label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.result-item .value {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-main);
}

.breakdown h3 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    color: var(--text-main);
}

.breakdown-item {
    display: flex;
    justify-content: space-between;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.95rem;
    color: var(--text-muted);
}

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

.breakdown-item span:last-child {
    font-weight: 600;
    color: var(--text-main);
}

.breakdown-item.highlight {
    color: var(--primary-color);
}

.breakdown-item.highlight span:last-child {
    color: var(--primary-color);
}

.breakdown-item.total-loan {
    border-top: 2px solid var(--border-color);
    border-bottom: 3px solid var(--border-color);
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-main);
}

@media (max-width: 768px) {
    body {
        padding: 1rem 0.5rem;
    }

    .container {
        width: 100%;
        overflow-x: hidden;
        /* Prevent horizontal scroll */
    }

    header h1 {
        font-size: 1.75rem;
    }

    .calculator-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
        /* Reduced gap */
    }

    .card {
        padding: 1rem;
        /* Reduced padding */
    }

    /* Input adjustments */
    .input-group {
        margin-bottom: 1.25rem;
    }

    /* Rent control specific */
    .rent-control {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .rent-input-wrapper {
        width: 100%;
        max-width: none;
    }

    /* Term section adjustments */
    .section-header-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .toggle-container {
        width: 100%;
        justify-content: space-between;
        flex-wrap: wrap;
    }

    .view-controls {
        margin-right: 0;
        width: 100%;
        display: flex;
        justify-content: space-between;
        /* Distribute buttons evenly */
    }

    .view-btn {
        flex: 1;
        text-align: center;
        padding: 8px 4px;
        /* Smaller padding */
        font-size: 0.85rem;
    }

    .toggle-wrapper {
        width: 100%;
        justify-content: space-between;
        margin-top: 0.5rem;
    }

    /* Projections inputs */
    .projections-inputs {
        grid-template-columns: 1fr;
    }

    /* Charts row */
    .charts-row {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }

    /* Full width metric */
    .full-width-metric {
        flex-direction: column;
        align-items: center;
        text-align: center;
        gap: 0.5rem;
    }

    .full-width-metric .label {
        margin-bottom: 0.25rem;
    }

    /* Results section */
    .secondary-results {
        gap: 0.75rem;
    }

    .main-result .value {
        font-size: 2rem;
    }
}

@media (max-width: 600px) {
    body {
        padding: 0.5rem;
        align-items: flex-start;
        /* Allow scrolling from top */
    }

    .container {
        width: 100%;
        max-width: 100%;
    }

    /* Force Single Column Everywhere */
    .calculator-grid,
    .cost-grid,
    .term-grid,
    .investment-results,
    .charts-row,
    .projections-inputs,
    .secondary-results {
        grid-template-columns: 1fr !important;
        gap: 1rem;
    }

    /* Adjust Font Sizes */
    header h1 {
        font-size: 1.5rem;
    }

    .main-result .value {
        font-size: 2rem;
    }

    /* Card Padding */
    .card {
        padding: 1rem;
    }

    /* Ensure Inputs Fit */
    input,
    select,
    button {
        max-width: 100%;
    }

    /* View Controls Stack */
    .view-controls {
        flex-direction: column;
    }

    .view-btn {
        width: 100%;
        margin-bottom: 2px;
    }

    /* Larger Rent Slider for Mobile */
    .rent-control input[type="range"] {
        height: 40px;
        /* Increase touch area */
        -webkit-appearance: none;
        /* Override default look */
        appearance: none;
        background: transparent;
        margin: 10px 0;
    }

    /* Webkit Thumb */
    .rent-control input[type="range"]::-webkit-slider-thumb {
        -webkit-appearance: none;
        height: 28px;
        width: 28px;
        border-radius: 50%;
        background: var(--primary-color);
        cursor: pointer;
        margin-top: -12px;
        /* Center on track */
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }

    /* Webkit Track */
    .rent-control input[type="range"]::-webkit-slider-runnable-track {
        width: 100%;
        height: 4px;
        cursor: pointer;
        background: #e2e8f0;
        border-radius: 2px;
    }

    /* Firefox Thumb */
    .rent-control input[type="range"]::-moz-range-thumb {
        height: 28px;
        width: 28px;
        border: none;
        border-radius: 50%;
        background: var(--primary-color);
        cursor: pointer;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }

    /* Firefox Track */
    .rent-control input[type="range"]::-moz-range-track {
        width: 100%;
        height: 4px;
        cursor: pointer;
        background: #e2e8f0;
        border-radius: 2px;
    }
}

@media (max-width: 480px) {
    header h1 {
        font-size: 1.5rem;
    }

    .view-controls {
        flex-direction: column;
        /* Stack buttons on very small screens if needed, or keep row */
    }

    .cost-grid,
    .term-grid {
        grid-template-columns: 1fr 1fr;
        /* Force 2 columns on small screens instead of 1 big one if possible, or 1fr for very small */
    }

    /* Ensure very small screens don't break layout */
    .investment-results {
        grid-template-columns: 1fr;
    }
}

/* Investment Section */
.investment-section {
    grid-column: 1 / -1;
    /* Span full width */
}

.cost-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    /* Reduced min width */
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.input-wrapper-sm label {
    font-size: 0.9rem;
    margin-bottom: 0.25rem;
    color: var(--text-muted);
}

.rent-control {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.rent-control input[type="range"] {
    flex: 1;
    accent-color: var(--primary-color);
}

.investment-results {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    /* Reduced min width */
    gap: 1rem;
    margin-top: 2rem;
    margin-bottom: 2rem;
}

.result-card {
    background: var(--bg-color);
    padding: 1.5rem;
    border-radius: var(--radius);
    text-align: center;
    border: 1px solid var(--border-color);
}

.result-card.highlight {
    background: linear-gradient(135deg, var(--accent-color), #059669);
    color: white;
    border: none;
}

.result-card .label {
    display: block;
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
    opacity: 0.9;
}

.result-card .value {
    display: block;
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.result-card .sub-label {
    font-size: 0.8rem;
    opacity: 0.8;
}

.chart-container {
    position: relative;
    height: 300px;
    width: 100%;
    margin-top: 2rem;
}

/* Term Section */
.term-section {
    grid-column: 1 / -1;
}

.term-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    /* Reduced min width */
    gap: 1rem;
    margin-bottom: 2rem;
}

.term-metric {
    text-align: center;
    padding: 1rem;
    background: var(--bg-color);
    border-radius: 8px;
}

.term-metric .label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.term-metric .value {
    display: block;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-main);
}

.term-metric.highlight .value {
    color: var(--primary-color);
}

.charts-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.chart-row-full {
    margin-bottom: 2rem;
}

.chart-wrapper h3 {
    text-align: center;
    font-size: 1rem;
    margin-bottom: 1rem;
    color: var(--text-muted);
}

.chart-container-sm {
    position: relative;
    height: 250px;
    width: 100%;
}

/* Print Button */
.print-btn {
    margin-top: 1rem;
    padding: 0.5rem 1.5rem;
    background-color: var(--text-main);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    font-weight: 500;
    transition: background-color 0.2s;
}

.print-btn:hover {
    background-color: var(--text-muted);
}

.projections-inputs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 1.5rem;
    padding: 1rem;
    background-color: var(--bg-color);
    border-radius: 8px;
}

.projections-inputs .input-group label {
    font-size: 0.9rem;
    margin-bottom: 0.25rem;
}

.projections-inputs .input-group input {
    width: 100%;
    padding: 0.25rem;
    border-radius: 4px;
}

/* View Controls (Segmented Button) */
.view-controls {
    display: flex;
    background-color: var(--bg-secondary);
    border-radius: 8px;
    padding: 4px;
    margin-right: 1.5rem;
    border: 1px solid var(--border-color);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.05);
}

.view-btn {
    background: none;
    border: none;
    padding: 8px 16px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.view-btn:hover {
    color: var(--text-main);
    background-color: rgba(255, 255, 255, 0.05);
}

.view-btn.active {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    color: white;
    box-shadow: 0 2px 4px rgba(16, 185, 129, 0.3);
}

.toggle-wrapper {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* Full Width Metric (Break Even Year) */
.full-width-metric {
    grid-column: 1 / -1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(135deg, var(--card-bg), var(--bg-secondary));
    padding: 1.25rem 1.5rem;
    border-radius: 12px;
    margin-top: 1rem;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.full-width-metric .label {
    margin-bottom: 0;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-main);
}

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

/* Toggle Switch */
.section-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.toggle-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: .4s;
}

input:checked+.slider {
    background-color: var(--text-main);
}

input:checked+.slider:before {
    transform: translateX(16px);
}

.slider.round {
    border-radius: 24px;
}

.slider.round:before {
    border-radius: 50%;
}

.toggle-label {
    font-size: 0.9rem;
    font-weight: 500;
}

/* Chart Header with Select */
.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.chart-header h3 {
    margin-bottom: 0;
}

.view-select {
    padding: 0.25rem 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 0.8rem;
}

/* Detailed Charts Visibility */
.detailed-chart {
    display: block;
}

.detailed-chart.hidden {
    display: none;
}

.projections-inputs.hidden {
    display: none;
}