/* ============================================================
   RESPONSIVE — Small screens (< 900px)
   Side panel moves to right edge, shrinks gracefully
   ============================================================ */
@media (max-width: 900px) {
  :root { --side-w: 200px; }
  .sp-item-name { font-size: 0.58rem; }
  .sp-item-stat { font-size: 0.72rem; }
  .sp-item-cost { font-size: 0.60rem; }
  .sp-item      { min-height: 70px; padding: 6px 4px; }
}

/* ============================================================
   RESPONSIVE — Tablet / landscape phone (≤ 768px)
   Side panel becomes a horizontal scrollable strip at bottom
   ============================================================ */
@media (max-width: 768px) {
  .sp-section-hdr { padding: 4px 2px; font-size: 0.58rem; white-space: nowrap; }

  /* Header buttons tighten */
  .hdr-nav-btn          { font-size: 0.54rem; padding: 5px 8px; }
  .hdr-spin-countdown   { display: none; }
  .hdr-prestige-btn     { display: none; }

  /* Modal shop fills more on tablet */
  .modal-shop-box { max-width: 100%; max-height: 92vh; }
  .shop-tab       { font-size: 0.60rem; padding: 10px 4px; }

  /* Aliens grid adjusts */
  .aliens-grid-fs { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}

/* ============================================================
   RESPONSIVE — Small phones (≤ 600px)
   ============================================================ */
@media (max-width: 600px) {
  .notif-stack     { max-width: 260px; right: 8px; }
  .stats-tab       { font-size: 0.58rem; padding: 11px 4px; }
  .stats-grid      { grid-template-columns: repeat(2, 1fr); }
}

/* ============================================================
   RESPONSIVE — Very small phones (≤ 380px)
   Hide side panel entirely, compact header
   ============================================================ */
@media (max-width: 380px) {
  :root { --hdr-h: 54px; }

  .hdr-stat-val     { font-size: 0.68rem; }
  .hdr-stat-label   { font-size: 0.48rem; }
  .hdr-stat-sub     { font-size: 0.55rem; }
  .hdr-divider      { height: 22px; }

  .stats-grid       { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  .stat-card        { padding: 12px 10px; }
  .stat-val         { font-size: 0.88rem; }
}

/* ============================================================
   DESKTOP ENHANCEMENTS (≥ 768px)
   ============================================================ */
@media (min-width: 768px) {
  .aliens-grid-fs { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
  .modal-box      { padding: 32px 30px; }
}

/* ============================================================
   MOBILE + RESPONSIVE PATCH (Patch 6)
   Full mobile/tablet/ultrawide support.
   All rules use !important where needed to override the
   three-column layout set in index.html's inline <style>.
   ============================================================ */

/* ── Mobile Navigation Bar ───────────────────────────────────
   Shown only at ≤ 600px. Hidden on all larger viewports.
   Z-index: above panels (50), below modals (9999).
   ─────────────────────────────────────────────────────────── */
#mobile-nav {
  display: none; /* enabled via @media below */
  position: fixed;
  bottom: 0; left: 0; right: 0;
  /* Respect iOS home-indicator safe area */
  height: calc(56px + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: rgba(4,6,28,0.98);
  border-top: 1px solid rgba(80,180,255,0.20);
  z-index: 200;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  align-items: stretch;
}

.mob-nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  border-top: 2px solid transparent;
  color: rgba(100,140,200,0.55);
  font-family: var(--font-hd, 'Orbitron', monospace);
  font-size: 0.50rem;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  cursor: pointer;
  padding: 6px 2px 4px;
  min-width: 0;
  transition: color 0.15s, border-color 0.15s, transform 0.12s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.mob-nav-btn:active { transform: scale(0.90); transition-duration: 60ms; }
.mob-nav-btn.mob-active {
  color: var(--accent, #50c8ff);
  border-top-color: var(--accent, #50c8ff);
}
.mob-nav-icon {
  font-size: 1.30rem;
  line-height: 1;
  display: block;
}
.mob-nav-label {
  font-size: 0.50rem;
  letter-spacing: 0.5px;
  line-height: 1;
  white-space: nowrap;
}

/* ── Responsive: Small Tablet / Narrow Desktop (≤ 900px) ─────
   Shrink panel widths so the alien area has enough room.
   ─────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  #panel-left  { width: 175px !important; }
  #panel-right { width: 250px !important; }
}

/* ── Responsive: Landscape Phone / Small Tablet (≤ 768px) ───
   Header: strip badges that overflow at this width.
   Panel layout switches to mobile drawers (block below) so the
   left panel (stats / missions / picker) is never unreachable.
   ─────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #hdr-golden-badge  { display: none !important; }
}

/* ── Mobile: Phone / Small Tablet (≤ 768px) ─────────────────
   Full-screen panel drawers + bottom navigation bar.
   ─────────────────────────────────────────────────────────── */
@media (max-width: 768px) {

  /* Show the bottom nav bar */
  #mobile-nav { display: flex; }

  /* Push game content above nav bar so bottom content isn't hidden */
  #habitat-root {
    padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* ── Panel visibility ─────────────────────────────────────
     By default both side panels are hidden.
     switchMobilePanel() adds .mob-visible to show one.
     ───────────────────────────────────────────────────────── */
  #panel-left:not(.mob-visible),
  #panel-right:not(.mob-visible) {
    display: none !important;
  }

  /* When visible: fixed overlay from header bottom to nav top */
  #panel-left.mob-visible,
  #panel-right.mob-visible {
    position: fixed !important;
    top: 52px !important;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    z-index: 50;
    display: flex !important;
    flex-direction: column !important;
    /* Single scroll owner: the inner flex:1 content panel
       (#side-panel-content / #upgrade-panel-wrap / #prestige-panel-wrap)
       already scrolls — this outer panel must not also scroll, or two
       scrollbars stack on mobile. */
    overflow: hidden !important;
    background: rgba(4,6,28,0.99) !important;
    border-right: none !important;
    border-left:  none !important;
    animation: mobPanelIn 0.22s cubic-bezier(0.4,0,0.2,1);
  }
  @keyframes mobPanelIn {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  /* Center panel fills full width when it's the only visible panel */
  #panel-center { width: 100% !important; }

  /* ── Toast: clear mobile nav bar ─────────────────────────── */
  .game-toast {
    bottom: calc(68px + env(safe-area-inset-bottom, 0px)) !important;
    max-width: calc(100vw - 32px) !important;
    white-space: normal !important;
    text-align: center !important;
  }

  /* ── Notifications ────────────────────────────────────────── */
  #notif-stack {
    right: 8px !important;
    max-width: calc(100vw - 16px) !important;
  }

  /* ── Header: strip elements that overflow narrow screens ──── */
  #hdr-prestige-block { display: none !important; }
  #hdr-golden-badge   { display: none !important; }
  .hdr-name-btn {
    max-width: 80px !important;
    font-size: 0.60rem !important;
    padding: 5px 8px !important;
  }

  /* ── Header: flow layout on phones ──────────────────────────
     The GC counter is absolutely centered on desktop; on narrow
     screens that put it on top of the companion chip. In flow it
     takes the free space between the chip and the icon cluster —
     nothing can overlap and nothing can leave the viewport. ── */
  #habitat-header { gap: 6px !important; padding: 0 8px !important; }
  .hdr-logo { display: none !important; }
  .hdr-center-hud {
    position: static !important;
    transform: none !important;
    flex: 1 1 auto !important;
    min-width: 0 !important;
  }
  .hud-clicks-val {
    font-size: clamp(0.78rem, 4vw, 1.05rem) !important;
    max-width: 100%;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .hud-cps-val { font-size: 0.55rem !important; }
  .hdr-right-section { gap: 4px !important; }
  .hdr-icon-btn { width: 34px !important; height: 34px !important; }

  /* ── Start screen ─────────────────────────────────────────── */
  .start-panel { padding: 28px 20px 24px !important; }
  .start-logo  { font-size: 3.5rem !important; }

  /* ── Modals ───────────────────────────────────────────────── */
  .modal-overlay { padding: 10px !important; }

  /* Galaxy map: reduce the oversized 80px horizontal padding */
  .gmap-top-bar { padding: 18px 18px 12px !important; }
  .gmap-title-text {
    font-size: clamp(0.80rem, 5vw, 1.1rem) !important;
    letter-spacing: 5px !important;
  }

  /* Galaxy map zigzag: tighten info columns on narrow screens */
  .gmap-row { padding: 6px 10px !important; }
  .gmap-info-left  { padding-right: 8px !important; }
  .gmap-info-right { padding-left:  8px !important; }
  .gmap-node-title {
    font-size: clamp(0.52rem, 2.4vw, 0.70rem) !important;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .gmap-node-num-label,
  .gmap-node-status { font-size: 0.50rem !important; }

  /* Galaxy map info card: safe margin */
  .gmap-info-overlay { padding: 10px !important; }

  /* World-entry overlay: safe width on phones */
  .weo-overlay { padding: 10px !important; }
  .weo-card {
    padding: 22px 16px 18px !important;
    max-width: calc(100vw - 20px) !important;
  }

  /* Combat jar: slight size reduction so it doesn't crowd the alien */
  .combat-jar {
    width: 60px !important;
    height: 180px !important;
  }

  /* Behavior meters: tighter padding */
  #behavior-meters { padding: 4px 8px 4px !important; }

  /* Right panel upgrade list: smaller phase headers on mobile */
  .upg-phase-title { font-size: 0.60rem !important; letter-spacing: 1.5px !important; }
}

/* ── Very small phones (≤ 380px) ─────────────────────────────
   Extra compaction to prevent header overflow.
   ─────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  /* Rename still lives in Settings → Commander Name; the header button
     costs ~70px the currency counter needs on phones. */
  .hdr-name-btn { display: none !important; }
}

@media (max-width: 380px) {
  .hdr-logo    { font-size: 1.1rem !important; }
  .hdr-name-btn { max-width: 60px !important; font-size: 0.54rem !important; padding: 4px 6px !important; }
  .start-panel { padding: 20px 16px 18px !important; }
  .start-logo  { font-size: 3rem !important; }
}

/* ── Landscape phone: short viewport (≤ 500px tall) ──────────
   Compact mobile nav to reclaim vertical space.
   ─────────────────────────────────────────────────────────── */
@media (max-height: 500px) and (max-width: 900px) {
  /* Shrink nav to icon-only strip */
  #mobile-nav {
    height: calc(44px + env(safe-area-inset-bottom, 0px)) !important;
  }
  .mob-nav-label { display: none; }
  .mob-nav-btn   { padding: 4px 2px !important; gap: 0; }
  .mob-nav-icon  { font-size: 1.1rem !important; }

  /* Push content above smaller nav */
  #habitat-root {
    padding-bottom: calc(44px + env(safe-area-inset-bottom, 0px)) !important;
  }
  #panel-left.mob-visible,
  #panel-right.mob-visible {
    bottom: calc(44px + env(safe-area-inset-bottom, 0px)) !important;
  }
  .game-toast {
    bottom: calc(54px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* Compact header */
  #habitat-header { height: 44px !important; }
  .hud-clicks-val { font-size: clamp(0.85rem, 3vw, 1.1rem) !important; }
  .hud-cps-val    { font-size: 0.58rem !important; }
  .hdr-icon-btn   { width: 32px !important; height: 32px !important; }

  /* Adjust fixed panel top offset to match compact header */
  #panel-left.mob-visible,
  #panel-right.mob-visible { top: 44px !important; }
}

/* ── Ultrawide screens (≥ 1920px) ────────────────────────────
   Cap the layout width and center it so it doesn't stretch.
   ─────────────────────────────────────────────────────────── */
@media (min-width: 1920px) {
  #habitat-root {
    max-width: 1920px;
    margin-left: auto;
    margin-right: auto;
    position: relative; /* keep fixed children relative to viewport not this element */
  }
}
