:root {
  --bg: #ffffff;
  --bg-side: #f6f8f8;
  --bg-hover: #e9eeee;
  --bg-input: #f1f4f4;
  --text: #1c2425;
  --muted: #6b7778;
  --border: #dfe5e5;
  --accent: #e11d2e;
  --accent-text: #ffffff;
  --user-bubble: #fde8ea;
  --danger: #e11d2e;
  --shadow: 0 6px 24px rgba(0, 0, 0, .12);
}
:root[data-theme="dark"] {
  --bg: #1b2122;
  --bg-side: #141919;
  --bg-hover: #242c2d;
  --bg-input: #263031;
  --text: #e8eeee;
  --muted: #92a0a1;
  --border: #2d3738;
  --user-bubble: #4a2227;
  --shadow: 0 6px 24px rgba(0, 0, 0, .5);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b2122;
    --bg-side: #141919;
    --bg-hover: #242c2d;
    --bg-input: #263031;
    --text: #e8eeee;
    --muted: #92a0a1;
    --border: #2d3738;
    --user-bubble: #4a2227;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.6;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }

.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* Sidebar */
.sidebar {
  width: 270px; flex-shrink: 0;
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 14px; font-weight: 700; font-size: 18px; }
.logo {
  width: 32px; height: 32px; border-radius: 9px;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-size: 18px;
}
.new-chat {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; text-align: left; font-weight: 500;
}
.new-chat:hover { background: var(--bg-hover); }
.chat-list { flex: 1; overflow-y: auto; margin: 12px -4px; padding: 0 4px; }
.chat-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  font-size: 14px;
}
.chat-item:hover, .chat-item.active { background: var(--bg-hover); }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .del { opacity: 0; color: var(--muted); padding: 0 4px; }
.chat-item:hover .del { opacity: 1; }
.chat-item .del:hover { color: var(--danger); }
.empty-list { color: var(--muted); font-size: 13px; padding: 8px 10px; }

.sidebar-footer { border-top: 1px solid var(--border); padding-top: 12px; display: grid; gap: 10px; }
.guard {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer;
}
.guard-label { display: flex; gap: 10px; align-items: center; }
.guard-label strong { display: block; font-size: 14px; line-height: 1.2; }
.guard-label small { color: var(--muted); font-size: 12px; }
.guard input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  width: 38px; height: 22px; border-radius: 22px; background: var(--border);
  position: relative; flex-shrink: 0; transition: background .2s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 16px; height: 16px; border-radius: 50%; background: #fff;
  transition: transform .2s;
}
.guard input:checked + .switch { background: var(--accent); }
.guard input:checked + .switch::after { transform: translateX(16px); }
.guard input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }
.footer-row { display: flex; gap: 8px; }
.ghost { flex: 1; padding: 8px; border-radius: 8px; font-size: 13px; color: var(--muted); }
.ghost:hover { background: var(--bg-hover); color: var(--text); }
.ghost.danger:hover { color: var(--danger); }

/* Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.topbar h1 {
  margin: 0; font-size: 16px; font-weight: 600; flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.badge {
  background: var(--user-bubble); color: var(--accent);
  border-radius: 999px; padding: 2px 12px; font-size: 13px; font-weight: 600; white-space: nowrap;
}
.badge[hidden] { display: none; }
.icon-btn { font-size: 20px; padding: 2px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--bg-hover); }
.menu-btn { display: none; }

.messages { flex: 1; overflow-y: auto; }
.thread { max-width: 768px; margin: 0 auto; padding: 24px 16px 8px; }

/* Empty state */
.welcome { text-align: center; padding-top: 8vh; }
.welcome .logo { width: 56px; height: 56px; font-size: 30px; border-radius: 16px; margin: 0 auto 16px; }
.welcome h2 { margin: 0 0 24px; font-size: 28px; }
.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; text-align: left; }
.card {
  border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px;
}
.card:hover { background: var(--bg-hover); }
.card b { display: block; font-size: 14px; }
.card span { color: var(--muted); font-size: 13px; line-height: 1.4; display: block; }

/* Messages */
.msg { display: flex; gap: 12px; margin-bottom: 22px; animation: fade .25s ease; }
.msg.user { justify-content: flex-end; }
.msg.user .body {
  background: var(--user-bubble); border-radius: 18px 18px 4px 18px;
  padding: 10px 16px; max-width: 80%;
}
.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-size: 15px;
}
.msg.bot .body { flex: 1; min-width: 0; padding-top: 2px; }
.body { white-space: pre-wrap; overflow-wrap: anywhere; }
.actions { margin-top: 6px; display: flex; gap: 4px; }
.actions button { font-size: 12px; color: var(--muted); padding: 2px 8px; border-radius: 6px; }
.actions button:hover { background: var(--bg-hover); color: var(--text); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.chip {
  border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px; font-size: 14px;
}
.chip:hover { background: var(--bg-hover); border-color: var(--accent); }

.typing { display: inline-flex; gap: 5px; padding: 10px 0; }
.typing i {
  width: 8px; height: 8px; border-radius: 50%; background: var(--muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(1) infinite; }

@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-6px); opacity: 1; } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* Composer */
.composer-wrap { max-width: 768px; width: 100%; margin: 0 auto; padding: 8px 16px 12px; }
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-input); border-radius: 24px; padding: 8px 8px 8px 18px;
  border: 1px solid transparent;
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; resize: none; border: 0; outline: 0; background: transparent;
  color: var(--text); font: inherit; max-height: 180px; padding: 6px 0; line-height: 1.5;
}
.send {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: var(--accent-text); font-size: 18px; font-weight: 700;
}
.send:disabled { opacity: .35; cursor: not-allowed; }
.send.stop { font-size: 12px; }
.disclaimer { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 999px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Mobile */
.backdrop { display: none; }
@media (max-width: 768px) {
  .menu-btn { display: block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20;
    transform: translateX(-100%); transition: transform .25s; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .backdrop.show { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 10; }
  .cards { grid-template-columns: 1fr; }
  .welcome h2 { font-size: 24px; }
}
