/* =============================================================
   CRM OrunAye — main.css
   Variables, reset, layout base, sidebar, Mobile First
   ============================================================= */

/* ─── CSS Custom Properties ──────────────────────────────────── */
:root {
  /* Brand palette */
  --color-brand:        #7c3aed;   /* purple-700 */
  --color-brand-dark:   #5b21b6;   /* purple-800 */
  --color-brand-light:  #ede9fe;   /* purple-100 */
  --color-brand-text:   #ffffff;

  /* Neutrals */
  --color-bg:           #f8f7ff;
  --color-surface:      #ffffff;
  --color-border:       #e5e7eb;
  --color-muted:        #6b7280;
  --color-text:         #111827;
  --color-text-light:   #374151;

  /* Status colours */
  --color-success:      #059669;
  --color-warning:      #d97706;
  --color-danger:       #dc2626;
  --color-info:         #2563eb;

  /* Sidebar */
  --sidebar-width:      260px;
  --topbar-height:      56px;

  /* Spacing */
  --space-xs:  4px;
  --space-sm:  8px;
  --space-md:  16px;
  --space-lg:  24px;
  --space-xl:  32px;
  --space-2xl: 48px;

  /* Typography */
  --font-base:  'Segoe UI', system-ui, -apple-system, sans-serif;
  --font-mono:  'Cascadia Code', 'Consolas', monospace;
  --text-xs:    0.75rem;
  --text-sm:    0.875rem;
  --text-base:  1rem;
  --text-lg:    1.125rem;
  --text-xl:    1.25rem;
  --text-2xl:   1.5rem;

  /* Shadows */
  --shadow-sm:  0 1px 2px rgba(0,0,0,.06);
  --shadow-md:  0 4px 12px rgba(0,0,0,.10);
  --shadow-lg:  0 8px 24px rgba(0,0,0,.14);

  /* Border radius */
  --radius-sm:  4px;
  --radius-md:  8px;
  --radius-lg:  12px;
  --radius-xl:  16px;
  --radius-full: 9999px;

  /* Transitions */
  --transition: 220ms ease;
}

/* ─── Reset ──────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-base);
  font-size: var(--text-base);
  color: var(--color-text);
  background-color: var(--color-bg);
  line-height: 1.5;
  min-height: 100dvh;
}

img, svg { display: block; max-width: 100%; }

a {
  color: var(--color-brand);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
}

ul, ol { list-style: none; }

input, select, textarea {
  font-family: inherit;
  font-size: inherit;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: 600;
  line-height: 1.25;
}

/* ─── Utilities ──────────────────────────────────────────────── */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

[hidden] { display: none !important; }

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ─── Login Overlay ──────────────────────────────────────────── */
.login-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--color-brand-dark) 0%, var(--color-brand) 100%);
  z-index: 9999;
  padding: var(--space-md);
}

.login-card {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: var(--space-xl) var(--space-xl);
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-lg);
}

.login-logo {
  text-align: center;
  margin-bottom: var(--space-xl);
}

.login-logo-text {
  display: block;
  font-size: var(--text-2xl);
  font-weight: 700;
  color: var(--color-brand);
  letter-spacing: -0.02em;
}

.login-logo-sub {
  display: block;
  font-size: var(--text-sm);
  color: var(--color-muted);
  margin-top: var(--space-xs);
}

.login-form { display: flex; flex-direction: column; gap: var(--space-md); }

.login-error {
  background: #fee2e2;
  color: var(--color-danger);
  border-radius: var(--radius-md);
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-sm);
}

/* ─── App Shell ──────────────────────────────────────────────── */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* ─── Top Bar ────────────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--topbar-height);
  background: var(--color-brand);
  color: var(--color-brand-text);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 0 var(--space-md);
  box-shadow: var(--shadow-md);
}

.topbar-menu-btn {
  color: var(--color-brand-text);
  font-size: 1.4rem;
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  min-height: 36px;
  transition: background var(--transition);
}
.topbar-menu-btn:hover { background: rgba(255,255,255,0.15); }

.topbar-title {
  flex: 1;
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  position: relative;
}

.topbar-btn {
  color: var(--color-brand-text);
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  min-height: 36px;
  transition: background var(--transition);
}
.topbar-btn:hover { background: rgba(255,255,255,0.15); }

.user-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: rgba(255,255,255,0.25);
  font-weight: 700;
  font-size: var(--text-sm);
  color: #fff;
  text-transform: uppercase;
}

.user-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  z-index: 200;
  overflow: hidden;
}

.user-menu-name {
  padding: var(--space-sm) var(--space-md);
  font-weight: 600;
  font-size: var(--text-sm);
  color: var(--color-text);
}

.user-menu-role {
  padding: 0 var(--space-md) var(--space-sm);
  font-size: var(--text-xs);
  color: var(--color-muted);
  text-transform: capitalize;
}

.user-menu-divider {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 0;
}

.user-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-sm);
  color: var(--color-danger);
  transition: background var(--transition);
}
.user-menu-item:hover { background: #fee2e2; }

/* ─── Layout ─────────────────────────────────────────────────── */
.layout {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

/* ─── Sidebar ────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  background: #1e1b4b;           /* deep indigo */
  color: #c7d2fe;                /* indigo-200 */
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--topbar-height));
  position: fixed;
  top: var(--topbar-height);
  left: 0;
  z-index: 90;
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform var(--transition);
  box-shadow: var(--shadow-lg);
}

/* Sidebar open state */
.sidebar.is-open {
  transform: translateX(0);
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  top: var(--topbar-height);
  background: rgba(0,0,0,0.45);
  z-index: 80;
  backdrop-filter: blur(2px);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md);
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.sidebar-brand {
  font-size: var(--text-base);
  font-weight: 700;
  color: #fff;
}

.sidebar-close-btn {
  color: #c7d2fe;
  font-size: 1.1rem;
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}
.sidebar-close-btn:hover { background: rgba(255,255,255,0.1); }

.sidebar-nav {
  flex: 1;
  padding: var(--space-sm) 0;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 11px var(--space-md);
  color: #a5b4fc;
  font-size: var(--text-sm);
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
  border-left: 3px solid transparent;
  text-decoration: none;
}

.sidebar-link:hover {
  background: rgba(255,255,255,0.07);
  color: #fff;
  text-decoration: none;
}

.sidebar-link.is-active {
  background: rgba(124,58,237,0.35);
  color: #fff;
  border-left-color: var(--color-brand);
}

.nav-icon {
  flex-shrink: 0;
  opacity: 0.8;
}

.sidebar-footer {
  padding: var(--space-sm) var(--space-md);
  border-top: 1px solid rgba(255,255,255,0.08);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.sidebar-user {
  flex: 1;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  min-width: 0;
}

.sidebar-user-avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--color-brand);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
}

.sidebar-user-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.sidebar-user-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-user-role {
  font-size: var(--text-xs);
  color: #a5b4fc;
  text-transform: capitalize;
}

.sidebar-logout-btn {
  color: #a5b4fc;
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
  display: flex;
  align-items: center;
}
.sidebar-logout-btn:hover { background: rgba(220,38,38,0.2); color: #fca5a5; }

/* ─── Main Content ───────────────────────────────────────────── */
.main-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
  height: calc(100dvh - var(--topbar-height));
  /* On mobile, full width (sidebar overlays) */
  width: 100%;
}

.view-container {
  max-width: 1280px;
  margin: 0 auto;
}

/* ─── Loading Spinner ────────────────────────────────────────── */
.loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  min-height: 200px;
  color: var(--color-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-brand-light);
  border-top-color: var(--color-brand);
  border-radius: var(--radius-full);
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Global Search Overlay ──────────────────────────────────── */
.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 5000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: var(--topbar-height) var(--space-md) var(--space-md);
  backdrop-filter: blur(3px);
}

.search-overlay-inner {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 580px;
  max-height: 80dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.search-input-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md);
  border-bottom: 1px solid var(--color-border);
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: var(--text-base);
  color: var(--color-text);
  background: transparent;
}

.search-close-btn {
  color: var(--color-muted);
  font-size: 1.1rem;
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}
.search-close-btn:hover { background: var(--color-border); }

.search-results {
  overflow-y: auto;
  padding: var(--space-sm);
}

.search-empty {
  text-align: center;
  color: var(--color-muted);
  font-size: var(--text-sm);
  padding: var(--space-xl);
}

/* ─── Toast Container ────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: var(--space-md);
  right: var(--space-md);
  z-index: 9000;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  pointer-events: none;
  max-width: calc(100vw - var(--space-xl));
}

/* ─── Modal Container ────────────────────────────────────────── */
.modal-container {
  position: fixed;
  inset: 0;
  z-index: 8000;
  display: flex;
  align-items: flex-end;   /* bottom sheet on mobile */
  justify-content: center;
  padding: var(--space-md);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(2px);
}

.modal-box {
  position: relative;
  background: var(--color-surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  width: 100%;
  max-width: 560px;
  max-height: 90dvh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  z-index: 1;
  animation: slide-up 0.22s ease;
}

@keyframes slide-up {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-lg) var(--space-lg) var(--space-md);
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  background: var(--color-surface);
  z-index: 1;
}

.modal-title {
  font-size: var(--text-xl);
  font-weight: 700;
}

.modal-close {
  color: var(--color-muted);
  font-size: 1.2rem;
  padding: var(--space-xs);
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}
.modal-close:hover { background: var(--color-border); }

.modal-body {
  padding: var(--space-lg);
}

.modal-footer {
  padding: var(--space-md) var(--space-lg) var(--space-lg);
  display: flex;
  gap: var(--space-sm);
  justify-content: flex-end;
  border-top: 1px solid var(--color-border);
}

/* ─── Page Header ────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-sm);
  margin-bottom: var(--space-lg);
}

.page-title {
  font-size: var(--text-2xl);
  font-weight: 700;
  color: var(--color-text);
}

/* ─── Grid helpers ───────────────────────────────────────────── */
.grid {
  display: grid;
  gap: var(--space-md);
}

.grid-2 { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }

/* ─── Desktop breakpoint: sidebar always visible ─────────────── */
@media (min-width: 768px) {
  /* Hide toggle + close button */
  .topbar-menu-btn { display: none; }
  .sidebar-close-btn { display: none; }

  /* Sidebar always open, not overlapping */
  .sidebar {
    position: sticky;
    top: 0;
    transform: none;
    height: calc(100dvh - var(--topbar-height));
    flex-shrink: 0;
  }

  .layout {
    overflow: visible;
  }

  .main-content {
    /* Leave room for sidebar */
    flex: 1;
    min-width: 0;
  }

  /* Modal centered, not bottom sheet */
  .modal-container {
    align-items: center;
  }

  .modal-box {
    border-radius: var(--radius-xl);
    max-height: 85dvh;
  }

  /* Wider grid on larger screens */
  .grid-2 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(3, 1fr); }
  .grid-4 { grid-template-columns: repeat(4, 1fr); }
}

@media (min-width: 1024px) {
  .main-content {
    padding: var(--space-xl);
  }
}
