/* =========================================================
   Fidel Financial Services — LMS
   Responsive stylesheet: Mobile / Tablet / Desktop
   Breakpoints:
     Mobile  : ≤ 600px
     Tablet  : 601px – 1024px
     Desktop : ≥ 1025px
   ========================================================= */

/* ── Variables ──────────────────────────────────────────── */
:root {
  --navy:      #15294d;
  --navy-deep: #0e1d38;
  --navy-mid:  #1f3a66;
  --navy-soft: #2c5083;
  --cream:     #f7f3e9;
  --cream-dim: #ece5d3;
  --ink:       #1c1f1d;
  --gold:      #c9a227;
  --gold-light:#e4d193;
  --red:       #b3433f;
  --amber:     #c9882f;
  --radius:    10px;
  --shadow:    0 2px 10px rgba(21,41,77,.12);
  --sidebar-w: 240px;       /* desktop sidebar width */
  --topbar-h:  54px;        /* mobile top-bar height  */
}

/* ── Reset ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--cream);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
  /* Prevent text resize on orientation change (iOS) */
}
a { color: inherit; }

/* ── Shell layout ───────────────────────────────────────── */
.app-shell {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh; /* dynamic viewport on mobile browsers */
}

/* ── Sidebar ────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: white;
  color: var(--navy);
  display: flex;
  flex-direction: column;
  padding: 22px 0;
  flex-shrink: 0;
  border-right: 1px solid var(--cream-dim);
  /* Transitions for mobile drawer */
  transition: transform .25s ease;
}

.sidebar .brand {
  padding: 0 20px 18px;
  border-bottom: 1px solid var(--cream-dim);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.sidebar .brand img { max-width: 160px; height: auto; display: block; flex: 1; }

/* Close (×) button — hidden on desktop, shown on mobile/tablet */
.sidebar-close {
  display: none;
  background: none;
  border: none;
  font-size: 24px;
  color: var(--navy);
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  cursor: pointer;
  font-size: 14px;
  color: var(--navy);
  border-left: 3px solid transparent;
  opacity: .75;
  /* Large touch target */
  min-height: 48px;
}
.nav-item:hover, .nav-item:focus { background: var(--cream); opacity: 1; outline: none; }
.nav-item.active {
  background: var(--cream);
  border-left-color: var(--gold);
  font-weight: 700;
  opacity: 1;
}

.sidebar .user-box {
  margin-top: auto;
  padding: 16px 20px;
  border-top: 1px solid var(--cream-dim);
  font-size: 12px;
}
.sidebar .user-box .name { font-weight: 600; font-size: 13px; color: var(--navy); }
.sidebar .user-box button {
  margin-top: 8px;
  width: 100%;
  background: var(--navy);
  border: none;
  color: white;
  padding: 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  min-height: 44px;
}
.sidebar .user-box button:hover { background: var(--navy-mid); }

/* Mobile overlay backdrop */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(14,29,56,.5);
  z-index: 299;
  backdrop-filter: blur(2px);
}
.sidebar-overlay.active { display: block; }

/* ── Main wrap ──────────────────────────────────────────── */
.main-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ── Top nav-bar (back/forward, hamburger, bell) ─────────── */
.nav-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 20px;
  background: var(--cream);
  border-bottom: 1px solid var(--cream-dim);
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 50px;
}
.nav-bar-spacer { flex: 1; }

/* Hamburger button — hidden on desktop */
.hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 4px;
  width: 40px;
  height: 40px;
  background: white;
  border: 1px solid #d8d1bd;
  border-radius: var(--radius);
  cursor: pointer;
  box-shadow: var(--shadow);
  padding: 0;
}
.hamburger span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--navy);
  border-radius: 2px;
  transition: all .2s;
}

/* Back/Forward arrows */
.nav-arrow {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid #d8d1bd;
  background: white;
  color: var(--navy);
  cursor: pointer;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
.nav-arrow:hover:not(:disabled) { background: var(--navy); color: white; border-color: var(--navy); }
.nav-arrow:disabled { opacity: .35; cursor: not-allowed; box-shadow: none; }

/* ── Page content ────────────────────────────────────────── */
.main {
  flex: 1;
  padding: 20px 28px 32px;
  max-width: 1200px;
  width: 100%;
}

/* ── Page header ─────────────────────────────────────────── */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}
.page-header h2 { margin: 0; font-size: 22px; color: var(--navy); }
.page-header .sub { font-size: 13px; color: #6b6557; margin-top: 2px; }

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  background: var(--navy);
  color: white;
  border: none;
  padding: 10px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  min-height: 40px;
  white-space: nowrap;
}
.btn:hover { background: var(--navy-mid); }
.btn.secondary {
  background: transparent;
  color: var(--navy);
  border: 1px solid var(--navy);
}
.btn.secondary:hover { background: var(--cream); }
.btn.danger  { background: var(--red); }
.btn.danger:hover  { background: #973330; }
.btn.gold    { background: var(--gold); color: var(--navy-deep); }
.btn.gold:hover    { background: #b8932a; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Stat cards ─────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: white;
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  border-left: 4px solid var(--navy);
}
.stat-card.warn   { border-left-color: var(--amber); }
.stat-card.danger { border-left-color: var(--red); }
.stat-card .label {
  font-size: 11px;
  color: #6b6557;
  text-transform: uppercase;
  letter-spacing: .4px;
}
.stat-card .value {
  font-size: 22px;
  font-weight: 700;
  color: var(--navy);
  margin-top: 4px;
}
.stat-card .value.money::before { content: "UGX "; font-size: 12px; font-weight: 500; }

/* ── Cards ──────────────────────────────────────────────── */
.card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 18px;
}

/* ── Tables — horizontally scrollable on small screens ─── */
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 500px; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--cream-dim); white-space: nowrap; }
th { color: #6b6557; text-transform: uppercase; font-size: 11px; letter-spacing: .4px; }
tr:hover td { background: var(--cream); }

/* ── Badges ─────────────────────────────────────────────── */
.badge { display: inline-block; padding: 3px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge.pending    { background: var(--gold-light); color: #7a5d10; }
.badge.approved,.badge.active,.badge.paid,.badge.member { background: #dbe3ef; color: var(--navy); }
.badge.rejected,.badge.blacklisted,.badge.defaulted,.badge.overdue { background: #f3dcda; color: var(--red); }
.badge.settled    { background: #e4d193; color: #7a5d10; }
.badge.non_member,.badge.partial { background: var(--cream-dim); color: #6b6557; }

/* ── Forms ──────────────────────────────────────────────── */
label { display: block; font-size: 12px; color: #6b6557; margin: 10px 0 4px; font-weight: 600; }
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d8d1bd;
  border-radius: 8px;
  font-size: 16px;   /* ≥16px stops iOS Safari auto-zooming on focus */
  font-family: inherit;
  background: white;
  /* Allow a minimum tap target */
  min-height: 44px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--navy-soft);
  border-color: var(--navy-soft);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.form-actions { margin-top: 16px; display: flex; gap: 10px; flex-wrap: wrap; }

/* ── Tabs ────────────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--cream-dim);
  overflow-x: auto;   /* scrollable on mobile */
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: 10px 14px;
  cursor: pointer;
  font-size: 13px;
  color: #6b6557;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  min-height: 44px;
  display: flex;
  align-items: center;
}
.tab.active { color: var(--navy); border-bottom-color: var(--gold); font-weight: 600; }

/* ── Modals ──────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(21,41,77,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 998;
  padding: 16px;   /* so modal never touches screen edge */
}
.modal {
  background: white;
  border-radius: var(--radius);
  padding: 22px;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.modal h3 { margin-top: 0; color: var(--navy); }
.modal-close {
  float: right;
  cursor: pointer;
  font-size: 22px;
  color: #9a937e;
  line-height: 1;
  padding: 4px;
  /* Larger tap target */
  min-width: 32px;
  text-align: center;
}

/* ── Guarantor rows ──────────────────────────────────────── */
.guarantor-row { display: flex; gap: 8px; align-items: flex-end; margin-bottom: 6px; flex-wrap: wrap; }
.guarantor-row input { flex: 1; min-width: 100px; }

/* ── Toast ───────────────────────────────────────────────── */
.toast {
  position: fixed;
  top: 16px;
  right: 16px;
  left: 16px;             /* full-width on mobile */
  max-width: 420px;
  margin: 0 auto;         /* centred if viewport > max-width */
  background: var(--navy);
  color: white;
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 13px;
  z-index: 999;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: nowrap;
}
.toast.error { background: var(--red); }

/* ── Empty state ─────────────────────────────────────────── */
.empty-state { text-align: center; padding: 40px 20px; color: #9a937e; }

/* ── Notification bell ───────────────────────────────────── */
.notif-bell-wrap { position: relative; display: flex; align-items: center; }
.notif-bell {
  background: white;
  border: 1px solid #d8d1bd;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  color: var(--navy);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  position: relative;
}
.notif-bell:hover { background: var(--navy); color: white; border-color: var(--navy); }
.notif-bell.has-unread { animation: bellShake .6s ease; }

@keyframes bellShake {
  0%,100% { transform: rotate(0); }
  20% { transform: rotate(-15deg); }
  40% { transform: rotate(15deg); }
  60% { transform: rotate(-10deg); }
  80% { transform: rotate(10deg); }
}

.notif-badge {
  position: absolute;
  top: -4px; right: -4px;
  background: var(--red);
  color: white;
  border-radius: 50%;
  min-width: 18px; height: 18px;
  font-size: 10px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  padding: 0 3px;
  border: 2px solid var(--cream);
}

.notif-panel {
  position: fixed;      /* fixed so it doesn't scroll off on mobile */
  top: 60px;
  right: 12px;
  width: min(360px, calc(100vw - 24px));
  max-height: 75vh;
  max-height: 75dvh;
  background: white;
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(21,41,77,.18);
  border: 1px solid var(--cream-dim);
  z-index: 900;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.notif-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--cream-dim);
  font-size: 14px; font-weight: 700; color: var(--navy);
  flex-shrink: 0;
}
.notif-read-all {
  background: none; border: none; cursor: pointer;
  font-size: 11px; color: var(--gold); font-weight: 600;
  min-height: 36px; padding: 0 4px;
}
.notif-read-all:hover { text-decoration: underline; }
#notifList { overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }

.notif-item {
  display: flex; gap: 10px; padding: 12px 14px;
  border-bottom: 1px solid var(--cream);
  cursor: pointer; position: relative;
  min-height: 56px;
}
.notif-item.unread { background: #f0f4fa; }
.notif-item:hover  { background: var(--cream); }
.notif-icon {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0; margin-top: 1px;
}
.notif-icon.loan_submitted { background: #dbe3ef; }
.notif-icon.loan_approved  { background: #d8ece1; }
.notif-icon.loan_rejected  { background: #f5dada; }
.notif-icon.overdue        { background: #fde8c8; }
.notif-icon.success        { background: #d8ece1; }
.notif-icon.info,.notif-icon.warning { background: var(--cream-dim); }
.notif-body { flex: 1; min-width: 0; }
.notif-title { font-size: 12px; font-weight: 700; color: var(--navy); margin-bottom: 2px; }
.notif-msg   { font-size: 11px; color: #555; line-height: 1.4; white-space: normal; }
.notif-time  { font-size: 10px; color: var(--gold); margin-top: 3px; }
.notif-dismiss {
  background: none; border: none; cursor: pointer;
  color: #ccc; font-size: 16px; padding: 6px;
  position: absolute; top: 6px; right: 6px;
  min-width: 32px; min-height: 32px;
  display: flex; align-items: center; justify-content: center;
}
.notif-dismiss:hover { color: var(--red); }
.notif-empty { text-align: center; padding: 32px 16px; color: #9a937e; font-size: 13px; }
.notif-unread-dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--navy);
  position: absolute; left: 5px; top: 50%; transform: translateY(-50%);
}

/* ── Login page ──────────────────────────────────────────── */
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--navy-deep), var(--navy));
  padding: 20px;
}
.login-card {
  background: var(--cream);
  border-radius: 14px;
  padding: 36px 32px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 40px rgba(0,0,0,.3);
}
.login-card img.login-logo {
  display: block;
  max-width: 200px;
  margin: 0 auto 20px;
}
.login-card h1 { color: var(--navy); margin: 0 0 4px; font-size: 20px; text-align: center; }
.login-card .tag { color: #6b6557; font-size: 13px; margin-bottom: 20px; text-align: center; }
.login-card .btn { width: 100%; margin-top: 12px; padding: 12px; font-size: 15px; }
.login-error { color: var(--red); font-size: 13px; margin-top: 10px; }

/* ── ════════════════════════════════════════════════════════
   TABLET  601px – 1024px
   Sidebar collapses to a slide-in drawer.
   ═══════════════════════════════════════════════════════ */
@media (max-width: 1024px) {
  /* Sidebar becomes an off-canvas drawer */
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 300;
    transform: translateX(-100%);
    box-shadow: 4px 0 24px rgba(21,41,77,.15);
    padding-top: env(safe-area-inset-top, 0);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: block; }

  /* Hamburger visible */
  .hamburger { display: flex; }

  /* Main takes full width */
  .main-wrap { width: 100%; }
  .main { padding: 16px 16px 28px; }

  /* Tables always scroll */
  .card table { min-width: 480px; }

  /* Two-column forms stay two-column on tablet */
  .form-row { grid-template-columns: 1fr 1fr; }

  /* Wider modals */
  .modal { max-width: 92vw; }

  /* Stat grid: 3 per row on tablet */
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ── ════════════════════════════════════════════════════════
   MOBILE  ≤ 600px
   Single-column everything. Full-screen modals.
   ═══════════════════════════════════════════════════════ */
@media (max-width: 600px) {
  /* Stat grid: 2 per row on small phones */
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-card .value { font-size: 18px; }

  /* Single-column forms */
  .form-row { grid-template-columns: 1fr; gap: 0; }

  /* Full-width modals */
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal {
    max-width: 100%;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 92vh;
    max-height: 92dvh;
    padding: 20px 16px;
    /* slide up from bottom */
  }

  /* Bigger page headers wrap cleanly */
  .page-header h2 { font-size: 18px; }
  .page-header .sub { font-size: 12px; }

  /* Action buttons go full-width on narrow screens */
  .page-header .btn { width: 100%; text-align: center; }

  /* Tables: min-width prevents squishing, scrolls instead */
  table { min-width: 460px; font-size: 12px; }
  th, td { padding: 8px 10px; }

  /* Nav bar compact */
  .nav-bar { padding: 6px 12px; gap: 4px; }
  .nav-arrow { width: 34px; height: 34px; font-size: 13px; }

  /* Main padding */
  .main { padding: 12px 12px 24px; }

  /* Login card */
  .login-card { padding: 28px 20px; }

  /* Notification panel full-width */
  .notif-panel {
    right: 0; left: 0;
    width: 100%;
    top: 56px;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 70vh;
    max-height: 70dvh;
  }

  /* Toast full width */
  .toast { left: 12px; right: 12px; }

  /* Form actions stack */
  .form-actions { flex-direction: column; }
  .form-actions .btn { width: 100%; text-align: center; }

  /* Guarantor rows stack */
  .guarantor-row { flex-direction: column; }
  .guarantor-row input { min-width: 0; width: 100%; }
}

/* ── Safe area (iPhone notch / home bar) ─────────────────── */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .main { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
  .user-box { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}

/* ── Print: hide nav chrome ──────────────────────────────── */
@media print {
  .sidebar, .nav-bar { display: none !important; }
  .main-wrap { width: 100% !important; }
  .main { padding: 0 !important; }
}
