/* ============================================================
   BRAINFLOOD LAYOUT SYSTEM — Sidebar + Topbar
   ============================================================
   Replaces horizontal nav dropdowns with persistent left sidebar.
   Uses CSS Grid for the overall page layout.
   All colors reference theme variables from themes.css.
   ============================================================ */

/* ---- Body Grid Layout ----
 * minmax(0, 1fr): a bare 1fr track is minmax(auto, 1fr) — its min-content
 * floor is set by the widest non-wrapping child (the topbar toolbar,
 * ~510px). On phones that widened the layout viewport past the screen,
 * which zoomed the page out and broke every fixed-position overlay
 * (the onboarding wizard rendered clipped off-screen at 390px). */
body.has-sidebar {
    display: grid;
    grid-template-columns: 220px minmax(0, 1fr);
    grid-template-rows: auto auto 1fr;
    min-height: 100vh;
    min-height: 100dvh;
}

/* ---- Sidebar ---- */
.sidebar {
    grid-row: 1 / -1;
    grid-column: 1;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--bg-secondary);
    border-right: 1px solid rgba(var(--glass-overlay-color), 0.08);
    display: flex;
    flex-direction: column;
    z-index: 160;
    padding-bottom: env(safe-area-inset-bottom, 0);
    scrollbar-width: thin;
    scrollbar-color: rgba(var(--glass-overlay-color), 0.12) transparent;
}

.sidebar::-webkit-scrollbar {
    width: 4px;
}

.sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
    background: rgba(var(--glass-overlay-color), 0.12);
    border-radius: 4px;
}

/* Sidebar Brand Row */
.sidebar-brand-row {
    display: flex;
    align-items: center;
    border-bottom: 1px solid rgba(var(--glass-overlay-color), 0.12);
    flex-shrink: 0;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    text-decoration: none;
    color: var(--text-primary);
    font-family: 'Space Grotesk', sans-serif;
    font-size: 1.125rem;
    font-weight: 700;
    flex: 1;
    min-width: 0;
}

/* Phase 2R: brand icon heartbeat + accent halo so the sidebar header
   carries the same alive-feeling vocabulary as auth/joinplay. Subtle
   enough not to distract during sustained admin work. */
.sidebar-brand-icon {
    width: 28px;
    height: 28px;
    flex-shrink: 0;
    border-radius: 6px;
    filter: drop-shadow(0 0 8px rgba(var(--accent-rgb), 0.35));
    animation: sidebar-brand-heartbeat 4s ease-in-out infinite;
}
@keyframes sidebar-brand-heartbeat {
    0%, 100% { transform: scale(1); }
    50%      { transform: scale(1.06); }
}
@media (prefers-reduced-motion: reduce) {
    .sidebar-brand-icon { animation: none; }
}

.sidebar-brand-icon img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Phase 2R: brand wordmark — gradient text + subtle shimmer, mirrors
   the auth/joinplay treatment so the surface signature is consistent
   across every entrypoint. */
.sidebar-brand .sidebar-link-text {
    background: linear-gradient(135deg,
        var(--accent) 0%,
        var(--color-secondary, var(--cyan)) 60%,
        var(--accent-light) 100%);
    background-size: 200% auto;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: sidebar-brand-shimmer 8s linear infinite;
}
@keyframes sidebar-brand-shimmer {
    from { background-position: 0% center; }
    to   { background-position: 200% center; }
}
@media (prefers-reduced-motion: reduce) {
    .sidebar-brand .sidebar-link-text { animation: none; }
}

/* Sidebar Navigation */
.sidebar-nav {
    flex: 1;
    padding: 0.5rem 0;
    overflow-y: auto;
}

.sidebar-section {
    margin-bottom: 0.25rem;
}

.sidebar-section-label {
    padding: 0.625rem 1rem 0.25rem;
    font-size: 0.625rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    user-select: none;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4375rem 1rem;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.8125rem;
    font-weight: 500;
    margin: 0 0.375rem;
    border-radius: 6px;
    transition: all 0.15s;
    cursor: pointer;
    border: none;
    background: none;
    width: calc(100% - 0.75rem);
    text-align: left;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-link:hover {
    color: var(--text-primary);
    background: rgba(var(--glass-overlay-color), 0.12);
}

/* Label text fades with the rail on collapse/hover-expand (see the
   .sidebar.collapsed .sidebar-link-text rule). Slightly quicker than the
   width transition so text trails the widening edge as it reveals. */
.sidebar-link-text {
    transition: opacity 0.18s ease;
}

/* Phase 2R: active route — accent rail bar slides in from the left
   plus a soft accent halo. Matches the active-section parent style so
   the page hierarchy reads at a glance. */
.sidebar-link.active {
    color: var(--accent);
    background: rgba(var(--accent-rgb), 0.1);
    box-shadow: inset 3px 0 0 0 var(--accent), 0 0 12px rgba(var(--accent-rgb), 0.18);
    font-weight: 600;
}
.sidebar-link.admin-link.active {
    box-shadow: inset 3px 0 0 0 var(--color-warning), 0 0 12px rgba(var(--warning-rgb), 0.2) !important;
}

/* Admin section toggle */
.sidebar-section-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: calc(100% - 0.75rem);
    margin: 0 0.375rem;
    padding: 0.4375rem 1rem;
    background: none;
    border: none;
    border-radius: 6px;
    color: var(--text-secondary);
    font-size: 0.8125rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    text-align: left;
}

.sidebar-section-toggle:hover {
    color: var(--text-primary);
    background: rgba(var(--glass-overlay-color), 0.12);
}

.sidebar-section-toggle.host-toggle {
    color: var(--accent);
}

.sidebar-section-toggle.host-toggle:hover {
    background: rgba(var(--accent-rgb), 0.08);
}

/* Phase 2R: retire hardcoded admin gold (#d4a017) — flow through
   --color-warning so light themes get readable contrast and Sports
   Hype's broadcast palette retints. */
.sidebar-section-toggle.admin-toggle {
    color: var(--color-warning);
}

.sidebar-section-toggle.admin-toggle:hover {
    background: rgba(var(--warning-rgb), 0.08);
}

/* Phase 2R: parent-toggle glow when one of its children is the
   currently-active route. The .has-active-child class is set by
   navigationData()._matchActiveSection(). */
.sidebar-section-toggle.has-active-child {
    background: rgba(var(--accent-rgb), 0.05);
    box-shadow: inset 3px 0 0 0 rgba(var(--accent-rgb), 0.55);
}
.sidebar-section-toggle.host-toggle.has-active-child {
    background: rgba(var(--accent-rgb), 0.08);
}
.sidebar-section-toggle.admin-toggle.has-active-child {
    background: rgba(var(--warning-rgb), 0.08);
    box-shadow: inset 3px 0 0 0 rgba(var(--warning-rgb), 0.65);
}

.sidebar-chevron {
    margin-left: auto;
    flex-shrink: 0;
    transition: transform 0.2s;
}

.sidebar-section-toggle.open .sidebar-chevron {
    transform: rotate(180deg);
}

/* Admin submenu */
.sidebar-submenu {
    overflow: hidden;
}

.sidebar-submenu .sidebar-link {
    padding-left: 2.5rem;
    font-size: 0.75rem;
    padding-top: 0.3rem;
    padding-bottom: 0.3rem;
}

.sidebar-sub-label {
    padding: 0.5rem 1rem 0.125rem 2.5rem;
    font-size: 0.5625rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    opacity: 0.6;
    user-select: none;
}

.sidebar-sub-label:first-child {
    padding-top: 0.25rem;
}

.sidebar-link .ext-icon {
    margin-left: 0.35rem;
    font-size: 0.7em;
    opacity: 0.55;
}

/* Phase 2R: bug fix — .admin-link was using --red (looked like an
   error state on every admin row). Now uses --color-warning to match
   the parent admin-toggle. .active state amps the warning palette. */
.sidebar-link.admin-link {
    color: var(--color-warning) !important;
    opacity: 0.85;
}

.sidebar-link.admin-link:hover {
    background: rgba(var(--warning-rgb), 0.08) !important;
    opacity: 1;
}

.sidebar-link.admin-link.active {
    color: var(--color-warning) !important;
    background: rgba(var(--warning-rgb), 0.12) !important;
    opacity: 1;
}

.sidebar-link svg {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
}

/* Sidebar badge (for counts, live indicators) */
.sidebar-badge {
    margin-left: auto;
    font-size: 0.65rem;
    font-weight: 700;
    padding: 0.0625rem 0.375rem;
    border-radius: 8px;
    line-height: 1.4;
    flex-shrink: 0;
}

.sidebar-badge-count {
    background: var(--accent);
    color: #fff;
}

.sidebar-badge-live {
    background: rgba(var(--success-rgb), 0.15);
    color: var(--green);
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.sidebar-badge-live::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--green);
    animation: sidebar-pulse 2s ease-in-out infinite;
}

@keyframes sidebar-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.sidebar-badge-warning {
    background: rgba(var(--warning-rgb), 0.15);
    color: var(--yellow);
}

/* Break indicator in sidebar */
.sidebar-break-active {
    color: var(--yellow) !important;
}

/* DM badge in sidebar */
.sidebar-dm-badge {
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    background: var(--accent);
    color: white;
    border-radius: 8px;
    font-size: 0.65rem;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    margin-left: auto;
    flex-shrink: 0;
}

/* Sidebar Divider */
.sidebar-divider {
    height: 1px;
    background: rgba(var(--glass-overlay-color), 0.12);
    margin: 0.375rem 0.75rem;
}

/* Sidebar Footer (user profile + theme strip) */
.sidebar-footer {
    border-top: 1px solid rgba(var(--glass-overlay-color), 0.08);
    padding: 0.5rem;
    flex-shrink: 0;
    background: linear-gradient(to top,
        rgba(var(--accent-rgb), 0.04) 0%,
        transparent 80%);
}

/* Legal footer — always visible at sidebar bottom (Privacy / Terms / Support).
   GDPR requires the privacy notice to be accessible at the point of data
   collection. Discreet placement matches the rest of the chrome. */
.sidebar-legal {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 0.3rem;
    padding: 0.6rem 0.5rem max(0.6rem, env(safe-area-inset-bottom, 0));
    font-size: 0.72rem;
    color: var(--text-muted, #94a3b8);
    border-top: 1px solid rgba(var(--glass-overlay-color), 0.06);
    flex-shrink: 0;
}
.sidebar-legal-link {
    color: var(--text-muted, #94a3b8);
    text-decoration: none;
    padding: 0.15rem 0.3rem;
    border-radius: 4px;
    transition: color 0.15s, background 0.15s;
}
.sidebar-legal-link:hover {
    color: var(--accent-light, #c4b5fd);
    background: rgba(var(--accent-rgb), 0.06);
}
.sidebar-legal-link:focus-visible {
    outline: 2px solid var(--accent, #7c3aed);
    outline-offset: 2px;
}
.sidebar-legal-dot { opacity: 0.5; }
/* Hide when sidebar is collapsed to icon-only state. */
.sidebar.collapsed .sidebar-legal { display: none; }

/* ─── Phase 2R rev2: always-visible theme swatch strip ───
   Two-row vertical layout: a header row with "THEME" + the active
   theme's name, then a wrapping grid of swatches that auto-fills to
   the sidebar's width — no truncation regardless of theme count or
   sidebar width. */
.sidebar-theme-strip {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    padding: 0.55rem 0.5rem 0.65rem;
    margin-bottom: 0.4rem;
    border-bottom: 1px solid rgba(var(--glass-overlay-color), 0.06);
}

.sidebar-theme-strip-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.15rem 0.1rem;
    width: 100%;
    background: transparent;
    border: 0;
    color: inherit;
    cursor: pointer;
    border-radius: 6px;
    transition: background-color 0.15s ease;
}

.sidebar-theme-strip-header:hover {
    background: rgba(var(--glass-overlay-color), 0.06);
}

.sidebar-theme-chevron {
    flex-shrink: 0;
    color: var(--text-muted);
    transition: transform 0.2s ease;
    margin-left: auto;
}

.sidebar-theme-chevron.open {
    transform: rotate(180deg);
}

/* When the swatch row is hidden, tighten the strip padding so the
   header sits flush against the user button below it. */
.sidebar-theme-strip.collapsed {
    padding-bottom: 0.45rem;
    gap: 0;
}

.sidebar-theme-strip-label {
    font-size: 0.6rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
    user-select: none;
}

/* Active theme name displayed alongside the THEME label so the host
   can see at a glance which skin they have on without hovering each
   swatch. */
.sidebar-theme-strip-active {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--accent-light);
    letter-spacing: 0.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-shadow: 0 0 8px rgba(var(--accent-rgb), 0.25);
}

/* Wrapping grid of swatches — auto-fills to whatever sidebar width
   is available. With 22px swatches + 8px gaps, fits 6 per row in a
   220px sidebar, 5 in a 200px container, etc. No more right-edge
   truncation. */
.sidebar-theme-swatches {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(22px, 1fr));
    gap: 0.4rem 0.4rem;
    padding: 0 0.05rem;
}

.sidebar-theme-swatch {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid rgba(var(--glass-overlay-color), 0.18);
    cursor: pointer;
    transition:
        transform 0.18s var(--spring-ease, cubic-bezier(0.34, 1.56, 0.64, 1)),
        border-color 0.18s,
        box-shadow 0.18s;
    padding: 0;
    background-clip: padding-box;
    position: relative;
    /* Center each swatch within its grid cell so the visual rhythm
       reads even when the row isn't fully populated. */
    margin: 0 auto;
}

.sidebar-theme-swatch:hover {
    transform: scale(1.18);
    border-color: rgba(var(--glass-overlay-color), 0.35);
}

.sidebar-theme-swatch.active {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(var(--accent-rgb), 0.3),
                0 0 14px rgba(var(--accent-rgb), 0.4);
    transform: scale(1.1);
}

.sidebar-theme-swatch.active::after {
    content: '✓';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--btn-accent-text, white);
    font-size: 0.65rem;
    font-weight: 900;
    text-shadow: 0 0 4px rgba(0,0,0,0.6);
}

/* Tooltip on hover (theme name) */
.sidebar-theme-swatch::before {
    content: attr(data-name);
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%) scale(0.9);
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid rgba(var(--glass-overlay-color), 0.15);
    padding: 0.25rem 0.55rem;
    border-radius: 6px;
    font-size: 0.7rem;
    font-weight: 600;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s, transform 0.15s;
    z-index: 200;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}
.sidebar-theme-swatch:hover::before {
    opacity: 1;
    transform: translateX(-50%) scale(1);
}

/* Theme strip hidden when sidebar is collapsed (icons-only mode) —
   the user can still hit the user-menu dropdown for it. */
@media (min-width: 961px) {
    .sidebar.collapsed .sidebar-theme-strip {
        display: none;
    }
}

.sidebar-user-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.625rem;
    width: 100%;
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-primary);
    transition: background 0.15s;
    text-align: left;
}

.sidebar-user-btn:hover {
    background: rgba(var(--glass-overlay-color), 0.12);
}

.sidebar-user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    /* Phase 2R: avatar gradient now flows through --accent + --color-secondary
       so it retints with the active skin (purple→cyan in midnight,
       magenta→cyan in Neon Arena, cyan→orange in Sports Hype). */
    background: linear-gradient(135deg, var(--accent) 0%, var(--color-secondary, var(--cyan)) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.75rem;
    color: var(--btn-accent-text, white);
    flex-shrink: 0;
    box-shadow: 0 0 12px rgba(var(--accent-rgb), 0.25);
}

.sidebar-user-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 6px;
    object-fit: cover;
}

.sidebar-user-info {
    flex: 1;
    min-width: 0;
}

.sidebar-user-name {
    font-weight: 600;
    font-size: 0.8125rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-user-role {
    font-size: 0.6875rem;
    color: var(--text-muted);
    text-transform: capitalize;
}

/* Sidebar User Menu (dropdown above user button) */
.sidebar-user-menu {
    position: absolute;
    bottom: calc(100% + 0.375rem);
    left: 0.5rem;
    right: 0.5rem;
    background: var(--bg-secondary);
    border: 1px solid rgba(var(--glass-overlay-color), 0.1);
    border-radius: 10px;
    padding: 0.5rem;
    box-shadow: 0 -8px 32px rgba(var(--glass-overlay-color-inv), 0.4);
    z-index: 300;
}

/* Theme picker inside user dropdown */
.dropdown-theme-section {
    position: relative;
}
.dropdown-theme-section .dropdown-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.dropdown-theme-panel {
    background: var(--bg-secondary, #111827);
    border: 1px solid rgba(var(--glass-overlay-color), 0.12);
    border-radius: 8px;
    margin: 0.25rem 0;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    max-height: 240px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: rgba(var(--glass-overlay-color), 0.12) transparent;
}

/* ---- Slim Topbar ---- */
.topbar {
    position: sticky;
    top: 0;
    z-index: 150;
    height: 48px;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0 1.25rem;
    background: var(--navbar-bg);
    backdrop-filter: var(--backdrop-blur);
    border-bottom: var(--navbar-border);
}

.topbar-title {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-spacer {
    flex: 1;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    flex-shrink: 0;
}

/* Topbar icon buttons (search, notifications, soundboard) */
.topbar-icon-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 7px;
    transition: all 0.15s;
}

.topbar-icon-btn:hover {
    color: var(--text-primary);
    background: rgba(var(--glass-overlay-color), 0.12);
}

.topbar-icon-btn.has-unread {
    color: var(--accent);
}

/* Topbar +New button */
.topbar-new-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.3125rem 0.75rem;
    background: linear-gradient(135deg, var(--purple) 0%, var(--cyan) 100%);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.8125rem;
    transition: all 0.15s;
    white-space: nowrap;
}

.topbar-new-btn:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(var(--accent-rgb), 0.3);
}

/* Topbar search hint */
.topbar-search-hint {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.5rem;
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.15s;
}

.topbar-search-hint:hover {
    color: var(--text-secondary);
    background: rgba(var(--glass-overlay-color), 0.12);
}

.topbar-search-hint kbd {
    font-family: inherit;
    font-size: 0.6875rem;
    padding: 0.0625rem 0.375rem;
    background: rgba(var(--glass-overlay-color), 0.12);
    border: 1px solid rgba(var(--glass-overlay-color), 0.1);
    border-radius: 4px;
    color: var(--text-muted);
}

/* Topbar +New dropdown */
.topbar-new-panel {
    position: absolute;
    top: calc(100% + 0.375rem);
    right: 0;
    min-width: 200px;
    background: var(--bg-secondary);
    border: 1px solid rgba(var(--glass-overlay-color), 0.1);
    border-radius: 8px;
    padding: 0.375rem;
    box-shadow: 0 10px 40px rgba(var(--glass-overlay-color-inv), 0.5);
    z-index: 200;
}

.topbar-new-panel .sidebar-link {
    margin: 0;
    width: 100%;
    padding: 0.5rem 0.75rem;
}

/* ---- All non-sidebar children go to column 2 ---- */
body.has-sidebar > *:not(.sidebar):not(.sidebar-backdrop) {
    grid-column: 2;
}

/* Teleport mount-points: content teleports to <body>, so these must not consume grid rows.
   position:absolute removes them from grid flow while keeping them mountable by Vue. */
body.has-sidebar > #command-palette,
body.has-sidebar > #soundboard-app,
body.has-sidebar > #venue-controls-app {
    position: absolute;
}

/* Content area — always in the 1fr row */
body.has-sidebar .content {
    min-height: 0;
    padding-top: 1.5rem;
    overflow: hidden;
}

/* Full-height editor pages: pass grid height through the .content → #vue-app chain */
body.has-sidebar .content.content--full-height {
    padding-top: 0;
    overflow: visible;
}
body.has-sidebar .content.content--full-height #vue-app {
    height: 100%;
}

/* ---- Notification dropdown in topbar ---- */
.topbar-notif-wrapper {
    position: relative;
}

/* ---- Break Mode button in topbar ---- */
.topbar-break-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.3rem 0.5rem;
    height: 34px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 7px;
    transition: all 0.2s;
    font-size: 0.75rem;
    font-weight: 600;
    white-space: nowrap;
}

.topbar-break-btn:hover {
    color: var(--text-primary);
    background: rgba(var(--glass-overlay-color), 0.08);
}

.topbar-break-btn.break-active {
    color: var(--red, #ef4444);
    background: rgba(239, 68, 68, 0.12);
    animation: break-pulse 2s ease-in-out infinite;
}

.topbar-break-btn.break-active:hover {
    background: rgba(239, 68, 68, 0.2);
}

@keyframes break-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.3); }
    50% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
}

.break-topbar-label {
    font-size: 0.75rem;
    font-weight: 600;
}

/* Mobile toggle: hidden by default, shown via media query */
.topbar-mobile-toggle {
    display: none;
}

/* CSS-only hamburger icon */
.hamburger-icon {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 20px;
    height: 20px;
}

.hamburger-icon span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text-secondary);
    border-radius: 1px;
    transition: transform 0.2s ease, opacity 0.2s ease;
}

/* Animate to X when open */
.topbar-mobile-toggle.menu-open .hamburger-icon span:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
}

.topbar-mobile-toggle.menu-open .hamburger-icon span:nth-child(2) {
    opacity: 0;
}

.topbar-mobile-toggle.menu-open .hamburger-icon span:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
}

/* ---- Responsive: Sidebar collapse ---- */
@media (max-width: 960px) {
    body.has-sidebar {
        grid-template-columns: minmax(0, 1fr);
    }

    /* Phase 2R: mobile drawer — wider tap target (260px), spring
       easing on slide, deeper backdrop blur, and subtle accent halo
       on the right edge of the drawer to mark "this is a panel". */
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(260px, 80vw);
        transform: translateX(-100%);
        transition: transform 0.32s cubic-bezier(0.34, 1.2, 0.64, 1);
        z-index: 1000;
        box-shadow: 12px 0 32px rgba(0, 0, 0, 0.45),
                    1px 0 0 rgba(var(--accent-rgb), 0.15);
    }

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

    .sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 999;
        background: rgba(var(--glass-overlay-color-inv), 0.65);
        backdrop-filter: blur(8px);
        -webkit-backdrop-filter: blur(8px);
        opacity: 0;
        transition: opacity 0.25s ease;
    }

    .sidebar-backdrop.open {
        display: block;
        opacity: 1;
    }

    /* Mobile-only close button on the brand row */
    .sidebar-mobile-close {
        display: inline-flex;
    }

    body.has-sidebar > *:not(.sidebar):not(.sidebar-backdrop) {
        grid-column: 1;
    }

    .topbar-mobile-toggle {
        display: inline-flex !important;
    }

    .topbar-new-btn span.btn-label,
    .break-topbar-label,
    .stage-control-label {
        display: none;
    }

    .topbar {
        z-index: 150; /* stays below mobile sidebar (1000) + backdrop (999) */
    }

    body.has-sidebar.sidebar-open {
        overflow: hidden;
    }
}

@media (min-width: 961px) {
    .sidebar-backdrop {
        display: none !important;
    }

    .topbar-mobile-toggle {
        display: none !important;
    }

    /* Phase 2R: mobile-only close button — hidden on desktop (the
       collapse caret handles that surface). */
    .sidebar-mobile-close {
        display: none !important;
    }
}

/* Mobile-only close button styling */
.sidebar-mobile-close {
    width: 36px;
    height: 36px;
    border: none;
    background: rgba(var(--glass-overlay-color), 0.06);
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-right: 0.5rem;
    transition: background 0.15s, color 0.15s, transform 0.15s var(--spring-ease, cubic-bezier(0.34, 1.56, 0.64, 1));
}
.sidebar-mobile-close:hover {
    background: rgba(var(--glass-overlay-color), 0.12);
    color: var(--text-primary);
}
.sidebar-mobile-close:active { transform: scale(0.92); }

/* ---- Theme selector in sidebar ---- */
.sidebar-theme-wrapper {
    padding: 0.375rem 0.5rem;
}

.sidebar-theme-btn {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    width: 100%;
    padding: 0.45rem 0.625rem;
    border: none;
    border-radius: 8px;
    background: rgba(var(--glass-overlay-color), 0.08);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.8rem;
    transition: background 0.15s, color 0.15s;
}

.sidebar-theme-btn:hover {
    background: rgba(var(--glass-overlay-color), 0.08);
    color: var(--text-primary);
}

.sidebar-theme-btn-swatch {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    flex-shrink: 0;
    border: 2px solid rgba(var(--glass-overlay-color), 0.15);
}

.sidebar-theme-btn-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Theme picker popup panel */
.sidebar-theme-panel {
    position: absolute;
    bottom: calc(100% + 0.5rem);
    left: 0.5rem;
    width: 240px;
    background: var(--bg-secondary, #111827);
    border: 1px solid rgba(var(--glass-overlay-color), 0.12);
    border-radius: 12px;
    padding: 0.75rem;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    z-index: 200;
}

.theme-panel-header {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.5rem;
    padding: 0 0.25rem;
}

.theme-panel-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.25rem;
}

.theme-panel-option {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.5rem;
    border: 1.5px solid transparent;
    border-radius: 6px;
    background: rgba(var(--glass-overlay-color), 0.08);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.78rem;
    transition: all 0.15s;
    width: 100%;
}

.theme-panel-option:hover {
    background: rgba(var(--glass-overlay-color), 0.08);
    color: var(--text-primary);
}

.theme-panel-option.active {
    border-color: var(--accent);
    background: rgba(var(--accent-rgb), 0.08);
    color: var(--text-primary);
}

.theme-panel-swatch {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    flex-shrink: 0;
    border: 2px solid rgba(var(--glass-overlay-color), 0.1);
}

.theme-panel-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============================================================
   COLLAPSED SIDEBAR (desktop only)
   ============================================================ */

/* Collapse toggle button */
.sidebar-collapse-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    background: none;
    border: none;
    border-radius: 6px;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s;
    flex-shrink: 0;
    font-size: 0.875rem;
    margin-right: 0.5rem;
}

.sidebar-collapse-btn:hover {
    color: var(--text-primary);
    background: rgba(var(--glass-overlay-color), 0.12);
}

.sidebar-collapse-icon {
    transition: transform 0.25s;
    font-size: 1rem;
    line-height: 1;
}

.sidebar-collapse-icon.flipped {
    transform: rotate(180deg);
}

/* Sidebar icon (SVG) base styles */
.sidebar-emoji {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}
.sidebar-emoji .sidebar-icon {
    width: 18px;
    height: 18px;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Transition for sidebar width — smooth ease-out so the rail glides open
   on hover instead of snapping. will-change keeps the width animation on
   its own layer to avoid repaint jank. */
@media (min-width: 961px) {
    .sidebar {
        transition: width 0.26s cubic-bezier(0.22, 1, 0.36, 1),
                    box-shadow 0.26s cubic-bezier(0.22, 1, 0.36, 1);
        will-change: width;
    }

    body.has-sidebar {
        transition: grid-template-columns 0.26s cubic-bezier(0.22, 1, 0.36, 1);
    }

    /* Collapsed state */
    body.sidebar-collapsed.has-sidebar {
        grid-template-columns: 56px minmax(0, 1fr);
    }

    .sidebar.collapsed {
        width: 56px;
    }

    /* Hover-expand: widen the sidebar to its full size without
       reflowing the body grid. The body keeps `sidebar-collapsed`
       so column 1 stays 56px wide; the sidebar overflows its grid
       cell visually, floating on top of the main content. Pinning
       the sidebar open (chevron click) still reflows the grid —
       only the hover transition stays cheap. */
    body.sidebar-collapsed .sidebar.hover-expanded {
        width: 220px;
        box-shadow: 8px 0 32px rgba(0, 0, 0, 0.35),
                    0 0 1px rgba(var(--accent-rgb), 0.2);
    }

    /* Labels FADE out (instead of display:none popping) and clip via the
       link's own overflow:hidden, so they glide with the rail on
       hover-expand instead of snapping. Section + sub-labels collapse fully
       (they carry no icon, so nothing to keep visible). */
    .sidebar.collapsed .sidebar-link-text {
        opacity: 0;
    }
    .sidebar.collapsed .sidebar-label-text,
    .sidebar.collapsed .sidebar-sub-label {
        display: none;
    }

    /* Hide entire sections that don't make sense collapsed */
    .sidebar.collapsed .sidebar-hide-collapsed {
        display: none;
    }

    /* Hide theme swatches when collapsed */
    .sidebar.collapsed .sidebar-theme-wrapper {
        display: none;
    }

    /* Hide the divider above user btn when collapsed */
    .sidebar.collapsed .sidebar-footer .sidebar-divider {
        display: none;
    }

    /* Collapsed: keep links LEFT-aligned (do NOT re-center). The icon then
       stays at the same x-position as when expanded, so widening the rail on
       hover reveals the faded label with zero sideways icon jump — the main
       source of the old "klunky" reveal. */

    .sidebar.collapsed .sidebar-emoji {
        width: auto;
    }
    .sidebar.collapsed .sidebar-emoji .sidebar-icon {
        width: 20px;
        height: 20px;
    }

    /* Section labels become thin dividers when collapsed */
    .sidebar.collapsed .sidebar-section-label {
        height: 1px;
        padding: 0;
        margin: 0.375rem 0.75rem;
        background: rgba(var(--glass-overlay-color), 0.12);
        overflow: hidden;
        font-size: 0;
    }

    /* Section toggles (Admin / Host Tools) also stay left-aligned when
       collapsed so their icon matches the links and doesn't jump. */

    /* Hide admin submenu when sidebar is collapsed */
    .sidebar.collapsed .sidebar-submenu {
        display: none;
    }

    /* Brand: center icon when collapsed */
    .sidebar.collapsed .sidebar-brand-row {
        justify-content: center;
        position: relative;
    }
    .sidebar.collapsed .sidebar-brand {
        justify-content: center;
        padding: 0.75rem 0.5rem;
        flex: initial;
    }
    .sidebar.collapsed .sidebar-collapse-btn {
        position: absolute;
        right: -12px;
        top: 50%;
        transform: translateY(-50%);
        width: 24px;
        height: 24px;
        border-radius: 50%;
        background: var(--bg-secondary, #1a1a2e);
        border: 1px solid rgba(var(--glass-overlay-color), 0.12);
        margin-right: 0;
        font-size: 0.75rem;
        z-index: 10;
        box-shadow: 0 1px 4px rgba(0,0,0,0.3);
    }

    /* User footer: show only avatar */
    .sidebar.collapsed .sidebar-user-btn {
        justify-content: center;
        padding: 0.5rem;
    }

    .sidebar.collapsed .sidebar-user-info,
    .sidebar.collapsed .sidebar-user-btn svg {
        display: none;
    }

    /* DM badge: overlay on emoji when collapsed */
    .sidebar.collapsed .sidebar-dm-badge {
        position: absolute;
        top: 0;
        right: 2px;
        min-width: 14px;
        height: 14px;
        font-size: 0.55rem;
        padding: 0 3px;
    }

    .sidebar.collapsed .sidebar-link {
        position: relative;
    }

    /* Recent game status badge hidden when collapsed */
    .sidebar.collapsed .recent-game-status {
        display: none;
    }

    /* Badge (live, count) adjustments */
    .sidebar.collapsed .sidebar-badge {
        display: none;
    }

    /* ─── Phase 2R: animated tooltips on collapsed-mode icons ───
       Reads the link's `title` attribute via `attr(title)` and floats a
       theme-aware tooltip to the right of the icon on hover. The
       `attr()` content function is widely supported. */
    .sidebar.collapsed .sidebar-link::after,
    .sidebar.collapsed .sidebar-section-toggle::after {
        content: attr(title);
        position: absolute;
        left: calc(100% + 8px);
        top: 50%;
        transform: translateY(-50%) translateX(-4px);
        background: var(--bg-secondary);
        color: var(--text-primary);
        border: 1px solid rgba(var(--accent-rgb), 0.25);
        padding: 0.4rem 0.7rem;
        border-radius: 8px;
        font-size: 0.78rem;
        font-weight: 600;
        white-space: nowrap;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.15s, transform 0.15s var(--spring-ease, cubic-bezier(0.34, 1.56, 0.64, 1));
        box-shadow:
            0 4px 18px rgba(0, 0, 0, 0.4),
            0 0 12px rgba(var(--accent-rgb), 0.2);
        z-index: 250;
    }
    .sidebar.collapsed .sidebar-link:hover::after,
    .sidebar.collapsed .sidebar-section-toggle:hover::after {
        opacity: 1;
        transform: translateY(-50%) translateX(0);
    }

    /* Tooltip caret pointing back to the icon */
    .sidebar.collapsed .sidebar-link::before,
    .sidebar.collapsed .sidebar-section-toggle::before {
        content: '';
        position: absolute;
        left: calc(100% + 4px);
        top: 50%;
        transform: translateY(-50%);
        width: 0;
        height: 0;
        border-top: 5px solid transparent;
        border-bottom: 5px solid transparent;
        border-right: 5px solid var(--bg-secondary);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.15s 0.05s;
        z-index: 251;
    }
    .sidebar.collapsed .sidebar-link:hover::before,
    .sidebar.collapsed .sidebar-section-toggle:hover::before {
        opacity: 1;
    }

    /* Reduced-motion users: tooltip still appears but no slide, and the
       rail width / label fade snap instantly instead of animating. */
    @media (prefers-reduced-motion: reduce) {
        .sidebar.collapsed .sidebar-link::after,
        .sidebar.collapsed .sidebar-section-toggle::after {
            transition: opacity 0.15s;
            transform: translateY(-50%);
        }
        .sidebar,
        body.has-sidebar,
        .sidebar-link-text {
            transition: none;
        }
    }
}

/* On mobile, never show collapsed state — use the slide-in drawer */
@media (max-width: 960px) {
    .sidebar-collapse-btn {
        display: none;
    }

    .sidebar.collapsed {
        width: 220px;
    }
}

/* Lock body scroll while the mobile sidebar drawer is open so touch-drags
   inside the drawer don't cascade to the page underneath. The Alpine
   watcher in templates/base.html toggles this class. Only meaningful on
   small viewports where the drawer is the active overlay. */
@media (max-width: 960px) {
    body.drawer-open {
        overflow: hidden;
    }
}
