/* ============================================================================
   NEAR CRM — styles. Dependency-free, responsive (desktop / tablet / phone).
   ========================================================================== */
:root {
  --brand: #1e3a8a;
  --brand-600: #1d4ed8;
  --brand-500: #2563eb;
  --brand-50: #eff4ff;
  --ink: #0f172a;
  --ink-2: #334155;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --success: #047857;
  --success-bg: #d1fae5;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --info: #1d4ed8;
  --info-bg: #dbeafe;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 16px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 12px 40px rgba(15, 23, 42, 0.18);
  --sidebar-w: 248px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
body.no-scroll { overflow: hidden; }
a { color: var(--brand-600); text-decoration: none; }
h1 { font-size: 1.4rem; margin: 0; }
h2 { font-size: 1.15rem; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }

/* ── Layout ─────────────────────────────────────────────────────────── */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w);
  background: var(--brand); color: #cbd5e1;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 800; font-size: 1.25rem; letter-spacing: 0.5px; color: #fff; padding: 20px 22px; }
.brand span { color: #93c5fd; }
.brand--mobile { display: none; padding: 0; font-size: 1.1rem; }
.nav { flex: 1; overflow-y: auto; padding: 6px 12px; display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: var(--radius-sm);
  color: #c7d2e6; font-weight: 500; transition: background 0.15s, color 0.15s;
}
.nav-link:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.nav-link.active { background: rgba(255, 255, 255, 0.16); color: #fff; }
.nav-link__icon { width: 20px; height: 20px; display: inline-flex; }
.nav-link__icon svg { width: 20px; height: 20px; }
.sidebar__user { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-top: 1px solid rgba(255, 255, 255, 0.12); }
.sidebar__user-info { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.sidebar__user-info strong { color: #fff; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar__user-info small { color: #93a7c4; font-size: 0.75rem; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--brand-500); color: #fff; display: grid; place-items: center; font-weight: 700; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: none; align-items: center; gap: 12px; padding: 10px 14px; background: var(--brand); color: #fff; position: sticky; top: 0; z-index: 20; }
.outlet { padding: 24px clamp(16px, 3vw, 32px); }
.scrim { display: none; }

.icon-btn { background: transparent; border: 0; color: inherit; cursor: pointer; padding: 8px; border-radius: 8px; display: inline-flex; }
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.icon-btn svg { width: 22px; height: 22px; }

/* ── Buttons ────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: var(--radius-sm); padding: 9px 16px;
  font-size: 0.9rem; font-weight: 600; cursor: pointer; background: #fff; color: var(--ink-2);
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn--primary { background: var(--brand-600); color: #fff; }
.btn--primary:hover { background: var(--brand); }
.btn--ghost { background: #fff; border-color: var(--line); color: var(--ink-2); }
.btn--ghost:hover { background: var(--bg); }
.btn--danger { background: var(--danger); color: #fff; }
.btn--danger:hover { background: #991b1b; }
.btn--sm { padding: 6px 10px; font-size: 0.82rem; }
.link-btn { background: none; border: 0; color: var(--brand-600); font-weight: 600; cursor: pointer; padding: 4px 6px; font-size: 0.85rem; border-radius: 6px; }
.link-btn:hover { background: var(--brand-50); }
.link-btn.danger { color: var(--danger); }
.link-btn.danger:hover { background: var(--danger-bg); }

/* ── Toolbar / page ─────────────────────────────────────────────────── */
.page { display: flex; flex-direction: column; gap: 18px; }
.toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.toolbar h1 { flex: 1; min-width: 160px; }
.toolbar__actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.search, .filter {
  border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 8px 12px; font-size: 0.9rem;
  background: #fff; color: var(--ink); min-width: 140px;
}
.search { min-width: 200px; }
.search:focus, .filter:focus, input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-500); outline-offset: -1px; border-color: var(--brand-500); }

/* ── Tables ─────────────────────────────────────────────────────────── */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); padding: 12px 16px; border-bottom: 1px solid var(--line); background: #fafbfc; }
.table td { padding: 13px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--brand-50); }
.row--clickable { cursor: pointer; }
.col-actions { text-align: right; white-space: nowrap; width: 1%; }
.row-actions { display: inline-flex; gap: 2px; justify-content: flex-end; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.table thead th.num, .table th.num { text-align: right; }
.strong { font-weight: 600; color: var(--ink); }
.muted-text { color: var(--muted); }
.subtext { color: var(--muted); font-size: 0.82rem; }

/* ── Badges ─────────────────────────────────────────────────────────── */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 0.74rem; font-weight: 600; white-space: nowrap; }
.badge--success { background: var(--success-bg); color: var(--success); }
.badge--warn { background: var(--warn-bg); color: var(--warn); }
.badge--danger { background: var(--danger-bg); color: var(--danger); }
.badge--info { background: var(--info-bg); color: var(--info); }
.badge--muted { background: #e5e7eb; color: #475569; }

/* ── Empty / loading ────────────────────────────────────────────────── */
.empty, .loading { background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); padding: 48px 24px; text-align: center; color: var(--muted); }
.loading { border-style: solid; }

/* ── Cards & dashboard ──────────────────────────────────────────────── */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.kpi__label { color: var(--muted); font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi__value { font-size: 1.7rem; font-weight: 800; margin-top: 6px; color: var(--ink); }
.kpi__sub { color: var(--muted); font-size: 0.82rem; margin-top: 2px; }
.kpi--accent { background: linear-gradient(135deg, var(--brand-600), var(--brand)); color: #fff; border: 0; }
.kpi--accent .kpi__label, .kpi--accent .kpi__sub, .kpi--accent .kpi__value { color: #fff; }
.kpi--warn .kpi__value { color: var(--danger); }

.panels { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 4px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel__head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.panel__head h2 { margin: 0; font-size: 1rem; }
.panel__body { padding: 8px 18px 16px; }
.stage-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; }
.stage-row__label { width: 130px; font-size: 0.85rem; color: var(--ink-2); flex: 0 0 auto; }
.stage-bar { flex: 1; height: 10px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.stage-bar__fill { height: 100%; background: var(--brand-500); border-radius: 999px; }
.stage-row__value { width: 110px; text-align: right; font-size: 0.85rem; font-variant-numeric: tabular-nums; color: var(--ink-2); }
.progress { height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; min-width: 90px; flex: 1; }
.progress__fill { height: 100%; border-radius: 999px; background: var(--brand-500); transition: width 0.3s; }
.progress__fill--low { background: #ef4444; }
.progress__fill--mid { background: #f59e0b; }
.progress__fill--ok { background: #10b981; }
.progress-row { display: flex; align-items: center; gap: 8px; min-width: 130px; }
.progress-row > span { font-size: 0.8rem; color: var(--muted); width: 38px; text-align: right; font-variant-numeric: tabular-nums; }
.num.over { color: var(--danger); }

.mini-list { list-style: none; margin: 0; padding: 0; }
.mini-list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.mini-list li:last-child { border-bottom: 0; }
.mini-list .grow { flex: 1; min-width: 0; }
.mini-list .grow strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Detail pages ───────────────────────────────────────────────────── */
.detail-head { display: flex; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.detail-head h1 { margin-bottom: 4px; }
.detail-head .spacer { flex: 1; }
.back-link { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-weight: 600; margin-bottom: 8px; background: none; border: 0; cursor: pointer; padding: 0; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.def-list { display: grid; grid-template-columns: max-content 1fr; gap: 8px 18px; margin: 0; }
.def-list dt { color: var(--muted); font-size: 0.85rem; }
.def-list dd { margin: 0; color: var(--ink); }
.section-title { font-size: 0.95rem; font-weight: 700; margin: 22px 0 10px; }

/* ── Forms / modal ──────────────────────────────────────────────────── */
.modal-overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.5); display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; z-index: 100; overflow-y: auto; }
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg); width: 100%; max-width: 560px; display: flex; flex-direction: column; }
.modal--sm { max-width: 420px; }
.modal--lg { max-width: 820px; }
.modal__head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal__head h2 { margin: 0; }
.modal__close { background: none; border: 0; font-size: 1.6rem; line-height: 1; color: var(--muted); cursor: pointer; }
.modal__body { padding: 20px; }
.modal__foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); background: #fafbfc; border-radius: 0 0 var(--radius) var(--radius); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field--full { grid-column: 1 / -1; }
.field label { font-size: 0.82rem; font-weight: 600; color: var(--ink-2); }
.field .req { color: var(--danger); }
.field small { color: var(--muted); font-size: 0.76rem; }
input, select, textarea { font: inherit; color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 9px 11px; background: #fff; width: 100%; }
textarea { resize: vertical; }
.checkbox { flex-direction: row; align-items: center; gap: 8px; display: flex; }
.checkbox input { width: auto; }
.form-error { grid-column: 1 / -1; background: var(--danger-bg); color: var(--danger); padding: 9px 12px; border-radius: var(--radius-sm); font-size: 0.85rem; }
.confirm-text { margin: 0; color: var(--ink-2); }

/* ── Toast ──────────────────────────────────────────────────────────── */
#toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 200; }
.toast { background: var(--ink); color: #fff; padding: 12px 18px; border-radius: var(--radius-sm); box-shadow: var(--shadow-lg); opacity: 0; transform: translateY(10px); transition: opacity 0.25s, transform 0.25s; max-width: 360px; font-size: 0.9rem; }
.toast--in { opacity: 1; transform: translateY(0); }
.toast--error { background: var(--danger); }

/* ── Login ──────────────────────────────────────────────────────────── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(135deg, var(--brand), #0b1c47); }
.login__card { background: #fff; border-radius: 16px; box-shadow: var(--shadow-lg); padding: 32px; width: 100%; max-width: 380px; }
.login__brand { font-weight: 800; font-size: 1.6rem; text-align: center; margin-bottom: 4px; }
.login__brand span { color: var(--brand-600); }
.login__sub { text-align: center; color: var(--muted); margin: 0 0 22px; font-size: 0.9rem; }
.login form { display: flex; flex-direction: column; gap: 14px; }
.login .btn { width: 100%; padding: 11px; }

/* ── Misc helpers ───────────────────────────────────────────────────── */
.totals { display: grid; gap: 6px; max-width: 320px; margin-left: auto; }
.totals div { display: flex; justify-content: space-between; }
.totals .grand { font-weight: 800; font-size: 1.1rem; border-top: 1px solid var(--line); padding-top: 8px; }
.items-table input { padding: 6px 8px; }
.tabs { display: inline-flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 999px; }
.tab { border: 0; background: none; padding: 6px 14px; border-radius: 999px; font-weight: 600; color: var(--muted); cursor: pointer; font-size: 0.85rem; }
.tab.active { background: #fff; color: var(--brand-600); box-shadow: var(--shadow); }
.row-inline { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ── Responsive ─────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .panels, .detail-grid { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .sidebar {
    position: fixed; left: 0; top: 0; z-index: 50; transform: translateX(-100%);
    transition: transform 0.25s ease; box-shadow: var(--shadow-lg);
  }
  .nav-open .sidebar { transform: translateX(0); }
  .nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); z-index: 40; }
  .topbar { display: flex; }
  .brand--mobile { display: block; }
  .outlet { padding: 16px; }

  /* Tables collapse to cards */
  .table, .table thead, .table tbody, .table tr, .table td { display: block; }
  .table thead { display: none; }
  .table-wrap { background: transparent; border: 0; box-shadow: none; }
  .table tr { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 12px; padding: 6px 4px; }
  .table td { border: 0; padding: 8px 14px; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right; }
  .table td::before { content: attr(data-label); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; text-align: left; flex: 0 0 auto; }
  .table td.col-actions { justify-content: flex-end; }
  .table td.col-actions::before { content: ""; }
  .row-actions { justify-content: flex-end; width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .totals { max-width: none; }
}
