/*
 * AmanaPlus - application stylesheet (loads after bootstrap.min.css).
 * Layout vocabulary (header / sidebar / nav-menu / nav-submenu / cards /
 * split auth page) follows the AppDashboard conventions, implemented here
 * from scratch on top of Bootstrap 5.3 colour modes.
 *
 *  1. Tokens (light + dark)        5. Cards, KPI tiles, page header
 *  2. Bootstrap brand overrides    6. Auth (split) pages
 *  3. App shell: header, sidebar   7. States: loader, access, read-only
 *  4. Sidebar internals            8. DataTables, charts, utilities
 */

/* ---------- 1. Tokens ------------------------------------------------- */
:root {
  --brand: #0f766e;
  --brand-rgb: 15, 118, 110;
  --brand-hover: #0b5f58;
  --accent: #d99a1e;
  --accent-rgb: 217, 154, 30;

  --header-height: 64px;
  --sidebar-width: 268px;
  --sidebar-collapsed-width: 76px;
  --radius: .75rem;
  --transition: .2s ease;

  --surface: #ffffff;
  --surface-2: #f4f7f8;
  --surface-border: #e3e9ec;
  --sidebar-bg: #ffffff;
  --sidebar-text: #44525b;
  --sidebar-hover: rgba(var(--brand-rgb), .08);
  --sidebar-active-bg: rgba(var(--brand-rgb), .12);
  --sidebar-active-text: var(--brand);
  --heading-muted: #8696a0;
  --shadow-sm: 0 1px 2px rgba(16, 24, 32, .06);
  --shadow-md: 0 6px 20px rgba(16, 24, 32, .08);
  --auth-panel: linear-gradient(155deg, #0b4f4a 0%, #0f766e 55%, #14a89a 100%);
  --chart-grid: rgba(16, 24, 32, .08);
  --chart-text: #5b6a73;
}

[data-bs-theme="dark"] {
  --brand: #2bb3a3;
  --brand-rgb: 43, 179, 163;
  --brand-hover: #4cc7b8;
  --accent: #e8b04a;
  --accent-rgb: 232, 176, 74;

  --surface: #172127;
  --surface-2: #0f171b;
  --surface-border: #26343c;
  --sidebar-bg: #121b20;
  --sidebar-text: #aebcc4;
  --sidebar-hover: rgba(255, 255, 255, .06);
  --sidebar-active-bg: rgba(var(--brand-rgb), .16);
  --sidebar-active-text: #5fd6c8;
  --heading-muted: #6f8089;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 6px 20px rgba(0, 0, 0, .45);
  --auth-panel: linear-gradient(155deg, #0a2f2c 0%, #0b4f4a 55%, #0f766e 100%);
  --chart-grid: rgba(255, 255, 255, .08);
  --chart-text: #93a4ad;

  --bs-body-bg: #0f171b;
  --bs-body-bg-rgb: 15, 23, 27;
  --bs-tertiary-bg: #172127;
  --bs-tertiary-bg-rgb: 23, 33, 39;
  --bs-secondary-bg: #1d2a31;
  --bs-secondary-bg-rgb: 29, 42, 49;
  --bs-border-color: #26343c;
  --bs-border-color-translucent: rgba(255, 255, 255, .1);
}

/* ---------- 2. Bootstrap brand overrides ------------------------------ */
:root,
[data-bs-theme="dark"] {
  --bs-primary: var(--brand);
  --bs-primary-rgb: var(--brand-rgb);
  --bs-link-color: var(--brand);
  --bs-link-color-rgb: var(--brand-rgb);
  --bs-link-hover-color: var(--brand-hover);
  --bs-focus-ring-color: rgba(var(--brand-rgb), .25);
  --bs-border-radius: .5rem;
  --bs-border-radius-lg: var(--radius);
}

[data-bs-theme="light"] {
  --bs-body-bg: #f4f7f8;
  --bs-body-bg-rgb: 244, 247, 248;
}

body {
  background: var(--bs-body-bg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.btn-primary {
  --bs-btn-bg: var(--brand);
  --bs-btn-border-color: var(--brand);
  --bs-btn-hover-bg: var(--brand-hover);
  --bs-btn-hover-border-color: var(--brand-hover);
  --bs-btn-active-bg: var(--brand-hover);
  --bs-btn-active-border-color: var(--brand-hover);
  --bs-btn-disabled-bg: var(--brand);
  --bs-btn-disabled-border-color: var(--brand);
  --bs-btn-color: #fff;
  --bs-btn-hover-color: #fff;
  --bs-btn-active-color: #fff;
}

[data-bs-theme="dark"] .btn-primary {
  --bs-btn-color: #06201d;
  --bs-btn-hover-color: #06201d;
  --bs-btn-active-color: #06201d;
}

.btn-outline-primary {
  --bs-btn-color: var(--brand);
  --bs-btn-border-color: var(--brand);
  --bs-btn-hover-bg: var(--brand);
  --bs-btn-hover-border-color: var(--brand);
  --bs-btn-active-bg: var(--brand);
  --bs-btn-active-border-color: var(--brand);
  --bs-btn-hover-color: #fff;
  --bs-btn-active-color: #fff;
}

[data-bs-theme="dark"] .btn-outline-primary {
  --bs-btn-hover-color: #06201d;
  --bs-btn-active-color: #06201d;
}

.form-control:focus, .form-select:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 .2rem rgba(var(--brand-rgb), .2);
}

.form-check-input:checked {
  background-color: var(--brand);
  border-color: var(--brand);
}

.text-brand { color: var(--brand) !important; }
.bg-brand-subtle { background: rgba(var(--brand-rgb), .12) !important; color: var(--brand) !important; }

/* ---------- 3. App shell: header + content ---------------------------- */
.header {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 1030;
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: 0 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--surface-border);
  transition: left var(--transition);
}

@media (min-width: 1200px) {
  .header { left: var(--sidebar-width); padding: 0 1.5rem; }
  .sidebar-collapsed .header { left: var(--sidebar-collapsed-width); }
}

.header .header-btn {
  width: 40px;
  height: 40px;
  display: inline-grid;
  place-items: center;
  border: 0;
  border-radius: .5rem;
  background: transparent;
  color: var(--bs-body-color);
  font-size: 1.15rem;
  position: relative;
}

.header .header-btn:hover { background: var(--sidebar-hover); }
.header .header-btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.header .header-spacer { flex: 1 1 auto; }
.header .user-chip { display: flex; align-items: center; gap: .6rem; padding: .25rem .5rem; border-radius: .5rem; border: 0; background: transparent; color: inherit; }
.header .user-chip:hover { background: var(--sidebar-hover); }
.avatar-initial { display: grid; place-items: center; font-weight: 700; }
.text-xs { font-size: .7rem; }
.avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: var(--surface-2); }

/* Theme toggle: show the icon of the theme you would switch TO. */
[data-theme-toggle] .theme-icon-to-light { display: none; }
[data-theme-toggle] .theme-icon-to-dark { display: inline; }
[data-bs-theme="dark"] [data-theme-toggle] .theme-icon-to-light { display: inline; }
[data-bs-theme="dark"] [data-theme-toggle] .theme-icon-to-dark { display: none; }

.app-main {
  min-height: 100vh;
  padding: calc(var(--header-height) + 1.5rem) 1rem 1.5rem;
  transition: margin-left var(--transition);
}

@media (min-width: 1200px) {
  .app-main { margin-left: var(--sidebar-width); padding-left: 1.5rem; padding-right: 1.5rem; }
  .sidebar-collapsed .app-main { margin-left: var(--sidebar-collapsed-width); }
}

.app-footer {
  padding: 1rem 0 0;
  color: var(--heading-muted);
  font-size: .8125rem;
}

/* ---------- 4. Sidebar ------------------------------------------------ */
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 1040;
  width: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--surface-border);
  transform: translateX(-100%);
  transition: transform var(--transition), width var(--transition);
}

.sidebar-open .sidebar { transform: none; box-shadow: var(--shadow-md); }

.sidebar-overlay {
  position: fixed;
  inset: 0;
  z-index: 1035;
  background: rgba(8, 14, 18, .55);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition), visibility var(--transition);
}

.sidebar-open .sidebar-overlay { opacity: 1; visibility: visible; }

@media (min-width: 1200px) {
  .sidebar { transform: none; box-shadow: none; }
  .sidebar-overlay { display: none; }
  .sidebar-close { display: none !important; }
  .sidebar-collapsed .sidebar { width: var(--sidebar-collapsed-width); }
}

@media (max-width: 1199.98px) {
  .sidebar-toggle-desktop-only { display: none; }
}

.sidebar-header {
  height: var(--header-height);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  border-bottom: 1px solid var(--surface-border);
}

.sidebar-logo { display: flex; align-items: center; gap: .65rem; text-decoration: none; color: inherit; min-width: 0; }
.sidebar-logo-mark { width: 38px; height: 38px; flex: 0 0 38px; display: grid; place-items: center; }
.sidebar-logo-mark img { width: 100%; height: 100%; }
.sidebar-logo-text { display: flex; flex-direction: column; line-height: 1.15; white-space: nowrap; }
.sidebar-logo-name { font-weight: 700; font-size: 1.1rem; letter-spacing: -.01em; }
.sidebar-logo-name b { color: var(--accent); font-weight: 700; }
.sidebar-logo-label { font-size: .7rem; color: var(--heading-muted); text-transform: uppercase; letter-spacing: .08em; }

.sidebar-close { border: 0; background: transparent; color: inherit; width: 36px; height: 36px; border-radius: .5rem; }

.sidebar-nav { flex: 1 1 auto; overflow-y: auto; overflow-x: hidden; padding: .75rem .75rem 1.25rem; scrollbar-width: thin; }

.nav-menu, .nav-submenu { list-style: none; margin: 0; padding: 0; }
.nav-menu > .nav-item + .nav-item { margin-top: .15rem; }

.sidebar .nav-link {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .6rem .75rem;
  border-radius: .55rem;
  color: var(--sidebar-text);
  font-size: .9rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}

.sidebar .nav-link > i:first-child { flex: 0 0 22px; font-size: 1.1rem; text-align: center; }
.sidebar .nav-link > span { flex: 1 1 auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar .nav-link:hover { background: var(--sidebar-hover); color: var(--bs-body-color); }
.sidebar .nav-link:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }
.sidebar .nav-link.active { background: var(--sidebar-active-bg); color: var(--sidebar-active-text); font-weight: 600; }

.nav-arrow { flex: 0 0 auto; font-size: .7rem; transition: transform var(--transition); }
.has-submenu.open > .nav-link .nav-arrow { transform: rotate(180deg); }

.nav-submenu { display: none; margin: .15rem 0 .35rem 1.35rem; padding-left: .6rem; border-left: 1px solid var(--surface-border); }
.has-submenu.open > .nav-submenu { display: block; }
.nav-submenu .nav-link { padding: .45rem .75rem; font-size: .86rem; font-weight: 450; gap: .6rem; }
.nav-submenu .nav-link > i:first-child { flex-basis: 18px; font-size: .95rem; opacity: .85; }

.nav-heading { padding: 1rem .75rem .35rem; font-size: .68rem; font-weight: 600; text-transform: uppercase; letter-spacing: .1em; color: var(--heading-muted); }

/* Access level marker shown next to an item (VIEW = read-only). */
.nav-badge { font-size: .62rem; font-weight: 600; padding: .1rem .4rem; border-radius: 1rem; background: var(--surface-2); color: var(--heading-muted); }

.sidebar-footer { flex: 0 0 auto; padding: .75rem 1rem; border-top: 1px solid var(--surface-border); font-size: .75rem; color: var(--heading-muted); }

/* Icon rail (desktop only) */
@media (min-width: 1200px) {
  .sidebar-collapsed .sidebar-header { justify-content: center; padding: 0; }
  .sidebar-collapsed .sidebar-logo-text,
  .sidebar-collapsed .sidebar .nav-link > span,
  .sidebar-collapsed .sidebar .nav-arrow,
  .sidebar-collapsed .sidebar .nav-badge,
  .sidebar-collapsed .sidebar-footer,
  .sidebar-collapsed .sidebar .nav-heading { display: none; }
  .sidebar-collapsed .sidebar-nav { padding: .75rem .5rem; }
  .sidebar-collapsed .sidebar .nav-link { justify-content: center; padding: .65rem 0; }
  .sidebar-collapsed .sidebar .nav-submenu { display: none !important; }
}

/* ---------- 5. Cards, KPI tiles, page header -------------------------- */
.card {
  --bs-card-bg: var(--surface);
  --bs-card-border-color: var(--surface-border);
  --bs-card-border-radius: var(--radius);
  --bs-card-cap-bg: transparent;
  box-shadow: var(--shadow-sm);
}

.card-header { font-weight: 600; }

.page-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: 1.25rem; }
.page-header h1 { font-size: 1.4rem; font-weight: 700; margin: 0; letter-spacing: -.01em; }
.page-header .breadcrumb { margin: .15rem 0 0; font-size: .8125rem; }

.kpi-card { position: relative; overflow: hidden; }
.kpi-card .kpi-icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: .75rem; font-size: 1.3rem; background: rgba(var(--brand-rgb), .12); color: var(--brand); }
.kpi-card.kpi-accent .kpi-icon { background: rgba(var(--accent-rgb), .16); color: var(--accent); }
.kpi-card.kpi-danger .kpi-icon { background: rgba(var(--bs-danger-rgb), .14); color: var(--bs-danger); }
.kpi-card .kpi-label { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--heading-muted); font-weight: 600; }
.kpi-card .kpi-value { font-size: clamp(1.2rem, .9rem + 1vw, 1.65rem); font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; white-space: nowrap; }
.kpi-card .kpi-delta { font-size: .8rem; }

/* Money and ids line up in tables */
.num, td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- 6. Auth (split layout) ------------------------------------ */
.auth-split { min-height: 100vh; display: grid; grid-template-columns: 1fr; background: var(--bs-body-bg); }
.auth-panel { display: none; position: relative; overflow: hidden; padding: 3rem; color: #fff; background: var(--auth-panel); }
.auth-panel .auth-art { position: absolute; inset: auto 0 0 0; width: 100%; opacity: .95; pointer-events: none; }
.auth-panel .auth-brand { display: flex; align-items: center; gap: .75rem; font-size: 1.35rem; font-weight: 700; }
.auth-panel .auth-brand img { width: 44px; height: 44px; }
.auth-panel h2 { font-size: 2rem; font-weight: 700; letter-spacing: -.02em; max-width: 22ch; margin-top: 3rem; }
.auth-panel p { max-width: 42ch; color: rgba(255, 255, 255, .8); }
.auth-panel ul { list-style: none; padding: 0; margin: 1.5rem 0 0; display: grid; gap: .6rem; }
.auth-panel li i { color: var(--accent); margin-right: .5rem; }
.auth-form-wrap { display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 2rem 1.25rem; }
.auth-form { width: 100%; max-width: 400px; }
.auth-form .auth-mobile-brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.25rem; margin-bottom: 2rem; }
.auth-form .auth-mobile-brand img { width: 38px; height: 38px; }

@media (min-width: 992px) {
  .auth-split { grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr); }
  .auth-panel { display: block; }
  .auth-form .auth-mobile-brand { display: none; }
}

.auth-theme-toggle { position: fixed; top: .75rem; right: .75rem; z-index: 10; }

/* ---------- 7. States: loader, access, read-only ---------------------- */
.app-loader { display: grid; place-items: center; min-height: 40vh; color: var(--heading-muted); }
.app-loader .spinner-border { color: var(--brand); }

.state-page { text-align: center; padding: 4rem 1rem; max-width: 520px; margin: 0 auto; }
.state-page .state-icon { font-size: 3.5rem; color: var(--heading-muted); }
.state-page h2 { font-weight: 700; margin-top: 1rem; }

/* Controls the user lacks access to. auth.js adds .no-access (hidden) or
   .is-readonly (visible but inert) according to data-requires="MODULE:LEVEL". */
.no-access { display: none !important; }
.is-readonly { pointer-events: none; opacity: .55; }
form.is-readonly .form-control,
form.is-readonly .form-select { background: var(--surface-2); }

/* Brand-coloured ledger statuses */
.badge-status { font-weight: 600; letter-spacing: .02em; }

/* ---------- 8. DataTables, charts, utilities -------------------------- */
.table { --bs-table-bg: transparent; --bs-table-border-color: var(--surface-border); }
.table > thead > tr > th { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--heading-muted); font-weight: 600; white-space: nowrap; }
div.dt-container .dt-search input,
div.dt-container .dt-length select { background-color: var(--surface); }
div.dt-container .page-link { color: var(--brand); }
div.dt-container .page-item.active .page-link { background: var(--brand); border-color: var(--brand); color: #fff; }
[data-bs-theme="dark"] div.dt-container .page-item.active .page-link { color: #06201d; }

.chart-box { position: relative; height: 280px; }
.chart-box.chart-sm { height: 200px; }

@media (prefers-reduced-motion: reduce) {
  .sidebar, .sidebar-overlay, .header, .app-main, .nav-arrow, .sidebar .nav-link { transition: none; }
}

/* ---------- 9. Admin pages ------------------------------------------- */
.matrix .table-group-row th { background: var(--surface-2); }
.matrix input[type=checkbox] { width: 1.15rem; height: 1.15rem; cursor: pointer; }
.matrix input[type=checkbox]:disabled { cursor: not-allowed; }
pre.audit-json { background: var(--surface-2); border: 1px solid var(--surface-border); border-radius: .5rem; padding: .75rem 1rem; font-size: .8125rem; max-height: 50vh; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.btn[disabled], .btn:disabled { cursor: not-allowed; }

/* Wide tables scroll inside their own box instead of pushing the whole page sideways (phones). */
div.dt-container .dt-layout-full { overflow-x: auto; }
div.dt-container .dt-layout-full > table { min-width: 36rem; }

.col-share { width: 7rem; }


/* ---- savings ---------------------------------------------------------------- */
.picker-list { z-index: 1056; max-height: 14rem; overflow-y: auto; }
.picker-list .list-group-item { cursor: pointer; }
.receipt-amount { font-size: 1.9rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.amount-credit { color: var(--bs-success); }
.amount-debit { color: var(--bs-danger); }
td.amount, th.amount { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-reversed td { opacity: .6; }
@media print {
  body * { visibility: hidden !important; }
  .receipt-print, .receipt-print *, .report-print, .report-print * { visibility: visible !important; }
  .report-print { position: absolute; left: 0; top: 0; width: 100%; }
  .modal { position: static !important; display: block !important; }
  .modal-dialog { margin: 0 auto !important; max-width: 380px !important; }
  .modal-content { border: 0 !important; box-shadow: none !important; background: #fff !important; color: #000 !important; }
  .modal-backdrop { display: none !important; }
  .receipt-print { position: absolute; left: 0; top: 0; width: 100%; background: #fff !important; color: #000 !important; }
}

/* ---- header organisation name ---- */
.header-org { font-weight: 700; color: var(--brand); max-width: 45vw; letter-spacing: -.01em; }
.header-sep { color: var(--heading-muted); margin: 0 .5rem; }

/* ---- loans ---- */
.timeline { position: relative; padding-left: 1.1rem; border-left: 2px solid var(--bs-border-color); }
.timeline li { position: relative; padding-bottom: 1rem; }
.timeline li:last-child { padding-bottom: 0; }
.timeline li::before { content: ''; position: absolute; left: calc(-1.1rem - 6px); top: .35rem; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); }
.preview-figures { display: flex; flex-wrap: wrap; gap: .25rem 1.5rem; }
.preview-figures b { font-variant-numeric: tabular-nums; }

/* ---- arrears tiles and charts ---- */
.tile-btn { border: 1px solid var(--bs-border-color); background: var(--bs-body-bg); cursor: pointer; padding: 0; }
.tile-btn:hover, .tile-btn:focus-visible { border-color: var(--brand); }

.viz-root {
  color-scheme: light;
  --viz-surface: #fcfcfb; --viz-text: #52514e; --viz-text-strong: #0b0b0b; --viz-grid: rgba(11, 11, 11, .08);
  --series-1: #2a78d6;  /* blue   */
  --series-2: #eb6834;  /* orange */
}
:root[data-bs-theme="dark"] .viz-root {
  color-scheme: dark;
  --viz-surface: #1a1a19; --viz-text: #c3c2b7; --viz-text-strong: #f0efec; --viz-grid: rgba(240, 239, 236, .10);
  --series-1: #3987e5; --series-2: #d95926;
}
.viz-card .viz-head { display: flex; align-items: baseline; gap: .5rem; }
.viz-card .viz-title { font-size: .95rem; font-weight: 600; margin: 0; }
.viz-card .viz-sub { color: var(--viz-text); font-size: .8rem; }
.viz-canvas-wrap { position: relative; height: 260px; }
.viz-table { display: none; max-height: 260px; overflow: auto; }
.viz-card.show-table .viz-table { display: block; }
.viz-card.show-table .viz-canvas-wrap { display: none; }
.setting-narrow { max-width: 10rem; }

.idle-warning { position: fixed; top: 1rem; left: 50%; transform: translateX(-50%); z-index: 2000; max-width: 26rem; }
