/* Central Grommunio Design System & Semantic Color Tokens */
:root {
    /* Base typography */
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
    
    /* Brand Palette */
    --color-teal-50: #effbf9;
    --color-teal-100: #c9f3ed;
    --color-teal-500: #0c8b83;
    --color-teal-600: #09756e;
    --color-teal-700: #065b55;
    
    --color-navy-900: #0b1a29;
    --color-navy-800: #10283f;
    --color-navy-700: #1b3d5b;
    --color-navy-600: #25527a;

    /* Light Theme Tokens */
    --bg-canvas: #f4f7f8;
    --bg-surface: #ffffff;
    --bg-surface-hover: #f0f4f6;
    --bg-surface-active: #e3ebef;
    --bg-elevated: #ffffff;
    --bg-inset: #eef2f5;

    --border-subtle: #dce5e8;
    --border-strong: #bac9cf;
    --border-focus: #0c8b83;

    --text-primary: #183247;
    --text-secondary: #587080;
    --text-muted: #889fab;
    --text-inverse: #ffffff;

    --primary: var(--color-teal-500);
    --primary-hover: var(--color-teal-600);
    --primary-active: var(--color-teal-700);
    --primary-contrast: #ffffff;

    --status-unread: #0c8b83;
    --status-flagged: #d97706;
    --status-danger: #dc2626;
    --status-success: #16a34a;

    --shadow-sm: 0 1px 2px rgba(16, 40, 63, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(16, 40, 63, 0.08), 0 2px 4px -1px rgba(16, 40, 63, 0.04);
    --shadow-lg: 0 10px 15px -3px rgba(16, 40, 63, 0.1), 0 4px 6px -2px rgba(16, 40, 63, 0.05);
    --shadow-dock: 0 20px 25px -5px rgba(16, 40, 63, 0.2), 0 10px 10px -5px rgba(16, 40, 63, 0.08);

    --topbar-height: 3.5rem;
    --sidebar-width: 15rem;
    --msglist-width: 22rem;

    /* Bootstrap 5 Token Sync */
    --bs-body-bg: var(--bg-canvas);
    --bs-body-color: var(--text-primary);
    --bs-card-bg: var(--bg-surface);
    --bs-card-color: var(--text-primary);
    --bs-card-border-color: var(--border-subtle);
    --bs-border-color: var(--border-subtle);
    --bs-secondary-bg: var(--bg-surface-hover);
    --bs-tertiary-bg: var(--bg-inset);
    --bs-emphasis-color: var(--text-primary);
    --bs-secondary-color: var(--text-secondary);
}

/* Explicit Light Theme */
[data-theme="light"], [data-bs-theme="light"] {
    --bg-canvas: #f4f7f8;
    --bg-surface: #ffffff;
    --bg-surface-hover: #f0f4f6;
    --bg-surface-active: #e3ebef;
    --bg-elevated: #ffffff;
    --bg-inset: #eef2f5;

    --border-subtle: #dce5e8;
    --border-strong: #bac9cf;
    --border-focus: #0c8b83;

    --text-primary: #183247;
    --text-secondary: #587080;
    --text-muted: #889fab;
    --text-inverse: #ffffff;

    --primary: var(--color-teal-500);
    --primary-hover: var(--color-teal-600);
    --primary-active: var(--color-teal-700);
    --primary-contrast: #ffffff;

    --status-unread: #0c8b83;
    --status-flagged: #d97706;
    --status-danger: #dc2626;
    --status-success: #16a34a;

    --bs-body-bg: var(--bg-canvas);
    --bs-body-color: var(--text-primary);
    --bs-card-bg: var(--bg-surface);
    --bs-card-color: var(--text-primary);
    --bs-card-border-color: var(--border-subtle);
    --bs-border-color: var(--border-subtle);
    --bs-secondary-bg: var(--bg-surface-hover);
    --bs-tertiary-bg: var(--bg-inset);
    --bs-emphasis-color: var(--text-primary);
    --bs-secondary-color: var(--text-secondary);
}

/* Dark Theme (Media query or explicit data-theme="dark" / data-bs-theme="dark") */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]):not([data-bs-theme="light"]) {
        --bg-canvas: #12161c;
        --bg-surface: #161b22;
        --bg-surface-hover: #1c222b;
        --bg-surface-active: #222934;
        --bg-elevated: #1a2029;
        --bg-inset: #0e1217;

        --border-subtle: #262d38;
        --border-strong: #384353;
        --border-focus: #0080ff;

        --text-primary: #e6f0f5;
        --text-secondary: #9cb5c4;
        --text-muted: #627e91;
        --text-inverse: #12161c;

        --primary: #0080ff;
        --primary-hover: #006ed6;
        --primary-active: #005bb5;
        --primary-contrast: #ffffff;

        --status-unread: #0080ff;
        --status-flagged: #f59e0b;
        --status-danger: #ef4444;
        --status-success: #22c55e;

        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
        --shadow-dock: 0 20px 25px -5px rgba(0, 0, 0, 0.6);

        --bs-body-bg: var(--bg-canvas);
        --bs-body-color: var(--text-primary);
        --bs-card-bg: var(--bg-surface);
        --bs-card-color: var(--text-primary);
        --bs-card-border-color: var(--border-subtle);
        --bs-border-color: var(--border-subtle);
        --bs-secondary-bg: var(--bg-surface-hover);
        --bs-tertiary-bg: var(--bg-inset);
        --bs-emphasis-color: var(--text-primary);
        --bs-secondary-color: var(--text-secondary);

        --bs-success-bg-subtle: rgba(34, 197, 94, 0.15);
        --bs-secondary-bg-subtle: var(--bg-surface-hover);
        --bs-light-bg-subtle: var(--bg-surface);
        color-scheme: dark;
    }

    :root:not([data-theme="light"]):not([data-bs-theme="light"]) .alert-success {
        background-color: rgba(34, 197, 94, 0.15) !important;
        border-color: rgba(34, 197, 94, 0.3) !important;
        color: #86efac !important;
    }

    :root:not([data-theme="light"]):not([data-bs-theme="light"]) .alert-secondary {
        background-color: var(--bg-surface-hover) !important;
        border-color: var(--border-subtle) !important;
        color: var(--text-secondary) !important;
    }

    :root:not([data-theme="light"]):not([data-bs-theme="light"]) .bg-success-subtle {
        background-color: rgba(34, 197, 94, 0.15) !important;
        border-color: rgba(34, 197, 94, 0.3) !important;
        color: #86efac !important;
    }

    :root:not([data-theme="light"]):not([data-bs-theme="light"]) .bg-secondary-subtle {
        background-color: var(--bg-surface-hover) !important;
        border-color: var(--border-subtle) !important;
        color: var(--text-secondary) !important;
    }

    :root:not([data-theme="light"]):not([data-bs-theme="light"]) .bg-light-subtle {
        background-color: var(--bg-surface) !important;
        border-color: var(--border-subtle) !important;
        color: var(--text-primary) !important;
    }
}

[data-theme="dark"], [data-bs-theme="dark"] {
    --bg-canvas: #12161c;
    --bg-surface: #161b22;
    --bg-surface-hover: #1c222b;
    --bg-surface-active: #222934;
    --bg-elevated: #1a2029;
    --bg-inset: #0e1217;

    --border-subtle: #262d38;
    --border-strong: #384353;
    --border-focus: #0080ff;

    --text-primary: #e6f0f5;
    --text-secondary: #9cb5c4;
    --text-muted: #627e91;
    --text-inverse: #12161c;

    --primary: #0080ff;
    --primary-hover: #006ed6;
    --primary-active: #005bb5;
    --primary-contrast: #ffffff;

    --status-unread: #0080ff;
    --status-flagged: #f59e0b;
    --status-danger: #ef4444;
    --status-success: #22c55e;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    --shadow-dock: 0 20px 25px -5px rgba(0, 0, 0, 0.6);

    --bs-body-bg: var(--bg-canvas);
    --bs-body-color: var(--text-primary);
    --bs-card-bg: var(--bg-surface);
    --bs-card-color: var(--text-primary);
    --bs-card-border-color: var(--border-subtle);
    --bs-border-color: var(--border-subtle);
    --bs-secondary-bg: var(--bg-surface-hover);
    --bs-tertiary-bg: var(--bg-inset);
    --bs-emphasis-color: var(--text-primary);
    --bs-secondary-color: var(--text-secondary);
}

/* Global resets & typography */
*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: var(--font-family);
    background: var(--bg-canvas);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

a {
    color: var(--primary);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* Reconnection and Error modals */
#blazor-error-ui {
    background: #fff4c6;
    color: #1a2a35;
    bottom: 0;
    box-shadow: var(--shadow-md);
    display: none;
    left: 0;
    padding: 0.75rem 1.25rem;
    position: fixed;
    width: 100%;
    z-index: 2000;
}

#blazor-error-ui .dismiss {
    cursor: pointer;
    position: absolute;
    right: 1rem;
    top: 0.5rem;
}

.environment-badge {
    display: inline-flex;
    gap: 0.45rem;
    align-items: center;
    padding: 0.25rem 0.6rem;
    color: var(--text-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--bg-surface);
}

.environment-badge span {
    width: 0.45rem;
    height: 0.45rem;
    border-radius: 50%;
    background: var(--primary);
}

.sign-in-panel {
    max-width: 30rem;
    margin: 10vh auto;
    padding: 2.5rem;
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: 1rem;
    box-shadow: var(--shadow-lg);
}

.sign-in-form {
    margin-top: 1.5rem;
}

.sign-in-form .form-control {
    min-height: 2.75rem;
    border-radius: 0.5rem;
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1px solid var(--border-subtle);
}

.sign-in-form .form-control:focus {
    border-color: var(--border-focus);
    outline: none;
    box-shadow: 0 0 0 3px rgba(12, 139, 131, 0.15);
}

.eyebrow {
    color: var(--primary);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.btn-primary {
    background-color: var(--primary);
    border-color: var(--primary);
    color: var(--primary-contrast);
    font-weight: 600;
}

.btn-primary:hover, .btn-primary:focus {
    background-color: var(--primary-hover);
    border-color: var(--primary-hover);
    color: var(--primary-contrast);
}

/* Dark Theme Overrides for Bootstrap Components & Helpers */
[data-theme="dark"], [data-bs-theme="dark"] {
    color-scheme: dark;
}

[data-theme="light"], [data-bs-theme="light"] {
    color-scheme: light;
}

[data-theme="dark"] .bg-white,
[data-bs-theme="dark"] .bg-white {
    background-color: var(--bg-surface) !important;
    color: var(--text-primary) !important;
}

[data-theme="dark"] .bg-light,
[data-bs-theme="dark"] .bg-light {
    background-color: var(--bg-surface-hover) !important;
    color: var(--text-primary) !important;
}

[data-theme="dark"] .card-header.bg-white,
[data-bs-theme="dark"] .card-header.bg-white {
    background-color: var(--bg-surface) !important;
    border-color: var(--border-subtle) !important;
    color: var(--text-primary) !important;
}

[data-theme="dark"] .card-footer.bg-light,
[data-bs-theme="dark"] .card-footer.bg-light {
    background-color: var(--bg-surface-hover) !important;
    border-color: var(--border-subtle) !important;
    color: var(--text-secondary) !important;
}

[data-theme="dark"] .card,
[data-bs-theme="dark"] .card {
    background-color: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-subtle);
}

[data-theme="dark"] .table,
[data-bs-theme="dark"] .table {
    --bs-table-bg: var(--bg-surface);
    --bs-table-color: var(--text-primary);
    --bs-table-border-color: var(--border-subtle);
    color: var(--text-primary);
}

[data-theme="dark"] .table > :not(caption) > * > *,
[data-bs-theme="dark"] .table > :not(caption) > * > * {
    background-color: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-subtle);
}

[data-theme="dark"] .form-control,
[data-theme="dark"] .form-select,
[data-bs-theme="dark"] .form-control,
[data-bs-theme="dark"] .form-select {
    background-color: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-subtle);
}

[data-theme="dark"] .form-control:focus,
[data-theme="dark"] .form-select:focus,
[data-bs-theme="dark"] .form-control:focus,
[data-bs-theme="dark"] .form-select:focus {
    background-color: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-focus);
    box-shadow: 0 0 0 0.25rem rgba(20, 184, 166, 0.25);
}

[data-theme="dark"] .input-group-text,
[data-bs-theme="dark"] .input-group-text {
    background-color: var(--bg-surface-hover);
    color: var(--text-secondary);
    border-color: var(--border-subtle);
}

[data-theme="dark"] .breadcrumb-item,
[data-bs-theme="dark"] .breadcrumb-item {
    color: var(--text-muted);
}

[data-theme="dark"] .breadcrumb-item.active,
[data-bs-theme="dark"] .breadcrumb-item.active {
    color: var(--text-secondary);
}

[data-theme="dark"] .text-muted,
[data-bs-theme="dark"] .text-muted {
    color: var(--text-muted) !important;
}

[data-theme="dark"] .text-secondary,
[data-bs-theme="dark"] .text-secondary {
    color: var(--text-secondary) !important;
}

[data-theme="dark"] .text-dark,
[data-bs-theme="dark"] .text-dark {
    color: var(--text-primary) !important;
}

[data-theme="dark"] .alert-warning,
[data-bs-theme="dark"] .alert-warning {
    background-color: rgba(245, 158, 11, 0.15) !important;
    border-color: rgba(245, 158, 11, 0.3) !important;
    color: #fbbf24 !important;
}

[data-theme="dark"] .alert-danger,
[data-bs-theme="dark"] .alert-danger {
    background-color: rgba(239, 68, 68, 0.15) !important;
    border-color: rgba(239, 68, 68, 0.3) !important;
    color: #f87171 !important;
}

[data-theme="dark"] .alert-info,
[data-bs-theme="dark"] .alert-info {
    background-color: rgba(20, 184, 166, 0.15) !important;
    border-color: rgba(20, 184, 166, 0.3) !important;
    color: #5eead4 !important;
}

[data-theme="dark"] .alert-success,
[data-bs-theme="dark"] .alert-success {
    background-color: rgba(34, 197, 94, 0.15) !important;
    border-color: rgba(34, 197, 94, 0.3) !important;
    color: #86efac !important;
}

[data-theme="dark"] .alert-secondary,
[data-bs-theme="dark"] .alert-secondary {
    background-color: var(--bg-surface-hover) !important;
    border-color: var(--border-subtle) !important;
    color: var(--text-secondary) !important;
}

[data-theme="dark"] .alert-primary,
[data-bs-theme="dark"] .alert-primary {
    background-color: rgba(20, 184, 166, 0.15) !important;
    border-color: rgba(20, 184, 166, 0.3) !important;
    color: #5eead4 !important;
}

[data-theme="dark"] .alert-light,
[data-bs-theme="dark"] .alert-light {
    background-color: var(--bg-surface-hover) !important;
    border-color: var(--border-subtle) !important;
    color: var(--text-primary) !important;
}

[data-theme="dark"] .bg-success-subtle,
[data-bs-theme="dark"] .bg-success-subtle {
    background-color: rgba(34, 197, 94, 0.15) !important;
    border-color: rgba(34, 197, 94, 0.3) !important;
    color: #86efac !important;
}

[data-theme="dark"] .bg-secondary-subtle,
[data-bs-theme="dark"] .bg-secondary-subtle {
    background-color: var(--bg-surface-hover) !important;
    border-color: var(--border-subtle) !important;
    color: var(--text-secondary) !important;
}

[data-theme="dark"] .bg-light-subtle,
[data-bs-theme="dark"] .bg-light-subtle {
    background-color: var(--bg-surface) !important;
    border-color: var(--border-subtle) !important;
    color: var(--text-primary) !important;
}

[data-theme="dark"] .table-light,
[data-bs-theme="dark"] .table-light {
    --bs-table-bg: var(--bg-surface-hover);
    --bs-table-color: var(--text-primary);
    --bs-table-border-color: var(--border-subtle);
    color: var(--text-primary) !important;
}

[data-theme="dark"] .list-group-item,
[data-bs-theme="dark"] .list-group-item {
    background-color: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-subtle);
}

[data-theme="dark"] .list-group-item-action:hover,
[data-theme="dark"] .list-group-item-action:focus,
[data-bs-theme="dark"] .list-group-item-action:hover,
[data-bs-theme="dark"] .list-group-item-action:focus {
    background-color: var(--bg-surface-hover);
    color: var(--text-primary);
}

[data-theme="dark"] .badge.bg-dark,
[data-bs-theme="dark"] .badge.bg-dark {
    background-color: var(--bg-surface-active) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border-subtle);
}

[data-theme="dark"] .badge.bg-light,
[data-bs-theme="dark"] .badge.bg-light {
    background-color: var(--bg-surface-hover) !important;
    color: var(--text-primary) !important;
}

[data-theme="dark"] .dropdown-menu,
[data-bs-theme="dark"] .dropdown-menu {
    background-color: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-subtle);
}

[data-theme="dark"] .dropdown-item,
[data-bs-theme="dark"] .dropdown-item {
    color: var(--text-secondary);
}

[data-theme="dark"] .dropdown-item:hover,
[data-theme="dark"] .dropdown-item:focus,
[data-bs-theme="dark"] .dropdown-item:hover,
[data-bs-theme="dark"] .dropdown-item:focus {
    background-color: var(--bg-surface-hover);
    color: var(--text-primary);
}

.help-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: 0.5rem;
    color: var(--text-primary);
}

.help-card summary {
    padding: 0.75rem 1rem;
    font-weight: 600;
    cursor: pointer;
    color: var(--text-primary);
}

.help-card summary:hover {
    background: var(--bg-surface-hover);
}

.help-card div {
    background: var(--bg-canvas);
    color: var(--text-secondary);
    border-top: 1px solid var(--border-subtle);
}
