/* ===========================================================
   mobile.css — responsive overrides (loaded LAST so it wins).
   Owned by the mobile pass. Uses ONLY existing design tokens
   (--bg, --fg, --line, --accent, --hover, --radius, etc.) so it
   works in both light and dark themes. No new colors introduced.
   =========================================================== */

/* ---- Global: never let anything cause horizontal page scroll,
        and honor notch safe-areas. ------------------------------- */
@media (max-width: 768px) {
  html, body {
    max-width: 100%;
    overflow-x: hidden;
  }
  /* iOS dynamic viewport so the app-shell never gets cut by the
     browser chrome / address bar. */
  .app-shell {
    height: 100dvh;
  }
}

/* ===========================================================
   PHONE LAYOUT  (<= 640px)
   =========================================================== */
@media (max-width: 640px) {

  /* ---------- Topbar ---------- */
  .topbar {
    height: auto;
    min-height: var(--topbar-h);
    padding: 8px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-top: max(8px, env(safe-area-inset-top));
    flex-wrap: wrap;
    gap: 8px;
    row-gap: 8px;
  }
  .topbar-left {
    gap: 10px;
    flex: 1 1 auto;
    min-width: 0;
  }
  .topbar-right {
    gap: 8px;
    flex: 0 0 auto;
  }

  .brand {
    font-size: 15px;
    flex-shrink: 0;
  }
  .brand-text small { display: none; }

  /* Sync status / source pill: keep but compact, allow them to drop. */
  .sync-status { display: none; }
  .source-pill { display: none; }

  /* View tabs become a full-width horizontally scrollable strip on
     its own row beneath the brand/user row. */
  .view-tabs {
    order: 3;
    flex: 1 0 100%;
    width: 100%;
    gap: 4px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
    margin: 0 -12px;
    padding-left: 12px;
    padding-right: 12px;
    scroll-snap-type: x proximity;
  }
  .view-tabs::-webkit-scrollbar { display: none; }
  .view-tab {
    flex: 1 0 auto;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 14px;
    font-size: 13px;
    white-space: nowrap;
    border-radius: var(--radius);
    scroll-snap-align: start;
  }

  /* User menu: avatar-only button, but ≥40px tap target. */
  .user-name { display: none; }
  .caret { display: none; }
  .user-btn {
    padding: 4px;
    min-height: 40px;
    min-width: 40px;
    justify-content: center;
  }
  .user-avatar { width: 32px; height: 32px; font-size: 13px; }
  /* Dropdown should not overflow the viewport. */
  .user-dropdown {
    position: fixed;
    top: auto;
    right: 8px;
    left: 8px;
    min-width: 0;
    width: auto;
    max-width: calc(100vw - 16px);
  }

  /* ---------- View toolbars ---------- */
  .view-toolbar {
    padding: 10px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    gap: 8px;
  }
  .view-body {
    padding: 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .view-body-kanban {
    padding: 10px 0 10px 12px;   /* left pad only; columns peek on right */
    padding-left: max(12px, env(safe-area-inset-left));
  }

  /* Toolbar row: search on its own line, actions full width below. */
  .toolbar-row {
    gap: 8px;
  }
  .search-wrap {
    flex: 1 1 100%;
    max-width: none;
  }
  .toolbar-actions {
    margin-left: 0;
    width: 100%;
    flex-wrap: wrap;
  }
  .toolbar-actions .btn {
    flex: 1 1 auto;
    justify-content: center;
    min-height: 42px;
  }

  .view-heading { font-size: 15px; }

  /* ≥16px inputs to stop iOS zoom-on-focus; comfortable tap height. */
  .search-input {
    font-size: 16px;
    padding: 11px 36px 11px 12px;
  }
  .search-kbd { display: none; }

  /* ---------- Stage filter pills (horizontal scroll) ---------- */
  .stage-tabs-scroll {
    margin: 0 -12px;
    padding: 0 12px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .stage-tabs-scroll::-webkit-scrollbar { display: none; }
  .stage-tab {
    min-height: 36px;
    padding: 7px 13px;
    font-size: 13px;
  }

  /* ---------- Kanban: one column at a time, snap scroll ---------- */
  .kanban {
    grid-auto-columns: 85vw;
    gap: 10px;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    padding-right: 12px;
    padding-bottom: 12px;
  }
  .kan-col {
    scroll-snap-align: center;
    min-height: 60vh;
  }
  .kan-col-header { padding: 12px 14px; }
  .kan-card {
    padding: 12px 14px;
    /* tap, don't drag, on touch — bigger hit area */
  }
  .kan-card-name { font-size: 14px; }
  .kan-card-meta { font-size: 12px; }

  /* ---------- Calendar: fit 7 columns, compact cells ---------- */
  .cal-nav {
    flex: 1 1 100%;
    justify-content: center;
    gap: 4px;
  }
  .cal-nav .btn { min-height: 40px; min-width: 40px; justify-content: center; }
  .cal-title {
    font-size: 15px;
    min-width: 0;
    flex: 1 1 auto;
    margin: 0 2px;
  }
  .cal-legend {
    flex: 1 1 100%;
    margin-left: 0;
    justify-content: center;
    font-size: 11px;
  }

  .cal-weekdays > div {
    padding: 6px 2px;
    font-size: 9px;
    text-align: center;
    letter-spacing: 0.02em;
    overflow: hidden;
  }
  .cal-grid { gap: 1px; }
  .cal-day {
    min-height: 58px;
    padding: 3px;
    gap: 2px;
  }
  .cal-day-num { font-size: 11px; }
  .cal-day-today-pill {
    /* shrink to a dot-like badge to save horizontal room */
    font-size: 0;
    padding: 0;
    width: 7px;
    height: 7px;
    border-radius: 50%;
  }
  .cal-appts { gap: 2px; }
  .cal-appt {
    padding: 2px 3px;
    font-size: 9px;
    border-left-width: 2px;
    /* clip to cell so the time label never spills past the day border */
    max-width: 100%;
    overflow: hidden;
  }
  /* keep the time label visible (name is hidden by main calendar.css
     rule at <=768px); show a compact dot+time so the day reads. */
  .cal-appt-time {
    font-size: 9px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .cal-day-add {
    /* always show add affordance on touch (no hover) on visit days */
    width: 22px; height: 22px; font-size: 16px;
  }
  .cal-day.visit-day .cal-day-add { display: inline-flex; }
  .cal-more { font-size: 9px; padding: 1px 3px; }

  /* ---------- Dashboard / reports ---------- */
  .dashboard { gap: 14px; }
  .kpi-row {
    /* 2-up KPI cards */
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
  .kpi-card { padding: 14px; }
  .kpi-value { font-size: 24px; }
  .kpi-label { font-size: 11px; }

  .dash-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  .dash-panel.span-2 { grid-column: span 1; }
  .funnel-row { grid-template-columns: 8px 92px 1fr 22px; gap: 6px; }
  .funnel-name { font-size: 11px; }
  .hbar-row { grid-template-columns: 84px 1fr 22px; gap: 6px; }
  .chart-svg { height: 100px; }

  /* ---------- Tasks (global view) ---------- */
  /* tasks.css already collapses the table into stacked blocks at
     <=640px; just give the rows breathing room and full width. */
  .tasks-view-toolbar { gap: 8px; }
  /* segmented filter strip can be wider than the phone — let it scroll
     horizontally instead of clipping the last option. */
  .tasks-filters {
    flex: 1 1 100%;
    justify-content: flex-start;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .tasks-filters::-webkit-scrollbar { display: none; }
  .tasks-filter-btn { flex: 0 0 auto; white-space: nowrap; }
  .tasks-filter-btn { min-height: 34px; padding: 6px 14px; font-size: 13px; }
  .tasks-prio-filter { font-size: 16px; min-height: 38px; }
  .tasks-table-wrap { border-radius: var(--radius); }
  .tasks-table tr { padding: 12px 14px; }
  .tasks-table td.col-actions .btn { min-height: 38px; padding: 7px 12px; }
  .tasks-empty-view { padding: 40px 16px; }

  /* tasks inside lead detail */
  .task-row {
    grid-template-columns: 24px 1fr auto;
    grid-auto-rows: auto;
    row-gap: 6px;
    padding: 12px;
  }
  .task-actions .btn { min-height: 36px; padding: 6px 10px; }

  /* ---------- Modals: full-screen bottom sheet ---------- */
  .modal {
    align-items: flex-end;
  }
  .modal-content,
  .modal-content.modal-sm,
  .modal-content.modal-lg {
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
    animation: sheet-up 0.22s ease-out;
  }
  .modal-head {
    padding: 16px;
    padding-top: max(16px, env(safe-area-inset-top));
  }
  .modal-body {
    padding: 16px;
    -webkit-overflow-scrolling: touch;
  }
  .modal-foot {
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .modal-foot .btn {
    flex: 1 1 auto;
    justify-content: center;
    min-height: 46px;
  }
  @keyframes sheet-up {
    from { opacity: 0; transform: translateY(24px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  /* ---------- Slide-overs: full-screen panel ---------- */
  .slide-over-content,
  .slide-over.slide-over-sm .slide-over-content {
    width: 100%;
    max-width: 100%;
    animation: slide-up-full 0.22s ease-out;
  }
  .slide-over-head {
    padding: 14px 16px;
    padding-top: max(14px, env(safe-area-inset-top));
  }
  .slide-over-title { font-size: 17px; }
  .slide-over-body {
    padding: 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    -webkit-overflow-scrolling: touch;
  }
  .slide-over-actions .btn { min-height: 40px; }
  @keyframes slide-up-full {
    from { transform: translateY(16px); opacity: 0.4; }
    to   { transform: translateY(0); opacity: 1; }
  }

  /* ---------- Forms: single column, ≥16px inputs ---------- */
  .form-grid { grid-template-columns: 1fr; gap: 14px; }
  .lead-detail-row { grid-template-columns: 1fr; gap: 2px; }
  .form-row input,
  .form-row select,
  .form-row textarea {
    font-size: 16px;        /* prevent iOS zoom-on-focus */
    padding: 11px 12px;
  }
  .form-row label { font-size: 13px; }
  .prio-radio-group { flex-wrap: wrap; }
  .prio-radio-group label { min-height: 40px; }

  /* appointment detail rows stack */
  .appt-detail-row { grid-template-columns: 1fr; gap: 2px; }
  .appt-actions { flex-wrap: wrap; }
  .appt-actions .btn { flex: 1 1 auto; justify-content: center; min-height: 44px; }

  /* ---------- Toasts: full-width at top, clear of notch ---------- */
  .toast-container {
    top: max(12px, env(safe-area-inset-top));
    left: 8px;
    right: 8px;
  }
  .toast { max-width: none; }

  /* ---------- Generic button tap targets in panels ---------- */
  .lead-detail-section .btn,
  .slide-over-body .btn,
  .tasks-toolbar .btn { min-height: 38px; }
}

/* ===========================================================
   VERY NARROW  (<= 380px) — extra squeeze for small phones
   =========================================================== */
@media (max-width: 380px) {
  .view-tab { padding: 8px 10px; font-size: 12px; }
  .cal-day { min-height: 52px; }
  .cal-weekdays > div { font-size: 8px; }
  .kpi-value { font-size: 21px; }
  .funnel-row { grid-template-columns: 8px 78px 1fr 20px; }
  .hbar-row { grid-template-columns: 72px 1fr 20px; }
}
