/* ===========================================
   Base Layout - Body, Navigation, Sidebar
   =========================================== */

body {
    margin: 0;
    padding: 0;
    background: var(--surface);
}

.container {
    max-width: 1400px;
    padding: 1rem;
}

/* Auth-screen layout: vertically + horizontally center the card in the viewport
   (login, error pages). Used by templates that override {% block body %}.
   max-width:none overrides the .container cap so the flex centering resolves to
   the true viewport center on wide screens (not the left-aligned 1400px box). */
.auth-container {
    max-width: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    min-height: calc(100dvh - 4rem); /* viewport minus the top nav */
    padding-top: 15vh; /* sit the card in the upper third, not dead-center */
}

/* Top Navigation */
.top-nav {
    background: var(--c-bg);
    border-bottom: 1px solid var(--border-light);
    padding: 0.75rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.top-nav .brand {
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--text-primary);
    text-decoration: none;
    letter-spacing: 2px;
    text-transform: uppercase;
}

.nav-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* Header Project Dropdown */
.header-project { position: relative; }

.header-project-btn {
    background: var(--surface-alt);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 0.4rem 0.75rem;
    border-radius: var(--r-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
    font-size: 0.8rem;
}

.header-project-btn:hover { background: var(--surface-hover); border-color: var(--border-subtle); }

.header-project-dropdown {
    display: none;
    position: absolute;
    left: 0;
    top: 100%;
    min-width: 240px;
    background: var(--c-bg);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    padding-top: 6px;
}

.header-project-dropdown::before {
    content: '';
    position: absolute;
    top: -10px;
    left: 0;
    right: 0;
    height: 10px;
}

.header-project:hover .header-project-dropdown { display: block; }

.header-project-dropdown a {
    display: block;
    padding: 0.75rem 1.25rem;
    text-decoration: none;
    color: var(--text-primary);
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border-light);
}

.header-project-dropdown a:last-child { border-bottom: none; }
.header-project-dropdown a:hover { background: var(--surface); }
.header-project-dropdown a.active { background: var(--c-secondary-soft); color: var(--text-primary); }
.header-project-dropdown .divider { border-top: 2px solid var(--border-light); }

/* Header Channel Dropdown */
.header-channel { position: relative; margin-left: 0.5rem; }

.header-channel-btn {
    background: var(--surface);
    color: var(--text-primary);
    border: 1px solid var(--border-light);
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
    font-size: 0.8rem;
}

.header-channel-btn:hover { background: var(--border-light); }

.header-channel-dropdown {
    display: none;
    position: absolute;
    left: 0;
    top: 100%;
    min-width: 220px;
    background: var(--c-bg);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    box-shadow: var(--shadow-lg);
    z-index: 999;
    padding-top: 6px;
}

.header-channel-dropdown::before {
    content: '';
    position: absolute;
    top: -10px;
    left: 0;
    right: 0;
    height: 10px;
}

.header-channel:hover .header-channel-dropdown { display: block; }

.header-channel-dropdown a {
    display: block;
    padding: 0.75rem 1.25rem;
    text-decoration: none;
    color: var(--text-primary);
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border-light);
}

.header-channel-dropdown a:last-child { border-bottom: none; }
.header-channel-dropdown a:hover { background: var(--surface); }
.header-channel-dropdown a.active { background: var(--c-secondary-soft); color: var(--text-primary); }
.header-channel-dropdown .divider { border-top: 2px solid var(--border-light); }
.header-channel-dropdown .channel-role {
    color: var(--text-secondary);
    font-size: 0.75rem;
    margin-left: 0.25rem;
}
.header-channel-dropdown .dropdown-platform-header {
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.header-channel-dropdown .dropdown-channel-item {
    padding-left: 2.25rem;
    font-size: 0.875rem;
}

/* User Menu */
.top-nav .user-menu {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.top-nav .user-menu span { color: var(--text-secondary); font-size: 0.9rem; }
.top-nav .user-menu form { margin: 0; display: flex; align-items: center; }

.top-nav .user-menu button {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
    margin: 0;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
}

.top-nav .user-menu button:hover {
    background: var(--surface);
}

/* App Layout */
.app-layout {
    display: flex;
    min-height: calc(100vh - 56px);
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--c-bg);
    border-right: 1px solid var(--border-light);
    padding: 1rem 0;
    flex-shrink: 0;
    position: sticky;
    top: 56px;
    height: calc(100vh - 56px);
    overflow-y: auto;
    overflow-x: hidden;
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                padding 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.main-content {
    flex: 1;
    min-width: 0;  /* allow flex item to shrink below content's intrinsic width */
    /* Pico CSS adds auto margins + a horizontal max-width to bare <main> elements;
       reset both so the dashboard fills the available column at every viewport. */
    width: auto;
    max-width: 100%;
    margin: 0;
    padding: 1.5rem 2rem;
    overflow-x: auto;
    background: var(--surface);
    box-sizing: border-box;
}

/* Sidebar Project Dropdown */
.sidebar-project {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-light);
    margin-bottom: 0.5rem;
}

.sidebar-project-btn {
    width: 100%;
    background: var(--surface-alt);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 0.5rem 0.75rem;
    border-radius: var(--r-md);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 500;
    font-size: 0.85rem;
}

.sidebar-project-btn:hover { background: var(--surface-hover); border-color: var(--border-subtle); }

.sidebar-project-dropdown {
    display: none;
    position: absolute;
    left: 1rem;
    right: 1rem;
    background: var(--c-bg);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    padding-top: 6px;
}

.sidebar-project-dropdown::before {
    content: '';
    position: absolute;
    top: -10px;
    left: 0;
    right: 0;
    height: 10px;
}

.sidebar-project:hover .sidebar-project-dropdown { display: block; }

.sidebar-project-dropdown a {
    display: block;
    padding: 0.85rem 1.25rem;
    text-decoration: none;
    color: var(--text-primary);
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border-light);
}

.sidebar-project-dropdown a:last-child { border-bottom: none; }
.sidebar-project-dropdown a:hover { background: var(--surface); }
.sidebar-project-dropdown a.active { background: var(--c-secondary-soft); color: var(--text-primary); }

/* Sidebar Navigation */
.sidebar-label {
    padding: 0.5rem 1.2rem;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.5px;
}

.sidebar-nav {
    padding: 0;
    position: sticky;
    top: 0;
    opacity: 1;
    transition: opacity 0.2s ease;
}

.sidebar-nav a {
    display: block;
    padding: 0.7rem 1.2rem;
    text-decoration: none;
    color: var(--text-secondary);
    font-size: 0.9rem;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}

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

.sidebar-nav a.active {
    color: var(--text-primary);
    background: var(--c-secondary-soft);
    border-left-color: var(--c-secondary);
    font-weight: 500;
}

/* Visual break between the project-scoped nav items and the
   superadmin-only ones (TASK-264). */
.sidebar-nav .sidebar-divider {
    border: none;
    border-top: 1px solid var(--border-light, var(--c-neutral));
    margin: 0.6rem 1.2rem;
}
.sidebar-nav .sidebar-group-label {
    display: block;
    padding: 0.4rem 1.2rem 0.1rem 1.2rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--c-muted, var(--text-secondary));
}

/* Sidebar Toggle */
.sidebar-toggle {
    position: fixed;
    left: var(--sidebar-width);
    top: 50%;
    transform: translate(-50%, -50%);
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--c-bg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-toggle-btn);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: var(--text-secondary);
    z-index: 1001;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-toggle:hover {
    background: var(--c-secondary);
    border-color: var(--c-secondary);
    color: var(--text-primary);
}

.sidebar-toggle .toggle-icon {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Collapsed State — desktop-only. The mobile drawer uses body.sidebar-open
   and lives in the @media (max-width: 768px) block below; gating the
   desktop rules to >=769px prevents a stale localStorage 'sidebar-collapsed'
   value from forcing width: 0 on the mobile drawer. */
@media (min-width: 769px) {
    .app-layout.sidebar-collapsed .sidebar {
        width: 0;
        padding: 0;
        border-right-color: transparent;
    }

    .app-layout.sidebar-collapsed .sidebar-nav {
        opacity: 0;
        pointer-events: none;
    }

    .app-layout.sidebar-collapsed .sidebar-toggle {
        left: 0;
        transform: translate(50%, -50%);
    }

    .app-layout.sidebar-collapsed .sidebar-toggle .toggle-icon {
        transform: rotate(180deg);
    }
}

/* Links & Buttons Base */
a { transition: color 0.15s ease; }
button, .btn { transition: all 0.15s ease; }
button:hover:not(:disabled), .btn:hover:not(:disabled) { transform: translateY(-1px); }
button:active:not(:disabled), .btn:active:not(:disabled) { transform: translateY(0); }

/* Back Link */
.back-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    margin-bottom: 1rem;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.85rem;
}

.back-link:hover { color: var(--c-secondary); }

/* Sidebar Rate Limit */
.sidebar-rate-limit {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border-light);
    background: var(--c-bg);
}

.rate-limit-label {
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    margin-bottom: 0.35rem;
}

.rate-limit-bar {
    height: 4px;
    background: var(--border-light);
    border-radius: 2px;
    overflow: hidden;
}

.rate-limit-fill {
    height: 100%;
    background: var(--c-success);
    border-radius: 2px;
    transition: width 0.3s ease;
}

.sidebar-rate-limit.warning .rate-limit-fill {
    background: var(--c-warning);
}

.sidebar-rate-limit.danger .rate-limit-fill {
    background: var(--c-danger);
}

.sidebar-rate-limit.burst .rate-limit-fill {
    background: var(--c-warning);
}

.rate-limit-burst {
    margin-top: 0.35rem;
    padding: 0.25rem 0.4rem;
    border-radius: 4px;
    background: color-mix(in srgb, var(--c-warning), transparent 88%);
    border: 1px solid var(--c-warning);
    color: var(--c-warning);
    font-size: 0.65rem;
    line-height: 1.2;
    text-align: center;
}

.rate-limit-text {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
    text-align: right;
}

a.sidebar-rate-limit-link {
    display: block;
    text-decoration: none;
    color: inherit;
    cursor: pointer;
}

a.sidebar-rate-limit-link:hover {
    background: var(--surface-hover);
}

a.sidebar-rate-limit-link:hover .rate-limit-label {
    color: var(--c-secondary);
}

.sidebar-rate-limit.no-plan {
    background: color-mix(in srgb, var(--c-warning), transparent 92%);
}

a.sidebar-rate-limit-link.no-plan:hover {
    background: color-mix(in srgb, var(--c-warning), transparent 84%);
}

.rate-limit-no-plan {
    margin-top: 0.35rem;
    padding: 0.35rem 0.5rem;
    border-radius: 4px;
    background: color-mix(in srgb, var(--c-warning), transparent 82%);
    border: 1px solid var(--c-warning);
    color: var(--c-warning);
    font-size: 0.7rem;
    line-height: 1.2;
    text-align: center;
    font-weight: 600;
}

/* Brand Logo */
.brand img {
    height: 18px;
    width: auto;
    max-width: 120px;
}

.brand .logo-for-light { display: block; }
.brand .logo-for-dark { display: none; }

[data-theme="dark"] .brand .logo-for-light { display: none; }
[data-theme="dark"] .brand .logo-for-dark { display: block; }

/* Theme Picker Button */
.theme-picker-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.3rem 0.5rem;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    margin: 0;
    min-width: 2.2rem;
}

.theme-picker-btn:hover {
    background: var(--surface);
    transform: none;
}

/* Theme Toggle Button */
.theme-toggle {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.3rem 0.5rem;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    margin: 0;
    min-width: 2.2rem;
}

.theme-toggle:hover {
    background: var(--surface);
    transform: none;
}

/* In light mode: show moon (switch to dark), hide sun */
.theme-icon-light { display: none; }
.theme-icon-dark { display: inline; }

/* In dark mode: show sun (switch to light), hide moon */
[data-theme="dark"] .theme-icon-light { display: inline; }
[data-theme="dark"] .theme-icon-dark { display: none; }

/* Mobile burger menu (project pages only — rendered in projects/layout.html) */
.burger-menu {
    display: none;
    position: fixed;
    top: 10px;
    left: 10px;
    width: 36px;
    height: 36px;
    background: var(--c-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    padding: 0;
    color: var(--text-primary);
    z-index: 1002;
    box-shadow: var(--shadow-toggle-btn);
}

.burger-menu:hover {
    background: var(--surface);
}

.mobile-scrim {
    display: none;
    position: fixed;
    top: 56px;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--overlay-bg);
    z-index: 999;
}

@media (max-width: 768px) {
    /* Belt-and-suspenders: stop any rogue child from making the page horizontally
       scrollable. The top-nav and user-menu shrink to fit, but if a downstream
       component overflows we'd rather clip than expose horizontal scroll. */
    body {
        overflow-x: hidden;
        max-width: 100%;
        margin: 0;
        padding: 0;
    }

    /* Defeat Pico's auto-margin / typography-spacing on the layout container so
       the dashboard column reaches both viewport edges. */
    .app-layout {
        max-width: 100%;
        margin: 0;
    }

    /* Sidebar becomes an off-canvas drawer. We cover from top: 0 so the drawer
       reads as a full-screen overlay; the burger sits above it (z-index 1002). */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        width: var(--sidebar-width);
        height: 100vh;
        padding-top: 3.5rem;  /* don't tuck nav items under the burger */
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 1000;
        box-shadow: var(--shadow-lg);
    }

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

    .mobile-scrim {
        top: 0;
    }

    body.sidebar-open .mobile-scrim {
        display: block;
    }

    /* Hide desktop collapse toggle on mobile; the burger replaces it */
    .sidebar-toggle {
        display: none;
    }

    .burger-menu {
        display: inline-flex;
    }

    /* Top nav wraps to two rows: nav-left (logo + dropdowns) on row 1,
       user-menu (theme/account/logout) on row 2. Burger sits over the top-left. */
    .top-nav {
        flex-wrap: wrap;
        align-items: flex-start;
        padding: 0.5rem 0.5rem 0.5rem 3rem;
        gap: 0.4rem;
        row-gap: 0.5rem;
    }

    /* User info first (row 1), project/channel picker below it (row 2) on mobile.
       Brand sits with the dropdowns; the burger is the visual anchor at top-left. */
    .top-nav .user-menu {
        order: 1;
        flex: 1 1 100%;
        flex-wrap: wrap;
        gap: 0.3rem;
        justify-content: flex-end;
    }

    .nav-left {
        order: 2;
        flex: 1 1 100%;
        flex-wrap: wrap;
        gap: 0.4rem;
        min-width: 0;
    }

    /* Tighten user-menu items so a name + Logout fits comfortably */
    .top-nav .user-menu a,
    .top-nav .user-menu form button {
        font-size: 0.72rem;
        padding: 0.3rem 0.5rem;
    }

    .top-nav .user-menu span {
        font-size: 0.72rem;
    }

    .theme-picker-btn,
    .theme-toggle {
        min-width: 2rem;
        padding: 0.25rem 0.4rem;
    }

    /* The taller two-row nav means we can't pin .app-layout height to 100vh-56px */
    .app-layout {
        min-height: auto;
    }

    /* Main content uses full width when sidebar is a drawer */
    .main-content {
        padding: 1rem;
    }

    /* Hide the floating chat widget while the drawer is open so it doesn't
       compete with the sidebar nav for attention or peek out from under it. */
    body.sidebar-open .chat-widget {
        display: none;
    }
}
