/* ============================================================
   MOBILE RESPONSIVENESS LAYER (phones <=768px, small <=480px)
   Loaded after tailwind.css + refactored.css in theme_head.ejs.
   Goals: kill horizontal overflow, stack crowded rows, enlarge
   touch targets, keep desktop untouched (>=769px = no-op).
   ============================================================ */

/* ---------- 0. Base: never allow page-level sideways scroll ---------- */
html, body {
  overflow-x: clip;
  max-width: 100vw;
}
@supports not (overflow: clip) {
  html, body { overflow-x: hidden; }
}
img, svg, canvas, video {
  max-width: 100%;
  height: auto;
}
img:not([height]) { height: auto; }

/* Safe-area for notched phones */
body {
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

/* Toasts must never cover the full screen on phones */
.toast {
  max-width: calc(100vw - 32px);
  left: 16px;
  right: 16px;
}
@media (min-width: 640px) {
  .toast { left: auto; }
}

/* All floating dropdowns / tooltips stay inside the viewport */
.custom-select-options,
#policyNoteDropdown,
#pubnav-mobile-panel {
  max-width: calc(100vw - 32px);
}

/* Range thumbs: 18px -> 28px hit area on touch */
@media (pointer: coarse) {
  input[type="range"]::-webkit-slider-thumb {
    height: 28px;
    width: 28px;
    margin-top: -11px;
  }
  input[type="range"]::-moz-range-thumb {
    height: 28px;
    width: 28px;
  }
}

/* Kill heavy blur orbs on low-end phones (paint cost, no layout change) */
@media (max-width: 768px) {
  .orb-3 { display: none; }
  .orb-1, .orb-2 { opacity: calc(var(--orb-opacity, 0.15) * 0.6); }
}

/* ---------- 1. Public nav / landing nav ---------- */
@media (max-width: 768px) {
  /* Status page badge: .dribbble-badge sets display:inline-flex AFTER
     tailwind.css, so Tailwind's `hidden` loses the cascade — hide by id. */
  #statusNavBadge { display: none !important; }
  /* Larger tap targets for hamburger + theme toggle */
  #pubnav-menu-btn,
  #theme-toggle-btn,
  #theme-toggle-btn-mobile {
    min-width: 44px;
    min-height: 44px;
    width: 44px;
    height: 44px;
  }
  /* Mobile panel drops below fixed h-16 bar without clipping */
  #pubnav-mobile-panel {
    position: absolute;
    top: calc(100% + 8px);
    left: 16px;
    right: 16px;
    margin: 0;
    max-height: calc(100dvh - 80px);
    max-height: calc(100vh - 80px);
    overflow-y: auto;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  }
  #pubnav-mobile-panel a {
    min-height: 44px;
    display: flex;
    align-items: center;
  }
}

/* ---------- 2. Landing hero + terminal ---------- */
@media (max-width: 640px) {
  /* Hero: 36px -> 30px so headline doesn't push CTA 5 screens down */
  section h1.text-4xl {
    font-size: 1.875rem;
    line-height: 1.15;
  }
  section h1 .text-2xl {
    font-size: 1.25rem;
    line-height: 1.3;
  }
  .terminal-simulator-card {
    padding: 1rem;
    font-size: 0.75rem;
    line-height: 1.55;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .terminal-simulator-card .grid.grid-cols-2 {
    grid-template-columns: 1fr;
  }
  #term-logs > div {
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  /* Disable power-hungry 3D float on phones */
  .wrapper-float-3d { animation: none; }
}

/* ---------- 3. Browser mockup bezels (triage/policy/settings/logs) ---------- */
@media (max-width: 640px) {
  .browser-mockup-frame > div:first-child {
    flex-wrap: wrap;
    gap: 8px;
    row-gap: 8px;
  }
  /* URL pill truncates with a real ellipsis (text wrapped in .mock-url-text) */
  .browser-mockup-frame > div:first-child > div:first-child span[class*="font-mono"] {
    max-width: 200px;
    min-width: 0;
  }
  .mock-url-text {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Status badges take the full row and wrap — never clipped, never capped */
  .browser-mockup-frame > div:first-child > div:last-child span {
    max-width: none;
    white-space: normal;
    text-align: center;
    line-height: 1.4;
    font-size: 9px;
  }
  /* Cockpit Segmented Control: single-row horizontally scrollable on small screens */
  .cockpit-segmented-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    padding-bottom: 0.25rem;
  }
  .cockpit-segmented-control {
    width: max-content;
    min-width: 100%;
    flex-wrap: nowrap !important;
  }
  .cockpit-tab-btn {
    flex: 0 0 auto !important;
    white-space: nowrap;
    min-height: 42px;
    padding-left: 0.875rem;
    padding-right: 0.875rem;
    justify-content: center;
  }
}

/* ---------- 4. Comparison table safety net ---------- */
.comparison-table-wrapper {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}
.canonical-model-matrix {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}
pre {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
pre code {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  word-break: normal;
}
@media (max-width: 767px) {
  .comparison-table th,
  .comparison-table td {
    padding: 1rem;
  }
}

/* ---------- 5. Dashboard shell (index.ejs / merchant.ejs) ---------- */
@media (max-width: 1023px) {
  /* Gutters eat 16% of a 390px screen — reclaim them */
  #contentArea { padding: 1rem; }
  /* Topbar: hide low-value metadata that crowds the 64px row */
  #lastRefreshedText,
  #autoRefreshCountdown { display: none; }
  header.glass-topbar { padding-left: 1rem; padding-right: 1rem; }
  header.glass-topbar .mobile-nav-toggle {
    min-width: 44px;
    min-height: 44px;
  }
}
@media (max-width: 640px) {
  /* Export + jurisdiction selects collapse to icon-adjacent; keep time range */
  #exportSelect { display: none; }
  #pageSubtitle { display: none; }
  /* Notification + custom dropdowns fit inside 390px */
  #policyNoteDropdown {
    width: calc(100vw - 32px);
    max-width: 20rem;
    position: fixed;
    right: 16px;
    left: 16px;
    margin-top: 8px;
    /* glass-card is translucent: banner text behind the dropdown bled
       through and the two texts mashed into one unreadable block. */
    background: var(--bg-card-solid);
  }
  .custom-select-options {
    max-width: calc(100vw - 32px);
  }
  /* Pilot / BYOK / email banners stack instead of side-squeeze */
  #pilotModeBanner,
  #byokMissingKeyBanner,
  #emailVerificationBanner,
  #globalAlertBanner {
    flex-direction: column;
    align-items: stretch;
  }
  #pilotModeBanner > button,
  #byokMissingKeyBanner > button,
  #emailVerificationBanner > button,
  #globalAlertBanner > div:last-child {
    width: 100%;
    justify-content: center;
  }
  /* Engine status strip wraps instead of clipping under overflow:hidden */
  #contentArea .glass-card.overflow-hidden:has(#uptime),
  #contentArea div:has(> #uptime) {
    flex-wrap: wrap;
  }
  /* Generic guard: the engine strip is the flex row containing #uptime */
  #uptime { white-space: nowrap; }
  /* Recent-scans + triage tables: horizontal swipe with momentum */
  #recentScans,
  .table-responsive-wrap {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }
  #recentScans table,
  .table-high-density {
    min-width: 640px;
  }
  /* Hide least-critical columns on phones (kept on md+) */
  #recentScansTableBody td:nth-child(2),
  #recentScans thead th:nth-child(2),
  #recentScansTableBody td:nth-child(4),
  #recentScans thead th:nth-child(4),
  #recentScansTableBody td:nth-child(6),
  #recentScans thead th:nth-child(6) {
    display: none;
  }
  /* Policy network graph: pan sideways instead of crushing the 9px node
     labels. Scoped to the graph itself -- `.policy-network-container svg`
     also matched the clock icon in the card title and blew it up to
     520x520px, which stretched the heading to 591px wide. */
  .policy-network-container > div:last-child {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .policy-network-container > div:last-child > svg {
    min-width: 520px;
    max-width: none;
  }
  /* Stat + chart grids already sm:/lg: prefixed — tighten gaps only */
  #tab-overview .grid { gap: 0.75rem; }
  /* Scan-distribution doughnut fills the whole narrow card on phones
     (it reads as one giant ring). Cap its height so it stays a widget. */
  #tab-overview .chart-container-stack .chart-canvas-wrapper {
    flex: none;
    min-height: 0;
    height: 230px;
  }
}

/* ---------- 6. Auth pages (login / signup / forgot / reset / mfa) ---------- */
@media (max-width: 640px) {
  /* 100vh includes the iOS URL bar + keyboard; dvh tracks the real viewport */
  body.h-screen { height: auto; min-height: 100dvh; }
  /* Validation tooltips: wrap instead of bleeding off-screen */
  .custom-validation-tooltip {
    left: 0;
    right: 0;
    transform: scale(0.95) translateY(6px);
    white-space: normal;
    max-width: 100%;
  }
  .custom-validation-tooltip.visible {
    transform: scale(1) translateY(0);
  }
  .tooltip-arrow { left: 24px; }
  /* Checkboxes / radios meet 44px-equivalent hit area via padding */
  label:has(input[type="checkbox"]) {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }
}

/* ---------- 7. Onboarding ---------- */
@media (max-width: 640px) {
  /* Webhook URL + secret rows stack: input full-width, button full-width */
  #step3 .flex.items-center.gap-3:has(#webhookEndpointUrl),
  #step3 .flex.items-center.gap-3:has(#webhookSecret),
  #step2 .flex.items-center.gap-3:has(#onboardingGeminiKey),
  #step2 .flex.items-center.gap-3:has(#apiKeyDisplay) {
    flex-direction: column;
    align-items: stretch;
  }
  #step3 .flex.items-center.gap-3:has(#webhookEndpointUrl) > button,
  #step3 .flex.items-center.gap-3:has(#webhookSecret) > button,
  #step2 .flex.items-center.gap-3:has(#onboardingGeminiKey) > button,
  #step2 .flex.items-center.gap-3:has(#onboardingGeminiKey) > a,
  #step2 .flex.items-center.gap-3:has(#apiKeyDisplay) > button {
    width: 100%;
    justify-content: center;
    min-height: 44px;
  }
  #webhookEndpointUrl, #webhookSecret, #onboardingGeminiKey {
    font-size: 11px;
    overflow-wrap: anywhere;
  }
  /* Tooltips: wrap + work on focus (touch has no hover) */
  .tooltip-icon .tooltip-text {
    white-space: normal;
    max-width: 240px;
    width: max-content;
    text-align: center;
    line-height: 1.45;
  }
  .tooltip-icon:focus-within .tooltip-text {
    visibility: visible;
    opacity: 1;
  }
  .tooltip-icon { outline-offset: 4px; }
  /* Phone row: picker + number never crush each other */
  #step1 .flex.gap-2:has(#phonePicker) {
    flex-wrap: wrap;
  }
}

/* ---------- 8. Docs / blog / legal / status / contact ---------- */
@media (max-width: 640px) {
  /* 32px gutters waste 16% of a phone — halve them */
  main[class*="p-8"], main[class*="p-12"],
  div[class*="p-8"]:has(> pre) { padding: 1rem; }
  pre, pre code {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    word-break: break-word;
  }
  code { overflow-wrap: anywhere; word-break: break-word; }
  code.whitespace-nowrap, .whitespace-nowrap { overflow-wrap: normal !important; word-break: normal !important; white-space: nowrap !important; }
  /* Blog metric trio stacks (scoped — settings grids handled in §10) */
  .visual-mockup .grid.grid-cols-3 { grid-template-columns: 1fr; }
  /* Long visual-mockup rows wrap */
  [class*="visual-mockup"] [class*="justify-between"] {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  /* Article modal: reclaim padding, 44px close target */
  .article-modal { padding: 1rem; }
}

/* ---------- 9. Provenance / MFA small cards (scoped — do NOT touch landing stats) ---------- */
@media (max-width: 640px) {
  /* Provenance audit-detail 2-col squeezes at 390px */
  .glass-card .grid.grid-cols-2:not(#backupCodesGrid) { grid-template-columns: 1fr; }
  /* Opt back in for intentional 2-col micro-grids (backup codes) */
  #backupCodesGrid.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- 10. Settings modal ---------- */
/* settings-modal.css is loaded AFTER this file in index.ejs, so every rule
   here is ID-prefixed to win on specificity regardless of load order.
   At <=700px that stylesheet collapsed the nav to a 58px unlabelled icon
   rail and left a fixed h-16 header whose title text wrapped to four lines
   under the (whitespace-nowrap) version + status pills, overlapping the
   content and pushing the close button off-screen. */
@media (max-width: 700px) {
  /* The modal's own inline <style> (which loads after every stylesheet)
     forces overlay padding .75rem and caps the card at 95vw / 840px, so
     the card could never fill a phone screen. */
  #settingsFloatingModal.settings-floating-overlay { padding: 0; }
  #settingsFloatingModal .settings-floating-card {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }

  /* Header: title + close only. The subtitle is the thing that wrapped. */
  #settingsFloatingModal .modal-header-bar {
    height: auto;
    min-height: 56px;
    padding: 8px 12px;
    gap: 8px;
  }
  #settingsFloatingModal .modal-header-bar p { display: none; }
  #settingsFloatingModal .modal-header-bar h2 { font-size: 0.875rem; }
  #settingsFloatingModal .settings-status-pill { display: none; }
  /* version pill — the other whitespace-nowrap offender */
  #settingsFloatingModal .modal-header-bar span.whitespace-nowrap { display: none; }
  #settingsFloatingModal .modal-header-bar button {
    min-width: 44px;
    min-height: 44px;
  }

  /* Stack the tab strip above the panel content. */
  #settingsFloatingModal .settings-modal-body {
    flex-direction: column;
  }

  /* Nav becomes a horizontal, scrollable chip rail instead of a 58px
     icon rail with no labels. */
  #settingsFloatingModal .modal-sidebar-area {
    width: 100%;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-right: none;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-card-solid);
  }
  #settingsFloatingModal .modal-sidebar-area::-webkit-scrollbar { display: none; }
  /* "Find a setting" search has no room and no useful result set here. */
  #settingsFloatingModal .modal-sidebar-area > label { display: none; }
  /* Flatten the three groups into a single row. */
  #settingsFloatingModal .modal-sidebar-area > div,
  #settingsFloatingModal .modal-sidebar-area > div > div {
    display: contents;
  }
  /* Group captions ("Workspace", "Core Policies", ...). */
  #settingsFloatingModal .modal-sidebar-area > div > span { display: none; }
  /* ...but settings-modal.css hides *every* span in the rail
     (`.modal-sidebar-area span { display: none }`), which also erased the
     button labels and left a row of bare icons. Restore them. */
  #settingsFloatingModal .modal-sidebar-area .modal-settings-nav-item > span {
    display: inline-flex;
    align-items: center;
  }
  #settingsFloatingModal .modal-sidebar-area .modal-settings-nav-item {
    width: auto;
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 10px 14px;
    min-height: 44px;
    justify-content: center;
    gap: 8px;
  }

  /* Panel content gets the full width and phone-sized gutters. */
  #settingsFloatingModal .modal-content-area {
    width: 100%;
    padding: 1rem;
  }
  #settingsFloatingModal .modal-content-area .settings-row { flex-wrap: wrap; }
  #settingsFloatingModal .modal-content-area input[type="range"] { width: 100%; }
  /* Card header rows that put a title beside action buttons squeezed the
     title into a 3-word column (API Credentials & Secret Keys vs Revoke /
     Generate). Stack them and give the actions a full-width row. */
  #settingsFloatingModal .modal-content-area div:has(> div > #btnModalGenerateApiKey),
  #settingsFloatingModal .modal-content-area div:has(> div > #btnModalRevokeApiKey) {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  #settingsFloatingModal #btnModalGenerateApiKey,
  #settingsFloatingModal #btnModalRevokeApiKey {
    flex: 1 1 auto;
    justify-content: center;
    min-height: 44px;
  }
  #settingsFloatingModal .appearance-preview-grid {
    grid-template-columns: 1fr;
  }
  #settingsFloatingModal .grid.grid-cols-3,
  #settingsFloatingModal .grid.grid-cols-2 {
    grid-template-columns: 1fr;
  }
}

/* ---------- 11. Sidebar drawer polish (pairs with theme_head rules) ---------- */
@media (max-width: 1023px) {
  #mainSidebar { max-width: 85vw; }
  #mainSidebar .nav-item {
    min-height: 44px;
  }
}

/* ---------- 12. Mobile Spacing & Section Typography (Prompt 8 & 10) ---------- */
@media (max-width: 768px) {
  /* Reduce section vertical padding (currently desktop-sized) to py-12 (48px) */
  section.py-24,
  section.py-20,
  section.py-16,
  section.py-12,
  section[class*="py-24"],
  section[class*="py-20"],
  section[class*="py-16"],
  section[class*="py-12"] {
    padding-top: 3rem !important; /* 48px (~py-12) */
    padding-bottom: 3rem !important; /* 48px (~py-12) */
  }

  /* Reduce oversized bottom margins on section intro headers to tighten layout */
  section div.container-narrow[class*="mb-16"],
  section div.container-narrow[class*="mb-12"],
  section .text-center[class*="mb-16"],
  section .text-center[class*="mb-12"] {
    margin-bottom: 2rem !important; /* 32px */
  }

  /* Cap h2 headings so they fit on at most two lines at 390px */
  h2,
  h2[class*="text-lg"],
  h2[class*="text-xl"],
  h2[class*="text-2xl"],
  h2[class*="text-3xl"],
  h2[class*="text-4xl"],
  h2[class*="text-5xl"] {
    font-size: 1.125rem !important; /* 18px - ensures all h2 headings fit on at most 2 lines at 390px */
    line-height: 1.3 !important;
    letter-spacing: -0.02em !important;
  }
}
