:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #dde1e6;
  --text: #1f2430;
  --text-muted: #5c6370;
  --accent: #185fa5;
  --accent-text: #ffffff;
  --user-bubble: #185fa5;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #ffffff;
  --error-bg: #fbe7e7;
  --error-border: #b3261e;
  --error-text: #5c1a1a;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* `.screen[hidden]` (0,2,0) must outrank `.screen { display: flex }` (0,1,0)
   above it, or the [hidden] attribute — same specificity as the browser's
   default `[hidden] { display: none }` rule — loses the cascade and both
   screens render stacked in document flow at once. */
.screen[hidden] {
  display: none;
}

/* ---------------------------------------------------------------------- */
/* Token screen                                                           */
/* ---------------------------------------------------------------------- */

#token-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.token-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.brand-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  margin: 0 0 18px;
}

.hero-logo {
  width: 100%;
  max-width: 300px;
  height: auto;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.token-card h1 {
  margin: 0;
  font-size: 1.4rem;
}

.token-card .subtitle {
  margin: 0 0 20px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.token-card label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.token-card input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  margin-bottom: 14px;
}

.token-card button {
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.token-card button:hover {
  filter: brightness(1.05);
}

.token-card button:disabled,
.token-card input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.hint {
  margin: 16px 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.error-text {
  color: var(--error-border);
  font-size: 0.85rem;
  margin: 14px 0 0;
}

/* ---------------------------------------------------------------------- */
/* Chat screen                                                            */
/* ---------------------------------------------------------------------- */

#chat-screen {
  height: 100vh;
  height: 100dvh;
}

.chat-header {
  display: flex;
  flex-wrap: wrap;
  row-gap: 4px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.chat-header h1 {
  margin: 0;
  font-size: 1.1rem;
}

.chat-header-brand {
  display: flex;
  align-items: center;
  min-width: 0;
}

/* The society's own branding — separate from .chat-header-brand above,
   which is the product identity and stays the same for every society. */
.society-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.society-logo {
  height: 52px;
  width: auto;
  flex-shrink: 0;
}

.disclaimer-bar {
  margin: 0;
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--text-muted);
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  /* If the header wraps (.chat-header's flex-wrap, narrow viewports),
     this pushes actions to the right edge of their own second line
     instead of sitting flush left under the logo — space-between only
     works within a line that has two items to space apart, not a
     wrapped line with just this one. */
  margin-left: auto;
}

.header-action-sep {
  color: var(--border);
  font-size: 0.85rem;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 4px 6px;
}

.link-btn:hover {
  text-decoration: underline;
}

.message-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: min(640px, 85%);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg.user {
  align-self: flex-end;
}

.msg.assistant {
  align-self: flex-start;
}

.msg-text {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.45;
  font-size: 0.97rem;
  overflow-wrap: break-word;
  word-break: break-word;
}

.msg.user .msg-text {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 2px;
}

.msg.assistant .msg-text {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 2px;
}

.msg-text code {
  background: rgba(0, 0, 0, 0.06);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

.msg.user .msg-text code {
  background: rgba(255, 255, 255, 0.2);
}

.msg-text ul {
  margin: 6px 0;
  padding-left: 20px;
}

/* Wide tables scroll inside their own box rather than pushing the bubble
   (and the whole page) wider than the viewport — matters most on mobile. */
.msg-table-wrap {
  margin: 6px 0;
  max-width: 100%;
  overflow-x: auto;
}

.msg-table-wrap table {
  border-collapse: collapse;
  font-size: 0.9rem;
  white-space: nowrap;
}

.msg-table-wrap th,
.msg-table-wrap td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.msg-table-wrap th {
  background: var(--bg);
  font-weight: 600;
}

.sources,
.tools-used {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 0 4px;
}

.sources ul {
  margin: 4px 0 0;
  padding-left: 18px;
}

.sources li {
  margin-bottom: 2px;
  word-break: break-word;
}

.meta-label {
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.03em;
  margin-right: 4px;
}

.thinking .msg-text {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 12px 16px;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: blink 1.2s infinite ease-in-out;
}

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

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

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

.error-banner {
  align-self: stretch;
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.9rem;
}

.error-banner p {
  margin: 0;
}

.error-banner button {
  flex-shrink: 0;
  background: var(--error-border);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}

.chat-form {
  flex-shrink: 0;
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.chat-form textarea {
  flex: 1 1 auto;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  line-height: 1.4;
}

.chat-form button {
  flex-shrink: 0;
  padding: 0 18px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

.chat-form button:disabled,
.chat-form textarea:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ---------------------------------------------------------------------- */
/* Responsive                                                             */
/* ---------------------------------------------------------------------- */

@media (max-width: 640px) {
  .chat-header h1 {
    font-size: 1rem;
  }

  .society-logo {
    height: 40px;
  }

  .society-bar {
    padding: 6px 12px;
  }

  .disclaimer-bar {
    font-size: 0.7rem;
  }

  .msg {
    max-width: 92%;
  }

  .chat-form {
    padding: 10px 12px;
  }

  .message-list {
    padding: 12px;
  }
}
