/* Aiess CRM — phone layout (≤ 768px). Shipped with the release as assets/mobile-N.css; desktop is untouched.
 * Uses only the CRM's own design tokens. Pairs with mobile.js (bottom tab bar, "More" sheet, element tagging). */

@media (width <= 768px) {
  :root { --sidebar-width: 0px; --mnav-h: 64px; --safe-b: env(safe-area-inset-bottom, 0px); }
  html { -webkit-tap-highlight-color: transparent; }
  body { overscroll-behavior-y: none; }
  .app-content, .app-main, .card, .kanban-cards { scrollbar-width: none; }
  .app-content::-webkit-scrollbar, .app-main::-webkit-scrollbar { display: none; }
  button, a, select, input, [role=button] { touch-action: manipulation; }

  /* ---------- shell: no sidebar, bottom tab bar instead ---------- */
  .sidebar { display: none !important; }
  .app-main { margin-left: 0 !important; width: 100% !important; min-width: 0; }
  .app-content { padding: 14px 16px calc(var(--mnav-h) + var(--safe-b) + 28px) !important; }

  /* ---------- top bar: compact, sticky, frosted ---------- */
  .topbar {
    position: sticky; top: 0; z-index: 30; height: 56px !important; min-height: 56px; padding: 0 8px 0 16px !important; gap: 4px !important;
    background: color-mix(in srgb, var(--color-bg) 88%, transparent) !important;
    backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
    border-bottom: 1px solid var(--hairline) !important;
  }
  .topbar-title { display: flex !important; align-items: center; margin: 0 !important; line-height: 1.2 !important; font-size: 17px !important; font-weight: 600 !important; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
  .topbar-title::before {
    content: "A"; display: inline-grid; place-items: center; width: 26px; height: 26px; margin-right: 10px; flex-shrink: 0;
    border-radius: 8px; background: var(--accent); color: #fff; font: 600 15px/1 var(--font-serif, Georgia, serif);
  }
  .topbar-actions { gap: 2px !important; flex-shrink: 0; }
  .topbar-actions .btn-icon { width: 40px !important; height: 40px !important; border-radius: 12px !important; }
  .topbar-actions .btn-primary { box-shadow: none !important; width: 40px; height: 40px; min-width: 40px; padding: 0 !important; border-radius: 12px !important; justify-content: center; font-size: 0 !important; gap: 0 !important; margin-left: 4px; }
  .topbar-actions .btn-primary > span { display: none !important; }
  .topbar-actions .btn-primary svg { width: 20px !important; height: 20px !important; margin: 0 !important; }
  .m-hide-sm { display: none !important; }

  /* ---------- page headers ---------- */
  .page-header { flex-direction: column !important; align-items: stretch !important; gap: 12px !important; margin-bottom: 16px !important; }
  .page-title, .page-content h1:first-of-type { font-size: 26px !important; line-height: 1.15 !important; letter-spacing: -0.02em; }
  .page-subtitle { font-size: 14px !important; line-height: 1.45 !important; }
  .m-actions {
    display: flex !important; flex-wrap: nowrap !important; gap: 8px !important; overflow-x: auto; scrollbar-width: none;
    margin-left: -16px !important; margin-right: -16px !important; padding: 2px 16px 4px !important; scroll-padding: 16px;
  }
  .m-actions::-webkit-scrollbar { display: none; }
  .m-actions > * { flex: 0 0 auto !important; white-space: nowrap; }
  .m-actions .btn, .m-actions .whatsapp-btn { height: 38px; border-radius: 999px !important; padding: 0 14px !important; font-size: 13.5px !important; }

  /* ---------- filters: one swipeable row of pills ---------- */
  .filter-bar, .m-filters {
    display: flex !important; flex-wrap: nowrap !important; gap: 8px !important; overflow-x: auto; scrollbar-width: none;
    margin: 0 -16px 14px !important; padding: 2px 16px 4px !important; align-items: center;
  }
  .filter-bar::-webkit-scrollbar, .m-filters::-webkit-scrollbar { display: none; }
  .filter-bar > *, .m-filters > * { flex: 0 0 auto !important; width: auto !important; min-width: 0 !important; max-width: none !important; }
  .filter-bar .input-with-icon, .m-filters > .input-with-icon, .m-filters > input.input { flex: 0 0 210px !important; width: 210px !important; }
  .filter-bar select.input, .m-filters select.input {
    height: 36px !important; border-radius: 999px !important; padding: 0 30px 0 14px !important; font-size: 13.5px !important; font-weight: 500;
    background-color: var(--surface) !important; background-position: right 10px center !important; max-width: 220px !important;
  }
  .filter-bar input.input, .m-filters input.input { height: 36px !important; border-radius: 999px !important; font-size: 14px !important; }

  /* ---------- inputs never trigger zoom, comfortable targets ---------- */
  input.input, select.input, textarea.input, .input { font-size: 16px; }
  .btn { min-height: 38px; }
  .btn-sm { min-height: 34px; }

  /* ---------- KPI / stat cards: 2-up grid ---------- */
  .m-kpis, div:has(> .stat-card + .stat-card) { display: grid !important; grid-template-columns: 1fr 1fr !important; gap: 10px !important; }
  .m-kpis > .card, .stat-card { padding: 14px !important; border-radius: 14px !important; min-width: 0; }
  .m-kpis > .card > div:last-child, .stat-card-value { font-size: 22px !important; line-height: 1.2 !important; }
  .m-kpis > .card > div:first-child, .stat-card-label { font-size: 11.5px !important; letter-spacing: 0.02em; }
  .stat-card-change { font-size: 12px !important; }
  .m-kpis > .card:first-child:nth-last-child(odd), div:has(> .stat-card + .stat-card) > .stat-card:first-child:nth-last-child(odd) { grid-column: 1 / -1; }

  /* ---------- Leads table → cards ---------- */
  .card:has(> .m-card-table), .card:has(> div > .m-card-table) { padding: 0 !important; overflow: hidden; }
  .card > div:has(> .m-card-table) { padding: 0 !important; overflow: visible !important; }
  .m-card-table { display: block !important; width: 100% !important; min-width: 0 !important; table-layout: auto !important; }
  .m-card-table thead { display: none !important; }
  .m-card-table tbody { display: block !important; }
  .m-card-table tr {
    display: grid !important; gap: 8px 10px; padding: 14px 14px !important; border-bottom: 1px solid var(--hairline);
    align-items: center; position: relative; min-width: 0;
  }
  .m-card-table td { display: block; padding: 0 !important; border: 0 !important; width: auto !important; min-width: 0; overflow: hidden; }
  .m-card-table tr:last-child { border-bottom: 0; }
  .m-card-table tr:active { background: var(--surface-hover); }

  .m-leads-table tr { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "lead date" "phone status" "src menu"; }
  .m-leads-table td:nth-child(1) { display: none !important; }
  .m-leads-table td:nth-child(2) { grid-area: lead; }
  .m-leads-table td:nth-child(3) { grid-area: phone; }
  .m-leads-table td:nth-child(4) { grid-area: status; justify-self: end; overflow: visible; }
  .m-leads-table td:nth-child(5) { grid-area: src; display: flex; gap: 6px; align-items: center; }
  .m-leads-table td:nth-child(5) > div { flex-direction: row !important; align-items: center; gap: 6px !important; }
  .m-leads-table td:nth-child(5) > div > span:last-child { display: none !important; }
  .m-leads-table td:nth-child(6) { display: none !important; }
  .m-leads-table td:nth-child(7) { grid-area: date; justify-self: end; text-align: right; align-self: start; }
  .m-leads-table td:nth-child(7) > div { align-items: flex-end; }
  .m-leads-table td:nth-child(8) { grid-area: menu; justify-self: end; overflow: visible; }
  .m-leads-table td:nth-child(2) .avatar { width: 40px !important; height: 40px !important; font-size: 14px !important; }
  .m-leads-table td:nth-child(2) [title] { font-size: 15px; }
  .m-leads-table td:nth-child(2) div[title]:not(:first-child) { font-size: 12.5px !important; }
  .m-leads-table td:nth-child(3) a[href^="tel:"] { font-size: 14px !important; padding: 6px 0; }
  .m-leads-table td:nth-child(3) a[href*="wa.me"] { padding: 6px 10px !important; border-radius: 999px !important; }

  .m-pay-table tr { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "cust amount" "date status" "meta menu"; }
  .m-pay-table td:nth-child(1) { display: none !important; }
  .m-pay-table td:nth-child(2) { grid-area: date; font-size: 12.5px; color: var(--text-muted); }
  .m-pay-table td:nth-child(3) { grid-area: cust; }
  .m-pay-table td:nth-child(3) > div:first-child { font-size: 15px; font-weight: 600; }
  .m-pay-table td:nth-child(4) { grid-area: amount; justify-self: end; font-size: 17px !important; font-weight: 700; font-variant-numeric: tabular-nums; }
  .m-pay-table td:nth-child(5) { grid-area: status; justify-self: end; overflow: visible; }
  .m-pay-table td:nth-child(6) { grid-area: meta; font-size: 12.5px; color: var(--text-secondary); text-transform: capitalize; }
  .m-pay-table td:nth-child(7) { display: none !important; }
  .m-pay-table td:nth-child(8) { display: none !important; }
  .m-pay-table td:nth-child(9) { grid-area: menu; justify-self: end; overflow: visible; }

  /* every other wide table scrolls sideways inside its card instead of crushing */
  .card:has(> table:not(.m-card-table)), .card > div:has(> table:not(.m-card-table)) { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .card > table:not(.m-card-table), .card > div > table:not(.m-card-table) { min-width: 620px; }

  /* ---------- lead detail ---------- */
  .m-lead-head { flex-wrap: wrap !important; gap: 10px 14px !important; align-items: center !important; }
  .m-lead-head h1 { font-size: 24px !important; line-height: 1.2 !important; }
  .m-lead-head .avatar-lg { width: 52px !important; height: 52px !important; font-size: 18px !important; }
  .lead-detail-grid { gap: 14px !important; grid-template-columns: minmax(0, 1fr) !important; }
  .lead-detail-grid > *, .app-content > *, .page-content > * { min-width: 0; }
  .lead-detail-grid .card { padding: 16px !important; border-radius: 16px !important; }

  /* ---------- pipeline: one column per screen, snap scrolling ---------- */
  .kanban-board { scroll-snap-type: x mandatory; gap: 12px !important; margin: 0 -16px; padding: 0 16px 16px !important; scroll-padding: 16px; scrollbar-width: none; }
  .kanban-board::-webkit-scrollbar { display: none; }
  .kanban-board > div { scroll-snap-align: start; }
  .kanban-column { width: calc(100vw - 52px) !important; max-width: 360px; }

  /* ---------- sheets, toasts, AI panel sit above the tab bar ---------- */
  .modal { padding-bottom: calc(16px + var(--safe-b)) !important; }
  .toast-container { bottom: calc(var(--mnav-h) + var(--safe-b) + 12px) !important; left: 12px !important; right: 12px !important; }
  .ai-panel.open { z-index: 60 !important; }

  /* ---------- bottom tab bar ---------- */
  .mnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; display: grid; grid-template-columns: repeat(5, 1fr);
    height: calc(var(--mnav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: saturate(1.4) blur(16px); -webkit-backdrop-filter: saturate(1.4) blur(16px);
    border-top: 1px solid var(--hairline);
  }
  .mnav-item {
    position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    color: var(--text-muted); font: 500 11px/1.1 inherit; letter-spacing: 0.01em; border: 0; background: none; padding: 0; cursor: pointer;
    transition: color .15s ease;
  }
  .mnav-ico { position: relative; display: grid; place-items: center; width: 56px; height: 30px; border-radius: 999px; transition: background .2s ease, transform .2s ease; }
  .mnav-ico svg { width: 21px; height: 21px; }
  .mnav-item.active { color: var(--accent-text); font-weight: 600; }
  .mnav-item.active .mnav-ico { background: var(--accent-soft); }
  .mnav-item:active .mnav-ico { transform: scale(.92); }
  .mnav-badge {
    position: absolute; top: -1px; left: calc(50% + 5px); min-width: 15px; height: 15px; padding: 0 4px; border-radius: 999px;
    background: var(--accent); color: #fff; font: 700 10px/15px inherit; text-align: center; border: 1.5px solid var(--surface); box-sizing: content-box;
  }

  /* ---------- "More" sheet ---------- */
  .mnav-scrim { position: fixed; inset: 0; z-index: 70; background: rgba(15, 14, 12, .42); opacity: 0; pointer-events: none; transition: opacity .2s ease; }
  .mnav-sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 71; max-height: 86vh; overflow-y: auto;
    background: var(--surface); border-radius: 22px 22px 0 0; box-shadow: var(--shadow-lg);
    padding: 8px 12px calc(16px + var(--safe-b)); transform: translateY(105%); transition: transform .28s cubic-bezier(.2, .8, .2, 1);
  }
  .mnav-sheet.open { transform: none; }
  .mnav-sheet.open + .mnav-scrim, .mnav-scrim.open { opacity: 1; pointer-events: auto; }
  .mnav-grab { width: 38px; height: 4px; border-radius: 4px; background: var(--border-hover); margin: 4px auto 12px; }
  .mnav-user { display: flex; align-items: center; gap: 12px; padding: 6px 8px 14px; border-bottom: 1px solid var(--hairline); margin-bottom: 6px; }
  .mnav-user .avatar { width: 42px; height: 42px; }
  .mnav-user-name { font-weight: 600; color: var(--text-primary); font-size: 15px; }
  .mnav-user-role { color: var(--text-muted); font-size: 12.5px; text-transform: capitalize; }
  .mnav-sheet-list { display: flex; flex-direction: column; }
  .mnav-sheet-list .nav-item {
    display: flex !important; align-items: center; gap: 14px; height: 52px; padding: 0 10px !important; border-radius: 12px;
    color: var(--text-primary) !important; font-size: 15px !important; font-weight: 500; text-decoration: none; justify-content: flex-start !important;
    background: none; border: 0; width: 100%; text-align: left;
  }
  .mnav-sheet-list .nav-item span { display: inline !important; }
  .mnav-sheet-list .nav-item:active { background: var(--surface-hover); }
  .mnav-sheet-list .nav-item svg { width: 20px; height: 20px; color: var(--text-secondary); flex-shrink: 0; }
  .mnav-sheet-list .nav-badge { display: inline-block !important; margin-left: auto; }
  .mnav-sheet-list .mnav-logout { color: var(--color-danger) !important; }
  .mnav-sheet-list .mnav-logout svg { color: var(--color-danger); }
  .mnav-ver { text-align: center; color: var(--text-muted); font-size: 11.5px; padding: 14px 0 2px; font-variant-numeric: tabular-nums; }
  .mnav-sep { height: 1px; background: var(--hairline); margin: 6px 10px; }
}

@media (width > 768px) {
  .mnav, .mnav-sheet, .mnav-scrim { display: none !important; }
}
