/* ==========================================================================
   Fincharts CRM — Theme & Component Library
   Ported from the approved demo (fincharts-crm-demo.html) and doc 00 §13.
   Premium corporate SaaS / ERP look: slate neutrals + indigo accent, Inter.
   Tailwind utility classes handle layout; this file owns the component skin
   and the application shell so every module screen inherits the same look.
   ========================================================================== */

:root {
  --brand:        #4f46e5;   /* indigo-600  — primary accent / buttons       */
  --brand-700:    #4338ca;   /* indigo-700  — hover                          */
  --brand-300:    #a5b4fc;   /* indigo-300  — active text on dark sidebar    */
  --brand-500:    #6366f1;   /* indigo-500  — focus border / active accent   */
  --brand-50:     #eef2ff;   /* indigo-50   — soft highlight bg              */
  --brand-ring:   rgba(99, 102, 241, 0.12);
  --bg:           #f8fafc;   /* slate-50    — app background                 */
  --surface:      #ffffff;   /* cards / topbar                              */
  --sidebar:      #0f172a;   /* slate-900   — sidebar                        */
  --sidebar-2:    #1e293b;   /* slate-800   — sidebar borders                */
  --border:       #e2e8f0;   /* slate-200   — soft grey borders             */
  --border-soft:  #f1f5f9;   /* slate-100                                   */
  --text:         #0f172a;   /* slate-900                                   */
  --text-2:       #334155;   /* slate-700                                   */
  --muted:        #64748b;   /* slate-500                                   */
  --muted-2:      #94a3b8;   /* slate-400                                   */
  --sidebar-w:    256px;
  --topbar-h:     60px;
}

html, body { font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif; }
body { background: var(--bg); color: var(--text); margin: 0; }
* { box-sizing: border-box; }
a { text-decoration: none; }

/* Scrollbars ---------------------------------------------------------------- */
.scrollbar-thin::-webkit-scrollbar { width: 6px; height: 6px; }
.scrollbar-thin::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
.scrollbar-thin::-webkit-scrollbar-track { background: transparent; }

/* ==========================================================================
   Application shell
   ========================================================================== */
.app-shell { min-height: 100vh; }

.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0; width: var(--sidebar-w);
  background: var(--sidebar); color: #cbd5e1; display: flex; flex-direction: column;
  z-index: 60; overflow-y: auto;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 12px; height: var(--topbar-h);
  padding: 0 20px; border-bottom: 1px solid var(--sidebar-2); flex-shrink: 0;
}
.sidebar-brand .name { font-weight: 700; color: #fff; font-size: 15px; line-height: 1; }
.sidebar-brand .sub  { font-size: 10px; color: #64748b; letter-spacing: .08em; text-transform: uppercase; }
.sidebar-nav { padding: 12px 10px; flex: 1; }
.sidebar-section { font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: #475569; padding: 14px 12px 6px; }

.sidebar-link {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px;
  color: #cbd5e1; font-size: 13px; font-weight: 500; cursor: pointer;
  border-left: 3px solid transparent; transition: background .15s, color .15s;
}
.sidebar-link:hover { background: rgba(255,255,255,0.05); color: #fff; }
.sidebar-link.active { background: rgba(99,102,241,0.15); color: var(--brand-300); border-left-color: var(--brand-500); }
.sidebar-link .ico { width: 18px; height: 18px; flex-shrink: 0; opacity: .9; }
.sidebar-link .chevron { margin-left: auto; transition: transform .2s; opacity: .6; }

.group-toggle.open .chevron { transform: rotate(90deg); }
.group-children { display: none; padding-left: 8px; }
.group-toggle.open + .group-children { display: block; }
.group-children .sidebar-link { font-size: 12.5px; padding: 7px 12px 7px 22px; color: #94a3b8; }
.group-children .sidebar-link:hover { color: #e2e8f0; }
.group-children .sidebar-link.active { color: var(--brand-300); }

/* Main column + topbar */
.main { margin-left: var(--sidebar-w); min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; height: var(--topbar-h); background: var(--surface);
  border-bottom: 1px solid var(--border); display: flex; align-items: center;
  gap: 16px; padding: 0 24px; z-index: 50;
}
.topbar h1 { font-size: 16px; font-weight: 600; color: var(--text); margin: 0; }
.topbar-search {
  display: flex; align-items: center; gap: 8px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px;
  min-width: 240px; color: var(--muted);
}
.topbar-search input { border: 0; background: transparent; outline: none; font-size: 13px; width: 100%; color: var(--text); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 8px; display: inline-flex; align-items: center;
  justify-content: center; color: var(--muted); cursor: pointer; position: relative;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--border-soft); color: var(--text-2); }
.icon-btn .dot { position: absolute; top: 8px; right: 9px; width: 7px; height: 7px; border-radius: 9999px; background: #ef4444; border: 2px solid #fff; }
.avatar { width: 34px; height: 34px; border-radius: 9999px; background: var(--brand); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-weight: 600; font-size: 13px; }

.content { padding: 24px; flex: 1; }
.page-title { font-size: 20px; font-weight: 600; color: var(--text); }
.page-subtitle { font-size: 13px; color: var(--muted); margin-top: 2px; }

/* Mobile sidebar (off-canvas) */
.sidebar-toggle { display: none; }
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 55; }
@media (max-width: 1024px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .sidebar-toggle { display: inline-flex; }
  .sidebar-overlay.show { display: block; }
}

/* ==========================================================================
   Cards
   ========================================================================== */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.card-pad { padding: 20px; }
.card-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.card-title { font-size: 14px; font-weight: 600; color: var(--text); }

.kpi-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px;
  transition: transform .15s, box-shadow .15s;
}
.kpi-card:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(0,0,0,0.06); }
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.kpi-value { font-size: 24px; font-weight: 700; color: var(--text); margin-top: 6px; }
.kpi-delta { font-size: 12px; font-weight: 600; margin-top: 6px; display: inline-flex; align-items: center; gap: 4px; }
.kpi-delta.up { color: #16a34a; }
.kpi-delta.down { color: #dc2626; }
.kpi-icon { width: 40px; height: 40px; border-radius: 10px; background: var(--brand-50); color: var(--brand); display: inline-flex; align-items: center; justify-content: center; }

/* ==========================================================================
   Badges & pills
   ========================================================================== */
.badge { display: inline-flex; align-items: center; padding: 2px 10px; font-size: 11px; font-weight: 600; border-radius: 9999px; }
.badge-green  { background: #dcfce7; color: #166534; }
.badge-yellow { background: #fef3c7; color: #92400e; }
.badge-red    { background: #fee2e2; color: #991b1b; }
.badge-blue   { background: #dbeafe; color: #1e40af; }
.badge-gray   { background: #f1f5f9; color: #475569; }
.badge-purple { background: #ede9fe; color: #6d28d9; }
.badge-teal   { background: #ccfbf1; color: #115e59; }
.badge-orange { background: #ffedd5; color: #9a3412; }
.infinity-pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; font-size: 12px; font-weight: 600; border-radius: 9999px; background: var(--brand-50); color: #4338ca; }
.infinity-pill .inf-sym { font-size: 16px; line-height: 1; }

/* ==========================================================================
   Forms
   ========================================================================== */
.form-section { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.form-label { font-size: 12px; font-weight: 500; color: var(--text-2); margin-bottom: 4px; display: block; }
.form-input, .form-select, .form-textarea {
  width: 100%; padding: 9px 12px; border: 1px solid #cbd5e1; border-radius: 7px;
  font-size: 13px; background: #fff; color: var(--text); transition: border-color .15s, box-shadow .15s;
}
.form-input::placeholder, .form-textarea::placeholder { color: var(--muted-2); }
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none; border-color: var(--brand-500); box-shadow: 0 0 0 3px var(--brand-ring);
}
.form-hint { font-size: 11px; color: var(--muted); margin-top: 4px; }
.form-error { font-size: 11px; color: #dc2626; margin-top: 4px; }

/* ==========================================================================
   Buttons
   ========================================================================== */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 9px 16px; border-radius: 7px; font-weight: 500; font-size: 13px; cursor: pointer; border: 1px solid transparent; transition: background .15s, border-color .15s, color .15s, box-shadow .15s; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-700); }
.btn-secondary { background: #fff; color: var(--text-2); border-color: #cbd5e1; }
.btn-secondary:hover { background: var(--border-soft); }
.btn-ghost { color: var(--brand); background: transparent; }
.btn-ghost:hover { background: var(--brand-50); }
.btn-danger { background: #dc2626; color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn .ico { width: 16px; height: 16px; }

/* ==========================================================================
   Tabs
   ========================================================================== */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); }
.tab { padding: 10px 16px; font-size: 13px; font-weight: 500; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; }
.tab:hover { color: var(--text-2); }
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }

/* ==========================================================================
   Tables & filter bar
   ========================================================================== */
.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 600; padding: 11px 16px; background: #fff;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data tbody td { padding: 12px 16px; border-bottom: 1px solid var(--border-soft); color: var(--text-2); }
table.data tbody tr { transition: background .12s; }
table.data tbody tr:hover { background: #f8fafc; }
table.data tbody tr:last-child td { border-bottom: 0; }

/* Professional list-table polish — applies to every .data table (presentation only).
   Subtle tinted header + zebra rows + stronger hover; opt-in right-aligned tabular
   numbers via class="num" and single-line cells via class="nowrap". */
table.data thead th { background: #f8fafc; }
table.data tbody tr:nth-child(even) { background: #fcfdfe; }
table.data tbody tr:nth-child(even):hover,
table.data tbody tr:hover { background: #f5f8ff; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data td.nowrap, table.data th.nowrap { white-space: nowrap; }
.row-action { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 6px; color: var(--muted); cursor: pointer; transition: background .15s, color .15s; }
.row-action:hover { background: var(--brand-50); color: var(--brand-700); }

/* Pagination */
.pager { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; gap: 10px; }
.pager-info { font-size: 12px; color: var(--muted); }
.pager-btns { display: flex; gap: 4px; align-items: center; }
.pager-btn { min-width: 30px; height: 30px; padding: 0 8px; font-size: 12px; font-weight: 500; border: 1px solid #cbd5e1; background: #fff; color: var(--text-2); border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }
.pager-btn:hover:not(:disabled):not(.active) { background: var(--border-soft); }
.pager-btn:disabled { opacity: .45; cursor: not-allowed; }
.pager-btn.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.pager-ellipsis { padding: 0 4px; color: var(--muted-2); font-size: 12px; }

/* ==========================================================================
   Dropdown / menu
   ========================================================================== */
.dropdown { position: relative; }
.dropdown-menu { position: absolute; right: 0; top: 100%; margin-top: 8px; min-width: 220px; background: #fff; border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 10px 25px rgba(0,0,0,0.08); z-index: 70; display: none; overflow: hidden; }
.dropdown.open .dropdown-menu { display: block; }
.dropdown-item { display: flex; align-items: center; gap: 10px; padding: 10px 14px; font-size: 13px; color: var(--text-2); cursor: pointer; }
.dropdown-item:hover { background: var(--border-soft); }
.dropdown-divider { height: 1px; background: var(--border); }

/* ==========================================================================
   Modal
   ========================================================================== */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.55); z-index: 100; display: none; align-items: flex-start; justify-content: center; padding: 32px 16px; overflow-y: auto; }
.modal-overlay.open { display: flex; }
.modal-panel { background: var(--bg); border-radius: 12px; width: 100%; max-width: 720px; box-shadow: 0 20px 50px rgba(0,0,0,0.2); overflow: hidden; }
.modal-header { background: #fff; padding: 16px 24px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.modal-title { font-size: 15px; font-weight: 600; color: var(--text); }
.modal-body { padding: 20px 24px; max-height: calc(100vh - 220px); overflow-y: auto; }
.modal-footer { background: #fff; padding: 14px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* ==========================================================================
   Toasts
   ========================================================================== */
.toast-host { position: fixed; top: 24px; right: 24px; z-index: 200; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { background: #ecfdf5; border: 1px solid #a7f3d0; color: #065f46; padding: 10px 14px; border-radius: 8px; font-size: 13px; font-weight: 500; box-shadow: 0 10px 25px rgba(0,0,0,0.08); display: flex; align-items: center; gap: 8px; min-width: 240px; opacity: 0; transform: translateY(-6px); transition: opacity .2s, transform .2s; pointer-events: auto; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast .tick { width: 18px; height: 18px; border-radius: 9999px; background: #10b981; color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 11px; flex-shrink: 0; }

/* ==========================================================================
   Auth (login) screen
   ========================================================================== */
.auth-bg { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(135deg, #4338ca 0%, #6d28d9 50%, #1e3a8a 100%); }
.auth-card { background: #fff; border-radius: 16px; box-shadow: 0 25px 60px rgba(0,0,0,0.3); padding: 40px; width: 100%; max-width: 420px; }

/* Utility */
.grid-kpi { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 16px; }
@media (max-width: 1024px) { .grid-kpi { grid-template-columns: repeat(2, minmax(0,1fr)); } }
@media (max-width: 560px)  { .grid-kpi { grid-template-columns: 1fr; } }
.muted { color: var(--muted); }
.text-brand { color: var(--brand); }
