:root {
  --bg: #0b1220;
  --panel: #111a2e;
  --panel-2: #17223a;
  --text: #e7ecf3;
  --muted: #93a3bf;
  --accent: #4f8dff;
  --accent-hover: #6ea1ff;
  --user: #1f3b6d;
  --bot: #17223a;
  --danger: #ff6b6b;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.28);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  /* Hard cap: nothing on the page may cause horizontal scroll. If a
     runaway URL or table blows past the viewport, it gets clipped
     rather than widening the document. */
  overflow-x: hidden;
  max-width: 100vw;
}

body { display: flex; flex-direction: column; }

button, input, textarea {
  font-family: inherit;
  font-size: 16px;   /* >=16px so iOS doesn't zoom the page on focus */
  color: var(--text);
}

a { color: var(--accent); text-decoration: none; }
a:active, a:hover { text-decoration: underline; }

/* ---- Login ---- */
body.login {
  align-items: center;
  justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
}
body.login main {
  background: var(--panel);
  padding: 28px 22px;
  border-radius: var(--radius);
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
}
body.login h1 { margin: 0 0 6px; font-size: 22px; }
body.login .subtitle { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
body.login form { display: grid; gap: 10px; }
body.login label { font-size: 13px; color: var(--muted); }
body.login input {
  background: var(--panel-2);
  border: 1px solid #24324e;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  outline: none;
}
body.login input:focus { border-color: var(--accent); }
body.login button {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
}
body.login button:hover { background: var(--accent-hover); }
body.login .error { color: var(--danger); margin: 4px 0 0; font-size: 13px; }
body.login .notice {
  margin-top: 14px;
  background: var(--panel-2);
  border: 1px solid #24324e;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 14px;
  color: var(--text);
}
body.login .notice p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
body.login .fallback { margin-top: 16px; text-align: center; }
body.login .ghost {
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
  padding: 6px 10px;
}
body.login .ghost:hover { color: var(--text); }
body.login #password-form { margin-top: 10px; display: grid; gap: 10px; }

/* ---- App shell ---- */
body.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 16px 10px;
  background: var(--panel);
  border-bottom: 1px solid #1f2a44;
}
.app-header h1 { font-size: 17px; margin: 0; font-weight: 600; letter-spacing: 0.2px; }
.app-header-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.app-header-title .greeting {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.app-header-actions { display: flex; align-items: center; gap: 8px; }
.app-header-actions a {
  color: var(--muted);
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 8px;
}
.app-header-actions a:hover { color: var(--text); text-decoration: none; background: var(--panel-2); }
#clear-btn {
  background: transparent;
  border: 1px solid #2b3a5c;
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
}
#clear-btn:hover { color: var(--text); border-color: #3c4e77; }

/* ---- Follow-up chips ---- */
.followups {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.followup-chip {
  background: transparent;
  border: 1px solid #2b3a5c;
  color: var(--accent);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  line-height: 1.2;
  text-align: left;
}
.followup-chip:hover { background: var(--panel-2); border-color: var(--accent); }
.followup-chip:active { transform: scale(0.98); }

/* ---- Feedback row ---- */
.feedback {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #1f2a44;
  font-size: 13px;
  color: var(--muted);
}
.feedback .fb-prompt { margin-right: 4px; }
.feedback .fb-btn {
  background: transparent;
  border: 1px solid #2b3a5c;
  color: var(--text);
  padding: 3px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}
.feedback .fb-btn:hover:not(:disabled) { border-color: var(--accent); }
.feedback .fb-btn:disabled { opacity: 0.4; cursor: default; }
.feedback .fb-btn.active { border-color: var(--danger); }
.feedback .fb-note {
  display: flex;
  gap: 6px;
  flex: 1 1 100%;
  margin-top: 6px;
}
.feedback .fb-note-input {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid #24324e;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  outline: none;
}
.feedback .fb-note-input:focus { border-color: var(--accent); }
.feedback .fb-note-send {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}
.feedback .fb-thanks { color: var(--muted); font-style: italic; }

/* ---- Admin page ---- */
body.app.admin { overflow: auto; }
.admin-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px max(12px, env(safe-area-inset-left)) max(16px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-right));
  display: grid;
  gap: 14px;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}
.admin-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.admin-card h2 { font-size: 15px; margin: 0 0 10px; font-weight: 600; }
.admin-card h2 .hint { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 4px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 10px;
}
.stat {
  background: var(--panel-2);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.stat-label { color: var(--muted); font-size: 12px; }
.stat-value { font-size: 20px; font-weight: 600; margin-top: 4px; }

.admin-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.admin-list li {
  background: var(--panel-2);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13.5px;
  display: grid;
  gap: 4px;
}
.admin-list li.empty { color: var(--muted); font-style: italic; }
.admin-list .row-ts { color: var(--muted); font-size: 11px; }
.admin-list .row-q { color: var(--text); }
.admin-list .row-note { color: #ffd7d7; font-size: 12px; }
.admin-list .row-sections { color: var(--muted); font-size: 12px; }
.admin-list .sid { color: var(--accent); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.admin-list .count { float: right; color: var(--muted); }

.badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  margin-left: 4px;
}
.badge.miss { background: #3a1b1b; color: #ffd7d7; }
.badge.cite { background: var(--panel-2); color: var(--accent); }

.admin-filter { margin-bottom: 8px; color: var(--muted); font-size: 13px; }

/* ---- Chat list ---- */
.chat {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.msg {
  display: flex;
  max-width: 88%;
  min-width: 0;
}
.msg.user { align-self: flex-end; }
.msg.bot { align-self: flex-start; }
.bubble {
  background: var(--bot);
  border-radius: var(--radius);
  padding: 10px 14px;
  line-height: 1.45;
  box-shadow: var(--shadow);
  /* `overflow-wrap: anywhere` lets long URLs and other unbreakable tokens
     break at any character so they don't push the bubble off-screen on
     narrow phones. `min-width: 0` unlocks shrinking inside flexbox. */
  overflow-wrap: anywhere;
  word-break: break-word;
  min-width: 0;
  max-width: 100%;
}
/* Belt-and-braces: apply the wrap rules to every descendant so nothing
   (<a>, <p>, <li>, <strong>, marked.js output, etc.) can force the
   bubble wider than its flex parent. `!important` defends against any
   inherited rules from future additions. */
.bubble *,
.bubble a {
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
  max-width: 100%;
}
/* Tables can be wider than the viewport; let them scroll inside the bubble
   instead of blowing out the layout. */
.bubble table {
  display: block;
  overflow-x: auto;
  max-width: 100%;
  border-collapse: collapse;
  margin: 6px 0;
  font-size: 13.5px;
}
.bubble table th, .bubble table td {
  padding: 4px 8px;
  border: 1px solid #2b3a5c;
  white-space: nowrap;
}
.bubble pre {
  white-space: pre-wrap;
  word-break: break-word;
  background: rgba(255, 255, 255, 0.05);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  overflow-x: auto;
  max-width: 100%;
}
.bubble a {
  /* If a link's text is the URL itself, guarantee it wraps anywhere. */
  overflow-wrap: anywhere;
  word-break: break-word;
}
.msg.user .bubble { background: var(--user); }

/* Markdown inside bot answers */
.bubble .answer p { margin: 0 0 8px; }
.bubble .answer p:last-child { margin-bottom: 0; }
.bubble .answer ol, .bubble .answer ul { padding-left: 22px; margin: 6px 0; }
.bubble .answer li { margin: 2px 0; }
.bubble .answer strong { color: #ffffff; }
.bubble .answer code {
  background: rgba(255, 255, 255, 0.08);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13.5px;
}

.bubble .citation { margin-top: 10px; }

/* Loom embed */
.loom-wrap {
  position: relative;
  padding-top: 56.25%;
  width: 100%;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #000;
}
.loom-wrap iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}
.cite-label {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
}

/* Help-link card */
.help-link {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  background: var(--panel-2);
  border: 1px solid #24324e;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  color: var(--text);
}
.help-link:hover { border-color: var(--accent); text-decoration: none; }
.help-link .help-text { display: flex; flex-direction: column; min-width: 0; }
.help-link .help-label { font-weight: 600; }
.help-link .help-url {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.help-link .arrow { color: var(--accent); font-size: 20px; flex-shrink: 0; }

/* Thinking / loading indicator */
.thinking {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  color: var(--muted);
  font-size: 14px;
}
.thinking .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}
.thinking .dot:nth-child(2) { animation-delay: 0.2s; }
.thinking .dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; }
  40% { opacity: 1; }
}

/* ---- Input bar ---- */
.input-bar {
  display: flex;
  gap: 8px;
  padding: 10px 10px max(10px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid #1f2a44;
}
#input {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid #24324e;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  resize: none;
  max-height: 30vh;
  outline: none;
  line-height: 1.35;
}
#input:focus { border-color: var(--accent); }
#send-btn {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0 18px;
  font-weight: 600;
  cursor: pointer;
}
#send-btn:hover:not(:disabled) { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Error bubble ---- */
.bubble.error {
  background: #3a1b1b;
  color: #ffd7d7;
}
