:root {
  --ipesa-red: #EA392F;
  --ipesa-green: #3FC91E;
  --bg: #121212;
  --bg-sidebar: #161616;
  --bg-card: #1E1E1E;
  --bg-card-hover: #242424;
  --border: #2A2A2A;
  --text: #F5F5F5;
  --text-muted: #9A9A9A;
  --text-faint: #6B6B6B;
  --shadow: none;
}

/* Applied by views/partials/theme-init.hbs before first paint (reads the
   admin's saved preference from localStorage), and flipped live by the
   topbar's theme-toggle button — see views/partials/topbar.hbs. */
:root[data-theme="light"] {
  --bg: #F4F5F7;
  --bg-sidebar: #FFFFFF;
  --bg-card: #FFFFFF;
  --bg-card-hover: #F2F3F5;
  --border: #E4E6EB;
  --text: #16181A;
  --text-muted: #6B7280;
  --text-faint: #9CA3AF;
  --shadow: 0 2px 8px rgba(16, 24, 40, 0.06);
}

/* Logo swap: the white-wordmark logo only reads on the dark sidebar/login
   card; the colored-text variant takes over once light theme is active. */
.logo-light-theme-only { display: none !important; }
:root[data-theme="light"] .logo-dark-theme-only { display: none !important; }
:root[data-theme="light"] .logo-light-theme-only { display: block !important; }

* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  overflow-x: hidden;
}

a { color: inherit; }

/* ---------- App shell (sidebar + main) ---------- */

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-logo { display: flex; align-items: center; padding: 4px 8px 28px; text-decoration: none; }
.sidebar-logo .logo-image { height: 42px; width: auto; }

.sidebar-nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }

.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 14px; border-radius: 10px;
  color: var(--text-muted); text-decoration: none;
  font-size: 14px; font-weight: 500;
}
.nav-item:hover { background: var(--bg-card); color: var(--text); }
.nav-item.active {
  background: linear-gradient(135deg, var(--ipesa-red), #c72e25);
  color: #fff;
}
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; }

.nav-section-title {
  font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--text-faint); padding: 16px 14px 6px;
}
.nav-section-title:first-child { padding-top: 4px; }
.nav-divider { height: 1px; background: var(--border); margin: 8px 10px 0; }

.sidebar-copyright { font-size: 11px; color: var(--text-faint); text-align: left; margin-top: auto; padding: 12px 8px 0; }
.sidebar-bottom { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 12px; }

.main-panel { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* ---------- Topbar ---------- */

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 24px; padding: 18px 32px;
  border-bottom: 1px solid var(--border);
}
.search-form {
  flex: 1; max-width: 480px;
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 16px;
}
.search-icon { width: 18px; height: 18px; color: var(--text-faint); flex-shrink: 0; }
.search-form input {
  background: transparent; border: none; outline: none;
  color: var(--text); font-size: 14px; width: 100%;
}
.search-form input::placeholder { color: var(--text-faint); }

.topbar-right { display: flex; align-items: center; gap: 16px; }

.theme-toggle {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: var(--bg-card); border: 1px solid var(--border); color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; margin: 0; padding: 0;
}
.theme-toggle:hover { background: var(--bg-card-hover); color: var(--text); }
.theme-icon { width: 18px; height: 18px; }
.theme-icon-moon { display: none; }
:root[data-theme="light"] .theme-icon-sun { display: none; }
:root[data-theme="light"] .theme-icon-moon { display: block; }

.admin-avatar {
  width: 40px; height: 40px; border-radius: 50%; overflow: hidden;
  background: linear-gradient(135deg, var(--ipesa-green), var(--ipesa-red));
  color: #fff; font-weight: 700; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; text-decoration: none; flex-shrink: 0;
}
.admin-avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- Page content ---------- */

.page-content { padding: 32px; max-width: 1400px; }

.page-header { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 28px; flex-wrap: wrap; gap: 16px; }
.page-date { color: var(--text-muted); font-size: 14px; margin-bottom: 2px; line-height: 1.2; }
.page-greeting { font-size: 32px; font-weight: 800; letter-spacing: -0.01em; line-height: 1.15; }
.page-header-actions { display: flex; gap: 10px; }
.wallet-balance { text-align: right; }
.wallet-balance-label { color: var(--text-muted); font-size: 13px; margin-bottom: 4px; }
.wallet-balance-value { font-size: 32px; font-weight: 800; letter-spacing: -0.01em; color: var(--text); }
.btn-ghost {
  background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 10px 18px; font-size: 14px; font-weight: 600;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 8px;
}
.btn-ghost:hover { background: var(--bg-card-hover); }
.btn-ghost.disabled { opacity: 0.4; cursor: default; pointer-events: none; }
.pagination-row { display: flex; justify-content: space-between; gap: 12px; margin-top: 16px; }
.btn-primary {
  background: var(--ipesa-red); border: none; color: #fff;
  border-radius: 10px; padding: 10px 18px; font-size: 14px; font-weight: 600;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 8px;
}
.btn-primary:hover { background: #d32e25; }

/* ---------- Stat cards ---------- */

.stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 18px; margin-bottom: 18px; }
.stat-card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px;
  padding: 20px 16px; text-align: center; box-shadow: var(--shadow);
}
.stat-label { color: var(--text-muted); font-size: 14px; margin-bottom: 14px; }
.stat-value { font-size: 32px; font-weight: 800; letter-spacing: -0.01em; }

/* ---------- Chart cards row ---------- */

.chart-row { display: grid; grid-template-columns: 1fr 1fr 1.3fr; gap: 18px; margin-bottom: 18px; align-items: stretch; }
.chart-card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px;
  padding: 24px; box-shadow: var(--shadow);
}
.chart-card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.chart-card-head h3 { font-size: 17px; font-weight: 700; margin: 0; }
.chart-period {
  background: var(--bg); border: 1px solid var(--border); color: var(--text-muted);
  border-radius: 8px; padding: 6px 12px; font-size: 12px;
}

.donut-wrap { display: flex; align-items: center; gap: 28px; }
.donut-chart { position: relative; width: 180px; height: 180px; flex-shrink: 0; }
.donut-chart svg { width: 100%; height: 100%; }
.donut-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.donut-center-value { font-size: 30px; font-weight: 800; }
.donut-center-label { font-size: 11px; color: var(--text-muted); letter-spacing: 0.04em; text-transform: uppercase; }
.donut-legend { display: flex; flex-direction: column; gap: 14px; }
.donut-legend-row { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.legend-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.donut-legend-count { font-weight: 700; width: 22px; }
.donut-legend-label { color: var(--text-muted); }

/* ---------- Risk breakdown bar ---------- */

.risk-bar { display: flex; width: 100%; height: 10px; border-radius: 999px; overflow: hidden; background: var(--bg); margin-bottom: 20px; }
.risk-bar-segment { height: 100%; transition: width 0.3s ease; }
.risk-metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.risk-metric { padding: 16px; border-radius: 14px; background: var(--bg); border: 1px solid var(--border); }
.risk-metric-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.risk-metric-label { font-size: 13px; color: var(--text-muted); font-weight: 600; }
.risk-metric-pct { margin-left: auto; font-size: 12px; font-weight: 700; color: var(--text-faint); }
.risk-metric-value { font-size: 26px; font-weight: 800; letter-spacing: -0.01em; }

.approved-amount { font-size: 32px; font-weight: 800; margin: 4px 0 20px; }
.bar-chart { display: flex; align-items: flex-end; gap: 14px; height: 160px; }
.bar-chart-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 8px; height: 100%; justify-content: flex-end; }
.bar-chart-bar {
  width: 100%; max-width: 34px; border-radius: 6px 6px 2px 2px;
  background: linear-gradient(180deg, var(--ipesa-red) 0%, var(--ipesa-green) 100%);
  min-height: 2px;
}
.bar-chart-day { color: var(--text-muted); font-size: 12px; }

/* ---------- Bottom totals + area chart ---------- */

.totals-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.totals-row { display: flex; justify-content: space-between; align-items: flex-start; flex-wrap: wrap; gap: 20px; margin-bottom: 12px; }
.totals-figures { display: flex; gap: 56px; }
.totals-figure-label { color: var(--text-muted); font-size: 14px; margin-bottom: 6px; }
.totals-figure-value { font-size: 32px; font-weight: 800; }
.totals-figure-value .cents { font-size: 18px; color: var(--text-muted); }
.chart-toggle { display: flex; gap: 8px; }
.chart-toggle button {
  background: var(--bg); border: 1px solid var(--border); color: var(--text-muted);
  border-radius: 8px; padding: 8px 16px; font-size: 13px; font-weight: 600;
  cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.chart-toggle button.active { background: var(--bg-card-hover); color: var(--text); }

.legend-row { display: flex; gap: 24px; flex-wrap: wrap; padding: 16px 0; border-bottom: 1px solid var(--border); margin-bottom: 16px; font-size: 13px; }
.legend-row .all-tab { font-weight: 700; border-bottom: 2px solid var(--text); padding-bottom: 16px; margin-bottom: -17px; }
.legend-row .legend-item { display: flex; align-items: center; gap: 8px; color: var(--text-muted); }

.area-chart-wrap { position: relative; }
.area-chart-wrap svg { width: 100%; height: 200px; display: block; }
.area-chart-axis { display: flex; justify-content: space-between; color: var(--text-faint); font-size: 12px; margin-top: 8px; }

.daily-bar-chart { display: flex; align-items: flex-end; gap: 8px; height: 200px; }
.daily-bar-chart .bar-chart-col { flex: 1; }
.daily-bar-chart .bar-chart-bar { max-width: 100%; background: linear-gradient(180deg, var(--ipesa-green) 0%, var(--ipesa-red) 100%); }
.daily-bar-chart .bar-chart-day { font-size: 10px; white-space: nowrap; }

.hidden { display: none !important; }

/* ---------- Generic content pages (users/loans/content/cards) ---------- */

h2.section-title { font-size: 16px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin: 32px 0 16px; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; }
.card-tile {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 14px;
  overflow: hidden; text-decoration: none; color: inherit; display: block; box-shadow: var(--shadow);
}
.card-tile:hover { background: var(--bg-card-hover); }
.card-tile .thumb {
  height: 120px; background: #141414 center/cover no-repeat;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-faint); font-size: 12px;
}
.card-tile .thumb img { width: 100%; height: 100%; object-fit: cover; }
.card-tile .info { padding: 14px 16px; }
.card-tile .title { font-weight: 600; font-size: 14px; }
.card-tile .meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.badge.active { background: rgba(63,201,30,0.15); color: var(--ipesa-green); }
.badge.inactive { background: rgba(255,255,255,0.08); color: var(--text-faint); }
.badge.status-PENDING { background: rgba(232,168,60,0.15); color: #E8A83C; }
.badge.status-GRANTED { background: rgba(63,201,30,0.15); color: var(--ipesa-green); }
.badge.status-PAID { background: rgba(62,143,214,0.15); color: #3E8FD6; }
.badge.status-REJECTED { background: rgba(234,57,47,0.15); color: var(--ipesa-red); }
.badge.status-SUCCESS { background: rgba(63,201,30,0.15); color: var(--ipesa-green); }
.badge.status-FAILED { background: rgba(234,57,47,0.15); color: var(--ipesa-red); }
.badge.status-APPROVED { background: rgba(63,201,30,0.15); color: var(--ipesa-green); }
.badge.platform { background: rgba(62,143,214,0.15); color: #3E8FD6; }

.data-table { width: 100%; border-collapse: collapse; background: var(--bg-card); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; font-size: 13px; box-shadow: var(--shadow); }
.data-table th, .data-table td { text-align: left; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.data-table th { color: var(--text-muted); font-weight: 600; text-transform: uppercase; font-size: 11px; letter-spacing: 0.03em; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr[onclick] { cursor: pointer; }
.data-table tbody tr[onclick]:hover { background: var(--bg-card-hover); }

.kv-table { width: 100%; border-collapse: collapse; }
.kv-table td { padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.kv-table td:first-child { color: var(--text-muted); width: 40%; }
.kv-edit-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.kv-edit-input {
  flex: 1; background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 6px 10px; font-size: 14px;
}
.kv-edit-toggle { font-size: 12px; color: var(--text-faint); text-decoration: none; flex-shrink: 0; padding-right: 4px; }
.kv-edit-toggle:hover { text-decoration: underline; color: var(--text-muted); }
.text-success { color: var(--ipesa-green); }
.text-danger { color: var(--ipesa-red); }

.filter-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; align-items: center; }
.filter-chip { padding: 7px 16px; border-radius: 999px; background: var(--bg-card); border: 1px solid var(--border); color: var(--text-muted); text-decoration: none; font-size: 12px; font-weight: 600; }
.filter-chip.selected { background: var(--ipesa-red); border-color: var(--ipesa-red); color: #fff; }
.filter-row select, .filter-row input[type="number"] {
  background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 7px 10px; font-size: 13px;
}

.edit-form { background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 28px; max-width: 480px; box-shadow: var(--shadow); }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; margin-bottom: 8px; }
.stats-grid-2x2 { grid-template-columns: repeat(2, 1fr); margin-top: 18px; margin-bottom: 0; }
.detail-grid .edit-form { max-width: none; width: 100%; }

.detail-header-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.detail-actions-row { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.profile-picture-row { display: flex; align-items: center; gap: 20px; margin-bottom: 8px; }
.profile-picture {
  width: 88px; height: 88px; border-radius: 50%; overflow: hidden; flex-shrink: 0;
  background: linear-gradient(135deg, var(--ipesa-green), var(--ipesa-red));
  color: #fff; font-weight: 700; font-size: 28px;
  display: flex; align-items: center; justify-content: center;
}
.profile-picture img { width: 100%; height: 100%; object-fit: cover; }
.current-image { width: 100%; max-width: 240px; border-radius: 10px; margin: 8px 0; display: block; }
.checkbox-row { display: flex; align-items: center; gap: 8px; margin-top: 16px; }
.checkbox-row input { width: auto; }
.back-link { display: inline-block; margin-bottom: 16px; font-size: 13px; color: var(--ipesa-red); text-decoration: none; }

label { display: block; font-size: 13px; font-weight: 600; margin: 16px 0 6px; color: var(--text-muted); }
input[type=email], input[type=password], input[type=text], input[type=number], textarea {
  width: 100%; padding: 11px 14px; border: 1px solid var(--border); border-radius: 10px;
  font-size: 14px; background: var(--bg); color: var(--text);
}
textarea { font-family: inherit; }
button, input[type=submit] {
  background: var(--ipesa-red); color: #fff; border: none; border-radius: 10px;
  padding: 11px 22px; font-size: 14px; font-weight: 600; cursor: pointer; margin-top: 20px;
}
.btn-small { margin-top: 0; padding: 7px 14px; font-size: 12px; }
.btn-reject { background: #3a3a3a; margin-left: 6px; }
.error { color: var(--ipesa-red); font-size: 13px; margin-top: 8px; }
.success { color: var(--ipesa-green); font-size: 13px; margin-top: 8px; }

/* ---------- Login (pre-auth, standalone) ---------- */

.login-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px; background: var(--bg); }
.login-footer { color: var(--text-faint); font-size: 12px; margin: 0; }
.login-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 20px; padding: 40px; width: 100%; max-width: 360px; box-shadow: var(--shadow); }
.login-logo { display: block; height: 44px; width: auto; margin: 0 auto 20px; }
.login-card button { width: 100%; text-align: center; }
.login-card h1 { color: var(--text); font-size: 22px; margin-bottom: 24px; text-align: center; }
.login-card h1 span { color: var(--ipesa-red); }

/* ---------- Responsive: phones / tablets / laptops / desktops / TV ---------- */

.table-scroll { overflow-x: auto; }

.menu-toggle {
  display: none;
  width: 40px; height: 40px; border-radius: 10px; flex-shrink: 0;
  background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
  align-items: center; justify-content: center; cursor: pointer; margin: 0; padding: 0;
}
.menu-toggle svg { width: 20px; height: 20px; }

.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5); z-index: 90;
}

/* Large desktop / TV — the fixed 1400px content cap starts feeling small on
   really wide displays, so let it breathe (and widen the rail slightly to
   match) instead of leaving a wall of empty space either side. */
@media (min-width: 1800px) {
  .page-content { max-width: 1800px; }
  .sidebar { width: 268px; }
}

/* Tablet */
@media (max-width: 1024px) {
  .chart-row { grid-template-columns: 1fr; }
  .sidebar { width: 200px; }
}

/* Small tablet / large phone */
@media (max-width: 780px) {
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .totals-figures { gap: 32px; }
}

/* Phone — sidebar becomes an off-canvas drawer triggered by the topbar's
   hamburger button (see partials/sidebar.hbs + partials/topbar.hbs). */
@media (max-width: 640px) {
  .app-shell { flex-direction: column; }
  .detail-grid { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed; top: 0; left: 0; width: 260px; height: 100vh; z-index: 100;
    transform: translateX(-100%); transition: transform 0.25s ease;
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-backdrop {
    display: block; opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
  }
  .sidebar-backdrop.open { opacity: 1; pointer-events: auto; }

  .menu-toggle { display: flex; }

  .topbar { padding: 14px 16px; gap: 12px; min-width: 0; }
  .search-form { max-width: none; min-width: 0; }
  .search-form input { min-width: 0; }
  .topbar-right { flex-shrink: 0; }

  .page-content { padding: 20px 16px; max-width: 100%; }
  .page-header { flex-direction: column; align-items: flex-start; }
  .page-greeting { font-size: 24px; }
  .page-header-actions { width: 100%; }
  .page-header-actions a { flex: 1; min-width: 0; text-align: center; }

  .stats-row { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .stat-card { padding: 16px 10px; }
  .stat-label { font-size: 12px; margin-bottom: 8px; }
  .stat-value { font-size: 24px; }

  .donut-wrap { flex-direction: column; align-items: flex-start; gap: 16px; }
  .bar-chart { gap: 8px; }

  .risk-metrics { grid-template-columns: 1fr; gap: 10px; }
  .risk-metric-value { font-size: 22px; }

  .totals-row { flex-direction: column; align-items: stretch; }
  .totals-figures { gap: 24px; }
  .chart-toggle { align-self: flex-start; }

  .legend-row { gap: 14px; }
}
