:root {
    color-scheme: dark;
    --crm-bg: #090b10;
    --crm-bg-soft: #0d1017;
    --crm-surface: rgba(22, 26, 36, .86);
    --crm-surface-solid: #151923;
    --crm-surface-elevated: #1b202c;
    --crm-text: #f5f7fb;
    --crm-text-secondary: #9aa3b4;
    --crm-text-tertiary: #6f7889;
    --crm-border: rgba(255, 255, 255, .085);
    --crm-border-strong: rgba(255, 255, 255, .14);
    --crm-accent: #6f8cff;
    --crm-accent-soft: rgba(111, 140, 255, .14);
    --crm-success: #4ad6a0;
    --crm-warning: #f0b45f;
    --crm-danger: #ff6f7d;
    --crm-radius-sm: 10px;
    --crm-radius-md: 14px;
    --crm-radius-lg: 20px;
    --crm-shadow: 0 18px 55px rgba(0, 0, 0, .32);
    --crm-sidebar-width: 224px;
    --crm-topbar-height: 64px;
    --crm-bottom-height: 72px;
    --crm-ease: cubic-bezier(.2, .75, .25, 1);
    --bg: var(--crm-bg);
    --surface: var(--crm-surface-solid);
    --surface-elevated: var(--crm-surface-elevated);
    --text-primary: var(--crm-text);
    --text-secondary: var(--crm-text-secondary);
    --border: var(--crm-border);
    --accent: var(--crm-accent);
    --danger: var(--crm-danger);
    --success: var(--crm-success);
    --warning: var(--crm-warning);
    --radius-sm: var(--crm-radius-sm);
    --radius-md: var(--crm-radius-md);
    --radius-lg: var(--crm-radius-lg);
    --shadow: var(--crm-shadow);
}

* { box-sizing: border-box; }

html {
    overflow-x: hidden;
    max-width: 100%;
}

body.crm-enabled {
    min-height: 100dvh !important;
    max-width: 100vw !important;
    overflow-x: hidden !important;
    margin: 0 !important;
    padding: var(--crm-topbar-height) 0 0 var(--crm-sidebar-width) !important;
    background:
        radial-gradient(circle at 80% -10%, rgba(84, 108, 205, .12), transparent 32rem),
        var(--crm-bg) !important;
    color: var(--crm-text) !important;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important;
    -webkit-font-smoothing: antialiased;
}

.crm-page {
    width: min(100%, 1180px);
    max-width: 100%;
    min-height: calc(100dvh - var(--crm-topbar-height));
    margin: 0 auto;
    padding: 26px 30px 48px;
    box-sizing: border-box;
    overflow-x: clip;
}

body[data-crm-section="ai"] .crm-page { width: 100%; max-width: 1440px; }

/* Ensure all page-level sheets and modal overlays sit cleanly ABOVE the App Shell bars */
.overlay, .modal-overlay, #overlay, .master-overlay {
    z-index: 1050 !important;
}
.sheet, .modal, #detailSheet, #actionModal, .master-sheet {
    z-index: 1100 !important;
    padding-bottom: max(36px, env(safe-area-inset-bottom)) !important;
}

.crm-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 900;
    width: var(--crm-sidebar-width);
    padding: 18px 12px 14px;
    display: flex;
    flex-direction: column;
    background: rgba(12, 15, 22, .92);
    border-right: 1px solid var(--crm-border);
    backdrop-filter: blur(24px) saturate(130%);
}

.crm-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 42px;
    padding: 4px 8px 14px;
    color: var(--crm-text);
    text-decoration: none;
    font-size: 15px;
    font-weight: 720;
    letter-spacing: -.02em;
}

.crm-brand span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.crm-brand-mark {
    width: 30px;
    height: 30px;
    border-radius: 9px;
    display: grid;
    place-items: center;
    color: white;
    background: linear-gradient(145deg, #7891ff, #536ce5);
    box-shadow: 0 8px 24px rgba(87, 112, 232, .25);
    flex-shrink: 0;
}

.crm-brand-mark svg { width: 18px; height: 18px; }
.crm-nav { display: grid; gap: 3px; }
.crm-nav-secondary { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--crm-border); }

.crm-nav-link {
    min-height: 42px;
    padding: 0 11px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    gap: 11px;
    color: var(--crm-text-secondary);
    text-decoration: none;
    font-size: 13px;
    font-weight: 560;
    transition: color .16s var(--crm-ease), background .16s var(--crm-ease), transform .16s var(--crm-ease);
}

.crm-nav-link:hover { color: var(--crm-text); background: rgba(255, 255, 255, .045); }
.crm-nav-link:active { transform: scale(.985); }
.crm-nav-link.active { color: #dfe5ff; background: var(--crm-accent-soft); }
.crm-nav-link svg { width: 18px; height: 18px; flex: 0 0 18px; stroke-width: 1.8; }
.crm-nav-spacer { flex: 1; }

.crm-create-button {
    margin: 12px 0 0;
    min-height: 40px;
    border: 0;
    border-radius: 12px;
    color: white;
    background: linear-gradient(145deg, #5b79ff, #435cd8);
    font: inherit;
    font-size: 13px;
    font-weight: 680;
    cursor: pointer;
    box-shadow: 0 9px 24px rgba(79, 105, 221, .22);
}

.crm-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 54px;
    padding: 8px;
    border: 1px solid var(--crm-border);
    border-radius: 14px;
    background: rgba(255,255,255,.025);
}

.crm-avatar {
    width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
    color: #dce3ff; background: var(--crm-accent-soft); font-size: 12px; font-weight: 760;
    flex-shrink: 0;
}
.crm-profile-copy { min-width: 0; flex: 1; }
.crm-profile-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; font-weight: 650; }
.crm-profile-role { margin-top: 2px; color: var(--crm-text-tertiary); font-size: 10px; text-transform: capitalize; }

.crm-topbar {
    position: fixed;
    z-index: 850;
    inset: 0 0 auto var(--crm-sidebar-width);
    height: var(--crm-topbar-height);
    padding: 0 24px;
    display: flex;
    align-items: center;
    gap: 14px;
    background: rgba(9, 11, 16, .78);
    border-bottom: 1px solid var(--crm-border);
    backdrop-filter: blur(24px) saturate(140%);
}

.crm-topbar-title { min-width: 0; font-size: 15px; font-weight: 680; letter-spacing: -.015em; }
.crm-topbar-context { margin-left: 7px; color: var(--crm-text-tertiary); font-size: 11px; font-weight: 500; }
.crm-topbar-spacer { flex: 1; }
.crm-icon-button, .crm-search-trigger {
    min-width: 44px; min-height: 40px; border: 1px solid var(--crm-border); border-radius: 12px;
    color: var(--crm-text-secondary); background: rgba(255,255,255,.03); font: inherit; cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center; gap: 9px;
}
.crm-icon-button:hover, .crm-search-trigger:hover { color: var(--crm-text); border-color: var(--crm-border-strong); background: rgba(255,255,255,.055); }
.crm-icon-button svg, .crm-search-trigger svg { width: 18px; height: 18px; }
.crm-search-trigger { width: min(290px, 28vw); justify-content: flex-start; padding: 0 12px; font-size: 12px; }
.crm-search-trigger kbd { margin-left: auto; color: var(--crm-text-tertiary); font: 10px inherit; }

.crm-notification-button { position: relative; }
.crm-badge-count {
    position: absolute; top: -5px; right: -5px; min-width: 18px; height: 18px; padding: 0 5px;
    border: 2px solid var(--crm-bg); border-radius: 999px; display: none; place-items: center;
    color: white; background: var(--crm-danger); font-size: 9px; font-weight: 760;
}
.crm-badge-count.visible { display: grid; }

.crm-bottom-nav { display: none; }

.crm-overlay {
    position: fixed; inset: 0; z-index: 1200; display: none; align-items: flex-start; justify-content: center;
    padding: max(10vh, 70px) 16px 16px; background: rgba(2,4,8,.62); backdrop-filter: blur(12px);
}
.crm-overlay.open { display: flex; }
.crm-dialog {
    width: min(620px, 100%); max-height: min(680px, 80dvh); overflow: hidden;
    border: 1px solid var(--crm-border-strong); border-radius: 20px; background: rgba(23,27,37,.98);
    box-shadow: var(--crm-shadow); animation: crm-dialog-in .18s var(--crm-ease);
}
.crm-dialog-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--crm-border); }
.crm-dialog-head > svg { width: 18px !important; height: 18px !important; color: var(--crm-text-secondary); flex-shrink: 0; }
.crm-dialog-head input {
    flex: 1; min-width: 0; height: 38px; border: 0; outline: 0; color: var(--crm-text);
    background: transparent; font: 14px inherit;
}
.crm-dialog-head .crm-icon-button { width: 34px; height: 34px; min-width: 34px; min-height: 34px; border-radius: 9px; padding: 0; }
.crm-dialog-head .crm-icon-button svg { width: 16px; height: 16px; }
.crm-dialog-body { max-height: calc(min(680px, 80dvh) - 72px); overflow: auto; padding: 10px; }
.crm-result-group + .crm-result-group { margin-top: 14px; }
.crm-result-label { padding: 5px 9px; color: var(--crm-text-tertiary); font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; }
.crm-result-item { display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 9px; border-radius: 12px; color: inherit; text-decoration: none; }
.crm-result-item:hover, .crm-result-item:focus-visible { background: rgba(255,255,255,.05); outline: none; }
.crm-result-icon { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; color: #ccd5ff; background: var(--crm-accent-soft); flex-shrink: 0; }
.crm-result-icon svg { width: 16px; height: 16px; }
.crm-result-copy { min-width: 0; flex: 1; }
.crm-result-title { display: block; font-size: 13px; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--crm-text); }
.crm-result-subtitle { display: block; margin-top: 3px; color: var(--crm-text-secondary); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crm-empty-search { padding: 34px 18px; color: var(--crm-text-secondary); text-align: center; font-size: 12px; }

.crm-more-sheet { align-items: flex-end; padding: 0; }
.crm-more-sheet .crm-dialog { width: min(560px, 100%); max-height: 78dvh; border-radius: 24px 24px 0 0; animation: crm-sheet-in .22s var(--crm-ease); padding: 8px 14px max(18px, env(safe-area-inset-bottom)); }
.crm-sheet-handle { width: 38px; height: 4px; margin: 2px auto 14px; border-radius: 999px; background: rgba(255,255,255,.18); }
.crm-more-title { padding: 0 6px 12px; font-size: 20px; font-weight: 720; letter-spacing: -.03em; }
.crm-more-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 8px; }
.crm-more-grid .crm-nav-link { min-height: 58px; border: 1px solid var(--crm-border); background: rgba(255,255,255,.025); }

.crm-action-sheet .crm-more-grid { grid-template-columns: 1fr; }
.crm-shared-toast {
    position: fixed; z-index: 1500; left: 50%; bottom: 28px; transform: translate(-50%, 18px);
    padding: 11px 15px; border: 1px solid var(--crm-border-strong); border-radius: 12px;
    color: var(--crm-text); background: rgba(27,32,44,.96); box-shadow: var(--crm-shadow);
    opacity: 0; pointer-events: none; font-size: 12px; transition: .2s var(--crm-ease);
}
.crm-shared-toast.visible { opacity: 1; transform: translate(-50%, 0); }

/* Retire page-owned cross-navigation while preserving page-specific controls. */
body.crm-enabled a.nav-link,
body.crm-enabled a.database-link,
body.crm-enabled a.settings-link,
body.crm-enabled .top .nav { display: none !important; }

:where(a, button, input, select, textarea):focus-visible { outline: 2px solid rgba(130,154,255,.9) !important; outline-offset: 2px; }

@keyframes crm-dialog-in { from { opacity: 0; transform: translateY(-8px) scale(.985); } }
@keyframes crm-sheet-in { from { transform: translateY(24px); opacity: 0; } }

@media (max-width: 820px) {
    body.crm-enabled {
        padding: 54px 0 calc(var(--crm-bottom-height) + env(safe-area-inset-bottom)) !important;
    }
    .crm-sidebar { display: none; }
    .crm-topbar { inset: 0 0 auto 0; height: 54px; padding: 0 12px; }
    .crm-topbar-title { font-size: 14px; }
    .crm-topbar-context, .crm-search-trigger span, .crm-search-trigger kbd { display: none; }
    .crm-search-trigger { width: 44px; padding: 0; justify-content: center; }
    .crm-page { min-height: calc(100dvh - 54px); padding: 15px 12px 28px; }
    .crm-bottom-nav {
        position: fixed; z-index: 920; inset: auto 0 0 0; height: calc(var(--crm-bottom-height) + env(safe-area-inset-bottom));
        padding: 4px 4px env(safe-area-inset-bottom); display: flex; justify-content: space-around;
        background: rgba(12,15,22,.95); border-top: 1px solid var(--crm-border); backdrop-filter: blur(24px) saturate(140%);
    }
    .crm-bottom-link {
        flex: 1 1 0; min-width: 0; max-width: 20%; min-height: 52px; padding: 4px 1px; border: 0; border-radius: 10px;
        color: var(--crm-text-tertiary); background: transparent; display: flex; flex-direction: column;
        align-items: center; justify-content: center; gap: 2px; text-decoration: none; font: 9.5px inherit;
        font-weight: 600; cursor: pointer;
    }
    .crm-bottom-link span {
        display: block; width: 100%; text-align: center; overflow: hidden; text-overflow: ellipsis;
        white-space: nowrap; letter-spacing: -.02em;
    }
    .crm-bottom-link svg { width: 20px; height: 20px; stroke-width: 1.8; flex-shrink: 0; }
    .crm-bottom-link.active { color: #dce3ff; background: var(--crm-accent-soft); }
    .crm-bottom-link:active { transform: scale(.96); }
    .crm-overlay { padding-top: 64px; }
    .crm-dialog { max-height: calc(100dvh - 78px); }
    .crm-dialog-body { max-height: calc(100dvh - 148px); }
    .crm-shared-toast { bottom: calc(var(--crm-bottom-height) + env(safe-area-inset-bottom) + 12px); max-width: calc(100vw - 24px); }
}

@media (max-width: 360px) {
    .crm-page { padding-left: 9px; padding-right: 9px; }
    .crm-bottom-nav { padding-left: 2px; padding-right: 2px; }
    .crm-bottom-link { font-size: 9px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
