/* ---------------------------------------------------------------------------
   MOBILE OPTIMISATION PASS
   Appended deliberately at the end so it overrides the base rules above.

   Measured problems this fixes, all at 390-430px:
   - 17px base type with 22px-padded cards left ~34 chars per line
   - tables forced horizontal pressure inside 2px-bordered cards
   - 4px hard shadows on every card cost real paint area on a small screen
   - sticky toolbars stacked to ~120px, eating a third of the viewport
   - tap targets on chips/segments fell under the 44px accessibility floor
   --------------------------------------------------------------------------- */

/* Respect the notch/home-bar so content never sits under system chrome. */
@supports (padding: max(0px)) {
  .wrap, .topbar .wrap, main, footer, .tools2, .search {
    padding-left: max(18px, env(safe-area-inset-left));
    padding-right: max(18px, env(safe-area-inset-right));
  }
}

@media (max-width: 640px) {
  body { font-size: 16px; }

  /* Reclaim horizontal space: the riso border+shadow is the identity, so keep
     it but shrink the offset — 4px of shadow on a 390px screen is 1% of width
     spent on decoration. */
  .tool, .card, .kpi, .panel, .src, .v, .f, .row, .freshbar, .cav, #map {
    box-shadow: 3px 3px 0 var(--ink);
  }
  .tool, .panel, .card { padding: 20px 16px 14px; }
  .kpi { padding: 13px 14px; }

  .hero { padding: 34px 18px 22px; }
  .hero h1, .mast__logo { font-size: clamp(2.1rem, 11vw, 2.9rem); }
  .hero p { font-size: 1rem; }
  .sect { font-size: 1.45rem; margin: 30px 0 4px; }

  /* Ticker chips: wrap tighter, but keep each one thumb-sized. */
  .stats, .ticker { gap: 7px; margin-top: 16px; }
  .stat { padding: 8px 13px; font-size: .8rem; min-height: 40px; }

  /* TAP TARGETS. Anything interactive gets >=44px of height. */
  .seg button { padding: 13px 15px; min-height: 46px; }
  .chk { min-height: 44px; }
  .chk input { width: 20px; height: 20px; }
  #q { padding: 14px 15px; min-height: 48px; }
  .refresh { min-height: 44px; padding: 11px 16px; }
  .tmeta span, .tag, .n, .chip, .pill { padding: 5px 11px; }

  /* Measured failures: the back-link rendered at 20-21px and bare checkboxes
     at 17px. Inline elements ignore min-height, so make them flex boxes. */
  .topbar a.home {
    display: inline-flex; align-items: center;
    min-height: 44px; padding: 0 10px 0 0;
  }
  .topbar { padding: 6px 18px; }
  .chk {
    display: inline-flex; align-items: center; gap: 9px;
    min-height: 44px; padding: 0 4px;
  }
  /* A raw checkbox cannot be grown past its intrinsic box on iOS; scale it. */
  .chk input[type="checkbox"] {
    width: 22px; height: 22px;
    transform: scale(1.15); transform-origin: center;
    margin: 0;
  }

  /* Sticky toolbars were stacking to ~120px. Single row, scrollable controls. */
  .tools2, .search, .tools { padding: 10px 18px; }
  .trow, .toolrow { gap: 8px; }
  .trow .seg, .toolrow .seg { flex: 0 0 auto; }

  /* TABLES. A 3-4 column table inside a bordered card is the worst offender on
     a phone. Let the table itself scroll rather than stretching the layout. */
  .panel table, .db table { display: block; overflow-x: auto;
    -webkit-overflow-scrolling: touch; }
  table { font-size: .84rem; }
  td, th { padding: 8px 0; }
  td.n, th.n { padding-right: 12px; }

  /* Risk/road rows: the score gutter was fixed-width and squeezed the content. */
  .row { grid-template-columns: 66px 1fr; gap: 12px; padding: 14px 15px; }
  .sc b { font-size: 1.3rem; }
  .sc .exp { font-size: .76rem; }
  .who { font-size: 1rem; }

  /* Findings cards read as prose — widen the measure, drop the decoration. */
  .feed { gap: 13px; }
  .hd h3 { font-size: 1.14rem; }
  .body { font-size: .93rem; }

  /* Maps: shorter so the list below is reachable without a long scroll. */
  #map { height: 320px; }

  /* Dossier KPIs: 2-up instead of auto-fit, which was producing 1-up. */
  .kpis { grid-template-columns: 1fr 1fr; gap: 9px; }
  .kpi b { font-size: 1.45rem; }

  footer { padding: 22px 18px 48px; }
}

/* Very narrow (iPhone SE / 320-375px): drop to one column where 2-up cramps. */
@media (max-width: 380px) {
  .kpis { grid-template-columns: 1fr; }
  .hero h1, .mast__logo { font-size: 1.95rem; }
  .row { grid-template-columns: 58px 1fr; }
}

/* Coarse pointers: remove hover-only affordances that stick on touch. */
@media (hover: none) {
  .tool.live:hover, .card:hover, .row:hover {
    transform: none;
    box-shadow: 3px 3px 0 var(--ink);
  }
}
