/* Mobile layer shared by every page. Loaded after each page's own <style>, so it wins ties.
   Pairs with assets/mobile.js, which adds the classes used here. Without JS the pages fall back
   to their own mobile CSS (scrolling nav, scrolling tables). */

.nav-toggle, .nav-current, .toc-fab, .toc-sheet, .ledger, .arch-m { display: none; }

/* ---------- Top nav: menu button instead of a hidden sideways scroll ---------- */
@media (max-width: 980px) {
  .topnav.has-menu { overflow: visible; padding: 0.55rem 1rem; gap: 0.75rem; }
  .topnav.has-menu .divider, .topnav.has-menu .doc-tag { display: none; }
  .topnav.has-menu .nav-current { display: block; flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-weight: 600; font-size: 0.9rem; padding-left: 0.75rem; border-left: 1px solid var(--border); }
  .topnav.has-menu .nav-toggle { display: inline-flex; align-items: center; gap: 0.5rem; margin-left: auto; flex-shrink: 0; font: inherit; font-weight: 700; font-size: 0.9rem; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 100px; padding: 0.45rem 0.95rem; min-height: 40px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
  .topnav.has-menu .nav-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .nav-toggle .bars { display: inline-grid; gap: 3px; width: 16px; }
  .nav-toggle .bars i { display: block; height: 2px; background: currentColor; border-radius: 2px; transition: transform 0.2s, opacity 0.2s; }
  .topnav.menu-open .nav-toggle .bars i:nth-child(1) { transform: translateY(5px) rotate(45deg); }
  .topnav.menu-open .nav-toggle .bars i:nth-child(2) { opacity: 0; }
  .topnav.menu-open .nav-toggle .bars i:nth-child(3) { transform: translateY(-5px) rotate(-45deg); }
  .topnav.has-menu .nav-links { display: none; position: absolute; top: 100%; left: 0; right: 0; flex-direction: column; align-items: stretch; gap: 0; margin: 0; padding: 0.35rem 0.75rem 0.75rem; background: var(--bg); border-bottom: 1px solid var(--border); box-shadow: 0 14px 28px rgba(26, 26, 26, 0.14); max-height: calc(100vh - 60px); max-height: calc(100dvh - 60px); overflow-y: auto; font-size: 1rem; }
  .topnav.menu-open .nav-links { display: flex; }
  .topnav.has-menu .nav-links a { display: flex; align-items: center; min-height: 48px; padding: 0.75rem; border-bottom: 1px solid var(--border); color: var(--text); font-weight: 600; white-space: normal; }
  .topnav.has-menu .nav-links a:last-child { border-bottom: none; }
  .topnav.has-menu .nav-links a.active { color: var(--accent-dark); background: var(--accent-bg); box-shadow: inset 3px 0 0 var(--accent); }
}

/* ---------- Tablets: wide worksheet tables scroll inside their own box ---------- */
.swipe-hint { font-size: 0.8rem; color: var(--muted); margin: 0.75rem 0 -0.5rem; }
@media (max-width: 1024px) {
  main table.scroll-x { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  /* Keep header and body in one table so the columns line up while scrolling */
  main table.scroll-x thead, main table.scroll-x tbody { display: table-row-group; width: auto; }
  .ex-calc { font-size: 0.8rem; }
}

@media (max-width: 768px) {
  /* Reorg playbook target-structure diagram, as cards */
  .arch.has-m { display: none; }
  .arch-m { display: grid; gap: 0.75rem; margin: 1.5rem 0; }
  .arch-m .pf { background: var(--accent-bg); border: 1.5px solid var(--accent); border-radius: 10px; padding: 0.85rem; }
  .arch-m .solo { background: var(--bg); border: 1px dashed var(--muted); border-radius: 10px; padding: 0.85rem; }
  .arch-m h5 { margin: 0; font-size: 0.95rem; font-weight: 800; color: var(--accent-dark); }
  .arch-m .solo h5 { color: var(--muted); margin-bottom: 0.6rem; }
  .arch-m .t { font-size: 0.82rem; color: var(--muted); margin: 0.1rem 0 0.6rem; }
  .arch-m ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.5rem; }
  .arch-m li { margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 0.55rem 0.75rem; font-size: 0.9rem; font-weight: 600; }
  .arch-m li small { display: block; color: var(--muted); font-size: 0.8rem; font-weight: 500; }
  .arch-m li.sib { background: none; border: 1px dashed var(--accent); color: var(--muted); font-weight: 500; }
}

/* ---------- Phones: inputs, tap targets, padding ---------- */
@media (max-width: 768px) {
  /* The pages set overflow-x: hidden here, which turns body into a scroll box and stops the
     top bar sticking. clip trims the same overflow without that side effect. */
  html, body { overflow-x: clip; }
  /* Under 16px, iOS zooms the page in when a field gets focus */
  input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea { font-size: 16px !important; }
  .copy, .lc-share button, .btn-clear { position: relative; }
  .copy::after, .lc-share button::after, .btn-clear::after { content: ""; position: absolute; inset: -12px -10px; }
  .calc { padding: 0.9rem; }
  .calc .plat, .lc-result .plat { padding: 0.85rem 0.9rem; }
  .lc-result { padding: 0.9rem; }
  .callout { overflow-wrap: anywhere; }

  /* Contents button and sheet */
  .toc-fab { display: inline-flex; position: fixed; right: max(1rem, env(safe-area-inset-right)); bottom: max(1rem, env(safe-area-inset-bottom)); z-index: 60; align-items: center; gap: 0.5rem; font: inherit; font-weight: 700; font-size: 0.9rem; color: #fff; background: var(--text); border: none; border-radius: 100px; padding: 0.7rem 1.1rem; min-height: 46px; box-shadow: 0 6px 18px rgba(26, 26, 26, 0.28); cursor: pointer; opacity: 0; transform: translateY(14px); pointer-events: none; transition: opacity 0.2s, transform 0.2s; -webkit-tap-highlight-color: transparent; }
  .toc-fab.show { opacity: 1; transform: none; pointer-events: auto; }
  .toc-fab .bars { display: inline-grid; gap: 3px; width: 14px; }
  .toc-fab .bars i { display: block; height: 2px; background: currentColor; border-radius: 2px; }
  .toc-fab .bars i:nth-child(2) { width: 70%; }
  .toc-sheet:not([hidden]) { display: block; position: fixed; inset: 0; z-index: 70; background: rgba(26, 26, 26, 0.45); }
  .toc-sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-height: 78vh; max-height: 78dvh; overflow-y: auto; overscroll-behavior: contain; background: var(--bg); border-radius: 16px 16px 0 0; padding: 0 0.75rem calc(0.75rem + env(safe-area-inset-bottom)); }
  .toc-sheet-h { position: sticky; top: 0; z-index: 1; display: flex; justify-content: space-between; align-items: center; padding: 0.6rem 0.25rem; background: var(--bg); font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); border-bottom: 1px solid var(--border); }
  .toc-close { font: inherit; font-size: 1.15rem; line-height: 1; background: none; border: none; color: var(--text); min-width: 44px; min-height: 44px; margin-right: -0.5rem; cursor: pointer; }
  .toc-sheet ul { list-style: none; margin: 0; padding: 0; }
  .toc-sheet li { margin: 0; }
  .toc-sheet a { display: flex; align-items: center; min-height: 48px; padding: 0.7rem 0.6rem; color: var(--text); text-decoration: none; border-bottom: 1px solid var(--border); font-size: 0.95rem; font-weight: 600; }
  .toc-sheet li:last-child a { border-bottom: none; }
  .toc-sheet li.sub a { padding-left: 1.6rem; color: var(--muted); font-weight: 500; font-size: 0.9rem; }
  .toc-sheet a.active { color: var(--accent-dark); background: var(--accent-bg); box-shadow: inset 3px 0 0 var(--accent); }
}

/* ---------- Small phones: tables become cards, ledgers reflow ---------- */
@media (max-width: 640px) {
  main table.stack { display: block; overflow: visible; background: none; border: none; border-radius: 0; margin: 1rem 0; }
  main table.stack thead { display: none; }
  main table.stack tbody { display: grid; gap: 0.75rem; width: auto; }
  main table.stack tr { display: block; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
  main table.stack td { display: block; padding: 0.6rem 0.9rem; border-bottom: 1px solid var(--border); }
  main table.stack td:last-child { border-bottom: none; }
  main table.stack td.blank { display: none; }
  main table.stack td:first-child { background: var(--surface-2); font-weight: 700; }
  main table.stack-lead-long td:first-child { background: var(--surface); font-weight: 400; }
  main table.stack td[data-label]::before { content: attr(data-label); display: block; font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 0.15rem; }
  main table.stack-inline td:not(:first-child) { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; }
  main table.stack-inline td:not(:first-child)::before { margin: 0; flex-shrink: 0; max-width: 45%; }
  main table.stack-inline td:not(:first-child) > .cell { text-align: right; min-width: 0; }
  main table.stack .pill { white-space: normal; }
  main table.stack tr:hover td { background: inherit; }
  main table.stack tr:hover td:first-child { background: var(--surface-2); }
  main table.stack-lead-long tr:hover td:first-child { background: var(--surface); }

  .src, .ex-tag { white-space: nowrap; }
  .ex-header { gap: 0.75rem; align-items: flex-start; }
  .ex-tag { flex-shrink: 0; margin-top: 0.15rem; }

  .has-ledger { display: none !important; }
  .ledger { display: block; font-size: 0.88rem; line-height: 1.45; font-variant-numeric: tabular-nums; background: var(--surface-2); border-radius: 8px; padding: 0.75rem 0.9rem; margin: 0.75rem 0; }
  .ledger.from-pre { background: var(--surface); border: 1px solid var(--border); }
  .lg-row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; column-gap: 0.75rem; padding: 0.12rem 0; }
  .lg-k { color: var(--muted); }
  .lg-v { margin-left: auto; text-align: right; font-weight: 600; }
  .lg-only .lg-k { color: var(--text); font-weight: 600; }
  .lg-gap { height: 0.55rem; }
  .lg-rule { border-top: 1px solid var(--border); margin: 0.3rem 0; }
  main pre { white-space: pre-wrap; overflow-wrap: anywhere; }
}

@media print {
  .nav-toggle, .nav-current, .toc-fab, .toc-sheet, .ledger, .arch-m, .swipe-hint { display: none !important; }
  .has-ledger, .arch.has-m { display: block !important; }
}
