/* =========================================================================
   COREIDON.AI — DESIGN SYSTEM
   Mobile-first. All color + spacing drawn from CSS custom properties so the
   theme toggle can flip surfaces without JS touching any component.
   ========================================================================= */

/* ---- Design tokens ---------------------------------------------------- */
:root {
    /* Typography */
    --font-heading: 'Space Grotesk', 'Inter', sans-serif;
    --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

    /* Spacing scale (4px base) */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;
    --space-12: 48px;
    --space-16: 64px;
    --space-20: 80px;

    /* Radius — sharp to slightly rounded */
    --radius-xs: 2px;
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 8px;
    --radius-pill: 999px;
    --radius: var(--radius-md);  /* legacy alias */

    /* Motion — snappy, not bouncy */
    --ease-snappy: cubic-bezier(0.4, 0, 0.2, 1);
    --transition: 160ms var(--ease-snappy);

    /* Z-index scale */
    --z-base: 1;
    --z-sticky: 100;
    --z-sidebar: 900;
    --z-toggle: 950;
    --z-modal: 2000;
    --z-toast: 9999;
}

/* ---- LIGHT THEME ------------------------------------------------------ */
:root,
[data-theme="light"] {
    color-scheme: light;

    /* Surfaces */
    --bg-canvas: #FFFFFF;
    --bg-surface: #F8F5F0;      /* secondary white — hero sections, muted blocks */
    --bg-elevated: #FFFFFF;     /* cards over canvas */
    --bg-sunken: #F1EEE8;       /* inset cells, inputs disabled */

    /* Text */
    --text-primary: #000000;
    --text-secondary: #2B303B;
    --text-muted: rgba(43, 48, 59, 0.65);
    --text-inverse: #FFFFFF;

    /* Borders */
    --border-subtle: #E5E7EB;
    --border-medium: #D1D5DB;
    --border-strong: #2B303B;

    /* Brand */
    --brand-navy: #091C3A;
    --brand-navy-hover: #0F2A55;
    --brand-navy-text: #FFFFFF;

    /* Expressive accent — cyan. Never primary CTA. */
    --accent: #0284C7;
    --accent-hover: #0369A1;
    --accent-soft: rgba(2, 132, 199, 0.08);
    --focus-ring: rgba(2, 132, 199, 0.35);

    /* Semantic */
    --success: #16A34A;
    --success-soft: rgba(22, 163, 74, 0.10);
    --danger: #DC2626;
    --danger-soft: rgba(220, 38, 38, 0.10);

    /* Shadow — whisper-soft, we rely on borders for hierarchy */
    --shadow-xs: 0 1px 2px rgba(9, 28, 58, 0.04);
    --shadow-sm: 0 2px 8px rgba(9, 28, 58, 0.05);
    --shadow-md: 0 8px 20px rgba(9, 28, 58, 0.06);
    --shadow-lg: 0 16px 40px rgba(9, 28, 58, 0.08);

    /* Legacy aliases — kept so existing inline styles don't break */
    --bg-main: var(--bg-canvas);
    --bg-secondary: var(--bg-surface);
    --card-bg: var(--bg-elevated);
    --accent-primary: var(--brand-navy);
    --accent-green: var(--success);
    --accent-red: var(--danger);
}

/* ---- DARK THEME ------------------------------------------------------- */
[data-theme="dark"] {
    color-scheme: dark;

    /* Surfaces — layered navy-ink family */
    --bg-canvas: #0A1220;        /* main page bg */
    --bg-surface: #121B2A;       /* card interiors, table headers */
    --bg-elevated: #243447;      /* popovers, raised */
    --bg-sunken: #0E1826;        /* inset cells */
    --bg-strong: #2B303B;        /* brand secondary dark — sidebar/topbar */

    /* Text */
    --text-primary: #FFFFFF;
    --text-secondary: rgba(248, 245, 240, 0.75);
    --text-muted: rgba(248, 245, 240, 0.45);
    --text-inverse: #091C3A;

    /* Borders */
    --border-subtle: #1F2A3C;    /* thin dividers */
    --border-medium: #2D3D52;    /* card outlines */
    --border-strong: #091C3A;    /* strong interactive accent */

    /* Brand */
    --brand-navy: #091C3A;
    --brand-navy-hover: #122C56;
    --brand-navy-text: #FFFFFF;

    /* Expressive accent — brighter cyan for contrast on dark */
    --accent: #38BDF8;
    --accent-hover: #7DD3FC;
    --accent-soft: rgba(56, 189, 248, 0.12);
    --focus-ring: rgba(56, 189, 248, 0.45);

    /* Semantic — softened for dark surfaces */
    --success: #34D399;
    --success-soft: rgba(52, 211, 153, 0.14);
    --danger: #FB7185;
    --danger-soft: rgba(251, 113, 133, 0.14);

    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 8px 20px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.55);

    /* Legacy aliases */
    --bg-main: var(--bg-canvas);
    --bg-secondary: var(--bg-surface);
    --card-bg: var(--bg-strong);
    --accent-primary: var(--brand-navy);
    --accent-green: var(--success);
    --accent-red: var(--danger);
}

/* ---- Reset + base ----------------------------------------------------- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    background-color: var(--bg-canvas);
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
    transition: background-color var(--transition), color var(--transition);
}

::selection {
    background: var(--accent-soft);
    color: var(--text-primary);
}

/* Focus — visible cyan ring everywhere */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
}

/* ---- Typography ------------------------------------------------------- */
h1, h2, h3, h4, h5, h6, .brand-name {
    font-family: var(--font-heading);
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

h1 { font-size: 1.75rem; line-height: 1.15; }
h2 { font-size: 1.375rem; line-height: 1.2; }
h3 { font-size: 1.125rem; line-height: 1.25; }
h4 { font-size: 1rem; line-height: 1.3; }

@media (min-width: 768px) {
    body { font-size: 15px; }
    h1 { font-size: 2.25rem; }
    h2 { font-size: 1.625rem; }
    h3 { font-size: 1.25rem; }
}

@media (min-width: 1024px) {
    h1 { font-size: 2.5rem; }
}

.brand-name {
    font-family: var(--font-heading);
    text-transform: uppercase;
    letter-spacing: 0.02em;
    font-size: 1rem;
}

code, kbd, samp, pre {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.9em;
}

a {
    color: var(--accent);
    text-decoration: none;
    transition: color var(--transition);
}
a:hover { color: var(--accent-hover); }

/* ---- Layout ----------------------------------------------------------- */
/* Mobile-first: single-column stack. Sidebar slides in as overlay. */
.modern-layout {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg-canvas);
}

.modern-main {
    flex: 1;
    width: 100%;
    max-width: 100%;
    padding: var(--space-4) var(--space-4) var(--space-10);
}

@media (min-width: 768px) {
    .modern-main {
        padding: var(--space-6) var(--space-8) var(--space-12);
    }
}

@media (min-width: 1024px) {
    .modern-layout {
        display: grid;
        grid-template-columns: 260px minmax(0, 1fr);
    }
    .modern-main {
        padding: var(--space-10) var(--space-12);
        max-width: 1440px;
        margin: 0 auto;
    }
}

/* ---- Sidebar ---------------------------------------------------------- */
/* Mobile: fixed off-canvas drawer, slides in when .sidebar-open on .modern-layout */
.modern-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: min(84vw, 320px);
    height: 100vh;
    height: 100dvh;
    background: var(--bg-canvas);
    border-right: 1px solid var(--border-subtle);
    padding: var(--space-6) var(--space-5);
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
    z-index: var(--z-sidebar);
    transform: translateX(-100%);
    transition: transform 260ms var(--ease-snappy);
    overflow-y: auto;
    overscroll-behavior: contain;
}

[data-theme="dark"] .modern-sidebar {
    background: var(--bg-strong);
    border-right-color: var(--border-subtle);
}

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

/* Backdrop when drawer is open on mobile */
.modern-layout.sidebar-open::after {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(10, 18, 32, 0.55);
    backdrop-filter: blur(2px);
    z-index: calc(var(--z-sidebar) - 1);
}

@media (min-width: 1024px) {
    .modern-sidebar {
        position: sticky;
        top: 0;
        transform: none;
        /* Fill the grid track so the sidebar doesn't overflow onto the
           main content. The mobile drawer width (min(84vw,320px)) would
           otherwise render 60px wider than the 260px track. */
        width: 100%;
        height: 100vh;
        height: 100dvh;
        padding: var(--space-8) var(--space-6);
        gap: var(--space-8);
    }
    .modern-layout.sidebar-open::after { display: none; }
}

.brand-container {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    white-space: nowrap;
    flex-shrink: 0;
}

.logo-svg,
.brand-logo-container.logo-svg {
    width: 36px !important;
    height: 36px !important;
    flex-shrink: 0;
}

.sidebar-close-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    align-self: flex-end;
    width: 36px;
    height: 36px;
    padding: 0;
    color: var(--text-secondary);
    background: transparent;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    transition: var(--transition);
}
.sidebar-close-trigger:hover {
    color: var(--text-primary);
    border-color: var(--border-medium);
}

@media (min-width: 1024px) {
    .sidebar-close-trigger { display: none; }
}

/* ---- Navigation ------------------------------------------------------- */
.nav-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.nav-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 10px 14px;
    text-decoration: none;
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-weight: 500;
    font-size: 0.9rem;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    white-space: nowrap;
    transition: var(--transition);
}

.nav-item:hover {
    background: var(--bg-surface);
    color: var(--text-primary);
}

.nav-item.active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 600;
}

[data-theme="dark"] .nav-item:hover {
    background: var(--bg-elevated);
    color: var(--text-primary);
}

/* ---- Header (top bar) ------------------------------------------------- */
.modern-main > header {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    margin-bottom: var(--space-6);
    flex-wrap: wrap;
}

@media (min-width: 768px) {
    .modern-main > header {
        margin-bottom: var(--space-10);
    }
}

#menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    min-width: 44px;
    border-radius: var(--radius-md);
    background: var(--bg-canvas);
    color: var(--text-primary);
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: var(--transition);
}
#menu-toggle:hover {
    border-color: var(--accent);
    color: var(--accent);
}

@media (min-width: 1024px) {
    #menu-toggle { display: none !important; }
}

#page-title {
    font-family: var(--font-heading);
    font-size: 0.95rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#user-greeting {
    max-width: 220px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* ---- Cards ------------------------------------------------------------ */
.card-modern {
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-5);
    margin-bottom: var(--space-5);
}

[data-theme="dark"] .card-modern {
    background: var(--bg-surface);
    border-color: var(--border-subtle);
}

@media (min-width: 768px) {
    .card-modern {
        padding: var(--space-8);
        margin-bottom: var(--space-8);
    }
}

/* ---- Buttons ---------------------------------------------------------- */
.btn-modern {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: 10px 18px;
    min-height: 40px;
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: 0.85rem;
    letter-spacing: 0.02em;
    background: var(--brand-navy);
    color: var(--brand-navy-text);
    border: 1px solid var(--brand-navy);
    border-radius: var(--radius-md);
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--transition),
                border-color var(--transition),
                transform var(--transition),
                color var(--transition),
                opacity var(--transition);
}

.btn-modern:hover:not(:disabled) {
    background: var(--brand-navy-hover);
    border-color: var(--brand-navy-hover);
    transform: translateY(-1px);
}

.btn-modern:active:not(:disabled) { transform: translateY(0); }

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

/* Secondary button: transparent, cyan on hover */
.btn-outline {
    background: transparent;
    border: 1px solid var(--border-medium);
    color: var(--text-primary);
}

.btn-outline:hover:not(:disabled) {
    background: transparent;
    border-color: var(--accent);
    color: var(--accent);
    transform: translateY(-1px);
}

[data-theme="dark"] .btn-outline {
    border-color: var(--border-medium);
    color: var(--text-primary);
}

@media (min-width: 768px) {
    .btn-modern {
        padding: 12px 22px;
        min-height: 44px;
        font-size: 0.9rem;
    }
}

/* ---- Form controls ---------------------------------------------------- */
.modern-input,
.modern-select {
    display: block;
    width: 100%;
    height: 44px;
    padding: 10px 14px;
    background-color: var(--bg-elevated);
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.95rem;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    transition: border-color var(--transition), box-shadow var(--transition);
}

[data-theme="dark"] .modern-input,
[data-theme="dark"] .modern-select {
    background-color: var(--bg-surface);
    border-color: var(--border-medium);
}

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

.modern-input:focus,
.modern-select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--focus-ring);
}

.modern-select {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 40px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%232B303B' d='M6 8 0 0h12z'/></svg>");
    background-repeat: no-repeat;
    background-position: right 14px center;
    cursor: pointer;
}

[data-theme="dark"] .modern-select {
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%23F8F5F0' d='M6 8 0 0h12z'/></svg>");
}

@media (min-width: 768px) {
    .modern-input, .modern-select {
        height: 46px;
        padding: 11px 16px;
        padding-right: 40px; /* for select arrow */
    }
    .modern-input { padding-right: 16px; }
}

/* ---- Tables ----------------------------------------------------------- */
.table-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    background: var(--bg-elevated);
}

[data-theme="dark"] .table-container {
    background: var(--bg-surface);
}

.modern-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
    min-width: 640px;
}

.modern-table th {
    padding: var(--space-3) var(--space-4);
    font-family: var(--font-heading);
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-subtle);
    white-space: nowrap;
}

[data-theme="dark"] .modern-table th {
    background: var(--bg-surface);
    color: var(--text-secondary);
}

.modern-table td {
    padding: var(--space-4);
    border-bottom: 1px solid var(--border-subtle);
    font-size: 0.9rem;
    color: var(--text-primary);
    vertical-align: middle;
}

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

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

[data-theme="dark"] .modern-table tbody tr:hover {
    background: var(--bg-elevated);
}

@media (min-width: 768px) {
    .modern-table th {
        padding: var(--space-4) var(--space-5);
    }
    .modern-table td {
        padding: var(--space-4) var(--space-5);
        font-size: 0.95rem;
    }
}

/* ---- Status pills ----------------------------------------------------- */
.status-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    border-radius: var(--radius-pill);
    font-family: var(--font-heading);
    font-size: 0.68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    line-height: 1.2;
    white-space: nowrap;
    border: 1px solid transparent;
}

.status-pill.active {
    background: var(--success-soft);
    color: var(--success);
    border-color: color-mix(in srgb, var(--success) 30%, transparent);
}

.status-pill.inactive {
    background: var(--danger-soft);
    color: var(--danger);
    border-color: color-mix(in srgb, var(--danger) 30%, transparent);
}

/* ---- Modern switch ---------------------------------------------------- */
.modern-switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
    flex-shrink: 0;
}

.modern-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background-color: var(--border-medium);
    transition: background var(--transition);
    border-radius: var(--radius-pill);
}

.slider::before {
    content: '';
    position: absolute;
    height: 16px;
    width: 16px;
    left: 3px;
    top: 3px;
    background-color: #FFFFFF;
    border-radius: 50%;
    transition: transform var(--transition);
}

input:checked + .slider {
    background-color: var(--success);
}
input:checked + .slider::before {
    transform: translateX(18px);
}

input:focus-visible + .slider {
    box-shadow: 0 0 0 3px var(--focus-ring);
}

/* ---- Modal ------------------------------------------------------------ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 18, 32, 0.55);
    backdrop-filter: blur(4px);
    display: none;
    align-items: flex-start;
    justify-content: center;
    z-index: var(--z-modal);
    padding: var(--space-4);
    overflow-y: auto;
}

.modal-overlay[style*="flex"],
.modal-overlay.visible {
    display: flex;
}

.modal-modern {
    position: relative;
    width: 100%;
    max-width: 600px;
    margin: auto;
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
    box-shadow: var(--shadow-lg);
}

[data-theme="dark"] .modal-modern {
    background: var(--bg-surface);
    border-color: var(--border-medium);
}

.modal-modern h2 {
    margin-bottom: var(--space-5);
}

@media (min-width: 768px) {
    .modal-modern { padding: var(--space-8); }
    .modal-overlay { align-items: center; }
}

.modal-modern .modal-close {
    position: absolute;
    top: var(--space-3);
    right: var(--space-3);
    width: 36px;
    height: 36px;
    padding: 0;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    border-radius: var(--radius-md);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}
.modal-modern .modal-close:hover {
    color: var(--text-primary);
    background: var(--bg-surface);
}
[data-theme="dark"] .modal-modern .modal-close:hover {
    background: var(--bg-sunken);
}
.modal-modern .modal-close .material-icons { font-size: 20px; }

/* Wider variants for modals with many fields / wide trees */
.modal-modern.is-wide   { max-width: 820px; }
.modal-modern.is-xwide  { max-width: 960px; }

.modal-subtitle {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-bottom: var(--space-5);
    margin-top: calc(-1 * var(--space-4));
}

/* Inline-checkbox label row */
.checkbox-label {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: 0.9rem;
    cursor: pointer;
}

/* ---- App pills (scope editor / card grid) ----------------------------- */
.app-item {
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    transition: var(--transition);
    cursor: pointer;
    position: relative;
    flex-shrink: 0;
    width: 100%;
}

[data-theme="dark"] .app-item {
    background: var(--bg-surface);
    border-color: var(--border-medium);
}

.app-item:hover {
    border-color: var(--accent);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

.app-item-content {
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 100%;
}

.app-item .app-name {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: 0.9rem;
    letter-spacing: 0.01em;
    line-height: 1.35;
    word-break: break-word;
    color: var(--text-primary);
}

/* Scope chips — cyan expressive accent */
.scope-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.scope-badge {
    background: var(--accent-soft);
    color: var(--accent);
    font-family: var(--font-heading);
    font-size: 0.62rem;
    font-weight: 700;
    padding: 3px 8px;
    border-radius: var(--radius-sm);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    line-height: 1.3;
    border: 1px solid color-mix(in srgb, var(--accent) 22%, transparent);
}

/* ---- Scope selector (within modal) ------------------------------------ */
.scope-checkbox-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 10px 12px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
}

.scope-checkbox-item:hover {
    background: var(--bg-surface);
    border-color: var(--border-medium);
}

.scope-checkbox-item.checked {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.scope-checkbox-label {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.scope-name {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: 0.85rem;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--text-primary);
}

.scope-description {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

/* ---- Responsive table variant (card-per-row on mobile) ---------------- */
@media (max-width: 767px) {
    .modern-table.responsive-table {
        display: block;
        min-width: 0;
        border: none;
    }
    .modern-table.responsive-table thead { display: none; }

    .modern-table.responsive-table tbody,
    .modern-table.responsive-table tr {
        display: block;
        width: 100%;
    }

    .modern-table.responsive-table tr {
        margin-bottom: var(--space-4);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-md);
        background: var(--bg-elevated);
    }

    .modern-table.responsive-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        text-align: right;
        padding: 12px 14px;
        border-bottom: 1px solid var(--border-subtle);
    }

    .modern-table.responsive-table td::before {
        content: attr(data-label);
        font-family: var(--font-heading);
        font-weight: 700;
        text-transform: uppercase;
        font-size: 0.7rem;
        letter-spacing: 0.06em;
        color: var(--text-secondary);
        margin-right: var(--space-4);
    }

    .modern-table.responsive-table td:last-child { border-bottom: none; }
}

/* ---- Theme toggle (sun/moon, always visible) ------------------------- */
.theme-toggle {
    position: fixed;
    bottom: calc(var(--space-5) + env(safe-area-inset-bottom, 0px));
    right: var(--space-5);
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: var(--radius-pill);
    background: var(--bg-elevated);
    color: var(--text-primary);
    border: 1px solid var(--border-medium);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
    z-index: var(--z-toggle);
    transition: var(--transition);
}

.theme-toggle:hover {
    border-color: var(--accent);
    color: var(--accent);
    transform: translateY(-1px);
}

.theme-toggle .material-icons {
    font-size: 20px;
}

[data-theme="dark"] .theme-toggle {
    background: var(--bg-elevated);
}

@media (min-width: 1024px) {
    .theme-toggle {
        bottom: var(--space-8);
        right: var(--space-8);
    }
}

/* ---- Toast ------------------------------------------------------------ */
.toast-container {
    position: fixed;
    bottom: var(--space-5);
    left: 50%;
    transform: translateX(-50%);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    pointer-events: none;
    padding: 0 var(--space-4);
    max-width: calc(100vw - var(--space-8));
}

.toast {
    padding: 10px 18px;
    border-radius: var(--radius-md);
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--brand-navy-text);
    background: var(--brand-navy);
    box-shadow: var(--shadow-md);
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 200ms var(--ease-snappy), transform 200ms var(--ease-snappy);
    pointer-events: auto;
    border: 1px solid transparent;
}

.toast.visible {
    opacity: 1;
    transform: translateY(0);
}

.toast.green { background: var(--success); color: #FFFFFF; }
.toast.red   { background: var(--danger); color: #FFFFFF; }

[data-theme="dark"] .toast.green,
[data-theme="dark"] .toast.red {
    color: #FFFFFF;
}

/* ---- Utility classes -------------------------------------------------- */

/* Layout primitives */
.stack     { display: flex; flex-direction: column; gap: var(--space-4); }
.stack-sm  { display: flex; flex-direction: column; gap: var(--space-2); }
.stack-lg  { display: flex; flex-direction: column; gap: var(--space-6); }

.row            { display: flex; gap: var(--space-3); align-items: center; flex-wrap: wrap; }
.row-tight      { display: flex; gap: var(--space-2); align-items: center; }
.row-end        { display: flex; gap: var(--space-2); align-items: center; justify-content: flex-end; flex-wrap: wrap; }
.row-between    { display: flex; gap: var(--space-3); align-items: center; justify-content: space-between; flex-wrap: wrap; }
.row-top        { display: flex; gap: var(--space-3); align-items: flex-start; }
.spacer         { flex: 1 1 auto; }

/* Responsive grid helpers. Collapse to single column on mobile. */
.grid-2 { display: grid; grid-template-columns: 1fr; gap: var(--space-4); }
.grid-3 { display: grid; grid-template-columns: 1fr; gap: var(--space-4); }
@media (min-width: 640px) {
    .grid-2 { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 768px) {
    .grid-3 { grid-template-columns: 1fr 1fr 1fr; }
}

/* Typography */
.text-xs  { font-size: 0.75rem; }
.text-sm  { font-size: 0.85rem; }
.text-md  { font-size: 0.95rem; }
.text-lg  { font-size: 1.125rem; }

.text-muted    { color: var(--text-muted); }
.text-secondary{ color: var(--text-secondary); }
.text-primary  { color: var(--text-primary); }
.text-danger   { color: var(--danger); }
.text-success  { color: var(--success); }
.text-accent   { color: var(--accent); }

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

.fw-500        { font-weight: 500; }
.fw-600        { font-weight: 600; }
.fw-700        { font-weight: 700; }

.mono          { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; }

/* Small uppercase labels (form fields, table-like headers) */
.label-sm {
    display: block;
    font-family: var(--font-heading);
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: var(--space-2);
}

/* Responsive-visibility toggles */
.hidden-mobile  { display: none; }
.hidden-desktop { display: initial; }
@media (min-width: 768px) {
    .hidden-mobile  { display: initial; }
    .hidden-desktop { display: none; }
}

/* ---- Component classes ----------------------------------------------- */

/* Card header: title block (left) + actions (right), stacks on mobile. */
.card-header {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-bottom: var(--space-5);
}
.card-header .card-title    { font-size: 1.5rem; }
.card-header .card-subtitle { color: var(--text-secondary); font-size: 0.9rem; }
.card-header .card-header-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }

@media (min-width: 768px) {
    .card-header {
        flex-direction: row;
        align-items: flex-end;
        justify-content: space-between;
        margin-bottom: var(--space-8);
    }
    .card-header .card-title { font-size: 2rem; }
}

/* Form groups + rows */
.form-group { display: flex; flex-direction: column; gap: var(--space-2); }
.form-group label,
.form-label {
    display: block;
    font-family: var(--font-heading);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin: 0;
}

.form-row-2 { display: grid; grid-template-columns: 1fr; gap: var(--space-3); }
.form-row-3 { display: grid; grid-template-columns: 1fr; gap: var(--space-3); }
@media (min-width: 640px) {
    .form-row-2 { grid-template-columns: 1fr 1fr; gap: var(--space-4); }
}
@media (min-width: 768px) {
    .form-row-3 { grid-template-columns: 1fr 1fr 1fr; gap: var(--space-4); }
}

/* Inline status slot between form body and actions */
.form-status {
    min-height: 1.25rem;
    font-size: 0.85rem;
    font-weight: 600;
}
.form-status.is-error   { color: var(--danger); }
.form-status.is-success { color: var(--success); }

/* Action row at the bottom of forms / modals */
.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    justify-content: flex-end;
    margin-top: var(--space-2);
}

/* Hint text under a form / in a callout box */
.form-help {
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* Compact icon-only button (28×28) — used across tree rows, tables */
.btn-icon {
    width: 28px;
    height: 28px;
    padding: 0;
    min-height: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.btn-icon .material-icons { font-size: 16px; }

.btn-icon-sm {
    width: 24px;
    height: 24px;
}
.btn-icon-sm .material-icons { font-size: 14px; }

/* Destructive variant */
.btn-danger {
    background: var(--danger);
    border-color: var(--danger);
    color: #FFFFFF;
}
.btn-danger:hover:not(:disabled) {
    background: color-mix(in srgb, var(--danger) 82%, #000 18%);
    border-color: color-mix(in srgb, var(--danger) 82%, #000 18%);
    color: #FFFFFF;
}

/* Group of action buttons in a tight flex row */
.btn-group { display: inline-flex; gap: 4px; flex-shrink: 0; }

/* Password-field wrapper: input + inline eye toggle */
.pw-field { position: relative; }
.pw-field .modern-input { padding-right: 44px; }
.pw-field .pw-toggle {
    position: absolute;
    right: var(--space-2);
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    padding: 0;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}
.pw-field .pw-toggle:hover { color: var(--text-primary); }
.pw-field .pw-toggle .material-icons { font-size: 20px; }

/* Callout / helper panel — subtle surface with optional title */
.panel-note {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: var(--space-4);
    font-size: 0.85rem;
    line-height: 1.55;
    color: var(--text-secondary);
}
.panel-note .panel-note-title {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: 0.75rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-primary);
    margin-bottom: var(--space-2);
}

[data-theme="dark"] .panel-note {
    background: var(--bg-sunken);
    border-color: var(--border-subtle);
}

/* Empty-state centered message inside lists/tables */
.empty-state {
    padding: var(--space-6);
    color: var(--text-secondary);
    text-align: center;
    font-size: 0.9rem;
}

/* Divider */
.divider {
    height: 1px;
    background: var(--border-subtle);
    border: 0;
    margin: var(--space-4) 0;
}

/* Inline read-only value display (used where an editable select was) */
.readonly-value {
    font-size: 0.85rem;
    color: var(--text-secondary);
}
.readonly-value.is-role { font-weight: 600; font-size: 0.85rem; color: var(--text-primary); }

/* Small pill-like tag (e.g., unit type badge, company chip) */
.tag {
    display: inline-block;
    font-size: 0.68rem;
    font-weight: 600;
    padding: 2px 8px;
    background: var(--bg-surface);
    color: var(--text-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-pill);
    letter-spacing: 0.02em;
}
[data-theme="dark"] .tag {
    background: var(--bg-sunken);
    border-color: var(--border-subtle);
}

/* Identity cell pattern: primary line + muted subline */
.identity .identity-name { font-weight: 700; color: var(--text-primary); }
.identity .identity-meta { font-size: 0.8rem; color: var(--text-secondary); margin-top: 2px; }

/* Modal body scroll guard: max-height area inside the modal */
.modal-scroll {
    max-height: 60vh;
    overflow-y: auto;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
}

/* "Edit row" highlight used in the org-unit manager */
.edit-row-active {
    background: var(--accent-soft);
}

/* ---- Org-unit manager (inside the modal) ----------------------------- */
.ou-manager-hint { color: var(--text-secondary); font-size: 0.85rem; margin-bottom: var(--space-4); }
.ou-manager-hint .material-icons { font-size: 14px; vertical-align: middle; }

.ou-filter-bar {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
    align-items: end;
}
@media (min-width: 640px) {
    .ou-filter-bar { grid-template-columns: 180px 1fr; }
}

.ou-create-fieldset {
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: var(--space-4);
    margin-bottom: var(--space-6);
}
.ou-create-fieldset > legend {
    font-family: var(--font-heading);
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 0 var(--space-2);
    color: var(--text-secondary);
}
.ou-create-fieldset .ou-create-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-3);
    align-items: end;
}
@media (min-width: 768px) {
    .ou-create-fieldset .ou-create-grid {
        grid-template-columns: minmax(0, 1fr) 160px minmax(0, 1fr) auto;
        gap: var(--space-3);
    }
}

.ou-tree-container {
    max-height: 480px;
    overflow-y: auto;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
}

.ou-drop-root {
    padding: 10px var(--space-4);
    background: var(--bg-surface);
    font-family: var(--font-heading);
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-secondary);
    border-bottom: 1px dashed var(--border-subtle);
    text-align: center;
}
.ou-drop-root .material-icons { font-size: 14px; vertical-align: middle; }

.ou-row {
    --ou-pad-left: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 1rem 10px var(--ou-pad-left);
    border-bottom: 1px solid var(--border-subtle);
    cursor: grab;
}
.ou-row:last-child { border-bottom: none; }

.ou-row-label {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    font-weight: 600;
    min-width: 0;
}
.ou-row-label .drag-grip       { font-size: 16px; opacity: 0.3; }
.ou-row-label .branch-icon     { font-size: 16px; opacity: 0.6; }
.ou-row-label .branch-icon-sub { opacity: 0.4; }
.ou-row-label .unit-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ou-edit-row {
    --ou-pad-left: 1rem;
    padding: var(--space-3) 1rem var(--space-3) var(--ou-pad-left);
    border-bottom: 1px solid var(--border-subtle);
    background: var(--accent-soft);
}
.ou-edit-row .ou-edit-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-3);
    margin-bottom: var(--space-3);
}
@media (min-width: 640px) {
    .ou-edit-row .ou-edit-grid {
        grid-template-columns: minmax(0, 1fr) 160px minmax(0, 1fr);
    }
}

/* ---- Global Admin page ----------------------------------------------- */
.search-field {
    position: relative;
    width: 100%;
}
@media (min-width: 640px) {
    .search-field { width: 260px; }
}
.search-field > .modern-input { padding-left: 2.25rem; }
.search-field > .material-icons {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
    font-size: 18px;
}

.company-chip {
    display: inline-block;
    font-family: var(--font-heading);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    border: 1px solid var(--border-subtle);
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    color: var(--text-primary);
}

/* ---- Bulk upload modal ------------------------------------------------ */
.template-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
}
.template-link .material-icons { font-size: 16px; }
.template-link:hover { color: var(--accent-hover); }

.bulk-results {
    margin-top: var(--space-2);
    max-height: 260px;
    overflow-y: auto;
}
.bulk-results-table { font-size: 0.85rem; min-width: 0; }

/* ---- Apps Editor (scope management modal) ---------------------------- */
.permissions-editor { display: flex; flex-direction: column; gap: var(--space-6); }

.permissions-editor > header {
    border-bottom: 1px solid var(--border-subtle);
    padding-bottom: var(--space-5);
}
.permissions-editor > header h2 { font-size: 1.5rem; margin: 0; }
.permissions-editor > header .identity-strip {
    color: var(--text-secondary);
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: 0.75rem;
    letter-spacing: 0.06em;
    margin-top: var(--space-2);
}
.permissions-editor > header .identity-strip .identity-value {
    color: var(--brand-navy);
}
[data-theme="dark"] .permissions-editor > header .identity-strip .identity-value {
    color: var(--accent);
}

.permissions-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-5);
}
@media (min-width: 768px) {
    .permissions-grid { grid-template-columns: 1fr 1fr; gap: var(--space-8); }
}

.permissions-column .permissions-column-title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
    font-family: var(--font-heading);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.06em;
}
.permissions-column.available .permissions-column-title { color: var(--text-muted); }
.permissions-column.available .permissions-column-title .material-icons { font-size: 18px; }

.permissions-column.allowed .permissions-column-title { color: var(--success); }
.permissions-column.allowed .permissions-column-title .material-icons { font-size: 18px; }

.permissions-dropzone {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    min-height: 160px;
    max-height: 320px;
    overflow-y: auto;
    padding: var(--space-4);
    border-radius: var(--radius-md);
    background: var(--bg-surface);
    border: 2px dashed var(--border-medium);
}
.permissions-dropzone.is-allowed {
    background: var(--success-soft);
    border: 2px solid color-mix(in srgb, var(--success) 30%, transparent);
    border-style: solid;
}
[data-theme="dark"] .permissions-dropzone { background: var(--bg-sunken); }

.permissions-editor > footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-3);
    padding-top: var(--space-5);
    border-top: 1px solid var(--border-subtle);
}

/* Scope selector sub-modal uses a higher z-index than parent modal */
#scope-selector-modal { z-index: 3000; }
#scope-selector-modal .modal-modern { max-width: 500px; }
#scope-checkboxes-container {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-bottom: var(--space-5);
    max-height: 240px;
    overflow-y: auto;
    padding-right: var(--space-2);
}
#scope-hint-text {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: var(--space-4);
}

/* Destructive outline variant — used for the Revoke App button */
.btn-outline-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}
.btn-outline-danger:hover:not(:disabled) {
    color: #FFFFFF;
    background: var(--danger);
    border-color: var(--danger);
    transform: translateY(-1px);
}

.modal-loading {
    padding: var(--space-12) var(--space-6);
    text-align: center;
}
.modal-loading .loading-spinner { margin: 0 auto; }
.modal-loading .loading-label {
    margin-top: var(--space-4);
    font-family: var(--font-heading);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
}

/* ---- Profile page ----------------------------------------------------- */
.profile-card { max-width: 820px; }
.profile-title { margin-bottom: var(--space-6); }

.profile-meta {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-3);
    padding: var(--space-5);
    background: var(--bg-surface);
    border-radius: var(--radius-md);
    margin-top: var(--space-2);
}
@media (min-width: 640px) {
    .profile-meta { grid-template-columns: repeat(3, 1fr); gap: var(--space-4); }
}
[data-theme="dark"] .profile-meta {
    background: var(--bg-sunken);
    border: 1px solid var(--border-subtle);
}
