/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Light theme (default) */
:root {
    color-scheme: light;
    --bg-page: #f4f3ef;
    --bg-card: #ffffff;
    --bg-subtle: #f7f6f3;
    --bg-input: #ffffff;
    --border-color: #e4e2dc;
    --border-strong: #cfccc4;
    --text-primary: #161614;
    --text-secondary: #57564f;
    --text-muted: #85837b;
    --accent-color: #2a78d6;
    --accent-strong: #1f63b5;
    --accent-soft: rgba(42, 120, 214, 0.10);
    --accent-ring: rgba(42, 120, 214, 0.22);
    --hero-bg: #12233b;
    --hero-text: #ffffff;
    --hero-muted: rgba(255, 255, 255, 0.68);
    --series-1: #2a78d6;
    --series-1-fill: rgba(42, 120, 214, 0.12);
    --series-2: #eb6834;
    --grid-color: #ebe9e4;
    --shadow-light: 0 1px 2px rgba(22, 22, 20, 0.04), 0 2px 8px rgba(22, 22, 20, 0.04);
    --shadow-medium: 0 2px 4px rgba(22, 22, 20, 0.05), 0 8px 24px rgba(22, 22, 20, 0.08);
    --radius: 16px;
    --radius-small: 10px;
    --spacing-xs: 8px;
    --spacing-sm: 16px;
    --spacing-md: 24px;
    --spacing-lg: 32px;
    --spacing-xl: 48px;
}

/* Dark theme, following the device setting */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --bg-page: #0f0f0e;
        --bg-card: #1a1a19;
        --bg-subtle: #222220;
        --bg-input: #141413;
        --border-color: #2e2e2b;
        --border-strong: #45443f;
        --text-primary: #f5f4ef;
        --text-secondary: #c3c2b7;
        --text-muted: #8f8e86;
        --accent-color: #3987e5;
        --accent-strong: #5a9cec;
        --accent-soft: rgba(57, 135, 229, 0.14);
        --accent-ring: rgba(57, 135, 229, 0.30);
        --hero-bg: #16273f;
        --hero-text: #ffffff;
        --hero-muted: rgba(255, 255, 255, 0.66);
        --series-1: #3987e5;
        --series-1-fill: rgba(57, 135, 229, 0.16);
        --series-2: #d95926;
        --grid-color: #2a2a28;
        --shadow-light: 0 1px 2px rgba(0, 0, 0, 0.3);
        --shadow-medium: 0 8px 24px rgba(0, 0, 0, 0.4);
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg-page: #0f0f0e;
    --bg-card: #1a1a19;
    --bg-subtle: #222220;
    --bg-input: #141413;
    --border-color: #2e2e2b;
    --border-strong: #45443f;
    --text-primary: #f5f4ef;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --accent-color: #3987e5;
    --accent-strong: #5a9cec;
    --accent-soft: rgba(57, 135, 229, 0.14);
    --accent-ring: rgba(57, 135, 229, 0.30);
    --hero-bg: #16273f;
    --hero-text: #ffffff;
    --hero-muted: rgba(255, 255, 255, 0.66);
    --series-1: #3987e5;
    --series-1-fill: rgba(57, 135, 229, 0.16);
    --series-2: #d95926;
    --grid-color: #2a2a28;
    --shadow-light: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-medium: 0 8px 24px rgba(0, 0, 0, 0.4);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-page);
    color: var(--text-primary);
    line-height: 1.5;
    font-size: 16px;
    font-weight: 400;
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.container {
    max-width: 1080px;
    margin: 0 auto;
    padding: var(--spacing-md);
    min-height: 100vh;
}

/* Header */
header {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    margin: var(--spacing-md) 0 var(--spacing-lg);
}

.logo {
    flex: none;
    display: block;
    width: 48px;
    height: 48px;
    border-radius: 13px;
    box-shadow: var(--shadow-medium);
}

header h1 {
    font-size: 1.75rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.03em;
    line-height: 1.15;
}

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

.header-controls {
    margin-left: auto;
    display: flex;
    align-items: flex-end;
    gap: var(--spacing-sm);
}

.currency-picker,
.theme-picker {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.control-label,
.currency-picker label {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
}

.currency-picker select {
    appearance: none;
    -webkit-appearance: none;
    padding: 8px 36px 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
    background-color: var(--bg-card);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2385837b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9375rem;
    font-weight: 500;
    cursor: pointer;
    box-shadow: var(--shadow-light);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.currency-picker select:hover {
    border-color: var(--border-strong);
}

.currency-picker select:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 4px var(--accent-ring);
}

.theme-switch {
    display: flex;
    gap: 2px;
    padding: 3px;
    background: var(--bg-subtle);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
}

.theme-switch button {
    display: grid;
    place-items: center;
    width: 36px;
    height: 32px;
    border: none;
    border-radius: 7px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.theme-switch button:hover {
    color: var(--text-primary);
}

.theme-switch button:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--accent-ring);
}

.theme-switch button[aria-checked="true"] {
    background: var(--bg-card);
    color: var(--text-primary);
    box-shadow: var(--shadow-light);
}

/* Main calculator section */
.calculator-section {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-light);
    padding: var(--spacing-lg);
    margin-bottom: var(--spacing-md);
}

.section-title {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: var(--spacing-sm);
}

/* Input grid */
.input-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-lg);
}

.input-group {
    display: flex;
    flex-direction: column;
}

.input-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.input-wrapper input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-small);
    font-family: inherit;
    font-size: 1.0625rem;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    background: var(--bg-input);
    color: var(--text-primary);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input-wrapper input:hover {
    border-color: var(--border-strong);
}

.input-wrapper input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 4px var(--accent-ring);
}

.currency, .percent, .unit {
    position: absolute;
    font-size: 0.9375rem;
    font-weight: 500;
    color: var(--text-muted);
    pointer-events: none;
    z-index: 1;
}

.currency {
    left: 14px;
}

.percent, .unit {
    right: 14px;
}

.input-wrapper:has(.currency) input {
    padding-left: 54px;
}

.input-wrapper:has(.percent) input,
.input-wrapper:has(.unit) input {
    padding-right: 40px;
}

/* Tabs (segmented control) */
.tabs {
    display: flex;
    gap: 4px;
    margin-bottom: var(--spacing-lg);
    padding: 4px;
    background: var(--bg-subtle);
    border: 1px solid var(--border-color);
    border-radius: 12px;
}

.tab {
    flex: 1;
    padding: 10px 12px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.9375rem;
    font-weight: 500;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

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

.tab:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--accent-ring);
}

.tab.active {
    background: var(--bg-card);
    color: var(--text-primary);
    box-shadow: var(--shadow-light);
}

/* Tab content */
.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Results section */
.results-section {
    margin-bottom: var(--spacing-lg);
}

.result-card {
    background: var(--bg-subtle);
    border-radius: var(--radius);
    padding: 20px var(--spacing-md);
    border: 1px solid var(--border-color);
}

.result-card.primary {
    position: relative;
    overflow: hidden;
    background: var(--hero-bg);
    color: var(--hero-text);
    border: none;
    padding: var(--spacing-lg) var(--spacing-md);
    margin-bottom: var(--spacing-sm);
    box-shadow: var(--shadow-medium);
}

.result-card.primary::after {
    content: "";
    position: absolute;
    right: -80px;
    top: -120px;
    width: 320px;
    height: 320px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(57, 135, 229, 0.45), transparent 70%);
    pointer-events: none;
}

.result-card h3, .result-card h4 {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.result-card.primary h3 {
    color: var(--hero-muted);
}

.amount {
    position: relative;
    font-size: 2.75rem;
    font-weight: 600;
    letter-spacing: -0.035em;
    line-height: 1.15;
    font-variant-numeric: tabular-nums;
}

.amount.small {
    font-size: 1.5rem;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

.result-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: var(--spacing-sm);
}

/* Chart section */
.chart-section {
    margin-bottom: var(--spacing-lg);
    padding: var(--spacing-md);
    background: var(--bg-card);
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
}

.chart-section h3,
.breakdown-section h3 {
    font-size: 1.0625rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.01em;
    margin-bottom: var(--spacing-sm);
}

.chart-section h3 + .chart-note {
    margin-top: -8px;
    margin-bottom: var(--spacing-sm);
    font-size: 0.9375rem;
    color: var(--text-secondary);
}

.chart-note strong {
    color: var(--text-primary);
    font-weight: 600;
}

.chart-wrapper {
    position: relative;
    height: 360px;
}

/* Breakdown section */
.breakdown-section {
    margin-top: var(--spacing-lg);
}

.breakdown-table-wrapper {
    overflow: auto;
    max-height: 480px;
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
}

.breakdown-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg-card);
    font-variant-numeric: tabular-nums;
}

.breakdown-table th,
.breakdown-table td {
    padding: 12px 20px;
    text-align: right;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.breakdown-table th:first-child,
.breakdown-table td:first-child {
    text-align: left;
}

.breakdown-table th {
    position: sticky;
    top: 0;
    background: var(--bg-subtle);
    font-weight: 500;
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

.breakdown-table td {
    font-size: 0.9375rem;
    color: var(--text-primary);
}

.breakdown-table td:first-child {
    color: var(--text-secondary);
}

.breakdown-table .balance-cell {
    font-weight: 600;
}

.swatch {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 8px;
    vertical-align: 1px;
}

.swatch-contrib {
    background: var(--series-2);
}

.swatch-interest {
    background: var(--series-1);
}

.breakdown-table tbody tr:hover {
    background: var(--accent-soft);
}

.breakdown-table tbody tr:last-child td {
    border-bottom: none;
}

/* Goal and Required tabs */
.goal-inputs,
.required-inputs {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-md);
}

.goal-results .result-card,
.required-results .result-card {
    margin-bottom: 0;
}

.hint {
    margin-top: var(--spacing-sm);
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* Responsive design */
@media (max-width: 768px) {
    .container {
        padding: var(--spacing-sm);
    }

    header {
        margin: var(--spacing-sm) 0 var(--spacing-md);
    }

    header {
        flex-wrap: wrap;
    }

    header h1 {
        font-size: 1.5rem;
    }

    .header-controls {
        margin-left: 0;
        width: 100%;
    }

    .currency-picker {
        flex: 1;
    }

    .calculator-section {
        padding: 20px var(--spacing-sm);
    }

    .input-grid {
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }

    .result-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .tab {
        font-size: 0.8125rem;
        padding: 10px 6px;
    }

    .chart-section {
        padding: var(--spacing-sm);
    }

    .chart-wrapper {
        height: 280px;
    }

    .breakdown-table th,
    .breakdown-table td {
        padding: 10px 12px;
        font-size: 0.875rem;
    }

    .amount {
        font-size: 2.125rem;
    }

    .amount.small {
        font-size: 1.25rem;
    }
}

@media (max-width: 420px) {
    .input-grid {
        grid-template-columns: 1fr;
    }
}

/* About / FAQ section (readable content for visitors and search engines) */
.info-section {
    max-width: 760px;
    margin: var(--spacing-xl) auto var(--spacing-lg);
    padding: 0 var(--spacing-xs);
    color: var(--text-secondary);
}

.info-section h2 {
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: -0.015em;
    color: var(--text-primary);
    margin: var(--spacing-lg) 0 12px;
}

.info-section h2:first-child {
    margin-top: 0;
}

.info-section p {
    margin-bottom: 12px;
}

.info-section strong {
    color: var(--text-primary);
    font-weight: 600;
}

.nowrap {
    white-space: nowrap;
}

.feature-list {
    padding-left: 20px;
}

.feature-list li {
    margin-bottom: 8px;
}

.faq {
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-card);
    overflow: hidden;
}

.faq details + details {
    border-top: 1px solid var(--border-color);
}

.faq summary {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--spacing-sm);
    padding: 16px 20px;
    font-weight: 500;
    color: var(--text-primary);
    cursor: pointer;
    list-style: none;
}

.faq summary::-webkit-details-marker {
    display: none;
}

.faq summary::after {
    content: "+";
    flex: none;
    font-size: 1.25rem;
    line-height: 1;
    color: var(--text-muted);
    transition: transform 0.15s ease;
}

.faq details[open] summary::after {
    transform: rotate(45deg);
}

.faq summary:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 3px var(--accent-ring);
}

.faq details p {
    padding: 0 20px 16px;
    margin: 0;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation: none !important;
        transition: none !important;
    }
}
