:root {
  --bg: #070a12;
  --panel: #0e1424;
  --panel-2: #121a2e;
  --border: rgba(255, 255, 255, 0.08);
  --text: #eef2ff;
  --muted: #93a0bd;
  --accent: #7c5cff;
  --accent-2: #4cc9f0;
  --danger: #ff5c7a;
  --user: #1a2440;
  --assistant: #121826;
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
  --radius: 18px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(900px 500px at 0% 0%, rgba(124, 92, 255, 0.18), transparent 55%),
    radial-gradient(700px 400px at 100% 0%, rgba(76, 201, 240, 0.12), transparent 50%),
    var(--bg);
}

button, input, textarea, select { font: inherit; color: inherit; }
textarea { resize: none; }

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  max-width: 1400px;
  margin: 0 auto;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
  border-right: 1px solid var(--border);
  background: rgba(8, 12, 22, 0.75);
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}
.brand p { margin: 0.1rem 0 0; color: var(--muted); font-size: 0.85rem; }
.logo {
  width: 42px; height: 42px; border-radius: 14px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #0b1020; font-weight: 900;
}

.sidebar-section h3 {
  margin: 0 0 0.5rem;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.chat-list { display: grid; gap: 0.4rem; max-height: calc(100vh - 260px); overflow: auto; }
.chat-item {
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 12px;
  padding: 0.7rem 0.75rem;
  cursor: pointer;
  color: var(--muted);
}
.chat-item:hover, .chat-item.active {
  background: var(--panel);
  border-color: var(--border);
  color: var(--text);
}
.chat-item small { display: block; opacity: 0.7; font-size: 0.75rem; margin-top: 0.2rem; }

.sidebar-foot { margin-top: auto; }

.main {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 1.05rem; }
.muted { color: var(--muted); }
.topbar .muted { margin: 0.15rem 0 0; font-size: 0.85rem; }

.messages {
  overflow: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.msg {
  max-width: min(780px, 100%);
  padding: 0.9rem 1rem;
  border-radius: 16px;
  border: 1px solid var(--border);
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: var(--shadow);
}
.msg.user {
  align-self: flex-end;
  background: linear-gradient(180deg, #222b48, var(--user));
}
.msg.assistant {
  align-self: flex-start;
  background: var(--assistant);
}
.msg .role {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.msg.error { border-color: rgba(255, 92, 122, 0.45); color: #ffd0d9; }

.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 28rem;
}
.empty h2 {
  margin: 0 0 0.5rem;
  color: var(--text);
  font-size: 1.5rem;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.6rem;
  padding: 0.75rem 1.25rem 0.25rem;
}
.composer textarea, .field input, .field textarea, .field select {
  width: 100%;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  padding: 0.85rem 0.95rem;
  outline: none;
}
.composer textarea:focus, .field input:focus, .field textarea:focus, .field select:focus {
  border-color: rgba(124, 92, 255, 0.55);
  box-shadow: 0 0 0 3px rgba(124, 92, 255, 0.15);
}

.fine { text-align: center; font-size: 0.78rem; margin: 0.35rem 0 0.9rem; }

.btn {
  border: 0;
  border-radius: 999px;
  padding: 0.75rem 1.1rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), #5b8cff);
  color: white;
}
.btn.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn.danger {
  background: rgba(255, 92, 122, 0.15);
  color: #ffb3c1;
  border: 1px solid rgba(255, 92, 122, 0.35);
}
.btn.full { width: 100%; }

.icon-btn {
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 10px;
  width: 38px; height: 38px;
  cursor: pointer;
}
#menu-btn { display: none; }

.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: grid; place-items: center;
  padding: 1rem;
  z-index: 50;
}
.modal.hidden { display: none; }
.modal-card {
  width: min(520px, 100%);
  background: #0d1322;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 1rem;
  box-shadow: var(--shadow);
}
.modal-head, .modal-actions {
  display: flex; align-items: center; justify-content: space-between; gap: 0.75rem;
}
.modal-head h2 { margin: 0; }
.modal-actions { margin-top: 1rem; }
.field { display: grid; gap: 0.35rem; margin-top: 0.85rem; }
.field span { color: var(--muted); font-size: 0.9rem; }
.hint { font-size: 0.82rem; margin: 0.85rem 0 0; }
code {
  background: rgba(255,255,255,0.06);
  padding: 0.1rem 0.35rem;
  border-radius: 6px;
}

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(84vw, 300px);
    z-index: 40;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  #menu-btn { display: inline-grid; place-items: center; }
}
