:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e5e9f2;
  --brand: #1e3a8a;
  --brand-2: #2563eb;
  --brand-ink: #ffffff;
  --user-bubble: #1e3a8a;
  --user-ink: #ffffff;
  --bot-bubble: #ffffff;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --glow: rgba(37, 99, 235, 0.1);
  --radius: 16px;
}

/* Dark tokens — applied when the OS is dark AND the user hasn't forced light... */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0b1220;
    --panel: #111a2e;
    --panel-2: #0e1626;
    --text: #e8eefc;
    --muted: #93a4c3;
    --border: #1e2a44;
    --brand: #3b82f6;
    --brand-2: #60a5fa;
    --user-bubble: #2563eb;
    --user-ink: #ffffff;
    --bot-bubble: #16223a;
    --shadow: 0 12px 34px rgba(0, 0, 0, 0.4);
    --glow: rgba(96, 165, 250, 0.14);
  }
}

/* ...and always when the user explicitly picks dark. */
:root[data-theme='dark'] {
  --bg: #0b1220;
  --panel: #111a2e;
  --panel-2: #0e1626;
  --text: #e8eefc;
  --muted: #93a4c3;
  --border: #1e2a44;
  --brand: #3b82f6;
  --brand-2: #60a5fa;
  --user-bubble: #2563eb;
  --user-ink: #ffffff;
  --bot-bubble: #16223a;
  --shadow: 0 12px 34px rgba(0, 0, 0, 0.4);
  --glow: rgba(96, 165, 250, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  background-image: radial-gradient(1200px 600px at 15% -10%, var(--glow), transparent 60%),
    radial-gradient(1000px 500px at 100% 110%, var(--glow), transparent 55%);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.shell {
  max-width: 1140px;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  background: var(--panel);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.app {
  flex: 1;
  min-width: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 264px;
  flex: 0 0 264px;
  display: flex;
  flex-direction: column;
  background: var(--panel-2);
  border-right: 1px solid var(--border);
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
}

.sidebar-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.new-chat {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border: none;
  padding: 7px 13px;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
}
.new-chat:hover {
  opacity: 0.92;
}
.new-chat:active {
  transform: scale(0.96);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text);
  font-size: 13.5px;
  transition: background 0.12s;
}
.chat-item:hover {
  background: rgba(148, 163, 184, 0.14);
}
.chat-item.active {
  background: rgba(37, 99, 235, 0.16);
}
.chat-item.active {
  box-shadow: inset 3px 0 0 var(--brand-2);
}
.chat-item .info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.chat-item .title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}
.chat-item .time {
  font-size: 11px;
  color: var(--muted);
}
.chat-item .del {
  flex: 0 0 auto;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 6px;
  opacity: 0;
  transition: opacity 0.12s, color 0.12s, background 0.12s;
}
.chat-item:hover .del {
  opacity: 1;
}
.chat-item .del:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.12);
}

.sidebar-foot {
  padding: 10px 16px 14px;
  font-size: 11px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.backdrop {
  display: none;
}

.menu-btn {
  display: none;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  margin-right: 2px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: var(--brand-ink);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 20px;
  box-shadow: var(--shadow);
}

.status-dot {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #22c55e;
  border: 2px solid var(--panel);
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.6);
  animation: pulse 2.4s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5);
  }
  70% {
    box-shadow: 0 0 0 7px rgba(34, 197, 94, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(34, 197, 94, 0);
  }
}

.theme-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.theme-btn:hover {
  color: var(--brand-2);
  border-color: var(--brand-2);
  background: var(--panel-2);
}

/* JS always resolves to a concrete data-theme, so icon rules stay simple:
   light → show moon (click to go dark); dark → show sun (click to go light). */
.theme-btn .icon-sun,
.theme-btn .icon-moon {
  display: none;
}
:root[data-theme='light'] .theme-btn .icon-moon {
  display: block;
}
:root[data-theme='dark'] .theme-btn .icon-sun {
  display: block;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.brand-text strong {
  font-size: 15px;
}

.brand-text span {
  font-size: 12px;
  color: var(--muted);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.lang-hint {
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 5px 10px;
  border-radius: 999px;
  letter-spacing: 0.04em;
}

.ghost-btn {
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--brand-2);
  background: transparent;
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.ghost-btn:hover {
  background: var(--panel-2);
  border-color: var(--brand-2);
}

/* ---------- Chat area ---------- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}

.row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  animation: rise 0.25s ease;
}

.row.user {
  flex-direction: row-reverse;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  margin-top: 2px;
}

.avatar.bot {
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff;
}

.avatar.user {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
}

.bubble {
  max-width: 76%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.55;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}

.row.user .bubble {
  background: var(--user-bubble);
  color: var(--user-ink);
  border-bottom-right-radius: 5px;
}

.row.bot .bubble {
  background: var(--bot-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}

/* Streaming caret */
.caret {
  display: inline-block;
  width: 7px;
  height: 1.05em;
  margin-left: 1px;
  vertical-align: -2px;
  background: var(--brand-2);
  border-radius: 2px;
  animation: caret-blink 1s steps(2, start) infinite;
}
@keyframes caret-blink {
  50% {
    opacity: 0;
  }
}

/* Copy button on assistant answers */
.row.bot {
  position: relative;
}
.copy-btn {
  position: absolute;
  top: -6px;
  right: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font: inherit;
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 999px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, border-color 0.15s;
}
.row.bot:hover .copy-btn {
  opacity: 1;
}
.copy-btn:hover {
  color: var(--brand-2);
  border-color: var(--brand-2);
}
.copy-btn.copied {
  opacity: 1;
  color: #22c55e;
  border-color: #22c55e;
}

/* Markdown inside bubbles */
.bubble p {
  margin: 0 0 10px;
}
.bubble p:last-child {
  margin-bottom: 0;
}
.bubble ul,
.bubble ol {
  margin: 6px 0 10px;
  padding-left: 20px;
}
.bubble li {
  margin: 3px 0;
}
.bubble strong {
  font-weight: 700;
}
.bubble code {
  background: rgba(148, 163, 184, 0.18);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 0.9em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.bubble table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}
.bubble th,
.bubble td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  text-align: left;
}
.bubble th {
  background: var(--panel-2);
  font-weight: 600;
}
.row.user .bubble th,
.row.user .bubble td {
  border-color: rgba(255, 255, 255, 0.25);
}

/* Typing indicator */
.typing {
  display: inline-flex;
  gap: 4px;
  padding: 4px 2px;
}
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.3s infinite ease-in-out;
}
.typing span:nth-child(2) {
  animation-delay: 0.18s;
}
.typing span:nth-child(3) {
  animation-delay: 0.36s;
}
@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0.25;
    transform: translateY(0);
  }
  40% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* ---------- Suggestions ---------- */
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 20px 12px;
}

.chip {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 8px 13px;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.12s, background 0.12s;
}

.chip:hover {
  border-color: var(--brand-2);
  transform: translateY(-1px);
}

/* ---------- Composer ---------- */
.composer {
  border-top: 1px solid var(--border);
  padding: 12px 20px 14px;
  background: var(--panel);
}

.composer-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 8px 8px 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.composer-form:focus-within {
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.composer-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  resize: none;
  max-height: 160px;
  padding: 6px 0;
  outline: none;
  line-height: 1.5;
}

.composer-input::placeholder {
  color: var(--muted);
}

.send-btn {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

.send-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.send-btn:not(:disabled):active {
  transform: scale(0.94);
}

.turnstile-host {
  display: flex;
  justify-content: center;
}
.turnstile-host:not(:empty) {
  margin-top: 10px;
}

.disclaimer {
  margin: 8px 4px 0;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
}

/* ---------- Mobile: sidebar becomes a drawer ---------- */
@media (max-width: 760px) {
  .menu-btn {
    display: grid;
    place-items: center;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
  }
  .shell.drawer-open .sidebar {
    transform: translateX(0);
  }
  .shell.drawer-open .backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.5);
    z-index: 20;
  }
}

@media (max-width: 600px) {
  .bubble {
    max-width: 84%;
  }
  .brand-text span {
    display: none;
  }
}
