/* ===========================================
   HEADER.CSS - Header, App Launcher & Controls
   ===========================================

   TABLE OF CONTENTS
   -----------------
   1. Header
   2. App Launcher
   3. Header Controls
   4. Launcher Overlay & Panel
   5. Hospital Toggle

   =========================================== */

/* -------------------------------------------
   HEADER
   ------------------------------------------- */
header {
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border-color);
    padding: 0.875rem 1.5rem;
    padding-top: calc(0.875rem + env(safe-area-inset-top, 0px));
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 200;
    box-shadow: var(--shadow-sm);
    flex-shrink: 0;
    /* GPU compositing to prevent iOS Safari tap flash */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

[data-theme="dark"] header {
    background: var(--bg-secondary);
}

.header-inner {
    max-width: 1100px;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
    gap: 1rem;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-shrink: 0;
}


.header-title {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    display: flex;
    align-items: center;
}

.header-title .separator {
    color: var(--text-muted);
    margin: 0 0.5rem;
    font-weight: 400;
}

.header-subtitle,
.subtitle-separator {
    font-size: 1.0625rem;
    color: var(--theme-primary);
    font-weight: 700;
}

@media (min-width: 601px) {
    .header-title {
        font-size: var(--text-lg);
    }

    .header-subtitle,
    .subtitle-separator {
        font-size: var(--text-xl);
    }
}

@media (max-width: 600px) {
    header {
        padding: 0.625rem 1rem;
        padding-top: calc(0.625rem + env(safe-area-inset-top, 0px));
    }

    .header-inner {
        flex-direction: row;
        align-items: center;
        gap: 0.5rem;
    }

    .header-brand {
        min-width: 0;
        overflow: hidden;
    }
}

/* -------------------------------------------
   APP LAUNCHER
   ------------------------------------------- */

/* Waffle grid button */
.waffle-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 50%;
    display: grid;
    grid-template-columns: repeat(3, 4px);
    grid-template-rows: repeat(3, 4px);
    gap: 3px;
    align-content: center;
    justify-content: center;
    transition: background var(--transition-fast);
    padding: 0;
    flex-shrink: 0;
}
@media (hover: hover) {
    .waffle-btn:hover {
        background: var(--waffle-highlight);
    }
}
.waffle-btn[aria-expanded="true"] {
    background: var(--waffle-highlight);
}
.waffle-btn span {
    width: 4px;
    height: 4px;
    border-radius: 1px;
    background: var(--text-secondary);
    transition: background var(--transition-fast);
}
@media (hover: hover) {
    .waffle-btn:hover span {
        background: var(--text-primary);
    }
}
.waffle-btn[aria-expanded="true"] span {
    background: var(--text-primary);
}

/* -------------------------------------------
   HEADER CONTROLS (desktop only)
   Hospital selector + theme toggle, right-aligned
   ------------------------------------------- */
.header-controls {
    display: none; /* hidden on mobile */
    align-items: center;
    gap: 0.625rem;
    margin-left: auto;
    flex-shrink: 0;
    visibility: hidden; /* FOUC guard — shown when hospital-selector-ready */
}

body.hospital-selector-ready .header-controls {
    visibility: visible;
}

@media (min-width: 601px) {
    .header-controls {
        display: flex;
    }
}

/* Header hospital — typography context: no container, integrated text */
.header-controls .hospital-selector-btn {
    background: transparent;
    border: none;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-md);
    font-size: var(--text-base);
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    transition: background-color var(--transition-fast);
}

.header-controls .hospital-site-name {
    color: var(--text-primary);
    transition: color var(--transition-fast);
}

/* Vertical slide wrapper for hospital name transition */
.hospital-site-wrapper {
    display: inline-flex;
    overflow: hidden;
    height: 1.5em;
    justify-content: flex-end;
    transition: width 300ms var(--ease-out-back-subtle);
}

.hospital-site-track {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    transition: transform 300ms var(--ease-out-back-subtle);
}

.hospital-site-track span {
    height: 1.5em;
    display: flex;
    align-items: center;
    white-space: nowrap;
    color: var(--text-primary);
    font-weight: 600;
}

.hospital-site-wrapper[data-selected="whipps"] .hospital-site-track {
    transform: translateY(-1.5em);
}

.header-controls .hospital-specialty-name {
    color: var(--theme-primary);
    transition: color var(--transition-fast);
}

/* Mini chevron: always visible, right-pointing, muted */
.header-controls .hospital-selector-chevron {
    display: block;
    width: 14px;
    height: 14px;
    color: var(--text-secondary);
    transform: rotate(-90deg);
    transition: color var(--transition-fast), translate var(--transition-fast);
    flex-shrink: 0;
}

/* Hover: background tint + chevron nudge to signal interactivity */
@media (hover: hover) {
    .header-controls .hospital-selector-btn:hover {
        background-color: var(--bg-tertiary);
    }
    .header-controls .hospital-selector-btn:hover .hospital-selector-chevron {
        color: var(--text-primary);
        translate: 1px 0;
    }
}
.hover-capable .header-controls .hospital-selector-btn:hover {
    background-color: var(--bg-tertiary);
}
.hover-capable .header-controls .hospital-selector-btn:hover .hospital-selector-chevron {
    color: var(--text-primary);
    translate: 1px 0;
}

/* Attention ring for first-time users — draws eye to the selector */
.hospital-selector-btn.needs-attention {
    position: relative;
}

.hospital-selector-btn.needs-attention::after {
    content: '';
    position: absolute;
    inset: -6px;
    border-radius: 8px;
    border: 2px solid rgba(0, 94, 184, 0.45);
    border: 2px solid color-mix(in srgb, var(--theme-primary) 55%, transparent);
    opacity: 0;
    transform: scale(0.98);
    pointer-events: none;
    will-change: transform, opacity;
    animation: attentionRing 2s var(--ease-out-expo) 3;
}

@keyframes attentionRing {
    0% { opacity: 0; transform: scale(0.98); }
    18% { opacity: 1; transform: scale(1); }
    70% { opacity: 0; transform: scale(1.12); }
    100% { opacity: 0; transform: scale(1.12); }
}

@media (prefers-reduced-motion: reduce) {
    .hospital-selector-btn.needs-attention::after {
        animation: none;
    }
    .hospital-site-wrapper,
    .hospital-site-track {
        transition: none;
    }
}

/* Patient mode: hide header controls */
body.patient-mode .header-controls,
html.patient-mode-pending .header-controls {
    display: none !important;
}

/* Launcher overlay + panel */
.launcher-overlay {
    display: none;
    position: fixed;
    top: calc(var(--header-height, 56px) + env(safe-area-inset-top, 0px));
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 201;
    contain: layout style paint;
    overscroll-behavior: contain;
}
.launcher-overlay.open {
    display: block;
}

.launcher-panel {
    position: absolute;
    background: var(--launcher-panel-bg);
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: var(--radius-lg);
    padding: 1.25rem;
    overflow-y: auto;
    contain: layout style paint;
    animation: launcherSlideDown 200ms ease;
}

@keyframes launcherSlideDown {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Desktop: anchored panel */
@media (min-width: 901px) {
    .launcher-panel {
        top: 0;
        left: 1rem;
        width: 360px;
        padding: 1rem;
        max-height: calc(100vh - var(--header-height, 56px) - env(safe-area-inset-top, 0px) - 1rem);
        box-shadow: var(--shadow-lg);
    }
}

/* Mobile: floating panel */
@media (max-width: 900px) {
    .launcher-overlay {
        top: calc(var(--header-height-mobile, 60px) + env(safe-area-inset-top, 0px));
    }
    .launcher-panel {
        position: absolute;
        top: 0.5rem;
        left: 0.75rem;
        right: 0.75rem;
        max-height: calc(100vh - var(--header-height-mobile, 60px) - env(safe-area-inset-top, 0px) - 1.5rem);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-lg);
        overflow-y: auto;
    }
}


/* Category inner cards */
.launcher-category {
    background: var(--launcher-category-bg);
    border: 1px solid var(--launcher-category-border);
    border-radius: var(--radius-md);
    padding: 1rem 1rem 0.75rem;
}
.launcher-category + .launcher-category {
    margin-top: 0.75rem;
}

.launcher-category h3 {
    font-size: var(--text-2xs);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--text-tertiary, var(--text-secondary));
    margin-bottom: 0.75rem;
    padding-left: 0.125rem;
}

/* Tile grid */
.launcher-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.5rem;
}

/* Individual tile */
.launcher-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.625rem;
    padding: 0.875rem 0.5rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-primary);
    font-family: inherit;
    transition: background var(--transition-fast), border-color var(--transition-fast);
}

@media (hover: hover) {
    .launcher-tile:hover {
        background: var(--launcher-tile-hover);
    }
}
.hover-capable .launcher-tile:hover {
    background: var(--launcher-tile-hover);
}

.launcher-tile.active {
    background: var(--launcher-tile-hover);
}

/* Icon square */
.launcher-tile-icon {
    width: 38px;
    height: 38px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
}
.launcher-tile-icon svg {
    width: 20px;
    height: 20px;
}

/* Label */
.launcher-tile-label {
    font-size: var(--text-2xs);
    font-weight: 500;
    text-align: center;
    color: var(--text-secondary);
    line-height: 1.3;
}
.launcher-tile:hover .launcher-tile-label,
.launcher-tile.active .launcher-tile-label {
    color: var(--text-primary);
}

/* Specialty-based visibility */
.launcher-hidden {
    display: none;
}

/* Mobile launcher density — must follow base rules for cascade priority */
@media (max-width: 900px) {
    .launcher-panel {
        padding: 0.75rem;
    }
    .launcher-category {
        padding: 0.625rem 0.5rem 0.5rem;
    }
    .launcher-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 0.25rem;
    }
    .launcher-tile {
        padding: 0.625rem 0.25rem;
    }
}

/* Desktop launcher density — tighter spacing for pointer devices */
@media (min-width: 901px) {
    .launcher-category {
        padding: 0.75rem 0.75rem 0.625rem;
    }
    .launcher-category h3 {
        margin-bottom: 0.5rem;
    }
    .launcher-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 0.375rem;
    }
    .launcher-tile {
        padding: 0.5rem 0.375rem;
        gap: 0.375rem;
    }
    .launcher-tile-icon {
        width: 34px;
        height: 34px;
        border-radius: 8px;
    }
    .launcher-tile-icon svg {
        width: 18px;
        height: 18px;
    }
}

/* Patient mode: hide launcher */
body.patient-mode .waffle-btn,
body.patient-mode .launcher-overlay,
html.patient-mode-pending .waffle-btn,
html.patient-mode-pending .launcher-overlay {
    display: none !important;
}

/* -------------------------------------------
   HOSPITAL TOGGLE — Variant overrides
   Ghost/border-only style, height matches theme toggle (36px outer)
   ------------------------------------------- */
.hospital-toggle {
    transition: border-color var(--transition-base);
}

.hospital-toggle::before {
    width: var(--hospital-option-width, 42px);
    background: var(--bg-elevated);
    border: 1px solid var(--text-muted);
    border-radius: var(--radius-pill);
    transition: transform var(--duration-entrance) var(--ease-out-back),
                border-color var(--transition-base);
}

[data-theme="dark"] .hospital-toggle::before {
    background: var(--bg-tertiary);
}

/* Whipps selected - slide right */
.hospital-toggle[data-selected="whipps"]::before {
    transform: translateX(var(--hospital-option-width, 42px));
}

.hospital-option {
    width: var(--hospital-option-width, 42px);
    border-radius: var(--radius-pill);
    font-size: var(--text-xs);
    font-weight: 500;
    letter-spacing: 0.02em;
    background: none;
    border: none;
    transition: color var(--transition-base);
    white-space: nowrap;
}

.hospital-option.active {
    color: var(--text-primary);
    font-weight: 600;
}

/* Desktop: hide mobile footer-settings hospital toggle (use footer-desktop-row) */
@media (min-width: 601px) {
    .footer-settings .hospital-toggle {
        display: none;
    }
}

/* Hover effects */
@media (hover: hover) {
    .hospital-toggle:hover {
        border-color: var(--text-muted);
    }

    .hospital-toggle:hover::before {
        border-color: var(--text-secondary);
    }
}
.hover-capable .hospital-toggle:hover {
    border-color: var(--text-muted);
}
.hover-capable .hospital-toggle:hover::before {
    border-color: var(--text-secondary);
}

/* Patient mode: hide toggle (QR scan view) */
.patient-mode .hospital-toggle {
    display: none !important;
}

/* -------------------------------------------
   LOADING STATE (header-specific)
   ------------------------------------------- */
body.hospital-loading .hospital-selector-btn {
    opacity: 0.6;
    pointer-events: none;
}

/* Hospital toggle — absolutely positioned with top:50%, needs translateY centering */
@keyframes loadingSpinCentered {
    to { transform: translateY(-50%) rotate(360deg); }
}
