/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE OVERRIDES
   Loaded last so rules win against component CSS.
   Breakpoints:
     1400px  — shrink sidebar
     1200px  — compact nav, smaller metrics
     1024px  — tablet: collapse sidebar to drawer
      768px  — phone landscape / small tablet
      480px  — phone portrait
   ═══════════════════════════════════════════════════════════════ */

/* Global: allow iOS momentum scrolling inside scrollables */
.tblwrap, .pbody, .sb, .pipeline, .outreach-wrap, .analyzer-wrap,
.analytics-wrap, #tab-analyzer, #tab-analytics, #tab-outreach,
.outreach-contact-list, .dm-kol-items, .pcards, #blockedKanban,
.imp-map-wrap, .mo-box, .xdm-sidebar, .dm-content-main {
  -webkit-overflow-scrolling: touch;
}

/* Prevent body horizontal scroll from wide tables; tables still scroll inside tblwrap */
body { overscroll-behavior: none; }

/* ═══════════════════════════════════════════════════════════════
   HORIZONTAL OVERFLOW GUARD (all viewports)
   Content grows vertically only. Wide tables/kanbans scroll INSIDE
   their own wrapper, never pushing the page wider than the viewport.
   ═══════════════════════════════════════════════════════════════ */
html, body {
  max-width: 100vw;
  overflow-x: hidden;
}
.layout {
  max-width: 100vw;
  overflow-x: hidden;
}
.main {
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
}
/* Tab content wrappers must respect parent width and never overflow */
#tab-analyzer, #tab-analytics, #tab-outreach, #tab-search, #tab-list,
#tab-pipeline, #tab-discarded, #tab-blocked,
.analyzer-wrap, .analytics-wrap, .outreach-wrap, .pipeline {
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
}
/* Flex and grid children: forbid content-driven expansion */
.analyzer-wrap *, .analytics-wrap *, .outreach-wrap * { min-width: 0; }
/* Chip/tag rows always wrap */
#analyzerSelected, .outreach-chips, .chip-row, .tag-row,
[style*="display:flex"][style*="flex-wrap"] { max-width: 100%; }
/* Canvases, charts, images respect container */
canvas, svg, img, video, iframe { max-width: 100%; height: auto; }
canvas { height: auto; }
/* Wide scrollables: scroll inside themselves only */
.tblwrap, .pipeline, #blockedKanban, .imp-map-wrap {
  max-width: 100%;
  overflow-x: auto;
}
/* Long URLs / tokens break instead of stretching */
.pbody, .mo-box, .card, .ana-card, .az-card, .outreach-preview-panel,
.dm-content-main, .settings-input, .outreach-input {
  word-break: break-word;
  overflow-wrap: anywhere;
}

/* Nav: absorbable middle (tabs) + fixed right group that can't force overflow.
   Tabs shrink and scroll internally; brand & nr stay as fixed-size siblings.
   Use `overflow: clip` (not hidden) so it clips X without coercing Y — otherwise
   absolutely-positioned dropdowns (theme menu, Datos menu) get hidden too. */
.nav { min-width: 0; max-width: 100vw; overflow-x: clip; overflow-y: visible; }
.tabs { flex: 1 1 auto !important; min-width: 0 !important; overflow-x: auto; overflow-y: visible; }
/* overflow: visible so absolutely-positioned dropdowns (theme menu, Datos menu)
   can extend leftward past .nr's left edge without being clipped. The parent
   .nav already has overflow-x: clip, and progressive disclosure at 1500/1300/1150
   hides buttons before .nr would overflow horizontally. */
.nr { flex-shrink: 0; max-width: 60vw; overflow: visible; }
/* Dropdown hosts must never be clipped by their scroll siblings */
.nr .ddrop { position: relative; overflow: visible; }

/* Progressive disclosure: hide non-critical nav buttons before 1024px drawer kicks in */
@media (max-width: 1500px) {
  #testBtn { display: none; }
}
@media (max-width: 1300px) {
  #undoBtn, #redoBtn { display: none; }
}
@media (max-width: 1150px) {
  .nr .theme-btn { display: none; }
}

/* Global disabled state for buttons — visual feedback during async ops */
button:disabled, .btn:disabled, .btn[disabled] {
  opacity: .5;
  cursor: not-allowed !important;
  pointer-events: none;
}

/* X / Twitter native browser view — contacted-row styling */
.x-row { transition: background .15s; }
.x-row:hover { background: var(--bg2) !important; }
.x-row-done { opacity: .6; }
.x-row-done > div:nth-child(3) > div:first-child { text-decoration: line-through; color: var(--text3); }
.x-bulk-btn.active {
  background: rgba(240,180,41,.08) !important;
  border-color: rgba(240,180,41,.35) !important;
  color: var(--accent) !important;
}

/* Touch-friendly tap targets on coarse pointers */
@media (pointer: coarse) {
  .btn, .tab, .sett-tab, .fchip, .kwsg, .pgsel, .cbtn, .ecb,
  .outreach-date-btn, .outreach-tab, .theme-opt, .dopt,
  .mo-close, .pclose, .kdel, .sa {
    min-height: 32px;
  }
  input, select, textarea, button { font-size: max(16px, 1em); }
  /* Avoid iOS zoom when focusing inputs */
  .sin, .api-in, .cfgi, .nin, .kwi, .exsrch, .settings-input,
  input[type="text"], input[type="number"], input[type="email"],
  input[type="password"], input[type="search"], textarea, select {
    font-size: 16px !important;
  }
}

/* ─── Hamburger toggle (hidden on desktop) ───────────────────── */
.nav-burger {
  display: none;
  background: transparent;
  border: 1px solid var(--line2);
  color: var(--text2);
  border-radius: var(--radius);
  padding: 4px 9px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  margin-right: 4px;
}
.nav-burger:hover { background: var(--bg3); color: var(--text); }

/* Sidebar backdrop for mobile drawer */
.sb-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 39;
  animation: fadeIn .15s ease;
}
.sb-backdrop.show { display: block; }

/* ═══ ≤ 1400px — tighter desktop ═══ */
@media (max-width: 1400px) {
  .nav { padding: 0 10px; }
  .bsub { display: none; }
  .tab { padding: 0 10px; font-size: 11px; }
  .sc { padding: 8px 14px; }
  .sv { font-size: 16px; }
}

/* ═══ ≤ 1200px — compact nav, hide non-essentials ═══ */
@media (max-width: 1200px) {
  .layout { grid-template-columns: 220px 1fr; }
  .tokbar { display: none; }
  .enrich-status { display: none !important; }
  .save-status { display: none; }
  .main.popen { padding-right: 380px; }
}

/* ═══ ≤ 1024px — TABLET: sidebar becomes drawer ═══ */
@media (max-width: 1024px) {
  /* Collapse grid to single column; sidebar is an overlay drawer */
  .layout {
    grid-template-columns: 1fr !important;
    grid-template-rows: 40px 1fr !important;
    max-width: 100vw;
    overflow: hidden;
  }
  /* Prevent nav flex from blowing up past viewport */
  .nav { min-width: 0; max-width: 100vw; overflow: hidden; }
  .tabs { min-width: 0; flex: 1 1 auto; }
  .nr { min-width: 0; }
  .nav { grid-column: 1 / -1; grid-row: 1; }
  .main, .layout.no-sb .main {
    grid-column: 1;
    grid-row: 2;
  }

  /* Sidebar: slide-in drawer */
  .sb {
    position: fixed;
    top: 40px;
    left: 0;
    bottom: 0;
    width: min(320px, 88vw);
    height: calc(100vh - 40px);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.4, 0, .2, 1);
    border-right: 1px solid var(--line);
    box-shadow: 2px 0 16px rgba(0, 0, 0, .35);
    grid-column: unset;
    grid-row: unset;
    background: var(--sidebar-bg, var(--bg1));
  }
  .sb.open { transform: translateX(0); }

  /* Hamburger visible */
  .nav-burger { display: inline-flex; align-items: center; justify-content: center; }

  /* Side CRM panel becomes full width drawer; don't push main */
  .main.popen { padding-right: 0 !important; }
  .panel { width: min(480px, 100vw); }

  /* Nav: prioritize brand + burger; scroll tabs */
  .brand .bname { font-size: 12px; }
  .vsep { display: none; }
  .tabs { gap: 0; }
  .tab { padding: 0 8px; font-size: 11px; height: 40px; }

  /* Nav actions: compact, allow scroll-x if crowded */
  .nr { gap: 3px; }
  .nr .btn { font-size: 10px; padding: 4px 8px; }

  /* Stats bar: smaller cells */
  .sc { padding: 6px 12px; }
  .sv { font-size: 14px; }
  .sl { font-size: 8px; }

  /* Outreach: single column */
  .outreach-wrap { padding: 12px; }
  .outreach-grid { grid-template-columns: 1fr !important; }
  .outreach-tpl-panel { max-height: 200px; }
  .outreach-main { flex-direction: column; }
  .outreach-main > .outreach-preview-panel {
    flex: none; max-width: 100%; min-width: 0; width: 100%;
  }
  .outreach-channel-metrics { grid-template-columns: repeat(2, 1fr); }
  .outreach-kpi-row { grid-template-columns: repeat(3, 1fr); }

  /* Chat layout stacks on tablet */
  .outreach-chat-layout { flex-direction: column; height: auto; min-height: unset; }
  .outreach-contact-list { width: 100% !important; min-width: 0 !important; max-width: 100% !important; max-height: 220px; border-right: none; border-bottom: 1px solid var(--line); }
  .outreach-chat-panel { min-height: 420px; }

  /* X/Instagram DM */
  .xdm-layout { flex-direction: column; height: auto !important; min-height: unset; }
  .xdm-sidebar { width: 100%; min-width: 0; max-width: 100%; max-height: 240px; border-right: none; border-bottom: 1px solid #2f3336; }
  .xdm-main { min-height: 420px; }

  /* DM browser (X / IG) */
  .dm-browser-layout { flex-direction: column; height: auto; gap: 8px; }
  .dm-kol-list { width: 100%; max-height: 160px; }
  .dm-sidebar { width: 100%; }
  .dm-content-split { flex-direction: column; gap: 8px; }
  .dm-content-fred { width: 100%; max-height: 260px; }

  /* Analytics KPIs */
  .ana-kpi-row { grid-template-columns: repeat(3, 1fr); }
  .ana-grid { grid-template-columns: 1fr; }

  /* Pipeline → mirror the Descartados kanban: horizontal-scroll flex row with
     narrow fixed-width stages so 2 columns fit on a phone and the rest scroll
     into view. The previous grid attempt targeted .pipeline (#pipeWrap), but
     stages actually live inside an inner .pipeline-row, so the grid only saw
     ONE child and left the layout half-empty.
     The .pipeline-row child (built by pipeline.js with inline `display:flex;
     overflow-x:auto`) already has the right horizontal rail — we just size
     the columns down for mobile and let the columns scroll. */
  .pipeline {
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
  }
  .pstage {
    min-width: 180px !important;
    width: 180px !important;
    flex-shrink: 0;
    border-right: 1px solid var(--line);
    border-bottom: none;
  }
  /* Drop the per-column max-height cap so each stage expands to fill the
     available height (was 320px which left huge empty area below). */
  .pcards {
    max-height: none !important;
    flex: 1;
    min-height: 80px;
    overflow-y: auto;
  }

  /* Tables: allow narrower min-width */
  table { min-width: 1000px; }
}

/* ═══ ≤ 768px — PHONE landscape / small tablet ═══ */
@media (max-width: 768px) {
  body { font-size: 12px; }
  .nav { padding: 0 6px; gap: 4px; height: 40px; }
  .brand { gap: 5px; }
  .bname { font-size: 11px; letter-spacing: .2px; }
  .tabs { overflow-x: auto; overflow-y: hidden; }
  .tab { padding: 0 7px; font-size: 10px; gap: 3px; }
  .tc { font-size: 7px; }

  /* Nav right: hide non-essentials on phone, but keep Settings + the user
     pill visible — those are the only two controls the user actually needs
     while browsing on mobile. Theme is now inside Settings so no separate
     theme button. */
  .nr .btn { font-size: 11px; padding: 0 10px; height: 28px; }
  #testBtn, #undoBtn, #redoBtn,
  .nr .btn[onclick="manualSave()"],
  #datosDrop { display: none !important; }
  /* Settings stays visible top-right — compact ⚙ icon. Forced to display
     because some other rules try to set .nr .btn display:none. */
  #settingsBtn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    font-size: 0 !important;
    width: 32px;
    min-width: 32px;
    height: 28px;
    padding: 0 !important;
    line-height: 1;
    flex-shrink: 0;
  }
  /* User asked for the 3-line / hamburger glyph instead of the gear.
     Using the box-drawing horizontal triple-line so it renders at the same
     visual weight as a button label across iOS/Android/desktop. */
  #settingsBtn::before {
    content: '☰';
    font-size: 18px;
    line-height: 1;
  }
  /* User pill in nav: shrink to avatar-only on phone (email reveals on tap) */
  .kf-authbar-wrap.in-nav .kf-pill-email,
  .kf-authbar-wrap.in-nav .kf-pill-roletag-wrap { display: none !important; }
  .kf-authbar-wrap.in-nav .kf-authpill { padding: 2px; }
  /* Burger ☰ on mobile is now redundant — the toolbar Filtros button is the
     filter entry point, and tab switching happens via the scrollable tab strip.
     Keeping the burger would make the nav feel cluttered. */
  .nav-burger { display: none !important; }
  /* Toolbar Filtros button — same visual weight as the FILTROS / ACCIONES
     <details> summaries below it, so the three controls form one uniform
     set (mono font, uppercase, same height/border/radius). */
  #toolbarFilters {
    height: 32px;
    padding: 0 12px;
    font-size: 11px;
    font-family: var(--mono);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .5px;
    border-radius: 10px;
    flex-shrink: 0;
    white-space: nowrap;
    color: var(--text2);
    background: var(--bg2);
    border: 1px solid var(--line);
  }
  /* + KOL button next to Avanzados */
  .toolbar-addkol-btn {
    height: 32px;
    padding: 0 10px;
    font-size: 11px;
    font-family: var(--mono);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .5px;
    border-radius: 10px;
    flex-shrink: 0;
    white-space: nowrap;
    color: var(--green);
    background: var(--bg2);
    border: 1px solid var(--green)55;
    gap: 4px;
  }
  /* Match summary heights of the FILTROS/ACCIONES dropdowns to the button */
  .list-filters-collapse summary,
  .list-actions-collapse summary {
    min-height: 32px;
    padding: 6px 12px !important;
  }
  .list-filters-collapse,
  .list-actions-collapse {
    border-radius: 10px !important;
  }

  /* Stats bar → 3-col grid, horizontal scroll as fallback */
  .sbar { display: grid; grid-template-columns: repeat(3, 1fr); overflow-x: visible; }
  .sc { padding: 6px 8px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); min-width: 0; }
  .sc:nth-child(3n) { border-right: none; }
  /* DB cell is duplicated in footer logbar; hide to keep grid balanced */
  .sc-db { display: none; }
  .sc-right {
    grid-column: 1 / -1;
    margin-left: 0;
    justify-content: flex-start;
    padding: 6px 10px;
    border-top: 1px solid var(--line);
    flex-wrap: wrap;
  }
  .sv { font-size: 14px; }
  .sl { font-size: 7px; }

  /* Toolbar */
  .toolbar { padding: 6px 8px; gap: 5px; }
  .swrap { flex: 1 1 100%; min-width: 0; }
  .sin { font-size: 12px; padding: 6px 10px 6px 26px; }
  .pgctrls { margin-left: 0; }
  .pgctrls .pglbl { display: none; }

  /* Bulk bar wraps */
  .bulk-bar { flex-wrap: wrap; }
  .bulk-bar .btn { font-size: 10px !important; padding: 3px 7px !important; }

  /* Side panel CRM: full-screen drawer */
  .panel { width: 100vw !important; max-width: 100vw !important; top: 40px; }
  .phd { gap: 8px; }
  .pav { width: 44px; height: 44px; font-size: 15px; }
  .pnm { font-size: 13px; }

  /* Modals: near full screen */
  .mo-box {
    width: 96vw !important;
    max-width: 96vw !important;
    max-height: 92vh;
    padding: 14px;
  }
  #settingsModal > div {
    width: 96vw !important;
    max-width: 96vw !important;
    max-height: 92vh;
  }
  .sett-tabs { padding: 0 10px; overflow-x: auto; }
  .sett-tab { padding: 8px 10px; font-size: 10px; }
  #settingsBody { padding: 12px !important; }

  /* Settings fields stack */
  .settings-field { flex-wrap: wrap; gap: 4px 6px; }
  .settings-label { min-width: 0; flex-basis: 100%; }
  .settings-input { flex: 1 1 100%; width: 100% !important; min-width: 0; }
  .settings-save-btn { flex-shrink: 0; }

  /* Import modal grid adjusts */
  .imp-grid-hdr, .imp-grid-row {
    grid-template-columns: 100px 1fr 80px;
    gap: 4px;
    padding: 4px 6px;
    font-size: 9px;
  }
  .imp-dz { padding: 22px 12px; }

  /* Outreach: single column KPIs + metrics */
  .outreach-wrap { padding: 10px; }
  .outreach-kpi-row { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .outreach-kpi { padding: 12px 10px; }
  .outreach-kpi-v { font-size: 20px; }
  .outreach-channel-metrics { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  .outreach-ch-metric-icon { width: 28px; height: 28px; font-size: 16px; }

  /* Analytics KPIs */
  .ana-kpi-row { grid-template-columns: repeat(2, 1fr); }
  .ana-kpi-v { font-size: 18px; }
  .chart-container { height: 180px; }
  .analytics-wrap { padding: 10px; gap: 10px; }
  .ana-card { padding: 10px; }

  /* Analyzer metrics */
  .az-card { padding: 12px; }
  .az-metrics { gap: 6px; }
  .az-metric { min-width: 68px; padding: 6px 8px; }

  /* Pipeline cards */
  .pcards { max-height: 260px; }
  .psh { padding: 10px; }

  /* Blocked kanban — horizontal scroll kept but cards narrower */
  #blockedKanban > div { min-width: 180px !important; width: 180px !important; }

  /* Logbar hides db badge */
  .logbar .db-badge { display: none; }
  .logbar { padding: 4px 8px; font-size: 7px; }

  /* Tables: thinner padding */
  th, td { padding: 6px 8px; font-size: 10px; }
  .chcell { min-width: 120px; }
  .chnm { font-size: 10px; max-width: 90px; }
  table { min-width: 900px; }
}

/* ═══ ≤ 480px — PHONE portrait ═══ */
@media (max-width: 480px) {
  .bname { font-size: 12px; }
  .brand .bdot { width: 6px; height: 6px; }

  /* Stats bar: 2 cols */
  .sbar { grid-template-columns: repeat(2, 1fr); }
  .sc:nth-child(3n) { border-right: 1px solid var(--line); }
  .sc:nth-child(2n) { border-right: none; }

  /* Status indicator smaller */
  .status-ind { padding: 2px 6px; font-size: 7px; }

  /* Panel */
  .ptop { padding: 10px 12px 8px; }
  .pbody { padding: 10px 12px; }
  .ptabs { padding: 0 10px; overflow-x: auto; }

  /* Import actions stack */
  .imp-actions { flex-direction: column; }
  .imp-actions .btn { width: 100%; justify-content: center; }
  .imp-actions .btn-gold { margin-left: 0 !important; }

  /* Outreach single col */
  .outreach-kpi-row { grid-template-columns: 1fr 1fr; }
  .outreach-channel-metrics { grid-template-columns: 1fr 1fr; }
  .outreach-tabs { gap: 4px; flex-wrap: wrap; }
  .outreach-tab { padding: 6px 12px; font-size: 10px; }

  /* Ana cards single col */
  .ana-kpi-row { grid-template-columns: 1fr 1fr; }
  .ana-kpi-v { font-size: 16px; }

  /* Tables allow narrower */
  table { min-width: 760px; }

  /* Kill decorative widths */
  .vsep, .brand { }
}

/* ─── Orientation helpers ─────────────────────────────────── */
@media (max-width: 768px) and (orientation: landscape) {
  /* In landscape on phones, reclaim height from tall drawers */
  .outreach-contact-list { max-height: 180px; }
  .xdm-sidebar { max-height: 180px; }
  .pcards { max-height: 200px; }
}

/* ─── Print basics ─────────────────────────────────────────── */
@media print {
  .nav, .sb, .panel, .logbar, .toolbar, .sb-backdrop { display: none !important; }
  .main { padding: 0 !important; }
  body { background: #fff; color: #000; }
}

/* ─── Hi-DPI: avoid blurry 1px borders ─────────────────────── */
@media (min-resolution: 2dppx) {
  .tab, .sett-tab { border-bottom-width: 2px; }
}
