:root {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --text: #14221c;
  --muted: #5c6f66;
  --border: #d8e3dc;
  --accent: #0d6b4d;
  --accent-soft: #e6f2ec;
  --danger: #b91c1c;
  --danger-soft: #fef2f2;
  --info: #1e40af;
  --info-soft: #eff6ff;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20, 34, 28, 0.08), 0 8px 24px rgba(20, 34, 28, 0.06);
  --page-pad: 1rem;
  --touch-min: 2.75rem;
  font-family:
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
}

html {
  -webkit-text-size-adjust: 100%;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overflow-x: clip;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0)
    env(safe-area-inset-left, 0);
  display: flex;
  flex-direction: column;
}

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

/* Author display rules (e.g. .composer { display: grid }) must not override [hidden]. */
[hidden] {
  display: none !important;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem var(--page-pad);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

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

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 650;
}

.brand-sub {
  color: var(--muted);
  font-size: 0.8125rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.8125rem;
  font-weight: 500;
}

.user-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

button {
  font: inherit;
  cursor: pointer;
  min-height: var(--touch-min);
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 0.5rem 0.9rem;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(0.95);
}

.btn-secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

.btn-text {
  background: transparent;
  color: var(--muted);
  border: none;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 52rem;
  width: 100%;
  margin: 0 auto;
  padding: var(--page-pad);
  gap: 0.75rem;
  min-height: 0;
}

.demo-banner,
.status-banner {
  margin: 0;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
}

.demo-banner {
  background: #fff7ed;
  color: #9a3412;
  border: 1px solid #fdba74;
}

.status-banner--error {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid #fecaca;
}

.status-banner--info {
  background: var(--info-soft);
  color: var(--info);
  border: 1px solid #bfdbfe;
}

.signed-out-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem 1.25rem;
  min-height: 12rem;
}

.signed-out-copy {
  margin: 0;
  color: var(--muted);
  font-size: 1.05rem;
  text-align: center;
  max-width: 28rem;
}

.chat-panel {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  overflow-y: auto;
  min-height: 12rem;
}

.empty-state {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.message {
  padding: 0.75rem 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
}

.message--user {
  background: var(--accent-soft);
  border-color: #b7d9c8;
}

.message-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.25rem 0.75rem;
  margin-bottom: 0.25rem;
}

.message-role {
  font-size: 0.75rem;
  font-weight: 650;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.message-time {
  position: relative;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
  white-space: nowrap;
  text-transform: none;
  letter-spacing: normal;
}

.message-body {
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.message-body--md {
  white-space: normal;
}

.message-body--md > :first-child {
  margin-top: 0;
}

.message-body--md > :last-child {
  margin-bottom: 0;
}

.message-body--md p,
.message-body--md ul,
.message-body--md ol,
.message-body--md blockquote,
.message-body--md pre {
  margin: 0.5rem 0;
}

.message-body--md h3,
.message-body--md h4,
.message-body--md h5 {
  margin: 0.65rem 0 0.35rem;
  font-size: 1rem;
  font-weight: 650;
  color: var(--text);
}

.message-body--md h3 {
  font-size: 1.05rem;
}

.message-body--md ul,
.message-body--md ol {
  padding-left: 1.25rem;
}

.message-body--md blockquote {
  border-left: 3px solid var(--border);
  padding-left: 0.75rem;
  color: var(--muted);
}

.message-body--md pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.65rem;
  overflow-x: auto;
}

.message-body--md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  overflow-wrap: anywhere;
}

.message-body--md a {
  color: var(--accent);
  overflow-wrap: anywhere;
}

.citations {
  margin-top: 0.65rem;
  padding-top: 0.55rem;
  border-top: 1px dashed var(--border);
  font-size: 0.8125rem;
}

.citations-heading {
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.citations ul {
  margin: 0;
  padding-left: 1.1rem;
}

.citation-title {
  font-weight: 550;
}

.citation-uri {
  color: var(--muted);
  word-break: break-all;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.65rem;
  box-shadow: var(--shadow);
}

.composer textarea {
  font: inherit;
  width: 100%;
  min-height: var(--touch-min);
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  color: var(--text);
  background: var(--bg);
}

.composer textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

@media (min-width: 640px) {
  :root {
    --page-pad: 1.5rem;
  }

  .brand h1 {
    font-size: 1.25rem;
  }
}

@media (max-width: 480px) {
  .composer {
    grid-template-columns: 1fr;
  }

  .composer .btn-primary {
    width: 100%;
  }
}
