/* ==========================================================================
   Stratoterra — Responsive Design
   ========================================================================== */

/* --- Tablet: 768px - 1279px --- */
@media (max-width: 1279px) {
  :root {
    --panel-width: 100%;
    --panel-max-width: 100%;
    --panel-min-width: 100%;
  }

  .header-center {
    flex: 0 1 240px;
  }

  .nav-link {
    padding: 6px 8px;
    font-size: 12px;
  }

  /* Panel as full-width overlay */
  .panel {
    width: 100%;
    max-width: 100%;
    min-width: 100%;
    z-index: 950;
  }

  #main-content.panel-open #map-container {
    flex: 1;
  }

  /* Factor grid single column on narrow tablets */
  .factor-grid {
    grid-template-columns: 1fr 1fr;
  }

  .rankings-table {
    font-size: 12px;
  }

  .rankings-table th,
  .rankings-table td {
    padding: 6px 8px;
  }
}

/* --- Mobile: < 768px --- */

/* Mobile tab bar height variable */
:root {
  --mobile-tab-bar-height: 56px;
}

/* Mobile tab bar — hidden on desktop */
.mobile-tab-bar {
  display: none;
}

/* Mobile map toolbar — hidden on desktop */
.mobile-map-toolbar {
  display: none;
}

@media (max-width: 767px) {
  /* Header compact — logo + search only */
  #header {
    flex-wrap: wrap;
    height: auto;
    padding: 8px 12px;
    gap: 8px;
  }

  .header-left {
    order: 1;
  }

  .logo {
    font-size: 16px;
  }

  .header-center {
    order: 3;
    flex: 1 1 100%;
  }

  .header-right {
    order: 2;
    margin-left: auto;
  }

  /* Hide desktop nav on mobile */
  #main-nav {
    display: none;
  }

  /* Hide desktop bottom bar on mobile (replaced by mobile toolbar) */
  #bottom-bar {
    display: none;
  }

  /* ── Mobile bottom tab bar ── */
  .mobile-tab-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--mobile-tab-bar-height);
    background: var(--bg-secondary);
    border-top: 1px solid rgba(255,255,255,0.08);
    z-index: 1100;
    padding: 0;
    justify-content: space-around;
    align-items: stretch;
  }

  .mobile-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    padding: 4px 0;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 10px;
    font-family: var(--font-ui);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: color 0.15s;
  }

  .mobile-tab.active {
    color: var(--text-accent);
  }

  .mobile-tab__icon {
    font-size: 20px;
    line-height: 1;
    margin-bottom: 3px;
  }

  /* ── Mobile map toolbar (Color by + Overlay) ── */
  .mobile-map-toolbar {
    display: flex;
    position: fixed;
    bottom: calc(var(--mobile-tab-bar-height) + 8px);
    left: 54px;
    right: 8px;
    gap: 6px;
    z-index: 1050;
  }

  .mobile-map-toolbar select {
    flex: 1;
    height: 36px;
    padding: 0 8px;
    background: rgba(11, 14, 20, 0.92);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    font-size: 12px;
    font-family: var(--font-ui);
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%2394A3B8'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    padding-right: 24px;
  }

  /* ── Main content area — leave room for tab bar ── */
  #app {
    padding-bottom: var(--mobile-tab-bar-height);
  }

  /* ── Alert ticker — position above tab bar ── */
  .alert-ticker {
    bottom: var(--mobile-tab-bar-height) !important;
  }

  .ticker-primary {
    height: 36px;
  }

  .ticker-headline {
    font-size: 11px !important;
  }

  .ticker-tag__label {
    font-size: 9px !important;
  }

  .ticker-secondary {
    height: 22px;
  }

  .ticker-scroll-item {
    font-size: 10px !important;
  }

  /* Adjust main-content margin for ticker on mobile */
  #app.ticker-watch-on #main-content              { margin-bottom: 22px; }
  #app.ticker-primary-on #main-content            { margin-bottom: 36px; }
  #app.ticker-watch-on.ticker-primary-on #main-content { margin-bottom: 58px; }

  /* Shift map controls up when ticker bars are visible */
  #app.ticker-watch-on .mobile-map-toolbar,
  #app.ticker-watch-on .st-layers-toggle {
    bottom: calc(var(--mobile-tab-bar-height) + 22px + 8px);
  }
  #app.ticker-primary-on .mobile-map-toolbar,
  #app.ticker-primary-on .st-layers-toggle {
    bottom: calc(var(--mobile-tab-bar-height) + 36px + 8px);
  }
  #app.ticker-watch-on.ticker-primary-on .mobile-map-toolbar,
  #app.ticker-watch-on.ticker-primary-on .st-layers-toggle {
    bottom: calc(var(--mobile-tab-bar-height) + 58px + 8px);
  }

  /* Smooth transition for controls repositioning */
  .mobile-map-toolbar,
  .st-layers-toggle {
    transition: bottom 0.2s ease;
  }

  /* ── Color legend — above toolbar controls ── */
  .color-legend {
    bottom: 52px !important;
  }

  /* ── View containers — full screen above tab bar ── */
  .view-container {
    padding: 16px 12px;
    padding-bottom: 24px;
  }

  /* ── Full-screen panel on mobile ── */
  .panel {
    width: 100%;
    max-width: 100%;
    min-width: 100%;
    top: 0;
    z-index: 1000;
  }

  #main-content.panel-open #map-container {
    flex: 1;
  }

  /* ── Factor grid single column ── */
  .factor-grid {
    grid-template-columns: 1fr;
  }

  /* Layer tabs scroll */
  .layer-tabs {
    padding: 0 8px;
  }

  .layer-tab {
    padding: 8px 10px;
    font-size: 11px;
  }

  /* Rankings as cards on mobile */
  .rankings-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Alert summary compact */
  .alert-summary-bar {
    flex-direction: row;
    gap: 8px;
  }

  .alert-summary-count {
    padding: 10px;
  }

  .alert-summary-count__num {
    font-size: 20px;
  }

  /* Comparison tool */
  .compare-tags {
    flex-wrap: wrap;
  }

  .radar-container canvas {
    max-width: 300px;
    max-height: 300px;
  }

  /* Briefing */
  .market-strip {
    flex-wrap: wrap;
    gap: 8px;
  }

  .market-chip {
    min-width: 60px;
  }

  /* Disclaimer — above tab bar */
  #disclaimer {
    font-size: 10px;
    padding: 3px 8px;
    bottom: var(--mobile-tab-bar-height) !important;
  }

  /* Ensure touch targets >= 44px */
  .layer-tab,
  .region-tab,
  .panel-header__back,
  .panel-header__close,
  .alert-filter-select,
  .reasoning-toggle,
  .source-toggle {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }

  /* Search */
  #search {
    padding: 10px 12px;
    font-size: 16px; /* Prevents iOS zoom */
  }

  .search-dropdown__item {
    padding: 12px;
    min-height: 44px;
  }

  .search-dropdown {
    max-height: 200px;
  }
}

/* --- Large Desktop: >= 1440px --- */
@media (min-width: 1440px) {
  .rankings,
  .alerts-dashboard,
  .briefing,
  .comparison,
  .relation-explorer {
    max-width: 1200px;
  }
}

/* --- Accessibility: Focus Indicators --- */
*:focus-visible {
  outline: 2px solid var(--text-accent);
  outline-offset: 2px;
}

.nav-link:focus-visible,
.layer-tab:focus-visible,
.region-tab:focus-visible {
  outline: 2px solid var(--text-accent);
  outline-offset: -2px;
  border-radius: var(--radius-sm);
}

/* --- Accessibility: Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* --- Accessibility: High Contrast --- */
@media (prefers-contrast: high) {
  :root {
    --border-subtle: 1px solid rgba(200, 200, 255, 0.4);
    --border-card: 1px solid rgba(200, 200, 255, 0.3);
  }

  .factor-card {
    border-width: 2px;
  }
}

/* --- Print --- */
@media print {
  #header, #bottom-bar, #disclaimer, .color-legend, .leaflet-control-zoom {
    display: none !important;
  }

  body {
    background: white;
    color: black;
  }

  .panel {
    position: static;
    width: 100%;
    transform: none;
  }
}
