/* =============================================================
   CPA System — Mobile-first responsive & UX polish layer.
   Loaded AFTER Tailwind CDN so all `!important` overrides win.
   Breakpoints (mobile-first):
     ≤  640px → phone
     ≤ 1024px → tablet (sidebar collapses to drawer)
     >  1024px → desktop (current sidebar layout)
   ============================================================= */

/* ---------- 1. Global polish & a11y ------------------------- */
:root {
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.12);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* keyboard focus ring (does NOT interfere with mouse clicks) */
:focus-visible {
  outline: 2px solid #4059aa;
  outline-offset: 2px;
  border-radius: 6px;
}

/* Smooth tap targets — mobile guideline ≥ 44×44 */
button, .nav-item, .tab-btn, .bottom-nav-item, [onclick] { -webkit-tap-highlight-color: transparent; }

/* Prevent iOS auto-zoom on inputs (font-size ≥16px) */
@media (max-width: 640px) {
  .form-input, .form-select, input, select, textarea { font-size: 16px !important; }
}

/* Slightly softer card shadow + hover lift only where hover exists */
.card { box-shadow: var(--shadow-sm) !important; transition: box-shadow .2s ease, transform .2s ease; }
@media (hover: hover) {
  .card:hover { box-shadow: var(--shadow-md) !important; }
}

/* Buttons: bigger touch target on small screens */
@media (max-width: 640px) {
  .btn-primary, .btn-outline { padding: 11px 18px !important; min-height: 44px; }
}

/* ---------- 2. Mobile shell scaffolding --------------------- */
.mobile-only   { display: none !important; }
.desktop-only  { display: inherit; }

@media (max-width: 1024px) {
  .mobile-only  { display: flex !important; }
  .desktop-only { display: none !important; }
}

/* Hamburger / icon-button base */
.icon-btn {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; cursor: pointer; color: #444651;
  background: transparent; border: none;
  transition: background .15s ease;
}
.icon-btn:hover  { background: #eeedf4; }
.icon-btn:active { background: #dce1ff; }

/* ---------- 3. Sidebar → off-canvas drawer ------------------ */
@media (min-width: 1025px) {
  .sidebar {
    width: 232px !important;
    height: 100vh;
    min-height: 0 !important;
  }
  .main-content { margin-right: 232px !important; }
  .sidebar > .px-6 {
    padding: 14px 18px !important;
  }
  .sidebar > .px-6 .w-10 {
    width: 34px !important;
    height: 34px !important;
    border-radius: 10px !important;
  }
  .sidebar nav {
    padding-top: 10px !important;
    padding-bottom: 10px !important;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.28) transparent;
  }
  .sidebar nav::-webkit-scrollbar { width: 6px; }
  .sidebar nav::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.28);
    border-radius: 999px;
  }
  .sidebar-section-title {
    padding: 6px 18px 3px !important;
  }
  .nav-item {
    margin: 1px 8px !important;
    padding: 8px 12px !important;
    gap: 8px !important;
    font-size: 13px !important;
    border-radius: 9px !important;
  }
  .nav-item .material-symbols-outlined { font-size: 18px !important; }
  .nav-item span:not(.material-symbols-outlined) {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .sidebar > .px-4 {
    padding: 10px 12px !important;
  }
  .sidebar > .px-4 .avatar {
    width: 32px !important;
    height: 32px !important;
  }
}

@media (min-width: 1025px) and (max-height: 760px) {
  .sidebar > .px-6 {
    padding-top: 10px !important;
    padding-bottom: 10px !important;
  }
  .sidebar-section-title {
    font-size: 9px !important;
    padding-top: 4px !important;
  }
  .nav-item {
    padding-top: 6px !important;
    padding-bottom: 6px !important;
    font-size: 12.5px !important;
  }
  .nav-item .material-symbols-outlined { font-size: 17px !important; }
  .sidebar > .px-4 {
    padding-top: 8px !important;
    padding-bottom: 8px !important;
  }
}

@media (max-width: 1024px) {
  .sidebar {
    width: 280px !important;
    transform: translateX(100%);                /* RTL: push off-screen to the right */
    transition: transform .28s cubic-bezier(.4,.0,.2,1);
    box-shadow: -8px 0 24px rgba(0,0,0,0.12);
  }
  .sidebar.is-open { transform: translateX(0); }
  .main-content    { margin-right: 0 !important; }
}

#sidebarBackdrop {
  position: fixed; inset: 0;
  background: rgba(15, 17, 32, 0.5);
  backdrop-filter: blur(2px);
  z-index: 49;
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease;
}
#sidebarBackdrop.is-open { opacity: 1; pointer-events: auto; }

/* Sidebar close (X) button — visible only on mobile */
.sidebar-close {
  position: absolute; left: 12px; top: 12px;
  width: 36px; height: 36px;
  display: none; align-items: center; justify-content: center;
  border-radius: 10px; cursor: pointer;
  background: rgba(255,255,255,0.1); color: white; border: none;
}
.sidebar-close:hover { background: rgba(255,255,255,0.2); }
@media (max-width: 1024px) { .sidebar-close { display: inline-flex; } }

/* ---------- 4. Topbar refinements --------------------------- */
.topbar { padding: 0 16px !important; }
@media (min-width: 1024px) { .topbar { padding: 0 28px !important; } }

@media (max-width: 1024px) {
  /* Replace the wide search-box with an icon button on tablet/phone */
  .topbar .search-box {
    width: auto !important; padding: 0 !important; background: transparent !important;
  }
  .topbar .search-box input { display: none; }
  .topbar .search-box span.material-symbols-outlined {
    width: 40px; height: 40px;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 10px; background: #f4f3fa;
  }
  /* Hide the verbose "Admin" label, keep avatar */
  .topbar [class*="text-sm"][class*="font-bold"] { display: none; }
}

/* Mobile brand block (sits next to hamburger) */
.brand-mobile {
  display: none; align-items: center; gap: 8px;
}
.brand-mobile .brand-logo {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, #00236f 0%, #4059aa 100%);
  display: inline-flex; align-items: center; justify-content: center; color: white;
}
.brand-mobile .brand-text {
  font-weight: 800; font-size: 14px; color: #1a1b21; line-height: 1.1;
}
@media (max-width: 1024px) { .brand-mobile { display: inline-flex; } }
@media (max-width: 480px)  { .brand-mobile .brand-text { display: none; } }

/* ---------- 5. Page padding scales with screen -------------- */
@media (max-width: 1024px) {
  .page.p-7, [id^="page-"].p-7 { padding: 18px 16px !important; }
}
@media (max-width: 640px) {
  .page.p-7, [id^="page-"].p-7 { padding: 14px 12px !important; }
  /* H1s shrink on phones */
  .page h1.text-2xl { font-size: 1.25rem !important; }
}

/* Reserve space for the bottom nav on mobile */
@media (max-width: 1024px) {
  .main-content { padding-bottom: calc(72px + var(--safe-bottom)) !important; }
}

/* ---------- 6. Grid collapses ------------------------------- */
/* Tailwind classes are real CSS — override them via media queries */
@media (max-width: 1024px) {
  .grid.grid-cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .grid.grid-cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
}
@media (max-width: 640px) {
  .grid.grid-cols-4,
  .grid.grid-cols-3,
  .grid.grid-cols-2 { grid-template-columns: minmax(0, 1fr) !important; }
  /* Documents page sidebar + main : stack */
  .grid.grid-cols-4 > .col-span-3 { grid-column: auto !important; }
}

/* Kanban board scrolls horizontally instead of stacking — keeps the metaphor */
@media (max-width: 1024px) {
  #taskKanban {
    grid-template-columns: repeat(5, minmax(280px, 1fr)) !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 8px;
    scroll-snap-type: x mandatory;
  }
  #taskKanban > * { scroll-snap-align: start; }
}

/* ---------- 7. Tables become horizontally scrollable ------- */
@media (max-width: 1024px) {
  /* Make any .card that wraps a full-width table scroll its overflow */
  .card.overflow-hidden { overflow-x: auto !important; }
  /* Keep nominal min-width so columns don't squish into illegibility */
  .card > table, .card > .overflow-hidden > table { min-width: 720px; }
}

/* ---------- 8. Modals: full-screen on small phones --------- */
@media (max-width: 640px) {
  .modal-overlay { padding: 0 !important; align-items: stretch !important; }
  .modal {
    max-width: 100% !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
    padding: 18px !important;
  }
  /* Make the close-button row sticky so it remains reachable while scrolling */
  .modal > .flex.items-center.justify-between:first-child {
    position: sticky; top: -18px; background: white;
    margin: -18px -18px 12px; padding: 14px 18px;
    border-bottom: 1px solid #eeedf4; z-index: 5;
  }
}

/* Compact form gaps on mobile */
@media (max-width: 640px) {
  .form-group { margin-bottom: 12px; }
  .modal .grid.grid-cols-2 { grid-template-columns: minmax(0, 1fr) !important; }
}

/* ---------- 9. Bottom navigation (mobile) ------------------ */
.bottom-nav {
  display: none;
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(64px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: rgba(255,255,255,0.92);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid #eeedf4;
  z-index: 45;
  box-shadow: 0 -4px 16px rgba(16, 24, 40, 0.06);
}
.bottom-nav-inner {
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: 64px; max-width: 640px; margin: 0 auto;
}
.bottom-nav-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; cursor: pointer; color: #757682;
  font-size: 11px; font-weight: 600;
  border: none; background: transparent; font-family: inherit;
  position: relative;
  transition: color .15s ease;
}
.bottom-nav-item .material-symbols-outlined {
  font-size: 24px;
  font-variation-settings: 'FILL' 0, 'wght' 400, 'GRAD' 0, 'opsz' 24;
  transition: transform .15s ease;
}
.bottom-nav-item.active {
  color: #00236f;
}
.bottom-nav-item.active .material-symbols-outlined {
  font-variation-settings: 'FILL' 1, 'wght' 500, 'GRAD' 0, 'opsz' 24;
  transform: translateY(-1px);
}
.bottom-nav-item.active::before {
  content: '';
  position: absolute; top: 0; left: 50%; transform: translateX(-50%);
  width: 32px; height: 3px; border-radius: 0 0 3px 3px;
  background: #00236f;
}

@media (max-width: 1024px) { .bottom-nav { display: block; } }

/* ---------- 10. Toast: respect bottom-nav on mobile -------- */
@media (max-width: 1024px) {
  #toast {
    bottom: calc(80px + var(--safe-bottom)) !important;
    right: 12px !important; left: 12px !important;
    max-width: none !important;
  }
}

/* ---------- 11. Sidebar nav: bigger touch targets --------- */
@media (max-width: 1024px) {
  .nav-item { padding: 12px 16px !important; font-size: 15px !important; min-height: 48px; }
  .nav-item .material-symbols-outlined { font-size: 22px !important; }
}

/* ---------- 12. Documents page: stack sidebar on mobile ---- */
@media (max-width: 640px) {
  #page-documents aside.sticky { position: static !important; }
  #docsCategoryList {
    display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px;
  }
}

/* ---------- 13. Calendar grid sizing ----------------------- */
@media (max-width: 640px) {
  .cal-day { font-size: 12px; }
}

/* ---------- 14. Login page (mobile) ------------------------ */
@media (max-width: 640px) {
  .login-shell, .login-card { width: 100% !important; max-width: 100% !important; border-radius: 0 !important; }
}

/* ---------- 15. Reduce-motion respect ---------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ---------- 17. Client detail page ------------------------- */
.cd-tab {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 14px 18px;
  font-size: 14px; font-weight: 600;
  color: #757682; background: transparent;
  border: none; cursor: pointer;
  white-space: nowrap;
  border-bottom: 3px solid transparent;
  font-family: inherit;
  transition: color .15s ease, border-color .15s ease;
}
.cd-tab:hover  { color: #1a1b21; }
.cd-tab.active { color: #00236f; border-bottom-color: #00236f; }
.cd-tab .material-symbols-outlined { font-size: 18px; }
.cd-tab.active .material-symbols-outlined { font-variation-settings: 'FILL' 1, 'wght' 500, 'GRAD' 0, 'opsz' 24; }

.cd-tab-count {
  display: none;
  font-size: 11px; font-weight: 700;
  background: #eeedf4; color: #444651;
  padding: 1px 7px; border-radius: 10px;
}
.cd-tab-count.has { display: inline-flex; }
.cd-tab.active .cd-tab-count.has { background: #dce1ff; color: #00236f; }

.cd-stat {
  background: white; border-radius: 12px; padding: 16px;
  box-shadow: var(--shadow-sm);
  display: flex; flex-direction: column; gap: 4px;
  border-right: 3px solid transparent;
}
.cd-stat .label  { font-size: 11px; color: #757682; font-weight: 600; }
.cd-stat .value  { font-size: 22px; font-weight: 800; color: #1a1b21; line-height: 1.2; }
.cd-stat .meta   { font-size: 11px; color: #757682; }
.cd-stat.danger  { border-right-color: #ba1a1a; }
.cd-stat.warn    { border-right-color: #f59e0b; }
.cd-stat.ok      { border-right-color: #1a6b3c; }
.cd-stat.primary { border-right-color: #00236f; }

@media (max-width: 640px) {
  .cd-stat .value { font-size: 18px; }
  .cd-tab { padding: 12px 14px; font-size: 13px; }
}

/* Stage chip strip on overview */
.cd-stage-chip {
  display: inline-flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px 14px; border-radius: 10px;
  background: #f4f3fa; min-width: 110px;
}
.cd-stage-chip .n { font-size: 18px; font-weight: 800; color: #1a1b21; }
.cd-stage-chip .l { font-size: 11px; color: #757682; }

/* Timeline */
.cd-timeline-item {
  display: flex; gap: 12px; padding: 10px 0;
  border-bottom: 1px solid #f4f3fa;
}
.cd-timeline-item:last-child { border-bottom: none; }
.cd-timeline-icon {
  width: 36px; height: 36px; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.cd-health-icon,
.cd-signal-icon {
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.cd-health-icon { width: 40px; height: 40px; border-radius: 12px; }
.cd-signal {
  display: flex; align-items: center; gap: 8px;
  padding: 9px; border-radius: 10px;
  background: #faf8ff;
}
.cd-signal-icon { width: 30px; height: 30px; border-radius: 9px; }
.cd-signal-action {
  margin-inline-start: auto;
  font-size: 11px; font-weight: 800;
  color: var(--primary); background: white;
  border: 1px solid #e6e0f8;
  border-radius: 999px; padding: 3px 8px;
  flex-shrink: 0;
}
.cd-signal-action:hover { background: #f4f0ff; }
.cd-health-track,
.cd-aging-track {
  height: 7px; border-radius: 999px; overflow: hidden;
  background: #eeedf4;
}
.cd-health-track span,
.cd-aging-track span {
  display: block; height: 100%; border-radius: inherit;
}
.cd-aging-row {
  padding: 8px; border-radius: 10px;
  border: 1px solid transparent;
}
.cd-aging-row.is-worst {
  background: #fff7ed;
  border-color: #fed7aa;
}

@media (max-width: 1024px) {
  #page-client-detail .col-span-2 { grid-column: 1 / -1 !important; }
}
@media (max-width: 640px) {
  #page-client-detail .cd-insight-grid,
  #page-client-detail .cd-signal { grid-template-columns: minmax(0, 1fr) !important; }
  #page-client-detail .grid.grid-cols-3,
  #page-client-detail .grid.grid-cols-2 { grid-template-columns: minmax(0, 1fr) !important; }
}

/* ---------- 18. Print: hide chrome ------------------------- */
@media print {
  .sidebar, .topbar, .bottom-nav, #sidebarBackdrop, #toast { display: none !important; }
  .main-content { margin: 0 !important; padding: 0 !important; }
}
