:root {
  color-scheme: dark;
  --bg: #0e0e0e;
  --side: #141414;
  --panel: #1b1b1b;
  --line: #2c2c2c;
  --text: #f3f3f3;
  --muted: #9b9b9b;
  --user: #f4f1ea;
  --user-text: #161616;
  --danger: #e7a0a0;
  --gold: #d7b56d;
  --shadow: 0 16px 50px rgba(0, 0, 0, 0.35);
  --sans: "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --serif: "Iowan Old Style", Palatino, "Palatino Linotype", Georgia, serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--sans);
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
[hidden] { display: none !important; }

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

.boot {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  color: var(--muted);
}

.brand {
  margin: 0;
  font-family: var(--serif);
  letter-spacing: 0.06em;
  font-size: 15px;
}
.brand-sub, .lede, .hint, .chat-role, .bot-role, .bot-preview, .meta {
  color: var(--muted);
}
.brand-sub { margin: 4px 0 0; font-size: 13px; }

.text-btn, .solid-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  border-radius: 999px;
  padding: 8px 12px;
}
.text-btn:hover, .text-btn:focus-visible, .solid-link:hover, .solid-link:focus-visible {
  background: #262626;
  outline: none;
}
.solid-link { background: var(--text); color: #111; margin-top: 8px; }
.solid-link:hover { background: #fff; }

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(420px, 100%);
  background: var(--side);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 24px 22px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 8px 0 0; font-size: 28px; font-weight: 560; letter-spacing: -0.03em; }
.lede { margin: 8px 0 18px; }
.login-card label { display: block; margin: 12px 0 6px; font-size: 13px; color: var(--muted); }
.login-card input, .field input[type="text"], .field input[type="url"], .field input[type="password"] {
  width: 100%;
  border: 1px solid var(--line);
  background: #101010;
  border-radius: 12px;
  padding: 12px 14px;
}
.login-card input:focus, .field input:focus, textarea:focus, .check input:focus {
  outline: 2px solid #555;
  border-color: #555;
}
.login-card button, .composer button, .card-actions button, .retry {
  border: 0;
  border-radius: 999px;
  background: var(--text);
  color: #111;
  padding: 10px 16px;
  font-weight: 600;
}
.login-card button { width: 100%; margin-top: 16px; }
.login-card button:disabled, .composer button:disabled, .card-actions button:disabled, .retry:disabled {
  opacity: 0.45;
  cursor: default;
}
.form-error { min-height: 1.2em; margin: 10px 0 0; color: var(--danger); font-size: 14px; }
.form-error:empty { margin: 0; min-height: 0; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  overflow: hidden;
}
.sidebar, .chat { min-height: 0; display: flex; flex-direction: column; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); }
.side-head, .chat-head, .admin-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}
.side-head { align-items: flex-start; justify-content: space-between; }
.side-actions { display: flex; gap: 4px; flex-shrink: 0; }
.bot-list { list-style: none; margin: 0; padding: 8px; overflow: auto; flex: 1; }
.bot-item {
  width: 100%;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 10px;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 14px;
  padding: 10px;
}
.bot-item:hover { background: #1e1e1e; }
.bot-item[aria-current="true"] { background: #242424; }
.bot-copy { min-width: 0; }
.bot-top, .bot-bottom { display: flex; align-items: center; gap: 8px; }
.bot-top { justify-content: space-between; }
.bot-name { font-weight: 620; }
.bot-role, .bot-preview { margin: 2px 0 0; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bot-preview.pending-label { color: var(--gold); }

.avatar, .avatar-fallback {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  font-weight: 650;
  color: #fff;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar.lg, .avatar.lg .avatar-fallback { width: 36px; height: 36px; }
.av-0 { background: #8d4b3b; }
.av-1 { background: #3f5e4b; }
.av-2 { background: #3d4f6e; }
.av-3 { background: #6e4b62; }
.av-4 { background: #6a5732; }
.av-5 { background: #4d4638; }

.pending-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gold);
  flex-shrink: 0;
  animation: pulse 1.4s ease-in-out infinite;
}

.chat { background: var(--bg); }
.chat-head { min-height: 68px; }
.chat-title { min-width: 0; flex: 1; }
.chat-title h1, .chat-title p { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title h1 { margin: 0; font-size: 16px; font-weight: 640; }
.chat-title p, .chat-role { margin: 2px 0 0; font-size: 13px; }
.label-short { display: none; }
#new-chat { margin-left: auto; }

.thread {
  flex: 1;
  overflow: auto;
  padding: 22px 8vw 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 280px;
}
.row { display: flex; gap: 10px; align-items: flex-end; max-width: 760px; }
.row.user { align-self: flex-end; flex-direction: row-reverse; }
.row.bot, .row.system { align-self: flex-start; }
.row.system { align-self: center; }
.bubble {
  border-radius: 18px;
  padding: 10px 14px;
  max-width: min(560px, 78vw);
  overflow-wrap: anywhere;
}
.row.user .bubble { background: var(--user); color: var(--user-text); border-bottom-right-radius: 6px; }
.row.bot .bubble { background: var(--panel); border-bottom-left-radius: 6px; }
.row.system .bubble { background: transparent; color: var(--muted); text-align: center; }
.bubble a { color: inherit; }
.row.user .bubble a { color: #1a3f73; }
.bubble img { max-width: min(320px, 100%); border-radius: 12px; display: block; margin-top: 8px; }
.bubble a.shot { display: inline-block; margin: 8px 6px 0 0; }
.bubble a.shot img {
  width: 120px;
  height: 120px;
  max-width: 46vw;
  object-fit: cover;
  margin: 0;
}
.time { margin: 4px 2px 0; font-size: 11px; color: var(--muted); }
.row.user .time { text-align: right; }
.status-line { margin: 6px 0 0; font-size: 13px; color: var(--danger); display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
.retry { padding: 6px 12px; font-size: 13px; }
.bubble.think { color: var(--muted); animation: pulse 1.4s ease-in-out infinite; }

.composer {
  padding: 8px 16px calc(14px + env(safe-area-inset-bottom));
}
.hint { margin: 0 8px 8px; font-size: 12px; text-align: center; }
.hint.bad { color: var(--danger); }
.previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 8px 8px;
}
.previews:empty { display: none; }
.preview { position: relative; width: 64px; height: 64px; }
.preview img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 10px;
  display: block;
}
.composer-box {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  border: 1px solid var(--line);
  background: #161616;
  border-radius: 22px;
  padding: 8px;
}
.composer button.icon-btn,
.composer button.remove {
  background: transparent;
  color: var(--text);
  padding: 0;
}
.composer button.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 22px;
  line-height: 1;
  margin-bottom: 2px;
}
.composer button.icon-btn:hover,
.composer button.icon-btn:focus-visible { background: #262626; }
.composer button.remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #2a2a2a;
  border: 1px solid var(--line);
  font-size: 14px;
  line-height: 1;
}
textarea {
  flex: 1;
  resize: none;
  border: 0;
  background: transparent;
  padding: 8px 0;
  max-height: 160px;
  line-height: 1.4;
  font-size: 16px;
}
textarea:focus { outline: none; }
.composer button { margin-bottom: 2px; }

.admin { max-width: 920px; margin: 0 auto; padding: 12px 16px 48px; }
.admin-head { border: 0; padding: 20px 4px; justify-content: space-between; align-items: flex-end; }
.admin-head h1 { margin: 6px 0 0; font-size: 28px; letter-spacing: -0.03em; }
.cards { display: flex; flex-direction: column; gap: 16px; }
.card {
  border: 1px solid var(--line);
  background: var(--side);
  border-radius: 18px;
  padding: 16px;
}
.card-top { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.card-top h2 { margin: 0; font-size: 18px; }
.pill {
  margin-left: auto;
  font-size: 12px;
  border-radius: 999px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.pill.on { color: #d7ecc8; border-color: #355033; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.wide { grid-column: 1 / -1; }
.field span, .check { font-size: 13px; color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.file input { color: var(--muted); }
.card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.card-actions .ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
}
.card-msg { margin: 10px 0 0; min-height: 1.2em; font-size: 14px; }
.card-msg.bad { color: var(--danger); }

@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .pending-dot, .bubble.think { animation: none; opacity: 1; }
}

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .chat { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat { display: flex; }
  .thread { padding: 16px 12px; }
  .bubble { max-width: 86vw; }
  .chat-head { flex-wrap: nowrap; }
  .label-long { display: none; }
  .label-short { display: inline; }
  #new-chat { margin-left: auto; flex: 0 0 auto; padding-inline: 10px; }
  .grid { grid-template-columns: 1fr; }
}
@media (min-width: 801px) {
  .back { display: none !important; }
}
