:root {
  --bg: #0F1115;
  --surface: #171A21;
  --surface-2: #1E222B;
  --border: #262B36;
  --text: #E7E9EE;
  --text-dim: #8B93A7;
  --text-faint: #5B6274;
  --accent: #4FD1C5;
  --accent-dim: #2B6E68;
  --warn: #F2A65A;
  --danger: #E8695A;
  --radius: 14px;
  --font-ui: 'Manrope', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select {
  font-family: inherit;
  color: inherit;
}

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */

.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 14px;
  position: fixed;
  inset: 0 auto 0 0;
  transform: translateX(-100%);
  transition: transform .22s ease;
  z-index: 30;
}

.app.sidebar-open .sidebar { transform: translateX(0); }

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
  z-index: 20;
}

.app.sidebar-open .scrim { opacity: 1; pointer-events: auto; }

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--accent);
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.new-chat-btn span { color: var(--accent); font-size: 16px; }
.new-chat-btn:active { background: var(--border); }

.history-label {
  font-size: 12px;
  color: var(--text-faint);
  margin: 18px 4px 6px;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-item {
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13.5px;
  color: var(--text-dim);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 1px solid transparent;
}
.history-item:hover { background: var(--surface-2); }
.history-item.active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.sidebar-bottom {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.ghost-btn {
  background: none;
  border: none;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text-dim);
  font-size: 13.5px;
  cursor: pointer;
}
.ghost-btn:hover { background: var(--surface-2); color: var(--text); }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Main ---------- */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.model-select {
  flex: 1;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 10px;
  font-size: 13.5px;
  font-weight: 600;
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 20px;
  white-space: nowrap;
}
.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
}
.status-badge.connected .dot { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.status-badge.error .dot { background: var(--danger); }

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 18px 14px 6px;
}

.empty-state {
  max-width: 480px;
  margin: 8vh auto 0;
  text-align: center;
  color: var(--text-dim);
}
.empty-mark {
  color: var(--accent);
  display: inline-flex;
  margin-bottom: 14px;
}
.empty-state h1 {
  font-size: 21px;
  color: var(--text);
  margin: 0 0 8px;
  letter-spacing: -.01em;
}
.empty-state p {
  font-size: 14px;
  line-height: 1.55;
  margin: 0 0 20px;
}

.quick-prompts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.quick-prompts button {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 10px;
  border-radius: 10px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}
.quick-prompts button:hover { border-color: var(--accent-dim); color: var(--text); }

.messages {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding-bottom: 16px;
}

.msg {
  display: flex;
  gap: 10px;
  font-size: 14.5px;
  line-height: 1.6;
}
.msg-avatar {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  margin-top: 2px;
}
.msg.user .msg-avatar { background: var(--surface-2); color: var(--text-dim); }
.msg.assistant .msg-avatar { background: rgba(79,209,197,.12); color: var(--accent); }

.msg-body { min-width: 0; flex: 1; }

/* Pesan user: rata kanan, bubble. Pesan AI: rata kiri, polos */
.msg.user {
  flex-direction: row-reverse;
  justify-content: flex-start;
}
.msg.user .msg-body {
  flex: 0 1 auto;
  max-width: 78%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px 14px 4px 14px;
  padding: 9px 13px;
}
.msg.assistant .msg-body {
  max-width: 100%;
}
.msg-body p { margin: 0 0 10px; }
.msg-body p:last-child { margin-bottom: 0; }

.msg-body pre {
  background: #0B0D11 !important;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
  position: relative;
}
.msg-body code {
  font-family: var(--font-mono);
}
.msg-body :not(pre) > code {
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 13px;
}

.code-block-wrap { position: relative; }
.code-toolbar {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-bottom: -6px;
}
.code-toolbar button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11.5px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font-ui);
}
.code-toolbar button:hover { color: var(--accent); border-color: var(--accent-dim); }

.error-note {
  background: rgba(232,105,90,.08);
  border: 1px solid rgba(232,105,90,.35);
  color: #F3A79C;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13.5px;
}

.typing-dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-faint);
  animation: blink 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- Composer ---------- */

.composer {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.attachments-preview {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.attachments-preview:empty { display: none; }

.attachment-chip {
  position: relative;
  width: 56px;
  height: 56px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.attachment-chip img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attachment-chip .remove-chip {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,.65);
  color: #fff;
  border: none;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.file-chip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 56px;
  padding: 0 26px 0 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  flex-shrink: 0;
  max-width: 180px;
}
.file-chip .file-ext {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 9.5px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  text-transform: uppercase;
}
.file-chip .file-meta { min-width: 0; }
.file-chip .file-name {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-chip .file-size { font-size: 10.5px; color: var(--text-faint); }
.file-chip.processing .file-size { color: var(--accent); }

.msg-files {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.msg-images {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.msg-images img,
.chat-image {
  max-width: 220px;
  max-height: 220px;
  border-radius: 10px;
  border: 1px solid var(--border);
  display: block;
}
.msg.assistant .chat-image {
  max-width: min(320px, 100%);
  max-height: none;
  margin: 8px 0;
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 14px;
  font-size: 14.5px;
  line-height: 1.4;
}
.composer textarea:focus { outline: none; border-color: var(--accent-dim); }

.send-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--accent);
  color: #0B0D11;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:disabled { background: var(--surface-2); color: var(--text-faint); }

.attach-btn { flex-shrink: 0; }

/* ---------- Settings dialog ---------- */

.settings-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  width: min(360px, 90vw);
}
.settings-dialog::backdrop { background: rgba(0,0,0,.55); }

.settings-form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.settings-form h2 { margin: 0; font-size: 17px; }
.settings-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.settings-form input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 11px;
  font-size: 14px;
}
.hint { font-size: 12px; color: var(--text-faint); margin: 0; }

.settings-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.primary-btn {
  background: var(--accent);
  color: #0B0D11;
  border: none;
  padding: 9px 16px;
  border-radius: 9px;
  font-weight: 700;
  font-size: 13.5px;
  cursor: pointer;
}

/* ---------- Desktop ---------- */

@media (min-width: 860px) {
  .sidebar { position: relative; transform: none; }
  .scrim { display: none; }
  #openSidebar, #closeSidebar { display: none; }
  .app:not(.sidebar-open) .sidebar { display: none; }
}
