:root {
  --bg: #0b0b12;
  --bg-elev: #14141f;
  --panel: #12121c;
  --panel-hover: #1d1d2b;
  --border: #26263a;
  --text: #ececf3;
  --text-dim: #9a9ab0;
  --accent-1: #7c5cff;   /* violet */
  --accent-2: #3b82f6;   /* bleu */
  --user-bubble-1: #7c5cff;
  --user-bubble-2: #5b7cff;
  --lia-bubble: #1b1b28;
  --danger: #ff5c6c;
  --radius: 18px;
  --grad: linear-gradient(135deg, var(--accent-1), var(--accent-2));
  --code-bg: #0e0e18;
  --toast-bg: rgba(30,30,45,.85);
  --scroll-thumb: #2a2a3d;
  --topbar-bg: rgba(11,11,18,.7);
  --login-glow: #1a1730;
  font-synthesis: none;
}

:root[data-theme="light"] {
  --bg: #f5f5f8;
  --bg-elev: #ffffff;
  --panel: #fbfbfd;
  --panel-hover: #ececf1;
  --border: #e3e3ea;
  --text: #17171f;
  --text-dim: #6a6a7e;
  --lia-bubble: #eef0f4;
  --code-bg: #eceef4;
  --toast-bg: rgba(255,255,255,.9);
  --scroll-thumb: #cfcfda;
  --topbar-bg: rgba(255,255,255,.75);
  --login-glow: #e9e6f7;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 6px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------------- Layout ---------------- */
.app { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar (history) */
.sidebar {
  width: 280px; flex: 0 0 280px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transition: margin-left .25s ease;
}
.sidebar.collapsed { margin-left: -280px; }
.sidebar-head {
  padding: 14px 14px 10px; display: flex; align-items: center; gap: 10px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.brand .avatar { width: 30px; height: 30px; }
.new-btn {
  margin: 4px 12px 12px; padding: 11px 14px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--bg-elev); color: var(--text);
  display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600;
  transition: background .15s;
}
.new-btn:hover { background: var(--panel-hover); }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.conv-item {
  padding: 10px 12px; border-radius: 10px; margin-bottom: 3px;
  color: var(--text-dim); font-size: 14px; cursor: pointer;
  display: flex; align-items: center; gap: 8px;
  white-space: nowrap; overflow: hidden;
}
.conv-item:hover { background: var(--panel-hover); color: var(--text); }
.conv-item.active { background: var(--panel-hover); color: var(--text); }
.conv-item .conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.conv-item .conv-actions { display: none; gap: 4px; }
.conv-item:hover .conv-actions { display: flex; }
.icon-mini {
  background: none; border: none; color: var(--text-dim); padding: 2px 4px;
  border-radius: 6px; font-size: 13px;
}
.icon-mini:hover { color: var(--text); background: var(--panel-hover); }
.sidebar-foot {
  border-top: 1px solid var(--border); padding: 10px 14px;
  display: flex; flex-direction: column; gap: 8px;
  font-size: 13px; color: var(--text-dim);
}
.sidebar-foot #userEmail { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot .foot-row { display: flex; align-items: center; justify-content: space-between; }
.sidebar-foot .logout { color: var(--text-dim); background: none; border: none; }
.sidebar-foot .logout:hover { color: var(--danger); }

/* Main chat */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar {
  height: 56px; flex: 0 0 56px; display: flex; align-items: center; gap: 12px;
  padding: 0 16px; border-bottom: 1px solid var(--border);
  background: var(--topbar-bg); backdrop-filter: blur(12px);
}
.topbar .hamburger { background: none; border: none; color: var(--text); font-size: 20px; padding: 6px; border-radius: 8px; }
.topbar .hamburger:hover { background: var(--panel-hover); }
.topbar .title { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.topbar .title .avatar { width: 30px; height: 30px; }
.topbar .subtitle { font-size: 12px; color: var(--text-dim); font-weight: 400; }
.usage-pill {
  margin-left: auto; width: 42px; height: 42px; border: none; border-radius: 50%;
  background: transparent; display: inline-flex; align-items: center; justify-content: center;
  color: var(--text); padding: 0;
}
.usage-pill:hover .usage-ring { transform: translateY(-1px); filter: brightness(1.08); }
.usage-ring {
  --usage-deg: 0deg;
  width: 34px; height: 34px; border-radius: 50%;
  background: conic-gradient(var(--accent-2) var(--usage-deg), var(--border) 0);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 800; letter-spacing: 0;
  position: relative; transition: transform .15s, filter .15s;
}
.usage-ring::before {
  content: ""; position: absolute; inset: 4px; border-radius: 50%; background: var(--bg-elev);
}
.usage-ring span { position: relative; z-index: 1; }
.usage-ring.warn { background: conic-gradient(#f59e0b var(--usage-deg), var(--border) 0); }
.usage-ring.hot { background: conic-gradient(var(--danger) var(--usage-deg), var(--border) 0); }

/* Usage modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.42);
  display: none; align-items: flex-start; justify-content: flex-end;
  padding: 68px 18px 18px; backdrop-filter: blur(8px);
}
.modal-backdrop.open { display: flex; }
.usage-modal {
  width: min(380px, calc(100vw - 36px)); background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: 16px; padding: 16px;
  box-shadow: 0 22px 60px rgba(0,0,0,.35);
}
.usage-modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.modal-kicker { color: var(--text-dim); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.usage-modal h2 { margin: 3px 0 0; font-size: 20px; line-height: 1.2; }
.modal-close {
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: var(--panel-hover); color: var(--text); font-size: 22px; line-height: 1;
}
.usage-meta { margin-top: 10px; color: var(--text-dim); font-size: 13px; }
.usage-bars { margin-top: 18px; display: flex; flex-direction: column; gap: 18px; }
.usage-row-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.usage-row-head span { color: var(--text); font-size: 14px; font-weight: 700; }
.usage-row-head strong { font-size: 14px; }
.usage-track {
  height: 10px; overflow: hidden; border-radius: 999px; background: var(--panel);
  border: 1px solid var(--border);
}
.usage-fill {
  width: 0%; height: 100%; border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-1), var(--accent-2));
  transition: width .3s ease;
}
.usage-fill.weekly { background: linear-gradient(90deg, #14b8a6, #3b82f6); }
.usage-fill.warn { background: linear-gradient(90deg, #f59e0b, #f97316); }
.usage-fill.hot { background: linear-gradient(90deg, #fb7185, var(--danger)); }
.usage-reset { margin-top: 7px; color: var(--text-dim); font-size: 12.5px; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 24px 0 8px; }
.msg-wrap { max-width: 820px; margin: 0 auto; padding: 0 20px; }
.msg { display: flex; gap: 12px; margin-bottom: 22px; align-items: flex-start; }
.msg.user { flex-direction: row-reverse; }
.msg .avatar { width: 34px; height: 34px; flex: 0 0 34px; margin-top: 2px; }
.bubble {
  padding: 12px 16px; border-radius: var(--radius); font-size: 15px; line-height: 1.55;
  max-width: 76%; word-wrap: break-word; white-space: pre-wrap;
}
.msg.assistant .bubble { background: var(--lia-bubble); border: 1px solid var(--border); border-top-left-radius: 6px; }
.msg.user .bubble { background: linear-gradient(135deg, var(--user-bubble-1), var(--user-bubble-2)); color: #fff; border-top-right-radius: 6px; }
.bubble img.attach { max-width: 220px; border-radius: 12px; margin-bottom: 8px; display: block; }
.bubble a { color: #9db4ff; }

/* Assistant markdown-ish */
.bubble code { background: var(--code-bg); padding: 2px 6px; border-radius: 6px; font-size: 13px; }
.bubble pre { background: var(--code-bg); padding: 12px; border-radius: 10px; overflow-x: auto; }
.bubble pre code { background: none; padding: 0; }

/* Activity / thinking */
.activity {
  display: flex; align-items: center; gap: 10px; color: var(--text-dim);
  font-size: 14px; padding: 4px 0;
}
.dots { display: inline-flex; gap: 4px; }
.dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent-1); animation: bounce 1.2s infinite; }
.dots span:nth-child(2) { animation-delay: .2s; background: #6a6aff; }
.dots span:nth-child(3) { animation-delay: .4s; background: var(--accent-2); }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity:.5;} 30% { transform: translateY(-5px); opacity:1;} }

.steps-toggle { margin-top: 8px; }
.steps-toggle summary {
  cursor: pointer; color: var(--text-dim); font-size: 12.5px; list-style: none;
}
.steps-toggle summary::-webkit-details-marker { display: none; }
.steps-list { margin-top: 6px; padding-left: 4px; border-left: 2px solid var(--border); }
.step { font-size: 12.5px; color: var(--text-dim); padding: 3px 0 3px 10px; }

/* Empty state */
.empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; color: var(--text-dim); text-align: center; padding: 20px;
}
.empty .avatar { width: 84px; height: 84px; }
.empty h2 { color: var(--text); margin: 0; font-size: 22px; }

/* Composer */
.composer-wrap { padding: 12px 20px 20px; }
.composer {
  max-width: 820px; margin: 0 auto;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 24px;
  padding: 8px 8px 8px 16px; display: flex; align-items: flex-end; gap: 8px;
}
.composer:focus-within { border-color: #3a3a58; }
.composer textarea {
  flex: 1; background: none; border: none; color: var(--text); resize: none;
  font-size: 15px; line-height: 1.5; max-height: 180px; padding: 8px 0; outline: none;
}
.composer textarea::placeholder { color: var(--text-dim); }
.comp-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none; flex: 0 0 40px;
  display: flex; align-items: center; justify-content: center; font-size: 18px;
  background: var(--panel-hover); color: var(--text); transition: transform .1s, background .15s;
}
.comp-btn:hover { filter: brightness(1.15); }
.comp-btn.send { background: var(--grad); color: #fff; }
.comp-btn.send:disabled { opacity: .4; }
.comp-btn.rec { background: var(--grad); color: #fff; animation: pulse 1.4s infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(124,92,255,.5);} 50% { box-shadow: 0 0 0 8px rgba(124,92,255,0);} }
.attach-preview { max-width: 820px; margin: 0 auto 8px; display: flex; gap: 8px; flex-wrap: wrap; }
.attach-preview .thumb { position: relative; }
.attach-preview img { width: 56px; height: 56px; object-fit: cover; border-radius: 10px; }
.attach-preview .rm { position: absolute; top: -6px; right: -6px; background: #000; color: #fff; border-radius: 50%; width: 20px; height: 20px; border: none; font-size: 12px; }

/* Avatar visual (fallback gradient with initial) */
.avatar {
  border-radius: 50%; background: var(--grad); display: inline-flex;
  align-items: center; justify-content: center; color: #fff; font-weight: 700;
  overflow: hidden; flex-shrink: 0;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* Toast */
.toast-layer { position: fixed; top: 70px; left: 0; right: 0; display: flex; justify-content: center; pointer-events: none; z-index: 50; }
.toast {
  background: var(--toast-bg); backdrop-filter: blur(14px);
  border: 1px solid var(--border); color: var(--text);
  padding: 10px 18px; border-radius: 999px; font-size: 14px; font-weight: 500;
  opacity: 0; transform: translateY(-8px); transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* Login */
.login-page { height: 100vh; display: flex; align-items: center; justify-content: center; background: radial-gradient(1200px 600px at 50% -10%, var(--login-glow), var(--bg)); }
.login-card {
  width: 340px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 22px; padding: 32px 28px; text-align: center;
}
.login-card .avatar { width: 72px; height: 72px; margin: 0 auto 14px; font-size: 30px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 22px; color: var(--text-dim); font-size: 14px; }
.login-card input {
  width: 100%; padding: 13px 14px; margin-bottom: 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); font-size: 15px; outline: none;
}
.login-card input:focus { border-color: var(--accent-1); }
.login-card button {
  width: 100%; padding: 13px; border: none; border-radius: 12px; margin-top: 4px;
  background: var(--grad); color: #fff; font-size: 15px; font-weight: 600;
}
.login-card .err { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 8px; }

/* Conversation "..." menu */
.conv-item .conv-menu-btn {
  background: none; border: none; color: var(--text-dim); font-size: 18px;
  padding: 0 4px; border-radius: 6px; line-height: 1; opacity: 0; flex: 0 0 auto;
}
.conv-item:hover .conv-menu-btn, .conv-item.active .conv-menu-btn { opacity: 1; }
.conv-menu-btn:hover { color: var(--text); background: var(--panel-hover); }
.conv-menu {
  position: fixed; z-index: 60; min-width: 160px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0,0,0,.35); padding: 6px; display: none;
}
.conv-menu.open { display: block; }
.conv-menu button {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  background: none; border: none; color: var(--text); padding: 9px 12px;
  border-radius: 8px; font-size: 14px;
}
.conv-menu button:hover { background: var(--panel-hover); }
.conv-menu button.danger { color: var(--danger); }

/* Theme switch (footer) */
.theme-switch { display: inline-flex; gap: 2px; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px; padding: 2px; }
.theme-switch button {
  background: none; border: none; color: var(--text-dim); font-size: 14px;
  width: 30px; height: 26px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center;
}
.theme-switch button.active { background: var(--panel-hover); color: var(--text); }

@media (max-width: 720px) {
  .sidebar { position: absolute; z-index: 40; height: 100%; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  .bubble { max-width: 84%; }
}
