/* Theme: Warm Financial Editorial 
   Inspiration: Anthropic, Zendesk, Stacked
*/

:root {
    /* Palette - Earthy & Neutral */
    --bg-canvas: #F3F0EB;       /* Warm paper background */
    --bg-surface: #FFFFFF;      /* Clean white for cards */
    --bg-input: #FAFAF8;        /* Very subtle off-white for inputs */
    
    --text-primary: #1A1918;    /* Warm Charcoal */
    --text-secondary: #5E5C59;  /* Stone Gray */
    --text-tertiary: #8C8A85;   /* Light Stone */

    --brand-primary: #2C4F38;   /* Deep Forest Green - The main "Trust" color */
    --brand-hover: #1F3A29;     
    --brand-accent: #C76D48;    /* Clay/Terracotta - For highlights */
    
    --border-subtle: #E6E2DA;   /* Warm, faint border */
    --border-focus: #2C4F38;    /* Focus state color */

    /* Status Colors - Muted/Earthy versions */
    --success-bg: #E3F0E6;
    --success-text: #216E39;
    --danger-bg: #FFEBE9;
    --danger-text: #CC3300;

    /* Spacing & Radius */
    --radius-sm: 6px;
    --radius-md: 12px;
    --radius-lg: 24px;
    --shadow-soft: 0 4px 20px rgba(0, 0, 0, 0.04);
    --shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.08);

    /* Z-Index Tokens */
    --z-base: 1;
    --z-drawer: 100;
    --z-toggle: 110;
    --z-tooltip: 10000;
}

/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
  appearance: textfield;
}

/* --- Reset & Typography --- */

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-canvas);
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* Use Serif for Headings to give that "Editorial" vibe (Anthropic/NYT style) */
h1, h2, h3, legend, .header h1 {
    font-family: Georgia, Cambria, "Times New Roman", Times, serif;
    font-weight: 600;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

#app {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    padding-bottom: 4rem;
}

.container {
    max-width: 1200px; /* Slightly wider for modern look */
    margin: 0 auto;
    padding: 20px;
    width: 100%;
}

/* --- Header --- */

.header {
    /* Removed the gradient box. Now it's clean and editorial. */
    background: transparent;
    color: var(--text-primary);
    padding: 60px 0 40px 0;
    margin: 0;
    text-align: center;
    border-bottom: 1px solid var(--border-subtle);
    margin-bottom: 40px;
}

.header h1 {
    font-size: 3rem;
    margin-bottom: 12px;
    color: var(--brand-primary);
}

.header p {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 1.15rem;
    color: var(--text-secondary);
    max-width: 600px;
    margin: 0 auto;
}

/* --- Layout Grid --- */

.content {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
}

/* --- Calculator Layout Container --- */

.calculator-layout {
    position: relative;
    width: 100%;
    /* Default: Stacked (mobile) */
}

/* Loading/Error positioning */
/* Note: These must be inside .calculator-layout to sit in the results column on desktop */
.calculator-layout #loading,
.calculator-layout #error {
    grid-column: 1 / -1; /* Span full width on mobile */
}

/* Side-by-Side (≥ 1800px) */
@media (min-width: 1800px) {
    /* Widen the main containers */
    .container,
    .content {
        max-width: 1700px;
    }

    .calculator-layout {
        display: grid;
        grid-template-columns: minmax(0, 7fr) minmax(0, 13fr); /* Form 40%, Results 60% */
        column-gap: 32px;
    }


    /* Move status messages to results column */
    .calculator-layout #loading,
    .calculator-layout #error {
        grid-column: 2;
    }
}

/* --- Form Section --- */

.form-section {
    background: var(--bg-surface);
    padding: 24px;
    border-radius: var(--radius-md);
    /* Soft, diffused shadow instead of hard box-shadow */
    box-shadow: var(--shadow-soft);
    border: 1px solid var(--border-subtle);
    position: relative;
    width: 100%;
    /* Drawer positioning will be overridden per breakpoint */
}

/* ============================================
   STICKY FORM HEADER (NEW)
   ============================================ */

.form-header-sticky {
    position: sticky;
    top: -24px; /* Account for parent padding to sit flush at top when scrolling */
    z-index: 20; /* Above form content */
    background: var(--bg-surface);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 24px;
    margin: -24px -24px 24px -24px; /* Extend to edges, add bottom spacing */
    border-bottom: 1px solid var(--border-subtle);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05); /* Subtle depth when scrolling */
}

.form-header-sticky h2 {
    margin: 0;
    padding: 0;
    border: none;
    font-size: 1.5rem;
    color: var(--brand-primary);
}

/* Remove the old standalone h2 styles inside form-section since it's now in the sticky header */
.form-section > h2 {
    display: none; /* This h2 is now inside .form-header-sticky */
}

/* Mobile Bottom Sheet (< 900px) */
@media (max-width: 899px) {
    .form-section {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 70vh;
        width: 100vw;
        z-index: var(--z-drawer);
        transform: translateY(0);
        transition: transform 0.3s ease;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15);
        border-radius: var(--radius-md) var(--radius-md) 0 0; /* ADD THIS - rounded top only */
    }

    /* Add this new rule */
    .form-header-sticky {
        border-radius: var(--radius-md) var(--radius-md) 0 0;
    }

    /* Closed state */
    body.drawer-closed .form-section {
        transform: translateY(100%);
    }
}

/* Left Overlay (900px - 1799px) */

@media (min-width: 900px) and (max-width: 1799px) {
    .calculator-layout {
        padding-left: 60px;
        padding-right: 20px;
    }

    .form-section {
        position: fixed;
        top: 0;
        left: 0;
        width: max(800px, 60vw);
        height: 100vh;
        z-index: var(--z-drawer);
        border-radius: 0;
        transform: translateX(0); /* Open state */
        transition: transform 0.3s ease;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: 8px 0 30px rgba(0, 0, 0, 0.15);
    }

    /* UPDATED: Closed state now shows 60px sliver */
    body.drawer-closed .form-section {
        transform: translateX(calc(-100% + 40px));
        overflow: hidden;
    }

    /* Style the sliver to be visually distinct */
    body.drawer-closed .form-section .form-header-sticky {
        width: 40px;
        margin-left: auto;
        margin-right: -24px; /* Keep right edge flush with form-section edge */
        border-right: 2px solid var(--brand-primary);
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
    }

    /* Hide the h2 text when closed, keep toggle centered */
    body.drawer-closed .form-section .form-header-sticky h2 {
        display: none; /* Remove from flow so toggle can center */
    }

    /* Center the toggle in the sliver */
    body.drawer-closed .form-section .form-header-sticky {
        justify-content: center;
        padding-left: 0;
        padding-right: 0;
    }
}

/* Side-by-Side (≥ 1800px) */
@media (min-width: 1800px) {
    .form-section {
        position: relative;
        width: 100%;
        height: auto;
        min-width: unset;
        max-width: none;
        transform: translateX(0);
        transition: transform 0.3s ease;
        overflow-y: visible;
        border-radius: var(--radius-md);
    }

    /* Add this new rule */
    .form-header-sticky {
        border-radius: var(--radius-md) var(--radius-md) 0 0;
    }

    /* UPDATED: Closed state shows sliver on side-by-side layout too */
    body.drawer-closed .form-section {
        transform: translateX(calc(-100% + 60px));
    }

}

/* --- Drawer Toggles --- */

.drawer-toggle {
    background: transparent;
    border: none;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    z-index: var(--z-toggle);
    padding: 4px;
}

.drawer-toggle:hover {
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    transform: scale(1.1);
}

.drawer-toggle:active {
    transform: scale(0.95);
}

.drawer-toggle svg {
    width: 24px;
    height: 24px;
    color: var(--text-primary);
    transition: transform 0.2s ease;
}


/* Icon visibility based on drawer state AND breakpoint */

/* Mobile (< 900px): Use hamburger/close icons */
@media (max-width: 899px) {
    .icon-arrow-left,
    .icon-arrow-right {
        display: none !important;
    }
    .icon-hamburger,
    .icon-close {
        display: none;
    }
    body.drawer-open .icon-close {
        display: block;
    }
    body.drawer-open .icon-hamburger {
        display: none;
    }
    body.drawer-closed .icon-close {
        display: none;
    }
    body.drawer-closed .icon-hamburger {
        display: block;
    }
}

/* Tablet/Desktop (900px - 1799px): Use arrow icons */
@media (min-width: 900px) and (max-width: 1799px) {
    .icon-hamburger,
    .icon-close {
        display: none !important;
    }
    .icon-arrow-left,
    .icon-arrow-right {
        display: none;
    }
    /* Show left arrow when open, right arrow when closed */
    body.drawer-open .icon-arrow-left {
        display: block;
    }
    body.drawer-closed .icon-arrow-right {
        display: block;
    }
}


/* Mobile Toggle - Enhanced (< 900px) */
.drawer-toggle-mobile {
    position: fixed;
    bottom: calc(70vh + 16px); /* 16px gap above open drawer */
    left: 50%;
    transform: translateX(-50%);
    transition: bottom 0.3s ease, background 0.2s ease, box-shadow 0.2s ease;

    /* Fixed width prevents centering shift */
    width: 160px; /* Accommodates "Hide Inputs" text */
    height: 48px;
    border-radius: 24px; /* Pill shape */

    /* Layout for icon + text */
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 20px;

    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-soft);
    z-index: var(--z-toggle);
}

/* Closed state */
body.drawer-closed .drawer-toggle-mobile {
    bottom: 24px;
}

/* Remove position-shifting hover - only background/shadow changes */
.drawer-toggle-mobile:hover {
    transform: translateX(-50%); /* Stays centered */
    background: var(--bg-input);
    box-shadow: var(--shadow-hover);
}

.drawer-toggle-mobile:active {
    transform: translateX(-50%) scale(0.98);
}

/* Text styling */
.drawer-toggle-text {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
}

@media (min-width: 900px) {
    .drawer-toggle-mobile {
        display: none;
    }
}

/* Mobile Toggle Icon Rotation */
@media (max-width: 899px) {
    .drawer-toggle-mobile .icon-arrow {
        transition: transform 0.3s ease;
    }

    /* Open state: chevron points down */
    body.drawer-open .drawer-toggle-mobile .icon-arrow {
        transform: rotate(180deg);
    }

    /* Closed state: chevron points up (default) */
    body.drawer-closed .drawer-toggle-mobile .icon-arrow {
        transform: rotate(0deg);
    }
}

/* Desktop toggle positioning - NOW ON THE RIGHT */
.drawer-toggle-desktop {
    position: relative; /* Changed from absolute - it's now in flex container */
    /* Remove old top/left positioning */
}

@media (max-width: 899px) {
    .drawer-toggle-desktop {
        display: none;
    }
}

/* Hide desktop toggle for large screens ≥1800px */
@media (min-width: 1800px) {
    .drawer-toggle-desktop {
        display: none;
    }
}

.form-section h2 {
    font-size: 1.5rem;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-subtle);
    color: var(--brand-primary);
}

/* Clean up the Fieldsets to look like modern sections */
fieldset {
    border: none;
    padding: 0;
    margin: 0 0 24px 0;
    border-radius: 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 12px;
    align-items: start;
}

fieldset:last-child {
    margin-bottom: 0;
}

legend {
    grid-column: 1 / -1;
    padding: 0;
    margin-bottom: 12px;
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--text-primary);
    display: block;
    width: 100%;
    border-bottom: 1px solid var(--border-subtle);
    padding-bottom: 8px;
}

/* Full width elements in grid */
fieldset > p,
fieldset > table,
fieldset > .form-group-full,
fieldset > #cpf_status,
fieldset > .add-row-btn {
    grid-column: 1 / -1;
}

/* ============================================
   FIELDSET OPTIMIZATION FOR WIDE SCREENS
   ============================================ */

/* At 1800px+, form column gets ~640px (40% of 1600px)
   Use explicit 2 columns */
@media (min-width: 1800px) {
    fieldset {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Form tables: enforce scroll at ≥1800px side-by-side layout

   Form column is ~595px; tables need min-widths to trigger scroll */

@media (min-width: 1800px) {
    .form-section .interest-rate-table {
        min-width: 450px;
    }

    .form-section .upfront-costs-table {
        min-width: 800px;
    }

    .form-section .progressive-table {
        min-width: 800px;
    }
}

/* --- Inputs & Controls --- */

.form-group {
    margin-bottom: 0;
}

.form-group label {
    display: block;
    margin-bottom: 4px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; /* Keep labels readable */
}

/* Ensure inputs align even with multi-line labels */
.form-group:not(.form-group-checkbox) > label {
    min-height: 2.5em;
    line-height: 1.25;
    /* AIDEV-NOTE: Keep label contents (text + tooltip icons) on a single line.
       Using a horizontal flex row here prevents tooltip icons from wrapping
       onto a new line while still aligning multi-line labels to the bottom
       of the reserved label area for grid alignment. */
    display: flex;
    align-items: flex-end;
    column-gap: 4px;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 8px 12px;
    background-color: var(--bg-input);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    color: var(--text-primary);
    transition: all 0.2s ease;
    font-family: inherit;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    background-color: #fff;
    border-color: var(--brand-primary);
    box-shadow: 0 0 0 3px rgba(44, 79, 56, 0.1); /* Soft green glow */
}

/* Form Validation States */
input:invalid:not(:focus):not(:placeholder-shown) {
    border-color: var(--danger-text);
}

input:invalid:focus {
    border-color: var(--danger-text);
    box-shadow: 0 0 0 3px var(--danger-bg);
}

.form-text {
    font-size: 0.85rem;
    color: var(--text-tertiary);
    margin-bottom: 12px;
    margin-top: 0;
    line-height: 1.4;
}

/* Radio Buttons & Checkboxes */
.radio-group {
    /* AIDEV-NOTE: Use a 2‑column grid so each radio input and its label share
       a row (input in column 1, label in column 2). This keeps the controls
       inline while remaining robust on narrow screens. */
    display: grid;
    grid-template-columns: auto 1fr;
    column-gap: 8px;
    row-gap: 4px;
    align-items: center;
    margin-top: 4px;
}

.radio-group input[type="radio"] {
    justify-self: start;
}

.radio-group label,
.form-group-checkbox label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: normal;
    cursor: pointer;
    color: var(--text-primary);
    text-transform: none;
    letter-spacing: normal;
}

.form-group-checkbox {
    /* AIDEV-NOTE: Present checkbox and text as a single row so the control
       stays inline with its label instead of stacking vertically. */
    display: flex;
    align-items: center;
    gap: 8px;
}

.form-group-checkbox input[type="checkbox"] {
    margin: 0;
}

input[type="checkbox"],
input[type="radio"] {
    accent-color: var(--brand-primary);
    width: 18px;
    height: 18px;
    cursor: pointer;
}

/* --- Input Suffixes (%) --- */
.input-suffix-pct {
    position: relative;
    display: block;
    width: 100%;
}

.input-suffix-pct input {
    padding-right: 28px !important; /* Space for % */
}

.input-suffix-pct::after {
    content: "%";
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-tertiary);
    font-size: 0.9rem;
    pointer-events: none;
    line-height: 1;
}

/* Adjust for table inputs where padding is smaller */
td .input-suffix-pct input {
    padding-right: 20px !important;
}

td .input-suffix-pct::after {
    right: 4px;
    font-size: 0.8rem;
}

/* --- CPF Status Display --- */
#cpf_status {
    background: var(--bg-input);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 16px;
    margin-bottom: 20px;
}

#cpf_needed_display {
    font-size: 0.9rem;
    line-height: 1.5;
}

#cpf_needed_display small {
    color: var(--text-tertiary);
}

#downpayment_breakdown {
    margin-top: 10px;
    font-size: 0.9rem;
    line-height: 1.5;
    color: var(--text-secondary);
    grid-column: 1 / -1;
}

.breakdown-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 12px;
    align-items: start;
}

.breakdown-item {
    display: flex;
    flex-direction: column;
}

.breakdown-label {
    display: block;
    margin-bottom: 4px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    min-height: 2.5em;
    line-height: 1.25;
}

.breakdown-value {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.cash-breakdown-details {
    font-size: 0.75rem;
    color: var(--text-tertiary);
    line-height: 1.4;
}

.cash-breakdown-details > div {
    margin-bottom: 2px;
}

/* --- Results Section --- */

.results-section {
    background: transparent; /* Blend into canvas */
    padding: 0;
    border-radius: 0;
    box-shadow: none;
}

.results-section h2 {
    font-size: 2rem;
    margin-bottom: 30px;
    color: var(--text-primary);
}

/* Metric Cards */
.headline-metrics {
    margin-bottom: 40px;
}

.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
}

.metric-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 24px;
    text-align: left; /* Modern look aligns left usually */
    box-shadow: var(--shadow-soft);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.metric-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-hover);
}

.metric-label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-weight: 600;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.metric-value {
    display: block;
    font-size: 2rem; /* Larger numbers */
    font-weight: 700;
    color: var(--brand-primary);
    font-family: Georgia, serif; /* Serif numbers look elegant */
}

/* Transaction Summary Row Alignment */
.summary-metric-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 24px;
    padding-bottom: 16px;
    border-bottom: 1px dashed var(--border-subtle);
}

.summary-metric-row:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

@media (max-width: 900px) {
    .summary-metric-row {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 600px) {
    .summary-metric-row {
        grid-template-columns: 1fr;
    }
}

/* Sale Year Selector (JS-generated) */
.sale-year-selector {
    padding: 8px 12px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.sale-year-selector:hover {
    border-color: var(--brand-primary);
}

.sale-year-selector:focus {
    outline: none;
    border-color: var(--brand-primary);
    box-shadow: 0 0 0 3px rgba(44, 79, 56, 0.1);
}

/* --- Tables --- */

.table-section {
    margin-top: 40px;
    background: var(--bg-surface);
    border-radius: var(--radius-md);
    padding: 30px;
    border: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-soft);
}

.table-section h3 {
    font-size: 1.25rem;
    margin-bottom: 20px;
    color: var(--text-primary);
}

/* Universal Table Styles */
table {
    width: 100%;
    border-collapse: separate; /* Allows spacing if needed */
    border-spacing: 0;
    font-size: 0.9rem;
}

/* Remove grid look */
th, td {
    padding: 14px 16px;
    text-align: right;
    border-bottom: 1px solid var(--border-subtle);
    border-right: none;
    border-left: none;
    border-top: none;
}

th:first-child, td:first-child {
    text-align: left;
    padding-left: 8px;
}

th {
    background-color: transparent; /* No colored headers */
    color: var(--text-secondary);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    border-bottom: 2px solid var(--border-subtle);
}

tr:last-child td {
    border-bottom: none;
}

tr:hover td {
    background-color: var(--bg-canvas); /* Gentle highlight */
}

/* Special Table Cells (JS-rendered breakdowns) */
.mortgage-breakdown,
.tax-breakdown {
    font-size: 0.85rem;
    line-height: 1.6;
    color: var(--text-secondary);
}

.mortgage-breakdown strong,
.tax-breakdown strong {
    color: var(--text-primary);
    display: block;
    margin-bottom: 4px;
}

/* Input tables (like Upfront Costs) specific tweaks */
.upfront-costs-table td input,
.interest-rate-table td input,
.progressive-table td input,
.progressive-table td select {
    border: none;
    background: transparent;
    width: 100%;
    font-size: 0.9rem;
    padding: 4px;
    text-align: right;
    color: var(--text-primary);
}

/* Interest Rate Table column widths */
.interest-rate-table {
    table-layout: fixed;
}

.interest-rate-table th:nth-child(1),
.interest-rate-table td:nth-child(1) {
    width: 30%;
}

.interest-rate-table th:nth-child(2),
.interest-rate-table td:nth-child(2) {
    width: 40%;
}

.interest-rate-table th:nth-child(3),
.interest-rate-table td:nth-child(3) {
    width: 30%;
}

.upfront-costs-table td input:focus,
.interest-rate-table td input:focus,
.progressive-table td input:focus,
.progressive-table td select:focus {
    background: var(--bg-input);
    box-shadow: none;
    border-bottom: 2px solid var(--brand-primary);
    border-radius: 0;
}

/* Progressive Table Locked Rows */
tr[data-locked] {
    background-color: var(--bg-input);
}

tr[data-locked] input[readonly] {
    opacity: 0.7;
    cursor: not-allowed;
    background-color: transparent;
}

.progressive-table .tooltip-icon {
    margin-left: 6px;
}

/* Progressive Table column widths */
/* Desktop: Fixed layout with percentage widths */
@media (min-width: 901px) {
    .progressive-table {
        table-layout: fixed;
    }

    .progressive-table th:nth-child(1),
    .progressive-table td:nth-child(1) {
        width: 25%; /* Milestone column - wider to fit names */
    }

    .progressive-table th:nth-child(2),
    .progressive-table td:nth-child(2) {
        width: 10%; /* % of Price */
    }

    .progressive-table th:nth-child(3),
    .progressive-table td:nth-child(3) {
        width: 15%; /* Amount */
    }

    .progressive-table th:nth-child(4),
    .progressive-table td:nth-child(4) {
        width: 10%; /* Month */
    }

    .progressive-table th:nth-child(5),
    .progressive-table td:nth-child(5) {
        width: 10%; /* Cash % */
    }

    .progressive-table th:nth-child(6),
    .progressive-table td:nth-child(6) {
        width: 10%; /* CPF % */
    }

    .progressive-table th:nth-child(7),
    .progressive-table td:nth-child(7) {
        width: 20%; /* Loan Drawdown */
    }
}

/* Milestone name styling for progressive table */
.progressive-table .milestone-name-small {
    font-size: 0.85rem !important;
    text-align: left !important;
}

/* --- Buttons --- */

button {
    font-family: inherit;
    transition: all 0.2s ease;
}

.add-row-btn {
    background-color: var(--bg-surface);
    color: var(--brand-primary);
    border: 1px solid var(--brand-primary);
    padding: 10px 20px;
    border-radius: 50px; /* Pill shape */
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    margin-top: 10px;
}

.add-row-btn:hover {
    background-color: var(--brand-primary);
    color: white;
}

.add-row-btn:active {
    transform: scale(0.98);
}

.remove-row-btn, .remove-cost-row-btn, .remove-progressive-row-btn {
    background-color: transparent;
    color: var(--text-tertiary);
    border: 1px solid transparent;
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.85rem;
}

.remove-row-btn:hover:not(:disabled), 
.remove-cost-row-btn:hover:not(:disabled),
.remove-progressive-row-btn:hover:not(:disabled) {
    background-color: var(--danger-bg);
    color: var(--danger-text);
}

.remove-row-btn:active:not(:disabled),
.remove-cost-row-btn:active:not(:disabled),
.remove-progressive-row-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.remove-row-btn:disabled,
.remove-cost-row-btn:disabled,
.remove-progressive-row-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* --- Utilities & States --- */

.loading {
    text-align: center;
    padding: 40px;
    font-size: 1.1rem;
    color: var(--text-secondary);
    font-style: italic;
}

.error {
    background-color: var(--danger-bg);
    border: 1px solid rgba(204, 51, 0, 0.2);
    color: var(--danger-text);
    padding: 16px;
    border-radius: var(--radius-sm);
    margin-bottom: 20px;
}

.tooltip-icon {
    display: inline-block;
    cursor: help;
    margin-left: 4px;
    font-size: 0; /* Hide the existing emoji text */
    vertical-align: middle;
}

.tooltip-icon::before {
    content: "";
    display: inline-block;
    width: 14px;
    height: 14px;
    margin-left: 4px;
    vertical-align: middle;
    --icon-color: #666; /* default color */
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="7.5" fill="none" stroke="%23666" stroke-width="1.5"/><text x="8" y="11.5" text-anchor="middle" font-size="11" font-weight="bold" fill="%23666">i</text></svg>');
    background-size: contain;
    background-repeat: no-repeat;
    cursor: help;
}

/* Color variants */
.tooltip-icon.info::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="7.5" fill="none" stroke="%232196F3" stroke-width="1.5"/><text x="8" y="11.5" text-anchor="middle" font-size="11" font-weight="bold" fill="%232196F3">i</text></svg>');
}

.tooltip-icon.success::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="7.5" fill="none" stroke="%234CAF50" stroke-width="1.5"/><text x="8" y="11.5" text-anchor="middle" font-size="11" font-weight="bold" fill="%234CAF50">i</text></svg>');
}

.tooltip-icon.warning::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="7.5" fill="none" stroke="%23FF9800" stroke-width="1.5"/><text x="8" y="11.5" text-anchor="middle" font-size="11" font-weight="bold" fill="%23FF9800">i</text></svg>');
}

.tooltip-icon.error {
    background-color: transparent;
    border: none;
    color: inherit;
    padding: 0;
    margin-bottom: 0;
}

.tooltip-icon.error::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="7.5" fill="none" stroke="%23f44336" stroke-width="1.5"/><text x="8" y="11.5" text-anchor="middle" font-size="11" font-weight="bold" fill="%23f44336">i</text></svg>');
}

/* --- Responsive --- */

@media (max-width: 900px) {
    .content {
        gap: 40px;
    }

    .form-section {
        padding: 24px;
    }

    .header h1 {
        font-size: 2rem;
    }
    
    .header {
        padding: 40px 0 20px 0;
    }

    .metrics-grid {
        grid-template-columns: 1fr; 
    }
    
    th, td {
        padding: 10px 8px;
        font-size: 0.85rem;
    }
}

@media (max-width: 600px) {
    .container {
        padding: 12px;
    }

    .header h1 {
        font-size: 1.75rem;
    }

    .header p {
        font-size: 1rem;
    }

    .form-section {
        padding: 20px;
    }

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

    .table-section {
        padding: 20px;
        /* overflow-x: auto; REMOVED: Now using .table-scroll-wrapper for proper scrolling */
    }

    table {
        font-size: 0.8rem;
    }

    th, td {
        padding: 8px 6px;
    }
}

/* --- Custom Tooltip --- */
.custom-tooltip {
    position: fixed;
    background: var(--text-primary);
    color: var(--bg-surface);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    max-width: 250px;
    z-index: 10000;
    pointer-events: none; /* Critical: prevents tooltip from blocking mouse events */
    opacity: 0;           /* Use opacity for smooth transitions */
    transition: opacity 0.1s ease;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    line-height: 1.4;
}

/* AIDEV-TODO: Add mobile table handling */
/* Scroll container - works at all breakpoints */
.table-scroll-wrapper {
  grid-column: 1 / -1;  /* Span full fieldset width */
  width: 100%;
  overflow-x: auto;
  overflow-y: auto;  /* Enable vertical scrolling for tall tables */
  max-height: 600px;  /* Prevent tables from becoming too tall */
  -webkit-overflow-scrolling: touch;  /* iOS momentum scrolling */
  position: relative;
}
/* Large desktop: Keep scrolling enabled for sticky columns to work */
@media (min-width: 1001px) and (max-width: 1799px) {
  .table-scroll-wrapper {
    overflow-x: auto;  /* Re-enable for sticky column support */
  }

  /* Optional: Hide scroll indicators if tables typically fit */
  .table-scroll-wrapper::before,
  .table-scroll-wrapper::after {
    display: none;
  }
}

/* Subtle gradient shadows to indicate scrollable content */
@media (max-width: 1000px) {
  .table-scroll-wrapper::before,
  .table-scroll-wrapper::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    width: 20px;
    pointer-events: none;  /* Don't block interactions */
    z-index: 1;
    opacity: 0.6;
  }
  
  /* Left edge shadow */
  .table-scroll-wrapper::before {
    left: 0;
    background: linear-gradient(to right, 
      var(--bg-surface), 
      transparent
    );
  }
  
  /* Right edge shadow */
  .table-scroll-wrapper::after {
    right: 0;
    background: linear-gradient(to left, 
      var(--bg-surface), 
      transparent
    );
  }
}

/* Desktop: Maintain fixed layout for predictable columns */
@media (min-width: 901px) {
  .interest-rate-table,
  .progressive-table {
    table-layout: fixed;  /* Already exists, keeping for clarity */
  }
}
/* Tablet & Mobile: Switch to auto layout for natural wrapping */
@media (max-width: 900px) {
  .interest-rate-table,
  .upfront-costs-table,
  .progressive-table {
    table-layout: auto;
    min-width: 600px;  /* Prevent over-compression */
  }
  
  /* Progressive table needs more space due to long milestone names */
  .progressive-table {
    min-width: 700px;
  }
}
/* Mobile: More aggressive min-width to ensure readability */
@media (max-width: 600px) {
  .interest-rate-table {
    min-width: 500px;
  }
  
  .upfront-costs-table {
    min-width: 650px;  /* 7 columns need space */
  }
  
  .progressive-table {
    min-width: 750px;  /* Long names + 7 columns */
  }
}

/* Tablet: Enhanced touch targets */
@media (max-width: 900px) {
  th, td {
    padding: 12px 10px;  /* Increased from 10px 8px */
    min-height: 44px;    /* Touch target minimum */
  }
  
  /* Inputs inside tables - ensure adequate touch area */
  .upfront-costs-table td input,
  .interest-rate-table td input,
  .progressive-table td input,
  .progressive-table td select {
    min-width: 80px;     /* Prevent over-shrinking */
    min-height: 44px;    /* Touch target */
    padding: 10px 8px;   /* Comfortable tap area */
    font-size: 16px;     /* Prevents iOS auto-zoom */
  }
}
/* Mobile: Optimized for narrow screens */
@media (max-width: 600px) {
  th, td {
    padding: 12px 8px;
    white-space: nowrap;  /* Prevent awkward wrapping */
  }
  
  /* Slightly smaller inputs to fit more content */
  .upfront-costs-table td input,
  .interest-rate-table td input,
  .progressive-table td input {
    min-width: 70px;
    font-size: 16px;  /* Still prevent zoom */
  }
  
  /* Exception: Name/Milestone columns need more width */
  .upfront-costs-table td:first-child input,
  .progressive-table td:first-child input {
    min-width: 120px;
  }
}

/* Locked rows must remain visually distinct on mobile */
@media (max-width: 900px) {
  tr[data-locked] {
    background-color: var(--bg-input);  /* Preserve existing style */
  }
  
  tr[data-locked] input[readonly] {
    opacity: 0.7;
    cursor: not-allowed;
    min-width: 70px;  /* Still enforce minimum */
  }
}
/* Milestone names (first column) - prevent truncation */
@media (max-width: 900px) {
  .progressive-table td:first-child {
    min-width: 140px;  /* Long names like "Doors / Windows / Plumbing (5%)" */
  }
  
  .progressive-table .milestone-name-small {
    font-size: 0.85rem !important;
    text-align: left !important;
    white-space: normal;  /* Allow wrapping in this column */
    line-height: 1.3;
  }
}

/* Ensure action buttons remain clickable in scroll context */
@media (max-width: 900px) {
  .remove-row-btn,
  .remove-cost-row-btn {
    min-width: 60px;     /* Adequate touch target */
    min-height: 44px;
    padding: 8px 12px;
    white-space: nowrap;
  }
  
  /* Checkboxes need proper touch area */
  input[type="checkbox"] {
    width: 24px;   /* Larger than default 18px */
    height: 24px;
    margin: 10px;  /* Extra padding for easier tapping */
  }
}

/* Ledger table mobile/tablet scrolling */
@media (max-width: 900px) {
  .ledger-table {
    table-layout: auto;
    min-width: 900px;  /* 10 columns need more space */
  }
}

/* Mobile: More aggressive min-width for ledger tables */
@media (max-width: 600px) {
  .ledger-table {
    min-width: 950px;
  }

  #exitSummaryTable {
    min-width: 650px;  /* Only 5 columns */
  }
}

/* ============================================
   RESULTS SECTION OPTIMIZATION
   ============================================ */

/* At 1800px+, results column is ~864px
   Ledger table (900px min-width) will have minimal scroll
   or we can adjust it to fit perfectly */
@media (min-width: 1800px) {
    .ledger-table {
        min-width: 850px; /* Fits perfectly in 864px column */
    }

    /* Alternatively, let it scroll slightly - users at this resolution
       are comfortable with minor scrolling */
}

/* =================================== STICKY HEADERS & FIRST COLUMN =================================== */

/* Applied at all screen sizes - browser only activates when scrolling occurs */

/* ============= STICKY HEADER ROW (Vertical Scroll) ============= */

.table-scroll-wrapper table thead th,
.table-section table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background-color: var(--bg-surface);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
}

/* ============= STICKY FIRST COLUMN (Horizontal Scroll) ============= */

.table-scroll-wrapper table th:first-child,
.table-scroll-wrapper table td:first-child,
.table-section table th:first-child,
.table-section table td:first-child {
  position: sticky;
  left: 0;
  z-index: 3;
  background-color: var(--bg-surface);
  box-shadow: 2px 0 4px rgba(0, 0, 0, 0.06);
}

/* ============= CORNER CELL (Top-Left) ============= */

.table-scroll-wrapper table thead th:first-child,
.table-section table thead th:first-child {
  z-index: 5;
  box-shadow:
    2px 0 4px rgba(0, 0, 0, 0.06),
    0 2px 4px rgba(0, 0, 0, 0.06);
}

/* ============= PRESERVE EXISTING STYLES ============= */

tr:hover td:first-child {
  background-color: var(--bg-canvas);
}

tr[data-locked] td:first-child {
  background-color: var(--bg-input);
}
