/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    line-height: 1.6;
    color: #333;
    background-color: #f8f9fa;
}

/* Container */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

/* Header */
.main-header {
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    margin-bottom: 0;
}

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    max-width: 1200px;
    margin: 0 auto;
}

.nav-brand a {
    color: white;
    font-size: 1.5rem;
    font-weight: 700;
    text-decoration: none;
}

.nav-menu {
    display: flex;
    list-style: none;
    gap: 2rem;
}

.nav-link {
    color: white;
    text-decoration: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    transition: background-color 0.3s ease;
}

.nav-link:hover {
    background-color: rgba(255,255,255,0.1);
}

.nav-dropdown {
    position: relative;
}

.dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    background: white;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.3s ease;
    list-style: none;
    min-width: 180px;
    z-index: 1000;
}

.nav-dropdown:hover .dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.dropdown-menu li {
    margin: 0;
}

.dropdown-menu a {
    display: block;
    padding: 0.75rem 1rem;
    color: #333;
    text-decoration: none;
    transition: background-color 0.2s ease;
}

.dropdown-menu a:hover {
    background-color: #f8f9fa;
}

.nav-user {
    color: white;
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* Login page styles */
.login-page {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-container {
    background: white;
    padding: 2rem;
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    width: 100%;
    max-width: 400px;
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;
}

.login-header h1 {
    color: #2c3e50;
    margin-bottom: 0.5rem;
}

.login-footer {
    text-align: center;
    margin-top: 1.5rem;
    color: #666;
}

/* Page headers */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding-bottom: 1rem;
    border-bottom: 3px solid #e9ecef;
}

.page-header h1 {
    color: #2c3e50;
    font-size: 2rem;
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    text-decoration: none;
    border: none;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.3s ease;
    text-align: center;
}

.btn-primary {
    background: linear-gradient(135deg, #3498db, #2980b9);
    color: white;
    box-shadow: 0 2px 4px rgba(52,152,219,0.3);
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(52,152,219,0.4);
}

.btn-outline {
    border: 2px solid #3498db;
    color: #3498db;
    background: transparent;
}

.btn-outline:hover {
    background: #3498db;
    color: white;
}

.btn-danger {
    background: #e74c3c;
    color: white;
}

.btn-danger:hover {
    background: #c0392b;
}

.btn-small {
    padding: 0.25rem 0.75rem;
    font-size: 0.8rem;
}

.btn-full {
    width: 100%;
}

/* KPI Grid */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.kpi-card {
    background: white;
    padding: 1.5rem;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: transform 0.2s ease;
}

.kpi-card:hover {
    transform: translateY(-2px);
}

.kpi-card.alert {
    border-left: 4px solid #e74c3c;
}

.kpi-icon {
    font-size: 2rem;
}

.kpi-content h3 {
    font-size: 1.8rem;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 0.25rem;
}

.kpi-content p {
    color: #7f8c8d;
    font-size: 0.9rem;
    margin: 0;
}

/* Dashboard grid */
.dashboard-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.dashboard-section {
    background: white;
    padding: 1.5rem;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.dashboard-section h2 {
    color: #2c3e50;
    margin-bottom: 1rem;
    font-size: 1.3rem;
}

/* Tables */
.table-responsive {
    overflow-x: auto;
    margin-bottom: 1rem;
}

.table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.table th,
.table td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid #e9ecef;
}

.table th {
    background: #f8f9fa;
    font-weight: 600;
    color: #2c3e50;
}

.table tbody tr:hover {
    background-color: #f8f9fa;
}

.table .text-center {
    text-align: center;
    color: #7f8c8d;
    font-style: italic;
}

/* Status badges */
.status {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
    text-transform: capitalize;
}

.status.active {
    background: #d4edda;
    color: #155724;
}

.status.pending {
    background: #fff3cd;
    color: #856404;
}

.status.completed {
    background: #cce5ff;
    color: #004085;
}

.status.defaulted {
    background: #f8d7da;
    color: #721c24;
}

/* Action buttons */
.action-buttons {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* Forms */
.form-container {
    background: white;
    padding: 2rem;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.form-grid {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.form-section {
    border: 1px solid #e9ecef;
    border-radius: 8px;
    padding: 1.5rem;
}

.form-section h3 {
    color: #2c3e50;
    margin-bottom: 1rem;
    font-size: 1.2rem;
    border-bottom: 2px solid #e9ecef;
    padding-bottom: 0.5rem;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    color: #2c3e50;
    font-weight: 500;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #e9ecef;
    border-radius: 6px;
    font-size: 1rem;
    transition: border-color 0.3s ease;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: #3498db;
}

.form-group small {
    display: block;
    margin-top: 0.25rem;
    color: #7f8c8d;
    font-size: 0.8rem;
}

.form-actions {
    display: flex;
    gap: 1rem;
    justify-content: flex-start;
    padding-top: 1rem;
    border-top: 1px solid #e9ecef;
}

/* Search */
.search-section {
    background: white;
    padding: 1.5rem;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    margin-bottom: 1.5rem;
}

.search-form {
    display: flex;
    gap: 1rem;
    align-items: center;
    flex-wrap: wrap;
}

.search-form input {
    flex: 1;
    min-width: 250px;
    padding: 0.75rem;
    border: 2px solid #e9ecef;
    border-radius: 6px;
    font-size: 1rem;
}

/* Alerts */
.alert {
    padding: 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    border-left: 4px solid;
}

.alert-success {
    background: #d4edda;
    color: #155724;
    border-left-color: #28a745;
}

.alert-error {
    background: #f8d7da;
    color: #721c24;
    border-left-color: #dc3545;
}

.alert-warning {
    background: #fff3cd;
    color: #856404;
    border-left-color: #ffc107;
}

.alert ul {
    margin: 0;
    padding-left: 1.5rem;
}

/* Pagination */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    margin-top: 2rem;
}

.pagination-info {
    color: #7f8c8d;
    font-size: 0.9rem;
}

/* Footer */
.main-footer {
    background: #2c3e50;
    color: white;
    margin-top: 3rem;
    padding: 2rem 0;
    text-align: center;
}

.footer-content p {
    margin: 0.25rem 0;
}

/* Responsive design */
@media (max-width: 768px) {
    .container {
        padding: 1rem;
    }
    
    .navbar {
        flex-direction: column;
        gap: 1rem;
    }
    
    .nav-menu {
        flex-wrap: wrap;
        justify-content: center;
    }
    
    .page-header {
        flex-direction: column;
        text-align: center;
        gap: 1rem;
    }
    
    .kpi-grid {
        grid-template-columns: 1fr;
    }
    
    .dashboard-grid {
        grid-template-columns: 1fr;
    }
    
    .action-buttons {
        flex-direction: column;
    }
    
    .form-row {
        grid-template-columns: 1fr;
    }
    
    .search-form {
        flex-direction: column;
        align-items: stretch;
    }
    
    .search-form input {
        min-width: auto;
    }
    
    .form-actions {
        flex-direction: column;
    }
}

/* Utility classes */
.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

.mb-1 { margin-bottom: 1rem; }
.mb-2 { margin-bottom: 2rem; }
.mt-1 { margin-top: 1rem; }
.mt-2 { margin-top: 2rem; }

/* Modern Minimal Theme Overrides */
:root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e5e7eb;
    --primary: #2563eb;
    --primary-600: #1d4ed8;
    --danger: #ef4444;
    --warning: #f59e0b;
    --success: #10b981;
    --radius: 10px;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.08);
    --ring: 0 0 0 3px rgba(37, 99, 235, 0.25);
}

body {
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-feature-settings: "tnum" on;
}

.container {
    max-width: 1100px;
    padding: 24px;
}

/* Header & Nav */
.main-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: none;
}

.navbar {
    padding: 14px 20px;
}

.nav-brand a {
    color: var(--text);
}

.nav-menu {
    gap: 1rem;
}

.nav-link {
    color: var(--text);
    padding: 8px 10px;
    border-radius: 8px;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.nav-link:hover {
    background: #f1f5f9;
}

.nav-user {
    color: var(--text);
    gap: 0.5rem;
}

/* Theme toggle button */
#themeToggle.btn-icon {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 999px;
    padding: 6px 10px;
    width: 40px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
#themeToggle:hover { background: #f1f5f9; }
[data-theme="dark"] #themeToggle:hover { background: rgba(148, 163, 184, 0.12); }
#themeToggle .icon-sun, #themeToggle .icon-moon { font-size: 16px; line-height: 1; }
[data-theme="light"] #themeToggle .icon-sun { display: inline; }
[data-theme="light"] #themeToggle .icon-moon { display: none; }
[data-theme="dark"] #themeToggle .icon-sun { display: none; }
[data-theme="dark"] #themeToggle .icon-moon { display: inline; }

/* Hide welcome text (declutter) */
.nav-welcome { display: none; }

.dropdown-menu {
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    border-radius: 10px;
}

.dropdown-menu a {
    color: var(--text);
}

.dropdown-menu a:hover {
    background: #f8fafc;
}

/* Page header */
.page-header {
    border-bottom: 1px solid var(--border);
    padding-bottom: 12px;
}

.page-header h1 {
    color: var(--text);
    font-size: 1.5rem;
}

.page-actions {
    display: flex;
    gap: 0.5rem;
}

/* Buttons */
.btn {
    border-radius: var(--radius);
    font-weight: 600;
    padding: 10px 14px;
}

.btn-small {
    padding: 6px 10px;
    font-size: 0.85rem;
    border-radius: 8px;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: none;
}

.btn-primary:hover {
    background: var(--primary-600);
    transform: none;
}

.btn-outline {
    border: 1px solid var(--primary);
    color: var(--primary);
}

.btn-outline:hover {
    background: var(--primary);
    color: #fff;
}

.btn-danger {
    background: var(--danger);
}

.btn-danger:hover {
    background: #dc2626;
}

/* Cards / KPI */
.kpi-card {
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
}

.kpi-card:hover {
    transform: none;
    box-shadow: var(--shadow-md);
}

.kpi-content h3 {
    color: var(--text);
}

.dashboard-section {
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    border-radius: var(--radius);
}

.dashboard-section h2 {
    color: var(--text);
}

/* Tables */
.table {
    box-shadow: none;
    border: 1px solid var(--border);
}

.table th,
.table td {
    padding: 0.875rem 1rem;
    border-bottom: 1px solid var(--border);
}

.table th {
    background: #f8fafc;
    color: var(--text);
}

.table tbody tr:hover {
    background: #f6faff;
}

/* Status badges */
.status {
    border-radius: 999px;
    background: #eff6ff;
    color: var(--primary);
}

.status.active {
    background: #ecfdf5;
    color: #065f46;
}

.status.pending {
    background: #fffbeb;
    color: #92400e;
}

.status.completed {
    background: #eef2ff;
    color: #3730a3;
}

.status.defaulted {
    background: #fef2f2;
    color: #991b1b;
}

.status.overdue {
    background: #fef3c7;
    color: #92400e;
}

.status.current {
    background: #eff6ff;
    color: #1d4ed8;
}

.status.paid_off {
    background: #ecfdf5;
    color: #065f46;
}

/* Forms */
.form-container {
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    border-radius: var(--radius);
}

.form-section {
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.form-group input,
.form-group select,
.form-group textarea {
    border: 1px solid var(--border);
    border-radius: 10px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: var(--ring);
}

.form-actions {
    border-top: 1px solid var(--border);
}

/* Search */
.search-section {
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    border-radius: var(--radius);
}

.search-form input {
    border: 1px solid var(--border);
    border-radius: 10px;
}

/* Alerts */
.alert {
    border-left: none;
    border: 1px solid var(--border);
}

.alert-success {
    background: #ecfdf5;
    color: #065f46;
    border-color: #a7f3d0;
}

.alert-error {
    background: #fef2f2;
    color: #991b1b;
    border-color: #fecaca;
}

.alert-warning {
    background: #fffbeb;
    color: #92400e;
    border-color: #fde68a;
}

/* Footer */
.main-footer {
    background: var(--surface);
    color: var(--muted);
    margin-top: 2rem;
    padding: 2rem 0;
    border-top: 1px solid var(--border);
}

.footer-content p {
    margin: 0.125rem 0;
}

/* Login */
.login-page {
    background: var(--bg);
}

.login-container {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

.login-header h1 {
    color: var(--text);
}

/* Progress (used in lenders) */
.progress-bar {
    height: 6px;
    background: #f1f5f9;
    border-radius: 999px;
    overflow: hidden;
    margin-top: 0.25rem;
}

.progress-fill {
    height: 100%;
    background: var(--primary);
    border-radius: inherit;
}

/* Utilities */
.text-muted { color: var(--muted); }

@media (max-width: 768px) {
    .navbar { padding: 12px 16px; }
    .container { padding: 16px; }
}

/* Mobile refinements */
@media (max-width: 640px) {
    .nav-user span { display: none; }
    .btn { padding: 8px 12px; font-size: 0.9rem; }
    .table th, .table td { padding: 0.6rem; font-size: 0.95rem; }
    .page-header { align-items: flex-start; }
    .page-actions { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
    .search-form { flex-direction: column; align-items: stretch; }
    .kpi-grid, .dashboard-grid { grid-template-columns: 1fr; }
}

/* Mobile navigation */
@media (max-width: 768px) {
    #navToggle { display: inline-flex; margin-right: 8px; }
    .nav-menu {
        display: none;
        position: fixed;
        top: 56px;
        left: 0; right: 0;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        padding: 12px 16px;
        flex-direction: column;
        gap: 6px;
        max-height: 70vh;
        overflow-y: auto;
        z-index: 1000;
    }
    [data-nav-open="true"] .nav-menu { display: flex; }
    /* Show dropdown items inline on mobile */
    .nav-dropdown .dropdown-menu {
        position: static;
        opacity: 1; visibility: visible; transform: none;
        box-shadow: none; border: none; background: transparent; min-width: auto;
        padding-left: 8px;
    }
    .dropdown-menu a { padding-left: 16px; }
}

/* Stacked tables on phones */
@media (max-width: 640px) {
    .table-responsive { overflow: visible; }
    .table { display: block; border: none; }
    .table thead { display: none; }
    .table tbody { display: grid; gap: 10px; }
    .table tbody tr { display: grid; grid-template-columns: 1fr; gap: 8px; padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
    .table td { display: grid; grid-template-columns: 1fr auto; align-items: center; border: none; padding: 6px 0; }
    .table td::before { content: attr(data-label); color: var(--muted); font-weight: 500; margin-right: 12px; }
    .action-buttons { gap: 8px; }
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1020;
        --surface: #0f172a;
        --text: #e5e7eb;
        --muted: #94a3b8;
        --border: #1f2937;
        --primary: #60a5fa;
        --primary-600: #3b82f6;
    }

    .nav-link:hover { background: rgba(148, 163, 184, 0.12); }
    .dropdown-menu { background: var(--surface); border-color: var(--border); }
    .table th { background: #111827; }
    .table tbody tr:hover { background: rgba(255, 255, 255, 0.04); }
    .search-form input,
    .form-group input,
    .form-group select,
    .form-group textarea { background: #0b1220; color: var(--text); }
    .main-footer { background: var(--surface); }
}

/* Manual theme override via data-theme */
[data-theme="dark"] {
    --bg: #0b1020;
    --surface: #0f172a;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #1f2937;
    --primary: #60a5fa;
    --primary-600: #3b82f6;
}
[data-theme="dark"] .nav-link:hover { background: rgba(148, 163, 184, 0.12); }
[data-theme="dark"] .dropdown-menu { background: var(--surface); border-color: var(--border); }
[data-theme="dark"] .table th { background: #111827; }
[data-theme="dark"] .table tbody tr:hover { background: rgba(255, 255, 255, 0.04); }
[data-theme="dark"] .search-form input,
[data-theme="dark"] .form-group input,
[data-theme="dark"] .form-group select,
[data-theme="dark"] .form-group textarea { background: #0b1220; color: var(--text); }
[data-theme="dark"] .main-footer { background: var(--surface); }

/* Explicit light theme overrides (so toggle beats OS preference) */
[data-theme="light"] {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e5e7eb;
    --primary: #2563eb;
    --primary-600: #1d4ed8;
}

/* Ensure light theme table headers are light even if OS prefers dark */
[data-theme="light"] .table th,
[data-theme="light"] .table thead th,
[data-theme="light"] .table thead tr { background: #f8fafc; color: var(--text); }

/* Ensure light theme inputs are light */
[data-theme="light"] .search-form input,
[data-theme="light"] .form-group input,
[data-theme="light"] .form-group select,
[data-theme="light"] .form-group textarea { background: var(--surface); color: var(--text); border-color: var(--border); }

/* Readability and overflow fixes */
html, body { overflow-x: hidden; }

/* Ensure key UI text uses accessible colors */
.page-header p,
.kpi-content p,
.table .text-center,
small,
.text-muted { color: var(--muted); }

p, td, th, label { color: var(--text); }

/* Theme surfaces - unify backgrounds with theme tokens */
.kpi-card,
.dashboard-section,
.search-section,
.form-container,
.table,
.login-container { background: var(--surface); }

/* Constrain long numbers in KPI cards */
.kpi-card { align-items: center; }
.kpi-content { min-width: 0; }
.kpi-content h3 {
    font-size: 1.4rem;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: 0.2px;
}
.kpi-icon {
    font-size: 1.25rem;
    line-height: 1;
}

/* Inputs: placeholder visibility */
.search-form input::placeholder,
.form-group input::placeholder,
.form-group textarea::placeholder { color: var(--muted); opacity: 1; }

/* Buttons in dark mode: better contrast on outlines */
.btn-outline { border-color: var(--primary); }
.btn-outline:hover { background: var(--primary); color: #fff; }
