/* Shared authenticated navigation. All selectors are scoped to app-* classes
   or pages that render the shared app sidebar. */
:root {
    --app-sidebar-width: 258px;
    --app-sidebar-bg: #101a2d;
    --app-sidebar-border: rgba(148, 163, 184, .13);
    --app-sidebar-text: #c3cede;
    --app-sidebar-muted: #91a0b8;
    --app-sidebar-active: #2563eb;
    --app-sidebar-z: 1000;
}

body:has(.app-sidebar) {
    margin: 0;
    color: #172033;
    background: #f4f7fb;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.app-nav-open { overflow: hidden; }

.app-sidebar {
    position: fixed;
    z-index: var(--app-sidebar-z);
    inset: 0 auto 0 0;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    width: var(--app-sidebar-width);
    min-height: 100vh;
    padding: 22px 14px 18px;
    overflow-x: hidden;
    overflow-y: auto;
    color: #fff;
    background: var(--app-sidebar-bg);
    border-right: 1px solid var(--app-sidebar-border);
    box-shadow: 8px 0 28px rgba(15, 23, 42, .08);
}

.app-brand {
    display: flex;
    min-height: 56px;
    align-items: center;
    gap: 12px;
    margin: 0 5px 22px;
    padding: 0 8px 18px;
    color: #f8fafc;
    border-bottom: 1px solid var(--app-sidebar-border);
    text-decoration: none;
}

.app-brand-mark {
    display: block;
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    border-radius: 50%;
    object-fit: contain;
}

.app-brand-name {
    font-size: 20px;
    font-weight: 750;
    letter-spacing: -.035em;
}

.app-sidebar-menu {
    display: flex;
    min-height: 0;
    flex: 1;
    flex-direction: column;
    gap: 5px;
}

.app-nav-link {
    position: relative;
    display: flex;
    min-height: 45px;
    box-sizing: border-box;
    align-items: center;
    gap: 13px;
    padding: 0 12px;
    border: 1px solid transparent;
    border-radius: 10px;
    color: var(--app-sidebar-text);
    font-size: 14px;
    font-weight: 550;
    line-height: 1.25;
    text-decoration: none;
    transition: color .16s ease, background-color .16s ease, border-color .16s ease, transform .16s ease;
}

.app-nav-link > i {
    display: inline-flex;
    width: 20px;
    flex: 0 0 20px;
    justify-content: center;
    color: var(--app-sidebar-muted);
    font-size: 16px;
    transition: color .16s ease, transform .16s ease;
}

.app-nav-link:hover {
    color: #fff;
    background: rgba(148, 163, 184, .11);
}

.app-nav-link:hover > i { color: #dbeafe; transform: translateX(1px); }

.app-nav-link.is-active {
    color: #fff;
    border-color: rgba(147, 197, 253, .15);
    background: var(--app-sidebar-active);
    box-shadow: 0 5px 14px rgba(37, 99, 235, .2);
}

.app-nav-link.is-active::before {
    position: absolute;
    inset: 9px auto 9px -1px;
    width: 3px;
    border-radius: 0 4px 4px 0;
    background: #bfdbfe;
    content: "";
}

.app-nav-link.is-active > i { color: #fff; }
.app-nav-logout { margin-top: auto; border-top: 1px solid var(--app-sidebar-border); border-radius: 0 0 10px 10px; padding-top: 12px; }
.support-unread-badge { min-width: 19px; height: 19px; margin-left: auto; padding: 0 5px; border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; background: #ef4444; color: #fff; font-size: 11px; font-weight: 800; line-height: 1; }
.support-unread-badge.is-hidden { display: none; }

.app-nav-link:focus-visible,
.app-brand:focus-visible,
.app-nav-toggle:focus-visible {
    outline: 3px solid #93c5fd;
    outline-offset: 3px;
}

.app-nav-toggle,
.app-nav-backdrop { display: none; }

/* Existing pages own their layouts; these scoped rules align their outer
   content wrappers with the shared fixed navigation without affecting public
   pages or unrelated sidebar components. */
body:has(.app-sidebar) .content,
body:has(.app-sidebar) .dash,
body:has(.app-sidebar) .campaign-shell,
body:has(.app-sidebar) .campaign-page,
body:has(.app-sidebar) .page-wrap,
body:has(.app-sidebar) .billing-content,
body:has(.app-sidebar) .page {
    box-sizing: border-box;
    width: auto;
    min-width: 0;
    max-width: none;
    margin-left: var(--app-sidebar-width);
    margin-right: 0;
}

body:has(.app-sidebar) > :not(.billing-layout):not(.app-sidebar):not(.app-nav-toggle):not(.app-nav-backdrop):not(style):not(script):not(link) {
    box-sizing: border-box;
    width: auto;
    min-width: 0;
    max-width: calc(100vw - var(--app-sidebar-width));
    margin-left: var(--app-sidebar-width);
}

body:has(.app-sidebar) .content,
body:has(.app-sidebar) .dash,
body:has(.app-sidebar) .campaign-shell,
body:has(.app-sidebar) .campaign-page,
body:has(.app-sidebar) .page-wrap,
body:has(.app-sidebar) .billing-content,
body:has(.app-sidebar) .page,
body:has(.app-sidebar) main {
    min-width: 0;
}

body:has(.app-sidebar) .topbar { margin-left: var(--app-sidebar-width); }
body:has(.app-sidebar) table { max-width: 100%; }
body:has(.app-sidebar) .sidebar { display: none !important; }
body:has(.app-sidebar) > .ld {
    box-sizing: border-box;
    width: auto;
    max-width: none;
    min-width: 0;
    margin: 0 0 0 var(--app-sidebar-width);
    padding: 30px clamp(16px, 3vw, 38px);
}
body:has(.app-sidebar) > .container {
    box-sizing: border-box;
    width: min(700px, calc(100% - var(--app-sidebar-width) - 48px));
    max-width: 700px;
    margin: 40px 24px 40px calc(var(--app-sidebar-width) + 24px);
}
body:has(.app-sidebar) .table-box,
body:has(.app-sidebar) .ld-table,
body:has(.app-sidebar) .wrap { max-width: 100%; overflow-x: auto; }

/* Billing pages wrap the shared navigation and content in .billing-layout,
   unlike other authenticated pages. Keep that wrapper full-width and offset
   only the content so the fixed sidebar never gets counted twice. */
body:has(.app-sidebar) .billing-layout {
    display: block;
    width: 100%;
    min-width: 0;
    max-width: none;
    min-height: 100vh;
    margin: 0;
}

body:has(.app-sidebar) .billing-content {
    padding: 28px clamp(18px, 2.5vw, 36px);
}

body:has(.app-sidebar) .billing-content > *,
body:has(.app-sidebar) .content > *,
body:has(.app-sidebar) .dash > *,
body:has(.app-sidebar) .page-wrap > *,
body:has(.app-sidebar) .campaign-shell > * {
    min-width: 0;
    max-width: 100%;
}

body:has(.app-sidebar) .table-card,
body:has(.app-sidebar) .table-box,
body:has(.app-sidebar) .ld-table,
body:has(.app-sidebar) .wrap {
    overflow-x: auto;
}

body:has(.app-sidebar) input,
body:has(.app-sidebar) select,
body:has(.app-sidebar) textarea,
body:has(.app-sidebar) button {
    max-width: 100%;
}

body:has(.app-sidebar) .profile-grid,
body:has(.app-sidebar) .form-grid,
body:has(.app-sidebar) .support-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

body:has(.app-sidebar) .hero-stats {
    min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

body:has(.app-sidebar) .support-grid > *,
body:has(.app-sidebar) .profile-grid > *,
body:has(.app-sidebar) .form-grid > * { min-width: 0; }

body:has(.app-sidebar) .info-item strong,
body:has(.app-sidebar) .faq-item,
body:has(.app-sidebar) .msg { overflow-wrap: anywhere; }

@media (max-width: 1023px) {
    .app-sidebar {
        width: min(290px, calc(100vw - 52px));
        min-height: 100dvh;
        padding-top: 18px;
        transform: translateX(-105%);
        visibility: hidden;
        transition: transform .22s ease, visibility .22s;
    }

    .app-sidebar.is-open { transform: translateX(0); visibility: visible; }

    .app-nav-toggle {
        position: fixed;
        z-index: calc(var(--app-sidebar-z) + 2);
        top: 12px;
        left: 12px;
        display: flex;
        width: 42px;
        height: 42px;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 5px;
        padding: 0;
        border: 1px solid #dbe3ef;
        border-radius: 11px;
        color: #172033;
        background: #fff;
        box-shadow: 0 5px 18px rgba(15, 23, 42, .12);
        cursor: pointer;
    }

    .app-nav-toggle span { width: 18px; height: 2px; border-radius: 2px; background: currentColor; transition: transform .18s ease, opacity .18s ease; }
    .app-nav-toggle[aria-expanded="true"] span:first-child { transform: translateY(7px) rotate(45deg); }
    .app-nav-toggle[aria-expanded="true"] span:nth-child(2) { opacity: 0; }
    .app-nav-toggle[aria-expanded="true"] span:last-child { transform: translateY(-7px) rotate(-45deg); }

    .app-nav-backdrop:not([hidden]) {
        position: fixed;
        z-index: calc(var(--app-sidebar-z) - 1);
        inset: 0;
        display: block;
        border: 0;
        background: rgba(15, 23, 42, .48);
        animation: app-backdrop-in .18s ease both;
    }

    body:has(.app-sidebar) .content,
    body:has(.app-sidebar) .dash,
    body:has(.app-sidebar) .campaign-shell,
    body:has(.app-sidebar) .campaign-page,
    body:has(.app-sidebar) .page-wrap,
    body:has(.app-sidebar) .billing-content,
    body:has(.app-sidebar) .page,
    body:has(.app-sidebar) > .ld,
    body:has(.app-sidebar) > .container,
    body:has(.app-sidebar) > :not(.billing-layout):not(.app-sidebar):not(.app-nav-toggle):not(.app-nav-backdrop):not(style):not(script):not(link),
    body:has(.app-sidebar) .topbar {
        width: auto;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0;
    }

    body:has(.app-sidebar) .content,
    body:has(.app-sidebar) .dash,
    body:has(.app-sidebar) .campaign-shell,
    body:has(.app-sidebar) .campaign-page,
    body:has(.app-sidebar) .page-wrap,
    body:has(.app-sidebar) .billing-content,
    body:has(.app-sidebar) .page { padding-top: max(58px, 4vh); }
    body:has(.app-sidebar) > .ld,
    body:has(.app-sidebar) > .container { width: auto; max-width: 100%; margin: 0; padding: 64px 18px 20px; }

    body:has(.app-sidebar) .billing-layout { display: block; margin: 0; }
    body:has(.app-sidebar) .billing-content { width: 100%; margin: 0; padding: 64px 22px 24px; }
    body:has(.app-sidebar) .profile-grid,
    body:has(.app-sidebar) .form-grid,
    body:has(.app-sidebar) .support-grid { grid-template-columns: minmax(0, 1fr); }
    body:has(.app-sidebar) .support-hero,
    body:has(.app-sidebar) .page-hero { display: grid; grid-template-columns: minmax(0, 1fr); }
    body:has(.app-sidebar) .hero-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    body:has(.app-sidebar) .chat-form { grid-template-columns: minmax(0, 1fr); }
    body:has(.app-sidebar) .chat-form button { min-height: 44px; }

    body:has(.app-sidebar) .topbar { padding-left: 64px; }
}

@media (max-width: 600px) {
    .app-brand { margin-bottom: 16px; }
    .app-nav-link { min-height: 47px; }
    body:has(.app-sidebar) .content,
    body:has(.app-sidebar) .dash,
    body:has(.app-sidebar) .campaign-shell,
    body:has(.app-sidebar) .campaign-page,
    body:has(.app-sidebar) .page-wrap,
    body:has(.app-sidebar) .billing-content,
    body:has(.app-sidebar) .page { padding-right: 14px; padding-bottom: 20px; padding-left: 14px; }
    body:has(.app-sidebar) .billing-content { padding-top: 64px; }
    body:has(.app-sidebar) .panel,
    body:has(.app-sidebar) .hero,
    body:has(.app-sidebar) .page-hero,
    body:has(.app-sidebar) .support-hero,
    body:has(.app-sidebar) .usage-card,
    body:has(.app-sidebar) .table-card { padding: 16px; }
    body:has(.app-sidebar) .chat-box { padding: 12px; }
    body:has(.app-sidebar) .hero-stats { grid-template-columns: minmax(0, 1fr); }
    body:has(.app-sidebar) .chat-header,
    body:has(.app-sidebar) .faq-header { flex-direction: column; }
    body:has(.app-sidebar) .info-item { align-items: flex-start; flex-direction: column; gap: 5px; }
    body:has(.app-sidebar) .plan-grid { grid-template-columns: minmax(0, 1fr); }
    body:has(.app-sidebar) .table { min-width: 640px; }
}

@keyframes app-backdrop-in { from { opacity: 0; } to { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
    .app-sidebar, .app-nav-link, .app-nav-link > i, .app-nav-toggle span, .app-nav-backdrop:not([hidden]) {
        animation-duration: .01ms !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}
