/* ============================================================
   AnalizaVode CRM — MOBILE / PWA layer
   ------------------------------------------------------------
   IMPORTANT: This file is loaded AFTER style.css and contains
   ONLY small-screen / touch enhancements.

   Desktop is intentionally untouched:
   - The "mobile chrome" (top bar, drawer overlay, bottom bar)
     is display:none by default and only enabled inside
     @media (max-width: 900px).
   - All layout overrides live inside @media (max-width: 900px).
   ============================================================ */

/* ------------------------------------------------------------
   MOBILE CHROME — hidden on desktop (default state)
   ------------------------------------------------------------ */
.mobile-topbar { display: none; }

.mobile-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 30, 0.55);
  z-index: 1150;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.28s ease, visibility 0.28s ease;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.mobile-overlay.show { opacity: 1; visibility: visible; }

/* ------------------------------------------------------------
   INSTALL APP buttons — hidden until the browser offers install
   (works on mobile and desktop Chromium)
   ------------------------------------------------------------ */
.install-btn {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  padding: 11px 16px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.06);
  color: var(--white);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.install-btn.show { display: flex; }
.install-btn:active { background: rgba(255, 255, 255, 0.16); }

/* ============================================================
   SMALL SCREENS / TOUCH DEVICES
   ============================================================ */
@media (max-width: 900px) {

  /* ---------- Base ---------- */
  html { -webkit-text-size-adjust: 100%; }
  body {
    font-size: 15px;
    overscroll-behavior-y: none;
  }

  /* ---------- Horizontal-overflow guard ----------
     .main-content is a flex child with min-width:auto, so it would
     refuse to shrink below its content's min-content width (wide
     tables), pushing the whole layout wider than the viewport and
     causing horizontal scrolling / zoomed-out rendering on phones.
     min-width:0 lets it shrink; overflow-x:hidden is a safety net. */
  html, body { overflow-x: hidden; }
  .app .main-content { min-width: 0; width: 100%; }
  .view,
  .page-header,
  .header-actions,
  .table-container { min-width: 0; max-width: 100%; }

  /* Mobile chrome is only shown once the user is signed in */
  body:not(.av-logged-in) .mobile-topbar { display: none; }

  /* ============================================================
     TOP APP BAR
     ============================================================ */
  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: calc(56px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 8px 0 8px;
    background: var(--dark);
    color: var(--white);
    z-index: 1100;
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.18);
  }

  .mtb-btn {
    flex: 0 0 auto;
    width: 44px; height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--white);
    font-size: 22px;
    line-height: 1;
    border-radius: 10px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .mtb-btn:active { background: rgba(255, 255, 255, 0.14); }

  .mtb-logo {
    height: 22px;
    width: auto;
    filter: brightness(0) invert(1);
    flex: 0 0 auto;
  }

  .mtb-title {
    font-size: 15px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.92);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1 1 auto;
    min-width: 0;
  }

  /* ============================================================
     SIDEBAR -> OFF-CANVAS DRAWER
     (overrides the legacy 60px icon-rail behaviour in style.css)
     ============================================================ */
  .app .sidebar {
    width: 274px;
    max-width: 84vw;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 0 44px rgba(0, 0, 0, 0.45);
    z-index: 1200;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    overflow-y: auto;
  }
  .app .sidebar.open { transform: translateX(0); }

  /* Restore labels / badges / logout that the legacy rule hid */
  .app .sidebar .logo-text { display: block; }
  .app .sidebar .nav-badge { display: inline-block; }
  .app .sidebar .btn-logout { display: block; }
  .app .sidebar .nav-item span:not(.nav-icon) { display: inline-flex; }

  .app .sidebar .nav-item {
    font-size: 15px;
    padding: 13px 14px;
    gap: 12px;
    border-radius: 10px;
  }
  .app .sidebar .nav-icon { font-size: 18px; width: 22px; text-align: center; }
  .app .sidebar .sidebar-nav { overflow-y: auto; }

  /* ============================================================
     MAIN CONTENT
     ============================================================ */
  .app .main-content {
    margin-left: 0;
    padding: 12px;
    padding-top: calc(56px + env(safe-area-inset-top) + 12px);
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
    min-height: 100dvh;
  }

  /* ============================================================
     PAGE HEADER / SEARCH / ACTIONS
     ============================================================ */
  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 14px;
  }
  .page-header h2 { font-size: 1.3rem; }

  .header-actions {
    width: 100%;
    display: flex;
    gap: 8px;
    align-items: center;
  }
  .header-actions .search-input { flex: 1 1 auto; width: auto; min-width: 0; }

  .search-input,
  .select-input,
  .form-group input,
  .form-group select,
  .form-group textarea {
    font-size: 16px; /* prevents iOS auto-zoom on focus */
  }
  .search-input { width: 100%; }

  .section-title { font-size: 13px; }

  /* ============================================================
     BUTTONS / TOUCH TARGETS
     ============================================================ */
  .btn {
    min-height: 44px;
    padding: 11px 16px;
    font-size: 14px;
    border-radius: 10px;
  }
  .btn-sm { min-height: 38px; padding: 8px 12px; }

  .filter-pills {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 8px;
    padding-bottom: 6px;
    margin-bottom: 4px;
  }
  .filter-pills::-webkit-scrollbar { display: none; }
  .pill {
    flex: 0 0 auto;
    min-height: 38px;
    padding: 8px 14px;
    font-size: 13px;
  }

  /* ============================================================
     DASHBOARD GRIDS
     ============================================================ */
  .stats-grid,
  .stats-grid-microf { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .pipeline-grid,
  .pipeline-grid-microf { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .info-grid { grid-template-columns: 1fr; }

  .stat-card { padding: 14px; }
  .stat-value { font-size: 1.5rem; }
  .stat-label { font-size: 11px; }
  .pipeline-card { padding: 12px 8px; }

  /* ============================================================
     TABLES -> CARDS
     (labels are injected by js/mobile.js via td[data-label])
     ============================================================ */
  .table-container {
    background: transparent;
    box-shadow: none;
    border-radius: 0;
    overflow: visible;
  }

  .data-table { display: block; width: 100%; }
  .data-table thead { display: none; }
  .data-table tbody { display: block; }

  .data-table tr {
    display: block;
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: 14px;
    box-shadow: var(--shadow);
    margin-bottom: 12px;
    overflow: hidden;
  }
  .data-table tr:hover td { background: transparent; }

  .data-table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--gray-100);
    font-size: 14px;
    text-align: right;
    min-height: 44px;
  }
  .data-table td:last-child { border-bottom: none; }

  .data-table td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    max-width: 45%;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.4px;
    text-transform: uppercase;
    color: var(--gray-400);
    text-align: left;
  }

  /* Rows that span the whole table (loading / empty states) */
  .data-table td[colspan] {
    display: block;
    text-align: center;
    justify-content: center;
    min-height: 0;
    padding: 22px 14px;
    color: var(--gray-400);
  }
  .data-table td[colspan]::before { content: none; }

  /* Actions cell: buttons stretch a little for easier tapping */
  .data-table td .btn,
  .data-table td .btn-sm { min-height: 36px; }

  /* ============================================================
     PAGINATION
     ============================================================ */
  .pagination {
    justify-content: center;
    flex-wrap: wrap;
    gap: 6px;
    padding: 14px 0 4px;
  }

  /* ============================================================
     MODALS -> BOTTOM SHEETS
     ============================================================ */
  .modal { align-items: flex-end; }

  .modal-content {
    max-width: 100% !important;
    width: 100%;
    max-height: 92dvh;
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal-header {
    padding: 16px 18px;
    border-radius: 20px 20px 0 0;
  }
  .modal-header h3 { font-size: 1.05rem; }
  .modal-body { padding: 18px; }
  .modal-close { width: 40px; height: 40px; font-size: 20px; }

  .info-item label { font-size: 11px; }

  .actions-row { flex-direction: column; gap: 10px; }
  .actions-row .btn { width: 100%; }

  /* ============================================================
     LOGIN SCREEN
     ============================================================ */
  .login-screen {
    padding: 20px;
    align-items: flex-start;
    padding-top: calc(12vh + env(safe-area-inset-top));
  }
  .login-card {
    padding: 28px 22px;
    border-radius: 18px;
    max-width: 100%;
  }

  /* ============================================================
     TOAST — lift above the bottom bar
     ============================================================ */
  .toast {
    left: 12px;
    right: 12px;
    bottom: calc(20px + env(safe-area-inset-bottom));
    text-align: center;
  }

}

/* ============================================================
   VERY SMALL PHONES
   ============================================================ */
@media (max-width: 380px) {
  .stats-grid,
  .stats-grid-microf,
  .pipeline-grid,
  .pipeline-grid-microf { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-value { font-size: 1.3rem; }
  .mtb-title { font-size: 14px; }
}

/* ============================================================
   TOUCH DEVICES (any width) — comfortable tap targets
   ============================================================ */
@media (pointer: coarse) {
  .nav-item { min-height: 44px; }
  .data-table td .btn,
  .data-table td .btn-sm { min-height: 38px; }
}
