/* ==========================================================================
   Zain OS — App Shell (sidebar, top bar, canvas)
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  grid-area: sidebar;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  z-index: var(--z-sidebar);
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border-light);
}
.logo-block {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
}
.logo-mark {
  width: 28px; height: 28px;
  border-radius: 7px;
  background: var(--brand);
  color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: var(--fw-bold);
  font-size: var(--fs-15);
  letter-spacing: -0.02em;
  flex-shrink: 0;
}
.logo-mark.logo-lg { width: 36px; height: 36px; border-radius: 8px; font-size: var(--fs-18); }
.logo-text { font-size: var(--fs-15); font-weight: var(--fw-semibold); letter-spacing: -0.01em; }
.logo-text .os { color: var(--text-secondary); font-weight: var(--fw-medium); }

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-3) var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav-section-label {
  font-size: var(--fs-10);
  font-weight: var(--fw-semibold);
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: var(--sp-3) var(--sp-3) var(--sp-1);
}
.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 8px var(--sp-3);
  color: var(--nav-muted);
  border-radius: 6px;
  font-size: var(--fs-13);
  font-weight: var(--fw-medium);
  transition: background var(--dur-default) var(--ease),
              color var(--dur-default) var(--ease);
  cursor: pointer;
}
.nav-item:hover { background: var(--hover); color: var(--text); }
.nav-item.is-active {
  background: var(--brand-soft);
  color: var(--brand);
}
.nav-item .icon { width: 16px; height: 16px; flex-shrink: 0; }
.nav-item .nav-count {
  margin-left: auto;
  font-size: var(--fs-11);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  background: var(--card);
  padding: 1px 7px;
  border-radius: var(--r-pill);
}
.nav-item.is-active .nav-count {
  background: var(--brand);
  color: #fff;
}

.sidebar-footer {
  padding: var(--sp-3) var(--sp-3);
  border-top: 1px solid var(--border-light);
}

/* VPS status pill (sidebar footer) — replaces the old Logout link. */
.vps-status {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 6px 8px;
  font-size: var(--fs-11);
  color: var(--text-tertiary);
}
.vps-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #7ab28f;            /* ok = green */
  flex-shrink: 0;
  transition: background var(--dur-default) var(--ease);
}
.vps-dot.warn { background: #d4a574; }
.vps-dot.off  { background: var(--danger); }

/* ---------- Top bar ---------- */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: 0 var(--sp-6);
  height: var(--topbar-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border-light);
  position: sticky;
  top: 0;
  z-index: var(--z-topbar);
}
.topbar .breadcrumb {
  display: flex; align-items: center; gap: var(--sp-2);
  font-size: var(--fs-13);
  color: var(--text-secondary);
}
.topbar .breadcrumb .crumb-current { color: var(--text); font-weight: var(--fw-medium); }
.topbar .breadcrumb .sep { opacity: 0.5; }

.global-search {
  flex: 1;
  max-width: 520px;
  margin: 0 auto;
  position: relative;
}
.global-search-input {
  width: 100%;
  height: 36px;
  padding: 0 var(--sp-4) 0 38px;
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: var(--fs-13);
  transition: border-color var(--dur-default) var(--ease),
              box-shadow var(--dur-default) var(--ease);
}
.global-search-input::placeholder { color: var(--text-tertiary); }
.global-search-input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: var(--ring-focus);
}
.global-search .search-icon {
  position: absolute;
  left: 12px; top: 50%; transform: translateY(-50%);
  width: 14px; height: 14px;
  color: var(--text-tertiary);
  pointer-events: none;
}
.global-search .kbd {
  position: absolute;
  right: 10px; top: 50%; transform: translateY(-50%);
  font-family: var(--font-mono);
  font-size: var(--fs-10);
  color: var(--text-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  background: var(--bg);
  pointer-events: none;
}
.global-search-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0; right: 0;
  z-index: var(--z-dropdown);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-left: auto;
}

/* Alerts bell + dropdown panel */
.alerts-wrap { position: relative; display: inline-flex; }
.alerts-bell {
  position: relative;
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--r-sm);
  color: var(--text-secondary);
  background: transparent;
  border: 0;
  cursor: pointer;
  transition: background var(--dur-default) var(--ease), color var(--dur-default) var(--ease);
}
.alerts-bell:hover { background: var(--hover); color: var(--text); }
.alerts-bell.is-open { background: var(--hover); color: var(--text); }
.alerts-bell .icon { width: 18px; height: 18px; }
.alerts-bell .alerts-count {
  position: absolute;
  top: 4px; right: 4px;
  min-width: 16px; height: 16px;
  padding: 0 4px;
  border-radius: var(--r-pill);
  background: var(--brand);
  color: #fff;
  font-size: var(--fs-10);
  font-weight: var(--fw-bold);
  display: inline-flex; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums;
  border: 2px solid var(--bg);
}

.alerts-panel {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 360px;
  max-width: calc(100vw - 24px);
  max-height: calc(100vh - var(--topbar-h) - 24px);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.4);
  z-index: var(--z-dropdown);
  overflow: hidden;
  flex-direction: column;
}
.alerts-panel.is-open { display: flex; }

.alerts-panel .ap-head {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg);
}
.alerts-panel .ap-head .ap-title {
  font-size: var(--fs-13);
  font-weight: var(--fw-semibold);
}
.alerts-panel .ap-head .ap-meta {
  margin-left: auto;
  font-size: var(--fs-11);
  color: var(--text-tertiary);
  font-family: var(--font-mono);
}

.alerts-panel .ap-body {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}
.alerts-panel .ap-section-label {
  font-size: var(--fs-10);
  font-weight: var(--fw-semibold);
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 10px 14px 4px;
}
.alerts-panel .ap-item {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  border-left: 2px solid transparent;
  transition: background var(--dur-default) var(--ease), border-color var(--dur-default) var(--ease);
}
.alerts-panel .ap-item:hover { background: var(--hover); border-left-color: var(--brand-dim); }
.alerts-panel .ap-item .ap-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--brand);
  margin-top: 6px;
  flex-shrink: 0;
}
.alerts-panel .ap-item.ap-warn .ap-dot { background: #d4a574; }
.alerts-panel .ap-item.ap-info .ap-dot { background: var(--text-tertiary); }
.alerts-panel .ap-item .ap-text { flex: 1; min-width: 0; }
.alerts-panel .ap-item .ap-title-row {
  font-size: var(--fs-12);
  color: var(--text);
  font-weight: var(--fw-medium);
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.alerts-panel .ap-item .ap-sub {
  margin-top: 2px;
  font-size: var(--fs-11);
  color: var(--text-secondary);
  font-family: var(--font-mono);
}

.alerts-panel .ap-empty {
  padding: 28px 14px;
  text-align: center;
  font-size: var(--fs-12);
  color: var(--text-tertiary);
}

.alerts-panel .ap-foot {
  padding: 8px 14px;
  border-top: 1px solid var(--border-light);
  font-size: var(--fs-11);
  color: var(--text-tertiary);
  font-family: var(--font-mono);
  text-align: center;
}

/* On phones the panel becomes a full-width sheet anchored to the top bar */
@media (max-width: 640px) {
  .alerts-panel {
    position: fixed;
    top: var(--topbar-h);
    right: 8px;
    left: 8px;
    width: auto;
    max-width: none;
    max-height: calc(100vh - var(--topbar-h) - 16px);
  }
}

/* Profile chip */
.profile-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 4px 10px 4px 4px;
  border-radius: var(--r-pill);
  background: var(--card);
  border: 1px solid var(--border-light);
  cursor: pointer;
  transition: background var(--dur-default) var(--ease);
}
.profile-chip:hover { background: var(--hover); }
.profile-avatar {
  width: 26px; height: 26px;
  border-radius: var(--r-circle);
  background: var(--brand);
  color: #fff;
  font-size: var(--fs-12);
  font-weight: var(--fw-bold);
  display: inline-flex; align-items: center; justify-content: center;
}
.profile-chip .name { font-size: var(--fs-13); font-weight: var(--fw-medium); }

/* ---------- Main canvas ---------- */
.main {
  grid-area: main;
  padding: var(--canvas-pad-y) var(--canvas-pad-x) var(--sp-10);
  overflow-x: hidden;
}
.canvas {
  max-width: var(--canvas-max);
  margin: 0 auto;
}

/* Page header (above canvas content) */
.page-header {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-4);
  margin-bottom: var(--sp-6);
}
.page-header .page-title {
  font-size: var(--fs-22);
  font-weight: var(--fw-semibold);
  letter-spacing: -0.005em;
}
.page-header .page-subtitle {
  font-size: var(--fs-13);
  color: var(--text-secondary);
  margin-top: 2px;
}
.page-header .page-actions {
  margin-left: auto;
  display: flex; gap: var(--sp-2); align-items: center;
}

/* Filter bar (used on units, leads, installments) */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  align-items: center;
  margin-bottom: var(--sp-5);
}
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  height: 32px;
  padding: 0 12px;
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-pill);
  font-size: var(--fs-12);
  font-weight: var(--fw-medium);
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color var(--dur-default) var(--ease), color var(--dur-default) var(--ease);
}
.filter-chip:hover { border-color: var(--brand-dim); color: var(--text); }
.filter-chip.is-active {
  background: var(--brand-soft);
  border-color: var(--brand-dim);
  color: var(--brand);
}
.filter-chip .icon { width: 12px; height: 12px; }

/* ==========================================================================
   Mobile / tablet — hamburger + off-canvas drawer
   --------------------------------------------------------------------------
   Desktop (>1024px) is untouched. Below the breakpoint the sidebar becomes
   a slide-out drawer triggered by .sidebar-toggle in the topbar.
   ========================================================================== */

/* Hamburger button — hidden on desktop, shown on tablet/phone */
.sidebar-toggle {
  display: none;
  width: 36px;
  height: 36px;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-sm);
  color: var(--text);
  background: transparent;
  border: 0;
  cursor: pointer;
  margin-right: var(--sp-2);
  transition: background var(--dur-default) var(--ease);
}
.sidebar-toggle:hover { background: var(--hover); }
.sidebar-toggle:active { background: var(--card); }
.sidebar-toggle .icon { width: 20px; height: 20px; }

/* Drawer scrim — hidden until .active */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: calc(var(--z-sidebar) - 1);
  opacity: 0;
  transition: opacity var(--dur-default) var(--ease);
}
.sidebar-overlay.active {
  display: block;
  opacity: 1;
}

/* ---------- Tablet (≤1024px) ----------
   Collapse layout to a single column; sidebar becomes a fixed drawer that
   slides in from the left when .open is toggled on it.                       */
@media (max-width: 1024px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
  }
  .sidebar-toggle { display: inline-flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: min(280px, 86vw);
    height: 100vh;
    transform: translateX(-100%);
    transition: transform var(--dur-default) var(--ease);
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.18);
    will-change: transform;
  }
  .sidebar.open { transform: translateX(0); }
  .main { padding: var(--sp-5) var(--sp-5) var(--sp-8); }
  .global-search { max-width: none; margin: 0; }
  .global-search .kbd { display: none; }
}

/* ---------- Phone (≤640px) ---------- */
@media (max-width: 640px) {
  .main { padding: var(--sp-4) var(--sp-4) var(--sp-8); }
  .topbar { padding: 0 var(--sp-3); gap: var(--sp-2); }
  .breadcrumb { font-size: var(--fs-12); min-width: 0; }
  .breadcrumb .crumb-current { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; }
  .global-search-input { font-size: 16px; height: 38px; padding-left: 34px; }
  .global-search .search-icon { left: 10px; }
  .profile-chip .name { display: none; }
  .profile-chip { padding: 4px; }
  .topbar-actions { gap: 4px; }
  .page-header { flex-wrap: wrap; gap: var(--sp-3); }
  .page-header .page-actions { margin-left: 0; width: 100%; flex-wrap: wrap; }
  .page-header .page-actions > * { flex: 1 1 auto; min-width: 0; }
  .filter-bar { gap: 6px; }
  .filter-chip { height: 30px; padding: 0 10px; font-size: var(--fs-11); }
}

/* ---------- Tap targets on touch devices ---------- */
@media (hover: none) and (pointer: coarse) {
  .nav-item { padding-top: 10px; padding-bottom: 10px; }
  .btn, .filter-chip { min-height: 36px; }
}

/* ==========================================================================
   PWA standalone mode — safe-area handling for notched iPhones
   When installed and launched from home screen with
   apple-mobile-web-app-status-bar-style=black-translucent, the status bar
   overlays content. Push the topbar / sidebar down with env() insets.
   ========================================================================== */
@supports (padding: max(0px)) {
  /* Topbar respects the status-bar inset on standalone iOS */
  .topbar {
    padding-top: env(safe-area-inset-top, 0px);
    height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  }
  .sidebar-header {
    padding-top: calc(var(--sp-4) + env(safe-area-inset-top, 0px));
    height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  }
  /* Drawer + main canvas respect bottom inset (home indicator) */
  .sidebar-footer { padding-bottom: calc(var(--sp-3) + env(safe-area-inset-bottom, 0px)); }
  .main { padding-bottom: calc(var(--sp-10) + env(safe-area-inset-bottom, 0px)); }
}

/* On the mobile drawer, the sidebar is fixed — handle insets there too */
@media (max-width: 1024px) {
  @supports (padding: max(0px)) {
    .sidebar {
      padding-top: env(safe-area-inset-top, 0px);
    }
  }
}
