/* === Theme === */
:root {
  --bg: #fafafa; --fg: #111; --card: #fff; --border: #e2e2e8;
  --muted: #f0f0f4; --muted-fg: #6b6b80;
  --primary: #7c3aed; --primary-fg: #fff;
  --accent: #f0f0f4; --accent-fg: #111;
}
.dark {
  --bg: #0a0a14; --fg: #f0f0f0; --card: #111122; --border: #1e1e36;
  --muted: #1a1a2e; --muted-fg: #8888aa;
  --primary: #7c3aed; --primary-fg: #fff;
  --accent: #1a1a2e; --accent-fg: #f0f0f0;
}

/* === Reset === */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: 'Inter', sans-serif;
  background: var(--bg);
  color: var(--fg);
  display: flex;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }

/* === Scrollbar === */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* === Overlay === */
#overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 30; opacity: 0; pointer-events: none;
  transition: opacity .25s;
}
#overlay.show { opacity: 1; pointer-events: auto; }

/* =================================================
   SIDEBAR
   ================================================= */
#sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 272px;
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 40;
  transform: translateX(-100%);
  transition: transform .25s cubic-bezier(.4,0,.2,1);
}
#sidebar.show { transform: translateX(0); }

.sidebar-logo {
  height: 48px; display: flex; align-items: center; justify-content: center;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.sidebar-logo .logo { height: 28px; width: auto; }
.sidebar-new { padding: 8px; flex-shrink: 0; }
.sidebar-new button {
  width: 100%; height: 32px; display: flex; align-items: center; justify-content: center;
  gap: 6px; border: none; border-radius: 6px; font-size: 12px; font-weight: 500;
  background: var(--primary); color: var(--primary-fg); cursor: pointer;
  font-family: inherit; transition: opacity .2s;
}
.sidebar-new button:hover { opacity: .9; }
.sidebar-history {
  flex: 1; overflow-y: auto; padding: 8px;
}
.history-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px; border-radius: 6px; cursor: pointer;
  font-size: 12px; color: var(--muted-fg); transition: background .15s;
}
.history-item:hover { background: var(--accent); color: var(--fg); }
.history-item.active { background: var(--accent); color: var(--fg); }
.history-item span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-item .del-btn {
  opacity: 0; background: none; border: none; color: var(--muted-fg);
  cursor: pointer; padding: 2px; flex-shrink: 0; transition: opacity .15s;
}
.history-item:hover .del-btn { opacity: 1; }
.history-item .del-btn:hover { color: #f44; }

/* Desktop: sidebar inline */
@media (min-width: 768px) {
  #sidebar {
    position: relative; transform: none;
    width: 256px; transition: width .2s ease, border-width .2s;
  }
  #sidebar.hide-desktop {
    width: 0; border-right-width: 0; overflow: hidden;
  }
  #overlay { display: none !important; }
}

/* =================================================
   MAIN
   ================================================= */
#main {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0; height: 100%;
}

/* === Header === */
#header {
  height: 48px; padding: 0 10px;
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.header-left, .header-right {
  display: flex; align-items: center; gap: 8px;
}
.btn-icon {
  width: 32px; height: 32px; border-radius: 6px;
  background: var(--muted); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  color: var(--muted-fg); cursor: pointer; flex-shrink: 0;
  transition: color .2s;
}
.btn-icon:hover { color: var(--fg); }
.btn-text {
  height: 28px; padding: 0 8px; border-radius: 6px;
  background: var(--muted); border: 1px solid var(--border);
  color: var(--muted-fg); cursor: pointer; flex-shrink: 0;
  font-size: 11px; font-weight: 600; font-family: inherit;
  transition: color .2s;
}
.btn-text:hover { color: var(--fg); }
.select-model {
  height: 28px; padding: 0 6px; border-radius: 6px;
  background: var(--muted); border: 1px solid var(--border);
  color: var(--muted-fg); font-size: 11px; font-weight: 500;
  font-family: inherit; cursor: pointer; outline: none; flex-shrink: 0;
}

/* Toggle */
.toggle { position: relative; display: inline-block; flex-shrink: 0; }
.toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-track {
  display: block; width: 36px; height: 20px; border-radius: 10px;
  background: var(--muted); cursor: pointer; transition: background .2s;
  position: relative;
}
.toggle-thumb {
  position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  border-radius: 50%; background: var(--muted-fg); transition: .2s;
}
.toggle input:checked ~ .toggle-track { background: var(--primary); }
.toggle input:checked ~ .toggle-track .toggle-thumb {
  transform: translateX(16px); background: #fff;
}

/* =================================================
   MESSAGES
   ================================================= */
#messages {
  flex: 1; overflow-y: auto; position: relative;
}
#welcome {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; padding: 20px;
}
#welcome.hide { display: none; }
.welcome-icon {
  width: 48px; height: 48px; border-radius: 12px;
  background: color-mix(in srgb, var(--primary) 10%, transparent);
  display: flex; align-items: center; justify-content: center; margin-bottom: 8px;
}
#welcome h1 { font-size: 18px; font-weight: 600; }
#welcome p { font-size: 14px; color: var(--muted-fg); text-align: center; }

/* Message rows */
.msg-row { padding: 12px 0; }
.msg-inner {
  max-width: 640px; margin: 0 auto; padding: 0 12px;
  display: flex; gap: 10px;
}
.msg-inner.user { flex-direction: row-reverse; }
.msg-avatar {
  width: 28px; height: 28px; border-radius: 6px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600;
}
.msg-avatar.bot { background: var(--primary); color: var(--primary-fg); }
.msg-avatar.usr { background: #059669; color: #fff; }
.msg-body { min-width: 0; flex: 1; }
.msg-inner.user .msg-body { text-align: right; }
.msg-bubble {
  display: inline-block; text-align: left;
  font-size: 14px; line-height: 1.65;
}
.msg-inner.user .msg-bubble {
  background: color-mix(in srgb, var(--primary) 12%, transparent);
  border-radius: 16px 4px 16px 16px; padding: 8px 14px;
}
.msg-bubble p { margin: 4px 0; }
.msg-bubble strong { font-weight: 600; }

/* Thinking */
.think-wrap {
  margin-bottom: 8px; border-left: 2px solid color-mix(in srgb, var(--primary) 40%, transparent);
  padding: 2px 0 2px 10px; cursor: pointer; user-select: none;
  border-radius: 0 4px 4px 0;
}
.think-wrap:hover { background: color-mix(in srgb, var(--muted) 50%, transparent); }
.think-label {
  font-size: 11px; color: var(--muted-fg); display: flex; align-items: center; gap: 4px;
}
.think-body {
  display: none; margin-top: 4px; font-size: 12px;
  color: var(--muted-fg); white-space: pre-wrap;
}
.think-body.show { display: block; }

/* Typing dots */
.dot-pulse { display: flex; gap: 4px; align-items: center; height: 28px; }
.dot-pulse span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted-fg); animation: dot 1.2s infinite ease-in-out;
}
.dot-pulse span:nth-child(2) { animation-delay: .2s; }
.dot-pulse span:nth-child(3) { animation-delay: .4s; }
@keyframes dot {
  0%,100% { opacity:.3; transform: scale(.8); }
  50% { opacity:1; transform: scale(1); }
}

/* Error */
.msg-error { font-size: 13px; color: #f44; font-weight: 500; }

/* =================================================
   INPUT
   ================================================= */
#inputArea {
  border-top: 1px solid var(--border); padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  flex-shrink: 0;
}
.input-wrap {
  max-width: 640px; margin: 0 auto;
  display: flex; gap: 8px; align-items: flex-end;
}
#input {
  flex: 1; resize: none; border: 1px solid var(--border);
  background: var(--card); color: var(--fg);
  padding: 10px 12px; border-radius: 10px;
  font-size: 14px; font-family: inherit; line-height: 1.4;
  min-height: 40px; max-height: 120px; outline: none;
  transition: border-color .2s;
}
#input:focus { border-color: var(--primary); }
#input::placeholder { color: var(--muted-fg); }
#sendBtn {
  width: 40px; height: 40px; border-radius: 10px; flex-shrink: 0;
  background: var(--primary); color: var(--primary-fg);
  border: none; cursor: pointer; display: flex;
  align-items: center; justify-content: center;
  transition: opacity .2s;
}
#sendBtn:hover { opacity: .9; }
#sendBtn:disabled { opacity: .4; cursor: not-allowed; }

/* =================================================
   CODE BLOCKS
   ================================================= */
.code-block {
  margin: 12px 0; border-radius: 8px; overflow: hidden;
  border: 1px solid var(--border);
}
.code-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; background: var(--muted);
  font-size: 11px; font-family: 'Courier New', monospace;
  color: var(--muted-fg); text-transform: uppercase; letter-spacing: .05em;
}
.copy-btn {
  background: none; border: none; color: var(--muted-fg); padding: 4px;
  border-radius: 4px; cursor: pointer; display: flex; align-items: center;
  justify-content: center; opacity: .6; transition: .2s;
}
.copy-btn:hover { opacity: 1; color: var(--fg); background: var(--border); }
.code-block pre {
  margin: 0; padding: 12px; overflow-x: auto; font-size: 13px; line-height: 1.6;
}
.code-block pre code.hljs {
  font-family: 'Courier New', Consolas, monospace; padding: 0; background: transparent;
}
code.inline-code {
  background: var(--muted); color: var(--primary);
  padding: 2px 6px; border-radius: 4px;
  font-size: 12px; font-family: 'Courier New', monospace;
  border: 1px solid var(--border);
}

/* === Responsive fine-tuning === */
@media (min-width: 768px) {
  .msg-inner { padding: 0 16px; }
  .msg-row { padding: 16px 0; }
  #inputArea { padding: 16px; }
  #header { padding: 0 14px; }
}
