/* ============================================================
   Rapid Index — shared mobile experience layer
   Linked from every page. EVERYTHING in this file lives inside a
   max-width media query on purpose — this file's only job is
   mobile responsiveness. It must never change how the site looks
   on desktop. Rules use !important because each page ships its
   own inline <style> block and this file needs to reliably win
   on small screens regardless of link order.
   ============================================================ */

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

/* The hamburger button, its close (×) button, and the backdrop are
   injected by mobile-nav.js on every page load regardless of screen
   width. Without an explicit default here, they render as bare,
   unstyled elements on desktop (this is exactly what caused a stray
   "×" to show up next to the desktop logo). Hidden by default;
   the mobile media query below is the only place that turns them on. */
.rp-menu-btn, .rp-menu-close, .rp-nav-backdrop {
  display: none !important;
}

@media (max-width: 768px) {

  /* ---- Base readability: nobody should ever need to pinch-zoom ---- */
  body { font-size: 16px !important; line-height: 1.55 !important; }
  h1 { font-size: 26px !important; line-height: 1.2 !important; }
  h2 { font-size: 21px !important; line-height: 1.25 !important; }
  h3 { font-size: 17px !important; line-height: 1.3 !important; }
  p, li, td, th, label { font-size: 15px !important; }

  /* iOS Safari auto-zooms into any input/textarea/select under 16px —
     keeping everything at 16px+ stops that entirely. */
  input, textarea, select, button { font-size: 16px !important; }

  /* ---- Use the available screen width, keep comfortable margins ---- */
  .wrap { max-width: 100% !important; padding-left: 18px !important; padding-right: 18px !important; }

  /* ---- Grids: stack to one column on mobile (the live-polls feed
     is handled separately below — it stays horizontal) ---- */
  .article-grid, .region-grid, .method-grid, .port-grid,
  .services, .foot-grid, .cand-grid, .split-grid, .meta-grid,
  .index-grid, .services-grid, .about .wrap, .flagship .wrap,
  .hero .wrap { grid-template-columns: 1fr !important; }

  /* ================================================================
     NAVIGATION — hamburger + real off-canvas drawer.
     .navlinks is always display:flex now (not display:none), and
     visibility is controlled purely by transform, so it can slide
     in/out instead of pushing page content around.
     ================================================================ */
  header .nav, header .sitenav {
    padding: 12px 18px !important;
    position: relative;
  }

  /* Logo: pin height + let width follow its natural ratio, and keep
     it OUT of the generic "img{max-width:100%}" rule below — that
     rule is what was squishing it. */
  header img, .sitenav img, .nav img, .brand img {
    height: 28px !important;
    width: auto !important;
    max-width: none !important;
  }

  .rp-menu-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    margin-left: auto;
    background: transparent;
    border: 1px solid var(--line, #E2E5EA);
    border-radius: 6px;
    cursor: pointer;
    flex-shrink: 0;
  }
  .rp-menu-btn span,
  .rp-menu-btn span::before,
  .rp-menu-btn span::after {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--navy, #002143);
    position: relative;
    transition: transform 0.15s ease, opacity 0.15s ease;
  }
  .rp-menu-btn span::before, .rp-menu-btn span::after { content: ''; position: absolute; left: 0; }
  .rp-menu-btn span::before { top: -6px; }
  .rp-menu-btn span::after { top: 6px; }
  .rp-menu-btn.open span { background: transparent; }
  .rp-menu-btn.open span::before { transform: translateY(6px) rotate(45deg); }
  .rp-menu-btn.open span::after { transform: translateY(-6px) rotate(-45deg); }

  /* nav-btn / primary CTA (e.g. "Get in touch") stays in the header
     row rather than being forced full-width — the drawer holds the
     link list, this stays put next to the hamburger. */
  header .nav-btn, header .btn-primary { flex-shrink: 0; }

  .navlinks {
    display: flex !important;
    flex-direction: column !important;
    position: fixed !important;
    top: 0 !important;
    right: 0 !important;
    height: 100vh !important;
    width: min(78vw, 320px) !important;
    background: var(--paper, #fff) !important;
    box-shadow: -8px 0 28px rgba(0,0,0,0.18);
    padding: 78px 0 24px !important;
    gap: 0 !important;
    overflow-y: auto;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    z-index: 1001;
  }
  .navlinks.rp-open { transform: translateX(0) !important; }
  .navlinks a {
    display: block !important;
    width: 100% !important;
    padding: 15px 24px !important;
    font-size: 16px !important;
    min-height: 44px;
    box-sizing: border-box;
    border-bottom: 1px solid var(--line, #E2E5EA) !important;
  }

  .rp-nav-backdrop {
    display: block !important;
    position: fixed;
    inset: 0;
    background: rgba(10,16,24,0.45);
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }
  .rp-nav-backdrop.rp-open { opacity: 1; pointer-events: auto; }

  .rp-menu-close {
    display: block !important;
    position: absolute;
    top: 18px;
    right: 18px;
    width: 36px;
    height: 36px;
    border: none;
    background: var(--bg, #F6F7F9);
    border-radius: 50%;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    color: var(--navy, #002143);
  }

  body.rp-nav-locked { overflow: hidden !important; }

  /* ---- Buttons & tap targets everywhere: min 44px, roomy padding ---- */
  .btn, .btn-primary, .btn-ghost, button,
  .vote-btn, .tile-btn, a.tile-btn,
  #login-btn, #publish-btn, #logout-btn {
    min-height: 46px !important;
    padding: 13px 18px !important;
    font-size: 15px !important;
  }

  /* ---- Spacing between adjacent interactive elements, to avoid mis-taps ---- */
  .candidate, .region-card, .article-card, .cand-card,
  .index-card, .method-card { margin-bottom: 4px; }
  .row2 { gap: 12px !important; }

  /* ---- Bold stat cards: 2x2 grid, bigger numbers, icon badges.
     Scoped inside this mobile-only media query on purpose — this
     must never change the desktop stats bar. ---- */
  .stats-grid, .stat-row {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 12px !important;
  }
  .stat-cell {
    background: rgba(255,255,255,0.04) !important;
    border: 1px solid rgba(255,255,255,0.14) !important;
    border-radius: 12px !important;
    padding: 20px 12px !important;
  }
  .icon-badge {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: rgba(185,21,28,0.16);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 10px;
  }
  .icon-badge svg { width: 19px; height: 19px; color: var(--red, #e0464c); }
  .stat-cell.countdown .icon-badge { background: rgba(224,70,76,0.22); }
  .stat-cell .n { font-size: 26px !important; font-weight: 700 !important; line-height: 1.1; }
  .stat-cell .l { margin-top: 6px !important; font-size: 11px !important; }

  /* ---- Poll candidate rows: stack the vote button below, full width,
     instead of squeezing a 3rd grid column next to the name/bar ---- */
  .candidate {
    grid-template-columns: 48px 1fr !important;
    row-gap: 10px !important;
    padding: 14px 0 !important;
  }
  .candidate img, .avatar-fallback { width: 44px !important; height: 44px !important; }
  .candidate .vote-btn,
  .candidate .voted-tag {
    grid-column: 1 / -1 !important;
    width: 100% !important;
    text-align: center !important;
    margin-top: 4px !important;
  }

  /* ---- Cards / tiles scale cleanly. Logo is excluded above, so this
     generic image rule only ever touches content images (candidate
     photos, article thumbnails, etc). ---- */
  .poll-card, .panel, .poll-tile, .region-card, .article-card,
  .cand-card, .index-card { padding: 18px !important; }
  main img, article img, .cand-photo, .thumb img,
  .poll-card img, .poll-tile img, .article-card img {
    max-width: 100% !important;
    height: auto !important;
  }

  /* ================================================================
     "What people are voting on right now" — stays HORIZONTAL on
     mobile: a swipeable row, one card mostly in view with a peek of
     the next, snapping as you swipe left/right. Applies to both the
     homepage tiles (.poll-tile) and the polls.html listing cards
     (.poll-card), since both share the .poll-grid container.
     ================================================================ */
  .poll-grid {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    overflow-y: visible !important;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    gap: 14px !important;
    padding: 4px 18px 14px !important;
    margin: 0 -18px !important;
  }
  .poll-tile, .poll-grid > .poll-card, #poll-grid .poll-card {
    flex: 0 0 86% !important;
    width: 86% !important;
    min-width: 0 !important;
    scroll-snap-align: center;
  }
  .poll-tile .tag { font-size: 11px !important; }
  .poll-tile h3, .poll-grid .poll-card h3 { font-size: 18px !important; }
  .poll-tile a.tile-btn { padding: 13px !important; }

  /* ---- Comments (poll-engine-firebase.js) already scales reasonably,
     just widen tap targets to match the rest of the mobile rules ---- */
  .rp-comment-form button { min-height: 44px !important; padding: 12px 16px !important; }
  .rp-comment-form textarea { font-size: 16px !important; }

  /* ---- Article / report grids: single full-width column ---- */
  .article-card { grid-template-columns: 1fr !important; }

  /* ---- Footer: single column, roomier tap targets on links ---- */
  .foot-grid a { padding: 8px 0 !important; }
  .foot-bottom { flex-direction: column !important; gap: 8px !important; text-align: center !important; }

  /* ---- Tables that would otherwise force zoom: let them scroll horizontally
     as a controlled last resort, rather than shrinking text ---- */
  table.data, table.preview { display: block !important; overflow-x: auto !important; white-space: nowrap !important; }
}

@media (max-width: 420px) {
  /* Extra-small phones: trim margins a touch further */
  .wrap { padding-left: 14px !important; padding-right: 14px !important; }
  .poll-tile, .poll-grid > .poll-card, #poll-grid .poll-card { flex-basis: 90% !important; width: 90% !important; }
}
