:root {
  --bg0: #07090f;
  --bg1: #0d1220;
  --glass: rgba(16, 22, 38, 0.72);
  --glass-border: rgba(255, 255, 255, 0.08);
  --text: #eef2ff;
  --muted: #93a0c0;
  --accent: #5eead4;
  --accent2: #818cf8;
  --accent3: #f472b6;
  --danger: #fb7185;
  --ok: #34d399;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  --radius: 18px;
  --font: "Instrument Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg0);
  overflow: hidden;
}

#bg { position: fixed; inset: 0; z-index: 0; pointer-events: none; overflow: hidden; }
.grid {
  position: absolute; inset: 0;
  background-image:
    linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px);
  background-size: 48px 48px;
  mask-image: radial-gradient(ellipse at center, black 20%, transparent 75%);
}
.orb {
  position: absolute; border-radius: 50%; filter: blur(70px); opacity: 0.55;
  animation: float 18s ease-in-out infinite;
}
.orb-a { width: 420px; height: 420px; background: #2dd4bf55; top: -80px; left: -60px; }
.orb-b { width: 480px; height: 480px; background: #6366f155; bottom: -120px; right: -80px; animation-delay: -6s; }
.orb-c { width: 280px; height: 280px; background: #ec489955; top: 40%; left: 45%; animation-delay: -11s; }
@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(30px, -24px) scale(1.08); }
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.glass {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(18px) saturate(140%);
  -webkit-backdrop-filter: blur(18px) saturate(140%);
  box-shadow: var(--shadow);
}

.screen {
  position: relative; z-index: 2;
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
}
.login-card {
  width: min(420px, 100%);
  border-radius: 28px;
  padding: 32px;
}
.brand { display: flex; gap: 14px; align-items: center; margin-bottom: 28px; }
.brand h1 { margin: 0; font-size: 1.5rem; letter-spacing: -0.03em; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: 0.92rem; }
.brand.mini { gap: 10px; margin-bottom: 14px; font-weight: 600; }
.logo-mark {
  width: 48px; height: 48px; border-radius: 16px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #041016; font-weight: 700; font-size: 1.3rem;
  box-shadow: 0 10px 30px rgba(94, 234, 212, 0.25);
}
.logo-mark.sm { width: 34px; height: 34px; border-radius: 12px; font-size: 1rem; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin: 0 0 8px; }
input, textarea, select {
  width: 100%;
  border-radius: 14px;
  border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  padding: 12px 14px;
  font: inherit;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
}
input:focus, textarea:focus, select:focus {
  border-color: rgba(94, 234, 212, 0.45);
  box-shadow: 0 0 0 4px rgba(94, 234, 212, 0.12);
}
textarea { resize: vertical; min-height: 44px; }

.btn {
  border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border-radius: 14px;
  padding: 11px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform .15s, background .15s, border-color .15s;
}
.btn:hover { transform: translateY(-1px); background: rgba(255,255,255,0.1); }
.btn:active { transform: translateY(0); }
.btn.primary {
  background: linear-gradient(135deg, #2dd4bf, #6366f1);
  border: none;
  color: #041016;
}
.btn.primary:hover { filter: brightness(1.05); }
.btn.full { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255,255,255,0.1); }
.icon-btn.danger:hover { color: var(--danger); border-color: rgba(251,113,133,0.4); }
.linkish {
  background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; font-weight: 600;
}
.error { color: var(--danger); min-height: 1.2em; font-size: 0.9rem; }

.app {
  position: relative; z-index: 1;
  height: 100vh;
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 14px;
  padding: 14px;
}

.sidebar {
  border-radius: var(--radius);
  display: flex; flex-direction: column;
  min-height: 0;
  padding: 14px;
}
.sidebar-top { display: flex; flex-direction: column; gap: 10px; }
.search-wrap input { border-radius: 12px; padding: 10px 12px; }
.conv-list {
  flex: 1; overflow: auto; margin: 12px 0;
  display: flex; flex-direction: column; gap: 6px;
  scrollbar-width: thin;
}
.conv-item {
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  font: inherit;
}
.conv-item:hover { background: rgba(255,255,255,0.05); }
.conv-item.active {
  background: linear-gradient(135deg, rgba(45,212,191,0.15), rgba(99,102,241,0.18));
  border-color: rgba(94,234,212,0.25);
}
.conv-item .t { font-weight: 600; font-size: 0.92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .d { color: var(--muted); font-size: 0.75rem; margin-top: 2px; }
.sidebar-bottom {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
}
.nav-btn {
  border-radius: 12px; border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.03); color: var(--muted);
  padding: 10px 6px; font: inherit; font-size: 0.8rem; font-weight: 600; cursor: pointer;
}
.nav-btn.active {
  color: #041016;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border: none;
}

.main {
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column; gap: 12px;
}
.topbar {
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.top-title { font-weight: 700; letter-spacing: -0.02em; }
.top-sub { color: var(--muted); font-size: 0.82rem; }
.model-select { width: auto; min-width: 180px; border-radius: 12px; padding: 8px 12px; }
.chat-mode-badge {
  max-width: min(340px, 48vw);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(94, 234, 212, 0.28);
  background: linear-gradient(135deg, rgba(45, 212, 191, 0.12), rgba(99, 102, 241, 0.14));
  color: var(--text);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(3, 6, 14, 0.72);
  backdrop-filter: blur(8px);
}
.modal-card {
  position: relative;
  width: min(560px, 100%);
  border-radius: 24px;
  padding: 22px;
  z-index: 1;
  animation: rise .22s ease;
}
.modal-h {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}
.modal-h h2 { margin: 0; letter-spacing: -0.03em; }
.modal-h p { margin: 4px 0 0; }
.modal-section { margin-bottom: 16px; }
.choice-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.choice-grid.levels { grid-template-columns: 1fr; }
.choice-card {
  text-align: left;
  border-radius: 14px;
  border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.03);
  color: var(--text);
  padding: 12px 14px;
  cursor: pointer;
  font: inherit;
  transition: border-color .15s, background .15s, transform .15s;
}
.choice-card:hover { background: rgba(255,255,255,0.06); transform: translateY(-1px); }
.choice-card.active {
  border-color: rgba(94, 234, 212, 0.45);
  background: linear-gradient(135deg, rgba(45,212,191,0.14), rgba(99,102,241,0.16));
}
.choice-card .ct { font-weight: 700; }
.choice-card .cd { color: var(--muted); font-size: 0.8rem; margin-top: 3px; }
.glass-soft {
  border-radius: 14px;
  border: 1px solid var(--glass-border);
  background: rgba(0,0,0,0.22);
  padding: 12px 14px;
  margin-bottom: 16px;
}
.selection-preview {
  margin-top: 4px;
  font-family: var(--mono);
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--accent);
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
@media (max-width: 640px) {
  .choice-grid { grid-template-columns: 1fr; }
}

.panel { flex: 1; min-height: 0; overflow: auto; }
.dash-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 14px;
}
.card {
  border-radius: var(--radius);
  padding: 18px;
}
.card h3 { margin: 0; font-size: 1rem; }
.card-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.span-2 { grid-column: span 2; }
.hero-card {
  background:
    radial-gradient(circle at 90% 10%, rgba(94,234,212,0.18), transparent 40%),
    radial-gradient(circle at 10% 90%, rgba(129,140,248,0.18), transparent 42%),
    var(--glass);
}
.hero-row { display: flex; gap: 16px; align-items: center; }
.avatar {
  width: 64px; height: 64px; border-radius: 20px;
  display: grid; place-items: center;
  font-weight: 700; font-size: 1.4rem;
  background: linear-gradient(135deg, var(--accent3), var(--accent2));
  color: white;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.chip {
  font-size: 0.75rem; font-weight: 600;
  padding: 5px 10px; border-radius: 999px;
  background: rgba(255,255,255,0.06); border: 1px solid var(--glass-border);
}
.chip.accent {
  background: rgba(94,234,212,0.15);
  border-color: rgba(94,234,212,0.3);
  color: var(--accent);
}
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 18px; }
.stat {
  background: rgba(0,0,0,0.2); border-radius: 14px; padding: 12px;
  border: 1px solid var(--glass-border);
}
.stat-n { display: block; font-size: 1.4rem; font-weight: 700; }
.stat-l { color: var(--muted); font-size: 0.78rem; }

.accounts-list, .recent-chats, .models-grid { display: flex; flex-direction: column; gap: 8px; }
.account-row, .recent-row, .model-pill {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 12px;
  background: rgba(255,255,255,0.03); border: 1px solid var(--glass-border);
}
.account-row button, .recent-row { cursor: pointer; }
.models-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px; max-height: 280px; overflow: auto;
}
.model-pill { font-size: 0.82rem; font-family: var(--mono); }
.model-pill.active { border-color: rgba(94,234,212,0.4); background: rgba(94,234,212,0.08); }
.instr-preview { font-size: 0.9rem; line-height: 1.45; white-space: pre-wrap; }

.chat-shell {
  height: 100%;
  border-radius: var(--radius);
  display: grid;
  grid-template-rows: auto 1fr auto auto auto auto auto;
  min-height: 0;
  overflow: hidden;
}
.chat-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--glass-border);
}
.chat-title { font-weight: 700; }
.chat-actions { display: flex; gap: 6px; }
.messages {
  overflow: auto; padding: 20px 18px 10px;
  display: flex; flex-direction: column; gap: 14px;
}
.msg {
  max-width: min(820px, 92%);
  padding: 14px 16px;
  border-radius: 18px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  animation: rise .25s ease;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, rgba(99,102,241,0.35), rgba(45,212,191,0.22));
  border: 1px solid rgba(129,140,248,0.25);
}
.msg.assistant {
  align-self: flex-start;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--glass-border);
}
.msg .meta {
  font-size: 0.72rem; color: var(--muted); margin-bottom: 6px; font-family: var(--mono);
}
.composer-meta {
  display: grid;
  grid-template-columns: 1.2fr 1.2fr auto;
  gap: 10px;
  padding: 10px 14px 0;
  border-top: 1px solid var(--glass-border);
  align-items: end;
}
.composer-pick label {
  margin: 0 0 6px;
  font-size: 0.75rem;
}
.composer-select {
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 0.9rem;
}
.composer {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px;
  padding: 10px 14px 8px;
  align-items: end;
}
.attach-btn {
  width: 48px; height: 48px; border-radius: 16px;
  display: grid; place-items: center;
  border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.05);
  cursor: pointer; font-size: 1.3rem; font-weight: 700;
}
.attach-btn:hover { background: rgba(94,234,212,0.12); border-color: rgba(94,234,212,0.35); }
.attach-list {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 0 14px;
}
.attach-chip {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 12px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--glass-border);
  font-size: 0.82rem; max-width: 100%;
}
.attach-chip img {
  width: 36px; height: 36px; object-fit: cover; border-radius: 8px;
}
.attach-chip button {
  border: none; background: transparent; color: var(--danger); cursor: pointer; font-size: 1rem;
}
.tools-bar {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 8px 14px 0;
}
.tool-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: rgba(255,255,255,0.04);
  font-size: 0.82rem; cursor: pointer;
}
.tool-chip input { width: auto; }
.muted-chip { opacity: 0.75; cursor: default; }

.msg.thinking {
  align-self: flex-start;
  max-width: min(820px, 92%);
  border: 1px dashed rgba(129,140,248,0.45);
  background: linear-gradient(135deg, rgba(99,102,241,0.12), rgba(15,23,42,0.5));
  border-radius: 18px;
  padding: 12px 14px;
}
.msg.thinking summary {
  cursor: pointer; font-weight: 700; color: #c7d2fe;
}
.msg.thinking pre {
  white-space: pre-wrap; font-family: var(--mono); font-size: 0.8rem;
  color: #dbe4ff; margin: 10px 0 0; max-height: 260px; overflow: auto;
}
.msg.tool {
  align-self: flex-start;
  max-width: min(820px, 92%);
  border: 1px solid rgba(52,211,153,0.35);
  background: rgba(16,185,129,0.08);
  border-radius: 16px; padding: 12px 14px;
}
.msg.tool .meta { color: #6ee7b7; }
.msg .attach-inline {
  display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px;
}
.msg .attach-inline img {
  max-width: 180px; max-height: 140px; border-radius: 12px; border: 1px solid var(--glass-border);
}

@media (max-width: 720px) {
  .composer-meta { grid-template-columns: 1fr; }
  .composer { grid-template-columns: auto 1fr auto; }
}
.composer textarea {
  max-height: 180px; border-radius: 16px; padding: 14px;
}
.send { height: 48px; min-width: 96px; border-radius: 16px; }
.chat-status {
  min-height: 22px; padding: 0 16px 10px;
  color: var(--muted); font-size: 0.8rem;
}

.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-stack { display: flex; flex-direction: column; gap: 10px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.check input { width: auto; }
.dump {
  background: rgba(0,0,0,0.3); border-radius: 12px; padding: 12px;
  max-height: 320px; overflow: auto; font-family: var(--mono); font-size: 0.75rem;
  border: 1px solid var(--glass-border); white-space: pre-wrap;
}

.toast {
  position: fixed; z-index: 50; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: rgba(10,14,24,0.92); border: 1px solid var(--glass-border);
  color: var(--text); padding: 12px 18px; border-radius: 999px;
  box-shadow: var(--shadow); font-weight: 600;
}
.toast.ok { border-color: rgba(52,211,153,0.4); }
.toast.err { border-color: rgba(251,113,133,0.45); }

@media (max-width: 980px) {
  .app { grid-template-columns: 1fr; padding: 8px; gap: 8px; }
  .sidebar {
    position: fixed; z-index: 20; inset: 8px auto 8px 8px; width: min(300px, 88vw);
    transform: translateX(-120%); transition: transform .25s ease;
  }
  .sidebar.open { transform: none; }
  .dash-grid, .settings-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .models-grid { grid-template-columns: 1fr; }
}
