/* Legal DB — responsive layer (shared across landing, document, aggregate pages).
   Loaded AFTER the page's primary stylesheet so its @media rules win.
   See docs/legal-db-style-guide.md for the visual baseline this overrides. */

:root {
  /* Breakpoint tokens — used as documentation; @media queries are written
     literally because CSS doesn't interpolate vars into media conditions. */
  --bp-sm: 640px;   /* phone portrait */
  --bp-md: 1024px;  /* tablet / small laptop */
}

/* ---------- hamburger drawer (mobile nav) ----------
   CSS-only checkbox-sibling pattern. Hidden by default; shows at ≤640px.
   DOM order required: <input.hb-toggle> must precede .hb-drawer and
   .hb-backdrop so the `~` sibling selector reaches them when checked. */

.hb-toggle {
  position: absolute;
  width: 1px; height: 1px;
  opacity: 0;
  pointer-events: none;
}

.hb-btn,
.hb-drawer,
.hb-backdrop {
  display: none;
}

@media (max-width: 1024px) {
  /* CRITICAL: the site header uses backdrop-filter: blur() to fade content
     beneath it. Per CSS spec, ANY element with backdrop-filter becomes the
     containing block for descendants with position:fixed — meaning our drawer
     and bottom-sheet (both fixed-positioned and rendered inside <header>)
     would anchor to the 56px-tall header instead of the viewport, making the
     drawer invisible and the popover appear "stuck to the top".
     We disable backdrop-filter at tablet/phone widths so position:fixed
     resolves against the viewport. The visual loss is minimal — the header
     still has a solid background. */
  header.site-header {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

@media (max-width: 640px) {
  /* Hide the desktop inline nav; the hamburger replaces it. */
  header.site-header .header-cluster nav.top-nav { display: none; }

  /* Tighten the cluster so the brand + logo + hamburger fit comfortably. */
  header.site-header { padding: 0 12px; }
  .header-cluster { gap: 10px; }
  .header-lizzy-logo { height: 20px; }
  .brand.brand-legal-db { font-size: 16px; }

  /* Hamburger button — labeled to toggle #hb-toggle. 44×44 touch target.
     order:-1 makes it the first flex item; in RTL that's the VISUAL RIGHT,
     which is where users expect the menu trigger on Hebrew layouts. */
  .hb-btn {
    display: inline-flex;
    align-items: center; justify-content: center;
    width: 44px; height: 44px;
    border: 0; background: transparent;
    color: var(--text); cursor: pointer;
    border-radius: 8px;
    flex-shrink: 0;
    order: -1;
  }
  .hb-btn:hover, .hb-btn:focus { background: var(--surface-2); }
  /* SVG must not capture the click — otherwise the <label>'s for= association
     never fires when the user taps the icon center. */
  .hb-btn svg { display: block; pointer-events: none; }

  /* Slide-in drawer from the right (RTL — matches where the cluster anchors).
     Internal flex column with 3 sections: head (branded), nav (grows + scrolls),
     foot (legal links + copyright pinned to bottom). */
  .hb-drawer {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; right: 0; bottom: 0;
    width: min(82vw, 320px);
    background: var(--surface);
    border-inline-start: 1px solid var(--border);
    box-shadow: -8px 0 24px rgba(14, 16, 19, 0.10);
    transform: translateX(100%);
    transition: transform 220ms ease;
    z-index: 300;
    padding: 0;
    overflow: hidden;        /* sections manage their own scroll */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Drawer head — LegalDB brand + icon. Mirrors the desktop header pattern. */
  .hb-drawer-head {
    display: flex; align-items: center; gap: 10px;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border-light);
    font-weight: 700; font-size: 17px; color: var(--text);
    letter-spacing: -0.2px;
    flex-shrink: 0;
  }
  .hb-drawer-head img { display: block; flex-shrink: 0; }

  /* Nav section grows to fill — scrolls if links overflow. */
  .hb-drawer nav {
    display: flex; flex-direction: column; gap: 2px;
    padding: 10px 12px;
    flex: 1 1 auto;
    overflow-y: auto;
  }
  .hb-drawer a {
    display: block;
    padding: 12px 14px;
    font-size: 16px; color: var(--text);
    border-radius: 10px;
    min-height: 44px;
    text-decoration: none;
  }
  .hb-drawer a.current { color: var(--accent-dark); font-weight: 600; }
  .hb-drawer a:hover,
  .hb-drawer a:focus { background: var(--surface-2); color: var(--accent); }

  /* Drawer foot — pinned to bottom. Two rows: legal links, then © line. */
  .hb-drawer-foot {
    flex-shrink: 0;
    padding: 14px 18px 16px;
    border-top: 1px solid var(--border-light);
    background: var(--surface);
  }
  .hb-foot-links {
    display: flex; align-items: center; flex-wrap: wrap; gap: 8px;
    font-size: 13px;
    margin-bottom: 6px;
  }
  .hb-foot-links a { color: var(--text-muted); text-decoration: none; }
  .hb-foot-links a:hover { color: var(--accent); text-decoration: underline; }
  .hb-foot-links span { color: var(--text-dim); }
  .hb-foot-copy {
    font-size: 12px; color: var(--text-dim);
  }

  /* Dim backdrop behind the drawer. */
  .hb-backdrop {
    display: block;
    position: fixed; inset: 0;
    background: rgba(14, 16, 19, 0.40);
    z-index: 250;
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
  }

  /* Open state — checkbox toggled by the <label> button. */
  .hb-toggle:checked ~ .hb-drawer    { transform: translateX(0); }
  .hb-toggle:checked ~ .hb-backdrop  { opacity: 1; pointer-events: auto; }
}

/* ---------- global touch + safe-area niceties ---------- */
@media (max-width: 640px) {
  /* Make sure the filter pill itself meets the 44px touch guideline. The
     desktop rule sets it to ~32px; bump padding to grow vertical hit area
     without changing the visual proportions too much. */
  .filter-toggle { padding: 10px 14px; min-height: 40px; }

  /* Avoid horizontal scroll surprises on long Hebrew strings inside cards. */
  body { overflow-x: hidden; }

  /* Bottom safe-area for sticky footers / fixed bottom UI. */
  .site-footer {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
}

/* ---------- HOME — results bar, cards, sidebar tabs ---------- */
@media (max-width: 640px) {
  /* Main column padding shrinks. */
  .main { padding: 0 14px 60px; }

  /* Hero — drop the big 44px title to a phone-friendly size. */
  .hero { padding: 14px 16px 22px; gap: 16px; }
  .hero-title { font-size: clamp(26px, 7vw, 34px); line-height: 1.2; }

  /* Results bar (title + sort) — stack so the sort dropdown gets its own line. */
  .results-bar {
    flex-direction: column; align-items: stretch;
    gap: 10px; margin-bottom: 14px;
  }
  .results-bar .sort { justify-content: flex-end; }
  .sort select { padding: 8px 12px; font-size: 14px; }   /* taller for touch */

  /* Result cards — stack body internally, smaller padding. */
  .result-card { padding: 12px 14px; gap: 10px; align-items: flex-start; }
  .rc-top { flex-wrap: wrap; gap: 8px; }
  .rc-title { font-size: 14.5px; }
  .rc-meta { font-size: 12px; }

  /* Sidebar tab strip — let labels scroll horizontally instead of squishing. */
  .filter-panel .ftabs {
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;          /* Firefox */
    padding-bottom: 2px;            /* leave room for active underline */
  }
  .filter-panel .ftabs::-webkit-scrollbar { display: none; }
  .filter-panel .ftab {
    flex: 0 0 auto;
    scroll-snap-align: start;
    padding: 12px 14px;             /* touch-friendly */
    font-size: 13px;
  }

  /* Sidebar sits below the results column already (via the existing 768px
     rule). Drop its sticky positioning on phones — sticky in a flow that
     ends below 1 viewport scrolls weirdly. */
  .sidebar { position: static; }
}

/* ---------- Cited-tab overflow guard (applies at ALL widths) ----------
   Repeated reports of "clicking מצוטטים widens the page" trace to a chain
   of CSS defaults that lets cited content escape the 320px sidebar:
     1. <strong> is `display: inline` — overflow/text-overflow/white-space
        don't apply to inline boxes until they become flex items (block-ified).
        Force display: block to guarantee they ellipsize.
     2. Grid items default to min-width: auto (= min-content). If the cited
        case_name has no spaces it pushes the sidebar column past 320px.
        Force min-width: 0 on the sidebar grid item.
     3. body has no overflow-x setting at desktop — anything that does escape
        produces a horizontal scrollbar that the user perceives as the page
        "growing wider". Clip at the body level universally. */
html, body { overflow-x: hidden; }
.results-layout, .sidebar, .filter-panel, .fpanels, .fpanel { min-width: 0; }
.entity-list { min-width: 0; max-width: 100%; }
.entity-list li { min-width: 0; max-width: 100%; }
.entity-list a { min-width: 0; max-width: 100%; }
.entity-name { min-width: 0; max-width: 100%; }
.entity-name.cited-name {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
.cited-desc,
.cited-title {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- FILTER → bottom sheet on phones ---------- */
@media (max-width: 640px) {
  /* The popover is shown by removing the [hidden] attribute via the page's
     inline JS — same JS still works, we only restyle the panel itself.
     On phones we anchor to the bottom of the viewport, slide it up, and
     give it a drag-handle so the affordance reads as a sheet. */
  .filter-popover {
    position: fixed;
    top: auto; left: 0; right: 0; bottom: 0;
    max-width: 100%; min-width: 0;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 24px rgba(14, 16, 19, 0.18);
    padding: 18px 16px max(20px, env(safe-area-inset-bottom));
    z-index: 400;
    max-height: 78vh;
    overflow-y: auto;
    animation: filter-sheet-in 220ms ease-out;
  }
  /* Drag-handle pill above the form content. */
  .filter-popover::before {
    content: "";
    display: block;
    width: 42px; height: 4px;
    margin: -4px auto 14px;
    background: var(--text-dim);
    border-radius: 2px;
    opacity: 0.45;
  }
  /* Bigger touch targets inside the form. */
  .filter-form { gap: 18px; }
  .filter-year-range input,
  .filter-min-select { padding: 12px 14px; font-size: 15px; min-height: 44px; }
  .filter-actions { padding-top: 16px; gap: 12px; }
  .filter-clear { padding: 10px 0; font-size: 14px; min-height: 44px; display: inline-flex; align-items: center; }
  .filter-apply { padding: 12px 22px; font-size: 14px; min-height: 44px; }
}
@keyframes filter-sheet-in {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

/* ---------- DOCUMENT page — stack summary on top, PDF below.
   The desktop layout fixes <html>/<body> height to 100% with overflow:hidden
   so the split-pane can manage its own scroll. On phone/tablet we abandon
   that contract: body becomes a normal scrolling document, .split becomes a
   vertical flex column, summary above PDF. Triggers at ≤1024 — tablet
   portrait + phone, since the side-by-side becomes uncomfortable below that. */
@media (max-width: 1024px) {
  /* Restore document-flow scrolling. The desktop rule `html, body { height:
     100%; overflow: hidden }` blocks page scroll, which would trap the user
     above the PDF pane on mobile. */
  html, body {
    height: auto;
    overflow: auto;
    overflow-x: hidden;
  }
  /* Body was display:grid with rows auto 1fr auto. Drop to normal block
     flow so header → breadcrumb → split (summary, pdf) → footer stack as
     content, not as fixed-height grid rows. */
  body {
    display: block;
    min-height: 100vh;
  }

  /* Replace the 3-column grid with a vertical flex column. Summary is the
     FIRST DOM child of .split and renders first; PDF (3rd child) below.
     The 8px resizer (2nd child) is hidden — pointer-resize isn't useful
     on touch. */
  .split {
    display: flex;
    flex-direction: column;
    height: auto;
    overflow: visible;
  }
  .resizer { display: none; }

  /* Summary pane now flows naturally in the page — drop fixed height,
     keep the separator below it (was border-inline-start; on mobile the
     visual separation is a horizontal underline instead). */
  .summary-pane {
    height: auto;
    overflow: visible;
    border-inline-start: 0;
    border-bottom: 1px solid var(--border);
  }
  .summary-inner { padding: 14px 16px 20px; max-width: none; }

  /* PDF pane gets a fixed, screen-fillable height so PDF.js can render
     pages with its internal scrollbar. Without an explicit height it would
     collapse to 0 in flex-column flow. ~80vh leaves room for the doc-strip
     above it to show that more content lives there. */
  .pdf-pane {
    height: 80vh;
    min-height: 480px;
  }
}

/* Phone-portrait tweaks — only at ≤640. */
@media (max-width: 640px) {
  /* Smaller breadcrumb padding + tighter case-desc line. The existing 720px
     rule already wraps; we just tune sizes. */
  .breadcrumb-bar { padding: 8px 12px; font-size: 12.5px; }
  .breadcrumbs { gap: 4px; font-size: 12.5px; }
  .breadcrumbs .current { max-width: 100%; }
  .doc-meta { gap: 6px; flex-wrap: wrap; }

  /* Back control — strip the button chrome (border + bg + padding + label)
     down to just the chevron glyph as a tap target. Keeps aria-label intact
     so screen readers still announce "חזרה". The textual "חזרה" word inside
     the anchor is hidden visually with font-size:0 trick on the anchor while
     the chevron child stays visible at its own font-size. */
  .back-btn {
    background: transparent;
    border: 0;
    padding: 0;
    border-radius: 0;
    color: var(--text-muted);
    font-size: 0;                /* eats the "חזרה" text node */
    line-height: 1;
    min-width: 44px; min-height: 44px;
    justify-content: center;
  }
  .back-btn:hover {
    background: transparent;
    color: var(--accent);
    border-color: transparent;
  }
  .back-arrow {
    font-size: 28px;             /* re-establish font size for the chevron only */
    line-height: 1;
  }

  /* Doc-ask placeholder bar — drop the floating shadow, keep the rounded
     pill but at the page width. */
  .doc-ask { padding: 6px 10px 6px 6px; box-shadow: none; }
  .doc-ask-input { font-size: 14px; }

  /* PDF pane goes a bit shorter on phone to leave summary visible above. */
  .pdf-pane { height: 75vh; min-height: 420px; }

  /* PDF.js zoom buttons aren't useful on touch — pinch-zoom is native and
     more precise. Drop the whole toolbar to give the PDF more vertical
     room. (Kept at tablet portrait too — same reasoning.) */
  .pdf-toolbar { display: none; }
}

/* ---------- Citation tooltip "find location" icon.
   Desktop (>1024): hidden — the pill click jumps immediately, no need.
   Stacked (≤1024): a bare 16px SVG glyph in the tooltip's bottom-left
   corner. No background / no border. Invisible padding extends the touch
   target to ~40px so the small visible icon stays comfortable to tap. */
.pop-jump { display: none; }
@media (max-width: 1024px) {
  /* Tooltip needs relative positioning so the absolute button anchors to it. */
  #par-popover { position: fixed; }   /* already fixed; explicit for the abs child */
  .pop-jump {
    display: inline-flex;
    align-items: center; justify-content: center;
    position: absolute;
    bottom: 0; left: 0;              /* visual bottom-left in RTL */
    padding: 12px;                   /* invisible hit-area extension */
    background: transparent;
    color: var(--accent, #25A7FF);
    border: 0;
    cursor: pointer;
    /* Kills iOS Safari's 300ms double-tap-zoom delay on this control —
       without it taps can be swallowed when iOS waits to see if a second
       tap is coming. */
    touch-action: manipulation;
  }
  .pop-jump:hover,
  .pop-jump:focus { color: var(--accent-dark, #1991DC); outline: none; }
  .pop-jump svg {
    display: block;
    width: 16px; height: 16px;       /* visible glyph */
    pointer-events: none;            /* so the click target is always the button */
  }

  /* Tooltip body needs a little bottom padding so its last line of text
     doesn't run under the floating icon. */
  #par-popover .pop-body { padding-bottom: 28px; }
}

/* ---------- AGGREGATE pages (court / judge / party / lawyer / topic / case) ---------- */
@media (max-width: 640px) {
  /* Tighter header card. */
  .agg-header { padding: 16px 16px; margin: 14px 0 12px; border-radius: 10px; }
  .agg-header h1 { font-size: 20px; }
  .agg-meta { font-size: 13px; }

  /* Filter pill row — already flex-wraps; just shrink. */
  .agg-filter { gap: 8px; margin: 12px 0; }
  .agg-filter a { padding: 6px 12px; font-size: 13px; min-height: 36px; display: inline-flex; align-items: center; }

  /* Doc cards — stack the head row so the date drops below instead of
     getting auto-margin-pushed off the right edge. */
  .doc-card { padding: 12px 14px; border-radius: 8px; }
  .doc-card .head { gap: 6px; }
  .doc-card .doc-date { margin-inline-start: 0; width: 100%; order: 99; }
  .doc-card h3 { font-size: 14.5px; }
}
