/* EDJE · Site-wide mobile polish layer
 * Catches common mobile issues across every page without touching individual pages.
 * Loaded via edje-app-chrome.js on every page.
 */

/* Prevent any horizontal overflow site-wide */
html, body {
  overflow-x: hidden;
  max-width: 100vw;
}

/* Images never force horizontal scroll */
img, video, svg, canvas, iframe {
  max-width: 100%;
  height: auto;
}

/* Make all tap targets at least 44x44 (Apple HIG) on touch devices */
@media (hover: none) and (pointer: coarse) {
  button, a.btn, a.cta, .button, .tap, .nav-toggle {
    min-height: 40px;
  }
}

/* Tables are the #1 cause of horizontal scroll — allow scrolling in a container */
@media (max-width: 720px) {
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  pre {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 11.5px;
  }
  /* Shrink H1 across the board on mobile */
  h1 { font-size: clamp(26px, 7vw, 36px) !important; line-height: 1.1 !important; }
  h2 { font-size: clamp(20px, 5.5vw, 28px) !important; }
}

/* Mobile tap feedback — removes 300ms delay and the default iOS gray flash */
* { -webkit-tap-highlight-color: transparent; }
a, button { touch-action: manipulation; }

/* Nav breathing room — too tight to top on desktop. Add vertical padding on desktop
 * and keep safe-area awareness on mobile for notched phones. */
header, nav.nav {
  padding-top: calc(env(safe-area-inset-top, 0) + 16px);
  padding-bottom: 14px;
}
@media (max-width: 720px) {
  header, nav.nav {
    padding-top: calc(env(safe-area-inset-top, 0) + 10px);
    padding-bottom: 10px;
  }
}

/* Fix any absolutely-positioned fullscreen modal to respect safe area */
.modal, [role="dialog"] {
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

/* Prevent fixed-position elements from being hidden under bottom tab bar in PWA */
body.__edje-pwa {
  padding-bottom: calc(70px + env(safe-area-inset-bottom, 0));
}

/* Form controls on mobile — bigger tap targets, no zoom-on-focus on iOS */
@media (max-width: 720px) {
  input, select, textarea {
    font-size: 16px !important; /* iOS doesn't zoom when ≥16px */
  }
  input[type="text"], input[type="email"], input[type="password"],
  input[type="number"], select, textarea {
    padding: 12px 14px;
    border-radius: 10px;
    min-height: 44px;
  }
}

/* Ticker on mobile — slightly smaller to not dominate the viewport */
@media (max-width: 480px) {
  #edje-ticker {
    font-size: 10.5px !important;
  }
  #edje-ticker .t-label {
    display: none !important; /* "LIVE" label takes too much space on small screens */
  }
}

/* Any page container that uses fixed max-width — ONLY on mobile viewports.
 * The desktop main constraint (e.g. max-width: 460px on signin page) must be preserved. */
@media (max-width: 720px) {
  .container, main, .wrap, .wrapper {
    max-width: 100%;
    box-sizing: border-box;
  }
}

/* Hamburger menu — ensure it closes on route change */
.nav-links.open {
  box-shadow: 0 10px 30px -10px rgba(0,0,0,0.5);
}

/* ========================================================================
 * VIZ PAGES — Theater, Engine Room, Clock, Topology, Graph, Flow
 * The floating legends, tooltips, and KPI strips are built for desktop and
 * cover the canvas on mobile. Shrink + reposition them so the canvas stays
 * usable. The touch shim handles the gestures; this handles the layout.
 * ======================================================================== */
@media (max-width: 720px) {
  /* Shrink floating legends + make them collapsible-feeling */
  .legend {
    max-width: calc(50vw - 24px) !important;
    padding: 8px 10px !important;
    font-size: 10px !important;
    top: 68px !important;
    left: 10px !important;
  }
  .legend h3 { font-size: 11px !important; margin: 0 0 6px !important; }
  .legend-row { font-size: 9.5px !important; margin-bottom: 4px !important; }
  .legend .meta-line { display: none !important; }

  /* KPI/status strips */
  .kpi-strip, .status-chip, .edge-display {
    top: 68px !important;
    right: 10px !important;
    max-width: calc(50vw - 24px) !important;
    font-size: 10px !important;
  }
  .kpi { min-width: 70px !important; padding: 6px 8px !important; font-size: 10px !important; }
  .kpi .v { font-size: 12px !important; }

  /* Tooltips — keep inside viewport */
  .tt {
    max-width: calc(100vw - 32px) !important;
    font-size: 11px !important;
    padding: 10px 12px !important;
  }
  .tt .q { font-size: 12px !important; }

  /* Market-picker (Engine Room) */
  .market-picker {
    top: 60px !important;
    max-width: calc(100vw - 20px) !important;
    font-size: 11px !important;
  }

  /* Status bars at the bottom */
  .status-bar {
    bottom: 10px !important;
    left: 10px !important;
    right: 10px !important;
    font-size: 10px !important;
  }

  /* Column labels (Theater three columns) */
  .column-labels {
    top: 60px !important;
    padding: 0 10px !important;
    font-size: 9px !important;
  }

  /* Station detail panel (Engine Room) */
  .station-detail {
    top: auto !important;
    bottom: 70px !important;
    left: 10px !important;
    right: 10px !important;
    max-width: none !important;
    max-height: 40vh !important;
    overflow-y: auto !important;
  }

  /* Context menu (Graph) */
  .ctx-menu {
    max-width: calc(100vw - 32px) !important;
    font-size: 12px !important;
  }

  /* Make header shorter on viz pages */
  header {
    padding: 10px 14px !important;
  }
}

/* ===== Nov 7 · Mobile nav for inline-header pages =====
   engine-live, engine-room, algorithm, today, opportunities, performance, tour
   each ship their own <header> with a <nav> containing 5 links. At <640px
   those links overflow or cram the brand. On mobile, hide the inline nav
   links and let the bottom tabbar (injected by edje-app-chrome.js) handle
   navigation. Keep the brand visible and clickable. */
@media (max-width: 640px) {
  header nav,
  header > nav {
    display: none !important;
  }
  header {
    padding: 12px 16px !important;
    justify-content: flex-start !important;
  }
  /* Brand text sizing on narrow screens */
  header .brand {
    font-size: 18px !important;
    letter-spacing: -0.02em !important;
  }
  /* Secondary brand text (e.g. "· ENGINE LIVE") stays small */
  header .brand span[style*="muted"] {
    font-size: 11px !important;
  }
  /* Reserve bottom padding so content clears the fixed tab bar */
  body { padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px)) !important; }
}

/* ===== Nov 7 · Force mobile tabbar visible on inline-header pages =====
   edje-app-chrome.js already fires mountTabBar on mobile, but add a safety
   net rule in case CSS specificity from a page-local stylesheet hides it. */
@media (max-width: 899px) {
  #edjeTabBar { display: grid !important; }
}

/* ===== Nov 9 · Aggressive mobile defense =====
   Catches the long tail of pages that don't have their own @media
   breakpoints. Rules intentionally use max-width:100% so desktop is
   untouched; they only engage when content tries to exceed the viewport. */

/* No element may exceed viewport width — kills horizontal scroll */
@media (max-width: 760px) {
  body, main, section, article, header, footer, div {
    max-width: 100vw !important;
    box-sizing: border-box !important;
  }
  /* Force any explicit >= 400px inline width down to 100% */
  *[style*="width: 4"],
  *[style*="width: 5"],
  *[style*="width: 6"],
  *[style*="width: 7"],
  *[style*="width: 8"],
  *[style*="width: 9"],
  *[style*="width:1"] {
    max-width: 100% !important;
  }
  /* Fixed max-width containers collapse to viewport */
  [style*="max-width:1200px"],
  [style*="max-width:1240px"],
  [style*="max-width:1100px"],
  [style*="max-width:1000px"],
  [style*="max-width:960px"],
  [style*="max-width:900px"],
  [style*="max-width: 1200px"],
  [style*="max-width: 1240px"] {
    max-width: 100% !important;
    padding-left: 16px !important;
    padding-right: 16px !important;
  }
  /* 2+ column grids stack */
  [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns: repeat(2"],
  [style*="grid-template-columns: repeat(3"],
  [style*="grid-template-columns: repeat(4"] {
    grid-template-columns: 1fr !important;
  }
  /* Flex rows wrap */
  [style*="display: flex"]:not([style*="flex-direction: column"]),
  [style*="display:flex"]:not([style*="flex-direction:column"]) {
    flex-wrap: wrap !important;
  }
  /* Enormous headings cap at viewport */
  h1 { font-size: clamp(28px, 7vw, 44px) !important; line-height: 1.1 !important; word-wrap: break-word !important; }
  h2 { font-size: clamp(22px, 5.5vw, 32px) !important; line-height: 1.2 !important; }
  /* Long unbroken text/URLs wrap */
  p, li, td, th, pre, code, span {
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
  }
  pre { overflow-x: auto !important; }
  /* Page padding — pages without their own @media get breathable sides */
  body > section,
  body > main,
  body > article {
    padding-left: 16px !important;
    padding-right: 16px !important;
  }
  /* Form inputs full-width to prevent iOS zoom */
  input[type="text"], input[type="email"], input[type="password"], input[type="search"],
  input[type="number"], input[type="tel"], select, textarea {
    width: 100% !important;
    max-width: 100% !important;
    font-size: 16px !important; /* prevents iOS zoom-on-focus */
    box-sizing: border-box !important;
  }
  /* Buttons stretch to tap-friendly width */
  button, .btn, a.btn, a.cta, a.nav-cta {
    min-height: 44px !important;
  }
  /* Nav wraps instead of overflowing */
  nav, .nav, header nav {
    flex-wrap: wrap !important;
    row-gap: 8px !important;
  }
  /* Code blocks don't blow up layout */
  table {
    display: block !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    max-width: 100% !important;
  }
  /* Iframes (embeds) scale down */
  iframe { max-width: 100% !important; height: auto !important; }
  /* Sticky elements stay in viewport */
  [style*="position: sticky"],
  [style*="position:sticky"] { max-width: 100vw !important; }
}

/* iPhone SE (~375px) extra squeeze */
@media (max-width: 400px) {
  body { font-size: 15px !important; }
  h1 { font-size: clamp(26px, 7vw, 36px) !important; }
  .container, main > div, section > div {
    padding-left: 14px !important;
    padding-right: 14px !important;
  }
}

/* ══════════════════════════════════════════════════════════════ */
/* Nov 9 · iOS rendering + chat-page clutter fixes                */
/* ══════════════════════════════════════════════════════════════ */

/* 1. Fix iOS Safari double-render on h1 <em> shimmer.
      The cyan color in ask.html's inline style + the gradient fill in
      edje-polish.css were layering. Force the gradient-as-text fill to win
      cleanly, and disable the animation when prefers-reduced-motion is on. */
h1 em, h2 em {
  color: transparent !important;
  -webkit-text-fill-color: transparent !important;
  will-change: background-position;
  /* Prevent iOS from caching the gradient as a bitmap with ghosting */
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

/* Fallback: on very narrow mobile where the shimmer can look jittery,
   lock to a solid brand color — still looks premium, no ghost text. */
@media (max-width: 500px) {
  h1 em, h2 em {
    background: none !important;
    -webkit-text-fill-color: #22D3EE !important;
    color: #22D3EE !important;
    animation: none !important;
  }
}

/* 2. Simple/Pro mode toggle — hide on chat/ask pages (clutter against chat
      stream) and lift above sticky input on mobile. */
body.__edje-ask-page #edjeModeToggle,
body[data-page="ask"] #edjeModeToggle { display: none !important; }

@media (max-width: 720px) {
  #edjeModeToggle {
    bottom: calc(80px + env(safe-area-inset-bottom, 0)) !important;
    right: 12px !important;
  }
  /* When PWA tab-bar is present, lift even higher */
  body.__edje-pwa #edjeModeToggle {
    bottom: calc(96px + env(safe-area-inset-bottom, 0)) !important;
  }
}

/* 3. ⌘K keyboard shortcut button — hide on mobile (no keyboard shortcuts) */
@media (max-width: 720px) {
  .edje-cmdk-fab,
  #edjeCmdK,
  [data-edje-cmdk],
  button[aria-label*="Command"],
  button[title*="⌘K"],
  button[title*="Cmd+K"] {
    display: none !important;
  }
}

/* 4. Ask page content hiding behind bottom tab bar + input */
body[data-page="ask"] main,
.ask-page main,
main:has(.input-row) {
  padding-bottom: calc(140px + env(safe-area-inset-bottom, 0)) !important;
}

/* Make the ask input-row float cleanly above the tab bar on mobile */
@media (max-width: 720px) {
  .input-row {
    position: sticky;
    bottom: calc(68px + env(safe-area-inset-bottom, 0));
    background: rgba(7, 8, 12, 0.92);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    padding: 12px;
    margin: 0 -20px;
    border-top: 1px solid var(--line, #1D2130);
    z-index: 20;
  }
  body.__edje-pwa .input-row {
    bottom: calc(76px + env(safe-area-inset-bottom, 0));
  }
}

/* 5. Chat message cards: make sure they don't get hidden under mode toggle */
@media (max-width: 720px) {
  .chat .msg:last-of-type {
    margin-bottom: 20px;
  }
  .chat .m-card {
    max-width: 100% !important;
    overflow: hidden;
  }
}

/* 6. Universal: nothing floating in the bottom 80px should cover main content */
@media (max-width: 720px) {
  main, .wrap, .container { padding-bottom: 20px; }
}
