:root {
  --bg: #1c1c1c;
  --sidebar: #141414;
  --surface: #262626;
  --surface-2: #2c2c2c;
  --surface-3: #232323;
  --field: #161616;
  --dialog: #1f1f1f;
  --code-bg: #0f0f0f; --code-head: #1a1a1a;
  --line: rgba(255,255,255,.06);
  --line-2: rgba(255,255,255,.09);
  --line-3: rgba(255,255,255,.12);
  --text: #ececec; --text-2: #cfcfcf; --text-3: #c4c4c4;
  --muted: #8a8a8a; --muted-2: #6f6f6f; --muted-3: #666;
  --accent: #10a37f; --accent-hover: #0e8f70; --accent-soft: #7fe0c3;
  --danger: #ef4444;
  --border: rgba(255,255,255,.1);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; touch-action: pan-x pan-y; -webkit-text-size-adjust: 100%; }
html:has(body.chat-page), body.chat-page { overflow: hidden; overscroll-behavior: none; }
#app, #main { max-width: 100vw; }
body { font: 15px/1.6 'Geist', system-ui, sans-serif; background: var(--bg); color: var(--text); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { color: #2fc79d; }
::placeholder { color: var(--muted-2); }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 10px; border: 3px solid transparent; background-clip: content-box; }
button, a, .conv, .suggestion, input { transition: background-color 120ms ease, color 120ms ease, border-color 120ms ease; }

.icon {
  font-family: 'Material Symbols Rounded'; font-size: 18px; line-height: 1; font-weight: normal; user-select: none;
  display: inline-block; width: 1em; height: 1em; overflow: hidden; white-space: nowrap; flex-shrink: 0;
}
.icon.fill { font-variation-settings: 'FILL' 1; }
.spacer { flex: 1; }
.icon-btn {
  width: 32px; height: 32px; flex-shrink: 0; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn .icon { font-size: 20px; }
.icon-btn:hover { background: #222; color: var(--text); }
.logo {
  width: 28px; height: 28px; border-radius: 8px; flex-shrink: 0;
  background: var(--accent); color: #0b2a21;
  display: flex; align-items: center; justify-content: center;
}

#app { display: flex; height: 100vh; height: 100dvh; width: 100%; overflow: hidden; }

#sidebar {
  width: 272px; flex-shrink: 0;
  background: var(--sidebar); border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 14px 12px 18px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.sidebar-actions { padding: 0 12px; display: flex; flex-direction: column; gap: 8px; }
#new-chat {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 12px; border-radius: 10px; border: 1px solid rgba(255,255,255,.1);
  background: #1e1e1e; color: var(--text); font-size: 14px; font-weight: 500; text-align: left;
}
#new-chat:hover { background: #262626; border-color: rgba(255,255,255,.16); }
.search {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 12px; border-radius: 10px; background: #1a1a1a; color: #7a7a7a;
}
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 14px; padding: 0; }
.search input::-webkit-search-cancel-button { display: none; }

#conversations { flex: 1; overflow-y: auto; padding: 14px 12px 8px; display: flex; flex-direction: column; gap: 16px; }
.conv-group { display: flex; flex-direction: column; gap: 1px; }
.conv-group-label {
  font-size: 10.5px; font-weight: 500; color: var(--muted-3);
  padding: 0 10px 6px; letter-spacing: .08em; text-transform: uppercase;
}
.conv {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px 7px 10px; border-radius: 8px;
  cursor: pointer; font-size: 14px; color: var(--text-3);
  position: relative;
}
.conv + .conv::before {
  content: ''; position: absolute; left: 10px; right: 10px; top: -1px; height: 1px;
  background: rgba(255,255,255,.06); pointer-events: none;
}
.conv:hover::before, .conv:hover + .conv::before,
.conv.active::before, .conv.active + .conv::before { opacity: 0; }
.conv:hover { background: #222; color: var(--text); }
.conv.active { background: var(--surface); color: var(--text); }
.conv-title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conv-actions { display: none; gap: 2px; }
.conv:hover .conv-actions, .conv.active .conv-actions { display: flex; }
.conv-actions button {
  width: 24px; height: 24px; border: 0; border-radius: 6px; background: transparent; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
}
.conv-actions .icon { font-size: 16px; }
.conv-actions button:hover { background: #333; color: var(--text); }
.conv-actions button[data-delete]:hover { color: var(--danger); }
.conv-empty { color: var(--muted-2); font-size: 13px; padding: 0 10px; }

.sidebar-footer { border-top: 1px solid var(--line); padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 2px; }
.side-link {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; border: 0; border-radius: 8px;
  background: transparent; color: var(--text-2); font-size: 14px; text-align: left;
}
.side-link .icon { color: var(--muted); }
.side-link:hover { background: #1f1f1f; color: var(--text); }
.side-link-label { flex: 1; }
.mono-hint { font-family: 'Geist Mono', monospace; font-size: 11px; color: var(--muted-2); }
.user-card {
  display: flex; align-items: center; gap: 10px; margin-top: 8px; padding: 10px;
  border-radius: 12px; background: #1a1a1a; border: 1px solid var(--line);
}
.avatar-initials {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  background: #2b3a35; color: var(--accent-soft); font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}
.user-info { flex: 1; min-width: 0; }
.user-email { font-size: 13px; font-weight: 500; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.user-role { font-size: 11.5px; color: #7a7a7a; }
.user-card form { margin: 0; }
.user-card .icon-btn { width: 30px; height: 30px; }
.user-card .icon-btn .icon { font-size: 18px; }
.user-card .icon-btn:hover { background: #262626; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
#topbar {
  height: 56px; flex-shrink: 0; display: flex; align-items: center; gap: 8px;
  padding: 0 16px; border-bottom: 1px solid var(--line);
}
#topbar.empty { border-bottom-color: transparent; }
#toggle-sidebar { display: none; width: 34px; height: 34px; color: #9b9b9b; }
#toggle-sidebar:hover { background: #262626; }
#app.sidebar-collapsed #sidebar { display: none; }
#app.sidebar-collapsed #toggle-sidebar { display: flex; }
#conv-title {
  min-width: 0; padding: 0 6px; font-size: 14.5px; font-weight: 500; letter-spacing: -0.005em;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}

#conv-status { display: flex; align-items: center; gap: 8px; }
#conv-status[hidden] { display: none; }
.pill {
  display: flex; align-items: center; gap: 8px; white-space: nowrap; flex-shrink: 0;
  padding: 5px 10px; border-radius: 999px; background: var(--surface-3);
  border: 1px solid rgba(255,255,255,.07); font-size: 12.5px; color: #b4b4b4;
}
.pill .icon { font-size: 16px; color: var(--muted); }
.gauge { width: 44px; height: 4px; border-radius: 4px; background: #363636; overflow: hidden; display: block; }
.gauge > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.pill .num { font-family: 'Geist Mono', monospace; font-size: 11.5px; color: var(--text); }
.mem-left { color: var(--muted); }
.mem-left.full { color: #f0b429; }
.pill .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); display: block; box-shadow: 0 0 0 3px rgba(16,163,127,.18); }
.pill.expired .dot { background: var(--muted); box-shadow: none; }
a.pill { color: var(--accent); }
a.pill:hover { color: #2fc79d; border-color: rgba(16,163,127,.4); }

#messages { flex: 1; overflow-y: auto; padding: 0 20px; }
#thread { max-width: 768px; margin: 0 auto; padding: 28px 0 12px; display: flex; flex-direction: column; gap: 28px; }
#thread:empty { display: none; }

#empty-state[hidden] { display: none; }
#empty-state { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px 0; gap: 28px; }
.empty-head { text-align: center; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.logo-lg { width: 44px; height: 44px; border-radius: 12px; margin-bottom: 6px; }
.logo-lg .icon { font-size: 26px; }
#empty-state h1 { margin: 0; font-size: 30px; font-weight: 600; letter-spacing: -0.02em; }
#no-key-hint { margin: 0; color: var(--muted); font-size: 14px; }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; width: 100%; max-width: 640px; }
.suggestion {
  display: flex; flex-direction: column; align-items: flex-start; gap: 4px;
  padding: 14px 16px; border-radius: 14px; border: 1px solid rgba(255,255,255,.08);
  background: #212121; color: var(--text); text-align: left;
}
.suggestion:hover { background: #262626; border-color: rgba(255,255,255,.14); }
.suggestion .icon { font-size: 20px; color: var(--muted); margin-bottom: 6px; }
.suggestion-title { font-size: 14px; font-weight: 500; }
.suggestion-sub { font-size: 13px; color: var(--muted); line-height: 1.45; }

.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--surface-2); border-radius: 20px 20px 6px 20px;
  padding: 10px 16px; max-width: 78%; white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant { display: flex; gap: 14px; }
.avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0; margin-top: 2px;
  background: var(--bg); border: 1px solid var(--line-3); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
}
.avatar .icon { font-size: 15px; }
.msg-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.msg.assistant .bubble { word-wrap: break-word; text-wrap: pretty; }
.msg.assistant .bubble > * { margin: 0; }
.msg.assistant .bubble > * + * { margin-top: 12px; }
.bubble ol, .bubble ul { padding-left: 22px; }
.bubble li + li { margin-top: 6px; }
.bubble strong { font-weight: 600; }
.bubble table { border-collapse: collapse; }
.bubble th, .bubble td { border: 1px solid var(--line-2); padding: 4px 10px; }
.bubble :not(pre) > code {
  font-family: 'Geist Mono', monospace; font-size: 13px;
  background: var(--surface-2); padding: 2px 6px; border-radius: 6px;
}
.msg.error .bubble { color: var(--danger); }
.msg.refused .bubble { color: var(--muted); font-style: italic; }
.msg.forgotten { opacity: .45; }

.code-block { border-radius: 12px; overflow: hidden; background: var(--code-bg); border: 1px solid rgba(255,255,255,.07); }
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 10px 7px 14px; background: var(--code-head); border-bottom: 1px solid var(--line);
  font-size: 12px; color: var(--muted);
}
.code-head > span { font-family: 'Geist Mono', monospace; }
.code-head button {
  display: flex; align-items: center; gap: 5px; border: 0; border-radius: 6px;
  background: transparent; color: #9b9b9b; font-size: 12px; padding: 3px 8px;
}
.code-head button .icon { font-size: 15px; }
.code-head button:hover { background: #262626; color: var(--text); }
.code-block pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.code-block pre code, .code-block pre code.hljs {
  font-family: 'Geist Mono', monospace; font-size: 13px; line-height: 1.7;
  padding: 0; background: transparent; color: #e6edf3;
}

.msg-tools { display: flex; gap: 2px; margin-left: -6px; }
.msg-tools button {
  display: flex; align-items: center; gap: 5px; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted); font-size: 13px; padding: 4px 8px;
}
.msg-tools .icon { font-size: 17px; }
.msg-tools button:hover { background: #262626; color: var(--text); }

.bubble.scrambled {
  white-space: pre-wrap; word-wrap: break-word; color: var(--text-2);
  filter: blur(5px); user-select: none; pointer-events: none;
}
.pending-label { font-size: 12.5px; color: var(--muted); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: .5; } 50% { opacity: 1; } }
.msg.typing { align-items: center; }
.msg.typing .avatar { margin-top: 0; }
.dots { display: flex; gap: 5px; align-items: center; height: 28px; }
.dots span { width: 6px; height: 6px; border-radius: 50%; background: #9b9b9b; display: block; animation: dot 1.2s infinite ease-in-out; }
.dots span:nth-child(2) { animation-delay: .15s; }
.dots span:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 0%, 80%, 100% { opacity: .25; transform: none; } 40% { opacity: 1; transform: translateY(-3px); } }

#composer { padding: 8px 20px 14px; flex-shrink: 0; }
.composer-box {
  max-width: 768px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--surface); border: 1px solid var(--line-2); border-radius: 24px;
  padding: 8px 8px 8px 18px; box-shadow: 0 8px 30px rgba(0,0,0,.25);
}
#input {
  flex: 1; min-width: 0; resize: none; border: 0; outline: 0; background: transparent;
  font-size: 15px; line-height: 1.55; padding: 7px 0; max-height: 200px;
}
.composer-row { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.composer-row .spacer { display: none; }
#char-count:empty { display: none; }
#char-count { font-family: 'Geist Mono', monospace; font-size: 11.5px; color: var(--muted-2); white-space: nowrap; flex-shrink: 0; }
#send {
  width: 36px; height: 36px; border-radius: 50%; border: 0; flex-shrink: 0;
  background: var(--text); color: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
#send .icon { font-size: 20px; font-weight: 500; }
#send:disabled { background: #3a3a3a; color: #7a7a7a; cursor: default; }
#stop {
  width: 36px; height: 36px; border-radius: 50%; border: 0; flex-shrink: 0;
  background: var(--text); color: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
#stop .icon { font-size: 18px; }
#stop:hover { background: #fff; }
#send[hidden], #stop[hidden] { display: none; }
.disclaimer { text-align: center; color: var(--muted-2); font-size: 12px; margin: 10px 0 0; }
.disclaimer kbd { font-family: 'Geist Mono', monospace; font-size: 11px; }

#account-dialog {
  width: min(520px, calc(100vw - 32px)); padding: 0; color: var(--text);
  background: var(--dialog); border: 1px solid var(--line-2); border-radius: 18px;
  box-shadow: 0 24px 80px rgba(0,0,0,.5); overflow: hidden;
}
#account-dialog::backdrop { background: rgba(0,0,0,.6); backdrop-filter: blur(4px); }
.dialog-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 14px 24px; border-bottom: 1px solid var(--line);
}
.dialog-head h2 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.dialog-head .icon-btn { color: #9b9b9b; }
.dialog-head .icon-btn:hover { background: #2a2a2a; }
.dialog-section { padding: 20px 24px; display: flex; flex-direction: column; gap: 14px; }
.dialog-section + .dialog-section { border-top: 1px solid var(--line); padding-bottom: 22px; }
.dialog-section h3 { margin: 0; font-size: 14px; font-weight: 600; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.help { margin: 0; font-size: 13px; color: var(--muted); }
.key-pill {
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
  padding: 4px 10px; border-radius: 999px; font-family: 'Geist Mono', monospace; font-size: 12px;
  background: rgba(16,163,127,.1); border: 1px solid rgba(16,163,127,.28); color: var(--accent-soft);
}
.key-pill::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); display: block; }
.key-pill.none { background: var(--surface-3); border-color: rgba(255,255,255,.07); color: var(--muted); }
.key-pill.none::before { background: var(--muted); }
.dialog-section label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--text-2); }
.dialog-section input {
  background: var(--field); border: 1px solid rgba(255,255,255,.1); border-radius: 10px;
  padding: 10px 12px; outline: 0; color: var(--text); font-size: 14px;
}
.dialog-section input.mono { font-family: 'Geist Mono', monospace; }
.dialog-section input:focus { border-color: var(--accent); }
.field-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.dialog-section .error { margin: 0; min-height: 0; }
.dialog-section .error:empty { display: none; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions.spread { justify-content: space-between; }
.dialog-actions.spread > :last-child { margin-left: auto; }
.btn-text-danger { border: 0; background: transparent; color: var(--danger); font-size: 14px; padding: 8px 4px; }
.btn-text-danger:hover { color: #ff6b6b; }
.btn-accent { border: 0; border-radius: 10px; background: var(--accent); color: #fff; font-size: 14px; font-weight: 500; padding: 8px 16px; }
.btn-accent:hover { background: var(--accent-hover); }
.btn-accent:disabled { opacity: .6; cursor: default; }
.btn-outline { border: 1px solid rgba(255,255,255,.1); border-radius: 10px; background: transparent; color: var(--text); font-size: 14px; padding: 8px 16px; }
.btn-outline:hover { background: #2a2a2a; }
.btn-neutral { border: 1px solid rgba(255,255,255,.1); border-radius: 10px; background: #2a2a2a; color: var(--text); font-size: 14px; font-weight: 500; padding: 8px 16px; }
.btn-neutral:hover { background: #333; }

.auth-card label, .panel label { display: block; margin-bottom: 14px; font-weight: 500; }
label small, .muted { color: var(--muted); font-weight: 400; font-size: 13px; }
label small { display: block; margin-top: 4px; }
label code { font-size: 12px; }
label.inline { display: flex; gap: 8px; align-items: center; font-weight: 400; margin: 6px 0; }
.auth-card input:not([type=checkbox]), .panel input:not([type=checkbox]), .panel textarea, .panel select {
  display: block; width: 100%; margin-top: 4px;
  background: var(--field); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 12px; outline: 0;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
.row { display: flex; gap: 8px; align-items: center; }
.row input { flex: 1; }
.auth-card button, .panel button, .btn {
  display: inline-block; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 14px; white-space: nowrap; text-decoration: none; color: var(--text); margin-top: 4px;
}
.auth-card button:hover, .panel button:hover { background: #2f2f2f; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 500; }
button.primary:hover { background: var(--accent-hover); }
button.danger { color: var(--danger); }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.error { color: var(--danger); min-height: 1em; margin: 0 0 8px; font-size: 14px; }

.auth-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--dialog);
  border: 1px solid var(--line-2); border-radius: 18px; padding: 28px;
  box-shadow: 0 24px 80px rgba(0,0,0,.5);
}
.auth-card h1 { margin-top: 0; font-size: 24px; font-weight: 600; letter-spacing: -0.02em; }
.auth-card button.primary { width: 100%; margin: 4px 0 12px; padding: 10px; }
.auth-card .muted { text-align: center; margin: 0; }

.admin-page { max-width: 980px; margin: 0 auto; padding: 16px; }
.admin-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.admin-header h1 { font-weight: 600; letter-spacing: -0.02em; }
.panel { background: var(--sidebar); border: 1px solid var(--line-2); border-radius: var(--radius); padding: 20px; margin: 16px 0; }
.panel h2 { font-size: 17px; font-weight: 600; margin: 24px 0 12px; }
.panel h2:first-of-type { margin-top: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 16px; }
.flash { background: rgba(16,163,127,.12); border: 1px solid rgba(16,163,127,.4); border-radius: 10px; padding: 10px 14px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line-2); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; }
tr.disabled td { opacity: .5; }
.row-actions form { display: flex; gap: 6px; justify-content: flex-end; }
.row-actions button { padding: 4px 10px; font-size: 13px; margin: 0; }

#backdrop { display: none; }
@media (max-width: 768px) {
  #sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20; max-width: 85vw;
    transform: translateX(-100%); transition: transform .2s;
  }
  #app.sidebar-collapsed #sidebar { display: flex; }
  #app.sidebar-open #sidebar { transform: none; }
  #app.sidebar-open #backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
  #toggle-sidebar { display: flex; }
  .conv-actions { display: flex; }
  #messages, #composer { padding-left: 16px; padding-right: 16px; }
  #input, .search input, .dialog-section input, .auth-card input:not([type=checkbox]) { font-size: 16px; }
  .suggestions { display: none; }
  #conv-status .pill-cache, #conv-status .pill-new { display: none; }
}
@media (max-width: 480px) {
  .pill-mem .mem-label, .pill-mem .gauge, .pill-mem .num { display: none; }
  .pill-mem { gap: 6px; padding: 4px 8px; font-size: 12px; }
}
.auth-card label.remember { display: flex; margin: -4px 0 16px; font-weight: 400; font-size: 14px; color: var(--text-2); cursor: pointer; }
.auth-card label.remember input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
