/* =========================================================
   Polytraders™ — Mobile-friendly overrides for the Library page
   Loaded after css/library.css. All rules scoped to ≤720px or ≤900px.

   Goals
   1. Skip-link hidden until focused
   2. Status bar = horizontal scroll strip on phones (no awful wrap)
   3. Mobile nav drawer (hamburger) with full link list + theme + CTA
   4. Tables collapse to label-value cards on phones (each cell labelled)
   5. Toolbar chips/search: chip rows scroll horizontally; search full-width
   6. Tighter typography + spacing under 720px
   ========================================================= */

/* ---------- 1. Skip link ---------- */
.skip-link {
  position: absolute;
  left: -9999px;
  top: -9999px;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
.skip-link:focus {
  position: fixed;
  left: 12px;
  top: 12px;
  width: auto;
  height: auto;
  padding: 0.55rem 0.9rem;
  background: var(--color-primary);
  color: #fff;
  border-radius: 8px;
  z-index: 9999;
  font-size: 0.85rem;
  font-weight: 600;
}

/* ---------- 2. Status bar — horizontal scroller on mobile ---------- */
@media (max-width: 720px) {
  .statusbar {
    padding: 0;
  }
  .statusbar .container.statusbar-row {
    /* Drop the wrap/space-between layout, become a single scrollable strip */
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 0;
    padding: 8px 16px;
    /* Pretty fade-out at right edge to hint at scrollability */
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent 100%);
            mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent 100%);
  }
  .statusbar .container.statusbar-row::-webkit-scrollbar { display: none; }

  .statusbar-left,
  .statusbar-right {
    display: inline-flex;
    flex-wrap: nowrap;
    gap: 18px;
    flex-shrink: 0;
  }
  .statusbar-left > *,
  .statusbar-right > * {
    flex-shrink: 0;
    white-space: nowrap;
  }
  .statusbar-right {
    margin-left: 18px;
  }
}

/* ---------- 3. Mobile nav drawer ---------- */
/* Drawer-only block lives inside <nav>; only visible inside the mobile drawer */
.nav .drawer-actions { display: none; }
@media (max-width: 899px) {
  .nav .drawer-actions { display: flex; }
}

/* Hamburger button — desktop hides, mobile shows */
.nav-toggle {
  display: none;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.nav-toggle:hover { border-color: var(--color-border-strong); }
.nav-toggle svg { width: 20px; height: 20px; }
.nav-toggle .ic-close { display: none; }
.nav-toggle[aria-expanded="true"] .ic-open { display: none; }
.nav-toggle[aria-expanded="true"] .ic-close { display: block; }

@media (max-width: 899px) {
  .nav-toggle { display: inline-flex; }
}

/* On mobile the inline <nav> + nav-actions are converted into a slide-in drawer */
@media (max-width: 899px) {
  /* Clip the off-screen drawer so it doesn't create page-level horizontal scroll */
  html,
  body {
    overflow-x: hidden;
  }

  .header .header-inner {
    /* brand left, hamburger right; everything else hidden until drawer opens */
    gap: var(--space-3);
  }

  /* The desktop nav and CTA are pushed into a drawer */
  .nav,
  .nav-actions {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: auto;
    height: 100vh;
    height: 100dvh;
    width: min(86vw, 340px);
    background: var(--color-surface);
    border-left: 1px solid var(--color-border);
    transform: translateX(100%);
    transition: transform 220ms cubic-bezier(0.2, 0.7, 0.2, 1);
    z-index: 100;
  }
  [data-theme='light'] .nav,
  [data-theme='light'] .nav-actions {
    background: #fff;
  }

  body.nav-open .nav,
  body.nav-open .nav-actions {
    transform: translateX(0);
  }

  /* Stack everything in the drawer using a single fixed nav, hide nav-actions duplicate */
  .nav {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    padding: 84px 22px 22px;
    overflow-y: auto;
  }
  .nav a {
    display: block;
    padding: 14px 4px;
    font-size: 1rem;
    border-bottom: 1px solid var(--color-border);
    color: var(--color-text);
  }
  .nav a:hover,
  .nav a.active {
    color: var(--color-primary);
  }

  /* Hide the desktop nav-actions (theme + CTA) and recreate inside drawer via a footer block */
  .nav-actions {
    display: none;
  }

  /* Inline drawer-actions block (added in HTML inside .nav) */
  .nav .drawer-actions {
    margin-top: 22px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    border-top: none;
  }
  .nav .drawer-actions .btn {
    width: 100%;
    text-align: center;
    padding: 12px 16px;
    border-radius: 10px;
  }
  .nav .drawer-actions .theme-toggle {
    align-self: flex-start;
  }

  /* Scrim behind drawer — z-index sits below drawer but above page; 
     keep below the hamburger (which is z-index 110) so user can still close it */
  body.nav-open::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(2px);
    z-index: 90;
  }
  body.nav-open {
    overflow: hidden;
  }

  /* Header sits above scrim so the hamburger remains tappable as the close button */
  .header {
    position: relative;
    z-index: 105;
  }
  body.nav-open .header {
    z-index: 120;
  }

  /* Hamburger sits on top of drawer */
  .nav-toggle {
    z-index: 130;
    position: relative;
  }
}

/* ---------- 4. Tables → label-value cards on phones ---------- */
@media (max-width: 720px) {
  /* Kill the horizontal scroll wrap; the card layout doesn't need it */
  .bot-table-wrap {
    overflow-x: visible;
    border: none;
    background: transparent;
    border-radius: 0;
  }

  .bot-table {
    min-width: 0;
    width: 100%;
    display: block;
    border-collapse: separate;
  }
  .bot-table thead {
    display: none; /* column headers are repeated as labels in each cell */
  }
  .bot-table tbody {
    display: block;
  }
  .bot-table tbody tr {
    display: block;
    margin-bottom: var(--space-4);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-3);
    border-left: 3px solid var(--color-border);
    overflow: hidden;
  }
  .bot-table tbody tr[data-layer="disc"]  { border-left-color: var(--layer-disc); }
  .bot-table tbody tr[data-layer="risk"]  { border-left-color: var(--layer-risk); }
  .bot-table tbody tr[data-layer="exec"]  { border-left-color: var(--layer-exec); }
  .bot-table tbody tr[data-layer="strat"] { border-left-color: var(--layer-strat); }
  .bot-table tbody tr[data-layer="intel"] { border-left-color: var(--layer-intel); }
  .bot-table tbody tr[data-layer="sec"]   { border-left-color: var(--layer-sec); }
  .bot-table tbody tr[data-layer="gov"]   { border-left-color: var(--layer-gov); }

  .bot-table td {
    display: block;
    width: 100% !important;
    padding: 10px 0;
    border-bottom: 1px solid var(--color-divider, var(--color-border));
    white-space: normal;
  }
  .bot-table td:last-child {
    border-bottom: none;
    padding-bottom: 2px;
  }

  /* Remove the desktop layer-color left accent on .col-id (the row already has one) */
  .bot-table tbody tr td.col-id {
    border-left: none;
  }

  /* Each cell gets a tiny label injected via data-label */
  .bot-table td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-family: var(--font-mono);
    font-size: 0.62rem;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--color-text-faint);
    margin-bottom: 6px;
  }

  /* Header row of card: # + Bot + Status, side-by-side */
  .bot-table td.col-id {
    display: inline-block;
    width: auto !important;
    padding: 4px 10px 4px 0;
    border: none;
    color: var(--color-text-faint);
    font-size: 0.78rem;
    vertical-align: middle;
  }
  .bot-table td.col-id::before { display: none; }

  .bot-table td.col-bot {
    display: inline-block;
    width: calc(100% - 80px - 90px) !important;
    padding: 4px 8px;
    border: none;
    vertical-align: middle;
  }
  .bot-table td.col-bot::before { display: none; }
  .bot-table td.col-bot strong {
    font-size: 1rem;
    color: var(--color-text);
    line-height: 1.2;
  }
  .bot-table td.col-bot .bot-flag {
    display: inline-block;
    margin-left: 6px;
    margin-top: 0;
  }

  .bot-table td.col-status {
    display: inline-block;
    width: auto !important;
    padding: 4px 0;
    border: none;
    text-align: right;
    float: right;
    vertical-align: middle;
  }
  .bot-table td.col-status::before { display: none; }

  /* Then the labelled cells appear below as a clear-fix */
  .bot-table td.col-goal {
    clear: both;
    margin-top: 6px;
    padding-top: 12px;
    border-top: 1px solid var(--color-divider, var(--color-border));
  }

  /* Lists inside cells should remain readable, no nowrap */
  .bot-table .cell-list {
    margin: 0;
    padding-left: 0;
    list-style: none;
  }
  .bot-table .cell-list li {
    position: relative;
    padding-left: 14px;
    margin: 4px 0;
    font-size: 0.86rem;
    line-height: 1.45;
    color: var(--color-text-soft);
    word-break: break-word;
  }
  .bot-table .cell-list li::before {
    content: "·";
    position: absolute;
    left: 4px;
    color: var(--color-text-faint);
  }
  .bot-table .cell-list.mono li {
    font-family: var(--font-mono);
    font-size: 0.78rem;
  }
}

/* ---------- 5. Toolbar — horizontal scroll chip rows + full-width search ---------- */
@media (max-width: 720px) {
  .filter-toolbar,
  .lib-toolbar {
    position: static; /* sticky on a phone is jumpy with safari */
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    align-items: stretch;
  }

  .chip-row,
  .filter-group {
    /* horizontal scroll, no wrap, fade right edge */
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 8px;
    padding-bottom: 4px;
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent 100%);
            mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent 100%);
  }
  .chip-row::-webkit-scrollbar,
  .filter-group::-webkit-scrollbar { display: none; }
  .chip-row > *,
  .filter-group > * { flex-shrink: 0; }

  .chip-label,
  .filter-label {
    align-self: center;
    margin-right: 4px;
  }

  .search-wrap,
  .filter-search {
    width: 100%;
    max-width: none;
    flex: none;
    margin-left: 0;
  }
  .search-wrap input,
  .filter-search input {
    font-size: 16px; /* prevent iOS zoom on focus */
  }
}

/* ---------- 6. Mobile typography + spacing tightening ---------- */
@media (max-width: 720px) {
  .container {
    padding-left: 16px;
    padding-right: 16px;
  }

  /* Hero — slightly tighter */
  .lib-title {
    font-size: clamp(1.85rem, 9vw, 2.6rem);
    line-height: 1.08;
  }
  .lib-eyebrow {
    font-size: 0.66rem;
    letter-spacing: 0.14em;
  }
  .lib-lede {
    font-size: 0.95rem;
    line-height: 1.6;
  }
  .lib-hero {
    padding-top: 28px;
    padding-bottom: 28px;
  }

  /* Section titles */
  .section h2 {
    font-size: clamp(1.5rem, 6.4vw, 2rem);
    line-height: 1.18;
  }

  .section {
    padding-top: 36px;
    padding-bottom: 36px;
  }

  /* Layer pill larger tap target */
  .layer-pill {
    padding: 6px 12px;
    font-size: 0.7rem;
  }

  /* Why builder grid — already 1 col, just trim padding */
  .why-grid {
    grid-template-columns: 1fr !important;
    gap: 10px;
  }

  /* Hard rules — single column */
  .rules-list,
  .rules-grid {
    grid-template-columns: 1fr !important;
  }

  /* Explainer band — already one column, ensure tight padding */
  .explainer-band {
    padding: 18px 14px;
  }
  .explainer-grid {
    grid-template-columns: 1fr !important;
    gap: 16px;
  }
  .explainer-item {
    padding-left: 12px;
  }
  .explainer-item h4 {
    font-size: 0.95rem;
  }
  .explainer-item p {
    font-size: 0.92rem;
    line-height: 1.6;
  }
  .term {
    word-break: break-word;
  }

  /* CTA / footer */
  .cta-band {
    padding: 22px 18px;
    text-align: center;
  }
  .cta-band .btn {
    width: 100%;
  }

  /* Code spec card — let it fit phone width */
  .spec-card,
  .order-spec,
  pre {
    font-size: 0.78rem;
    overflow-x: auto;
  }
}

/* ---------- Tiny phones (≤380px) — extra trim ---------- */
@media (max-width: 380px) {
  .container { padding-left: 12px; padding-right: 12px; }
  .bot-table tbody tr { padding: 12px; }
  .lib-title { font-size: clamp(1.6rem, 8.5vw, 2.2rem); }
}
