/* =====================================================================
   Binguo — Mobile-first overrides pour Coach + Resa
   Applies on viewports ≤ 768px : bottom nav, bottom sheet modals,
   touch targets 44px+, safe area insets, pull-to-refresh, FAB.
   ===================================================================== */

/* === Safe area insets (notch iPhone, gesture bar Android) === */
: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);
  --bottom-nav-h: 64px;
  --fab-bottom: calc(var(--bottom-nav-h) + 18px + var(--safe-bottom));
}

@media (max-width: 768px) {

  /* ─── App shell : pas de sidebar, contenu plein écran ──────── */
  .app-shell {
    grid-template-columns: 1fr !important;
    height: 100dvh;       /* tient compte du clavier mobile */
    height: 100vh;        /* fallback */
  }
  .sidebar {
    /* La vieille sidebar drawer reste pour l'admin desktop, mais
       sur Coach/Resa on la cache et utilise bottom-nav à la place */
  }
  body.has-bottom-nav .sidebar {
    display: none;
  }
  body.has-bottom-nav .main {
    padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom));
  }

  /* ─── Topbar mobile compacte ─────────────────────────────────── */
  .topbar {
    min-height: 54px !important;
    padding: 8px 14px !important;
    padding-top: calc(8px + var(--safe-top)) !important;
    gap: 8px !important;
  }
  .topbar .search-wrap { max-width: none; flex: 1 1 0; min-width: 0; }
  .topbar .search-wrap input { font-size: 16px; /* ≥16px pour pas zoomer iOS */ }
  .topbar #orgSwitcherSlot { display: none; } /* déplacé dans bottom sheet */

  /* ─── Bottom navigation ──────────────────────────────────────── */
  .bottom-nav {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(var(--bottom-nav-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: color-mix(in srgb, var(--panel) 92%, transparent);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border);
    display: flex;
    z-index: 95;
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
  }
  .bottom-nav-item {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 8px 4px;
    color: var(--text-muted);
    background: none;
    border: none;
    font-size: 10px;
    font-weight: 600;
    cursor: pointer;
    position: relative;
    transition: color var(--t-fast);
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
  }
  .bottom-nav-item svg { width: 22px; height: 22px; transition: transform var(--t-fast); }
  .bottom-nav-item:active svg { transform: scale(0.88); }
  .bottom-nav-item.active { color: var(--primary); }
  .bottom-nav-item.active::before {
    content: '';
    position: absolute;
    top: 0; left: 50%;
    transform: translateX(-50%);
    width: 26px; height: 3px;
    background: var(--primary);
    border-radius: 0 0 3px 3px;
  }
  .bottom-nav-item .badge {
    position: absolute;
    top: 6px; right: calc(50% - 18px);
    background: var(--danger);
    color: white;
    font-size: 9px;
    font-weight: 700;
    min-width: 16px; height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--panel);
  }

  /* ─── FAB (Floating Action Button) ────────────────────────────── */
  .fab {
    position: fixed;
    bottom: var(--fab-bottom);
    right: 18px;
    width: 56px; height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: white;
    border: none;
    box-shadow: 0 6px 18px rgba(var(--primary-rgb), 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 90;
    transition: transform var(--t-fast), box-shadow var(--t-fast);
    -webkit-tap-highlight-color: transparent;
  }
  .fab:active { transform: scale(0.92); }
  .fab svg { width: 24px; height: 24px; }

  /* ─── Modals : bottom sheets ──────────────────────────────────── */
  .modal-overlay {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  .modal {
    max-width: 100vw !important;
    width: 100vw;
    max-height: 92vh;
    border-radius: 18px 18px 0 0 !important;
    margin: 0 !important;
    padding-bottom: var(--safe-bottom) !important;
    animation: bottomSheetIn 0.28s var(--ease) !important;
  }
  @keyframes bottomSheetIn {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }
  .modal-header {
    position: sticky;
    top: 0;
    z-index: 5;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    padding: 14px 18px !important;
  }
  .modal-header::before {
    content: '';
    display: block;
    width: 40px; height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: -6px auto 10px;
  }
  .modal-body { padding: 16px 18px !important; }
  .modal-footer {
    position: sticky;
    bottom: 0;
    background: var(--panel);
    padding: 12px 16px !important;
    padding-bottom: calc(12px + var(--safe-bottom)) !important;
    border-top: 1px solid var(--border);
    flex-wrap: wrap;
  }
  .modal-footer .btn { flex: 1 1 0; min-width: 100px; min-height: 44px; }

  /* ─── Drawer : full-screen sheet sur mobile ──────────────────── */
  .drawer {
    width: 100vw !important;
    max-width: 100vw !important;
    border-radius: 18px 18px 0 0;
    top: auto !important;
    height: calc(100dvh - 40px);
    transform: translateY(100%) !important;
    transition: transform 0.28s var(--ease) !important;
    box-shadow: 0 -16px 40px rgba(0,0,0,0.4) !important;
  }
  .drawer.open { transform: translateY(0) !important; }
  .drawer-header {
    padding: 14px 18px !important;
    padding-top: calc(14px + var(--safe-top));
  }
  .drawer-header::before {
    content: '';
    display: block;
    width: 40px; height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: -4px auto 10px;
  }

  /* ─── Touch targets 44px+ minimum (Apple HIG / Material) ─────── */
  .btn,
  .filter-chip,
  .nav-item,
  .icon-btn,
  .row-action-btn,
  .toggle,
  .tab,
  button:not(.bottom-nav-item):not(.modal-close):not(.fab) {
    min-height: 44px;
    min-width: 44px;
  }
  /* btn-sm reste compact mais tactile */
  .btn-sm { min-height: 44px; padding-top: 8px; padding-bottom: 8px; }
  .row-action-btn { min-width: 44px; min-height: 44px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .modal-close { min-width: 44px; min-height: 44px; }
  /* Le toggle visuel reste petit mais on étend la hit-area */
  .toggle { position: relative; }
  .toggle::before { content: ''; position: absolute; inset: -10px; }

  /* Form inputs : font ≥16px pour empêcher zoom iOS */
  input, select, textarea {
    font-size: 16px !important;
  }

  /* ─── Cards plus aérées tactilement ──────────────────────────── */
  .panel { padding: 16px !important; border-radius: 14px; }
  .kpi-card { padding: 16px !important; }
  .kpi-grid { gap: 10px !important; margin-bottom: 16px !important; }

  /* Un seul KPI par ligne sur très petits écrans */
  @media (max-width: 380px) {
    .kpi-grid { grid-template-columns: 1fr !important; }
  }

  /* ─── Tables horizontales : scroll-snap + sticky col 1 ───────── */
  .table-wrap, .panel > div[style*="overflow-x"] {
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }
  table { font-size: 13px; }
  th, td { padding: 10px 12px !important; }

  /* ─── List items denses (séances, adhérents, etc.) ──────────── */
  .task-item, .activity-item {
    padding: 12px 14px !important;
    min-height: 60px;
    border-radius: 12px;
  }

  /* ─── View headers stack vertical ────────────────────────────── */
  .view-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px !important;
  }
  .view-header h2 {
    font-size: 22px !important;
    margin-bottom: 0;
  }
  .view-header .flex.gap-2 { width: 100%; flex-wrap: wrap; }
  .view-header .btn { flex: 1 1 auto; }

  /* ─── Pull-to-refresh indicator (placeholder, JS gère le geste) */
  .pull-refresh {
    position: absolute;
    top: 0; left: 50%;
    transform: translate(-50%, -100%);
    width: 36px; height: 36px;
    border-radius: 50%;
    background: var(--panel);
    border: 1px solid var(--border);
    display: flex; align-items: center; justify-content: center;
    color: var(--primary);
    box-shadow: var(--shadow);
    transition: transform 0.18s var(--ease);
    z-index: 50;
  }
  .pull-refresh.active { transform: translate(-50%, 16px); }
  .pull-refresh.refreshing svg { animation: spin 1s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* ─── Disable hover effects on touch devices ─────────────────── */
  .panel:hover,
  .kpi-card:hover,
  .nav-item:hover,
  tbody tr:hover { transform: none !important; box-shadow: none !important; }

  /* ─── Skeleton loaders ──────────────────────────────────────── */
  .skel {
    background: linear-gradient(90deg, var(--panel-2) 25%, var(--border) 50%, var(--panel-2) 75%);
    background-size: 200% 100%;
    animation: skelShimmer 1.4s linear infinite;
    border-radius: 8px;
  }
  @keyframes skelShimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

  /* ─── Toast au-dessus de la bottom-nav ──────────────────────── */
  body.has-bottom-nav .toast {
    bottom: calc(var(--bottom-nav-h) + 16px + var(--safe-bottom)) !important;
    right: 12px !important;
    left: 12px !important;
    max-width: none !important;
    margin: 0 auto;
  }

  /* ─── Empty states améliorés ────────────────────────────────── */
  .empty {
    text-align: center;
    padding: 48px 20px;
    color: var(--text-dim);
  }
  .empty-icon {
    width: 72px; height: 72px;
    border-radius: 50%;
    background: var(--panel-2);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
  }
  .empty-icon svg { width: 32px; height: 32px; color: var(--text-muted); }
  .empty h3 { font-size: 16px; font-weight: 700; margin-bottom: 6px; color: var(--text); }
  .empty p { font-size: 13px; line-height: 1.5; max-width: 320px; margin: 0 auto 18px; }
  .empty .btn { min-width: 200px; }
}

/* === Skeleton (utilisable aussi sur desktop, pas que mobile) === */
.skel-line { height: 14px; }
.skel-line-sm { height: 11px; width: 60%; }
.skel-card { height: 80px; }
.skel-grid { display: grid; gap: 10px; }

/* === Petits écrans (320px - 380px) === */
@media (max-width: 380px) {
  .topbar { padding: 6px 10px !important; }
  .view-container { padding: 12px !important; }
  .panel, .kpi-card { padding: 12px !important; }
  h2 { font-size: 19px !important; }
  .btn { padding: 8px 12px; font-size: 12px; }
}

/* === Préférence dark mode système (par défaut on est déjà dark) === */
@media (prefers-color-scheme: light) {
  /* Le toggle theme reste manuel via [data-theme="light"] */
}
