:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #e1e4e9;
  --text: #15181d;
  --text-soft: #5a626d;
  --text-faint: #98a0ab;
  --accent: #2f64e0;
  --accent-soft: #e8eefb;
  --user-bubble: #eef0f3;
  --code-bg: #f4f6f8;
  --shadow: 0 1px 2px rgba(20, 24, 31, 0.04), 0 4px 16px rgba(20, 24, 31, 0.05);
  --radius: 12px;
  --font: "Noto Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --ui-font: "Noto Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Menlo", monospace;
}

[data-theme="dark"] {
  --bg: #121417;
  --surface: #191c20;
  --surface-2: #21252b;
  --border: #2d323a;
  --text: #e7e9ec;
  --text-soft: #a6adb7;
  --text-faint: #6d747e;
  --accent: #5b8cff;
  --accent-soft: #1d2740;
  --user-bubble: #21252b;
  --code-bg: #171a1e;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

/* Ensure the hidden attribute always wins over display rules below */
[hidden] { display: none !important; }

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--ui-font);
  -webkit-font-smoothing: antialiased;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
}

.app { display: flex; flex-direction: column; height: 100dvh; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 20px; height: 20px; border-radius: 6px;
  background: var(--accent);
}
.brand-name { font-weight: 600; letter-spacing: -0.01em; font-size: 15px; }

.controls { display: flex; align-items: center; gap: 8px; }

select#model-select, .artifact-select {
  font-family: var(--ui-font); font-size: 13px;
  color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 9px;
  padding: 7px 10px; cursor: pointer; max-width: 200px;
}

/* ---------- buttons ---------- */
.btn {
  font-family: var(--ui-font); font-size: 13px; font-weight: 500;
  border: 1px solid transparent; border-radius: 9px;
  padding: 7px 12px; cursor: pointer; color: var(--text);
  background: transparent; transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn.ghost { border-color: var(--border); background: var(--surface-2); }
.btn.ghost:hover { background: var(--border); }
.btn.small { font-size: 12px; padding: 5px 9px; }
.btn.icon { padding: 7px; width: 34px; height: 34px; display: grid; place-items: center; }
.btn.icon.small { width: 28px; height: 28px; }
.btn:disabled { opacity: 0.45; cursor: default; }

.theme-glyph { width: 15px; height: 15px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, var(--text) 48%, transparent 50%);
  box-shadow: inset -3px -3px 0 0 var(--surface); }
[data-theme="dark"] .theme-glyph { box-shadow: inset -4px -2px 0 0 var(--accent); background: var(--text); }

.paperclip, .send-glyph { width: 18px; height: 18px; display: inline-block; }
.paperclip {
  background: var(--text); -webkit-mask: var(--clip) center/contain no-repeat; mask: var(--clip) center/contain no-repeat;
  --clip: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48'/%3E%3C/svg%3E");
}
.send .send-glyph {
  background: #fff; -webkit-mask: var(--arrow) center/contain no-repeat; mask: var(--arrow) center/contain no-repeat;
  --arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 19V5M5 12l7-7 7 7'/%3E%3C/svg%3E");
}

/* ---------- main layout ---------- */
.main { flex: 1; display: flex; min-height: 0; }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.messages {
  flex: 1; overflow-y: auto; padding: 28px 0 12px;
  scroll-behavior: smooth;
}

.msg { max-width: 760px; margin: 0 auto; padding: 6px 24px 22px; }
.msg .role {
  font-size: 11px; font-weight: 700; letter-spacing: 0.07em; text-transform: uppercase;
  color: var(--text-faint); margin-bottom: 8px;
}
.msg.user .bubble {
  background: var(--user-bubble); border-radius: var(--radius);
  padding: 12px 16px; display: inline-block; max-width: 100%;
}
.msg .content { font-family: var(--font); font-size: 15px; line-height: 1.65; color: var(--text); }
.msg .content p { margin: 0 0 0.85em; }
.msg .content p:last-child { margin-bottom: 0; }
.msg .content h1, .msg .content h2, .msg .content h3 { font-weight: 600; line-height: 1.3; margin: 1.2em 0 0.5em; }
.msg .content ul, .msg .content ol { margin: 0 0 0.85em; padding-left: 1.4em; }
.msg .content li { margin: 0.2em 0; }
.msg .content a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.msg .content blockquote { margin: 0 0 0.85em; padding-left: 14px; border-left: 3px solid var(--border); color: var(--text-soft); }
.msg .content table { border-collapse: collapse; font-size: 14px; font-family: var(--ui-font); margin: 0 0 0.85em; }
.msg .content th, .msg .content td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }

/* code blocks */
.msg .content pre {
  position: relative; background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 14px; overflow-x: auto; margin: 0 0 0.9em;
  font-family: var(--mono); font-size: 13.5px; line-height: 1.55;
}
.msg .content pre code { font-family: var(--mono); background: none; padding: 0; }
.msg .content :not(pre) > code {
  font-family: var(--mono); font-size: 0.88em; background: var(--code-bg);
  border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px;
}
.code-tools { position: absolute; top: 8px; right: 8px; display: flex; gap: 6px; }
.code-tools button {
  font-family: var(--ui-font); font-size: 11px; font-weight: 600;
  background: var(--surface); color: var(--text-soft);
  border: 1px solid var(--border); border-radius: 7px; padding: 3px 8px; cursor: pointer;
}
.code-tools button:hover { color: var(--text); border-color: var(--text-faint); }

.thinking { color: var(--text-faint); font-style: italic; }
.cursor-blink::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.msg-tools { margin-top: 10px; display: flex; gap: 8px; }
.msg-tools button {
  font-size: 11px; font-weight: 600; color: var(--text-faint);
  background: transparent; border: 1px solid transparent; border-radius: 7px;
  padding: 3px 7px; cursor: pointer;
}
.msg-tools button:hover { color: var(--text); background: var(--surface-2); }

.empty {
  height: 100%; display: grid; place-content: center; text-align: center; color: var(--text-faint);
  gap: 8px; padding: 24px;
}
.empty h2 { font-family: var(--ui-font); font-weight: 600; color: var(--text); margin: 0; font-size: 19px; letter-spacing: -0.01em; }
.empty p { margin: 0; font-size: 13.5px; color: var(--text-faint); }

/* ---------- composer ---------- */
.composer {
  border-top: 1px solid var(--border); padding: 14px 24px 18px;
  background: var(--bg);
}
.composer-row {
  max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 8px 8px 8px 10px; box-shadow: var(--shadow);
}
.composer-row:focus-within { border-color: var(--accent); }
.input {
  flex: 1; border: none; outline: none; resize: none; background: transparent;
  font-family: var(--ui-font); font-size: 15px; line-height: 1.5; color: var(--text);
  max-height: 220px; padding: 6px 2px;
}
.attach { background: transparent; }
.attach:hover { background: var(--surface-2); }
.send {
  background: var(--accent); width: 36px; height: 36px; border-radius: 11px;
  display: grid; place-items: center; flex: none;
}
.send:hover:not(:disabled) { filter: brightness(1.05); }
.hint { max-width: 760px; margin: 8px auto 0; font-size: 11.5px; color: var(--text-faint); text-align: center; }

.attachments { max-width: 760px; margin: 0 auto 10px; display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: flex; align-items: center; gap: 6px; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px;
}
.chip img { width: 26px; height: 26px; object-fit: cover; border-radius: 4px; }
.chip .x { cursor: pointer; color: var(--text-faint); font-weight: 700; }
.chip .x:hover { color: var(--accent); }

/* ---------- artifact panel ---------- */
.artifacts {
  width: 46%; min-width: 360px; border-left: 1px solid var(--border);
  background: var(--surface); display: flex; flex-direction: column;
}
.artifact-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.artifact-select { flex: 1; min-width: 100px; }
.artifact-tabs { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 2px; }
.tab {
  font-size: 12px; font-weight: 600; color: var(--text-soft); background: transparent;
  border: none; border-radius: 7px; padding: 5px 12px; cursor: pointer;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.artifact-actions { display: flex; gap: 6px; }
.artifact-body { flex: 1; position: relative; min-height: 0; }
.artifact-frame { width: 100%; height: 100%; border: none; background: #fff; }
.artifact-code {
  position: absolute; inset: 0; margin: 0; overflow: auto; padding: 16px;
  background: var(--code-bg); font-family: var(--mono); font-size: 13px; line-height: 1.55;
}

/* mobile */
@media (max-width: 880px) {
  .artifacts {
    position: fixed; inset: 0; width: 100%; min-width: 0; z-index: 30;
  }
  .hint { display: none; }
}

/* ---------- history drawer ---------- */
.history-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(10, 14, 20, 0.32); display: flex;
}
.history-panel {
  width: 320px; max-width: 86vw; height: 100%;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.history-head {
  display: flex; align-items: center; gap: 8px;
  padding: 13px 14px; border-bottom: 1px solid var(--border);
}
.history-title { font-weight: 600; font-size: 14px; flex: 1; }
.history-list { flex: 1; overflow-y: auto; padding: 8px; }
.history-item {
  display: flex; align-items: center; gap: 6px;
  padding: 9px 10px; border-radius: 9px; cursor: pointer;
}
.history-item:hover { background: var(--surface-2); }
.history-item.active { background: var(--accent-soft); }
.history-item .h-main { flex: 1; min-width: 0; }
.history-item .h-title {
  font-size: 13.5px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history-item .h-time { font-size: 11px; color: var(--text-faint); margin-top: 2px; }
.history-item .h-del {
  color: var(--text-faint); border: none; background: transparent; cursor: pointer;
  font-weight: 700; padding: 2px 7px; border-radius: 6px; flex: none;
}
.history-item .h-del:hover { color: var(--accent); background: var(--surface); }
.history-empty { color: var(--text-faint); font-size: 13px; text-align: center; padding: 32px 16px; }
