/* ==========================================================================
   SELLOUT · mobile.css
   Couche mobile-first dédiée iPhone / petits écrans (V2.5.2)
   Chargée APRÈS v2-theme.css et v25-sources.css → ses règles l'emportent.
   ========================================================================== */

/* Safe-area iOS (notch, barre home) — dispo dès l'ajout de viewport-fit=cover */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* ==========================================================================
   COMMUN — touch-friendly partout (même en desktop étroit)
   ========================================================================== */

@media (pointer: coarse) {
  button, .btn, .btn-header, .btn-cta, .btn-sync,
  .period-btn, .tab-btn, .modal-close {
    min-height: 40px;
  }
}

/* ==========================================================================
   TABLETTE & MOBILE (≤ 900px)
   ========================================================================== */

@media (max-width: 900px) {

  html, body {
    -webkit-text-size-adjust: 100%;
    overflow-x: hidden;
  }

  body {
    font-size: 14px;
    padding-bottom: calc(var(--safe-bottom) + 8px);
  }

  /* Inputs : 16px minimum pour empêcher le zoom auto Safari iOS */
  input, select, textarea {
    font-size: 16px !important;
    padding: 10px 12px;
    border-radius: 8px;
    min-height: 44px;
  }

  /* Exception : inputs très petits dans les cartes (ex. capacité inline) */
  .inline-input, input.compact, .show-card input[type="number"] {
    font-size: 16px !important;
    min-height: 40px;
  }

  /* Sections : respirer */
  .section {
    padding: 14px 16px;
  }

  .card {
    padding: 14px;
    border-radius: 14px;
  }
}

/* ==========================================================================
   MOBILE PUR (≤ 768px) — iPhone et petits Android
   ========================================================================== */

@media (max-width: 768px) {

  /* --- HEADER : compact + drawer géré par mobile.js --- */

  .app-header {
    padding: 10px 14px;
    padding-top: calc(10px + var(--safe-top));
    padding-left: calc(14px + var(--safe-left));
    padding-right: calc(14px + var(--safe-right));
    flex-wrap: nowrap;
    gap: 8px;
  }

  .app-header-left {
    gap: 10px;
    flex: 1;
    min-width: 0;
  }

  .brand-name { font-size: 14px; letter-spacing: 0.04em; }
  .brand-sub { font-size: 8px; letter-spacing: 0.18em; }
  .brand-sep { display: none; }
  .brand-version {
    font-size: 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
  }

  /* Boutons secondaires masqués par défaut sur mobile, accessibles via drawer */
  .app-header-right .btn-header,
  .app-header-right .header-pill,
  .app-header-right .export-summary-btn,
  .app-header-right .btn-print,
  .app-header-right .save-indicator,
  .app-header-right .live-badge {
    display: none;
  }

  /* Message d'état (vibe) : redondant avec le badge de la show-card sur mobile */
  .app-header-left .vibe-msg {
    display: none;
  }

  /* Left : éviter le débordement du back + brand + version */
  .app-header-left .brand-name-sm { font-size: 13px; }
  .app-header-left .brand-sub-sm { font-size: 7px; }
  .app-header-left .version-link { font-size: 10px; }

  /* Le CTA "Nouveau show" reste visible mais compact */
  .app-header-right .btn-cta {
    padding: 8px 12px;
    font-size: 12px;
    border-radius: 10px;
    white-space: nowrap;
  }

  /* Quand le drawer injecte ces éléments, ils s'affichent en pleine largeur */
  #mobile-drawer .btn-header,
  #mobile-drawer .header-pill,
  #mobile-drawer .export-summary-btn,
  #mobile-drawer .btn-print,
  #mobile-drawer .save-indicator,
  #mobile-drawer .live-badge,
  #mobile-drawer .vibe-msg {
    display: flex !important;
    width: 100%;
    justify-content: flex-start;
    align-items: center;
    padding: 12px 16px;
    font-size: 14px;
    min-height: 48px;
    border-radius: 10px;
    text-align: left;
  }

  /* Bouton burger */
  .mobile-burger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: var(--card-hover, rgba(0,0,0,0.04));
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
  }

  [data-theme="dark"] .mobile-burger {
    background: rgba(255,255,255,0.06);
  }

  /* --- HERO --- */
  .hero {
    padding: 14px 16px 6px;
  }
  .hero-title { font-size: 18px; letter-spacing: 0.02em; }
  .hero-sub { font-size: 12px; }

  /* --- SYNC BAR --- */
  .sync-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 14px;
  }
  .btn-sync {
    width: 100%;
    padding: 12px 16px;
    font-size: 13px;
    min-height: 48px;
    border-radius: 10px;
  }
  .sync-status { text-align: center; font-size: 11px; }

  /* --- SHOWS HEADER / TRI --- */
  .shows-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  .shows-title { font-size: 16px; }
  .shows-sort {
    width: 100%;
    flex-wrap: wrap;
    gap: 6px;
  }
  .shows-sort-label { display: none; }
  .period-btn {
    flex: 1;
    min-width: 0;
    padding: 10px 8px;
    font-size: 12px;
    min-height: 42px;
    white-space: nowrap;
  }

  /* --- CARDS / SHOW CARDS : passer en colonne unique --- */
  .show-card,
  .tour-card,
  .kpi-row,
  #kpi-row,
  .kpi-grid,
  .stats-grid,
  .two-col,
  .three-col,
  .form-grid {
    grid-template-columns: 1fr !important;
    flex-direction: column !important;
    align-items: stretch !important;
  }

  /* KPI cards : s'assurer que chaque carte prend toute la largeur dispo */
  #kpi-row > div {
    flex: 1 1 100% !important;
    min-width: 0 !important;
    width: 100%;
  }

  /* Masquer le composant legacy v2-mobile-kpis : redondant avec #kpi-row
     et cause un anneau SVG géant car non stylé. */
  #mobile-kpis-container,
  .v2-mobile-kpis {
    display: none !important;
  }

  /* Show-detail : row des show cards (show-card / budget / tournée / projection)
     passe en colonne sur mobile */
  #show-card,
  #show-card ~ .card {
    min-width: 0 !important;
    flex: 1 1 100% !important;
  }

  /* Le conteneur parent des 4 show-cards force flex-wrap:wrap inline, on
     s'assure qu'aucune carte ne tente de rester sur une ligne étriquée. */
  #view-show .section + * > div[style*="flex-wrap:wrap"],
  #view-show .no-print > div[style*="flex-wrap:wrap"] {
    gap: 10px !important;
  }

  /* --- TABS BAR (onglets show view) : activer scroll horizontal --- */
  .tabs-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap !important;
    padding: 0 14px !important;
    scrollbar-width: none;
  }
  .tabs-bar::-webkit-scrollbar { display: none; }
  .tabs-bar .tab-btn {
    padding: 12px 14px;
    font-size: 13px;
    white-space: nowrap;
    flex-shrink: 0;
    min-height: 44px;
  }

  /* --- PLAN & BUDGET : grille 6-cols inadaptée sur mobile --- */
  /* Sur mobile on ne garde que Toggle | Touchpoint | Cible €.
     "Par qui", "Réel €" et "Priorité" sont masqués (accessibles depuis le
     Rapport ou en ouvrant le détail via expand). */
  .plan-row,
  .plan-header {
    grid-template-columns: 32px 1fr 80px !important;
    gap: 6px !important;
    padding: 8px 10px !important;
    font-size: 12px;
  }
  .plan-row > *:nth-child(3),
  .plan-row > *:nth-child(5),
  .plan-row > *:nth-child(6),
  .plan-header > *:nth-child(3),
  .plan-header > *:nth-child(5),
  .plan-header > *:nth-child(6) {
    display: none !important;
  }
  .plan-row input[type="number"],
  .plan-row input[type="text"] {
    width: 100% !important;
    min-width: 0 !important;
  }

  /* --- FORMULAIRES --- */
  .form-row,
  .form-row.single,
  .form-row.two,
  .form-row.three {
    grid-template-columns: 1fr !important;
    gap: 12px;
  }

  label, .label {
    font-size: 13px;
  }

  /* --- TABLES : scroll horizontal avec ombre indicative --- */
  .table-wrap,
  .table-scroll,
  .scroll-x {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    margin: 0 -14px;
    padding: 0 14px;
  }

  table {
    font-size: 12px;
    min-width: 480px; /* force scroll plutôt qu'écrasement */
  }

  th, td {
    padding: 10px 10px;
    white-space: nowrap;
  }

  /* Wrapper auto injecté par mobile.js pour les tables non déjà wrappées */
  .mobile-scroll-x {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 8px -14px;
    padding: 0 14px 6px;
    scrollbar-width: thin;
  }
  .mobile-scroll-x::-webkit-scrollbar { height: 4px; }
  .mobile-scroll-x::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
  }

  /* Indicateur visuel "scrollable" */
  .mobile-scroll-hint::after {
    content: "→ swipe";
    display: block;
    font-size: 10px;
    color: var(--muted);
    text-align: right;
    padding: 4px 4px 0;
    opacity: 0.6;
  }

  /* --- TABS scrollables --- */
  .tab-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    margin: 0 -14px;
    padding: 0 14px;
    scrollbar-width: none;
  }
  .tab-container::-webkit-scrollbar { display: none; }
  .tab-btn {
    padding: 12px 14px;
    font-size: 13px;
    white-space: nowrap;
    flex-shrink: 0;
    min-height: 44px;
  }

  /* --- MODALES : fullscreen sur mobile --- */
  .modal-overlay {
    padding: 0;
    align-items: flex-end;
  }
  .modal-box {
    max-width: 100%;
    width: 100%;
    max-height: 100vh;
    height: 100vh;
    border-radius: 16px 16px 0 0;
    padding: 20px 16px;
    padding-bottom: calc(20px + var(--safe-bottom));
    overflow-y: auto;
    animation: mobileSlideUp 0.25s ease;
  }
  @keyframes mobileSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }
  .modal-header h3 { font-size: 17px; }
  .modal-close {
    width: 40px;
    height: 40px;
    top: 12px;
    right: 12px;
  }
  .modal-footer {
    flex-direction: column-reverse;
    gap: 8px;
    position: sticky;
    bottom: 0;
    background: var(--card);
    padding-top: 12px;
    border-top: 1px solid var(--border);
    margin-top: 20px;
  }
  .modal-footer .btn,
  .modal-footer button {
    width: 100%;
    min-height: 48px;
    font-size: 14px;
  }

  /* --- HEATMAP / GANTT : plus lisibles --- */
  .heatmap-grid {
    grid-template-columns: repeat(7, 1fr) !important;
    gap: 3px;
  }
  .heatmap-cell { font-size: 9px; min-height: 28px; }

  .gantt-row {
    grid-template-columns: 80px 1fr !important;
    gap: 6px;
  }
  .gantt-label { font-size: 10px; }

  /* --- PROGRESS BARS & BADGES lisibles --- */
  .bdg { font-size: 10px; padding: 4px 10px; }
  .pb-wrap { height: 8px; }

  /* --- FOOTER branding --- */
  .app-footer, footer {
    padding: 16px !important;
    padding-bottom: calc(16px + var(--safe-bottom)) !important;
    text-align: center;
  }

  /* Boutons pleine largeur dans les panneaux de config source */
  .sources-panel button,
  .source-actions button {
    width: 100%;
    min-height: 42px;
    margin-top: 6px;
  }

  /* Charts Chart.js : limiter la hauteur pour ne pas exploser le scroll */
  canvas[id*="chart"], canvas.chart-canvas {
    max-height: 280px !important;
  }
}

/* ==========================================================================
   DRAWER MOBILE (injecté par mobile.js)
   ========================================================================== */

.mobile-drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 9998;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.mobile-drawer-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

#mobile-drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: min(320px, 85vw);
  background: var(--card);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 32px rgba(0,0,0,0.2);
  z-index: 9999;
  transform: translateX(100%);
  transition: transform 0.25s cubic-bezier(.4,0,.2,1);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px 16px;
  padding-top: calc(20px + var(--safe-top));
  padding-bottom: calc(20px + var(--safe-bottom));
  overflow-y: auto;
}

#mobile-drawer.open {
  transform: translateX(0);
}

#mobile-drawer .drawer-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--muted);
  text-transform: uppercase;
  padding: 8px 4px;
}

#mobile-drawer .drawer-close {
  align-self: flex-end;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  background: var(--card-hover, transparent);
  border-radius: 10px;
  cursor: pointer;
  color: var(--text);
  font-size: 18px;
  margin-bottom: 8px;
}

/* ==========================================================================
   PETIT MOBILE (≤ 400px)
   ========================================================================== */

@media (max-width: 400px) {
  .app-header { padding-left: calc(10px + var(--safe-left)); padding-right: calc(10px + var(--safe-right)); }
  .section { padding: 12px 12px; }
  .card { padding: 12px; }
  .brand-name { font-size: 13px; }
  .brand-version { max-width: 90px; }
  .app-header-right .btn-cta { padding: 7px 10px; font-size: 11px; }
  .hero-title { font-size: 16px; }
  .period-btn { font-size: 11px; padding: 9px 6px; }
}

/* ==========================================================================
   ORIENTATION LANDSCAPE iPhone — éviter que le header bouffe tout
   ========================================================================== */

@media (max-width: 900px) and (orientation: landscape) {
  .app-header { padding-top: 8px; padding-bottom: 8px; }
  .modal-box { height: 100vh; max-height: 100vh; border-radius: 0; }
}
