/* ==========================================================
   CSS VARIABLES
========================================================== */
:root {
  --navy-900: #0b1120;
  --navy-800: #111a2e;
  --navy-700: #16213e;
  --blue-600: #2563eb;
  --blue-500: #3b82f6;
  --blue-100: #dbeafe;
  --white: #ffffff;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-400: #94a3b8;
  --gray-500: #64748b;
  --gray-700: #334155;
  --gray-900: #0f172a;
  --error-bg: #fef2f2;
  --error-border: #fecaca;
  --error-text: #b91c1c;
  --success: #16a34a;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-card: 0 20px 60px rgba(0, 0, 0, 0.35);
  --transition: all 0.2s ease;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--gray-900);
}

.screen {
  min-height: 100vh;
  width: 100%;
}

/* ==========================================================
   LOGIN SCREEN
========================================================== */
.login-screen {
  background: radial-gradient(circle at top left, var(--navy-700), var(--navy-900) 70%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  background: var(--white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 40px 36px;
  width: 100%;
  max-width: 420px;
  animation: fadeInUp 0.4s ease;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.brand {
  text-align: center;
  margin-bottom: 28px;
}

.brand-logo {
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--blue-600), var(--navy-700));
  color: var(--white);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 1px;
}

.brand-logo.small {
  width: 34px;
  height: 34px;
  font-size: 13px;
  border-radius: 9px;
  margin: 0;
}

.brand-title {
  font-size: 20px;
  font-weight: 700;
  margin: 0 0 6px;
  color: var(--gray-900);
}

.brand-subtitle {
  font-size: 13.5px;
  color: var(--gray-500);
  margin: 0;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-group label {
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-700);
}

.field-group input {
  padding: 11px 13px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-md);
  font-size: 14px;
  transition: var(--transition);
  outline: none;
  width: 100%;
}

.field-group input:focus {
  border-color: var(--blue-500);
  box-shadow: 0 0 0 3px var(--blue-100);
}

.password-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.password-wrapper input {
  padding-right: 42px;
}

.toggle-password {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray-500);
  display: flex;
  align-items: center;
  padding: 4px;
  border-radius: 6px;
  transition: var(--transition);
}

.toggle-password:hover {
  color: var(--gray-700);
  background: var(--gray-100);
}

.error-box {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  font-size: 13px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  animation: fadeInUp 0.2s ease;
}

.btn-primary {
  background: var(--blue-600);
  color: var(--white);
  border: none;
  padding: 12px 16px;
  font-size: 14.5px;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary:hover {
  background: #1d4ed8;
  transform: translateY(-1px);
}

.btn-primary:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: var(--white);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.demo-role-section {
  margin-top: 28px;
  padding-top: 20px;
  border-top: 1px dashed var(--gray-200);
}

.demo-role-label {
  font-size: 12px;
  color: var(--gray-500);
  text-align: center;
  margin: 0 0 12px;
  font-style: italic;
}

.role-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.role-chip {
  position: relative;
  cursor: pointer;
}

.role-chip input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.role-chip span {
  display: block;
  text-align: center;
  padding: 8px 6px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--gray-200);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--gray-700);
  transition: var(--transition);
}

.role-chip input:checked+span {
  background: var(--blue-600);
  border-color: var(--blue-600);
  color: var(--white);
}

.role-chip:hover span {
  border-color: var(--blue-500);
}

/* ==========================================================
   CHAT SCREEN
========================================================== */
.chat-screen {
  display: flex;
  flex-direction: column;
  background: var(--gray-50);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--navy-900);
  color: var(--white);
  flex-wrap: wrap;
  gap: 10px;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-app-name {
  font-weight: 700;
  font-size: 15px;
}

.chat-header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.current-username {
  font-size: 13.5px;
  color: var(--gray-200);
}

.role-badge {
  background: var(--blue-600);
  color: var(--white);
  font-size: 11.5px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.btn-secondary {
  background: transparent;
  border: 1.5px solid rgba(255, 255, 255, 0.3);
  color: var(--white);
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: var(--transition);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.1);
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  padding: 16px 20px 20px;
  min-height: 0;
}

.chat-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}

.btn-ghost {
  background: none;
  border: 1px solid var(--gray-200);
  color: var(--gray-700);
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: var(--transition);
}

.btn-ghost:hover {
  background: var(--gray-100);
}

.messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 8px 4px 16px;
  min-height: 300px;
}

.msg-row {
  display: flex;
  animation: fadeInUp 0.2s ease;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.assistant {
  justify-content: flex-start;
}

.bubble {
  max-width: 75%;
  padding: 12px 15px;
  border-radius: var(--radius-md);
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
}

.msg-row.user .bubble {
  background: var(--blue-600);
  color: var(--white);
  border-bottom-right-radius: 4px;
}

.msg-row.assistant .bubble {
  background: var(--white);
  border: 1px solid var(--gray-200);
  color: var(--gray-900);
  border-bottom-left-radius: 4px;
}

.example-questions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.example-q-btn {
  background: var(--blue-100);
  color: var(--blue-600);
  border: none;
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.example-q-btn:hover {
  background: #c7ddfc;
}

.sources-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.source-chip {
  background: var(--gray-100);
  border: 1px solid var(--gray-200);
  color: var(--gray-700);
  font-size: 11.5px;
  padding: 4px 9px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.pagination-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  font-size: 12.5px;
  color: var(--gray-500);
  flex-wrap: wrap;
}

.pagination-row button {
  background: var(--white);
  border: 1px solid var(--gray-200);
  padding: 5px 10px;
  border-radius: var(--radius-md);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.pagination-row button:hover:not(:disabled) {
  background: var(--gray-100);
}

.pagination-row button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.permission-denied {
  color: var(--error-text);
  font-weight: 600;
}

.loading-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--gray-500);
  padding: 6px 4px 12px;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--blue-500);
  animation: pulse 1s infinite ease-in-out;
}

.dot:nth-child(2) {
  animation-delay: 0.15s;
}

.dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes pulse {

  0%,
  80%,
  100% {
    opacity: 0.3;
    transform: scale(0.8);
  }

  40% {
    opacity: 1;
    transform: scale(1);
  }
}

.chat-input-bar {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  border-top: 1px solid var(--gray-200);
  padding-top: 12px;
}

.chat-input-bar textarea {
  flex: 1;
  resize: none;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-md);
  padding: 11px 13px;
  font-size: 14px;
  font-family: inherit;
  max-height: 140px;
  outline: none;
  transition: var(--transition);
}

.chat-input-bar textarea:focus {
  border-color: var(--blue-500);
  box-shadow: 0 0 0 3px var(--blue-100);
}

.send-btn {
  width: auto;
  padding: 11px 22px;
  white-space: nowrap;
}

/* ==========================================================
   RESPONSIVE
========================================================== */
@media (max-width: 600px) {
  .login-card {
    padding: 28px 22px;
  }

  .role-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .chat-header {
    padding: 10px 14px;
  }

  .chat-main {
    padding: 12px 10px 14px;
  }

  .bubble {
    max-width: 88%;
  }

  .current-username {
    display: none;
  }
}


/* ==========================================================
   FIXES + MARKDOWN REPLIES + ACCESS-TEST CHIP
========================================================== */
[hidden] {
  display: none !important;
}

.chat-screen {
  height: 100vh;
}

.msg-row.assistant .bubble {
  white-space: normal;
  max-width: 92%;
  overflow-wrap: anywhere;
}

.msg-row.user .bubble {
  overflow-wrap: anywhere;
}

.msg-row.assistant .bubble> :first-child {
  margin-top: 0;
}

.msg-row.assistant .bubble> :last-child {
  margin-bottom: 0;
}

.msg-row.assistant .bubble p {
  margin: 0 0 10px;
}

.msg-row.assistant .bubble h1,
.msg-row.assistant .bubble h2,
.msg-row.assistant .bubble h3,
.msg-row.assistant .bubble h4 {
  margin: 14px 0 8px;
  line-height: 1.3;
  font-size: 15.5px;
}

.msg-row.assistant .bubble h1 {
  font-size: 18px;
}

.msg-row.assistant .bubble h2 {
  font-size: 16.5px;
}

.msg-row.assistant .bubble ul,
.msg-row.assistant .bubble ol {
  margin: 0 0 10px;
  padding-left: 22px;
}

.msg-row.assistant .bubble li {
  margin: 3px 0;
}

.msg-row.assistant .bubble hr {
  border: 0;
  border-top: 1px solid var(--gray-200);
  margin: 12px 0;
}

.msg-row.assistant .bubble a {
  color: var(--blue-600);
}

.msg-row.assistant .bubble code {
  background: var(--gray-100);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

.msg-row.assistant .bubble pre {
  background: var(--gray-100);
  border-radius: var(--radius-md);
  padding: 12px;
  overflow-x: auto;
  margin: 0 0 10px;
}

.msg-row.assistant .bubble pre code {
  background: none;
  padding: 0;
}

.table-wrap {
  overflow-x: auto;
  margin: 0 0 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-md);
}

.msg-row.assistant .bubble table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}

.msg-row.assistant .bubble th,
.msg-row.assistant .bubble td {
  padding: 8px 12px;
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--gray-200);
}

.msg-row.assistant .bubble th {
  background: var(--gray-100);
  font-weight: 600;
}

.msg-row.assistant .bubble tr:last-child td {
  border-bottom: 0;
}

.example-q-btn.deny {
  background: transparent;
  color: var(--gray-500);
  border: 1px dashed var(--gray-400);
}

.example-q-btn.deny:hover {
  background: var(--gray-100);
}

/* ---------- test panel ---------- */
.test-panel {
  background: var(--white);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-md);
  margin-bottom: 8px;
}

.test-panel summary {
  cursor: pointer;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 700;
  color: var(--gray-700);
}

.test-body {
  padding: 0 14px 12px;
  max-height: 30vh;
  overflow-y: auto;
}

.test-group {
  margin-top: 8px;
}

.test-label {
  display: block;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--gray-500);
}

.doc-link {
  display: inline-block;
  background: var(--gray-100);
  border: 1px solid var(--gray-200);
  color: var(--gray-700);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  text-decoration: none;
}

.doc-link:hover {
  background: var(--gray-200);
}

/* keep everything inside the screen and lift the chat bar */
.messages {
  min-height: 0;
}

.chat-main {
  min-height: 0;
  overflow: hidden;
}

.chat-input-bar {
  margin-bottom: 28px;
  flex-shrink: 0;
}

/* ---------- login page: demo accounts panel ---------- */
.login-layout {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 24px;
  width: 100%;
  max-width: 920px;
  flex-wrap: wrap;
}

.login-layout .login-card {
  flex: 1 1 360px;
}

.demo-panel {
  flex: 1 1 300px;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  color: var(--white);
  animation: fadeInUp 0.4s ease;
}

.demo-title {
  margin: 0 0 6px;
  font-size: 18px;
}

.demo-hint {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--gray-200);
}

.demo-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.demo-item {
  text-align: left;
  font-family: inherit;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: var(--white);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  cursor: pointer;
  transition: var(--transition);
}

.demo-item:hover:not(:disabled) {
  background: rgba(59, 130, 246, 0.35);
  border-color: var(--blue-500);
}

.demo-item:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.demo-role {
  display: block;
  font-weight: 700;
  font-size: 14px;
}

.demo-email {
  display: block;
  font-size: 12.5px;
  color: var(--gray-200);
}

.demo-access {
  display: block;
  font-size: 11.5px;
  color: var(--gray-400);
  margin-top: 2px;
}

@media (max-width: 800px) {
  .login-layout .login-card {
    order: -1;
  }

  .demo-panel {
    max-width: none;
  }
}