/* Tiphu Chat — Claude-inspired theme */

:root {
  --bg: #faf9f5;
  --bg-elev: #f0efe7;
  --bg-input: #ffffff;
  --bg-sidebar: #f5f4ed;
  --border: #e3e1d4;
  --border-strong: #d3d1c3;
  --text: #2a2a26;
  --text-dim: #6b6960;
  --text-faint: #9a9789;
  --accent: #c96442;
  --accent-strong: #b3573a;
  --accent-soft: rgba(201, 100, 66, 0.08);
  --brand: #f0d800;
  --brand-strong: #d6c200;
  --brand-ink: #2a2a26;
  --user-bubble: #ece9dc;
  --danger: #c24343;
  --success: #5b8f60;
  --radius: 12px;
  --radius-lg: 20px;
  --maxw: 760px;
  --font-sans: "Roboto", system-ui, -apple-system, "Segoe UI", "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji";
  --font-serif: ui-serif, Tiempos, Georgia, Charter, "Iowan Old Style", "Apple Garamond", "Times New Roman", serif;
  --font-mono: "Roboto Mono", ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-family: var(--font-sans);
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #262624;
  --bg-elev: #30302e;
  --bg-input: #1f1f1d;
  --bg-sidebar: #1f1f1d;
  --border: #3f3f3d;
  --border-strong: #4f4f4c;
  --text: #faf9f5;
  --text-dim: #b5b3aa;
  --text-faint: #807e76;
  --accent: #d97757;
  --accent-strong: #e0876b;
  --accent-soft: rgba(217, 119, 87, 0.14);
  --user-bubble: #30302e;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme]) {
    --bg: #262624;
    --bg-elev: #30302e;
    --bg-input: #1f1f1d;
    --bg-sidebar: #1f1f1d;
    --border: #3f3f3d;
    --border-strong: #4f4f4c;
    --text: #faf9f5;
    --text-dim: #b5b3aa;
    --text-faint: #807e76;
    --accent: #d97757;
    --accent-strong: #e0876b;
    --accent-soft: rgba(217, 119, 87, 0.14);
    --user-bubble: #30302e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-size: 15px; line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
body { min-height: 100vh; }
body.app {
  display: block;
  height: 100vh;       /* fallback */
  height: 100dvh;      /* iOS keyboard-aware */
  overflow: hidden;
}
/* JS-set custom var when visualViewport changes (iOS keyboard) */
body.app.kbd-open { height: var(--vvh, 100dvh); }

a { color: var(--accent); }
a:hover { color: var(--accent-strong); }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px; border-radius: 10px;
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border);
  cursor: pointer; font: inherit; font-size: 14px;
  text-decoration: none;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.btn:hover { background: var(--border); border-color: var(--border-strong); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 500; }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn.danger:hover { background: rgba(194, 67, 67, 0.1); }
.btn.icon { padding: 6px 10px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn.ghost:hover { background: var(--bg-elev); color: var(--text); }

.link-plain { color: inherit; text-decoration: none; }
.muted { color: var(--text-dim); }
.small { font-size: 13px; }

/* ===== Auth pages — ChatGPT-style modal ===== */
body.centered {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  padding: 24px 16px;
}
.auth-card {
  width: 100%; max-width: 440px;
  background: transparent;
  display: flex; flex-direction: column; align-items: stretch;
}
.auth-brand {
  display: flex; justify-content: center; margin-bottom: 22px;
}
.auth-brand img {
  width: 56px; height: 56px; object-fit: contain;
}
.auth-card h1 {
  margin: 0 0 12px; font-size: 32px; font-weight: 700;
  letter-spacing: -0.5px; line-height: 1.15;
  text-align: center; color: var(--text);
  font-family: var(--font-sans);
}
.auth-sub {
  margin: 0 0 28px; color: var(--text-dim);
  text-align: center; font-size: 15px; line-height: 1.45;
  max-width: 360px; align-self: center;
}
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form .field { display: flex; flex-direction: column; }
.auth-form input {
  padding: 14px 18px; border-radius: 9999px;
  background: transparent; color: var(--text);
  border: 1px solid var(--border-strong);
  font: inherit; font-size: 15px; outline: none;
  transition: border-color 0.12s, background 0.12s;
}
.auth-form input::placeholder { color: var(--text-faint); }
.auth-form input:focus { border-color: var(--text); }
.auth-form button[type="submit"] {
  margin-top: 4px;
  padding: 14px 18px;
  border-radius: 9999px;
  border: 0;
  background: var(--brand);
  color: var(--brand-ink);
  font: inherit; font-size: 15.5px; font-weight: 700;
  cursor: pointer;
  transition: background 0.12s;
}
.auth-form button[type="submit"]:hover { background: var(--brand-strong); }
.auth-footer {
  margin-top: 22px;
  text-align: center;
  font-size: 14px; color: var(--text-dim);
}
.auth-footer a { color: var(--text); text-decoration: underline; text-decoration-color: var(--text-dim); text-underline-offset: 3px; }
.auth-footer a:hover { color: var(--accent); }
.auth-card .alert {
  padding: 11px 16px; border-radius: 12px;
  background: rgba(194, 67, 67, 0.08); color: var(--danger);
  border: 1px solid rgba(194, 67, 67, 0.2);
  margin-bottom: 14px; font-size: 14px;
  text-align: center;
}

/* Mobile auth */
@media (max-width: 480px) {
  .auth-card h1 { font-size: 26px; }
  .auth-sub { font-size: 14px; margin-bottom: 22px; }
  .auth-brand img { width: 48px; height: 48px; }
}

/* Legacy .card (kept for other settings pages that may still use it) */
.card {
  width: 100%; max-width: 400px;
  background: var(--bg);
  padding: 32px 28px;
}
.card-brand {
  color: var(--text-dim); font-size: 13px; margin-bottom: 4px;
  display: flex; align-items: center; gap: 6px;
}
.card-brand .logo { color: var(--accent); font-size: 16px; }
.card-brand .logo-img { width: 20px; height: 20px; object-fit: contain; display: block; }
.card h1 {
  margin: 0 0 24px; font-size: 32px; font-weight: 500;
  font-family: var(--font-serif); letter-spacing: -0.5px;
}
.form { display: flex; flex-direction: column; gap: 18px; }
.form label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px; color: var(--text-dim);
}
.form input {
  padding: 11px 14px; border-radius: 10px;
  background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); font: inherit; font-size: 15px; outline: none;
  transition: border-color 0.12s;
}
.form input:focus { border-color: var(--accent); }
.form button { margin-top: 8px; padding: 11px 14px; font-size: 15px; }
.alert {
  padding: 11px 14px; border-radius: 10px;
  background: rgba(194, 67, 67, 0.08); color: var(--danger);
  border: 1px solid rgba(194, 67, 67, 0.2);
  margin-bottom: 16px; font-size: 14px;
}
.alert.success {
  background: var(--accent-soft); color: var(--accent);
  border-color: rgba(201, 100, 66, 0.2);
}
.key-reveal {
  display: block; margin-top: 10px;
  font-family: var(--font-mono);
  word-break: break-all; user-select: all;
  background: var(--bg-elev); padding: 10px 12px; border-radius: 8px;
  color: var(--text); font-size: 13px;
}

/* ===== App layout ===== */
.layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;        /* fallback */
  height: 100dvh;       /* shrink when iOS keyboard opens */
}
body.app.kbd-open .layout { height: var(--vvh, 100dvh); }

.sidebar {
  display: flex; flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  min-width: 0;
  min-height: 0;
  height: 100%;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 14px 8px;
  gap: 8px;
  flex: 0 0 auto;
}
.sidebar-head .brand {
  display: flex; align-items: center; gap: 8px;
  font-weight: 600; font-size: 14px; color: var(--text);
}
.sidebar-head .brand .logo { color: var(--accent); font-size: 16px; }
.sidebar-head .brand .logo-img { width: 22px; height: 22px; object-fit: contain; display: block; }
.btn-new {
  display: flex; align-items: center; gap: 8px;
  margin: 6px 10px 14px;
  padding: 9px 12px; border-radius: 10px;
  background: transparent; color: var(--text);
  border: 1px solid var(--border); cursor: pointer;
  font: inherit; font-size: 13.5px;
  transition: background 0.12s, border-color 0.12s;
}
.btn-new:hover { background: var(--bg-elev); border-color: var(--border-strong); }
.btn-new::before { content: "✎"; color: var(--text-dim); }
.conv-section-label {
  padding: 12px 18px 6px;
  font-size: 11.5px; font-weight: 600;
  color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.6px;
}
.conv-list {
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto;
  padding: 0 8px 20px;
  display: flex; flex-direction: column; gap: 1px;
}
.conv-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 10px; border-radius: 8px;
  color: var(--text-dim); text-decoration: none;
  font-size: 13.5px; cursor: pointer;
  min-width: 0;
  transition: background 0.1s, color 0.1s;
}
.conv-item:hover { background: var(--bg-elev); color: var(--text); }
.conv-item.active { background: var(--bg-elev); color: var(--text); }
.conv-item-title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-item-del {
  background: transparent; border: 0; color: var(--text-faint);
  cursor: pointer; padding: 2px 6px; border-radius: 4px;
  font-size: 16px; line-height: 1; opacity: 0;
}
.conv-item:hover .conv-item-del { opacity: 1; }
.conv-item-del:hover { color: var(--danger); }

.sidebar-foot {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
}
.user-menu {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 8px; border-radius: 8px;
  cursor: pointer;
  transition: background 0.1s;
  color: var(--text);
}
.user-menu:hover { background: var(--bg-elev); }
.user-menu-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; flex: 0 0 28px;
}
.user-menu-name {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13.5px;
}
.user-menu-chev { color: var(--text-faint); font-size: 12px; }

.user-popover {
  position: absolute;
  bottom: 60px; left: 12px; right: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.12);
  display: none;
  flex-direction: column; gap: 1px;
  z-index: 100;
}
.user-popover.open { display: flex; }
.user-popover a, .user-popover button {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-radius: 8px;
  font: inherit; font-size: 13.5px;
  background: transparent; border: 0; cursor: pointer;
  text-align: left; text-decoration: none; color: var(--text);
}
.user-popover a:hover, .user-popover button:hover { background: var(--bg-elev); }
.user-popover .divider { height: 1px; background: var(--border); margin: 4px 0; }
.user-popover .muted-label {
  padding: 8px 12px 4px; font-size: 11.5px;
  color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.6px;
}

/* ===== Main ===== */
.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  overflow: hidden;
  background: var(--bg);
}
.main-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 20px;
  height: 56px;
  border-bottom: 1px solid var(--border);
}
.conv-title {
  color: var(--text); font-size: 14px; font-weight: 500;
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#btn-toggle-sidebar { display: none; }

/* ===== Chat area ===== */
.chat {
  width: 100%;
  overflow-y: auto;
  padding: 24px 24px 12px;
  min-height: 0;
}
.empty {
  max-width: var(--maxw); margin: 12vh auto 0;
  text-align: center; color: var(--text-dim);
}
.empty h1 {
  color: var(--text); font-weight: 500; font-size: 32px;
  font-family: var(--font-serif); letter-spacing: -0.5px;
  margin: 0 0 10px;
}
.empty p { margin: 0; font-size: 15px; }

.messages {
  max-width: var(--maxw); margin: 0 auto;
  display: flex; flex-direction: column; gap: 24px;
}
.msg { display: flex; }
.msg-col { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.msg .bubble {
  padding: 10px 16px; border-radius: 18px;
  white-space: pre-wrap; word-wrap: break-word;
  overflow-wrap: anywhere;
  font-size: 15px; line-height: 1.6;
  max-width: 100%;
}

/* User: right-aligned pill */
.msg.user { justify-content: flex-end; }
.msg.user .msg-col { align-items: flex-end; max-width: 80%; }
.msg.user .bubble {
  background: var(--user-bubble);
  border-radius: 18px;
}

/* Assistant: no bubble, full width — markdown HTML handles whitespace, override pre-wrap */
.msg.assistant .msg-col { width: 100%; }
.msg.assistant .bubble {
  background: transparent;
  padding: 0;
  border-radius: 0;
  white-space: normal;
  font-size: 15.5px;
}

.msg .bubble.streaming::after {
  content: "";
  display: inline-block;
  width: 16px; height: 16px;
  margin-left: 6px;
  vertical-align: -3px;
  background-image: url('/static/img/tiphu-logo.png');
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  animation: think-pulse 1.1s ease-in-out infinite;
  transform-origin: center;
}
@keyframes think-pulse {
  0%, 100% { transform: scale(0.85); opacity: 0.7; }
  50%       { transform: scale(1.15); opacity: 1; }
}
.msg.error .bubble { color: var(--danger); }

.msg-actions {
  display: flex; gap: 2px;
  opacity: 0; transition: opacity 0.15s;
  margin-top: 2px;
}
.msg:hover .msg-actions, .msg.streaming .msg-actions { opacity: 1; }
.msg-action {
  background: transparent; border: 0;
  color: var(--text-faint); cursor: pointer;
  padding: 5px 8px; border-radius: 6px;
  font-size: 13px; line-height: 1;
  transition: background 0.1s, color 0.1s;
}
.msg-action:hover { background: var(--bg-elev); color: var(--text); }

/* ===== Composer ===== */
.composer-wrap {
  padding: 0 24px 18px;
  background: linear-gradient(180deg, transparent 0%, var(--bg) 30%);
}
.composer {
  display: flex; flex-direction: column;
  max-width: var(--maxw); width: 100%; margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 12px 8px;
  box-shadow: 0 1px 6px rgba(0,0,0,0.04);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 2px 14px rgba(0,0,0,0.06);
}
.composer textarea {
  flex: 1; resize: none;
  padding: 6px 4px;
  background: transparent; color: var(--text);
  border: 0; outline: none;
  font: inherit; font-size: 15px; line-height: 1.5;
  max-height: 200px; overflow-y: auto;
}
.composer textarea::placeholder { color: var(--text-faint); }
.composer-toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 2px 0;
}
.composer-toolbar .spacer { flex: 1; }

.model-picker {
  font: inherit; font-size: 13px;
  background: transparent; color: var(--text-dim);
  border: 0; border-radius: 6px;
  padding: 4px 22px 4px 8px;
  cursor: pointer; outline: none;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'><path fill='%239a9789' d='M5 7L1 3h8z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 6px center;
}
.model-picker:hover { background-color: var(--bg-elev); }

/* Always-on web badge */
.badge-web {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 10px; border-radius: 9999px;
  background: var(--brand); color: var(--brand-ink);
  font: inherit; font-size: 12px; font-weight: 600;
  cursor: help; user-select: none;
}
@media (max-width: 480px) {
  .badge-web { padding: 3px 8px; font-size: 11.5px; }
}

.composer button.send {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  border: 0; cursor: pointer;
  font-size: 14px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.12s;
  flex: 0 0 32px;
}
.composer button.send:hover { background: var(--brand-strong); }
.composer button.send:disabled { background: var(--border-strong); color: var(--bg); cursor: not-allowed; }
.composer button.send.stop {
  background: var(--brand); color: var(--brand-ink);
}
.composer button.send.stop:hover { background: var(--brand-strong); }

/* ===== Markdown ===== */
.msg.assistant .bubble { line-height: 1.6; }
.msg.assistant .bubble > *:first-child { margin-top: 0 !important; }
.msg.assistant .bubble > *:last-child { margin-bottom: 0 !important; }
.msg.assistant .bubble p { margin: 0 0 10px; }
.msg.assistant .bubble p:empty { display: none; }
.msg.assistant .bubble br + br { display: none; }
.msg.assistant .bubble h1,
.msg.assistant .bubble h2,
.msg.assistant .bubble h3,
.msg.assistant .bubble h4 {
  margin: 16px 0 6px; font-weight: 700;
  font-family: var(--font-sans); letter-spacing: -0.1px;
  line-height: 1.3;
}
.msg.assistant .bubble h1 { font-size: 22px; }
.msg.assistant .bubble h2 { font-size: 19px; }
.msg.assistant .bubble h3 { font-size: 16.5px; }
.msg.assistant .bubble h4 { font-size: 15.5px; }
.msg.assistant .bubble ul,
.msg.assistant .bubble ol { padding-left: 22px; margin: 4px 0 10px; }
.msg.assistant .bubble li { margin: 0; line-height: 1.55; }
.msg.assistant .bubble li + li { margin-top: 2px; }
.msg.assistant .bubble li p { margin: 0; }
.msg.assistant .bubble li > p:not(:last-child) { margin-bottom: 2px; }
.msg.assistant .bubble li > ul,
.msg.assistant .bubble li > ol { margin: 2px 0; }
.msg.assistant .bubble hr { margin: 14px 0; border: 0; border-top: 1px solid var(--border); }
.msg.assistant .bubble a { color: var(--accent); text-decoration: underline; }
.msg.assistant .bubble code {
  font-family: var(--font-mono);
  font-size: 13.5px;
  background: var(--bg-elev);
  padding: 2px 6px; border-radius: 4px;
}
.msg.assistant .bubble pre {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 12px 0;
}
.msg.assistant .bubble pre code {
  background: transparent; padding: 0;
  font-size: 13.5px;
}
.msg.assistant .bubble blockquote {
  border-left: 3px solid var(--border-strong);
  padding-left: 14px; margin: 10px 0;
  color: var(--text-dim);
}
.msg.assistant .bubble table {
  border-collapse: collapse; margin: 10px 0;
}
.msg.assistant .bubble th,
.msg.assistant .bubble td {
  border: 1px solid var(--border); padding: 6px 12px;
}
.msg.assistant .bubble th { background: var(--bg-elev); font-weight: 600; }

/* ===== Settings page ===== */
body.page { display: block; }
.settings-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}
.settings-nav {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 18px 10px;
  display: flex; flex-direction: column; gap: 2px;
}
.settings-nav-back {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-radius: 8px;
  color: var(--text-dim); text-decoration: none;
  font-size: 14px; margin-bottom: 14px;
}
.settings-nav-back:hover { background: var(--bg-elev); color: var(--text); }
.settings-nav h3 {
  margin: 0 0 6px; padding: 6px 12px;
  font-size: 11.5px; font-weight: 600;
  color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.6px;
}
.settings-nav a {
  padding: 7px 12px; border-radius: 8px;
  color: var(--text-dim); text-decoration: none;
  font-size: 14px;
}
.settings-nav a:hover { background: var(--bg-elev); color: var(--text); }
.settings-nav a.active { background: var(--bg-elev); color: var(--text); font-weight: 500; }

.settings-content {
  padding: 40px 48px;
  max-width: 760px;
}
.settings-content h1 {
  margin: 0 0 6px; font-size: 28px; font-weight: 500;
  font-family: var(--font-serif); letter-spacing: -0.5px;
}
.settings-content .lead {
  color: var(--text-dim); margin: 0 0 28px; font-size: 14.5px;
}
.settings-section { margin-bottom: 36px; }
.settings-section h2 {
  margin: 0 0 14px; font-size: 16px; font-weight: 600;
  padding-bottom: 6px;
}
.row {
  display: flex; gap: 10px; margin: 16px 0;
}
.row input {
  flex: 1; padding: 10px 12px; border-radius: 10px;
  background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); font: inherit; outline: none;
}
.row input:focus { border-color: var(--accent); }
.code {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px; white-space: pre; color: var(--text);
}
.table { width: 100%; border-collapse: collapse; margin-top: 16px; }
.table th, .table td {
  text-align: left; padding: 10px 12px;
  border-bottom: 1px solid var(--border); font-size: 14px;
}
.table th {
  font-weight: 500; color: var(--text-faint);
  font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.6px;
}
.table tr.revoked { opacity: 0.5; }
.badge-on {
  background: rgba(91, 143, 96, 0.15); color: var(--success);
  padding: 3px 10px; border-radius: 999px; font-size: 12px;
}

/* Theme toggle */
.theme-toggle {
  display: inline-flex; gap: 6px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 10px; padding: 3px;
}
.theme-toggle button {
  background: transparent; border: 0;
  padding: 6px 14px; border-radius: 8px;
  font: inherit; font-size: 13px; cursor: pointer;
  color: var(--text-dim);
}
.theme-toggle button.active {
  background: var(--bg); color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

/* ===== Responsive ===== */
@media (max-width: 760px) {
  .layout, .settings-layout { grid-template-columns: 1fr; }
  .sidebar, .settings-nav {
    position: fixed; inset: 0 auto 0 0;
    width: 84vw; max-width: 320px; z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s;
    box-shadow: 4px 0 20px rgba(0,0,0,0.25);
  }
  .sidebar.open { transform: translateX(0); }
  #btn-toggle-sidebar { display: inline-flex; }
  .settings-content { padding: 24px 20px; }

  /* Chat area: tighter padding so messages don't tràn */
  .chat { padding: 14px 12px 8px; }
  .messages { gap: 18px; }
  .msg.user .msg-col { max-width: 88%; }
  .msg .bubble { font-size: 15px; }
  .msg.assistant .bubble { font-size: 15px; }
  .msg.assistant .bubble h1 { font-size: 19px; }
  .msg.assistant .bubble h2 { font-size: 17px; }
  .msg.assistant .bubble h3 { font-size: 15.5px; }
  .msg.assistant .bubble ul,
  .msg.assistant .bubble ol { padding-left: 20px; }

  /* Code blocks: cho phép scroll ngang, không vỡ layout */
  .msg.assistant .bubble pre {
    padding: 10px 12px;
    font-size: 12.5px;
    max-width: 100%;
    overflow-x: auto;
  }
  .msg.assistant .bubble table {
    display: block;
    overflow-x: auto;
    max-width: 100%;
  }

  /* Composer: chừa chỗ cho nút send */
  .composer-wrap { padding: 0 12px 12px; }
  .composer { padding: 8px 10px 6px; border-radius: 16px; }
  .composer textarea { font-size: 16px; /* iOS prevent zoom on focus */ }

  /* Sources block trên mobile */
  .sources { max-width: 100%; font-size: 13px; }
}

/* Phòng cho devices rất hẹp (iPhone SE, etc.) */
@media (max-width: 380px) {
  .chat { padding: 10px 8px 6px; }
  .msg.user .msg-col { max-width: 92%; }
  .composer-wrap { padding: 0 8px 10px; }
}

/* ===== Sources (Perplexica) — collapsible ===== */
.sources {
  margin-top: 10px;
  max-width: 720px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev);
  font-size: 13px;
  overflow: hidden;
}
.sources-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px;
  background: transparent;
  border: 0;
  cursor: pointer;
  color: var(--text-dim);
  font: inherit;
  font-weight: 500;
  text-align: left;
}
.sources-toggle:hover { background: color-mix(in srgb, var(--bg) 70%, var(--text) 5%); color: var(--text); }
.sources-toggle .chev {
  display: inline-block;
  transition: transform 0.15s;
  font-size: 10px;
  width: 12px;
}
.sources.open .sources-toggle .chev { transform: rotate(90deg); }
.sources-body {
  display: none;
  padding: 4px 14px 12px;
  border-top: 1px solid var(--border);
}
.sources.open .sources-body { display: block; }
.sources-list {
  margin: 8px 0 0;
  padding-left: 22px;
  color: var(--text);
}
.sources-list li { margin: 4px 0; line-height: 1.45; }
.sources-list a { color: var(--text); text-decoration: underline; text-decoration-color: var(--text-dim); text-underline-offset: 2px; word-break: break-word; }
.sources-list a:hover { text-decoration-color: var(--text); color: var(--accent); }

/* ===== Clerk auth gate (full-page sign-in) ===== */
.auth-gate {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  padding: 20px;
  z-index: 1000;
  overflow-y: auto;
}
.auth-gate .auth-card { width: 100%; max-width: 440px; }
