/* ================================================================
   CML Dashboard — Mobile Stylesheet
   ----------------------------------------------------------------
   Loaded AFTER each page's inline <style> block to override.
   Breakpoints:
     phone:  max-width 640px  (iPhone portrait primary target)
     tablet: max-width 1024px and min-width 641px (iPad)
     desktop: ≥1025px — untouched, current design wins
   Naming:
     - All new utility classes prefixed m- to avoid collisions
     - Existing class names (.sidebar, .header, .kpi-card, etc.) only
       overridden inside media queries
   ================================================================ */

/* ================================================================
   TABLET (641px – 1024px) — sidebar collapses to icon rail
   ================================================================ */
@media (max-width: 1024px) and (min-width: 641px) {
  :root { --sidebar-w: 64px; }

  .sidebar {
    width: 64px;
    min-width: 64px;
  }
  .sidebar-brand {
    padding: 14px 10px;
    justify-content: center;
  }
  .brand-text { display: none; }

  .sidebar-nav { padding: 10px 6px; }
  .nav-section-label { display: none; }
  .nav-item {
    justify-content: center;
    padding: 10px 0;
    gap: 0;
    font-size: 0;
  }
  .nav-item .nav-icon { width: 18px; height: 18px; opacity: 0.85; }
  .nav-item > span:not(.nav-icon):not(.nav-badge) { display: none; }
  .nav-badge {
    position: absolute;
    top: 4px;
    right: 6px;
    font-size: 9px;
  }

  .sidebar-footer { padding: 10px 6px; justify-content: center; }
  .avatar { margin: 0 auto; }
  .user-name, .user-role { display: none; }

  /* KPI grid — 3 cols on tablet */
  .kpi-row {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* Panels — single column on tablet (was side-by-side) */
  .panel-row,
  .cols-6-4,
  .cols-5-5,
  .cols-6-4b {
    grid-template-columns: 1fr !important;
  }

  /* Header — keep all elements but tighten */
  .header { gap: 10px; }
  .header-title { font-size: 15px; }

  /* Date-range picker — wrap if needed */
  .period-picker { flex-wrap: wrap; }
}

/* ================================================================
   PHONE (≤640px) — drawer + card-stack + fullscreen modals
   ================================================================ */
@media (max-width: 640px) {
  :root { --header-h: 52px; }

  html, body { overflow-x: hidden; }
  body { font-size: 15px; }

  /* ---------- App shell — stop forcing flex viewport ---------- */
  .app {
    display: block;
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

  /* ---------- Sidebar → off-canvas drawer ---------- */
  .sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 282px;
    min-width: 282px;
    height: 100vh;
    transform: translateX(-100%);
    transition: transform 220ms ease;
    z-index: 1000;
    overflow-y: auto;
    box-shadow: 6px 0 24px rgba(0,0,0,0.4);
  }
  body.m-drawer-open .sidebar { transform: translateX(0); }

  /* drawer page-name header (injected by mobile.js) */
  .m-drawer-pagename {
    display: block;
    padding: 14px 18px 12px;
    font-size: 17px;
    font-weight: 700;
    letter-spacing: -0.01em;
    color: var(--text);
    border-bottom: 1px solid var(--border-soft);
    background: linear-gradient(180deg, rgba(242,106,46,0.06), transparent);
  }
  .m-drawer-pagename-sub {
    display: block;
    font-size: 10px;
    font-weight: 500;
    color: var(--text-3);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 2px;
  }

  /* ---------- Hamburger button (injected by mobile.js) ---------- */
  .m-hamburger {
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 8px);
    left: 8px;
    width: 38px; height: 38px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text);
    cursor: pointer;
    z-index: 1001;
    padding: 0;
  }
  .m-hamburger:active { background: var(--surface-3); }
  .m-hamburger svg { width: 18px; height: 18px; }

  /* ---------- Backdrop (injected by mobile.js) ---------- */
  .m-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 220ms ease;
    z-index: 999;
  }
  body.m-drawer-open .m-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* lock scroll when drawer or modal open */
  body.m-drawer-open,
  body.m-modal-open {
    overflow: hidden;
  }

  /* ---------- Main column ---------- */
  .main {
    width: 100vw;
    min-width: 0;
    height: auto;
    overflow: visible;
  }

  /* ---------- Header — sticky to viewport top, respects iPhone notch ---------- */
  .header {
    position: sticky;
    top: 0;
    z-index: 900;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: env(safe-area-inset-top, 0px) 12px 8px 56px; /* top safe area + room for hamburger */
    height: auto;
    min-height: calc(env(safe-area-inset-top, 0px) + 52px);
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
    row-gap: 6px;
  }
  .header-title { font-size: 15px; font-weight: 700; }
  .header-divider, .header-sub { display: none; }
  .header-date { font-size: 11px; }
  .build-marker { display: none; }
  .icon-btn { width: 34px; height: 34px; }
  .header-spacer { display: none; } /* let period-picker fill width on its own row */

  /* ---------- Date range picker — full-width row, horizontal scroll if overflows ---------- */
  .period-picker {
    flex: 1 1 100%;       /* drop onto its own row */
    order: 10;            /* always last in the header */
    display: flex;
    flex-wrap: nowrap;
    gap: 5px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .period-picker::-webkit-scrollbar { display: none; }
  .period-btn {
    padding: 6px 11px;
    font-size: 11px;
    min-height: 32px;
    flex-shrink: 0;       /* don't squash — let row scroll instead */
    white-space: nowrap;
  }

  /* ---------- Content area ---------- */
  .content {
    padding: 14px 12px 80px !important;
    gap: 14px !important;
  }

  /* ---------- KPI grid → 2 cols on phone ---------- */
  .kpi-row {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .kpi-card {
    padding: 12px !important;
    min-height: auto !important;
    border-radius: 10px;
  }
  .kpi-label { font-size: 10px !important; letter-spacing: 0.04em; }
  .kpi-value { font-size: 20px !important; line-height: 1.15 !important; }
  .kpi-delta { font-size: 10px !important; }
  .kpi-age { font-size: 9px !important; }
  .kpi-pin { width: 22px !important; height: 22px !important; }
  .kpi-icon { width: 24px !important; height: 24px !important; }

  /* ---------- Panels → full-width stack ---------- */
  .panel-row,
  .cols-6-4,
  .cols-5-5,
  .cols-6-4b {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  .panel { width: 100%; }
  .panel-head {
    padding: 12px 14px !important;
    flex-wrap: wrap;
    gap: 6px;
  }
  .panel-title { font-size: 12px !important; }
  .panel-body { padding: 12px !important; }
  .panel-legend { flex-wrap: wrap; gap: 8px; }

  /* ---------- SVG charts — responsive width ---------- */
  svg { max-width: 100%; height: auto; }
  .bar-chart-outer,
  .bar-chart-area { width: 100% !important; }

  /* ---------- Drill modal → fullscreen ---------- */
  .kd-overlay,
  .modal-overlay,
  [role="dialog"] { padding: 0 !important; }
  .kd-modal,
  .modal {
    width: 100vw !important;
    height: 100dvh !important;
    max-width: 100vw !important;
    max-height: 100dvh !important;
    border-radius: 0 !important;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .kd-head {
    padding: 14px !important;
    align-items: center;
  }
  .kd-title { font-size: 15px !important; }
  .kd-sub { font-size: 11px !important; }
  .kd-close,
  .modal-close,
  .modal-close-btn,
  [data-modal-close] {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    font-size: 20px !important;
  }
  .kd-body { padding: 0 12px 16px !important; }

  /* ---------- Tables → card stack ---------- */
  table[data-mobile-pattern="native"],
  table[data-mobile-pattern="frozen"] {
    display: table;
  }
  .table-scroll:has(table[data-mobile-pattern="frozen"]),
  .table-scroll.wide,
  .table-scroll--viewport {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  table[data-mobile-pattern="frozen"] th:first-child,
  table[data-mobile-pattern="frozen"] td:first-child,
  .table-scroll.wide table th:first-child,
  .table-scroll.wide table td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--surface-2);
  }

  table.m-cardify {
    display: block;
    width: 100%;
    border-collapse: separate;
  }
  table.m-cardify thead { display: none; }
  table.m-cardify tbody { display: block; }
  table.m-cardify tr {
    display: block;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }
  table.m-cardify td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 10px;
    padding: 4px 0 !important;
    border: none !important;
    text-align: right;
    font-size: 13px;
    color: var(--text);
  }
  table.m-cardify td[data-label]::before {
    content: attr(data-label);
    font-size: 10px;
    color: var(--text-3);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    text-align: left;
    flex-shrink: 0;
    max-width: 45%;
    line-height: 1.4;
    padding-top: 2px;
  }
  /* hide empty cells with no value */
  table.m-cardify td:empty { display: none; }

  /* ---------- Touch targets — minimum 36-44px ---------- */
  button,
  .nav-item,
  .period-btn,
  .icon-btn,
  .kpi-pin {
    min-height: 44px;
  }
  .nav-item { padding: 12px 14px; }

  /* ---------- Sign-in / index pages ---------- */
  /* These pages have no .sidebar, so the hamburger won't inject.
     Just make sure forms behave. */
  input, select, textarea {
    font-size: 16px !important; /* prevents iOS zoom on focus */
    min-height: 40px;
  }

  /* ---------- AI chat panel (cmldash.com chatbot) ---------- */
  /* Common selectors — covers a few likely class names. If the live
     chat uses something else, this is a no-op until we update. */
  .ai-chat-panel,
  .chat-panel,
  #cmlAiChatDrawer,
  #ai-chat,
  .floating-chat {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    border-radius: 16px 16px 0 0 !important;
    z-index: 998;
  }
}

/* ================================================================
   END mobile.css
   ================================================================ */
