:root {
  color-scheme: light;
  --background: 0 0% 15%;
  --foreground: 210 40% 98%;
  --card: 0 0% 15%;
  --card-foreground: 210 40% 98%;
  --popover: 0 0% 15%;
  --popover-foreground: 210 40% 98%;
  --primary: #d10008;
  --primary-foreground: 222.2 47.4% 11.2%;
  --secondary: 217.2 32.6% 17.5%;
  --secondary-foreground: 210 40% 98%;
  --muted: 217.2 32.6% 17.5%;
  --muted-foreground: 215 20.2% 65.1%;
  --accent: 217.2 32.6% 17.5%;
  --accent-foreground: 210 40% 98%;
  --destructive: 0 62.8% 30.6%;
  --destructive-foreground: 210 40% 98%;
  --border-token: 217.2 32.6% 17.5%;
  --input: 217.2 32.6% 17.5%;
  --ring: 212.7 26.8% 83.9%;
  --chart-1: 220 70% 50%;
  --chart-2: 160 60% 45%;
  --chart-3: 30 80% 55%;
  --chart-4: 280 65% 60%;
  --chart-5: 340 75% 55%;
  --brand-red: #d10008;
  --brand-brown: #b26e36;
  --brand-black: hsl(0 0% 15%);
  --bg: hsl(var(--foreground));
  --bg-elev: hsl(var(--foreground));
  --fg: hsl(var(--background));
  --fg-muted: hsl(0 0% 15% / 0.72);
  --fg-subtle: hsl(215 16.3% 46.9%);
  --border: hsl(var(--border-token));
  --border-strong: hsl(0 0% 15% / 0.32);
  --accent-color: var(--primary);
  --accent-fg: hsl(var(--primary-foreground));
  --user-bubble: var(--primary);
  --danger: hsl(var(--destructive-foreground));
  --danger-bg: hsl(var(--destructive));
  --radius: 0.5rem;
  --radius-sm: 0.375rem;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --visual: Rajdhani, Arial, sans-serif;
  --chat: Lora, Georgia, serif;
}

* {
  box-sizing: border-box;
}

html,
body,
#root {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.55 var(--visual);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--brand-brown);
  text-decoration: underline;
  text-decoration-color: currentColor;
  text-underline-offset: 2px;
}
a:hover {
  text-decoration-color: var(--fg-muted);
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}
button:not(:disabled) {
  cursor: pointer;
}

::selection {
  background: rgba(255, 255, 255, 0.16);
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  min-height: 0;
}

.app-header {
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.header-inner {
  display: grid;
  grid-template-columns: auto minmax(120px, 1fr) minmax(260px, 1fr) auto;
  align-items: center;
  gap: 16px;
  width: min(960px, 100%);
  margin: 0 auto;
  padding: 14px 20px;
}

.header-inner-admin {
  grid-template-columns: auto auto minmax(120px, 1fr) minmax(260px, 1fr) auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: var(--radius-sm);
  background: var(--fg);
  color: var(--bg);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0;
}

.brand-title {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.view-switch {
  display: inline-flex;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.view-tab {
  min-width: 68px;
  height: 28px;
  border: 0;
  border-radius: calc(var(--radius-sm) - 2px);
  background: transparent;
  color: var(--fg-subtle);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  text-transform: lowercase;
}

.view-tab-active {
  background: var(--fg);
  color: var(--bg);
}

.usage {
  justify-self: stretch;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px 16px;
  color: var(--fg-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-family: var(--mono);
}

.usage-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px 2px;
}

.usage-label,
.usage-percent {
  color: var(--fg-subtle);
}

.usage-track {
  height: 5px;
  overflow: hidden;
  border-radius: 999px;
  background: hsl(0 0% 15% / 0.12);
}

.usage-fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--brand-red);
}

.usage-percent {
}

.book-access {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  min-width: 0;
}

.book-chip {
  display: inline-flex;
  align-items: center;
  padding: 0px;
  color: var(--fg-muted);
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1;
}

.auth-user {
  justify-self: end;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  min-width: 0;
  color: var(--fg-muted);
  font-size: 12px;
  line-height: 1.2;
}

.auth-email {
  max-width: 220px;
  overflow: hidden;
  color: var(--fg);
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.auth-roles {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-scroll {
  min-height: 0;
  overflow-y: auto;
  padding: 28px 20px 24px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

.chat-stack {
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: min(720px, 100%);
  min-height: 100%;
  margin: 0 auto;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 100%;
  text-align: center;
}

.empty-card {
  width: min(420px, 100%);
  padding: 8px;
}

.empty-card h2 {
  margin: 0 0 8px;
  color: var(--fg);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0;
}

.empty-card p {
  margin: 0;
  color: var(--fg-muted);
  font-size: 14px;
}

.message {
  width: fit-content;
  max-width: 100%;
  padding: 0;
  overflow-wrap: anywhere;
  font-family: var(--chat);
}

.message-user {
  align-self: flex-end;
  max-width: min(75%, 600px);
  border-radius: var(--radius);
  background: var(--user-bubble);
  color: #fff;
  padding: 10px 14px;
  white-space: pre-wrap;
}

.message-assistant {
  align-self: stretch;
  color: var(--fg);
}

.message-error {
  align-self: stretch;
  border-left: 2px solid var(--danger);
  background: var(--danger-bg);
  color: #fecaca;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
}

.events-card {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--fg-subtle);
  font-size: 12px;
  font-family: var(--mono);
  padding-left: 12px;
  border-left: 1px solid var(--border);
}

.event-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.event-text {
  min-width: 0;
  overflow-wrap: anywhere;
  color: var(--fg-muted);
  font-style: italic;
}

.admin-panel {
  align-self: stretch;
  width: min(960px, 100%);
  margin: 0 auto;
}

.admin-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.admin-title {
  margin: 0;
  color: var(--fg);
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  text-transform: lowercase;
}

.admin-list {
  display: grid;
  gap: 8px;
}

.admin-session {
  display: grid;
  gap: 6px;
  border-left: 1px solid var(--border);
  padding-left: 10px;
  color: var(--fg-muted);
  font-size: 12px;
}

.admin-session-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  color: var(--fg-subtle);
  font-family: var(--mono);
  font-size: 11px;
}

.admin-question,
.admin-answer {
  margin: 0;
  overflow-wrap: anywhere;
}

.admin-answer {
  color: var(--fg-subtle);
}

.badge {
  display: inline-flex;
  align-items: center;
  flex: none;
  height: 18px;
  padding: 0 6px;
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: 0;
  text-transform: lowercase;
  white-space: nowrap;
}

.badge-secondary {
  background: var(--bg-elev);
  color: var(--fg);
}

.badge-destructive {
  background: var(--danger-bg);
  color: var(--danger);
}

.spinner {
  display: inline-block;
  flex: none;
  width: 10px;
  height: 10px;
  border: 1.5px solid var(--border-strong);
  border-top-color: var(--fg-muted);
  border-radius: 999px;
  animation: spin 0.7s linear infinite;
}

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

.md {
  font-size: 14px;
  line-height: 1.65;
}
.md > :first-child {
  margin-top: 0;
}
.md > :last-child {
  margin-bottom: 0;
}
.md p {
  margin: 0 0 0.75em;
}

.md pre {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  padding: 12px 14px;
  margin: 0.75em 0;
  font-size: 12.5px;
  line-height: 1.55;
}

.md code,
.event-row code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--bg-elev);
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  color: var(--fg);
}

.md pre code {
  background: transparent;
  padding: 0;
  font-size: inherit;
}

.md table {
  border-collapse: collapse;
  margin: 0.75em 0;
  font-size: 13px;
}
.md th,
.md td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.md th {
  background: var(--bg-elev);
  font-weight: 550;
}

.md ul,
.md ol {
  padding-left: 22px;
  margin: 0.5em 0 0.75em;
}
.md li {
  margin: 0.15em 0;
}

.md h1,
.md h2,
.md h3 {
  margin: 1.2em 0 0.4em;
  font-weight: 700;
  letter-spacing: 0;
}
.md h1 {
  font-size: 18px;
}
.md h2 {
  font-size: 16px;
}
.md h3 {
  font-size: 14px;
}

.md blockquote {
  margin: 0.75em 0;
  padding: 0 0 0 12px;
  border-left: 2px solid var(--border-strong);
  color: var(--fg-muted);
}

.sources {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 14px;
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  color: var(--fg-subtle);
  font-size: 12px;
  font-family: var(--mono);
}

.sources-label {
  grid-column: 1 / -1;
  color: var(--fg-muted);
  font-weight: 500;
}

.source-item {
  min-width: 0;
  overflow-wrap: anywhere;
}

.sources a,
.citation-link {
  color: var(--fg-muted);
}
.sources a:hover,
.citation-link:hover {
  color: var(--fg);
}

.composer {
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer-inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  width: min(720px, 100%);
  margin: 0 auto;
  padding: 14px 20px 18px;
}

.field {
  min-width: 0;
}

.input,
.textarea {
  display: block;
  width: 100%;
  border: 1px solid hsl(0 0% 15% / 0.18);
  border-radius: var(--radius);
  background: hsl(var(--foreground));
  color: var(--fg);
  outline: none;
  transition: border-color 0.12s ease;
}

.input {
  height: 32px;
  padding: 0 10px;
  font-size: 13px;
}
.textarea {
  min-height: 60px;
  resize: none;
  padding: 10px 12px;
  font-family: var(--chat);
}

.input::placeholder,
.textarea::placeholder {
  color: hsl(0 0% 15% / 0.48);
}

.input:hover,
.textarea:hover {
  border-color: var(--border-strong);
}
.input:focus-visible,
.textarea:focus-visible {
  border-color: var(--brand-red);
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 88px;
  height: 36px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: var(--accent-color);
  color: #fff;
  padding: 0 14px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
  transition:
    opacity 0.12s ease,
    background 0.12s ease;
}

.button:disabled {
  cursor: wait;
  opacity: 0.5;
}
.button:not(:disabled):hover {
  background: #bd0007;
}
.button:not(:disabled):active {
  opacity: 0.85;
}

.button .spinner {
  border-color: rgba(255, 255, 255, 0.32);
  border-top-color: #fff;
}

.composer-tools {
  display: flex;
  align-items: flex-end;
  align-self: stretch;
  min-width: 0;
}

@media (max-width: 720px) {
  .header-inner {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .usage,
  .auth-user {
    justify-self: start;
    text-align: left;
    align-items: flex-start;
  }
  .message-user {
    max-width: 88%;
  }
  .composer-inner {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
