/* RailPlus Assistant: one stylesheet, no external fonts or assets (CSP: 'self' only). */

:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sidebar: #11202e;
  --sidebar-text: #dbe4ec;
  --sidebar-muted: #8fa3b5;
  --text: #17212b;
  --muted: #5d6b78;
  --border: #e1e5ea;
  --accent: #c8102e;        /* signal red */
  --accent-dark: #a00c25;
  --accent-soft: #fbe9ec;
  --chip: #1f4e79;
  --chip-soft: #e7eff7;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(17, 32, 46, .06), 0 4px 16px rgba(17, 32, 46, .06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.25; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: 8px; padding: 9px 16px;
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:focus-visible, .icon-btn:focus-visible, .cite-chip:focus-visible, .conv-open:focus-visible {
  outline: 2px solid var(--chip); outline-offset: 2px;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { border-color: #b9c2cc; background: #fff; }
.btn-small { padding: 5px 10px; font-size: 13px; font-weight: 500; }
.btn-block { width: 100%; }
.icon-btn {
  border: 0; background: transparent; color: inherit; cursor: pointer;
  font-size: 18px; line-height: 1; padding: 6px 8px; border-radius: 6px;
}
.icon-btn:hover { background: rgba(127, 127, 127, .15); }
.link-btn {
  border: 0; background: none; padding: 2px 0; color: var(--chip); cursor: pointer; font: inherit;
}
.link-btn:hover { text-decoration: underline; }
.form-error { color: var(--accent-dark); margin: 0; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .01em; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px; flex: none;
  background: linear-gradient(135deg, var(--accent) 0 50%, #fff 50% 100%);
  box-shadow: inset 0 0 0 2px var(--accent);
}

/* ---------------------------------------------------------------- login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(380px, 100%); background: var(--panel); border-radius: 14px;
  box-shadow: var(--shadow); padding: 32px; display: grid; gap: 16px;
}
.login-card h1 { margin: 8px 0 0; font-size: 22px; }
.login-card label { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
input, textarea {
  font: inherit; color: inherit; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; background: #fff; width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--chip-soft); border-color: var(--chip); }

/* ---------------------------------------------------------------- layout */
.app { height: 100%; display: grid; grid-template-columns: 280px 1fr; }
.sidebar {
  background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column;
  gap: 16px; padding: 18px 14px; min-height: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.conversation-list { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 2px; }
.conv-row { display: flex; align-items: center; border-radius: 8px; }
.conv-row:hover, .conv-row.active { background: rgba(255, 255, 255, .08); }
.conv-row.active { box-shadow: inset 3px 0 0 var(--accent); }
.conv-open {
  flex: 1; min-width: 0; text-align: left; border: 0; background: none; color: inherit; overflow: clip;
  font: inherit; font-size: 14px; padding: 9px 10px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-action { font-size: 13px; opacity: 0; color: var(--sidebar-muted); }
.conv-row:hover .conv-action, .conv-row.active .conv-action, .conv-action:focus-visible { opacity: 1; }
.sidebar-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  border-top: 1px solid rgba(255, 255, 255, .1); padding-top: 12px;
}
.sidebar-foot .muted { color: var(--sidebar-muted); overflow: hidden; text-overflow: ellipsis; }
.sidebar .btn-ghost { color: var(--sidebar-text); border-color: rgba(255, 255, 255, .2); }
.sidebar .btn-ghost:hover { background: rgba(255, 255, 255, .08); }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.chat-head {
  display: flex; align-items: center; gap: 8px; padding: 12px 24px; min-height: 52px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.chat-title { margin: 0; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.messages { flex: 1; overflow-y: auto; padding: 24px; scroll-behavior: smooth; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; padding: 12vh 16px 0; }
.empty h2 { font-size: 24px; margin: 0 0 8px; }
.empty p { max-width: 520px; margin: 0 auto; }

.msg { margin-bottom: 22px; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .bubble {
  background: var(--sidebar); color: #fff; padding: 10px 14px; border-radius: 14px 14px 4px 14px;
  max-width: 80%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-assistant .answer {
  background: var(--panel); border: 1px solid var(--border); border-radius: 4px 14px 14px 14px;
  padding: 14px 18px; box-shadow: var(--shadow);
}
.answer.streaming::after {
  content: ""; display: inline-block; width: 8px; height: 15px; margin-left: 2px;
  background: var(--muted); vertical-align: text-bottom; animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.not-found-note { margin: 6px 4px 0; }

/* ---------------------------------------------------------------- prose */
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p { margin: 0 0 10px; }
.prose ul, .prose ol { margin: 0 0 10px; padding-left: 22px; }
.prose li { margin: 2px 0; }
.prose h3, .prose h4, .prose h5 { margin: 14px 0 6px; font-size: 15px; }
.prose code { font-family: var(--mono); font-size: 13px; background: #f0f2f5; padding: 1px 4px; border-radius: 4px; }
.prose mark { background: #fff3b0; }
.table-wrap { overflow-x: auto; margin: 0 0 10px; }
.prose table { border-collapse: collapse; font-size: 13px; min-width: 100%; }
.prose th, .prose td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; vertical-align: top; }
.prose th { background: #f3f5f7; }

.cite-chip {
  display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px;
  margin: 0 1px; padding: 0 5px; border: 0; border-radius: 10px; background: var(--chip-soft);
  color: var(--chip); font: 600 11px/1 var(--font); vertical-align: 2px; cursor: pointer;
}
.cite-chip:hover { background: var(--chip); color: #fff; }

/* ---------------------------------------------------------------- sources */
.sources { margin-top: 12px; }
.sources h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px 2px; }
.source-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 8px;
}
.source-head { display: flex; gap: 10px; align-items: flex-start; }
.source-id {
  flex: none; width: 22px; height: 22px; border-radius: 11px; background: var(--chip); color: #fff;
  display: grid; place-items: center; font-size: 12px; font-weight: 700;
}
.source-meta { display: grid; gap: 1px; min-width: 0; }
.source-meta strong { font-size: 14px; }
.passage {
  margin: 8px 0 2px 32px; padding-left: 10px; border-left: 3px solid var(--accent-soft);
  font-size: 13.5px; color: #2b3742; overflow-wrap: anywhere;
}
.passage.collapsed { max-height: 5.2em; overflow: hidden; -webkit-mask-image: linear-gradient(#000 60%, transparent); mask-image: linear-gradient(#000 60%, transparent); }
.source-card > .link-btn { margin-left: 32px; }
.source-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 0 32px; }
.also-in { border-style: dashed; }

/* ---------------------------------------------------------------- composer */
.composer-wrap { padding: 8px 24px 14px; background: linear-gradient(transparent, var(--bg) 30%); }
.composer-wrap > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.status-line { min-height: 20px; margin-bottom: 4px; color: var(--muted); font-size: 13px; }
.composer {
  display: flex; gap: 8px; align-items: flex-end; background: var(--panel);
  border: 1px solid var(--border); border-radius: 14px; padding: 8px; box-shadow: var(--shadow);
}
.composer textarea { border: 0; resize: none; max-height: 200px; padding: 8px; }
.composer textarea:focus { outline: none; }
.disclaimer { text-align: center; margin-top: 8px; }

/* ---------------------------------------------------------------- drawer */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100%); z-index: 30;
  background: var(--panel); box-shadow: -8px 0 32px rgba(17, 32, 46, .18);
  display: flex; flex-direction: column;
}
.drawer-head { display: flex; justify-content: space-between; gap: 12px; padding: 18px 20px 8px; }
.drawer-head h2 { margin: 2px 0 0; font-size: 17px; }
.drawer-head p { margin: 0; }
.drawer-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 20px 12px; border-bottom: 1px solid var(--border); }
.drawer-body { flex: 1; overflow-y: auto; padding: 16px 20px 32px; font-size: 14px; }

.backdrop { position: fixed; inset: 0; background: rgba(17, 32, 46, .45); z-index: 15; }
.only-mobile { display: none; }

/* ---------------------------------------------------------------- mobile */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(300px, 86vw); z-index: 20;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sidebar.open { transform: none; }
  .only-mobile { display: inline-flex; }
  .messages { padding: 16px 12px; }
  .chat-head { padding: 10px 12px; }
  .composer-wrap { padding: 6px 12px 10px; }
  .msg-user .bubble { max-width: 90%; }
  .passage, .source-actions, .source-card > .link-btn { margin-left: 0; }
  .conv-action { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
