/*!
 * Neonatology Academy — Mobile layer
 * =================================================================
 * Everything in this file is phone/small-tablet only. Desktop is not
 * touched: every rule lives inside a max-width media query, so if this
 * sheet is removed the desktop site is byte-for-byte what it was.
 *
 * Load it LAST, after neo-access.css. It is deliberately the final word
 * on small screens — it has to out-specify both redesign.css and Codex's
 * neo-editorial.css, which carry `!important` through their layout rules
 * and were authored for a wide reading column.
 *
 * What it fixes, measured on a 375x812 viewport before any change:
 *   - body copy ran at 13.6px, with 160 elements at 12.8px and 27 at 11.52px
 *   - 81 of 138 tap targets were under 44px tall
 *   - the specialty cards' action buttons were clipped 20px past the card edge
 *   - 165 of 167 Neo Summary tables were wider than the screen (560-640px)
 *   - the question explanation was one 4,074px wall inside a 473px window
 *
 * Companion: src/lib/neo-mobile.js adds the collapsing and the table
 * row labels. The CSS degrades safely without it.
 * ================================================================= */

/* ===================================================================
   1. TYPOGRAPHY
   The single biggest readability win. A phone is held closer than a
   monitor but the type was SMALLER, not larger. 16px is also the
   threshold below which iOS Safari zooms the page when a field is
   focused, which is why every input is pinned to it.
   =================================================================== */
@media (max-width: 768px) {

  :root {
    --nm-read:      1rem;      /* 16px — body copy                    */
    --nm-read-lh:   1.62;
    --nm-small:     0.875rem;  /* 14px — the floor for meta text      */
    --nm-tiny:      0.8125rem; /* 13px — absolute floor, labels only  */
    --nm-tap:       44px;      /* Apple HIG / Material minimum target */
    --nm-gutter:    16px;
  }

  html {
    /* Stop iOS inflating text in landscape on its own terms. */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  body {
    font-size: var(--nm-read);
    line-height: var(--nm-read-lh);
  }

  /* Reading surfaces: the question, the explanation, Neo Summary prose,
     guideline pages. These are what the site is actually for. */
  .nd-prose,
  .nd-prose p,
  .nd-prose li,
  #question-stem-display,
  #explanation-text-display,
  .qb-exp-body,
  .nst-reading-column,
  .nst-reading-column p,
  .nst-reading-column li,
  .nd-keypoints li,
  .nd-opt,
  .guideline-body p,
  .guideline-body li {
    font-size: var(--nm-read) !important;
    line-height: var(--nm-read-lh) !important;
  }

  /* A floor, not a rescale.
     Sub-14px type is set in nine different stylesheets — 56 declarations
     in styles.css alone — so naming the components would be both endless
     and brittle. `max(1em, 14px)` leaves anything already larger exactly
     as it was and only lifts what falls below the floor. It cannot
     compound: 1em is the parent's COMPUTED size, so a child of a lifted
     14px parent resolves to 14px, not 14px again and again.

     The site carries 140 `font-size: ... !important` declarations across
     nine stylesheets, so this rule has to be important too AND carry
     enough specificity to win the tie. `html body` plus the class inside
     :not() is what buys that — it is load-bearing, not decoration.

     Excluded: icon fonts (sized BY font-size, so a floor inflates the
     glyph), sup/sub, deliberate micro-labels, and this sheet's own. */
  html body :is(p, li, td, th, dd, dt, label, small, figcaption,
                blockquote, summary, a, span, div, strong, em, b, i,
                legend, caption):not(:is(
                  .fa, [class^="fa-"], [class*=" fa-"], .fas, .far, .fab,
                  .fa-solid, .fa-regular, .fa-brands, .material-icons,
                  [class*="icon"], [class*="badge"], [class*="-chip"],
                  [class*="-pill"], [class*="-tag"],
                  .nm-acc-count, .nm-scroll-hint, .nm-topic-count)) {
    font-size: max(1em, 14px) !important;
  }

  /* Deliberate micro-labels keep a lower floor — a status chip set in
     all-caps at 13px reads fine and does not want to be body copy. */
  html body :is(.badge, .chip, .tag, .pill, [class*="badge"],
                [class*="-chip"], [class*="-pill"], [class*="-tag"]) {
    font-size: max(1em, var(--nm-tiny)) !important;
  }

  /* One named exception. theme.css sets
       #specialty-status-table thead th { font-size:.73rem !important }
     and an ID outranks any class, so the floor above cannot reach it.
     Matched here rather than raising the specificity of the general
     rule, which would start an arms race with every ID in the site. */
  html body #specialty-status-table :is(th, td) {
    font-size: max(1em, 14px) !important;
  }

  /* The logo link measured 32px — the only control genuinely below the
     44px target once sub-pixel rounding is discounted. */
  .nnt-logo {
    min-height: var(--nm-tap);
    display: inline-flex;
    align-items: center;
  }

  /* The top bar was the last thing overflowing: a 214px logo set to
     `flex: 0 0 auto` plus 146px of actions plus 32px of padding is
     392px in a 375px bar, which pushed the action buttons 21px off the
     right edge. Letting the wordmark shrink is better than hiding it —
     the brand stays, and the buttons come back on screen. */
  .nnt-inner { min-width: 0; }

  .nnt-logo {
    flex: 0 1 auto !important;
    min-width: 0;
    overflow: hidden;
  }

  .nnt-logo > span,
  .nnt-logo .nnt-wordmark {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 1rem;
  }

  .nnt-actions { flex: 0 0 auto; }
}

/* Below ~360px even a shrunk wordmark crowds the controls out. The mark
   alone still identifies the site, and the title is on the page. */
@media (max-width: 360px) {
  .nnt-logo > span,
  .nnt-logo .nnt-wordmark {
    position: absolute;
    width: 1px; height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }

  /* Long clinical terms, formulas and URLs must wrap rather than push
     the layout sideways. "Qp:Qs = (SaO2 - SvO2)" was a real offender. */
  .nd-prose, .nst-reading-column, #question-stem-display,
  #explanation-text-display, .nd-opt, .nd-keypoints {
    overflow-wrap: break-word;
    word-break: normal;
    hyphens: auto;
  }

  /* Headings: keep the hierarchy, lose the desktop scale. */
  h1 { font-size: clamp(1.5rem, 6vw, 1.875rem); line-height: 1.2; }
  h2 { font-size: clamp(1.25rem, 5vw, 1.5rem);  line-height: 1.25; }
  h3 { font-size: clamp(1.125rem, 4.5vw, 1.3rem); line-height: 1.3; }
  h4, h5, h6 { font-size: 1.0625rem; line-height: 1.35; }

  /* iOS zooms the whole page when a field under 16px takes focus, then
     leaves it zoomed. This is the only reliable prevention. */
  input, select, textarea, button {
    font-size: max(16px, 1rem);
  }
}

/* ===================================================================
   2. TAP TARGETS
   81 of 138 controls were under 44px. Padding is used rather than a
   hard height so a control that wraps to two lines still grows.
   =================================================================== */
@media (max-width: 768px) {

  button, .btn, [role="button"], .nav-link,
  .spc2-btn-sm, .qb-btn, .pa-tab, .neo-summary-sidebar-btn {
    min-height: var(--nm-tap);
    padding-block: max(10px, 0.5em);
  }

  /* Icon-only controls need width too, or they stay a thin strip. */
  .icon-btn, .nnt-icon-btn, [class*="icon-button"],
  button[aria-label]:not(:has(span:not(:empty))) {
    min-width: var(--nm-tap);
  }

  /* Checkbox and radio hit areas in the practice filters. */
  input[type="checkbox"], input[type="radio"] {
    min-width: 22px;
    min-height: 22px;
  }

  /* Links sitting in a row of their own get breathing room so the
     wrong one is not tapped. */
  nav a, .nnt-mob-menu a, footer a {
    padding-block: 10px;
  }

  /* Navigation chips that are links rather than buttons — the formulas
     page has 13 of them, all landing at 43px, one short of the target. */
  .group-nav-link,
  a[class*="nav-link"],
  a[class*="-chip"],
  a[class*="-pill"] {
    min-height: var(--nm-tap);
    display: inline-flex;
    align-items: center;
  }
}

/* ===================================================================
   3. OVERFLOW
   The page never scrolled sideways, but content was being CLIPPED
   inside cards, which is worse: the reader cannot reach it at all.
   =================================================================== */
@media (max-width: 768px) {

  /* Measured: .spc2-actions / .spc2-btn-row reached x=395 in a 375px
     viewport, cutting "Start Questions" and hiding "Practice" entirely. */
  .spc2-actions,
  .spc2-btn-row {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  .spc2-btn-sm {
    flex: 1 1 auto;
    min-width: 0;
    white-space: normal;
  }

  /* Media and charts must never dictate the page width. The accuracy
     chart was laid out at 560px inside a 375px screen. */
  img, svg, video, canvas, iframe, figure {
    max-width: 100%;
    height: auto;
  }

  .pa-line-plot,
  .pa-chart,
  [class*="-chart"] {
    max-width: 100%;
    overflow-x: auto;
  }

  /* The accuracy trend carried min-width:560px, which forced a 621px
     render inside a 375px screen. A seven-point sparkline has no need
     of 560px; letting it fit removes the last sideways scroll on the
     question-bank home. The SVG is drawn from a viewBox, so it scales
     rather than crops. */
  .pa-trend-chart > *,
  .pa-line-plot {
    min-width: 0 !important;
  }

  .pa-trend-chart svg,
  .pa-line-plot svg {
    width: 100%;
    height: auto;
  }

  /* A <summary> laid out as a nowrap flex row pushed its count badge
     11px past the page edge on nicu-quality. Disclosure headers are the
     common case: an icon, a long title and a small badge, sized for a
     desktop column. Letting them wrap costs nothing when they fit. */
  summary {
    flex-wrap: wrap;
  }

  /* A flex child cannot shrink below its content unless told to, which
     is what turns a long title into a row that overflows. */
  summary > *,
  .dash-card summary > * {
    min-width: 0;
  }

  /* Generic guard: a grid built for three columns becomes one. */
  .pa-stat-grid,
  .stat-grid,
  [class*="-stat-grid"] {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
  }

  /* Honour the safe area on notched iPhones so nothing sits under the
     home indicator or the rounded corners. */
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  .qb-footer-bar, .qb-bottom-bar, [class*="bottom-bar"] {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
}

/* ===================================================================
   4. COLLAPSIBLE SECTIONS
   The owner's ask: "anything collapsible that can go alone and
   collapse". neo-mobile.js builds these; this is their appearance.
   They are real buttons with aria-expanded, so VoiceOver and TalkBack
   announce the state.
   =================================================================== */
.nm-acc-btn {
  display: none;             /* desktop: the section is simply open   */
}

@media (max-width: 768px) {

  .nm-acc-btn {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    width: 100%;
    margin: 10px 0 0;
    padding: 12px 14px;
    min-height: var(--nm-tap);
    font: 600 0.9375rem/1.3 inherit;
    text-align: left;
    color: var(--neo-navy, #0f172a);
    background: var(--neo-gray-50, #f8fafc);
    border: 1px solid var(--neo-gray-200, #e2e8f0);
    border-radius: 10px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .nm-acc-btn:hover,
  .nm-acc-btn:focus-visible {
    background: var(--neo-gray-100, #f1f5f9);
  }

  .nm-acc-btn:focus-visible {
    outline: 2px solid var(--neo-brand, #0f766e);
    outline-offset: 2px;
  }

  /* Chevron. Rotates to point down when the panel is open. */
  .nm-acc-btn::after {
    content: "";
    flex: none;
    width: 9px;
    height: 9px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(-45deg);
    transition: transform 0.18s ease;
    opacity: 0.65;
  }

  .nm-acc-btn[aria-expanded="true"]::after {
    transform: rotate(45deg);
  }

  /* A count of what is inside, so a collapsed section still tells the
     reader whether it is worth opening. */
  .nm-acc-btn .nm-acc-count {
    margin-left: auto;
    margin-right: 4px;
    font-weight: 500;
    font-size: 0.8125rem;
    color: var(--neo-gray-500, #64748b);
  }

  .nm-acc-panel[hidden] {
    display: none !important;
  }

  .nm-acc-panel {
    animation: nm-reveal 0.18s ease;
  }

  @keyframes nm-reveal {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: none; }
  }

  /* The heading the button replaces is redundant once the button
     carries the same words. */
  .nm-acc-btn + .nm-acc-panel > .nd-exp-h:first-child {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .nm-acc-btn::after,
  .nm-acc-panel { transition: none; animation: none; }
}

/* ===================================================================
   5. TABLES
   165 of 167 Neo Summary tables were wider than the screen. Scrolling
   one sideways loses the row's subject, which for a clinical
   comparison table is the one column you cannot afford to lose.
   Tables of 4 columns or fewer (165 of them) stack into labelled
   cards. Wider ones keep scrolling, with an affordance that says so.
   neo-mobile.js stamps data-label and restores the ARIA roles that
   display:block strips.
   =================================================================== */
@media (max-width: 640px) {

  table.nm-stack {
    display: block;
    width: 100% !important;
    border: 0;
    border-collapse: separate;
    border-spacing: 0;
    /* neo-clinical-tables.css gives these tables min-width:560px so they
       scroll on desktop. Stacked, that min-width is what pushed the text
       past the right edge and cut it off. */
    min-width: 0 !important;
  }

  /* A page's own table CSS can out-rank this sheet — lab-references.html
     sets `.lr-table { min-width: 380px }` in a <style> block, and
     min-width beats width, so the stacked table stayed 380px wide in a
     317px column and clipped. neo-mobile.js therefore clears min-width
     INLINE, which beats any non-important author rule outright. This
     stays as the fallback for tables the script has not reached. */
  .nm-stacked-wrap > table.nm-stack,
  .nm-stacked-wrap table.nm-stack {
    min-width: 0 !important;
    max-width: 100% !important;
    width: 100% !important;
  }

  /* With the table stacked there is nothing left to scroll sideways,
     and leaving the wrapper scrollable just traps touch gestures.
     neo-mobile.js sets .nm-stacked-wrap. */
  .nm-stacked-wrap {
    overflow-x: visible !important;
    min-width: 0 !important;
  }

  /* Kept as a SEPARATE rule on purpose: one unsupported selector
     invalidates an entire selector list, so pairing :has() with the
     class above would silently drop both. */
  .nst-table-wrap:has(table.nm-stack) {
    overflow-x: visible !important;
  }

  table.nm-stack thead {
    /* Visually hidden, still read aloud. */
    position: absolute;
    width: 1px; height: 1px;
    margin: -1px; padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  table.nm-stack tbody { display: block; }

  table.nm-stack tr {
    display: block;
    margin: 0 0 12px;
    padding: 4px 0;
    background: #fff;
    border: 1px solid var(--neo-gray-200, #e2e8f0);
    border-radius: 12px;
    overflow: hidden;
  }

  /* Both cell types stack. Many of these tables use a <th> for the
     row's subject, and leaving it as a table-cell kept it at a fixed
     160px while its siblings went full width. */
  /* `html body` here only to out-rank this sheet's own 14px floor,
     which is itself important and matches td/th through :is(). A
     stacked clinical table is primary reading, not secondary text. */
  html body table.nm-stack td,
  html body table.nm-stack tbody th {
    display: block;
    width: auto !important;
    min-width: 0 !important;
    max-width: 100% !important;
    padding: 10px 14px;
    border: 0;
    border-top: 1px solid var(--neo-gray-100, #f1f5f9);
    /* Important because the 14px floor earlier in this sheet is itself
       important; a stacked clinical table is primary reading, not the
       secondary text that floor was written for. */
    font-size: var(--nm-read) !important;
    line-height: var(--nm-read-lh);
    text-align: left;
    overflow-wrap: break-word;
  }

  table.nm-stack tr > :first-child {
    border-top: 0;
  }

  /* The column header, printed above its value. */
  table.nm-stack td[data-label]::before,
  table.nm-stack tbody th[data-label]::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 3px;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--neo-gray-500, #64748b);
  }

  /* The first cell is the row's subject — it becomes the card's title. */
  table.nm-stack tr > :first-child {
    background: var(--neo-gray-50, #f8fafc);
    font-weight: 600;
    font-size: 1.0625rem !important;
  }

  /* An empty cell says nothing; hide it rather than print a lone label. */
  table.nm-stack td.nm-empty,
  table.nm-stack tbody th.nm-empty { display: none; }

  /* The didactic yellow has to survive the restack. */
  table.nm-stack mark,
  table.nm-stack .ns-clinical-highlight,
  table.nm-stack .nd-hl { display: inline; }
}

/* Tables too wide to stack keep scrolling — but say so. */
@media (max-width: 768px) {

  .nm-scroll-wrap {
    position: relative;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  /* A fade on the right edge: the standard "there is more that way"
     signal. Removed once the reader has scrolled to the end. */
  .nm-scroll-wrap.nm-has-more::after {
    content: "";
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 28px;
    pointer-events: none;
    background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,0.92));
  }

  .nm-scroll-hint {
    margin: 0 0 6px;
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    color: var(--neo-gray-500, #64748b);
  }
}

/* ===================================================================
   6. NEO SUMMARY
   51 topics, every one open on load, producing a 222,282px page —
   274 phone screens for a single specialty. neo-mobile.js closes them;
   these rules make the closed list worth looking at.
   =================================================================== */
@media (max-width: 768px) {

  /* The topic header becomes the primary navigation on a phone, so it
     needs to look and behave like a row in a list. */
  #tab-book-summary .nst-header {
    min-height: var(--nm-tap);
    padding: 14px var(--nm-gutter) !important;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  #tab-book-summary .nst-header:focus-visible {
    outline: 2px solid var(--neo-brand, #0f766e);
    outline-offset: -2px;
  }

  /* Codex's editorial layer sets a wide reading column with
     !important; on a phone that forces a horizontal scroll. Overridden
     for width only — the colours, rails and type of that layer stand. */
  #tab-book-summary .nst-reading-column,
  #tab-book-summary .ns-editorial-topic .nst-body {
    max-width: 100% !important;
    padding-left: var(--nm-gutter) !important;
    padding-right: var(--nm-gutter) !important;
  }

  /* A closed topic should still show what it holds. */
  #tab-book-summary .nst-header[aria-expanded="false"] .nm-topic-count {
    color: var(--neo-gray-500, #64748b);
    font-weight: 500;
    font-size: 0.8125rem;
  }

  /* The specialty picker is the one control that must always be
     reachable; it is how you change chapter. Important because
     neo-summary.css sizes it with its own id-level rule. */
  #ns-specialty-select {
    min-height: var(--nm-tap) !important;
    width: 100%;
  }

  /* The per-topic practice links measured 36px. They sit side by side,
     so an undersized target here is a mis-tap into the wrong deck. */
  #tab-book-summary :is(.ns-practice-q, .ns-practice-f),
  #tab-book-summary :is([data-ns-expand], .neo-summary-sidebar-btn) {
    min-height: var(--nm-tap) !important;
    display: inline-flex;
    align-items: center;
  }
}

/* ===================================================================
   7. QUESTION PAGE
   The reading window measured 473px of an 812px screen. The chrome
   above it is useful but not all of it is useful at once.
   =================================================================== */
@media (max-width: 768px) {

  /* The question number palette is 59 squares; two rows is plenty
     before it starts eating the question. */
  #question-nav-grid-element,
  .qnp-grid {
    max-height: 96px;
    overflow-y: auto;
  }

  /* Answer options: full width, comfortable target, clear separation. */
  .nd-opt,
  .option-item,
  [class*="option-row"] {
    padding: 14px !important;
    min-height: var(--nm-tap);
  }

  /* The explanation's own prose gets the reading measure, not the
     desktop column. */
  #explanation-drawer {
    padding-left: var(--nm-gutter);
    padding-right: var(--nm-gutter);
  }

  /* Personal notes field: usable without zooming. */
  #question-notes, textarea {
    min-height: 88px;
    font-size: 16px;
  }
}

/* ===================================================================
   8. NAVIGATION AND FORMS
   =================================================================== */
@media (max-width: 768px) {

  /* The mobile menu is a full-height sheet; its rows need to be
     tappable and its text readable. */
  .nnt-mob-menu a,
  .nnt-mob-menu button {
    min-height: var(--nm-tap);
    font-size: 1rem;
  }

  /* Filter rows in the practice configurator stack rather than
     squeeze into unreadable columns. */
  .qb-filter-row,
  [class*="filter-row"],
  [class*="form-row"] {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  select, input[type="text"], input[type="number"],
  input[type="search"], input[type="email"], input[type="password"] {
    width: 100%;
    min-height: var(--nm-tap);
    box-sizing: border-box;
  }
}

/* ===================================================================
   9. PRINT AND HIGH CONTRAST
   A collapsed section must never hide content from a printer or from
   a forced-colors user who cannot see our chevron.
   =================================================================== */
@media print {
  .nm-acc-btn { display: none !important; }
  .nm-acc-panel[hidden] { display: block !important; }
  table.nm-stack { display: table !important; }
  table.nm-stack thead { position: static !important; clip: auto !important;
                         clip-path: none !important; width: auto !important;
                         height: auto !important; }
  table.nm-stack tr, table.nm-stack td { display: table-cell !important; }
  table.nm-stack tr { display: table-row !important; }
  table.nm-stack td[data-label]::before { display: none !important; }
}

@media (forced-colors: active) {
  .nm-acc-btn {
    border: 1px solid ButtonBorder;
    background: ButtonFace;
    color: ButtonText;
  }
  .nm-acc-btn::after { border-color: ButtonText; }
  .nm-scroll-wrap.nm-has-more::after { display: none; }
}
