:root {
  --bg: #12141a;
  --bg2: #1a1d26;
  --bg3: #232735;
  --border: #2e3344;
  --text: #e7e9ee;
  --text-dim: #9aa1b2;
  --accent: #6c8cff;
  --accent-dim: #3d4f96;
  --danger: #e5636c;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, "Segoe UI", Roboto, sans-serif;
}

#app { display: flex; height: 100dvh; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px;
}

.logo { font-weight: 700; font-size: 18px; letter-spacing: .5px; }

.sidebar-section { padding: 4px 8px; }

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px 2px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--text-dim);
}

.sidebar-footer { margin-top: auto; padding: 10px; }

.chat-item, .project-item {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  overflow: hidden;
}

.chat-item:hover, .project-item:hover { background: var(--bg3); }
.chat-item.active { background: var(--accent-dim); }

.chat-item .title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item.archived { opacity: .55; }

.project-item { font-weight: 600; }
.project-item .edit { opacity: 0; font-size: 13px; }
.project-item:hover .edit { opacity: .7; }

.project-chats { margin-left: 14px; border-left: 1px solid var(--border); padding-left: 4px; }

/* ---------- Main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
}

#chat-title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.header-actions { display: flex; gap: 6px; }

#model-select {
  max-width: 220px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 13px;
}

/* ---------- Messages ---------- */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg { max-width: 820px; width: fit-content; }
.msg.user { align-self: flex-end; }
.msg.assistant { align-self: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg.user .bubble { background: var(--accent-dim); }
.msg.assistant .bubble { background: var(--bg3); }
.msg.error .bubble { background: #4a2328; color: #ffb4ba; }

.bubble p:first-child { margin-top: 0; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre {
  background: #0d0f14;
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}
.bubble code { background: #0d0f14; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.bubble pre code { padding: 0; background: none; }
.bubble table { border-collapse: collapse; }
.bubble td, .bubble th { border: 1px solid var(--border); padding: 4px 8px; }

.msg .meta { font-size: 11px; color: var(--text-dim); margin-top: 3px; padding: 0 4px; }

.typing { color: var(--text-dim); font-style: italic; }

/* ---------- Composer ---------- */
#composer {
  display: flex;
  gap: 8px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg2);
}

#input {
  flex: 1;
  resize: none;
  max-height: 180px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font: inherit;
  outline: none;
}

#input:focus { border-color: var(--accent); }

/* ---------- Buttons ---------- */
button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  cursor: pointer;
}

button:hover { filter: brightness(1.1); }
button:disabled { opacity: .5; cursor: default; }

.icon-btn {
  background: transparent;
  color: var(--text-dim);
  padding: 4px 8px;
  font-size: 15px;
}

.icon-btn:hover { color: var(--text); background: var(--bg3); }

.ghost { background: var(--bg3); color: var(--text-dim); }
.danger { background: var(--danger); }

#btn-send { min-width: 52px; font-size: 17px; }
#btn-menu { display: none; }

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal.hidden, .hidden { display: none; }

.modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  width: min(480px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-box h3 { margin: 0; }

.modal-box label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-dim); }

.modal-box input, .modal-box textarea {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font: inherit;
}

.modal-actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  #sidebar {
    position: fixed;
    z-index: 40;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform .2s;
    box-shadow: 4px 0 24px rgba(0,0,0,.4);
  }
  #sidebar.open { transform: none; }
  #btn-menu { display: block; }
  #model-select { max-width: 130px; }
}
