/* ══════════════════════════════════════════════════════════════
   SIDEBAR — Collapsible left navigation for desktop command center
   States: expanded (240px) | collapsed icon rail (64px) | hidden (mobile)
   ══════════════════════════════════════════════════════════════ */

/* ── Sidebar Overlay (hidden by default, shown on tablet when sidebar opens) ── */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 99;
}
.sidebar-overlay.active {
    display: block;
}

/* ── App Shell Grid ──
   --shell-cols is set by media queries below; .app-shell reads it once.
   No selector can ever override grid-template-columns — specificity wars eliminated. */
.app-shell {
    display: grid;
    grid-template-columns: var(--shell-cols, var(--sidebar-width) 1fr);
    min-height: 100dvh;
}

/* ── Sidebar Container ── */
.app-sidebar {
    position: sticky;
    top: 0;
    height: 100dvh;
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 100;
    contain: layout style;
    will-change: width;
}
body.sidebar-collapsed .app-sidebar {
    width: var(--sidebar-collapsed-width);
}

/* ── Brand Area ── */
.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px 20px 16px;
    border-bottom: 1px solid var(--border-subtle);
    min-height: 60px;
    flex-shrink: 0;
}
.sidebar-brand-logo {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    color: var(--text);
}
.sidebar-brand-text {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 1;
    transition: opacity 0.2s ease;
}
body.sidebar-collapsed .sidebar-brand-text {
    opacity: 0;
    width: 0;
    pointer-events: none;
}
.sidebar-brand-name {
    font-size: 16px;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
    line-height: 1.2;
}
.sidebar-brand-sub {
    font-size: 11px;
    color: var(--text-tertiary);
    white-space: nowrap;
}
body.sidebar-collapsed .sidebar-brand {
    padding: 20px 14px 16px;
    justify-content: center;
}

/* ── Navigation ── */
.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px 8px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.sidebar-nav-group {
    margin-bottom: 16px;
}
.sidebar-nav-label {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: var(--text-tertiary);
    padding: 0 12px;
    margin-bottom: 6px;
    white-space: nowrap;
    overflow: hidden;
    opacity: 1;
    transition: opacity 0.2s ease;
}
body.sidebar-collapsed .sidebar-nav-label {
    opacity: 0;
    height: 0;
    margin: 0;
    padding: 0;
    pointer-events: none;
}

/* ── Nav Items ── */
.sidebar-nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 10px;
    cursor: pointer;
    color: var(--text-secondary);
    text-decoration: none;
    position: relative;
    transition: background 0.2s ease, color 0.2s ease;
    white-space: nowrap;
    overflow: hidden;
    width: 100%;
    box-sizing: border-box;
    background: transparent;
    border: none;
    height: auto;
    font-size: 14px;
}
.sidebar-nav-item:hover {
    background: rgba(255, 180, 80, 0.08);
    color: var(--text);
}
body.dark-mode .sidebar-nav-item:hover {
    background: rgba(255, 255, 255, 0.06);
}
/* Override [data-tooltip]:hover bleed from components.css — prevents the
   blue help-badge hover treatment (background: var(--apple-blue)) from
   applying to sidebar nav items that carry data-tooltip for collapsed tooltips. */
.sidebar-nav-item[data-tooltip]:hover {
    background: rgba(255, 180, 80, 0.08);
    color: var(--text);
    border-color: transparent;
}
body.dark-mode .sidebar-nav-item[data-tooltip]:hover {
    background: rgba(255, 255, 255, 0.06);
}
/* Active items must keep their highlight on hover — the broad dark-mode rule above
   (specificity 0-4-1) would otherwise override .sidebar-nav-item.active (0-2-0).
   These rules beat both the broad hover override and [data-tooltip]:hover. */
.sidebar-nav-item.active[data-tooltip]:hover {
    background: var(--sidebar-active);
}
body.dark-mode .sidebar-nav-item.active[data-tooltip]:hover {
    background: var(--sidebar-active);
}
/* Also hide the [data-tooltip]::before arrow (from components.css popover system)
   — sidebar uses its own ::after tooltip positioned to the right, never a top arrow. */
.sidebar-nav-item[data-tooltip]::before {
    display: none;
}
.sidebar-nav-item.active {
    background: var(--sidebar-active);
    color: var(--text);
    font-weight: 600;
}
/* Active indicator bar — opacity and border MUST be set here to override
   [data-tooltip]::before { opacity: 0; border: 6px solid transparent } from components.css,
   otherwise the bar is invisible at rest and pops in on hover via [data-tooltip]:hover::before. */
.sidebar-nav-item.active::before {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 24px;
    border-radius: 0 3px 3px 0;
    border: none;
    opacity: 1;
    background: var(--accent-gradient);
}
body.dark-mode .sidebar-nav-item.active::before {
    background: var(--accent-gradient);
}
/* Prevent [data-tooltip]:hover::before (components.css) from changing bar opacity on hover */
.sidebar-nav-item.active[data-tooltip]:hover::before {
    opacity: 1;
}

/* Sidebar Icons */
.sidebar-nav-icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}
.sidebar-nav-icon svg {
    width: 20px;
    height: 20px;
    stroke-width: 1.75;
    stroke: currentColor;
    fill: none;
}
.sidebar-nav-item.active .sidebar-nav-icon svg {
    stroke-width: 2;
}

/* Nav Item Label */
.sidebar-nav-item-label {
    font-size: 14px;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    opacity: 1;
    transition: opacity 0.2s ease;
}
body.sidebar-collapsed .sidebar-nav-item-label {
    opacity: 0;
    width: 0;
    pointer-events: none;
}
body.sidebar-collapsed .sidebar-nav-item {
    padding: 10px;
    justify-content: center;
}

/* Badge in sidebar */
.sidebar-badge {
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--danger);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    opacity: 1;
    transition: opacity 0.2s ease;
}
.sidebar-badge:empty {
    display: none;
}
body.sidebar-collapsed .sidebar-badge {
    /* Show as dot in collapsed mode */
    position: absolute;
    top: 6px;
    right: 6px;
    width: 8px;
    height: 8px;
    min-width: 8px;
    padding: 0;
    font-size: 0;
    border-radius: 50%;
    opacity: 1;
}
body.sidebar-collapsed .sidebar-badge:empty {
    display: none;
}

/* ── Sidebar Tooltip (collapsed mode) ── */
.sidebar-nav-item[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    transform: translateY(-50%);
    padding: 6px 12px;
    border-radius: 8px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    box-shadow: 0 4px 16px var(--shadow);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 1000;
}
body.sidebar-collapsed .sidebar-nav-item:hover[data-tooltip]::after {
    opacity: 1;
}
body:not(.sidebar-collapsed) .sidebar-nav-item[data-tooltip]::after {
    display: none;
}

/* ── Sidebar Footer ── */
.sidebar-footer {
    border-top: 1px solid var(--border-subtle);
    padding: 12px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.sidebar-collapse-toggle {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 10px;
    cursor: pointer;
    color: var(--text-secondary);
    background: none;
    border: none;
    font-size: 14px;
    font-weight: 500;
    width: 100%;
    transition: background 0.2s ease, color 0.2s ease;
    white-space: nowrap;
    overflow: hidden;
}
.sidebar-collapse-toggle:hover {
    background: rgba(255, 180, 80, 0.08);
    color: var(--text);
}
body.dark-mode .sidebar-collapse-toggle:hover {
    background: rgba(255, 255, 255, 0.06);
}
.sidebar-collapse-toggle svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.75;
    transition: transform 0.3s var(--transition-smooth);
}
body.sidebar-collapsed .sidebar-collapse-toggle svg {
    transform: rotate(180deg);
}
.sidebar-collapse-label {
    opacity: 1;
    transition: opacity 0.2s ease;
}
body.sidebar-collapsed .sidebar-collapse-label {
    opacity: 0;
    width: 0;
    pointer-events: none;
}
body.sidebar-collapsed .sidebar-collapse-toggle {
    justify-content: center;
    padding: 10px;
}

/* Footer action buttons (theme toggle, user) */
.sidebar-footer-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
}
body.sidebar-collapsed .sidebar-footer-actions {
    flex-direction: column;
    padding: 6px;
}
.sidebar-footer-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease;
    flex-shrink: 0;
}
.sidebar-footer-btn:hover {
    background: rgba(255, 180, 80, 0.08);
    color: var(--text);
}
body.dark-mode .sidebar-footer-btn:hover {
    background: rgba(255, 255, 255, 0.06);
}
.sidebar-footer-btn svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.75;
}
.sidebar-user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--bg-input);
    border: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    flex-shrink: 0;
    transition: border-color 0.2s ease;
}
.sidebar-user-avatar:hover {
    border-color: var(--apple-blue);
}

/* ── Main Content Area ── */
.app-main {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    min-width: 0;
    background: var(--bg);
    overflow-x: hidden;
}

/* ── Top Header Bar ── */
.app-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-card);
    flex-shrink: 0;
    z-index: 50;
    position: sticky;
    top: 0;
}
.header-breadcrumb {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    color: var(--text-secondary);
}
.header-breadcrumb a {
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s ease;
}
.header-breadcrumb a:hover {
    color: var(--text);
}
.header-breadcrumb .breadcrumb-separator {
    color: var(--text-tertiary);
    font-size: 12px;
}
.header-breadcrumb .breadcrumb-current {
    color: var(--text);
    font-weight: 600;
}
.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}
.header-greeting {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
}
.header-greeting .greeting-name {
    font-weight: 700;
    background: var(--accent-gradient-text);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.header-notification-btn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease;
}
.header-notification-btn:hover {
    background: var(--bg-input);
    color: var(--text);
}
.header-notification-btn svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.75;
}
.header-notification-badge {
    position: absolute;
    top: 4px;
    right: 4px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    background: var(--danger);
    color: #fff;
    font-size: 9px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}
.header-notification-badge:empty {
    display: none;
}

/* ── App Content Area ── */
.app-content {
    flex: 1;
    overflow-y: auto;
    position: relative;
}

/* ── Plugin Viewport ── */
.plugin-viewport {
    display: none;
}
.plugin-viewport.active {
    display: block;
}

/* ── Merge Plugin Headers with App Shell ──
   The app-header breadcrumb bar already shows the plugin name + navigation,
   so plugin-level headers are reduced to just their action buttons (if any).
   Progress bars (.progress-track) are preserved. */
.app-shell .plugin-container header {
    position: static;
    background: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    padding: 8px 20px 0;
    border: none;
    box-shadow: none;
    border-radius: 0;
    margin: 0;
    z-index: auto;
    min-height: 0;
}
.app-shell .plugin-container header::after {
    display: none;
}
.app-shell .plugin-container header .tool-header-brand {
    display: none;
}
.app-shell .plugin-container header .step-title {
    display: none;
}
.app-shell .plugin-container header .header-top {
    margin-bottom: 0;
    justify-content: flex-end;
}
/* Collapse header-top when brand is the only child (no action buttons) */
.app-shell .plugin-container header .tool-header-brand:only-child {
    margin: 0;
    padding: 0;
}
.app-shell .plugin-container header .header-top:has(> :only-child) {
    display: none;
    padding: 0;
}
/* Remove plugin header padding when fully collapsed */
.app-shell .plugin-container header:has(.header-top:has(> :only-child)):not(:has(.progress-track)) {
    padding: 0;
}
/* Hide the floating "← Exit Tool" button — sidebar handles navigation */
.app-shell #backToHome {
    display: none !important;
}

/* ── Mobile Bottom Nav (≤768px) ── */
.mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(56px + env(safe-area-inset-bottom));
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    background: var(--bg-sidebar);
    backdrop-filter: blur(20px) saturate(1.4);
    -webkit-backdrop-filter: blur(20px) saturate(1.4);
    border-top: 1px solid var(--border);
    min-width: 0;
}
.mobile-bottom-nav-inner {
    display: flex;
    flex: 1;
    min-height: 0;
    align-items: center;
    justify-content: space-around;
    height: 56px;
}
.mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 12px;
    color: var(--text-secondary);
    font-size: 10px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: color 0.2s ease;
    border: none;
    background: none;
}
.mobile-nav-item.active {
    color: var(--apple-blue);
}
.mobile-nav-item svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.75;
}

/* ── Mobile Hamburger ── */
.mobile-menu-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
}
.mobile-menu-btn svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.75;
}

/* ═══════════════════════════════════════
   Responsive Breakpoints
   --shell-cols state machine — one variable, set on :root/body, read once by .app-shell
   ═══════════════════════════════════════ */

/* ≥1280px — Full layout, sidebar expanded by default */
@media (min-width: 1280px) {
    :root { --shell-cols: var(--sidebar-width) 1fr; }
    body.sidebar-collapsed { --shell-cols: var(--sidebar-collapsed-width) 1fr; }
}

/* 1024px–1279px — Sidebar auto-collapsed to icon rail */
@media (min-width: 1024px) and (max-width: 1279px) {
    :root { --shell-cols: var(--sidebar-collapsed-width) 1fr; }
    .app-sidebar {
        width: var(--sidebar-collapsed-width);
    }
    .sidebar-brand-text,
    .sidebar-nav-label,
    .sidebar-nav-item-label,
    .sidebar-collapse-label {
        opacity: 0;
        width: 0;
        pointer-events: none;
    }
    .sidebar-brand {
        padding: 20px 14px 16px;
        justify-content: center;
    }
    .sidebar-nav-item {
        padding: 10px;
        justify-content: center;
    }
    .sidebar-collapse-toggle {
        justify-content: center;
        padding: 10px;
    }
    .sidebar-collapse-toggle svg {
        transform: rotate(180deg);
    }
    .sidebar-badge {
        position: absolute;
        top: 6px;
        right: 6px;
        width: 8px;
        height: 8px;
        min-width: 8px;
        padding: 0;
        font-size: 0;
        border-radius: 50%;
    }
    /* Show tooltips */
    .sidebar-nav-item:hover[data-tooltip]::after {
        opacity: 1;
    }
}

/* 768px–1023px — Sidebar hidden, hamburger in header */
@media (min-width: 768px) and (max-width: 1023px) {
    :root { --shell-cols: 1fr; }
    .app-sidebar {
        position: fixed;
        left: -260px;
        top: 0;
        width: var(--sidebar-width);
        transition: left 0.3s var(--transition-smooth);
        box-shadow: none;
    }
    .app-sidebar.mobile-open {
        left: 0;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
    }
    .mobile-menu-btn {
        display: flex;
    }
}

/* <768px — Mobile: no sidebar, bottom nav */
@media (max-width: 767px) {
    :root { --shell-cols: 1fr; }
    .app-sidebar {
        display: none;
    }
    .mobile-bottom-nav {
        display: block;
    }
    .app-content {
        padding-bottom: calc(56px + env(safe-area-inset-bottom));
    }
    .app-header {
        padding: 0 16px;
    }

    /* Dark mode mobile: improve header and bottom nav visibility */
    body.dark-mode .app-header {
        background: #1C1C1E;
        border-bottom-color: rgba(255, 255, 255, 0.10);
        box-shadow: 0 1px 0 rgba(255, 255, 255, 0.05);
    }
    body.dark-mode .mobile-bottom-nav {
        background: #1C1C1E;
        border-top-color: rgba(255, 255, 255, 0.10);
        box-shadow: 0 -1px 0 rgba(255, 255, 255, 0.05);
    }
    /* Dark mode mobile: subtle bg to lift content off pure-black body */
    body.dark-mode .app-content {
        background: #0D0D0D;
    }
}
