/* ─── theme tokens ─────────────────────────────────────────────────────── */
:root {
  --bg: #f7e7c2;
  --bg-elev: #fff8e8;
  --bg-input: #fffaf0;
  --fg: #2a1f12;
  --fg-muted: #6b5639;
  --accent: #8a4a1a;
  --accent-hover: #6a3812;
  --border: rgba(138, 74, 26, 0.22);
  --bubble-user: #8a4a1a;
  --bubble-user-fg: #fff8e8;
  --bubble-asst: #fff8e8;
  --bubble-asst-fg: #2a1f12;
  --bubble-system: rgba(138, 74, 26, 0.08);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --radius: 14px;
  --radius-sm: 10px;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  --max-w: 760px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c140b;
    --bg-elev: #2a1f12;
    --bg-input: #221710;
    --fg: #f1e4cb;
    --fg-muted: #b89a73;
    --accent: #d4894a;
    --accent-hover: #e89a5b;
    --border: rgba(212, 137, 74, 0.25);
    --bubble-user: #d4894a;
    --bubble-user-fg: #1c140b;
    --bubble-asst: #2a1f12;
    --bubble-asst-fg: #f1e4cb;
    --bubble-system: rgba(212, 137, 74, 0.08);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }
}

:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }
:root[data-theme="dark"]  body { --bg: #1c140b; --bg-elev: #2a1f12; --bg-input: #221710; --fg: #f1e4cb; --fg-muted: #b89a73; --accent: #d4894a; --accent-hover: #e89a5b; --border: rgba(212, 137, 74, 0.25); --bubble-user: #d4894a; --bubble-user-fg: #1c140b; --bubble-asst: #2a1f12; --bubble-asst-fg: #f1e4cb; --bubble-system: rgba(212, 137, 74, 0.08); --shadow: 0 1px 3px rgba(0, 0, 0, 0.3); }
:root[data-theme="light"] body { --bg: #f7e7c2; --bg-elev: #fff8e8; --bg-input: #fffaf0; --fg: #2a1f12; --fg-muted: #6b5639; --accent: #8a4a1a; --accent-hover: #6a3812; --border: rgba(138, 74, 26, 0.22); --bubble-user: #8a4a1a; --bubble-user-fg: #fff8e8; --bubble-asst: #fff8e8; --bubble-asst-fg: #2a1f12; --bubble-system: rgba(138, 74, 26, 0.08); --shadow: 0 1px 3px rgba(0, 0, 0, 0.08); }

/* ─── base ────────────────────────────────────────────────────────────── */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ─── preview banner ──────────────────────────────────────────────────── */
.preview-banner {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.4rem 0.85rem;
  background: repeating-linear-gradient(
    -45deg,
    rgba(212, 137, 74, 0.2) 0 10px,
    rgba(212, 137, 74, 0.08) 10px 20px
  );
  color: var(--fg);
  font-size: 0.82rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.preview-banner code {
  background: rgba(0, 0, 0, 0.12);
  padding: 0.05em 0.4em; border-radius: 4px; font-size: 0.95em;
}
.preview-banner i { color: var(--accent); }

/* ─── header ──────────────────────────────────────────────────────────── */
.tavern-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 0.6rem; }
.brand-mark { font-size: 1.6rem; }
.brand-text h1 { margin: 0; font-family: var(--font-serif); font-size: 1.15rem; font-weight: 600; line-height: 1.1; }
.brand-text .subtitle { margin: 0; font-size: 0.78rem; color: var(--fg-muted); }

.header-actions { display: flex; gap: 0.25rem; }
.icon-btn {
  background: transparent; border: none; color: var(--fg);
  padding: 0.4rem 0.5rem; font-size: 1.15rem; cursor: pointer; border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--bubble-system); }

/* ─── chat log ────────────────────────────────────────────────────────── */
.chat-log {
  flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain;
  padding: 1rem;
  display: flex; flex-direction: column; gap: 0.75rem;
  scroll-behavior: smooth;
}
.chat-log > * { max-width: var(--max-w); width: 100%; margin-inline: auto; }

.msg { display: flex; flex-direction: column; }
.msg-body {
  padding: 0.7rem 0.9rem; border-radius: var(--radius);
  line-height: 1.5; word-wrap: break-word; overflow-wrap: anywhere;
  box-shadow: var(--shadow);
}
.msg-body p { margin: 0 0 0.5rem; } .msg-body p:last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { margin: 0.5rem 0; padding-left: 1.4rem; }
.msg-body pre { background: rgba(0,0,0,0.06); padding: 0.6rem; border-radius: var(--radius-sm); overflow-x: auto; font-size: 0.85em; }
.msg-body code { background: rgba(0,0,0,0.06); padding: 0.1em 0.35em; border-radius: 4px; font-size: 0.9em; }
.msg-body table { border-collapse: collapse; margin: 0.5rem 0; font-size: 0.9em; width: 100%; }
.msg-body th, .msg-body td { border: 1px solid var(--border); padding: 0.3rem 0.5rem; text-align: left; }
.msg-body th { background: var(--bubble-system); }
.msg-body a { color: var(--accent); text-decoration: underline; }
.hint { font-size: 0.85rem; color: var(--fg-muted); margin-top: 0.5rem !important; }
.hint em { font-style: normal; background: var(--bubble-system); padding: 0.1em 0.35em; border-radius: 4px; }

.msg-user { align-items: flex-end; }
.msg-user .msg-body {
  background: var(--bubble-user); color: var(--bubble-user-fg);
  border-bottom-right-radius: 4px;
}
.msg-assistant { align-items: flex-start; }
.msg-assistant .msg-body {
  background: var(--bubble-asst); color: var(--bubble-asst-fg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-system .msg-body {
  background: var(--bubble-system); color: var(--fg-muted);
  font-size: 0.92em; text-align: center; border-radius: var(--radius-sm);
}
.msg-error .msg-body {
  background: rgba(180, 50, 50, 0.12); color: #b03b3b; border: 1px solid rgba(180, 50, 50, 0.25);
}

.tool-call, .status-note {
  font-size: 0.82em; color: var(--fg-muted); font-style: italic;
  padding: 0.35rem 0.65rem; background: var(--bubble-system); border-radius: var(--radius-sm);
  margin: 0.3rem 0 0.4rem; display: inline-flex; align-items: center; gap: 0.4rem; max-width: 100%;
}
.tool-call i, .status-note i { font-style: normal; }

/* sources block under an assistant message */
.sources-block {
  margin-top: 0.5rem;
  background: var(--bubble-system);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.7rem;
  font-size: 0.88em;
}
.sources-block summary {
  cursor: pointer; user-select: none; color: var(--fg-muted);
  list-style: none; display: flex; align-items: center; gap: 0.4rem;
  padding: 0.1rem 0;
}
.sources-block summary::-webkit-details-marker { display: none; }
.sources-block summary i { color: var(--accent); }
.sources-block[open] summary { margin-bottom: 0.3rem; }
.sources-list { margin: 0; padding-left: 1.4rem; display: flex; flex-direction: column; gap: 0.5rem; }
.sources-list li { padding: 0.2rem 0; }
.source-head { margin-bottom: 0.25rem; }
.source-head a { color: var(--accent); text-decoration: none; display: inline-flex; align-items: center; gap: 0.35rem; }
.source-head a:hover { text-decoration: underline; }
.source-head i { font-size: 0.85em; }
.source-snippet {
  margin: 0.2rem 0 0; padding: 0.4rem 0.6rem;
  background: var(--bg-input); border-left: 2px solid var(--accent);
  border-radius: 4px; color: var(--fg-muted);
  font-size: 0.94em; font-style: italic;
  white-space: pre-wrap;
}

.cursor::after {
  content: "▌"; display: inline-block; margin-left: 1px;
  animation: blink 1s steps(1) infinite; color: var(--fg-muted);
}
@keyframes blink { 50% { opacity: 0; } }

/* ─── input ───────────────────────────────────────────────────────────── */
.chat-input {
  display: flex; align-items: flex-end; gap: 0.5rem;
  padding: 0.65rem 0.85rem;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
}
.chat-input textarea {
  flex: 1; resize: none; padding: 0.65rem 0.85rem;
  background: var(--bg-input); color: var(--fg);
  border: 1px solid var(--border); border-radius: var(--radius);
  font-family: inherit; font-size: 1rem; line-height: 1.4;
  max-height: 30vh; min-height: 2.5rem; overflow-y: auto;
  outline: none;
}
.chat-input textarea:focus { border-color: var(--accent); }
.chat-input button {
  background: var(--accent); color: var(--bubble-user-fg);
  border: none; border-radius: var(--radius);
  padding: 0.6rem 0.8rem; cursor: pointer; height: 2.5rem; width: 2.8rem;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.chat-input button:hover:not(:disabled) { background: var(--accent-hover); }
.chat-input button:disabled { opacity: 0.5; cursor: not-allowed; }
.cf-turnstile { position: absolute; bottom: 100%; right: 0; }

/* ─── footer ──────────────────────────────────────────────────────────── */
.tavern-footer {
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 0.5rem;
  padding: 0.4rem 1rem;
  font-size: 0.72rem; color: var(--fg-muted);
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.tavern-footer a { color: var(--accent); text-decoration: none; }
.tavern-footer a:hover { text-decoration: underline; }

/* ─── responsive ──────────────────────────────────────────────────────── */
@media (min-width: 720px) {
  .brand-text h1 { font-size: 1.3rem; }
  .chat-log { padding: 1.5rem 1rem; gap: 1rem; }
  .msg-body { max-width: 80%; }
  .msg-user, .msg-assistant { display: block; }
  .msg-user .msg-body { margin-left: auto; }
}
