/* ============================================================
   LightCRM global styles — MOBILE FIRST.
   Base rules target phones; @media (min-width: 992px) adds the
   desktop layout (sidebar). Module-specific styles live in
   css/modules/<name>.css — do not pile them up here.
   ============================================================ */

:root {
  --lc-dark: #1e2530;
  --lc-border: #e9ecf1;
  --lc-muted: #8a94a6;
  --lc-topbar-h: 52px;
  --lc-bottomnav-h: 60px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Layout shell ---------- */
html {
  height: 100%;
}

body {
  background: #f5f6fa;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

.app-wrapper {
  display: flex;
  min-height: 100dvh;
}

.app-content {
  flex: 1 1 auto;
  min-width: 0;
  padding: 12px;
  /* keep content clear of the fixed top bar + bottom tab bar */
  padding-top: calc(var(--lc-topbar-h) + var(--safe-top) + 12px);
  padding-bottom: calc(var(--lc-bottomnav-h) + var(--safe-bottom) + 16px);
}

/* ---------- Mobile top bar ---------- */
.app-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1030;
  height: calc(var(--lc-topbar-h) + var(--safe-top));
  padding: var(--safe-top) 16px 0;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--lc-dark);
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
}

/* ---------- Mobile bottom tab bar ---------- */
.app-bottomnav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1030;
  height: calc(var(--lc-bottomnav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  background: #fff;
  border-top: 1px solid var(--lc-border);
  box-shadow: 0 -2px 12px rgba(30, 37, 48, 0.06);
}

.app-bottomnav .tab-link {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 2px;
  color: var(--lc-muted);
  text-decoration: none;
  font-size: 0.66rem;
  line-height: 1.1;
  border: 0;
  background: none;
}

.app-bottomnav .tab-link i {
  font-size: 1.25rem;
  line-height: 1;
}

.app-bottomnav .tab-link.active {
  color: #0d6efd;
  font-weight: 600;
}

.app-bottomnav .tab-label {
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Mobile "More" sheet ---------- */
.app-more-sheet {
  --bs-offcanvas-height: auto;
  max-height: 85dvh;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
}

.app-more-sheet .offcanvas-body {
  padding-bottom: calc(var(--safe-bottom) + 16px);
}

.more-menu {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.more-menu .more-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 4px;
  border-radius: 12px;
  background: #f2f4f8;
  color: #3c4555;
  text-decoration: none;
  font-size: 0.72rem;
  text-align: center;
  line-height: 1.15;
}

.more-menu .more-link i {
  font-size: 1.35rem;
  line-height: 1;
}

.more-menu .more-link.active {
  background: #e7f0ff;
  color: #0d6efd;
  font-weight: 600;
}

.more-user-box {
  border-top: 1px solid var(--lc-border);
  padding-top: 10px;
}

.more-user-box .user-box {
  border: 0;
  padding: 8px 0;
}

.more-user-box .user-box-name {
  color: #1e2530;
}

.more-user-box .btn-outline-light {
  /* the sidebar logout button style is for a dark background — flip it here */
  color: #5d6778;
  border-color: var(--lc-border) !important;
}

/* ---------- Desktop sidebar ---------- */
/* Width is a variable: drag the right edge to resize (js/core/sidebar.js),
   or collapse to a 64px icon rail with the « button in the brand row. */
.app-sidebar {
  width: var(--lc-sidebar-w, 230px);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100dvh;
  flex-direction: column;
  background: var(--lc-dark);
  transition: width 0.15s ease;
}

.app-sidebar.dragging {
  transition: none; /* live resize must follow the mouse instantly */
}

body.sidebar-dragging {
  user-select: none;
  cursor: col-resize;
}

/* Drag handle on the sidebar's right edge */
.sidebar-resizer {
  position: absolute;
  top: 0;
  right: -3px;
  width: 7px;
  height: 100%;
  cursor: col-resize;
  z-index: 6;
}

.sidebar-resizer:hover,
.app-sidebar.dragging .sidebar-resizer {
  background: rgba(255, 193, 7, 0.35);
}

/* Collapse/expand button in the brand row */
.sidebar-toggle {
  background: none;
  border: 0;
  padding: 2px 5px;
  border-radius: 6px;
  color: #aab4c2;
  line-height: 1;
}

.sidebar-toggle:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
}

/* ----- Collapsed: 64px icon rail (labels, user info & language hidden) ----- */
body.sidebar-mini .app-sidebar {
  width: 64px;
}

body.sidebar-mini .app-sidebar .menu-label,
body.sidebar-mini .app-sidebar .conn-dot,
body.sidebar-mini .app-sidebar .user-box-info,
body.sidebar-mini .app-sidebar .sidebar-footer,
body.sidebar-mini .app-sidebar .sidebar-resizer {
  display: none;
}

body.sidebar-mini .sidebar-brand {
  flex-direction: column;
  gap: 12px;
  padding: 14px 8px;
}

body.sidebar-mini .sidebar-toggle {
  margin: 0 !important;
}

body.sidebar-mini .app-sidebar .nav-link {
  justify-content: center;
  padding: 12px 0;
}

body.sidebar-mini .app-sidebar .nav-link i {
  margin-right: 0 !important;
  font-size: 1.15rem;
}

body.sidebar-mini .user-box {
  justify-content: center;
  padding: 10px 4px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px;
  font-size: 1.15rem;
  font-weight: 700;
  color: #fff;
}

/* The MENU LIST is the only scrolling region of the sidebar —
   the brand (top), user box and language selector (bottom) stay
   visible at any window height / zoom level. */
#sidebar-menu {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  /* Bootstrap .nav wraps by default — with a constrained height that
     would break the menu into columns. Keep one column + no sideways scroll. */
  flex-wrap: nowrap;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: #3a4456 transparent;
}

.app-sidebar .nav-link {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  color: #aab4c2;
  border-left: 3px solid transparent;
}

.app-sidebar .nav-link:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.05);
}

.app-sidebar .nav-link.active {
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
  border-left-color: #ffc107;
}

/* Signed-in user box pinned above the language selector */
#user-box {
  margin-top: auto;
}

.user-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.user-box-info {
  flex: 1;
  min-width: 0;
}

.user-box-name {
  color: #fff;
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-box-role {
  color: var(--lc-muted);
  font-size: 0.75rem;
}

/* Language selector at the sidebar bottom */
.sidebar-footer {
  padding: 12px 16px;
}

.sidebar-footer .form-select {
  background-color: #2a3342;
  border-color: #3a4456;
  color: #cfd6e0;
}

/* ---------- Login screen ---------- */
/* Until the session is known (and when signed out) all chrome is hidden */
body.auth-locked .app-sidebar,
body.auth-locked .app-topbar,
body.auth-locked .app-bottomnav {
  display: none !important;
}

body.auth-locked .app-content {
  padding-top: 12px;
  padding-bottom: 12px;
}

.login-wrap {
  flex: 1;
  min-height: calc(100dvh - 48px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  width: 100%;
  max-width: 380px;
  border: 0;
  box-shadow: 0 10px 30px rgba(30, 37, 48, 0.12);
}

/* Socket connection status dot (green = online, red = offline) */
.conn-dot {
  font-size: 0.55rem;
}

/* ---------- Toasts: full-width banners under the top bar on phones ---------- */
#toast-area {
  top: calc(var(--lc-topbar-h) + var(--safe-top));
  left: 0;
  right: 0;
  align-items: center;
}

#toast-area .toast {
  width: calc(100vw - 24px);
  max-width: 420px;
}

/* ---------- Shared utilities ---------- */
.cursor-pointer {
  cursor: pointer;
}

.min-width-0 {
  min-width: 0;
}

/* Searchable contact picker (App.ui.contactPicker) */
.cpicker {
  position: relative;
}

.cpicker .cp-menu {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 1060; /* above the shared modal */
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid var(--lc-border);
  border-radius: 0 0 8px 8px;
  box-shadow: 0 10px 24px rgba(30, 37, 48, 0.15);
}

.cpicker .cp-menu.show {
  display: block;
}

/* Shared rich text editor (App.ui.richEditor — Quill) */
.rich-editor .ql-container {
  min-height: 180px;
  font-size: 0.95rem;
  border-radius: 0 0 8px 8px;
}

/* The contenteditable must FILL the container — otherwise clicking
   the (mostly empty) lower area hits .ql-container dead space and
   the editor never receives focus ("can't click to type" bug). */
.rich-editor .ql-editor {
  min-height: 180px;
  cursor: text;
}

.rich-editor .ql-toolbar {
  border-radius: 8px 8px 0 0;
}

.rich-editor .ql-toolbar,
.rich-editor .ql-container {
  border-color: var(--lc-border);
}

/* Contact 360° slide-over panel (contacts module) */
.contact-detail {
  width: min(720px, 100%);
}

.contact-detail .cd-item {
  border-bottom: 1px solid var(--lc-border);
  padding: 8px 0;
}

.contact-detail .cd-item:last-child {
  border-bottom: 0;
}

/* Shared by the Dashboard & Reports modules:
   fixed-height chart wrapper + KPI headline cards */
.chart-box {
  position: relative;
  height: 240px;
}

.kpi-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: #fff;
  border: 1px solid var(--lc-border);
  border-radius: 10px;
  height: 100%;
}

.kpi-icon {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-size: 1.2rem;
}

.kpi-meta {
  min-width: 0;
}

.kpi-label {
  font-size: 0.78rem;
  color: var(--lc-muted);
}

.kpi-value {
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.2;
  word-break: break-word;
}

.kpi-hint {
  font-size: 0.72rem;
  color: #aab4c2;
}

.table td,
.table th {
  vertical-align: middle;
}

/* Completed tasks: strike-through, dimmed */
.task-done .task-title {
  text-decoration: line-through;
  color: #98a2b3;
}

/* ============================================================
   MOBILE DATA CARDS — tables with .table-mobile collapse into
   stacked cards on phones. Cell labels come from data-label="".
     <td data-label="Email">a@b.c</td>
   Special cells:
     .mob-main    — primary line (no label, larger text)
     .mob-corner  — pinned to the card's top-right (checkbox...)
     .mob-actions — action buttons row (bottom, right-aligned)
   ============================================================ */
@media (max-width: 991.98px) {
  /* Cards never overflow sideways — drop the scroll wrapper so
     in-card dropdowns (e.g. the deal stage menu) are not clipped */
  .table-responsive:has(> .table-mobile) {
    overflow: visible;
  }

  .table-mobile {
    border-collapse: separate;
  }

  .table-mobile thead {
    display: none;
  }

  .table-mobile tbody {
    display: block;
  }

  .table-mobile tbody tr {
    position: relative;
    display: block;
    background: #fff;
    border: 1px solid var(--lc-border);
    border-radius: 12px;
    padding: 10px 14px;
    margin-bottom: 10px;
    box-shadow: 0 1px 3px rgba(30, 37, 48, 0.05);
  }

  /* Status backgrounds (e.g. overdue tasks) move from cells to the card */
  .table-mobile tbody tr.table-danger {
    background: #fdedee;
    border-color: #f5c2c7;
  }

  .table-mobile tbody tr.table-danger > td {
    background: transparent;
    box-shadow: none;
  }

  /* Every value hugs the LEFT edge and grows DOWNWARD — nothing may
     push past the right edge (long names/emails/URLs wrap instead). */
  .table-mobile tbody td {
    display: block;
    border: 0;
    padding: 3px 0;
    background: transparent;
    text-align: left !important;
    min-width: 0;
    max-width: 100%;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* The label becomes a small caption ABOVE its value */
  .table-mobile td[data-label]::before {
    content: attr(data-label);
    display: block;
    color: var(--lc-muted);
    font-size: 0.72rem;
    font-weight: 400;
    margin-bottom: 1px;
  }

  .table-mobile td.mob-main {
    display: block;
    font-size: 1.02rem;
    padding-right: 36px; /* room for .mob-corner */
  }

  .table-mobile td.mob-corner {
    position: absolute;
    top: 12px;
    right: 14px;
    width: auto;
    padding: 0;
  }

  .table-mobile td.mob-actions {
    display: flex; /* the action buttons stay on ONE right-aligned row */
    justify-content: flex-end;
    align-items: center;
    gap: 6px;
    border-top: 1px dashed var(--lc-border);
    margin-top: 8px;
    padding-top: 8px;
  }

  .table-mobile td.mob-actions:empty {
    display: none;
  }

  /* Comfortable touch targets for the card action buttons */
  .table-mobile .mob-actions .btn {
    min-width: 42px;
    min-height: 38px;
  }

  /* Bigger checkboxes for fingers */
  .table-mobile .form-check-input {
    width: 1.35em;
    height: 1.35em;
  }
}

/* ---------- Touch ergonomics (phones) ---------- */
@media (max-width: 991.98px) {
  /* 16px inputs stop iOS Safari from zooming in on focus */
  .form-control,
  .form-select {
    font-size: 16px;
    min-height: 44px;
  }

  .btn {
    min-height: 40px;
  }

  .btn-sm,
  .app-more-sheet .btn-close,
  .modal .btn-close {
    min-height: 0;
  }

  /* Page headings slightly smaller on phones */
  .module-container h4 {
    font-size: 1.15rem;
  }

  /* Modal forms: full-bleed comfort */
  #app-modal .modal-body {
    padding-bottom: calc(var(--safe-bottom) + 16px);
  }
}

/* ============================================================
   DESKTOP (≥ 992px): sidebar layout, no top/bottom bars
   ============================================================ */
@media (min-width: 992px) {
  .app-content {
    padding: 24px;
  }

  #toast-area {
    top: 0;
    left: auto;
    right: 0;
    align-items: flex-end;
  }

  #toast-area .toast {
    width: 350px;
  }

  .chart-box {
    height: 280px;
  }

  /* On wide screens the table fits, so drop the horizontal scroll
     wrapper — otherwise in-table dropdowns (e.g. the deal stage
     selector) get clipped. Narrow screens keep Bootstrap's scroll. */
  .table-responsive {
    overflow: visible;
  }
}
