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

:root {
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #fafafa;
    --text-primary: #333;
    --text-secondary: #555;
    --text-heading: #2c3e50;
    --text-subheading: #34495e;
    --border-color: #ddd;
    --shadow: rgba(0, 0, 0, 0.1);
    --shadow-light: rgba(0, 0, 0, 0.05);
    --primary-color: #3498db;
    --primary-hover: #2980b9;
    --danger-color: #e74c3c;
    --danger-hover: #c0392b;
    --table-header-bg: #34495e;
    --table-header-text: #ffffff;
}

[data-theme="dark"] {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2d2d2d;
    --bg-tertiary: #363636;
    --text-primary: #e0e0e0;
    --text-secondary: #b0b0b0;
    --text-heading: #f0f0f0;
    --text-subheading: #d0d0d0;
    --border-color: #444;
    --shadow: rgba(0, 0, 0, 0.3);
    --shadow-light: rgba(0, 0, 0, 0.2);
    --primary-color: #5dade2;
    --primary-hover: #3498db;
    --danger-color: #e74c3c;
    --danger-hover: #c0392b;
    --table-header-bg: #404040;
    --table-header-text: #f0f0f0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    transition: background-color 0.3s, color 0.3s;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
}

h1 {
    text-align: center;
    color: var(--text-heading);
    font-size: 2.5em;
    margin: 0;
}

h2 {
    color: var(--text-subheading);
    margin-bottom: 20px;
    font-size: 1.8em;
}

h3 {
    color: var(--text-subheading);
    margin-bottom: 15px;
    font-size: 1.4em;
}

/* Theme Toggle */
.theme-toggle-container {
    display: flex;
    align-items: center;
    gap: 12px;
}

.theme-icon {
    font-size: 20px;
    transition: opacity 0.3s;
}

.theme-icon.sun {
    opacity: 1;
}

.theme-icon.moon {
    opacity: 0.3;
}

[data-theme="dark"] .theme-icon.sun {
    opacity: 0.3;
}

[data-theme="dark"] .theme-icon.moon {
    opacity: 1;
}

.theme-toggle {
    position: relative;
    width: 60px;
    height: 30px;
    display: inline-block;
}

.theme-toggle input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--primary-color);
    transition: 0.4s;
    border-radius: 34px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: 0.4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--table-header-bg);
}

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

.theme-toggle:hover .slider {
    box-shadow: 0 0 10px var(--shadow);
}

/* Loan Input Form */
.loan-inputs {
    background: var(--bg-secondary);
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 2px 10px var(--shadow);
    margin-bottom: 30px;
    transition: background-color 0.3s, box-shadow 0.3s;
}

#loan-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

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

.form-group label {
    margin-bottom: 5px;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 5px;
}

.form-group input {
    padding: 10px;
    border: 2px solid var(--border-color);
    border-radius: 5px;
    font-size: 16px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    transition: border-color 0.3s, background-color 0.3s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.add-loan-btn {
    grid-column: 1 / -1;
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 24px;
    font-size: 18px;
    border-radius: 5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    transition: background-color 0.3s;
}

.add-loan-btn:hover {
    background-color: var(--primary-hover);
}

.plus-icon {
    font-size: 24px;
    font-weight: bold;
}

/* Loans List */
.loans-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.loan-card {
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 2px 10px var(--shadow);
    border-left: 5px solid;
    transition: background-color 0.3s, box-shadow 0.3s;
    position: relative;
}

.close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: transparent;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-secondary);
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background-color 0.3s, color 0.3s;
}

.close-btn:hover {
    background-color: var(--danger-color);
    color: white;
}

.loan-details p {
    margin-bottom: 8px;
}

/* Comparison Table Section */
.comparison-table-section {
    background: var(--bg-secondary);
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 2px 10px var(--shadow);
    margin-bottom: 30px;
    transition: background-color 0.3s, box-shadow 0.3s;
}

/* Comparison Section */
.comparison-section {
    background: var(--bg-secondary);
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 2px 10px var(--shadow);
    transition: background-color 0.3s, box-shadow 0.3s;
}

/* Tabs */
.tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--border-color);
}

.tab-button {
    background: transparent;
    border: none;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: color 0.3s, border-color 0.3s;
    border-bottom: 3px solid transparent;
    margin-bottom: -2px;
}

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

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

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Amortization Table */
.amortization-table-wrapper {
    margin-top: 20px;
}

.amortization-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.amortization-table-wrapper label {
    font-weight: 600;
    color: var(--text-secondary);
    margin-right: 10px;
}

.download-btn {
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background-color 0.3s;
}

.download-btn:hover {
    background-color: var(--primary-hover);
}

#loan-selector {
    padding: 8px 12px;
    border: 2px solid var(--border-color);
    border-radius: 5px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 16px;
    margin-bottom: 20px;
    cursor: pointer;
}

#amortization-table {
    max-height: 500px;
    overflow-y: auto;
    display: block;
}

#amortization-table thead {
    position: sticky;
    top: 0;
    z-index: 10;
}

/* Charts */
.charts-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(350px, 100%), 1fr));
    gap: 30px;
    margin-bottom: 40px;
}

.chart-wrapper {
    background: var(--bg-tertiary);
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 1px 5px var(--shadow-light);
    transition: background-color 0.3s, box-shadow 0.3s;
    position: relative;
    width: 100%;
    min-height: 300px;
}

.chart-wrapper canvas {
    max-width: 100%;
    height: auto !important;
}

/* Comparison Table */
.comparison-table {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg-secondary);
}

thead {
    background-color: var(--table-header-bg);
    color: var(--table-header-text);
}

th, td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

tbody tr:hover {
    background-color: var(--bg-tertiary);
}

.remove-btn {
    background-color: var(--danger-color);
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s;
}

.remove-btn:hover {
    background-color: var(--danger-hover);
}

/* Extra Payment Toggle */
.extra-payment-toggle {
    padding: 10px;
    background: var(--bg-tertiary);
    border-radius: 5px;
}

/* Responsive Design - Tablet */
@media (max-width: 1024px) {
    .container {
        max-width: 100%;
        padding: 15px;
    }
    
    .loans-list {
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    }
}

/* Responsive Design - Mobile */
@media (max-width: 768px) {
    /* Header adjustments */
    .header {
        flex-direction: column;
        gap: 20px;
        text-align: center;
        margin-bottom: 20px;
    }
    
    h1 {
        font-size: 1.8em;
        line-height: 1.2;
    }
    
    h2 {
        font-size: 1.4em;
    }
    
    h3 {
        font-size: 1.2em;
    }
    
    .container {
        padding: 10px;
    }
    
    /* Form adjustments */
    .loan-inputs {
        padding: 15px;
        border-radius: 8px;
    }
    
    .comparison-section, .comparison-table-section {
        padding: 15px;
        border-radius: 8px;
    }
    
    #loan-form {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    /* Button improvements for touch */
    .add-loan-btn {
        padding: 14px 24px;
        font-size: 16px;
        min-height: 48px;
    }
    
    .tab-button {
        padding: 12px 16px;
        font-size: 14px;
        min-height: 44px;
    }
    
    .download-btn {
        padding: 12px 16px;
        font-size: 14px;
        min-height: 44px;
    }
    
    .remove-btn {
        padding: 8px 14px;
        min-height: 36px;
    }
    
    .clear-all-btn {
        padding: 10px 14px !important;
        font-size: 13px !important;
    }
    
    /* Cards and lists */
    .loans-list {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    .loan-card {
        padding: 15px;
    }
    
    /* Charts */
    .charts-container {
        grid-template-columns: 1fr;
        gap: 20px;
        padding: 0;
    }
    
    .chart-wrapper {
        padding: 15px 10px;
        min-height: 280px;
        max-width: 100%;
        overflow: hidden;
    }
    
    .chart-wrapper canvas {
        max-height: 400px;
    }
    
    /* Table responsive wrapper - ensure container allows overflow */
    .comparison-table-section {
        overflow: visible;
        padding-bottom: 20px;
    }
    
    #comparison-table table {
        min-width: 700px;
        font-size: 13px;
    }
    
    th, td {
        padding: 8px 6px;
        font-size: 13px;
    }
    
    /* Amortization controls */
    .amortization-controls {
        flex-direction: column;
        gap: 15px;
        align-items: stretch;
    }
    
    .amortization-controls > div {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    
    #loan-selector {
        width: 100%;
        padding: 10px;
        font-size: 16px;
    }
    
    #amortization-table {
        font-size: 13px;
        max-height: 400px;
    }
    
    /* Summary table responsive */
    .comparison-table-section > div:first-child {
        flex-direction: column;
        gap: 15px;
        align-items: stretch;
    }
    
    .comparison-table-section h2 {
        margin-bottom: 0;
    }
    
    /* Tabs container */
    .tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-bottom: none;
        padding-bottom: 2px;
        margin-bottom: 15px;
    }
    
    /* Form inputs touch-friendly */
    .form-group input {
        padding: 12px;
        font-size: 16px;
        min-height: 44px;
    }
    
    /* Close button positioning */
    .close-btn {
        top: 5px;
        right: 5px;
        width: 32px;
        height: 32px;
        font-size: 20px;
    }
}

/* Medium mobile devices */
@media (max-width: 480px) {
    .charts-container {
        grid-template-columns: 1fr;
    }
    
    .chart-wrapper {
        padding: 12px 8px;
        min-height: 300px;
    }
    
    .chart-wrapper canvas {
        max-height: 380px;
    }
}

/* Small mobile devices */
@media (max-width: 414px) {
    h1 {
        font-size: 1.5em;
    }
    
    h2 {
        font-size: 1.2em;
    }
    
    .container {
        padding: 8px;
    }
    
    .loan-inputs,
    .comparison-section,
    .comparison-table-section {
        padding: 12px;
    }
    
    .chart-wrapper {
        padding: 10px 5px;
        min-height: 320px;
    }
    
    .chart-wrapper canvas {
        max-height: 360px;
    }
    
    .charts-container {
        gap: 15px;
    }
    
    /* Even smaller table font */
    th, td {
        padding: 6px 4px;
        font-size: 12px;
    }
    
    /* Stack theme toggle vertically if needed */
    .theme-toggle-container {
        transform: scale(0.9);
    }
}

.clear-all-btn:hover {
    background-color: #c82333 !important;
    transform: scale(1.05);
    transition: all 0.2s ease;
}

/* Ensure tables are scrollable on mobile */
@media (max-width: 768px) {
    .comparison-table-section {
        position: relative;
    }
    
    /* Table wrapper with visible scrollbar and gradient */
    #comparison-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -10px;
        padding: 0 10px;
        position: relative;
    }
    
    /* Always show scrollbar on mobile for clarity */
    #comparison-table::-webkit-scrollbar {
        height: 8px;
        display: block;
    }
    
    #comparison-table::-webkit-scrollbar-track {
        background: var(--bg-tertiary);
        border-radius: 4px;
    }
    
    #comparison-table::-webkit-scrollbar-thumb {
        background: var(--primary-color);
        border-radius: 4px;
    }
    
    /* Gradient fade on right edge to indicate more content */
    .comparison-table-section::before {
        content: '';
        position: absolute;
        right: 0;
        top: 60px;
        bottom: 20px;
        width: 30px;
        background: linear-gradient(to right, transparent, var(--bg-secondary));
        pointer-events: none;
        z-index: 1;
        transition: opacity 0.3s;
    }
    
    /* Animated arrow indicator */
    .comparison-table-section::after {
        content: '👉 Swipe to see more';
        position: absolute;
        right: 10px;
        top: 65px;
        padding: 6px 12px;
        background: var(--primary-color);
        color: white;
        border-radius: 20px;
        font-size: 13px;
        font-weight: 600;
        box-shadow: 0 2px 8px rgba(0,0,0,0.2);
        animation: pulse-slide 3s ease-in-out infinite;
        z-index: 2;
    }
    
    @keyframes pulse-slide {
        0%, 100% { 
            transform: translateX(0);
            opacity: 0.9;
        }
        50% { 
            transform: translateX(-10px);
            opacity: 1;
        }
    }
    
    /* Hide indicators when table is scrolled */
    .comparison-table-section.scrolled::after,
    .comparison-table-section.scrolled::before {
        opacity: 0;
        pointer-events: none;
    }
    
    /* Add visible border to indicate scrollable area */
    #comparison-table table {
        border: 2px solid var(--border-color);
        border-radius: 8px;
        overflow: hidden;
    }
}

/* Improve select dropdown on mobile */
select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 20px;
    padding-right: 40px;
}

/* Term unit selector styling */
#term-unit {
    cursor: pointer;
    transition: all 0.3s;
}

#term-unit:hover {
    border-color: var(--primary-color);
}

#term-unit:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}