/* ===========================================
   RAIL.CSS — Desktop left rail (≥900px)
   ===========================================

   Spec: docs/superpowers/specs/2026-04-20-desktop-left-rail-design.md

   Structure:
     <aside class="rail">
       <div class="rail-brand">…<button class="rail-toggle">…</button></div>
       <nav class="rail-nav" aria-label="Tools">
         <div class="rail-group" data-group="prescribing">
           <div class="rail-group-heading">…</div>
           <button class="rail-item" …>…</button>
           …
         </div>
         <div class="rail-group" data-group="scores">…</div>
       </nav>
     </aside>

   State model (data-rail-state on <html>):
     mobile     — hide rail (also handled by media query)
     expanded   — full-width labelled rail (default at ≥1200px)
     collapsed  — narrow icon-only rail (default at 900-1199px)

   The toggle is ALWAYS visible at ≥900px and click flips between
   expanded/collapsed. There is no hover-peek; manual toggle only.
   =========================================== */

.rail-spacer {
    display: none;
}

.rail {
    display: none;
}

/* Backdrop — declared at all widths so opacity:0 is the computed value
   even outside the midrange tier. If these rules lived only inside the
   midrange media query, crossing 1200px would jump opacity from the
   default 1 to 0 with a transition, fading a fullscreen black overlay
   for 220ms (the "screen flash" on resize). The element is invisible at
   wide because pointer-events:none and opacity:0; the midrange media
   query just adds the [expanded] override. */
.rail-backdrop {
    position: fixed;
    inset: 0;
    background: var(--overlay-dark);
    opacity: 0;
    pointer-events: none;
    z-index: 1000;
    transition: opacity var(--rail-transition);
}

@media (min-width: 900px) {
    .rail {
        display: flex;
        flex-direction: column;
        flex: 0 0 var(--rail-width-expanded);
        /* Allow flex item to honour flex-basis below children's intrinsic
           min-content (the heading text's white-space:nowrap would otherwise
           floor-lock the rail at ~95px and prevent it collapsing to 56px). */
        min-width: 0;
        position: sticky;
        top: 0;
        align-self: flex-start;
        height: 100vh;
        background: var(--bg-secondary);
        border-right: 1px solid var(--border-color);
        color: var(--text-primary);
        font-family: var(--font-sans);
        z-index: 150;
        -webkit-font-smoothing: antialiased;
        transition: flex-basis var(--rail-transition);
    }

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

    /* ---------- Brand ---------- */

    /* Two-track grid: logo (1fr) + toggle (auto). At collapsed the logo
       track shrinks to 0fr and the toggle drifts into the centred slot. */
    .rail-brand {
        display: grid;
        grid-template-columns: 1fr auto;
        align-items: center;
        gap: 0.5rem;
        padding: 0 0.75rem;
        height: var(--topbar-height);
        box-sizing: border-box;
        border-bottom: 1px solid var(--border-subtle);
        flex-shrink: 0;
        transition: grid-template-columns var(--rail-transition),
                    padding var(--rail-transition);
    }

    .rail-brand-logo {
        display: flex;
        align-items: center;
        gap: 0.625rem;
        min-width: 0;
        overflow: hidden;
    }

    .rail-brand-mark {
        width: 30px;
        height: 30px;
        border-radius: var(--radius-md);
        display: block;
        color: var(--theme-primary, var(--nhs-blue));
        box-shadow: 0 1px 2px color-mix(in srgb, currentColor 25%, transparent);
        flex-shrink: 0;
        transition: color 200ms ease, box-shadow 200ms ease;
    }

    /* Drug-theme mask: on DMARD/Biologics pages with a drug selected, recolor
       the logo square to the active drug color. Compass rose stays white. */
    body[data-page="dmard"][data-active-drug] .rail-brand-mark,
    body[data-page="biologics"][data-active-class] .rail-brand-mark {
        color: var(--active-color);
    }

    .rail-brand-mark svg {
        width: 100%;
        height: 100%;
        display: block;
    }

    .rail-brand-name {
        font-size: 0.875rem;
        font-weight: 700;
        letter-spacing: -0.01em;
        color: var(--text-primary);
        white-space: nowrap;
    }

    /* Toggle — always visible at ≥900px (manual click is the only trigger).
       Compact (20px) so the brand row fits "RheumTools" without clipping. */
    .rail-toggle {
        display: grid;
        place-items: center;
        width: 20px;
        height: 20px;
        padding: 0;
        appearance: none;
        border: none;
        background: transparent;
        color: var(--text-muted);
        cursor: pointer;
        border-radius: var(--radius-sm);
        transition: background 0.12s ease, color 0.12s ease;
    }

    .rail-toggle:focus-visible {
        outline: none;
        box-shadow: var(--focus-ring-nhs-green);
    }

    @media (hover: hover) {
        .rail-toggle:hover {
            background: var(--bg-tertiary);
            color: var(--text-primary);
        }
    }

    .rail-toggle-icon {
        display: grid;
        place-items: center;
        transition: transform var(--rail-transition);
    }

    .rail-toggle svg {
        width: 14px;
        height: 14px;
        display: block;
    }

    /* Chevron flips to point outward (right) when the rail is collapsed,
       matching the user's expand intent. */
    [data-rail-state="collapsed"] .rail-toggle-icon {
        transform: rotate(180deg);
    }

    /* ---------- Nav ---------- */

    .rail-nav {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        padding: 0.25rem 0 0.5rem;
    }

    .rail-group {
        display: flex;
        flex-direction: column;
    }

    .rail-group[hidden],
    .rail-group.rail-group-hidden {
        display: none;
    }

    .rail-group > [role="group"] {
        display: flex;
        flex-direction: column;
    }

    /* Heading uses max-height collapse — bulletproof across browsers
       (the grid-template-rows: 0fr trick is flaky in some engines). 3rem
       generously covers 1rem top padding + ~12px text + 0.375rem bottom. */
    .rail-group-heading {
        overflow: hidden;
        max-height: 3rem;
        transition: max-height var(--rail-transition);
    }

    .rail-group-heading-text {
        display: block;
        font-size: 0.625rem;
        font-weight: 600;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--text-muted);
        padding: 1rem 1.125rem 0.375rem;
        white-space: nowrap;
    }

    /* ---------- Rail item ---------- */

    .rail-item {
        appearance: none;
        border: none;
        background: transparent;
        text-align: left;
        cursor: pointer;
        position: relative;
        display: flex;
        align-items: center;
        gap: 0.625rem;
        padding: 0.4375rem 0.625rem;
        margin: 0 0.5rem;
        border-radius: var(--radius-md);
        color: var(--text-secondary);
        font-family: var(--font-sans);
        font-size: 0.8125rem;
        font-weight: 500;
        transition: background 0.12s ease, color 0.12s ease,
                    margin var(--rail-transition),
                    padding var(--rail-transition),
                    gap var(--rail-transition);
    }

    .rail-item:focus-visible {
        outline: none;
        box-shadow: var(--focus-ring-nhs-green);
    }

    .rail-item-icon {
        width: 22px;
        height: 22px;
        border-radius: var(--radius-sm);
        display: grid;
        place-items: center;
        color: var(--text-muted);
        flex-shrink: 0;
        transition: background 0.12s ease, color 0.12s ease;
    }

    .rail-item-icon svg {
        width: 14px;
        height: 14px;
        stroke-width: 1.9;
    }

    .rail-item-label {
        flex: 1;
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        transition: opacity 180ms 40ms var(--ease-out-quint),
                    transform 180ms 40ms var(--ease-out-quint);
    }

    @media (hover: hover) {
        .rail-item:hover {
            background: var(--bg-tertiary);
            color: var(--text-primary);
        }
        .rail-item:hover .rail-item-icon {
            color: var(--text-primary);
        }
    }

    /* Per-module rail accent defaults — one source of truth in CSS so the
       drug/class-theme cascade below can override cleanly (no inline style,
       no !important). Must stay in sync with pageColors in js/core/data.js. */
    .rail-item[data-module="dmard"]         { --rail-accent: var(--nhs-blue); }
    .rail-item[data-module="biologics"]     { --rail-accent: var(--biologics-base); }
    .rail-item[data-module="prednisolone"]  { --rail-accent: var(--steroid-amber); }
    .rail-item[data-module="monitoring"]    { --rail-accent: var(--monitoring-primary); }
    .rail-item[data-module="osteoanabolic"] { --rail-accent: var(--osteo-primary); }
    .rail-item[data-module="das28"]         { --rail-accent: var(--das28-primary); }
    .rail-item[data-module="axspa"]         { --rail-accent: var(--axspa-primary); }
    .rail-item[data-module="gcaps"]         { --rail-accent: var(--emerald-primary); }
    .rail-item[data-module="sledai"]        { --rail-accent: var(--sledai-primary); }
    .rail-item[data-module="bvas"]          { --rail-accent: var(--bvas-purple); }

    /* Active state — uses --rail-accent set per-module above */
    .rail-item[aria-current="page"] {
        background: color-mix(in srgb, var(--rail-accent, var(--nhs-blue)) 8%, transparent);
        color: var(--rail-accent, var(--nhs-blue));
        font-weight: 600;
    }

    [data-theme="dark"] .rail-item[aria-current="page"] {
        background: var(--bg-tertiary);
        color: var(--text-primary);
    }

    .rail-item[aria-current="page"] .rail-item-icon {
        background: color-mix(in srgb, var(--rail-accent, var(--nhs-blue)) 24%, transparent);
        color: var(--rail-accent, var(--nhs-blue));
    }

    .rail-item[aria-current="page"]::before {
        content: "";
        position: absolute;
        left: -1px;
        top: 7px;
        bottom: 7px;
        width: 3px;
        border-radius: 2px;
        background: var(--rail-accent, var(--nhs-blue));
    }

    /* Drug-theme mask: on DMARD/Biologics pages with a drug/class selected,
       recolor the active rail item's accent to match. Wins over the
       per-module default via higher specificity (0,5,0 vs 0,2,0). */
    body[data-page="dmard"][data-active-drug] .rail-item[data-module="dmard"][aria-current="page"],
    body[data-page="biologics"][data-active-class] .rail-item[data-module="biologics"][aria-current="page"] {
        --rail-accent: var(--active-color);
    }

    /* ---------- Collapsed state ---------- */

    [data-rail-state="collapsed"] .rail {
        flex-basis: var(--rail-width-collapsed);
    }

    [data-rail-state="collapsed"] .rail-brand {
        grid-template-columns: 0fr auto;
        padding: 0;
        justify-content: center;
    }

    [data-rail-state="collapsed"] .rail-group-heading {
        max-height: 0;
    }

    /* Labels fade AND stop occupying flex space so the icon truly centres.
       Without width:0 + flex:0 the label's flex:1 keeps the icon left-aligned
       inside the pill and inside the active highlight box. */
    [data-rail-state="collapsed"] .rail-item-label {
        opacity: 0;
        width: 0;
        flex: 0 0 0;
        transform: translateX(-4px);
        pointer-events: none;
    }

    [data-rail-state="collapsed"] .rail-item {
        justify-content: center;
        margin: 0 0.375rem;
        padding-left: 0;
        padding-right: 0;
        gap: 0;
    }
}

/* Single shared tooltip — rail.js appends one <div class="rail-tooltip">
   to <body> and retargets it on hover/focus of any collapsed rail item.
   Replaces per-item ::after pseudo-elements: those promoted one
   compositing layer per item in WebKit and flashed at stale --tt-top/
   --tt-left coordinates during the rail's collapse reflow. One layer
   outside .rail can't be touched by the rail's reflow. JS sets inline
   top/left from the hovered button's rect and toggles data-visible.

   Lives outside the @media (min-width: 900px) wrapper so the baseline
   visibility:hidden + position:fixed apply at <900px too — without them
   the tooltip element (still in body from a prior desktop hover) falls
   back to default block styles and lands at bottom-left of the viewport
   on every module. */
.rail-tooltip {
    position: fixed;
    top: 0;
    left: 0;
    transform: translate(-4px, -50%);
    background: var(--text-primary);
    color: var(--bg-primary);
    font-size: 0.75rem;
    font-weight: 500;
    line-height: 1.2;
    padding: 0.3125rem 0.5rem;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
    z-index: 1100;
    transition: opacity 0.08s ease,
                transform 0.08s ease,
                visibility 0s 0.08s;
}

.rail-tooltip[data-visible="true"] {
    visibility: visible;
    opacity: 1;
    transform: translate(0, -50%);
    transition: opacity 0.08s ease,
                transform 0.08s ease,
                visibility 0s;
}

/* Midrange (900–1199): rail becomes a fixed-position overlay. Content
   doesn't have room for an expanded rail at this width, so peek-expand
   floats the rail above main content rather than pushing it. The
   .rail-spacer flex item reserves the 56px slot so layout never
   reflows when the rail widens or narrows.

   Default-collapsed visuals are applied at CSS-paint time (keyed on
   :root:not([data-rail-state="expanded"])) so there's no flash before
   JS writes data-rail-state. Peek-expand wins via specificity. */
@media (min-width: 900px) and (max-width: 1199.98px) {
    .rail {
        position: fixed;
        left: 0;
        top: 0;
        width: var(--rail-width-collapsed);
        flex-basis: var(--rail-width-collapsed);
        transition: width var(--rail-transition),
                    box-shadow var(--rail-transition);
    }

    .rail-spacer {
        display: block;
        flex: 0 0 var(--rail-width-collapsed);
        align-self: stretch;
    }

    [data-rail-state="expanded"] .rail {
        width: var(--rail-width-expanded);
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
        z-index: 1001;
    }

    [data-rail-state="expanded"] .rail-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    :root:not([data-rail-state="expanded"]) .rail-brand {
        grid-template-columns: 0fr auto;
        padding: 0;
        justify-content: center;
    }

    :root:not([data-rail-state="expanded"]) .rail-group-heading {
        max-height: 0;
    }

    :root:not([data-rail-state="expanded"]) .rail-item-label {
        opacity: 0;
        width: 0;
        flex: 0 0 0;
        transform: translateX(-4px);
        pointer-events: none;
    }

    :root:not([data-rail-state="expanded"]) .rail-item {
        justify-content: center;
        margin: 0 0.375rem;
        padding-left: 0;
        padding-right: 0;
        gap: 0;
    }

    :root:not([data-rail-state="expanded"]) .rail-toggle-icon {
        transform: rotate(180deg);
    }
}

/* Reduced motion: strip all rail transitions. Driven by rail-state.js
   reading matchMedia and writing <html data-rail-motion="reduced">. */
[data-rail-motion="reduced"] .rail,
[data-rail-motion="reduced"] .rail-brand,
[data-rail-motion="reduced"] .rail-group-heading,
[data-rail-motion="reduced"] .rail-item,
[data-rail-motion="reduced"] .rail-item-label {
    transition: none !important;
}
