:root {
  --blue: #1d4ed8;
  --blue-dark: #1e3a8a;
  --green: #15803d;
  --red: #b91c1c;
  --amber: #b45309;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-400: #94a3b8;
  --gray-600: #475569;
  --gray-800: #1e293b;

  /* Theme tokens — light by default. Dark overrides these, never the
     rules that use them, so every component stays theme-aware for free. */
  --bg: var(--gray-50);
  --surface: #ffffff;
  --surface-2: var(--gray-50);
  --text: var(--gray-800);
  --text-muted: var(--gray-600);
  --border: var(--gray-200);
  --border-light: var(--gray-100);
  --shadow: rgba(0,0,0,0.06);
  --shadow-strong: rgba(0,0,0,0.18);
  --overlay: rgba(0,0,0,0.4);
  /* Used where --blue-dark sits as text directly on --surface (headings,
     stat numbers) — needs to lighten in dark mode for contrast. Kept
     separate from --blue-dark itself, which stays put for button
     backgrounds and badge text, where the original navy still works fine
     against their own always-light chip backgrounds. */
  --accent-text: var(--blue-dark);
  --row-extension-bg: #fffbeb;
  --row-overdue-bg: #fef2f2;

  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

:root[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #263449;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #334155;
  --border-light: #2b3a51;
  --shadow: rgba(0,0,0,0.35);
  --shadow-strong: rgba(0,0,0,0.55);
  --overlay: rgba(0,0,0,0.6);
  --accent-text: #7fb1f7;
  --row-extension-bg: #3f371a;
  --row-overdue-bg: #3f2323;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #263449;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --border: #334155;
    --border-light: #2b3a51;
    --shadow: rgba(0,0,0,0.35);
    --shadow-strong: rgba(0,0,0,0.55);
    --overlay: rgba(0,0,0,0.6);
    --accent-text: #7fb1f7;
    --row-extension-bg: #3f371a;
    --row-overdue-bg: #3f2323;
  }
}

:root[data-theme="light"] {
  --accent-text: var(--blue-dark);
  --row-extension-bg: #fffbeb;
  --row-overdue-bg: #fef2f2;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 10% 10%, rgba(29,78,216,0.28), transparent 45%),
    radial-gradient(circle at 90% 15%, rgba(21,128,61,0.22), transparent 40%),
    radial-gradient(circle at 50% 100%, rgba(29,78,216,0.22), transparent 55%);
  background-attachment: fixed;
  color: var(--text);
}

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--surface);
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 4px 24px var(--shadow), 0 12px 40px rgba(29,58,138,0.12);
  width: 340px;
}

.login-card h1 {
  font-size: 20px;
  margin: 0 0 4px;
  color: var(--accent-text);
}

.login-card p.sub {
  margin: 0 0 24px;
  color: var(--text-muted);
  font-size: 13px;
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-muted);
}

input, select, textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  margin-bottom: 14px;
  background: var(--surface);
  color: var(--text);
}

input[readonly] {
  background: var(--surface-2);
  color: var(--text-muted);
}

.password-field {
  position: relative;
}
.password-field input {
  padding-right: 40px;
}
.password-toggle {
  position: absolute;
  right: 4px;
  top: 0;
  height: 38px;
  width: 32px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: var(--text-muted);
  padding: 0;
}
.password-toggle:hover {
  color: var(--text);
}

/* Hide the up/down spinner arrows on number inputs — cleaner look, and the
   arrows aren't useful for currency/rate/day-count values people type in.
   Needs both rules: the pseudo-elements for Chrome/Edge, and -webkit-appearance
   directly on the input for Safari, which doesn't reliably respect the first
   rule on its own. */
input[type="number"] {
  -webkit-appearance: none;
  -moz-appearance: textfield;
  appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.loan-preview {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}
.loan-preview strong { color: var(--text); }

button {
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
}

button:disabled {
  cursor: default;
  opacity: 0.6;
}

.btn-primary { background: var(--blue); color: white; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-secondary { background: var(--border-light); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: #fee2e2; color: var(--red); }
.btn-danger:hover { background: #fecaca; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

.detail-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
.detail-actions button {
  flex: 1;
  white-space: nowrap;
}

.error-msg {
  background: #fee2e2;
  color: var(--red);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  margin-bottom: 14px;
}

/* --- App shell --- */
header.app-header {
  background: linear-gradient(120deg, var(--blue-dark) 0%, var(--blue) 100%);
  box-shadow: 0 2px 12px rgba(29,58,138,0.25);
  color: white;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header.app-header h1 { font-size: 17px; margin: 0; }
header.app-header .who { font-size: 13px; opacity: 0.9; }

main {
  max-width: 1100px;
  margin: 24px auto;
  padding: 0 20px;
}

.app-layout {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 20px;
}
.app-layout main {
  max-width: 1100px;
  margin: 0;
  padding: 0;
  min-width: 0;
}

.search-row {
  display: flex;
  justify-content: center;
  margin-bottom: 14px;
}
.search-row input {
  width: 100%;
  max-width: 420px;
  margin-bottom: 0;
  text-align: center;
}

.card {
  background: var(--surface);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px var(--shadow);
}

.card h2 {
  font-size: 15px;
  margin: 0 0 16px;
  color: var(--text);
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  gap: 10px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0 16px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-light);
}

th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.02em; }

tr.extension { background: var(--row-extension-bg); }
tr.overdue { background: var(--row-overdue-bg); }
tr.paid { opacity: 0.6; }

.clickable-row {
  cursor: pointer;
  transition: background-color 0.12s ease, transform 0.08s ease;
}
.clickable-row:hover { background-color: var(--border-light); }
.clickable-row:active { transform: scale(0.99); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}
.badge-active { background: #dbeafe; color: var(--blue-dark); }
.badge-extension { background: #fef3c7; color: var(--amber); }
.badge-overdue { background: #fee2e2; color: var(--red); }
.badge-paid { background: #dcfce7; color: var(--green); }

.row-actions { display: flex; gap: 6px; }

.hidden { display: none !important; }

.modal-backdrop {
  position: fixed; inset: 0; background: var(--overlay);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal {
  background: var(--surface); color: var(--text); border-radius: 10px; padding: 24px;
  width: 420px; max-width: 92vw; max-height: 85vh; overflow-y: auto;
}
.modal h3 { margin-top: 0; font-size: 16px; }
.modal-wide { width: 620px; }

@media (max-width: 480px) {
  .modal { padding: 16px; }
  .form-grid { grid-template-columns: 1fr !important; gap: 0 !important; }
}

.muted { color: var(--text-muted); font-size: 12px; }

.detail-notes {
  margin: -6px 0 14px;
  padding: 8px 10px;
  font-size: 13px;
  background: var(--border-light);
  border-radius: 6px;
  white-space: pre-wrap;
}

.stat-row { display: flex; gap: 16px; margin-bottom: 14px; }
.stat {
  background: var(--surface); border-radius: 10px; padding: 12px 20px; flex: 1;
  box-shadow: 0 1px 3px var(--shadow);
}
.stat .num { font-size: 22px; font-weight: 700; color: var(--accent-text); }
.stat .label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.stat.overdue .num { color: var(--red); }

.link-btn {
  background: none; border: none; color: var(--blue); font-size: 13px; padding: 0; font-weight: 600;
}

.icon-btn {
  background: none; border: none; font-size: 15px; padding: 2px 6px; border-radius: 6px; line-height: 1;
}
.icon-btn:hover { background: var(--border-light); }

/* --- Theme toggle --- */
.theme-toggle {
  background: rgba(255,255,255,0.15);
  color: white;
  border-radius: 6px;
  font-size: 16px;
  line-height: 1;
  padding: 6px 10px;
}
.theme-toggle:hover { background: rgba(255,255,255,0.25); }

/* --- Dashboard drawer (account menu + admin Users panel, combined) --- */
.drawer-backdrop {
  position: fixed; inset: 0; background: var(--overlay); z-index: 90;
}
.dashboard-drawer {
  position: fixed; top: 0; left: 0; height: 100vh; width: 380px; max-width: 92vw;
  background: var(--surface); color: var(--text);
  box-shadow: 4px 0 24px var(--shadow-strong);
  transform: translateX(-100%);
  transition: transform 0.28s ease;
  z-index: 95;
  overflow-y: auto;
  padding: 20px;
}
.dashboard-drawer.open { transform: translateX(0); }
.drawer-header {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px;
}
.drawer-close {
  background: none; border: none; font-size: 22px; line-height: 1; color: var(--text-muted); padding: 4px 8px;
}
.drawer-close:hover { color: var(--text); }
.drawer-section {
  padding: 8px 0 16px;
  border-bottom: 1px solid var(--border-light);
  margin-bottom: 16px;
}
.drawer-section:last-child { border-bottom: none; margin-bottom: 0; }
.drawer-section table { font-size: 12px; }

.dropdown-item {
  display: block; width: 100%; text-align: left; background: none; border: none;
  border-radius: 6px; padding: 10px 14px; font-size: 13px; font-weight: 500; color: var(--text);
  text-decoration: none; box-sizing: border-box;
}
.dropdown-item:hover { background: var(--border-light); }

.sub-badge {
  font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px;
}
.sub-badge.sub-active { background: rgba(255,255,255,0.18); color: white; }
.sub-badge.sub-warning { background: #fef3c7; color: var(--amber); }
.sub-badge.sub-expired { background: #fee2e2; color: var(--red); }

.lockout {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
}
.lockout-card {
  background: var(--surface); color: var(--text); border-radius: 12px; padding: 40px; max-width: 420px;
  box-shadow: 0 4px 24px var(--shadow); text-align: center;
}
.lockout-card h2 { color: var(--red); margin: 0 0 12px; font-size: 20px; }
.lockout-card p { color: var(--text-muted); font-size: 14px; margin: 8px 0; }

.grace-banner {
  background: #fee2e2; color: var(--red); font-size: 13px; font-weight: 500;
  padding: 10px 20px; text-align: center;
}
