:root {
  --bg: #1a1918;
  --bg-2: #242321;
  --side: rgba(26, 25, 23, 0.75);
  --surface: rgba(44, 43, 40, 0.6);
  --surface-solid: #2d2c2a;
  --line: rgba(255, 255, 255, 0.08);
  --line-2: rgba(255, 255, 255, 0.14);
  --text: #ece9e1;
  --muted: #a6a39a;
  --dim: #77746c;
  --accent: #d97757;
  --accent-2: #e8a17f;
  --accent-glow: rgba(217, 119, 87, 0.35);
  --user: rgba(20, 20, 19, 0.7);
  --danger: #ef6461;
  --success: #67b987;
  --warning: #eab308;
  --radius: 22px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --serif: 'Newsreader', 'Tiempos Text', Georgia, serif;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace;
  --side-w: 288px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* Thin & Sharp Tinted Scrollbars across the entire app */
* {
  scrollbar-width: thin;
  scrollbar-color: rgba(217, 119, 87, 0.35) transparent;
}
::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: rgba(217, 119, 87, 0.3);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(217, 119, 87, 0.6);
}

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; user-select: none; }
input, textarea, select { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; pointer-events: none; }
::selection { background: rgba(217, 119, 87, 0.35); }

/* ---------- Glass Utility ---------- */
.glass {
  background: var(--surface);
  backdrop-filter: blur(28px) saturate(170%);
  -webkit-backdrop-filter: blur(28px) saturate(170%);
  border: 1px solid var(--line);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 10px 40px -10px rgba(0, 0, 0, 0.55);
}

/* ---------- Ambient Background ---------- */
.ambient { position: fixed; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; }
.orb { position: absolute; border-radius: 50%; filter: blur(95px); opacity: 0.38; will-change: transform; }
.o1 { width: 540px; height: 540px; background: #b85c3e; top: -160px; right: -120px; animation: drift1 26s ease-in-out infinite; }
.o2 { width: 480px; height: 480px; background: #5a4a7a; bottom: -180px; left: 16%; opacity: 0.22; animation: drift2 32s ease-in-out infinite; }
.o3 { width: 360px; height: 360px; background: #c9895f; top: 40%; left: 55%; opacity: 0.14; animation: drift3 22s ease-in-out infinite; }

@keyframes drift1 { 50% { transform: translate(-140px, 120px) scale(1.15); } }
@keyframes drift2 { 50% { transform: translate(160px, -90px) scale(0.9); } }
@keyframes drift3 { 50% { transform: translate(-200px, -60px) scale(1.25); } }

.grain {
  position: absolute; inset: -50%; opacity: 0.045;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  animation: grain 8s steps(6) infinite;
}
@keyframes grain { 50% { transform: translate(-4%, 3%); } }

body.no-fx .ambient { display: none; }
body.no-fx * { animation-duration: 0.001ms !important; }

/* ---------- App Layout ---------- */
.app { position: relative; z-index: 1; display: flex; height: 100dvh; width: 100vw; overflow: hidden; }

/* ══ Sidebar ══ */
.sidebar {
  width: var(--side-w);
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px;
  margin: 10px 0 10px 10px;
  border-radius: var(--radius);
  background: var(--side);
  z-index: 40;
  position: relative;
  transition: width 0.38s var(--ease), padding 0.38s var(--ease), margin 0.38s var(--ease), transform 0.38s var(--ease);
  overflow: hidden;
  user-select: none;
  /* prevent collapsed text overflow from bleeding */
  min-width: 0;
}

/* ── Collapsed sidebar — icons only, no text bleed ── */
.app.collapsed .sidebar {
  width: 64px !important;
  padding: 12px 10px !important;
  align-items: center;
}
/* Hide all text/labels in collapsed state */
.app.collapsed .sidebar .brand,
.app.collapsed .sidebar .new-chat > span:not(.plus),
.app.collapsed .sidebar .search,
.app.collapsed .sidebar .side-label,
.app.collapsed .sidebar .history,
.app.collapsed .sidebar .pinfo,
.app.collapsed .sidebar .gear {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  width: 0 !important;
  max-width: 0 !important;
  overflow: hidden;
  flex: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  font-size: 0 !important;
  border: none !important;
}
.app.collapsed .sidebar .side-top {
  justify-content: center;
}
.app.collapsed .sidebar #collapseBtn {
  margin: 0 auto;
}
/* New chat button icon-only */
.app.collapsed .sidebar .new-chat {
  width: 42px !important;
  height: 42px !important;
  min-width: 0;
  padding: 0 !important;
  justify-content: center;
  border-radius: 13px;
  margin: 6px auto 0;
}
.app.collapsed .sidebar .new-chat .plus {
  margin: 0;
}
/* Profile button icon-only */
.app.collapsed .sidebar .side-bottom {
  margin-top: auto;
  width: 100%;
  display: flex;
  justify-content: center;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}
.app.collapsed .sidebar .profile {
  width: 40px !important;
  height: 40px !important;
  padding: 0 !important;
  justify-content: center;
  border-radius: 50%;
  margin: 0 auto;
}
.app.collapsed .sidebar .avatar {
  margin: 0;
  flex: none;
}

.side-top { display: flex; align-items: center; gap: 10px; height: 40px; }
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 21px; letter-spacing: -0.01em; white-space: nowrap; font-weight: 500; }

.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center; border-radius: 10px; color: var(--muted);
  transition: background 0.2s, color 0.2s, transform 0.2s var(--spring); flex: none; cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }
.icon-btn:active { transform: scale(0.92); }

.new-chat {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 12px;
  white-space: nowrap; font-weight: 500; margin-top: 6px; transition: all 0.2s;
  background: rgba(255, 255, 255, 0.03); border: 1px solid var(--line); cursor: pointer;
}
.new-chat:hover { background: rgba(255, 255, 255, 0.08); border-color: var(--line-2); }
.new-chat .plus {
  width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #1a1412;
  display: grid; place-items: center; transition: transform 0.4s var(--spring), box-shadow 0.3s;
}
.new-chat:hover .plus { transform: rotate(90deg) scale(1.08); box-shadow: 0 0 20px 2px rgba(217, 119, 87, 0.5); }
.new-chat .plus svg { width: 15px; height: 15px; stroke-width: 2.4; }

.search {
  display: flex; align-items: center; gap: 8px; padding: 0 10px; height: 36px; border-radius: 11px;
  background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); color: var(--dim);
}
.search input { flex: 1; background: none; border: 0; outline: 0; color: var(--text); font-size: 13.5px; }
.search input::placeholder { color: var(--dim); }

.side-label { font-size: 11.5px; color: var(--dim); padding: 14px 8px 4px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; padding: 0 4px; }

/* History Item with Pen (Rename) and Bin (Delete) */
.h-item {
  position: relative; display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 8px 10px;
  border-radius: 10px; font-size: 13.5px; color: #cfccc4; white-space: nowrap; overflow: hidden;
  text-align: left; transition: background 0.2s, color 0.2s; cursor: pointer;
}
.h-item .title-span { flex: 1; overflow: hidden; text-overflow: ellipsis; pointer-events: none; }
.h-item:hover { background: rgba(255, 255, 255, 0.06); color: var(--text); }
.h-item.active { background: rgba(255, 255, 255, 0.1); color: var(--text); font-weight: 500; }

.h-actions { display: flex; align-items: center; gap: 2px; opacity: 0; transition: opacity 0.2s; }
.h-item:hover .h-actions { opacity: 1; }

.h-btn {
  width: 24px; height: 24px; display: grid; place-items: center; border-radius: 6px;
  color: var(--dim); transition: all 0.2s; cursor: pointer;
}
.h-btn:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }
.h-btn.del:hover { color: #f08a7a; }
.h-btn svg { width: 13px; height: 13px; }

/* Bottom Profile / Account trigger */
.side-bottom { border-top: 1px solid var(--line); padding-top: 8px; }
.profile {
  width: 100%; display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 12px;
  text-align: left; white-space: nowrap; transition: background 0.2s; cursor: pointer;
}
.profile:hover { background: rgba(255, 255, 255, 0.07); }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center;
  background: linear-gradient(135deg, #e3a284, #b5573a); color: #1d1513; font-weight: 600;
  font-size: 13px; flex: none; overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.pinfo { flex: 1; display: flex; flex-direction: column; line-height: 1.25; min-width: 0; pointer-events: none; }
.pinfo b { font-weight: 500; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pinfo small { color: var(--dim); font-size: 11.5px; }
.gear { color: var(--dim); transition: transform 0.6s var(--ease), color 0.2s; pointer-events: none; }
.profile:hover .gear { transform: rotate(90deg); color: var(--text); }

/* ---------- Main Container ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; height: 100%; overflow: hidden; }

/* Clean Minimal Topbar with Space and Context Meter */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 28px; height: 60px; z-index: 10; position: relative;
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.chat-title {
  font-size: 14.5px; color: #d8d5cd; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; font-weight: 500;
}
.topbar-right { display: flex; align-items: center; gap: 10px; }

/* Context Window Meter Pill (like Claude) */
.context-meter-btn {
  display: flex; align-items: center; gap: 7px; padding: 6px 12px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line);
  font-size: 12.5px; color: var(--muted); transition: all 0.2s; cursor: pointer;
}
.context-meter-btn:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); border-color: var(--line-2); }
.context-meter-btn svg { width: 14px; height: 14px; stroke: var(--accent); }
.context-bar-mini {
  width: 32px; height: 5px; border-radius: 999px; background: rgba(255, 255, 255, 0.1); overflow: hidden;
}
.context-bar-fill { height: 100%; width: 2%; background: var(--accent); border-radius: 999px; transition: width 0.3s; }

/* Docs link button in topbar */
.docs-link-btn {
  display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px;
  font-size: 12.5px; color: var(--muted); border: 1px solid var(--line);
  transition: all 0.2s;
}
.docs-link-btn:hover { color: #fff; background: rgba(255, 255, 255, 0.08); text-decoration: none; }
.docs-link-btn svg { width: 14px; height: 14px; }

.mobile-only { display: none; }

/* ---------- Mark (Logo) ---------- */
.mark { display: inline-block; background: url('favicon.svg') center/contain no-repeat; }
.mark.mini { width: 24px; height: 24px; animation: spin 22s linear infinite; }
.mark.big {
  width: 56px; height: 56px; filter: drop-shadow(0 0 28px rgba(217, 119, 87, 0.55));
  animation: spin 24s linear infinite, popIn 0.8s var(--spring) both;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes popIn { from { opacity: 0; transform: scale(0.4) rotate(-90deg); } }

/* ---------- Thread / Chat Messages ---------- */
.scroller {
  flex: 1; overflow-y: auto; padding: 0 28px; display: flex; flex-direction: column;
  position: relative; z-index: 5;
  /* Prevent jump-scroll during streaming — keep scroll position stable */
  overflow-anchor: none;
  overscroll-behavior-y: contain;
}

.welcome {
  max-width: 720px; width: 100%; margin: 0 auto; padding-top: 18vh;
  display: flex; flex-direction: column; align-items: center; text-align: center; gap: 8px;
  transition: opacity 0.4s, transform 0.5s var(--ease); pointer-events: none;
}
.welcome * { pointer-events: auto; }
.greet {
  font-family: var(--serif); font-weight: 400; font-size: clamp(34px, 5.2vw, 48px);
  letter-spacing: -0.02em; margin: 16px 0 0;
  background: linear-gradient(100deg, #f3eee5 30%, var(--accent-2) 50%, #f3eee5 70%);
  background-size: 250% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shimmer 6s ease-in-out infinite, rise 0.8s var(--ease) 0.1s both;
}
.sub { color: var(--muted); margin: 0; font-size: 16px; animation: rise 0.8s var(--ease) 0.25s both; }
@keyframes shimmer { 0%, 100% { background-position: 100% 0; } 50% { background-position: 0 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(12px); filter: blur(4px); } }

.app.chatting .welcome { display: none; }

.thread {
  max-width: 800px; width: 100%; margin: 0 auto; padding: 12px 0 48px;
  display: flex; flex-direction: column; gap: 28px;
}

.msg { animation: rise 0.45s var(--ease) both; }
.msg.user { align-self: flex-end; max-width: 82%; }
.msg.user .bubble {
  background: var(--user); border: 1px solid var(--line);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  padding: 12px 18px; border-radius: 20px 20px 6px 20px;
  white-space: pre-wrap; word-wrap: break-word; line-height: 1.6; font-size: 15.5px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
}
.msg.user .user-media {
  max-width: 300px; max-height: 240px; border-radius: 12px; margin-bottom: 8px;
  display: block; border: 1px solid var(--line); object-fit: cover;
}

.msg.ai { display: flex; gap: 14px; width: 100%; }
.msg.ai .ai-mark { width: 28px; height: 28px; flex: none; margin-top: 4px; }
.msg.ai.streaming .ai-mark { animation: spin 1.4s linear infinite; filter: drop-shadow(0 0 12px rgba(217, 119, 87, 0.8)); }
.msg.ai .body { flex: 1; min-width: 0; }

.content {
  font-family: var(--serif); font-size: 17.5px; line-height: 1.68; color: #ebe7de; word-wrap: break-word;
}
.content > *:first-child { margin-top: 0; }
.content > *:last-child { margin-bottom: 0; }
.content p { margin: 0 0 0.9em; }
.content h1, .content h2, .content h3 { font-family: var(--serif); font-weight: 500; line-height: 1.3; margin: 1.2em 0 0.5em; }
.content h1 { font-size: 1.45em; color: #fff; }
.content h2 { font-size: 1.28em; color: #f5f2eb; }
.content h3 { font-size: 1.12em; color: #ece8e1; }
.content ul, .content ol { padding-left: 1.4em; margin: 0 0 0.9em; }
.content li { margin: 0.3em 0; }
.content a { color: var(--accent-2); text-decoration: underline; text-underline-offset: 3px; }
.content blockquote {
  margin: 0 0 1em; padding: 6px 18px; border-left: 3px solid var(--accent); color: var(--muted);
  background: rgba(255, 255, 255, 0.02); border-radius: 0 8px 8px 0;
}
.content table {
  border-collapse: collapse; margin: 0 0 1em; font-family: var(--sans); font-size: 14px;
  width: 100%; display: block; overflow-x: auto;
}
.content th, .content td { border: 1px solid var(--line-2); padding: 8px 12px; }
.content th { background: rgba(255, 255, 255, 0.06); font-weight: 500; }
.content :not(pre) > code {
  font-family: var(--mono); font-size: 0.84em; background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--line); padding: 2px 6px; border-radius: 6px; color: #f0b9a1;
}

/* Image Embed Cards generated or fetched by AI */
.content img {
  max-width: 100%; border-radius: 14px; border: 1px solid var(--line);
  margin: 10px 0; box-shadow: 0 8px 28px rgba(0, 0, 0, 0.4); display: block;
}

/* Code Blocks */
.code-block {
  margin: 0 0 1.2em; border-radius: 14px; overflow: hidden; border: 1px solid var(--line);
  background: rgba(14, 14, 13, 0.85); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.code-head {
  display: flex; justify-content: space-between; align-items: center; padding: 8px 14px;
  font-family: var(--sans); font-size: 12px; color: var(--dim); background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid var(--line);
}
.code-head-actions { display: flex; align-items: center; gap: 8px; }
.code-head button {
  display: flex; align-items: center; gap: 6px; color: var(--muted); padding: 4px 9px;
  border-radius: 6px; font-size: 12px; transition: background 0.2s, color 0.2s; cursor: pointer;
}
.code-head button:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }
.code-head svg { width: 13px; height: 13px; }
.code-block pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.code-block code { font-family: var(--mono); font-size: 13.5px; line-height: 1.6; background: none !important; padding: 0 !important; }

/* Blinking Cursor during Streaming */
.caret {
  display: inline-block; width: 8px; height: 8px; margin-left: 4px; border-radius: 50%;
  background: var(--accent); vertical-align: middle; animation: blink 0.9s ease-in-out infinite;
  box-shadow: 0 0 12px var(--accent);
}
@keyframes blink { 50% { transform: scale(0.4); opacity: 0.3; } }

/* Thinking Shimmer */
.thinking { display: flex; align-items: center; gap: 10px; color: var(--muted); font-family: var(--sans); font-size: 14px; padding: 4px 0; }
.thinking .shim {
  background: linear-gradient(90deg, var(--dim) 0%, #f2ddd2 50%, var(--dim) 100%);
  background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shim 1.6s linear infinite;
}
@keyframes shim { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* Transparent & Collapsed Thought Process */
.reason-slot { margin: 0 0 8px; }
.reasoning {
  margin: 0 0 8px; font-family: var(--sans); font-size: 13px; color: var(--muted);
  border: none; background: transparent; transition: all 0.2s ease;
}
.reasoning summary {
  cursor: pointer; padding: 4px 0; list-style: none; display: inline-flex; gap: 6px; align-items: center;
  color: var(--dim); user-select: none; font-size: 12.5px; transition: color 0.2s;
}
.reasoning summary::-webkit-details-marker { display: none; }
.reasoning summary:hover { color: var(--text); }
.reasoning summary::before {
  content: '›'; font-size: 14px; transition: transform 0.25s; display: inline-block;
}
.reasoning[open] {
  background: rgba(255, 255, 255, 0.03); border: 1px solid var(--line);
  border-radius: 12px; padding: 6px 12px;
}
.reasoning[open] summary { color: var(--muted); margin-bottom: 6px; }
.reasoning[open] summary::before { transform: rotate(90deg); }
.reasoning div {
  padding: 4px 2px 6px; white-space: pre-wrap; line-height: 1.55; max-height: 260px;
  overflow-y: auto; color: var(--muted); font-size: 12.5px; font-family: var(--sans);
}

/* Action Buttons below AI message (Fixed: always clickable & visible on hover/last) */
.actions {
  display: flex; align-items: center; gap: 6px; margin-top: 10px; opacity: 0;
  transition: opacity 0.25s; font-family: var(--sans);
}
.msg.ai:hover .actions, .msg.ai:last-child .actions { opacity: 1; }
.msg.streaming .actions { opacity: 0 !important; }
.actions button {
  display: flex; align-items: center; justify-content: center; width: 32px; height: 32px; border-radius: 8px;
  color: var(--dim); transition: all 0.2s; cursor: pointer;
}
.actions button:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }
.actions button:active { transform: scale(0.88); }
.actions button.active-like { color: var(--accent) !important; }
.actions svg { width: 15px; height: 15px; }
.actions .meta { margin-left: 8px; font-size: 11.5px; color: var(--dim); }

/* ---------- Composer ---------- */
.composer-wrap {
  padding: 0 24px 16px; max-width: 820px; width: 100%; margin: 0 auto;
  position: relative; z-index: 30; pointer-events: auto;
}
.composer {
  border-radius: 24px; padding: 14px 16px 10px 18px; position: relative; z-index: 31;
  transition: border-color 0.3s, box-shadow 0.3s;
  background: rgba(48, 47, 44, 0.7); pointer-events: auto;
}
.composer:focus-within {
  border-color: rgba(217, 119, 87, 0.4);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 0 0 4px rgba(217, 119, 87, 0.08), 0 20px 50px -12px rgba(0, 0, 0, 0.7);
}

/* Image/File Attachment Pill Tray */
.media-tray { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.media-preview-pill {
  position: relative; display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 6px; border-radius: 10px; background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line); font-size: 12px; color: var(--muted);
}
.media-preview-pill img { width: 24px; height: 24px; border-radius: 6px; object-fit: cover; }
.media-remove-btn { color: var(--dim); display: grid; place-items: center; cursor: pointer; }
.media-remove-btn:hover { color: #f08a7a; }

.composer textarea {
  width: 100%; resize: none; background: none; border: 0; outline: 0;
  color: var(--text); font-family: var(--sans); font-size: 15.5px; line-height: 1.5;
  max-height: 220px; min-height: 26px;
}
.composer textarea::placeholder { color: var(--dim); }

.composer-row { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; position: relative; z-index: 32; }
.left-tools, .right-tools { display: flex; align-items: center; gap: 8px; }

.tool-btn {
  width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--line-2);
  display: grid; place-items: center; color: var(--muted); transition: all 0.2s var(--spring); cursor: pointer;
}
.tool-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); transform: translateY(-1px); }
.tool-btn svg { width: 16px; height: 16px; }

/* Model Picker in Composer */
.model-picker { position: relative; z-index: 35; }
.model-btn {
  display: flex; align-items: center; gap: 6px; padding: 7px 11px; border-radius: 10px;
  font-size: 13.5px; color: #cfccc4; transition: background 0.2s; border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04); cursor: pointer;
}
.model-btn:hover { background: rgba(255, 255, 255, 0.09); color: var(--text); }
.model-btn svg { width: 14px; height: 14px; transition: transform 0.3s var(--ease); }
.model-picker.open .model-btn svg { transform: rotate(180deg); }

.model-menu {
  position: absolute; right: 0; bottom: calc(100% + 12px); width: 330px; border-radius: 18px;
  padding: 8px; background: rgba(35, 34, 32, 0.94);
  transform-origin: bottom right; transform: scale(0.9) translateY(10px);
  opacity: 0; pointer-events: none; transition: transform 0.35s var(--spring), opacity 0.2s;
  z-index: 100; max-height: 65vh; overflow-y: auto; border: 1px solid var(--line-2);
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.8);
}
.model-picker.open .model-menu { transform: none; opacity: 1; pointer-events: auto; }
.mm-group {
  font-size: 11px; color: var(--dim); padding: 8px 10px 4px; text-transform: uppercase;
  letter-spacing: 0.07em; font-weight: 600;
}
.mm-item {
  width: 100%; text-align: left; display: flex; align-items: center; gap: 10px;
  padding: 9px 11px; border-radius: 12px; transition: background 0.2s; cursor: pointer;
}
.mm-item:hover { background: rgba(255, 255, 255, 0.08); }
.mm-item div { flex: 1; display: flex; flex-direction: column; pointer-events: none; }
.mm-item b { font-weight: 500; font-size: 13.5px; display: flex; align-items: center; gap: 6px; }
.mm-item small { color: var(--dim); font-size: 11.5px; line-height: 1.35; margin-top: 2px; }
.mm-item .badge-best {
  font-size: 10px; background: rgba(217, 119, 87, 0.2); color: var(--accent);
  padding: 1px 6px; border-radius: 4px; font-weight: 600; text-transform: uppercase;
}
.mm-item.locked {
  opacity: 0.72;
}
.mm-item.locked:hover {
  opacity: 1;
  background: rgba(217, 119, 87, 0.08);
}
/* Badge-lock: SVG lock icon + text, no emoji */
.badge-lock {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 10.5px; font-weight: 500; color: var(--muted);
  background: rgba(255, 255, 255, 0.07); padding: 1px 6px 1px 4px;
  border-radius: 5px; vertical-align: middle; text-transform: none;
}
.badge-lock svg { width: 11px; height: 11px; stroke: var(--muted); flex: none; }
.mm-item.locked .check { opacity: 1; color: var(--muted); }
.mm-item.locked .check svg { width: 14px; height: 14px; stroke: var(--muted); }

/* Guest token limit banner */
.guest-limit-banner {
  padding: 12px 16px; border-radius: 14px; background: rgba(217, 119, 87, 0.15);
  border: 1px solid var(--accent); color: #f5cfc1; font-size: 13.5px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px;
}
.guest-limit-btn {
  padding: 6px 14px; border-radius: 8px; background: var(--accent); color: #fff;
  font-weight: 600; font-size: 12.5px; cursor: pointer; white-space: nowrap;
}
.mm-item .check { color: var(--accent); opacity: 0; pointer-events: none; }
.mm-item.sel .check { opacity: 1; }

/* Send Button */
.send {
  width: 36px; height: 36px; border-radius: 12px; background: var(--accent); color: #fff;
  display: grid; place-items: center; position: relative; cursor: pointer;
  transition: transform 0.3s var(--spring), opacity 0.2s, background 0.2s, box-shadow 0.3s;
  box-shadow: 0 4px 18px -4px rgba(217, 119, 87, 0.7);
}
.send:hover:not(:disabled) { transform: scale(1.07); background: #e3815f; }
.send:active:not(:disabled) { transform: scale(0.92); }
.send:disabled { opacity: 0.35; cursor: not-allowed; box-shadow: none; pointer-events: none; }
.send svg { position: absolute; transition: transform 0.35s var(--spring), opacity 0.2s; stroke-width: 2.3; }
.send .stop { opacity: 0; transform: scale(0.4); fill: currentColor; stroke: none; }
.send.busy { pointer-events: auto !important; cursor: pointer !important; opacity: 1 !important; }
.send.busy .arrow { opacity: 0; transform: translateY(-14px); }
.send.busy .stop { opacity: 1; transform: none; }
.send.busy {
  background: #3d3b38; box-shadow: 0 0 0 0 rgba(217, 119, 87, 0.5);
  animation: ring 1.6s infinite;
}
@keyframes ring { 70% { box-shadow: 0 0 0 10px rgba(217, 119, 87, 0); } }

/* ══ AI response images — controlled size, never giant ══ */
.content img {
  max-width: min(380px, 100%);
  max-height: 280px;
  width: auto;
  height: auto;
  border-radius: 10px;
  object-fit: cover;
  display: block;
  margin: 10px 0;
  border: 1px solid var(--line);
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  cursor: zoom-in;
  transition: transform 0.2s, box-shadow 0.2s;
}
.content img:hover {
  transform: scale(1.03);
  box-shadow: 0 8px 36px rgba(0,0,0,0.5);
}

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

/* ══ Topbar chat title (shows current chat name) ══ */
.chat-title {
  font-size: 14.5px; color: #d8d5cd; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; font-weight: 500;
  flex: 1; min-width: 0;
}

/* ══ Main area uses remaining space regardless of sidebar state ══ */
/* ══ Main area uses remaining space regardless of sidebar state ══ */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; overflow: hidden; }
  position: fixed; top: 0; right: 0; bottom: 0; width: min(600px, 92vw);
  background: rgba(30, 29, 27, 0.94); backdrop-filter: blur(36px) saturate(180%);
  -webkit-backdrop-filter: blur(36px) saturate(180%);
  border-left: 1px solid var(--line); box-shadow: -10px 0 50px rgba(0,0,0,0.7);
  z-index: 250; display: flex; flex-direction: column;
  transform: translateX(105%); transition: transform 0.45s var(--spring);
}
.artifact-drawer.open { transform: translateX(0); }
.artifact-header {
  padding: 14px 20px; display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--line);
}
.artifact-title-box { display: flex; align-items: center; gap: 10px; }
.artifact-title { font-weight: 600; font-size: 15px; color: var(--text); }

.artifact-actions { display: flex; align-items: center; gap: 18px; }
.artifact-btn {
  display: flex; align-items: center; gap: 7px; padding: 7px 14px; border-radius: 9px;
  background: rgba(255, 255, 255, 0.06); border: 1px solid var(--line); font-size: 12.5px;
  color: var(--text); font-weight: 500; transition: background 0.2s, transform 0.2s; cursor: pointer;
}
.artifact-btn:hover { background: rgba(255, 255, 255, 0.12); transform: translateY(-1px); }
.artifact-btn:active { transform: scale(0.95); }

.artifact-nav {
  display: flex; padding: 8px 20px; gap: 8px; border-bottom: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.2);
}
.artifact-tab {
  padding: 6px 14px; border-radius: 8px; font-size: 13px; color: var(--muted);
  transition: all 0.2s; cursor: pointer;
}
.artifact-tab.active { background: rgba(255, 255, 255, 0.1); color: var(--text); font-weight: 500; }

.artifact-body { flex: 1; position: relative; overflow: hidden; }
.artifact-pane { position: absolute; inset: 0; display: none; }
.artifact-pane.active { display: block; }
.artifact-preview-frame { width: 100%; height: 100%; border: 0; background: #ffffff; }
.artifact-code-view {
  width: 100%; height: 100%; margin: 0; padding: 18px; overflow: auto;
  font-family: var(--mono); font-size: 13px; line-height: 1.6; background: #131312; color: #ece9e1;
}

/* ---------- Modals ---------- */
.modal {
  position: fixed; inset: 0; z-index: 200; display: grid; place-items: center;
  background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  opacity: 0; pointer-events: none; transition: opacity 0.3s; padding: 20px;
}
.modal.open { opacity: 1; pointer-events: auto; }

.modal-card {
  width: min(540px, 100%); border-radius: 26px; padding: 22px 26px;
  background: rgba(40, 39, 37, 0.94); transform: scale(0.9) translateY(16px);
  transition: transform 0.45s var(--spring); max-height: 90vh; overflow-y: auto;
  border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0,0,0,0.7);
}
.modal.open .modal-card { transform: none; }

.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head h2 { font-family: var(--serif); font-weight: 400; font-size: 24px; margin: 0; }

/* Tabs inside Modal */
.modal-tabs {
  display: flex; gap: 6px; border-bottom: 1px solid var(--line); padding-bottom: 10px; margin-bottom: 16px;
  overflow-x: auto;
}
.modal-tab-btn {
  padding: 6px 12px; border-radius: 9px; font-size: 13px; color: var(--muted);
  white-space: nowrap; transition: background 0.2s, color 0.2s; cursor: pointer;
}
.modal-tab-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }
.modal-tab-btn.active { color: var(--accent); background: rgba(217, 119, 87, 0.15); font-weight: 500; }

.tab-pane { display: none; }
.tab-pane.active { display: block; }

.row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.row span { display: flex; flex-direction: column; }
.row b { font-weight: 500; font-size: 14px; }
.row small { color: var(--dim); font-size: 12px; margin-top: 2px; }
.row select {
  max-width: 220px; background: rgba(0, 0, 0, 0.35); color: var(--text);
  border: 1px solid var(--line-2); border-radius: 10px; padding: 8px 10px; font-size: 13px;
}
.row input[type=range] { accent-color: var(--accent); width: 150px; }

.switch {
  appearance: none; width: 46px; height: 28px; border-radius: 999px; background: #4a4845;
  position: relative; cursor: pointer; transition: background 0.3s; flex: none;
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  border-radius: 50%; background: #fff; transition: transform 0.35s var(--spring);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); }

/* Plugins List Card */
.plugin-item {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 12px 14px; border-radius: 14px; background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--line); margin-bottom: 8px;
}
.plugin-info { display: flex; align-items: center; gap: 12px; min-width: 0; }
.plugin-icon { font-size: 22px; flex: none; }
.plugin-meta b { display: block; font-size: 13.5px; font-weight: 500; }
.plugin-meta small { color: var(--dim); font-size: 12px; }

/* Context Window Modal UI */
.context-progress-box {
  padding: 16px; border-radius: 16px; background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--line); margin-bottom: 16px;
}
.context-progress-bar {
  height: 8px; border-radius: 999px; background: rgba(255, 255, 255, 0.1);
  overflow: hidden; margin: 10px 0 6px;
}
.context-progress-fill {
  height: 100%; width: 2%; background: linear-gradient(90deg, var(--accent), #f3a07a);
  border-radius: 999px; transition: width 0.4s;
}
.context-stat-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px;
}
.context-stat-card {
  padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--line); font-size: 12px;
}
.context-stat-card span { color: var(--dim); display: block; }
.context-stat-card b { font-size: 14px; color: #fff; margin-top: 2px; }

/* Auth Buttons */
.auth-grid { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.auth-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px; padding: 11px;
  border-radius: 12px; font-weight: 500; font-size: 14px; transition: all 0.2s;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.04); cursor: pointer;
}
.auth-btn:hover { background: rgba(255, 255, 255, 0.09); border-color: var(--line-2); }
.auth-btn.discord { background: #5865f2; color: #fff; border-color: transparent; }
.auth-btn.discord:hover { background: #4752c4; }

.divider-text {
  text-align: center; margin: 14px 0; font-size: 12px; color: var(--dim); position: relative;
}
.divider-text::before, .divider-text::after {
  content: ''; position: absolute; top: 50%; width: 42%; height: 1px; background: var(--line);
}
.divider-text::before { left: 0; }
.divider-text::after { right: 0; }

.form-group { margin-bottom: 12px; }
.form-group label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 5px; }
.form-control {
  width: 100%; padding: 10px 12px; border-radius: 11px; background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--line-2); color: var(--text); outline: 0; font-size: 14px;
}
.form-control:focus { border-color: var(--accent); }

/* Data Leak Warning Card */
.leak-warning-card {
  display: none; margin: 12px 0; padding: 12px 14px; border-radius: 12px;
  background: rgba(239, 100, 97, 0.14); border: 1px solid rgba(239, 100, 97, 0.4);
  color: #ff9b97; font-size: 13px; line-height: 1.45;
}
.leak-warning-card.show { display: block; animation: rise 0.4s var(--ease); }
.leak-warning-actions { display: flex; gap: 10px; margin-top: 8px; }
.leak-btn {
  padding: 5px 12px; border-radius: 8px; font-size: 12px; font-weight: 500; cursor: pointer;
}
.leak-btn.proceed { background: rgba(255, 255, 255, 0.1); color: var(--text); }
.leak-btn.change { background: var(--danger); color: #fff; }

.btn-primary {
  width: 100%; padding: 12px; border-radius: 12px; background: var(--accent);
  color: #fff; font-weight: 600; font-size: 14.5px; transition: background 0.2s;
  margin-top: 8px; cursor: pointer;
}
.btn-primary:hover { background: #e3815f; }

.danger {
  margin-top: 16px; width: 100%; padding: 11px; border-radius: 12px; color: #f0a090;
  border: 1px solid rgba(240, 120, 100, 0.25); transition: background 0.2s; cursor: pointer;
}
.danger:hover { background: rgba(240, 120, 100, 0.1); }

/* Model Advice Cards */
.advice-card {
  padding: 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--line); margin-bottom: 8px;
}
.advice-card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.advice-card-head b { font-size: 14px; color: var(--text); }
.advice-card-desc { font-size: 12.5px; color: var(--muted); margin: 0; line-height: 1.4; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 28px; z-index: 300;
  transform: translate(-50%, 30px); opacity: 0; padding: 10px 18px; border-radius: 14px;
  font-size: 13.5px; pointer-events: none; transition: transform 0.45s var(--spring), opacity 0.3s;
  background: rgba(50, 49, 46, 0.94); border: 1px solid var(--line-2);
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

.scrim { display: none; }

/* ---------- Responsive / Mobile ---------- */
@media (max-width: 820px) {
  .mobile-only { display: grid; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; margin: 8px; z-index: 150;
    transform: translateX(calc(-100% - 20px)); background: rgba(28, 27, 25, 0.96);
  }
  .app.side-open .sidebar { transform: none; }
  .app.collapsed .sidebar { width: var(--side-w); }
  .app.collapsed .sidebar * { opacity: 1 !important; pointer-events: auto !important; }
  #collapseBtn { display: none; }
  .scrim {
    display: block; position: fixed; inset: 0; z-index: 140; background: rgba(0, 0, 0, 0.55);
    opacity: 0; pointer-events: none; transition: opacity 0.3s;
  }
  .app.side-open .scrim { opacity: 1; pointer-events: auto; }
  .topbar { padding: 10px 16px; }
  .scroller { padding: 0 14px; }
  .composer-wrap { padding: 0 10px calc(10px + env(safe-area-inset-bottom)); }
  .welcome { padding-top: 10vh; }
  .content { font-size: 16px; }
  .model-menu { width: 280px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ══════════════════════════════════════════════
   PATCH — All remaining fixes appended
══════════════════════════════════════════════ */

/* Error text inside AI response */
.err-text {
  color: #f08a7a;
  font-size: 13px;
  margin-top: 8px;
  padding: 8px 12px;
  background: rgba(239,100,97,0.1);
  border: 1px solid rgba(239,100,97,0.25);
  border-radius: 8px;
}

/* Topbar left group */
.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}
.topbar-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

/* Make sure .main never shrinks below 0 and doesn't overflow sidebar */
.app.collapsed .main {
  /* When sidebar collapses to 64px, main grows naturally via flex */
  flex: 1;
  min-width: 0;
}

/* Docs link button */
.docs-link-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 999px;
  background: rgba(255,255,255,0.04); border: 1px solid var(--line);
  font-size: 12.5px; color: var(--muted); text-decoration: none;
  transition: all 0.2s; white-space: nowrap; cursor: pointer;
}
.docs-link-btn:hover { background: rgba(255,255,255,0.09); color: var(--text); border-color: var(--line-2); }
.docs-link-btn svg { width: 14px; height: 14px; }

/* Smooth thread anchor at bottom so streaming doesn't scroll-jump */
#thread::after {
  content: '';
  display: block;
  height: 1px;
  overflow-anchor: auto;
}

/* Ensure images in content are never wider than their container */
.content img, .msg img {
  max-width: min(380px, 100%) !important;
  max-height: 280px !important;
  height: auto !important;
  width: auto !important;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  margin: 10px 0;
  border: 1px solid var(--line);
}

/* Guest limit banner */
.guest-limit-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(217,119,87,0.14);
  border: 1px solid rgba(217,119,87,0.4);
  color: #f5cfc1;
  font-size: 13px;
  margin-bottom: 8px;
  animation: rise 0.4s var(--ease);
}
.guest-limit-btn {
  flex-shrink: 0;
  padding: 5px 14px;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.guest-limit-btn:hover { background: #e3815f; }

/* Context meter pill */
.context-meter-btn {
  display: flex; align-items: center; gap: 7px; padding: 6px 12px;
  border-radius: 999px; background: rgba(255,255,255,0.04);
  border: 1px solid var(--line); font-size: 12.5px; color: var(--muted);
  transition: all 0.2s; cursor: pointer; white-space: nowrap;
}
.context-meter-btn:hover { background: rgba(255,255,255,0.09); color: var(--text); border-color: var(--line-2); }
.context-meter-btn svg { width: 14px; height: 14px; stroke: var(--accent); }
.context-bar-mini {
  width: 32px; height: 5px; border-radius: 999px;
  background: rgba(255,255,255,0.1); overflow: hidden;
}
.context-bar-fill {
  height: 100%; width: 1%;
  background: linear-gradient(90deg, var(--accent), #f3a07a);
  border-radius: 999px; transition: width 0.5s;
}

/* Mobile: collapsed class shouldn't shrink sidebar on mobile — use side-open instead */
@media (max-width: 820px) {
  .app.collapsed .sidebar {
    width: var(--side-w) !important;
    padding: 12px !important;
    align-items: flex-start;
  }
  .app.collapsed .sidebar .brand,
  .app.collapsed .sidebar .new-chat > span:not(.plus),
  .app.collapsed .sidebar .search,
  .app.collapsed .sidebar .side-label,
  .app.collapsed .sidebar .history,
  .app.collapsed .sidebar .pinfo,
  .app.collapsed .sidebar .gear {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
    width: auto !important;
    max-width: none !important;
    overflow: visible !important;
    flex: initial !important;
    padding: initial !important;
    margin: initial !important;
    font-size: initial !important;
    border: initial !important;
  }
  .app.collapsed .sidebar .new-chat {
    width: auto !important;
    height: auto !important;
    padding: 8px 10px !important;
  }
  .app.collapsed .sidebar .profile {
    width: 100% !important;
    height: auto !important;
    padding: 6px !important;
    border-radius: 12px !important;
  }
  .app.collapsed .sidebar .side-bottom {
    justify-content: flex-start;
    border-top: 1px solid var(--line);
    padding-top: 8px;
  }
}
