/* ============================================
   CSS CUSTOM PROPERTIES
   Centralized design tokens for consistent theming.
============================================ */
:root {
    --color-bg:         #f4f6f9;
    --color-surface:    #ffffff;
    --color-primary:    #2563eb;
    --color-primary-dk: #1d4ed8;
    --color-secondary:  #475569;
    --color-accent:     #10b981;
    --color-danger:     #ef4444;
    --color-text:       #1e293b;
    --color-text-muted: #64748b;
    --color-border:     #e2e8f0;
    --radius:           8px;
    --shadow-sm:        0 1px 3px rgba(0,0,0,0.08);
    --shadow-md:        0 4px 12px rgba(0,0,0,0.1);
    /* Fieldset interior fill — set to 'var(--color-surface)' to revert to white */
    --color-fieldset-bg: #f0f2f5;
    --font-sans:        'Segoe UI', system-ui, -apple-system, sans-serif;
    --transition:       0.2s ease;
}

/* ============================================
   RESET & BASE
============================================ */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 15px;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-sans);
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ============================================
   HEADER
============================================ */
.app-header {
    background: linear-gradient(135deg, var(--color-primary), var(--color-primary-dk));
    color: #fff;
    padding: 1.5rem 2rem;
    text-align: center;
}

.app-header h1 {
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.app-header .subtitle {
    font-size: 0.9rem;
    opacity: 0.85;
    margin-top: 0.25rem;
}

/* ============================================
   MAIN LAYOUT — Two-column: inputs + results
============================================ */
.app-main {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 1.5rem;
    max-width: 1400px;
    width: 100%;
    margin: 1.5rem auto;
    padding: 0 1.5rem;
    flex: 1;
}

/* ============================================
   INPUT PANEL
============================================ */
/* Input panel — original single scrollable container */
.input-panel {
    background: var(--color-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 1.5rem;
    height: fit-content;
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
}

/* Quick-run bar — sits above scrollable fieldsets for constant access */
.input-panel-run {
    padding: 0.5rem 0;
    margin-bottom: 0.75rem;
    border-bottom: 1px solid var(--color-border);
}

/* Compact run button within the quick-run bar */
.btn-run-sticky {
    width: 100%;
    padding: 0.4rem 0.75rem;
    font-size: 0.78rem;
}

.panel-title {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 1rem;
    color: var(--color-primary);
}

/* Input Groups (fieldsets)
   --color-fieldset-bg controls the subtle fill inside each group.
   Change to 'transparent' or 'var(--color-surface)' to revert
   to a flat white appearance. */
.input-group {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 1rem;
    background: var(--color-fieldset-bg);
}

/**
 * INPUT GROUP LEGEND
 * Transformed from flat text to a 'badge' style to improve section visibility.
 * The box uses the surface color to 'break' the parent border cleanly.
 */
.input-group legend {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-primary); /* Primary color used for better section-heading pop */
    padding: 0.2rem 0.6rem;      /* Created a box shape around the text */
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 4px;          /* Slightly tighter radius for the badge look */
    margin-left: 0.5rem;         /* Inset from the left edge of the fieldset */
}

/* Personal section — 3-column compact layout for age fields */
.personal-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.5rem;
}

.personal-row .field {
    margin-bottom: 0;
}

.personal-row .field label {
    font-size: 0.75rem;
    white-space: nowrap;
}

.personal-row .field input {
    padding: 0.4rem 0.5rem;
    font-size: 0.85rem;
}

/* Individual Fields */
.field {
    margin-bottom: 0.75rem;
}

.field:last-child {
    margin-bottom: 0;
}

.field label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 0.25rem;
    color: var(--color-text);
}

.field input[type="number"],
.field input.currency-input {
    width: 100%;
    padding: 0.5rem 0.65rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: var(--font-sans);
    color: var(--color-text);
    background: var(--color-surface);
    transition: border-color var(--transition), box-shadow var(--transition);
}

.field input[type="number"]:focus,
.field input.currency-input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.field-hint {
    display: block;
    font-size: 0.75rem;
    color: var(--color-text-muted);
    margin-top: 0.2rem;
}

/* Inline validation error states */
.field.has-error input[type="number"],
.field.has-error input.currency-input {
    border-color: var(--color-danger);
}

.field.has-error input[type="number"]:focus,
.field.has-error input.currency-input:focus {
    border-color: var(--color-danger);
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15);
}

.field-error {
    display: block;
    font-size: 0.75rem;
    color: var(--color-danger);
    margin-top: 0.2rem;
}

/* ============================================
   TOOLTIP (Floating logic)
============================================ */
.tooltip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--color-text-muted);
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    cursor: help;
    vertical-align: middle;
    margin-left: 4px;
}

/* 
   This element lives at the body root, 
   allowing it to bypass sidebar clipping. 
*/
.floating-tooltip {
    position: fixed; /* Fixed to viewport coordinates */
    background: var(--color-text);
    color: #fff;
    font-size: 0.75rem;
    font-weight: 400;
    padding: 0.6rem 0.8rem;
    border-radius: var(--radius);
    width: 240px;
    line-height: 1.4;
    z-index: 9999; /* Higher than all other UI elements */
    box-shadow: var(--shadow-md);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease;
}

.floating-tooltip.visible {
    opacity: 1;
    visibility: visible;
}

/* ============================================
   SPENDING TOGGLE (Monthly / Annual)
============================================ */
.input-with-toggle {
    display: flex;
    gap: 0.5rem;
    align-items: stretch;
}

.input-with-toggle input {
    flex: 1;
}

.toggle-group {
    display: flex;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    overflow: hidden;
    flex-shrink: 0;
}

.toggle-btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.75rem;
    font-weight: 600;
    border: none;
    background: var(--color-bg);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: background var(--transition), color var(--transition);
    font-family: var(--font-sans);
}

.toggle-btn.active {
    background: var(--color-primary);
    color: #fff;
}

.toggle-btn:hover:not(.active) {
    background: var(--color-border);
}

/* ============================================
   ACTION BUTTONS
============================================ */
.action-buttons {
    display: flex;
    gap: 0.75rem;
    margin-top: 1.25rem;
}

.btn {
    flex: 1;
    padding: 0.7rem 1rem;
    font-size: 0.85rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-family: var(--font-sans);
    transition: background var(--transition), transform 0.1s ease;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background: var(--color-primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--color-primary-dk);
}

.btn-secondary {
    background: var(--color-accent);
    color: #fff;
}

.btn-secondary:hover {
    background: #059669;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ============================================
   RESULTS PANEL
============================================ */
.results-panel {
    background: var(--color-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 1.5rem;
}

/* Metrics Row */
.metrics-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.metric-card {
    background: var(--color-bg);
    border-radius: var(--radius);
    padding: 1rem;
    text-align: center;
    border: 1px solid var(--color-border);
    transition: border-color var(--transition);
}

.metric-card.success {
    border-color: var(--color-accent);
}

.metric-card.warning {
    border-color: #f59e0b;
}

.metric-card.danger {
    border-color: var(--color-danger);
}

.metric-label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-muted);
    margin-bottom: 0.35rem;
}

.metric-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--color-text);
}

/* Chart Container */
.chart-container {
    position: relative;
    width: 100%;
    min-height: 400px;
}

.chart-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 400px;
    color: var(--color-text-muted);
    font-size: 0.9rem;
    text-align: center;
    padding: 2rem;
}

.chart-placeholder p {
    max-width: 360px;
}

#projectionChart {
    display: none;
}

/* Chart Tooltip Bar — fixed-position external tooltip above each chart */
.chart-tooltip-bar {
    min-height: 1.75rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.78rem;
    color: var(--color-text);
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem 1rem;
    align-items: center;
    opacity: 0;
    transition: opacity 0.15s ease;
}
.chart-tooltip-bar.visible { opacity: 1; }
.chart-tooltip-bar .tt-title {
    font-weight: 600;
    margin-right: 0.25rem;
}
.chart-tooltip-bar .tt-item { white-space: nowrap; }
.chart-tooltip-bar .tt-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    margin-right: 4px;
    vertical-align: middle;
}

/* Chart Legend Info */
.chart-legend-info {
    margin-top: 1rem;
    padding: 1rem;
    background: var(--color-bg);
    border-radius: var(--radius);
    font-size: 0.8rem;
    color: var(--color-text-muted);
    line-height: 1.5;
}

.chart-legend-info p {
    margin-bottom: 0.35rem;
}

.chart-legend-info p:last-child {
    margin-bottom: 0;
}
/* ============================================
   CHECKBOX FIELDS
============================================ */
.field-checkbox label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
}

.field-checkbox input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--color-primary);
    cursor: pointer;
    flex-shrink: 0;
}

/* ============================================
   TAB NAVIGATION
============================================ */
.tab-nav {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--color-border);
    margin-bottom: 1.25rem;
}

.tab-btn {
    padding: 0.6rem 1.25rem;
    font-size: 0.85rem;
    font-weight: 600;
    font-family: var(--font-sans);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    color: var(--color-text-muted);
    cursor: pointer;
    transition: color var(--transition), border-color var(--transition);
}

.tab-btn:hover {
    color: var(--color-text);
}

.tab-btn.active {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
}

/* Report Actions */
.report-actions {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 1rem;
}

/* Tab Content Panels */
.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* ============================================
   CASH FLOW CHART — Dual-axis styling
============================================ */
#cashflowChart {
    display: none;
}

/* ============================================
   RMD TABLE
============================================ */
.rmd-note {
    font-size: 0.8rem;
    color: var(--color-text-muted);
    margin-bottom: 0.75rem;
    line-height: 1.5;
}

.table-scroll {
    max-height: 500px;
    overflow-y: auto;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
}

.rmd-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.rmd-table thead {
    position: sticky;
    top: 0;
    z-index: 1;
}

.rmd-table th {
    background: var(--color-primary);
    color: #fff;
    padding: 0.6rem 0.75rem;
    text-align: right;
    font-weight: 600;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.rmd-table th:first-child {
    text-align: center;
}

.rmd-table td {
    padding: 0.5rem 0.75rem;
    text-align: right;
    border-bottom: 1px solid var(--color-border);
}

.rmd-table td:first-child {
    text-align: center;
    font-weight: 600;
}

.rmd-table tbody tr:hover {
    background: rgba(37, 99, 235, 0.04);
}

.rmd-table tbody tr.rmd-highlight {
    background: rgba(16, 185, 129, 0.08);
}

/* ============================================
   TAX RATE ROW — 2-column compact layout for tax rate fields
============================================ */
.tax-rate-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.tax-rate-row .field {
    margin-bottom: 0;
}

.tax-rate-row .field label {
    font-size: 0.75rem;
    white-space: nowrap;
}

.tax-rate-row .field input {
    padding: 0.4rem 0.5rem;
    font-size: 0.85rem;
}

/* ============================================
   PENSION TOGGLE — Reuse spending toggle style
============================================ */
#pensionTypeToggle {
    margin-bottom: 0.75rem;
}
/* ============================================
   SCENARIO ACTIONS — Inline save/load inside input panel
============================================ */
.scenario-actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
    margin-bottom: 0.5rem;
}

.btn-outline {
    background: transparent;
    color: var(--color-secondary);
    border: 1px solid var(--color-border);
}

.btn-outline:hover {
    background: var(--color-bg);
    border-color: var(--color-secondary);
    color: var(--color-text);
}

/* Smaller variant for toolbar context */
.btn-sm {
    padding: 0.4rem 0.75rem;
    font-size: 0.78rem;
}
/* ============================================
   SCENARIO LOAD TOAST
============================================ */
.scenario-toast {
    position: fixed;
    top: 1rem;
    left: 50%;
    transform: translateX(-50%) translateY(-20px);
    background: #1e293b;
    color: #fff;
    padding: 0.6rem 1.25rem;
    border-radius: var(--radius);
    font-size: 0.85rem;
    font-weight: 600;
    font-family: var(--font-sans);
    box-shadow: var(--shadow-md);
    opacity: 0;
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 9999;
    pointer-events: none;
}

.scenario-toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}
/* ============================================
   RETIREMENT STATUS BANNER
============================================ */
.retirement-banner {
    background: rgba(37, 99, 235, 0.08);
    border: 1px solid rgba(37, 99, 235, 0.2);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--color-primary);
    text-align: center;
    margin-bottom: 0.75rem;
}
/* ============================================
   ACCOUNT DETAIL TOGGLE (Simple / By Account Type)
   Progressive disclosure for tax-aware balance entry.
============================================ */
.detail-toggle {
    display: flex;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 0.75rem;
}

.detail-toggle-btn {
    flex: 1;
    padding: 0.4rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    border: none;
    background: var(--color-bg);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: background var(--transition), color var(--transition);
    font-family: var(--font-sans);
    text-align: center;
}

.detail-toggle-btn.active {
    background: var(--color-primary);
    color: #fff;
}

.detail-toggle-btn:hover:not(.active) {
    background: var(--color-border);
}

/* Account type grouping within detailed view */
.account-type-group {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 0.65rem 0.75rem;
    margin-bottom: 0.6rem;
    background: var(--color-surface);
}

.account-type-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--color-primary);
    margin-bottom: 0.4rem;
}

.account-type-sub {
    font-weight: 400;
    color: var(--color-text-muted);
    font-size: 0.72rem;
}

.account-type-fields {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.account-type-fields .field {
    margin-bottom: 0;
}

.account-type-fields .field label {
    font-size: 0.75rem;
}

.account-type-fields .field input {
    padding: 0.4rem 0.5rem;
    font-size: 0.82rem;
}

/* Computed total row */
.account-total-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0.75rem;
    background: rgba(37, 99, 235, 0.06);
    border-radius: var(--radius);
    margin-top: 0.25rem;
}

.account-total-label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--color-text-muted);
}

.account-total-value {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--color-primary);
}

/* ============================================
   FOOTER
============================================ */
.app-footer {
    text-align: center;
    padding: 1rem 2rem;
    font-size: 0.78rem;
    color: var(--color-text-muted);
    border-top: 1px solid var(--color-border);
    margin-top: auto;
}

/* ============================================
   RESPONSIVE — Stack on narrow viewports
============================================ */
@media (max-width: 900px) {
    .app-main {
        grid-template-columns: 1fr;
    }

    .input-panel {
        position: static;
        max-height: none;
    }

    /* Fixed-bottom sticky Run button on mobile */
    .input-panel-run {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 100;
        background: var(--color-surface);
        padding: 0.75rem 1rem;
        box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
        border-top: 1px solid var(--color-border);
        margin-bottom: 0;
    }

    body {
        padding-bottom: 4rem;
    }

    .metrics-row {
        grid-template-columns: repeat(2, 1fr);
    }

    /* Larger touch targets */
    .field input[type="number"],
    .field input.currency-input {
        padding: 0.7rem 0.8rem;
        font-size: 1rem;
    }

    .btn {
        padding: 0.75rem 1.25rem;
        font-size: 1rem;
    }

    /* Smaller chart containers */
    .chart-container {
        min-height: 280px;
    }

    .chart-placeholder {
        min-height: 280px;
    }

    /* Collapsible fieldsets */
    .input-group legend {
        cursor: pointer;
        user-select: none;
    }

    .input-group legend::after {
        content: ' \25BE';
        font-size: 0.8em;
    }

    .input-group.collapsed legend::after {
        content: ' \25B8';
    }

    .input-group.collapsed > *:not(legend) {
        display: none;
    }
}

@media (max-width: 500px) {
    .metrics-row {
        grid-template-columns: 1fr;
    }

    .personal-row {
        grid-template-columns: 1fr;
    }

    .action-buttons {
        flex-direction: column;
    }

    .input-with-toggle {
        flex-direction: column;
    }
}