/* Dark mode (default) */
:root {
    --bg: #0f1117;
    --surface: #1a1d27;
    --border: #2a2d3a;
    --text: #e4e6eb;
    --text-muted: #8b8fa3;
    --primary: #4d8eff;
    --primary-hover: #3a7bef;
    --danger: #f04e5e;
    --danger-hover: #d93a4a;
    --success: #2ecc71;
    --warning: #f0c040;
    --warning-text: #f0c040;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --badge-danger-bg: rgba(240,78,94,0.15);
    --badge-warning-bg: rgba(240,192,64,0.15);
    --badge-success-bg: rgba(46,204,113,0.15);
    --badge-muted-bg: rgba(139,143,163,0.1);
    color-scheme: dark;
}

/* Light mode override */
html[data-theme="light"] {
    --bg: #f8f9fa;
    --surface: #ffffff;
    --border: #e9ecef;
    --text: #212529;
    --text-muted: #6c757d;
    --primary: #0d6efd;
    --primary-hover: #0b5ed7;
    --danger: #dc3545;
    --danger-hover: #bb2d3b;
    --success: #198754;
    --warning: #ffc107;
    --warning-text: #664d03;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --badge-danger-bg: #f8d7da;
    --badge-warning-bg: #fff3cd;
    --badge-success-bg: #d1e7dd;
    --badge-muted-bg: #f8f9fa;
    color-scheme: light;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

/* Login */
.login-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1rem;
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2.5rem;
    width: 100%;
    max-width: 380px;
    box-shadow: var(--shadow);
}

.login-card h1 {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    text-align: center;
}

.login-card .logo {
    text-align: center;
    margin-bottom: 0.5rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

/* Layout */
.app {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 240px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1.5rem 0;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 10;
}

.sidebar-brand {
    padding: 0 1.25rem 1.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    text-transform: uppercase;
    border-bottom: 1px solid var(--border);
}

.sidebar-brand strong {
    display: block;
    font-size: 1.1rem;
    color: var(--text);
    letter-spacing: 0;
    text-transform: none;
}

.sidebar-nav {
    list-style: none;
    padding: 0.75rem 0;
    flex: 1;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 1.25rem;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
    transition: all 0.15s;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
    color: var(--text);
    background: var(--bg);
}

.sidebar-nav a.active {
    border-right: 2px solid var(--primary);
    font-weight: 500;
}

.sidebar-nav svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.sidebar-footer {
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--border);
}

.sidebar-footer a {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.85rem;
}

.sidebar-footer a:hover {
    color: var(--danger);
}

.main {
    flex: 1;
    margin-left: 240px;
    padding: 2rem;
    max-width: 1200px;
}

.page-header {
    margin-bottom: 1.5rem;
}

.page-header h2 {
    font-size: 1.35rem;
    font-weight: 600;
}

.page-header p {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-top: 0.25rem;
}

/* Cards */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 1.5rem;
}

.card-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.card-body {
    padding: 1.25rem;
}

/* Stats */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
}

.stat-card .label {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.stat-card .value {
    font-size: 1.75rem;
    font-weight: 700;
    margin-top: 0.25rem;
}

.stat-card .value.danger { color: var(--danger); }
.stat-card .value.warning { color: var(--warning-text); }
.stat-card .value.success { color: var(--success); }

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

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

th {
    text-align: left;
    padding: 0.75rem 1rem;
    font-weight: 600;
    color: var(--text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

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

tr:hover td {
    background: var(--bg);
}

/* Badges */
.badge {
    display: inline-block;
    padding: 0.2rem 0.6rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
    white-space: nowrap;
}

.badge-danger { background: var(--badge-danger-bg); color: var(--danger); }
.badge-warning { background: var(--badge-warning-bg); color: var(--warning-text); }
.badge-success { background: var(--badge-success-bg); color: var(--success); }
.badge-muted { background: var(--badge-muted-bg); color: var(--text-muted); }

/* Forms */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 0.35rem;
    color: var(--text);
}

input[type="text"],
input[type="password"],
input[type="email"],
select {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.9rem;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(77,142,255,0.15);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    border: 1px solid transparent;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.15s;
    white-space: nowrap;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

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

.btn-danger {
    background: var(--danger);
    color: #fff;
}

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

.btn-success {
    background: var(--success);
    color: #fff;
}

.btn-success:hover { background: #1a9c5c; }

.btn-warning {
    background: #e6a817;
    color: #000;
}

.btn-warning:hover { background: #d49b15; }

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}

.btn-outline:hover {
    background: var(--bg);
}

.btn-sm {
    padding: 0.3rem 0.65rem;
    font-size: 0.8rem;
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-block {
    display: flex;
    width: 100%;
    justify-content: center;
}

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    font-size: 0.875rem;
}

.alert-danger { background: var(--badge-danger-bg); color: var(--danger); border: 1px solid var(--border); }
.alert-success { background: var(--badge-success-bg); color: var(--success); border: 1px solid var(--border); }
.alert-warning { background: var(--badge-warning-bg); color: var(--warning-text); border: 1px solid var(--border); }

/* Empty state */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
}

.empty-state svg {
    width: 48px;
    height: 48px;
    margin-bottom: 1rem;
    opacity: 0.4;
}

.empty-state p {
    font-size: 0.9rem;
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 100;
    align-items: center;
    justify-content: center;
}

.modal-overlay.active { display: flex; }

.modal {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: 0 8px 30px rgba(0,0,0,0.12);
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    margin: 1rem;
}

.modal-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: 600;
}

.modal-header button {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 1.25rem;
    line-height: 1;
}

.modal-body {
    padding: 1.25rem;
}

.modal-footer {
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}

/* Searchable Select */
.searchable-select {
    position: relative;
}

.searchable-select input[type="text"] {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.9rem;
    background: var(--surface);
    color: var(--text);
}

.searchable-select input[type="text"].has-value {
    border-color: var(--primary);
    background: rgba(77,142,255,0.08);
}

.searchable-select-dropdown {
    display: none;
    position: fixed;
    max-height: 200px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 6px 6px;
    z-index: 9999;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.searchable-select.open .searchable-select-dropdown {
    display: block;
}

.searchable-select-option {
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 0.875rem;
}

.searchable-select-option:hover,
.searchable-select-option.highlighted {
    background: var(--bg);
    color: var(--primary);
}

.searchable-select-option.hidden {
    display: none;
}

.searchable-select-empty {
    padding: 0.75rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* Collapsible Sections */
.settings-section {
    margin-bottom: 1.5rem;
}

.settings-section-header {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    user-select: none;
    box-shadow: var(--shadow);
    transition: border-radius 0.15s;
}

.settings-section.open .settings-section-header {
    border-radius: var(--radius) var(--radius) 0 0;
    border-bottom-color: transparent;
}

.settings-section-header:hover {
    background: var(--bg);
}

.settings-section-title {
    font-weight: 600;
    font-size: 0.95rem;
}

.settings-section-title small {
    font-weight: 400;
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-left: 0.5rem;
}

.settings-section-chevron {
    width: 18px;
    height: 18px;
    color: var(--text-muted);
    transition: transform 0.2s;
    flex-shrink: 0;
}

.settings-section.open .settings-section-chevron {
    transform: rotate(180deg);
}

.settings-section-content {
    display: none;
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.settings-section.open .settings-section-content {
    display: block;
}

.settings-section-content .card {
    border: none;
    border-radius: 0;
    box-shadow: none;
    margin-bottom: 0;
}

.settings-section-content .card + .card {
    border-top: 1px solid var(--border);
}

.settings-section-content .card:last-child {
    border-radius: 0 0 var(--radius) var(--radius);
}

/* Action Menu (3-dot dropdown) */
.action-group {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}

.action-menu {
    position: relative;
    display: inline-block;
}

.action-menu-trigger {
    padding: 0.3rem 0.45rem !important;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.action-menu-dropdown {
    display: none;
    position: fixed;
    min-width: 120px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 9999;
    overflow: hidden;
}

.action-menu-dropdown.open {
    display: block;
}

.action-menu-item {
    display: block;
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: none;
    background: none;
    text-align: left;
    font-size: 0.85rem;
    cursor: pointer;
    white-space: nowrap;
    color: var(--text);
}

.action-menu-item.text-danger {
    color: var(--danger);
}

.action-menu-item:hover {
    background: var(--bg);
}

/* Loading */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Mobile menu button */
.mobile-menu-btn {
    display: none;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 20;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.4rem;
    color: var(--text);
    cursor: pointer;
    line-height: 0;
}

.sidebar-overlay {
    display: none;
}

/* Responsive */
@media (max-width: 768px) {
    .mobile-menu-btn {
        display: block;
    }

    .sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.5);
        z-index: 9;
    }

    .sidebar-overlay.open {
        display: block;
    }

    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 11;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .main {
        margin-left: 0;
        padding: 1rem;
        padding-top: 3.5rem;
    }

    .stats {
        grid-template-columns: 1fr 1fr;
    }

    .stat-card {
        padding: 0.85rem;
    }

    .stat-card .value {
        font-size: 1.35rem;
    }

    .page-header {
        margin-bottom: 1rem;
    }

    .page-header h2 {
        font-size: 1.15rem;
    }

    .card-body {
        padding: 0.85rem;
    }

    .card-header {
        padding: 0.75rem 0.85rem;
        font-size: 0.85rem;
    }

    /* Tables: horizontal scroll with smaller text */
    table {
        font-size: 0.8rem;
    }

    th, td {
        padding: 0.5rem 0.6rem;
    }

    /* Buttons in tables */
    .btn-sm {
        font-size: 0.7rem;
        padding: 0.25rem 0.45rem;
    }

    /* Modal full-width on mobile */
    .modal {
        width: 95%;
        max-width: none;
        max-height: 90vh;
    }

    .modal-body {
        padding: 1rem;
    }

    /* Forms */
    .form-group label {
        font-size: 0.8rem;
    }

    .form-group input,
    .form-group select,
    .form-group textarea {
        font-size: 0.9rem;
    }

    /* Login card on mobile */
    .login-card {
        padding: 1.5rem;
    }

    /* Settings sections */
    .settings-section-header {
        padding: 0.75rem 1rem;
        font-size: 0.85rem;
    }

    .settings-section-body {
        padding: 0.85rem;
    }

    /* Flex layouts in table cells */
    td[style*="white-space:nowrap"] {
        white-space: normal !important;
    }

    /* Searchable select */
    .searchable-select-dropdown {
        max-height: 160px;
    }
}

@media (max-width: 480px) {
    .stats {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    .main {
        padding: 0.75rem;
        padding-top: 3.5rem;
    }

    table {
        font-size: 0.75rem;
    }

    th, td {
        padding: 0.4rem 0.5rem;
    }

    .page-header p {
        font-size: 0.8rem;
    }
}

/* Utility */
.text-muted { color: var(--text-muted); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-right { text-align: right; }
.mt-1 { margin-top: 0.5rem; }
.mb-1 { margin-bottom: 0.5rem; }
.flex { display: flex; }
.items-center { align-items: center; }
.gap-1 { gap: 0.5rem; }
.justify-between { justify-content: space-between; }

/* Theme toggle */
.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    padding: 0.35rem;
    color: var(--text-muted);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
}

.theme-toggle:hover {
    color: var(--text);
    background: var(--bg);
}

/* Default: show sun (switch to light), hide moon */
.theme-icon-sun { display: block; }
.theme-icon-moon { display: none; }

/* Light mode: show moon (switch to dark), hide sun */
html[data-theme="light"] .theme-icon-sun { display: none !important; }
html[data-theme="light"] .theme-icon-moon { display: block !important; }
