:root {
    /* Palette: Slate & Blue */
    --bg-app: #0f172a;
    --bg-surface: #1e293b;
    --bg-surface-2: #334155;
    --bg-surface-3: #475569;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --primary-light: rgba(59, 130, 246, 0.15);
    --accent: #f59e0b;
    --accent-light: rgba(245, 158, 11, 0.15);
    --danger: #ef4444;
    --danger-light: rgba(239, 68, 68, 0.15);
    --success: #22c55e;
    --success-light: rgba(34, 197, 94, 0.15);
    --info: #06b6d4;
    --info-light: rgba(6, 182, 212, 0.15);
    --border: rgba(255, 255, 255, 0.08);

    /* Spacing & Radius */
    --radius-xs: 4px;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-8: 2rem;

    /* Elevation */
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.3);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.3);
    --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.4);

    /* Animation */
    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-fast: all 0.15s ease;
    --transition-slow: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    /* Sidebar */
    --sidebar-width: 340px;
}

/* Light Theme */
[data-theme="light"] {
    --bg-app: #f1f5f9;
    --bg-surface: #ffffff;
    --bg-surface-2: #e2e8f0;
    --bg-surface-3: #cbd5e1;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border: rgba(0, 0, 0, 0.08);
    --primary-light: rgba(59, 130, 246, 0.1);
    --danger-light: rgba(239, 68, 68, 0.1);
    --success-light: rgba(34, 197, 94, 0.1);
    --accent-light: rgba(245, 158, 11, 0.1);
    --info-light: rgba(6, 182, 212, 0.1);

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.08), 0 2px 4px -2px rgb(0 0 0 / 0.05);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.08), 0 4px 6px -4px rgb(0 0 0 / 0.05);
    --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.08);
}

/* ==================== RESET & BASE ==================== */
*,
*::before,
*::after {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-app);
    color: var(--text-primary);
    overflow-x: hidden;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ==================== TYPOGRAPHY ==================== */
h1, h2, h3, h4, h5, h6 {
    margin: 0;
    font-weight: 600;
    letter-spacing: -0.025em;
    line-height: 1.3;
}

h1 { font-size: 1.875rem; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.25rem; }
h4 { font-size: 1.125rem; }

p {
    margin: 0;
    color: var(--text-secondary);
    line-height: 1.6;
}

a {
    color: var(--primary);
    text-decoration: none;
    transition: var(--transition-fast);
}

a:hover {
    color: var(--primary-hover);
    text-decoration: underline;
}

/* ==================== BUTTONS ==================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background-color: var(--primary);
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 1rem;
    cursor: pointer;
    transition: var(--transition);
    width: 100%;
    line-height: 1.25;
    text-decoration: none;
    position: relative;
    overflow: hidden;
}

.btn:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn:active {
    transform: scale(0.98) translateY(0);
    box-shadow: none;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-sm {
    padding: 8px 14px;
    font-size: 0.875rem;
    width: auto;
    border-radius: var(--radius-sm);
}

.btn-lg {
    padding: 14px 28px;
    font-size: 1.1rem;
}

.btn-secondary {
    background-color: var(--bg-surface-2);
    color: var(--text-primary);
}

.btn-secondary:hover {
    background-color: var(--bg-surface-3);
}

.btn-ghost {
    background-color: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background-color: var(--bg-surface-2);
    color: var(--text-primary);
}

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

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

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

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

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

.btn-success:hover {
    background-color: #16a34a;
}

.btn-icon {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    flex-shrink: 0;
}

.btn-icon.btn-sm {
    width: 32px;
    height: 32px;
}

/* ==================== FORM ELEMENTS ==================== */
.input-group {
    margin-bottom: 16px;
}

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

.input {
    width: 100%;
    background-color: var(--bg-surface);
    border: 1.5px solid var(--bg-surface-2);
    color: var(--text-primary);
    padding: 12px 16px;
    border-radius: var(--radius-md);
    font-size: 1rem;
    transition: var(--transition);
    line-height: 1.5;
}

.input::placeholder {
    color: var(--text-muted);
}

.input:hover {
    border-color: var(--bg-surface-3);
}

.input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.input-error {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 3px var(--danger-light) !important;
}

.input-success {
    border-color: var(--success) !important;
}

select.input {
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%2394a3b8' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
    background-position: right 12px center;
    background-repeat: no-repeat;
    background-size: 16px;
    padding-right: 40px;
}

textarea.input {
    resize: vertical;
    min-height: 80px;
}

.field-error {
    color: var(--danger);
    font-size: 0.8rem;
    margin-top: 4px;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* ==================== CARDS ==================== */
.card {
    background-color: var(--bg-surface);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
    transition: var(--transition);
}

.card:hover {
    box-shadow: var(--shadow-lg);
}

.card-interactive {
    cursor: pointer;
}

.card-interactive:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
}

/* ==================== BADGES ==================== */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: 100px;
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    white-space: nowrap;
    line-height: 1.4;
}

.badge-success {
    background-color: var(--success-light);
    color: var(--success);
}

.badge-warning {
    background-color: var(--accent-light);
    color: var(--accent);
}

.badge-danger {
    background-color: var(--danger-light);
    color: var(--danger);
}

.badge-info {
    background-color: var(--info-light);
    color: var(--info);
}

.badge-neutral {
    background-color: var(--bg-surface-2);
    color: var(--text-secondary);
}

.badge-primary {
    background-color: var(--primary-light);
    color: var(--primary);
}

/* ==================== TABLES ==================== */
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.table thead {
    background-color: var(--bg-surface-2);
}

.table th {
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border);
}

.table td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}

.table tbody tr:hover {
    background-color: var(--bg-surface-2);
}

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

/* ==================== APP SHELL ==================== */
header {
    padding: 16px 20px;
    background-color: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    position: sticky;
    top: 0;
    z-index: 50;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

[data-theme="light"] header {
    background-color: rgba(255, 255, 255, 0.85);
}

main {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    padding-bottom: 100px;
}

/* ==================== NAV DROPDOWN ==================== */
.nav-dropdown {
    animation: dropdownSlide 0.15s ease-out;
}

@keyframes dropdownSlide {
    from {
        opacity: 0;
        transform: translateY(-8px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.nav-dropdown button:hover {
    background-color: var(--bg-surface-2) !important;
}

/* ==================== LOADING ==================== */
.loading-screen {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--bg-surface-2);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.spinner-sm {
    width: 20px;
    height: 20px;
    border-width: 2px;
}

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

.spin {
    animation: spin 0.8s linear infinite;
}

/* ==================== TOAST NOTIFICATIONS ==================== */
.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
    max-width: 380px;
    width: calc(100% - 32px);
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 14px 16px;
    border-radius: var(--radius-md);
    background-color: var(--bg-surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-xl);
    pointer-events: auto;
    animation: toastSlideIn 0.3s ease-out;
    font-size: 0.9rem;
    line-height: 1.4;
    backdrop-filter: blur(12px);
}

.toast-leaving {
    animation: toastSlideOut 0.2s ease-in forwards;
}

.toast-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    margin-top: 1px;
}

.toast-message {
    flex: 1;
    color: var(--text-primary);
}

.toast-close {
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px;
    line-height: 1;
}

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

.toast-success {
    border-left: 4px solid var(--success);
}

.toast-error {
    border-left: 4px solid var(--danger);
}

.toast-warning {
    border-left: 4px solid var(--accent);
}

.toast-info {
    border-left: 4px solid var(--info);
}

@keyframes toastSlideIn {
    from {
        opacity: 0;
        transform: translateX(100%) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateX(0) scale(1);
    }
}

@keyframes toastSlideOut {
    from {
        opacity: 1;
        transform: translateX(0) scale(1);
    }
    to {
        opacity: 0;
        transform: translateX(100%) scale(0.95);
    }
}

/* ==================== EMPTY STATE ==================== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
    text-align: center;
    color: var(--text-muted);
}

.empty-state-icon {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    opacity: 0.4;
}

.empty-state-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.empty-state-text {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-bottom: 20px;
    max-width: 280px;
}

/* ==================== MODAL ==================== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 9000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    animation: fadeIn 0.2s ease;
}

.modal-content {
    background-color: var(--bg-surface);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-xl);
    padding: 24px;
    max-width: 480px;
    width: 100%;
    animation: modalSlideUp 0.25s ease-out;
}

@keyframes modalSlideUp {
    from {
        opacity: 0;
        transform: translateY(16px) scale(0.97);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* ==================== STAT CARDS ==================== */
.stat-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    text-align: center;
}

.stat-card-value {
    font-size: 1.75rem;
    font-weight: 700;
    line-height: 1.2;
    margin-bottom: 4px;
}

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

/* ==================== STEP INDICATOR ==================== */
.step-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 12px 0;
}

.step-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--bg-surface-2);
    transition: var(--transition);
}

.step-dot.active {
    background-color: var(--primary);
    width: 24px;
    border-radius: 4px;
}

.step-dot.completed {
    background-color: var(--success);
}

/* ==================== USER AVATAR ==================== */
.avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    font-weight: 600;
    text-transform: uppercase;
    flex-shrink: 0;
}

.avatar-sm {
    width: 32px;
    height: 32px;
    font-size: 0.75rem;
}

.avatar-md {
    width: 40px;
    height: 40px;
    font-size: 0.875rem;
}

.avatar-lg {
    width: 56px;
    height: 56px;
    font-size: 1.1rem;
}

/* ==================== DETAILS/ACCORDION ==================== */
details.card summary {
    font-weight: 600;
    cursor: pointer;
    margin-bottom: 16px;
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

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

details.card summary::after {
    content: '';
    width: 20px;
    height: 20px;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpath d='m6 9 6 6 6-6'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: center;
    transition: transform 0.2s ease;
    flex-shrink: 0;
}

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

/* ==================== IMPERSONATION BANNER ==================== */
.impersonation-banner {
    background: linear-gradient(90deg, var(--danger), #dc2626);
    color: white;
    padding: 8px 16px;
    text-align: center;
    font-size: 0.85rem;
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
}

/* ==================== UTILITIES ==================== */
.hidden {
    display: none !important;
}

.flex-row {
    display: flex;
    gap: 12px;
}

.flex-col {
    display: flex;
    flex-direction: column;
}

.text-sm {
    font-size: 0.875rem;
}

.text-xs {
    font-size: 0.75rem;
}

.text-secondary {
    color: var(--text-secondary);
}

.text-muted {
    color: var(--text-muted);
}

.text-center {
    text-align: center;
}

.font-bold {
    font-weight: 600;
}

.truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.gap-4 { gap: 16px; }

.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mb-2 { margin-bottom: 8px; }
.mb-4 { margin-bottom: 16px; }

/* Accessibility */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Focus visible for keyboard navigation */
:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* ==================== ANIMATIONS ==================== */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

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

.fade-in {
    animation: fadeIn 0.3s ease-out;
}

.slide-up {
    animation: slideUp 0.3s ease-out;
}

/* ==================== SCROLLBAR ==================== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--bg-surface-2);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--bg-surface-3);
}

/* ==================== RESPONSIVE - MOBILE ==================== */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 100%;
    }

    /* Sidebar becomes bottom panel or full-width */
    aside {
        width: 100% !important;
        height: auto !important;
        max-height: 45vh;
        border-right: none !important;
        border-bottom: 1px solid var(--border);
    }

    /* Stack sidebar + map vertically */
    .dashboard-layout {
        flex-direction: column !important;
    }

    /* Cards get less padding on mobile */
    .card {
        padding: 16px;
    }

    main {
        padding: 16px;
        padding-bottom: 80px;
    }

    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.25rem; }
    h3 { font-size: 1.1rem; }

    .btn {
        padding: 10px 16px;
    }

    /* Tables scroll horizontally */
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .toast-container {
        left: 16px;
        right: 16px;
        max-width: none;
    }

    /* Stat grids become 2-col */
    .stat-grid {
        grid-template-columns: 1fr 1fr !important;
    }
}

@media (max-width: 480px) {
    .card {
        padding: 14px;
        border-radius: var(--radius-md);
    }

    .input {
        padding: 10px 14px;
        font-size: 16px; /* Prevent iOS zoom on focus */
    }

    .btn {
        font-size: 0.9rem;
    }

    .stat-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ==================== PRINT ==================== */
@media print {
    body {
        background: white;
        color: black;
    }

    header, nav, .btn, .toast-container, .impersonation-banner {
        display: none !important;
    }

    .card {
        box-shadow: none;
        border: 1px solid #ddd;
        break-inside: avoid;
    }

    .table th,
    .table td {
        border: 1px solid #ddd;
    }

    main {
        padding: 0;
    }
}

/* ==================== REDUCED MOTION ==================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .spinner {
        animation-duration: 2s !important;
        animation-iteration-count: infinite !important;
    }
}
