:root {
  --fmbep-green: #0d6e3f;
  --fmbep-green-dark: #095030;
  --fmbep-green-light: #e7f4ec;
  --fmbep-grey: #f4f6f5;
  --fmbep-text: #1c2620;
  --radius-lg: 18px;
  --radius-md: 12px;
  --shadow-soft: 0 10px 30px rgba(13, 110, 63, 0.08);
}

[data-bs-theme="dark"] {
  --fmbep-green-light: #10261a;
  --fmbep-grey: #10141c;
  --fmbep-text: #e7ece9;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--fmbep-grey);
  color: var(--fmbep-text);
}

/* ---- Navbar ---- */
.app-navbar {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(13, 110, 63, 0.1);
}
[data-bs-theme="dark"] .app-navbar {
  background: rgba(16, 20, 28, 0.85);
}
.brand-badge {
  width: 42px; height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--fmbep-green), #16a35c);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
  box-shadow: var(--shadow-soft);
}
.brand-text small { color: var(--fmbep-green); font-weight: 500; }
.btn-brand {
  background: linear-gradient(135deg, var(--fmbep-green), #16a35c);
  border: none; color: #fff; font-weight: 600;
  border-radius: 999px; padding: 0.5rem 1.2rem;
  box-shadow: var(--shadow-soft);
}
.btn-brand:hover { color: #fff; filter: brightness(1.08); }
.theme-toggle { width: 38px; height: 38px; }

/* ---- Hero ---- */
.hero-section {
  padding: 4.5rem 0 3rem;
  background: radial-gradient(circle at top right, var(--fmbep-green-light), transparent 60%);
}
.hero-badge {
  display: inline-flex; align-items: center; gap: 0.4rem;
  background: var(--fmbep-green-light);
  color: var(--fmbep-green-dark);
  padding: 0.35rem 0.9rem; border-radius: 999px; font-weight: 600; font-size: 0.85rem;
}
.hero-title { font-size: 2.6rem; font-weight: 800; line-height: 1.2; }
.hero-sub { font-size: 1.1rem; color: var(--fmbep-text); opacity: 0.75; max-width: 560px; }

/* ---- Glass cards ---- */
.glass-card {
  background: rgba(255, 255, 255, 0.75);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(13, 110, 63, 0.08);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-soft);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
[data-bs-theme="dark"] .glass-card {
  background: rgba(255, 255, 255, 0.04);
}
.glass-card:hover { transform: translateY(-4px); box-shadow: 0 16px 40px rgba(13,110,63,0.14); }

.service-icon {
  width: 52px; height: 52px; border-radius: 14px;
  background: var(--fmbep-green-light);
  color: var(--fmbep-green-dark);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.3rem;
}

.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: #16a35c; display: inline-block;
  box-shadow: 0 0 0 4px rgba(22,163,92,0.18);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(22,163,92,0.35); }
  70% { box-shadow: 0 0 0 8px rgba(22,163,92,0); }
  100% { box-shadow: 0 0 0 0 rgba(22,163,92,0); }
}

/* ---- Footer ---- */
.app-footer {
  background: rgba(255,255,255,0.6);
  border-top: 1px solid rgba(13,110,63,0.08);
}
[data-bs-theme="dark"] .app-footer { background: rgba(255,255,255,0.03); }
.app-footer a { color: inherit; text-decoration: none; }
.app-footer a:hover { color: var(--fmbep-green); }

/* ---- Chat interface ---- */
.chat-shell {
  height: calc(100vh - 220px);
  min-height: 480px;
  display: flex; flex-direction: column;
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(16px);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-soft);
  overflow: hidden;
  border: 1px solid rgba(13,110,63,0.08);
}
[data-bs-theme="dark"] .chat-shell { background: rgba(255,255,255,0.03); }
.chat-messages { flex: 1; overflow-y: auto; padding: 1.5rem; }
.msg-row { display: flex; margin-bottom: 1.1rem; }
.msg-row.user { justify-content: flex-end; }
.msg-bubble {
  max-width: 78%;
  padding: 0.75rem 1.05rem;
  border-radius: 16px;
  line-height: 1.55;
  font-size: 0.95rem;
}
.msg-row.ai .msg-bubble {
  background: var(--fmbep-green-light);
  color: var(--fmbep-text);
  border-bottom-left-radius: 4px;
}
.msg-row.user .msg-bubble {
  background: linear-gradient(135deg, var(--fmbep-green), #16a35c);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-bubble pre { background: rgba(0,0,0,0.06); padding: 0.6rem; border-radius: 8px; overflow-x: auto; }
.msg-bubble table { width: 100%; margin-top: 0.5rem; }
.msg-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--fmbep-green); color: #fff; font-size: 0.85rem; flex-shrink: 0;
}
.typing-dots span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--fmbep-green);
  display: inline-block; margin: 0 2px; animation: blink 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }

.chat-input-bar {
  border-top: 1px solid rgba(13,110,63,0.1);
  padding: 1rem;
  background: rgba(255,255,255,0.5);
}
[data-bs-theme="dark"] .chat-input-bar { background: rgba(255,255,255,0.02); }

.confidence-card {
  border-left: 4px solid var(--fmbep-green);
  background: var(--fmbep-green-light);
  border-radius: var(--radius-md);
  padding: 0.85rem 1rem;
  font-size: 0.88rem;
}

.screenshot-thumb {
  max-width: 220px; border-radius: 10px; margin-top: 0.5rem; display: block;
}

/* ---- Dashboard ---- */
.stat-card {
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  background: rgba(255,255,255,0.75);
  border: 1px solid rgba(13,110,63,0.08);
  box-shadow: var(--shadow-soft);
}
[data-bs-theme="dark"] .stat-card { background: rgba(255,255,255,0.04); }
.stat-icon {
  width: 46px; height: 46px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.15rem; color: #fff;
}
.stat-value { font-size: 1.7rem; font-weight: 800; }

.table-glass {
  background: rgba(255,255,255,0.75);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-soft);
}
[data-bs-theme="dark"] .table-glass { background: rgba(255,255,255,0.04); }

.sidebar-officer {
  min-height: 100vh;
  width: 250px;
  flex-shrink: 0;
  background: rgba(255,255,255,0.85);
  border-right: 1px solid rgba(13,110,63,0.08);
}
[data-bs-theme="dark"] .sidebar-officer { background: rgba(255,255,255,0.03); }
.sidebar-officer .nav-link {
  color: var(--fmbep-text); border-radius: 10px; padding: 0.6rem 0.9rem; font-weight: 500;
}
.sidebar-officer .nav-link.active, .sidebar-officer .nav-link:hover {
  background: var(--fmbep-green-light); color: var(--fmbep-green-dark);
}

/* ---- Officer/Admin dashboard mobile fix ----
   The sidebar was a fixed 250px column with no responsive handling, which
   on a ~390px phone left barely 100px for the actual content — squeezing
   the stat-card grid and filter forms so tightly that text and icons
   rendered on top of each other. Below 768px, stack the sidebar full-width
   above the content instead of beside it (standard, safe responsive
   pattern), so both regions get the full screen width and nothing overlaps. */
@media (max-width: 768px) {
  .app-shell-flex {
    flex-direction: column;
  }
  .sidebar-officer {
    width: 100%;
    min-height: 0;
    border-right: none;
    border-bottom: 1px solid rgba(13,110,63,0.08);
  }
  .sidebar-officer nav.nav {
    flex-direction: row !important;
    flex-wrap: wrap;
    gap: 0.4rem !important;
  }
  .sidebar-officer nav.nav hr {
    display: none; /* dividers look odd in a wrapped horizontal nav; the grouping is still visually clear from spacing */
  }
  .sidebar-officer .nav-link {
    padding: 0.45rem 0.7rem;
    font-size: 0.85rem;
  }
  main.flex-grow-1 {
    width: 100%;
    min-width: 0; /* lets Bootstrap's row/col grid compute widths correctly instead of overflowing */
  }
  /* Stat cards and filter forms: force to one per row on very narrow
     screens so labels/numbers never have to squeeze and overlap. */
  .stat-card .row.text-center .col-6,
  .row.g-3 > [class*="col-"] {
    min-width: 0;
  }
}

/* ---- Mobile chat fix: explicit measured-height layout, no nested scrolling ----
   The previous flex-grow-only approach could still overlap on some mobile
   browsers, because percentage/flex heights don't reliably account for the
   browser's dynamic address bar showing/hiding — the chat-shell's computed
   height could end up smaller than its content, causing the resolution bar
   and input bar to render on top of the last message instead of below it.
   This version uses JS-measured pixel heights (see chat.js: setLayoutVars)
   via the --vh and --nav-h custom properties, which is the standard robust
   fix for this class of mobile viewport bug. */
@media (max-width: 768px) {
  html {
    height: 100%; /* harmless on its own: only makes percentage heights resolve correctly, does not block scrolling */
  }
  body.chat-active-page {
    height: 100%;
    overflow: hidden; /* scoped correctly this time: only the chat page's BODY is scroll-locked, never <html> itself, so every other page scrolls normally */
    margin: 0;
  }
  body.chat-active-page .app-navbar {
    position: relative;
    z-index: 20;
  }
  body.chat-active-page .container.chat-active {
    display: flex;
    flex-direction: column;
    height: 100vh;   /* baseline fallback: works even if JS below hasn't run yet */
    height: 100dvh;  /* modern dynamic-viewport unit: no JS required, handles address-bar resize */
    height: calc(var(--vh, 1dvh) * 100 - var(--nav-h, 70px)); /* refined: exact navbar offset, JS-measured */
    max-height: 100dvh;
    padding-bottom: 0 !important;
    margin-bottom: 0 !important;
    overflow: hidden;
  }
  body.chat-active-page .chat-shell {
    flex: 1 1 auto;
    min-height: 0;
    height: auto !important; /* overrides the desktop calc() sizing */
    max-height: none;
    border-radius: 0;
    position: relative;
    z-index: 1;
  }
  body.chat-active-page .chat-messages {
    flex: 1 1 auto;
    min-height: 0; /* this is the ONLY element that scrolls on mobile */
    -webkit-overflow-scrolling: touch;
  }
  body.chat-active-page #resolutionBar {
    flex-shrink: 0;
    position: relative;
    z-index: 2;
    background: var(--fmbep-grey);
  }
  body.chat-active-page .chat-input-bar {
    flex-shrink: 0; /* input box always visible, never scrolled away */
    position: relative;
    z-index: 2;
    padding-bottom: max(1rem, env(safe-area-inset-bottom)); /* iPhone home-indicator safe area */
  }
  body.chat-active-page .app-footer {
    display: none; /* prevents a second, page-level scroll region on mobile */
  }
}
