/* ═══════════════════════════════════════════════════════════════
   APEX CRM — 05 Responsive & Mobile
   Phase 2: Mobile-first responsive system for all pages
   ═══════════════════════════════════════════════════════════════ */

/* ── Base touch targets ── */
button, .btn, a.btn, .tab, .tier-card, .nav-item, .filter-btn,
input[type="checkbox"], input[type="radio"] {
  min-height: 44px;
  min-width: 44px;
}
input[type="checkbox"], input[type="radio"] {
  width: 20px; height: 20px;
}

/* ── Tablet (≤1024px) ── */
@media (max-width: 1024px) {
  /* KPI rows: 2 columns */
  .kpi-row, .kpi-grid, .stats-row,
  div[style*="grid-template-columns: repeat(4"],
  div[style*="grid-template-columns:repeat(4"],
  div[style*="grid-template-columns: repeat(5"],
  div[style*="grid-template-columns:repeat(5"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Side-by-side layouts → stacked */
  .layout, .split-layout,
  div[style*="grid-template-columns: 360px"],
  div[style*="grid-template-columns:360px"],
  div[style*="grid-template-columns: 1fr 1fr"],
  div[style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* Chart grid: 2 col max */
  .chart-row, .chart-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Tier cards: stay 3-up at tablet */
  .tier-cards {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* Form grids: 2 columns still ok */
  .form-grid {
    grid-template-columns: 1fr 1fr !important;
  }

  /* Reduce page padding */
  .page, .content {
    padding: 16px !important;
  }
}

/* ── Mobile landscape / large phone (≤768px) ── */
@media (max-width: 768px) {
  /* KPI rows: 2 columns, smaller */
  .kpi-row, .kpi-grid, .stats-row {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }
  .kpi, .stat-card {
    padding: 14px !important;
  }
  .kpi .value, .stat-card .value {
    font-size: 22px !important;
  }
  .kpi .label, .stat-card .label {
    font-size: 10px !important;
  }

  /* All multi-column grids → single column */
  .chart-row, .chart-grid, .form-grid,
  .includes-grid, .tier-cards {
    grid-template-columns: 1fr !important;
  }

  /* Proposal side-by-side → stacked */
  .layout {
    grid-template-columns: 1fr !important;
  }

  /* Tables: horizontal scroll */
  .glass, .card, [class*="table-wrap"] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  table {
    min-width: 600px;
  }

  /* Tables → card view for narrow screens */
  .mobile-cards table thead { display: none; }
  .mobile-cards table, .mobile-cards tbody,
  .mobile-cards tr, .mobile-cards td {
    display: block;
    width: 100%;
  }
  .mobile-cards tr {
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 10px;
  }
  .mobile-cards td {
    padding: 4px 0;
    font-size: 13px;
    border: none;
  }
  .mobile-cards td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #94A3B8;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    display: block;
    margin-bottom: 2px;
  }

  /* Header adjustments */
  .header h1, h1 {
    font-size: 22px !important;
  }
  .header p, .sub {
    font-size: 12px !important;
  }

  /* Toolbar: stack vertically */
  .toolbar {
    flex-direction: column !important;
    align-items: stretch !important;
  }
  .toolbar input[type=text] {
    width: 100% !important;
    min-width: unset !important;
  }

  /* Tabs: scrollable */
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap !important;
  }
  .tab {
    white-space: nowrap;
    flex-shrink: 0;
    padding: 10px 14px !important;
    font-size: 12px !important;
  }

  /* Filter buttons: wrap nicely */
  .filter-bar, .filter-row {
    flex-wrap: wrap !important;
    gap: 6px !important;
  }

  /* Kanban / deal board: horizontal scroll */
  .kanban-board, .board-columns,
  div[style*="overflow-x"] {
    -webkit-overflow-scrolling: touch;
  }

  /* Modal: full width */
  .modal {
    width: 95vw !important;
    max-height: 85vh !important;
    padding: 20px !important;
  }

  /* Glass panels: reduce padding */
  .glass {
    padding: 16px !important;
    border-radius: 12px !important;
  }

  /* Proposal preview: scale down */
  .proposal {
    padding: 20px !important;
  }
  .proposal h1 { font-size: 22px !important; }
  .proposal h2 { font-size: 15px !important; }
  .prop-header { padding: 20px !important; }
  .prop-body { padding: 20px !important; }

  /* Page padding */
  .page, .content {
    padding: 12px !important;
  }
}

/* ── Small phone (≤480px) ── */
@media (max-width: 480px) {
  /* KPIs: single column */
  .kpi-row, .kpi-grid, .stats-row {
    grid-template-columns: 1fr !important;
  }

  /* Even smaller text */
  .header h1, h1 {
    font-size: 20px !important;
  }

  /* Tier cards: single column */
  .tier-cards {
    grid-template-columns: 1fr !important;
  }

  /* Reduce chart heights */
  canvas {
    max-height: 250px !important;
  }

  /* Actions column: stack */
  .actions {
    flex-direction: column !important;
    gap: 4px !important;
  }

  /* Buttons: full width */
  .btn:not(.btn-sm) {
    width: 100% !important;
  }
}

/* ── Safe area support (notched phones) ── */
@supports (padding: env(safe-area-inset-bottom)) {
  body {
    padding-bottom: env(safe-area-inset-bottom);
  }
  .header {
    padding-top: env(safe-area-inset-top);
  }
}

/* ── Landscape phone adjustments ── */
@media (max-height: 500px) and (orientation: landscape) {
  .modal {
    max-height: 90vh !important;
  }
  .kpi-row, .kpi-grid {
    grid-template-columns: repeat(4, 1fr) !important;
  }
}

/* ── Print media ── */
@media print {
  .stars, canvas#starfield, .star { display: none !important; }
  body { background: #fff !important; color: #000 !important; }
  .glass, .card { background: #fff !important; border: 1px solid #ddd !important; backdrop-filter: none !important; }
  .btn, .toolbar, .modal-overlay, .toast { display: none !important; }
}

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .star { animation: none !important; }
}

/* ── Dark mode auto-detection (for standalone pages) ── */
@media (prefers-color-scheme: light) {
  .auto-theme {
    --bg: #f5f5f7;
    --text: #1c1c1e;
  }
}

/* ── Bootstrap theme override — Sentinel champagne gold ── */
:root,
[data-bs-theme="dark"],
[data-bs-theme="light"] {
  --bs-theme:          #C8A97E !important;
  --bs-theme-rgb:      200, 169, 126 !important;
  --bs-theme-color:    #050505 !important;
  --bs-theme-color-rgb: 5, 5, 5 !important;
}

/* btn-theme: Bootstrap compiles #249d79 as hardcoded vars — override directly */
.btn-theme {
  --bs-btn-bg:                #C8A97E !important;
  --bs-btn-border-color:      #C8A97E !important;
  --bs-btn-hover-bg:          #B8996E !important;
  --bs-btn-hover-border-color:#B8996E !important;
  --bs-btn-active-bg:         #A8896E !important;
  --bs-btn-active-border-color:#A8896E !important;
  --bs-btn-disabled-bg:       #C8A97E !important;
  --bs-btn-disabled-border-color:#C8A97E !important;
  --bs-btn-color:             #050505 !important;
  --bs-btn-hover-color:       #050505 !important;
  --bs-btn-active-color:      #050505 !important;
  --bs-btn-disabled-color:    #050505 !important;
  background-color: #C8A97E !important;
  border-color:     #C8A97E !important;
  color:            #050505 !important;
}
