/**
 * DH Suite — App Switcher Component
 *
 * Dropdown grid for cross-app navigation (Google Workspace style).
 * Triggered by a grid icon in the unified header. Shows only apps
 * the user has roles for. Highlights the currently active app.
 *
 * Behavior:
 *   - Desktop: dropdown panel anchored to trigger button
 *   - Mobile (<768px): full-screen modal with 44x44px touch targets
 *   - Keyboard: arrow key navigation, Escape to close, focus trapping
 *   - No-JS fallback: renders as plain <nav> with stacked links
 *
 * HTML structure:
 *   <div class="app-switcher">
 *     <button class="app-switcher-trigger" aria-expanded="false"
 *             aria-controls="app-switcher-panel" aria-label="Switch app">
 *       <svg>…grid icon…</svg>
 *     </button>
 *     <div class="app-switcher-panel" id="app-switcher-panel"
 *          role="menu" aria-label="Applications">
 *       <nav class="app-switcher-grid">
 *         <a href="/reach/" class="app-switcher-item active"
 *            role="menuitem">
 *           <span class="app-switcher-icon">📞</span>
 *           <span class="app-switcher-label">Reach</span>
 *           <span class="app-switcher-desc">Intake Portal</span>
 *         </a>
 *         <a href="/respond/" class="app-switcher-item" role="menuitem">
 *           <span class="app-switcher-icon">🔧</span>
 *           <span class="app-switcher-label">Respond</span>
 *           <span class="app-switcher-desc">Field Ops</span>
 *         </a>
 *         <a href="/rally/" class="app-switcher-item" role="menuitem">
 *           <span class="app-switcher-icon">👥</span>
 *           <span class="app-switcher-label">Rally</span>
 *           <span class="app-switcher-desc">Volunteers</span>
 *         </a>
 *       </nav>
 *     </div>
 *   </div>
 *
 * JS wiring lives in shared/static/js/app-switcher.js.
 */

@layer components {

/* ==========================================================================
   Container
   ========================================================================== */

.app-switcher {
    position: relative;
}

/* ==========================================================================
   Trigger Button
   ========================================================================== */

.app-switcher-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-md);
    color: var(--color-gray-600);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.app-switcher-trigger:hover {
    background-color: var(--color-gray-100);
    color: var(--color-gray-800);
}

.app-switcher-trigger:focus-visible {
    outline: var(--focus-ring);
    outline-offset: var(--focus-ring-offset);
}

.app-switcher-trigger[aria-expanded="true"] {
    background-color: var(--color-gray-100);
    color: var(--color-gray-900);
}

.app-switcher-trigger svg {
    width: 20px;
    height: 20px;
}

/* ==========================================================================
   Dropdown Panel
   ========================================================================== */

.app-switcher-panel {
    display: none;
    position: absolute;
    top: calc(100% + var(--spacing-2));
    right: 0;
    min-width: 280px;
    background-color: white;
    border: 1px solid var(--color-gray-200);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: var(--spacing-4);
    z-index: 200;
}

.app-switcher-panel.open {
    display: block;
}

/* ==========================================================================
   App Grid
   ========================================================================== */

.app-switcher-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--spacing-2);
}

/* ==========================================================================
   App Item
   ========================================================================== */

.app-switcher-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-1);
    padding: var(--spacing-3);
    border-radius: var(--radius-md);
    text-decoration: none;
    color: var(--color-gray-700);
    transition: all var(--transition-fast);
    text-align: center;
    min-height: 44px;
}

.app-switcher-item:hover {
    background-color: var(--color-gray-50);
    text-decoration: none;
    color: var(--color-gray-900);
}

.app-switcher-item:focus-visible {
    outline: var(--focus-ring);
    outline-offset: calc(-1 * var(--focus-ring-offset, 2px));
}

/* --- Active app indicator ----------------------------------------------- */

.app-switcher-item.active {
    background-color: color-mix(in srgb, var(--color-primary) 10%, transparent);
    color: var(--color-primary);
}

.app-switcher-item.active:hover {
    background-color: color-mix(in srgb, var(--color-primary) 15%, transparent);
}

/* ==========================================================================
   Item Parts
   ========================================================================== */

.app-switcher-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    font-size: var(--font-size-xl);
    border-radius: var(--radius-md);
    background-color: var(--color-gray-100);
}

.app-switcher-item.active .app-switcher-icon {
    background-color: color-mix(in srgb, var(--color-primary) 15%, transparent);
}

.app-switcher-label {
    font-size: var(--font-size-sm);
    font-weight: 600;
    line-height: 1.2;
}

.app-switcher-desc {
    font-size: var(--font-size-xs);
    color: var(--color-gray-500);
    line-height: 1.2;
}

.app-switcher-item.active .app-switcher-desc {
    color: color-mix(in srgb, var(--color-primary) 70%, var(--color-gray-500));
}

/* ==========================================================================
   Mobile — Full-Screen Modal
   ========================================================================== */

@media (max-width: 767px) {
    .app-switcher-panel {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        min-width: 0;
        border: none;
        border-radius: 0;
        box-shadow: none;
        padding: 0;
        z-index: 500;
        display: none;
        flex-direction: column;
    }

    .app-switcher-panel.open {
        display: flex;
    }

    /* Backdrop */
    .app-switcher-panel::before {
        content: "";
        position: absolute;
        inset: 0;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: -1;
    }

    /* Content area */
    .app-switcher-grid {
        background-color: white;
        margin: auto var(--spacing-4);
        padding: var(--spacing-6);
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow-xl);
        grid-template-columns: repeat(3, 1fr);
        gap: var(--spacing-4);
    }

    /* Larger touch targets on mobile */
    .app-switcher-item {
        padding: var(--spacing-4);
        min-height: 88px;
    }

    .app-switcher-icon {
        width: 48px;
        height: 48px;
        font-size: var(--font-size-2xl);
    }
}

/* ==========================================================================
   No-JS Fallback
   ==========================================================================
   Without JS the panel is always visible as a plain nav.
   The JS file adds .js-enabled to .app-switcher, which enables
   the show/hide toggle. Without that class, the panel stays open.
   ========================================================================== */

.app-switcher:not(.js-enabled) .app-switcher-trigger {
    display: none;
}

.app-switcher:not(.js-enabled) .app-switcher-panel {
    display: block;
    position: static;
    border: none;
    box-shadow: none;
    padding: 0;
    min-width: 0;
    background: transparent;
}

.app-switcher:not(.js-enabled) .app-switcher-grid {
    display: flex;
    gap: var(--spacing-2);
}

.app-switcher:not(.js-enabled) .app-switcher-item {
    flex-direction: row;
    padding: var(--spacing-2) var(--spacing-3);
}

.app-switcher:not(.js-enabled) .app-switcher-icon {
    width: 24px;
    height: 24px;
    font-size: var(--font-size-base);
}

.app-switcher:not(.js-enabled) .app-switcher-desc {
    display: none;
}

} /* end @layer components */
