/* Admin theme: dark, restrained, "expensive" - near-black surfaces, a single muted gold
   accent used sparingly, soft elevation instead of hard borders. Everything here is scoped
   under .theme-material so it can't leak into the public/member theme (.theme-noir). Retains
   the historical .md-* class prefix from this theme's earlier light/Material-inspired look,
   even though the visual language has since moved on - renaming would mean touching every
   admin page instead of just this file and AdminLayout.razor. */

.theme-material {
    --bg: #0c0c0e;
    --sidebar-bg: #101012;
    --surface: #18181b;
    --surface-alt: #1e1e22;
    --surface-hover: rgba(255, 255, 255, 0.045);
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.16);
    --text: #f2efe9;
    --text-secondary: #9c988e;
    --accent: #cea15b;
    --accent-strong: #e3ba7a;
    --accent-text: #221a0c;
    --accent-tint: rgba(206, 161, 91, 0.14);
    --accent-glow: rgba(206, 161, 91, 0.28);
    --danger: #e8695f;
    --danger-tint: rgba(232, 105, 95, 0.14);
    --success: #6fbf8b;
    --success-tint: rgba(111, 191, 139, 0.14);
    --font-display: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

    height: 100vh;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    display: flex;
}

/* Light-mode override - toggled from Settings (ThemeService), persisted in local storage. Every
   surface in this theme already routes through the custom properties above, so remapping just
   these is the whole light theme - no selector-by-selector rework needed. */
.theme-material.light-mode {
    --bg: #ffffff;
    --sidebar-bg: #f7f6f3;
    --surface: #f2f0eb;
    --surface-alt: #ece9e2;
    --surface-hover: rgba(0, 0, 0, 0.04);
    --border: rgba(0, 0, 0, 0.09);
    --border-strong: rgba(0, 0, 0, 0.18);
    --text: #201c14;
    --text-secondary: #6b6558;
}

.theme-material h1, .theme-material h2, .theme-material h3, .theme-material h4, .theme-material h5 {
    font-family: var(--font-display);
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--text);
    margin-top: 0;
}

.theme-material a {
    color: var(--accent-strong);
}

/* --- Shell layout: sidebar + main --- */

.md-shell {
    display: flex;
    width: 100%;
    height: 100%;
}

.md-sidebar {
    width: 264px;
    flex-shrink: 0;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.md-brand {
    display: flex;
    align-items: baseline;
    gap: 0.55rem;
    padding: 1.15rem 1.25rem 1rem;
    font-family: var(--font-display);
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -0.01em;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.5rem;
}

.md-brand .md-glow-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 8px var(--accent-glow), 0 0 18px var(--accent-glow);
    flex-shrink: 0;
}

.md-sidebar-logo {
    width: 22px;
    height: 22px;
    object-fit: contain;
    border-radius: 5px;
    flex-shrink: 0;
}

.md-brand small {
    font-size: 0.68rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.md-nav-links {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    padding: 0 0.6rem;
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.md-nav-links a, .md-nav-links button.md-link {
    display: block;
    text-align: left;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: 0.875rem;
    font-weight: 500;
    padding: 0.6rem 0.8rem;
    border-radius: 9px;
    transition: background-color 0.12s ease, color 0.12s ease;
}

.md-nav-links a:hover, .md-nav-links button.md-link:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.md-nav-links a.active {
    background: var(--accent-tint);
    color: var(--accent-strong);
    font-weight: 600;
}

.md-nav-links a i {
    width: 1.1rem;
    text-align: center;
    flex-shrink: 0;
    margin-right: 0.3rem;
}

.md-sidebar-footer {
    flex-shrink: 0;
    padding: 0.75rem 0.6rem;
    border-top: 1px solid var(--border);
}

.md-sidebar-footer .md-user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.4rem 0.8rem 0.5rem;
    font-size: 0.82rem;
    color: var(--text);
    overflow: hidden;
}

.md-sidebar-footer .md-user span.name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.md-role-badge {
    flex-shrink: 0;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--accent-strong);
    background: var(--accent-tint);
    padding: 0.15rem 0.5rem;
    border-radius: 20px;
}

.md-sidebar-footer a, .md-sidebar-footer button.md-link {
    display: block;
    width: 100%;
    text-align: left;
    padding: 0.5rem 0.8rem;
    border-radius: 9px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: 0.82rem;
    cursor: pointer;
    transition: background-color 0.12s ease, color 0.12s ease;
}

.md-sidebar-footer a:hover, .md-sidebar-footer a.active, .md-sidebar-footer button.md-link:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.md-sidebar-footer a i, .md-sidebar-footer button.md-link i {
    width: 1.1rem;
    text-align: center;
    flex-shrink: 0;
    margin-right: 0.3rem;
}

.md-copyright {
    margin: 0.5rem 0 0;
    padding: 0 0.8rem;
    font-size: 0.68rem;
    color: var(--text-secondary);
    opacity: 0.7;
}

/* Mobile: off-canvas drawer (hidden on desktop, see media query below) */
.md-sidebar-toggle {
    display: none;
}

.md-backdrop {
    display: none;
}

.md-topbar {
    display: none;
}

.md-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
    overflow: hidden;
}

.md-content {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 2.5rem 3rem;
    max-width: 1040px;
    width: 100%;
}

/* --- Components --- */

.theme-material .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.22);
}

.theme-material .btn {
    font-family: var(--font-body);
    font-size: 0.85rem;
    font-weight: 600;
    background: var(--surface-alt);
    border: 1px solid var(--border-strong);
    color: var(--text);
    padding: 0.55rem 1.15rem;
    border-radius: 9px;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.theme-material .btn:hover:not(:disabled) {
    background: var(--surface-hover);
    border-color: var(--border-strong);
}

.theme-material .btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.theme-material .btn-primary {
    background: linear-gradient(180deg, var(--accent-strong), var(--accent));
    color: var(--accent-text);
    border-color: var(--accent);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px var(--accent-glow);
}

.theme-material .btn-primary:hover:not(:disabled) {
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 22px var(--accent-glow);
    filter: brightness(1.04);
}

.theme-material .btn-danger {
    background: transparent;
    border-color: rgba(232, 105, 95, 0.4);
    color: var(--danger);
}

.theme-material .btn-danger:hover:not(:disabled) {
    background: var(--danger-tint);
    border-color: var(--danger);
}

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

.theme-material .btn-ghost:hover:not(:disabled) {
    background: var(--surface-hover);
    color: var(--text);
}

.theme-material .btn-sm {
    padding: 0.32rem 0.8rem;
    font-size: 0.78rem;
}

/* --- Confirm dialog (Shared/ConfirmDialog.razor) --- */

.theme-material .md-confirm-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 1rem;
}

.theme-material .md-confirm-dialog {
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: 12px;
    padding: 1.5rem;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.theme-material .md-confirm-dialog p {
    margin: 0 0 1.25rem;
    color: var(--text);
    font-size: 0.92rem;
    line-height: 1.5;
}

.theme-material .md-confirm-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}

/* --- Toast (Shared/Toast.razor) --- */

.theme-material .md-toast {
    position: fixed;
    top: 1.25rem;
    right: 1.25rem;
    z-index: 150;
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.75rem 1.1rem;
    border-radius: 10px;
    font-size: 0.88rem;
    font-weight: 500;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    animation: md-toast-in 0.15s ease;
}

.theme-material .md-toast-success {
    background: var(--success-tint);
    border: 1px solid var(--success);
    color: var(--success);
}

.theme-material .md-toast-error {
    background: var(--danger-tint);
    border: 1px solid var(--danger);
    color: var(--danger);
}

@keyframes md-toast-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

.theme-material .field {
    margin-bottom: 1.15rem;
}

.theme-material label {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.4rem;
}

.theme-material .input,
.theme-material textarea.input {
    width: 100%;
    background: var(--surface-alt);
    border: 1px solid var(--border-strong);
    color: var(--text);
    padding: 0.62rem 0.8rem;
    font-family: var(--font-body);
    font-size: 0.9rem;
    border-radius: 8px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.theme-material .input::placeholder {
    color: var(--text-secondary);
    opacity: 0.7;
}

.theme-material .input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-tint);
}

.theme-material .alert {
    border-left: 3px solid var(--danger);
    background: var(--danger-tint);
    color: #ffb3ac;
    padding: 0.75rem 1rem;
    margin-bottom: 1.25rem;
    border-radius: 8px;
    font-size: 0.88rem;
}

.theme-material .muted {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.theme-material table {
    width: 100%;
    border-collapse: collapse;
}

.theme-material th {
    text-align: left;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
    font-weight: 600;
    padding: 0.6rem 0.85rem;
    border-bottom: 1px solid var(--border-strong);
    white-space: nowrap;
}

.theme-material td {
    padding: 0.75rem 0.85rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.88rem;
}

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

.theme-material tr:hover td {
    background: var(--surface-hover);
}

.theme-material .badge {
    display: inline-block;
    font-size: 0.68rem;
    font-weight: 600;
    padding: 0.22rem 0.65rem;
    border-radius: 20px;
    margin-left: 0.5rem;
    vertical-align: middle;
}

.theme-material .badge-success {
    background: var(--success-tint);
    color: var(--success);
}

.theme-material .badge-muted {
    background: var(--surface-alt);
    color: var(--text-secondary);
}

.theme-material .badge-primary {
    background: var(--accent-tint);
    color: var(--accent-strong);
}

/* Model capability tags (Pages/Admin/Models.razor) - a fixed color per capability, distinct from
   the accent-based badge-primary above, so each reads apart from the others at a glance. */
.theme-material .badge-tools {
    background: rgba(100, 181, 246, 0.18);
    color: #64b5f6;
}

.theme-material .badge-vision {
    background: rgba(129, 199, 132, 0.18);
    color: #81c784;
}

.theme-material .badge-reasoning {
    background: rgba(206, 147, 216, 0.18);
    color: #ce93d8;
}

.theme-material .badge-transcription {
    background: rgba(255, 183, 77, 0.18);
    color: #ffb74d;
}

.theme-material .badge-speech {
    background: rgba(77, 208, 225, 0.18);
    color: #4dd0e1;
}

.theme-material .badge-image-gen {
    background: rgba(240, 98, 146, 0.18);
    color: #f06292;
}

.theme-material .badge-video {
    background: rgba(255, 138, 101, 0.18);
    color: #ff8a65;
}

.theme-material .list-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.theme-material .list-group a {
    display: block;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1rem 1.25rem;
    color: var(--text);
    font-weight: 500;
    transition: box-shadow 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}

.theme-material .list-group a:hover {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
    border-color: var(--accent);
    transform: translateY(-1px);
}

/* --- Toggle switch (models catalog enable/disable) --- */

.md-switch {
    position: relative;
    display: inline-block;
    width: 38px;
    height: 22px;
    flex-shrink: 0;
    cursor: pointer;
}

.md-switch input {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    cursor: pointer;
}

.md-switch .track {
    position: absolute;
    inset: 0;
    background: var(--surface-alt);
    border: 1px solid var(--border-strong);
    border-radius: 20px;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}

.md-switch .track::before {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--text-secondary);
    transition: transform 0.15s ease, background-color 0.15s ease;
}

.md-switch input:checked ~ .track {
    background: var(--accent-tint);
    border-color: var(--accent);
}

.md-switch input:checked ~ .track::before {
    transform: translateX(16px);
    background: var(--accent-strong);
}

/* --- Slim usage bar (models catalog: used vs. known monthly limit) --- */

.md-usage-bar {
    width: 100%;
    height: 5px;
    background: var(--surface-alt);
    border-radius: 3px;
    overflow: hidden;
    margin-top: 0.35rem;
}

.md-usage-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), var(--accent-strong));
    border-radius: 3px;
}

.md-usage-bar-fill.over-limit {
    background: var(--danger);
}

/* --- Models page: collapsible provider groups + per-model rate-limit detail --- */

.md-provider-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    width: 100%;
    margin: 0 0 0.5rem;
    flex-wrap: wrap;
}

.md-provider-toggle {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex: 1;
    min-width: 0;
    background: none;
    border: none;
    padding: 0;
    color: var(--text);
    font-family: var(--font-body);
    font-size: 1rem;
    text-align: left;
    cursor: pointer;
}

.md-provider-actions {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.md-caret {
    display: inline-block;
    width: 1em;
    color: var(--accent-strong);
    flex-shrink: 0;
}

.md-model-name-btn {
    background: none;
    border: none;
    padding: 0;
    color: var(--text);
    font-family: var(--font-body);
    font-size: inherit;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
}

.md-model-name-btn:hover {
    color: var(--accent-strong);
}

.md-model-detail {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 1rem;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1rem;
    margin: 0.25rem 0;
}

.md-model-detail .field {
    margin-bottom: 0;
    min-width: 0;
}

/* The model table's cells are nowrap; the expanded detail panel sits inside one, so it must opt back
   into wrapping or its help text runs over the next column (seen at phone width). */
.md-model-detail {
    white-space: normal;
    overflow-wrap: anywhere;
}

@media (max-width: 520px) {
    .md-model-detail {
        grid-template-columns: 1fr;
    }
}

/* --- Dashboard quick-access tiles --- */

.md-tile-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 1rem;
    margin-top: 1.5rem;
}

.md-tile {
    display: flex;
    align-items: flex-start;
    gap: 0.9rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.25rem;
    color: var(--text);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.18);
    transition: border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

.md-tile:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 14px 34px rgba(0, 0, 0, 0.3);
}

.md-tile-icon {
    flex-shrink: 0;
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    background: var(--accent-tint);
    color: var(--accent-strong);
    font-size: 1.05rem;
}

.md-tile-body h3 {
    margin: 0 0 0.25rem;
    font-size: 0.95rem;
}

.md-tile-body p {
    margin: 0;
    font-size: 0.8rem;
    line-height: 1.45;
}

/* --- Chat History (read-only admin viewer) --- */

.theme-material .chat-session-picker {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.theme-material .chat-transcript {
    max-height: 480px;
    overflow-y: auto;
}

.theme-material .chat-transcript .msg-row {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    margin-bottom: 0.85rem;
    max-width: 85%;
}

.theme-material .chat-transcript .msg-row.user {
    flex-direction: row-reverse;
    margin-left: auto;
}

.theme-material .chat-transcript .msg-row.assistant {
    margin-right: auto;
}

.theme-material .chat-transcript .msg {
    padding: 0.6rem 0.85rem;
    border-radius: 10px;
    max-width: 100%;
    font-size: 0.9rem;
    line-height: 1.4;
    background: var(--surface-alt);
    border: 1px solid var(--border);
}

.theme-material .chat-transcript .msg.user {
    border-left: 3px solid var(--text-secondary);
}

.theme-material .chat-transcript .msg.assistant {
    border-left: 3px solid var(--accent);
}

.theme-material .chat-transcript .msg .who {
    display: block;
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

/* --- Responsive: tablet --- */
@media (max-width: 1000px) {
    .md-content {
        max-width: 100%;
        padding: 2rem 1.75rem;
    }
}

/* --- Responsive: mobile (sidebar becomes an off-canvas drawer) --- */
@media (max-width: 860px) {
    .md-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 50;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.6);
    }

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

    .md-backdrop.open {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.6);
        z-index: 40;
    }

    .md-topbar {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.85rem 1rem;
        border-bottom: 1px solid var(--border);
        position: sticky;
        top: 0;
        background: var(--bg);
        z-index: 30;
        flex-shrink: 0;
    }

    .md-sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border-radius: 8px;
        background: var(--surface-alt);
        border: 1px solid var(--border-strong);
        color: var(--text);
        cursor: pointer;
        font-size: 1.05rem;
        flex-shrink: 0;
    }

    .md-topbar-brand {
        font-family: var(--font-display);
        font-weight: 700;
        font-size: 0.95rem;
    }

    .md-content {
        padding: 1.25rem 1rem;
    }

    .theme-material h1 {
        font-size: 1.35rem;
    }

    .theme-material .card {
        padding: 1.1rem;
    }

    .md-tile-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    .md-content {
        padding: 1rem 0.85rem;
    }

    .theme-material .card {
        padding: 1rem;
    }

    .theme-material .btn {
        padding: 0.5rem 0.9rem;
    }
}

/* --- Readability pass: native controls + light-mode contrast --- */

/* Native <select> popups follow color-scheme; without it the option list is white-on-white in dark. */
.theme-material { color-scheme: dark; }
.theme-material.light-mode { color-scheme: light; }
.theme-material select option, .theme-material select optgroup { background-color: #1e1e22; color: #f2efe9; }
.theme-material.light-mode select option, .theme-material.light-mode select optgroup { background-color: #ffffff; color: #201c14; }

/* Pastel text tones and the pale-pink alert were tuned for the dark theme; darken them on light. */
.theme-material.light-mode .alert { color: #9d2f26; }
.theme-material.light-mode .badge-primary { color: #7a5514; }
.theme-material.light-mode .badge-success { color: #2d7a4a; }
.theme-material.light-mode .badge-tools { color: #1565c0; }
.theme-material.light-mode .badge-vision { color: #2e7d32; }
.theme-material.light-mode .badge-reasoning { color: #7b1fa2; }
.theme-material.light-mode .badge-transcription { color: #b26a00; }
.theme-material.light-mode .badge-speech { color: #00838f; }
.theme-material.light-mode .badge-image-gen { color: #c2185b; }
.theme-material.light-mode .badge-video { color: #d84315; }
.theme-material.light-mode .btn-ghost { color: var(--text); }

/* Generic .btn:hover swaps in a translucent fill; keep the gold gradient so the dark label stays readable. */
.theme-material .btn-primary:hover:not(:disabled) { background: linear-gradient(180deg, var(--accent-strong), var(--accent)); }
