/* ═══════════════════════════════════════════════════════════════
   HCCSL — Mobile UX Patch
   Load AFTER style.css
   Targets: overlapping panels, touch targets, table overflow,
            sticky offset mismatch, form stacking, score inputs
═══════════════════════════════════════════════════════════════ */

/* ── 1. ROOT MOBILE VARS ─────────────────────────────────────── */
:root {
  --header-h: 56px;
  --tabs-h:   50px;
}
@media (max-width: 600px) {
  :root {
    --header-h: 52px;
    --tabs-h:   48px;
  }
}

/* ── 2. STICKY OFFSET — fix 4px gap/overlap on mobile ─────────  */
.tabs {
  top: var(--header-h) !important;
}

/* ── 2b. TAB BAR — 2-row wrap on mobile, no side scroll ────────
   4 tabs per row × 2 rows = all 8 always visible, no scrolling. */
@media (max-width: 680px) {
  .tabs {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
    padding: 6px 8px !important;
    gap: 4px !important;
  }
  .tab {
    flex: 1 1 calc(25% - 4px) !important; /* 4 per row */
    min-width: 0 !important;
    justify-content: center !important;
    font-size: 11.5px !important;
    padding: 7px 4px !important;
    text-align: center !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }
}
@media (max-width: 400px) {
  .tab {
    font-size: 11px !important;
    padding: 6px 2px !important;
  }
}

/* ── 3. SAFE BOTTOM PADDING — clear iOS home indicator ─────────  */
.main {
  padding-bottom: max(60px, env(safe-area-inset-bottom) + 60px);
}

/* ── 4. AGENT PANELS — prevent overflow on narrow screens ───────
   ConflictDetector, ScheduleOptimizer, EOS panels all use
   fixed positioning with hardcoded widths that exceed 375px.
   Make them full-width, bottom-sheet style on mobile.         */
@media (max-width: 600px) {
  #agent-conflict-panel,
  #agent-optimizer-panel {
    width: calc(100vw - 24px) !important;
    left: 12px !important;
    right: 12px !important;
    bottom: max(12px, env(safe-area-inset-bottom) + 8px) !important;
    max-height: 50vh !important;
  }

  #agent-eos-panel {
    width: calc(100vw - 24px) !important;
    max-width: 420px;
    /* keep centered — override inline transform */
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    max-height: 90vh;
    overflow-y: auto;
  }

  #agent-bus-debug {
    width: 100vw !important;
    max-height: 50vh !important;
    border-radius: 0 !important;
  }

  #agent-health-widget {
    margin: 0 0 10px 0;
  }
  #agent-health-widget > div {
    font-size: 11px;
  }
}

/* ── 5. WEATHER BADGES — prevent wrapping into game rows ────────  */
.day-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  min-height: 28px;
  padding: 5px 12px;
  row-gap: 3px;
}
.wx-day-badge {
  flex-shrink: 0;
  max-width: 100%;
}
@media (max-width: 420px) {
  .wx-day-badge {
    font-size: 9px !important;
    padding: 1px 4px !important;
  }
  /* Stack bye teams below weather on very small screens */
  .day-head {
    flex-direction: row;
    flex-wrap: wrap;
  }
}

/* ── 6. GAME ROWS — hide low-priority columns on mobile ─────────  */
@media (max-width: 480px) {
  /* Hide game ID on very small screens — saves ~42px */
  .game-row .game-id,
  .score-row .score-id { display: none; }

  /* Shrink time badge */
  .game-time { min-width: 40px; font-size: 10px; }

  /* Diamond badge smaller */
  .game-diamond { padding: 2px 6px; font-size: 9px; }

  /* Teams take remaining space */
  .game-teams { font-size: 13px; }
}
@media (max-width: 360px) {
  /* Also hide time — only diamond + teams visible */
  .game-row .game-time { display: none; }
}

/* ── 7. SCORE INPUTS — 44px minimum touch target ───────────────  */
input.si {
  min-height: 44px !important;
  min-width: 48px !important;
  font-size: 16px !important; /* prevent iOS auto-zoom on focus */
  text-align: center;
  border-radius: 8px;
  touch-action: manipulation;
}
/* Prevent iOS zoom on ALL inputs/selects */
input[type="text"],
input[type="number"],
input[type="date"],
input[type="password"],
input[type="email"],
select,
textarea {
  font-size: 16px !important; /* iOS zooms when < 16px */
  touch-action: manipulation;
}

/* ── 8. FILTER BAR — proper wrapping, no overflow ───────────────  */
#team-filter-bar,
#diamond-filter-bar {
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 12px;
}
#team-filter-chips,
#diamond-filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}
.chip-filter {
  min-height: 36px;
  padding: 6px 12px;
  font-size: 12px;
  touch-action: manipulation;
}
@media (max-width: 480px) {
  .filter-label {
    width: 100%;
    font-size: 11px;
  }
}

/* ── 9. EXPORT BAR — wrap on mobile ────────────────────────────  */
.export-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 12px;
  align-items: center;
}
@media (max-width: 480px) {
  .export-bar .btn { flex: 1 1 auto; justify-content: center; font-size: 12px; padding: 8px 10px; }
  .export-label { width: 100%; font-size: 11px; margin-bottom: 2px; }
}

/* ── 10. STANDINGS TABLE — sticky team column ───────────────────  */
.st-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  /* Ensure it doesn't clip the sticky column shadow */
  position: relative;
}
@media (max-width: 680px) {
  /* Sticky first column (rank) + second column (team name) */
  .st td:nth-child(1),
  .st th:nth-child(1) {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--surface, #fff);
  }
  .st td:nth-child(2),
  .st th:nth-child(2) {
    position: sticky;
    left: 28px; /* rank col width */
    z-index: 2;
    background: var(--surface, #fff);
    box-shadow: 2px 0 4px rgba(0,0,0,0.06);
  }
  .st thead td:nth-child(1),
  .st thead th:nth-child(1),
  .st thead td:nth-child(2),
  .st thead th:nth-child(2) {
    background: var(--brand, #1d2d50);
  }
  /* Hide lower-priority columns on small screens */
  .st .pct,
  .st th:nth-child(5) { display: none; }
}
@media (max-width: 480px) {
  /* Also hide Last 10 and streak on very small */
  .st .l10, .st th:nth-child(12),
  .st .strk, .st th:nth-child(13) { display: none; }
  /* Tighter padding */
  .st td, .st th { padding: 8px 4px; font-size: 11px; }
}

/* ── 11. ADMIN SUBTABS — horizontal scroll on mobile ───────────  */
.admin-subtabs {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 100%;
  gap: 4px;
  padding: 4px;
  flex-wrap: nowrap;
}
.admin-subtabs::-webkit-scrollbar { display: none; }
.admin-subtab {
  flex-shrink: 0;
  min-height: 36px;
  white-space: nowrap;
  touch-action: manipulation;
}

/* ── 12. FORM ROW STACKING ──────────────────────────────────────  */
@media (max-width: 560px) {
  .row {
    flex-direction: column !important;
    gap: 10px;
  }
  .field {
    width: 100% !important;
    min-width: 0 !important;
    flex: none !important;
  }
  /* Season settings grid */
  #admin-settings .row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  /* But fully stack if only 1 or very wide field */
  #admin-settings .row > .field:only-child {
    grid-column: 1 / -1;
  }
}

/* ── 13. MANAGE DIAMONDS GRID ───────────────────────────────────  */
@media (max-width: 560px) {
  #dm { grid-template-columns: 1fr !important; }
}

/* ── 14. PLAYOFF BRACKET — horizontal scroll ───────────────────  */
/* The bracket SVG connector + flex container needs scroll, not clip */
.ply-bracket-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 8px;
}
/* Target the inline bracket divs rendered by renderPodAdmin */
#ply > div > div[style*="overflow-x"],
#ply-admin > div > div[style*="overflow-x"],
#ply > .card > div[style*="overflow-x"],
#ply-admin > .card > div[style*="overflow-x"] {
  -webkit-overflow-scrolling: touch;
}
@media (max-width: 560px) {
  /* Shrink bracket team chips so they don't require 480px+ */
  div[style*="min-width:160px"] {
    min-width: 130px !important;
  }
}

/* ── 15. PIN MODAL — safe on small screens ──────────────────────  */
#pin-modal > div {
  max-height: 90vh;
  overflow-y: auto;
}
@media (max-width: 380px) {
  #pin-modal > div {
    padding: 16px;
    width: 95vw;
  }
  /* Numpad buttons larger touch targets */
  #pin-modal .btn { min-height: 48px; font-size: 20px !important; }
}

/* ── 16. CROSSOVER ADMIN CARD — date+input wrap ─────────────────  */
@media (max-width: 560px) {
  #admin-settings .card div[style*="display:flex"][style*="flex-wrap:wrap"] {
    flex-direction: column;
  }
  #co-new-date,
  #co-new-name { width: 100% !important; }
}

/* ── 17. CHAMPION ADMIN — year + pod fields ─────────────────────  */
@media (max-width: 480px) {
  #champ-year,
  #champ-poda,
  #champ-podb,
  #new-season-year,
  #new-season-champ-poda,
  #new-season-champ-podb { width: 100% !important; }
}

/* ── 18. ACCORDION HEAD — full-width tap target ─────────────────  */
.acc-head {
  min-height: 44px;
  display: flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  cursor: pointer;
  user-select: none;
}

/* ── 19. BUTTONS — 44px minimum touch target everywhere ─────────  */
.btn, .wx-btn, .chip-del, button.acc-head {
  min-height: 36px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
@media (max-width: 600px) {
  .btn { min-height: 44px; }
  .wx-btn { min-height: 40px; min-width: 40px; }
}

/* ── 20. HEADER WEATHER — hide on very small screens ───────────  */
@media (max-width: 380px) {
  #hw-weather { display: none !important; }
  #hw-date { font-size: 11px; }
}

/* ── 21. TICKER — pause on touch, don't clip ────────────────────  */
.ticker-bar {
  -webkit-overflow-scrolling: touch;
}
.ticker-content {
  /* Add extra space so last item isn't clipped at screen edge */
  padding-right: 40px;
}

/* ── 22. SCORE ROWS — wrap inputs on mobile ─────────────────────  */
@media (max-width: 520px) {
  .score-row {
    flex-wrap: wrap;
    gap: 6px;
  }
  .score-teams {
    flex: 1 1 140px;
    min-width: 0;
  }
  .score-inputs {
    flex: 0 0 auto;
    margin-left: auto;
  }
  /* WX buttons stay on same line as inputs */
  .wx-btn { flex-shrink: 0; }
}

/* ── 23. EDIT GAMES TABLE — mobile scroll ───────────────────────  */
#edi { overflow-x: auto; -webkit-overflow-scrolling: touch; }
#edi table { min-width: 500px; }

/* ── 24. PARK MAP — full width on mobile ───────────────────────  */
@media (max-width: 600px) {
  #park-map { height: 360px !important; }
}

/* ── 25. RULES PAGE — tighter padding ──────────────────────────  */
@media (max-width: 480px) {
  .rule-item { padding: 8px 12px; font-size: 13px; }
  .rule-num { min-width: 44px; font-size: 11px; }
}

/* ── 26. METRIC GRID — 2 col on mobile ─────────────────────────  */
@media (max-width: 480px) {
  .metric-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px;
  }
  .metric-value { font-size: 20px; }
}

/* ── 27. SEASON HEALTH WIDGET — 2×2 on mobile ──────────────────  */
@media (max-width: 480px) {
  #agent-health-widget [style*="grid-template-columns:repeat(4"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ── 28. PREVENT HORIZONTAL SCROLL ON BODY ──────────────────────  */
html, body {
  overflow-x: hidden;
  max-width: 100vw;
}
/* But allow scroll within designated containers */
.st-wrap, #edi, .ply-bracket-scroll,
div[style*="overflow-x:auto"],
div[style*="overflow-x: auto"] {
  overflow-x: auto;
}
