:root,
:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #161922;
  --panel-2: #1c2030;
  --border: #262b3a;
  --text: #e6e8ee;
  --muted: #8b93a7;
  --accent: #6c9bff;
  --accent-2: #4f7fdc;
  --user: #1f3157;
  --assistant: #1f2a1f;
  --danger: #d65a5a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --danger-bg: #2a1818;
  --danger-border: #5a2a2a;
  --danger-text: #ffb3b3;
  --hover: #242a3d;
}

:root[data-theme="light"] {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #eef0f4;
  --border: #d8dbe3;
  --text: #1a1d24;
  --muted: #6b7280;
  --accent: #2f6cff;
  --accent-2: #2f6cff;
  --user: #dde7ff;
  --assistant: #ffffff;
  --danger: #c93a3a;
  --shadow: 0 1px 2px rgba(15, 17, 21, 0.06);
  --danger-bg: #fdecec;
  --danger-border: #f0c0c0;
  --danger-text: #a02323;
  --hover: #e6e9ef;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 100vh;
  height: 100vh;
  overflow: hidden;
}

button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.danger { color: var(--danger); border-color: var(--danger-border); }
button.danger:hover:not(:disabled) { background: var(--danger-bg); }

input, textarea, select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }

.muted { color: var(--muted); font-size: 12px; }

/* Sidebar */
#sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.sidebar-header {
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
}
.sidebar-header h1 { font-size: 14px; margin: 0; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.sidebar-header button { font-size: 16px; width: 30px; padding: 0; line-height: 28px; }

#project-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}
#project-list li {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#project-list li:hover { background: var(--panel-2); }
#project-list li.active { background: var(--accent-2); color: white; }
#project-list li.active .slug { color: rgba(255,255,255,0.75); }
#project-list li .slug { font-size: 11px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.sidebar-footer {
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
#theme-toggle {
  font-size: 14px;
  padding: 4px 8px;
  line-height: 1;
}
.footer-buttons { display: flex; gap: 6px; }
#user-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
#login-dialog { max-width: 360px; }
#login-dialog input { width: 100%; margin-top: 4px; }
#login-dialog::backdrop { background: rgba(0,0,0,0.7); }

/* Standalone login page */
body.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  grid-template-columns: none;
  grid-template-rows: none;
}
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px 26px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
}
.auth-card h1 {
  margin: 0 0 4px 0;
  font-size: 20px;
  font-weight: 600;
}
.auth-card .muted { margin: 0 0 18px 0; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-card label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
.auth-card input { width: 100%; }
.auth-card button[type="submit"] {
  background: var(--accent-2);
  color: white;
  border-color: var(--accent-2);
  padding: 9px 14px;
  font-weight: 500;
  margin-top: 4px;
}
.auth-card button[type="submit"]:hover:not(:disabled) { background: var(--accent); border-color: var(--accent); }
.auth-error {
  color: var(--danger);
  font-size: 13px;
  margin: 0;
  min-height: 18px;
}

/* Main */
#main {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
#chat-header {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.header-left h2 { margin: 0; font-size: 16px; font-weight: 600; }
.header-left { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.header-right { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#model-select { min-width: 240px; }

#system-prompt-row {
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--panel);
}
#system-prompt { width: 100%; }

/* Messages */
#messages {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}
.msg {
  max-width: 880px;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
  font-size: 14px;
}
.msg.user { background: var(--user); align-self: flex-end; }
.msg.assistant { background: var(--panel); }
.msg .role {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
}
.msg.error { background: var(--danger-bg); border-color: var(--danger-border); color: var(--danger-text); }

/* Composer */
#composer {
  border-top: 1px solid var(--border);
  padding: 12px 18px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  background: var(--panel);
}
#input { resize: vertical; min-height: 60px; max-height: 320px; }
#send { padding: 8px 18px; background: var(--accent-2); color: white; border-color: var(--accent-2); }
#send:hover:not(:disabled) { background: var(--accent); }

/* Dialogs */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  max-width: 460px;
  width: 90%;
}
dialog::backdrop { background: rgba(0,0,0,0.55); }
dialog form, .prompt-md-wrap { padding: 18px; }
dialog h3 { margin: 0 0 12px 0; }
dialog label { display: block; font-size: 13px; color: var(--muted); }
dialog input { width: 100%; margin-top: 4px; }
dialog menu { display: flex; gap: 8px; justify-content: flex-end; padding: 0; margin: 14px 0 0 0; }
dialog code { background: var(--panel-2); padding: 1px 6px; border-radius: 4px; font-size: 12px; }

#prompt-md-dialog { max-width: 800px; }
.prompt-md-wrap { display: flex; flex-direction: column; gap: 10px; max-height: 80vh; }
.prompt-md-wrap header { display: flex; justify-content: space-between; align-items: center; padding: 0; }
#prompt-md-content {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  overflow: auto;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  margin: 0;
  flex: 1;
}
