:root {
  --bg: #0f1115;
  --bg-elev: #161922;
  --bg-elev-2: #1d2130;
  --border: #262b3b;
  --text: #e8eaf2;
  --text-muted: #8a90a3;
  --accent: #c97a4e;
  --accent-hover: #d68a5e;
  --user-bubble: #1f2433;
  --assistant-bubble: transparent;
  --danger: #d65656;
  --radius: 8px;
  --radius-sm: 6px;
  --sidebar-w: 260px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f7f5;
    --bg-elev: #ffffff;
    --bg-elev-2: #efefea;
    --border: #e2e2dd;
    --text: #1a1a1a;
    --text-muted: #6b6b6b;
    --user-bubble: #efefea;
  }
}

* { box-sizing: border-box; }

/* The HTML [hidden] attribute should always win over component display
   rules. Without this, a `display: flex` (or grid, etc.) on a component
   class silently overrides `<element hidden>` and the element renders
   anyway — which is how the sign-in / setup-error overlays both showed
   even when the JS marked them hidden. */
[hidden] { display: none !important; }

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

body {
  min-height: 100vh;
}

#app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

/* Sign-in & setup-error screens */
.signin-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
  padding: 24px;
}

.signin-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  max-width: 420px;
  width: 100%;
  text-align: center;
}

.signin-card h1 {
  margin: 0 0 8px;
  font-size: 22px;
  font-weight: 600;
}

.signin-card .muted {
  font-size: 14px;
  margin: 8px 0 20px;
}

.signin-form {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.signin-form input[type="email"] {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font: inherit;
}

.signin-form input[type="email"]:focus {
  outline: none;
  border-color: var(--accent);
}

.signin-msg {
  font-size: 13px;
  margin: 8px 0;
  min-height: 1em;
}

.signin-msg.success { color: var(--accent); }
.signin-msg.error { color: var(--danger); }

/* Sidebar */
#sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--text-muted);
}

#project-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.project-item {
  margin-bottom: 4px;
}

.project-head {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  background: transparent;
  border: none;
  text-align: left;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  color: var(--text);
}

.project-head:hover { background: var(--bg-elev-2); }
.project-item.active > .project-head { background: var(--bg-elev-2); font-weight: 500; }

.project-head .caret {
  font-size: 10px;
  color: var(--text-muted);
  width: 12px;
  flex-shrink: 0;
}

.project-head .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-list {
  margin: 4px 0 4px 18px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conv-new,
.conv-item {
  background: transparent;
  border: none;
  text-align: left;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-new { color: var(--accent); font-weight: 500; }
.conv-new:hover { background: var(--bg-elev-2); }

.conv-item:hover { background: var(--bg-elev-2); color: var(--text); }
.conv-item.active { background: var(--bg-elev-2); color: var(--text); font-weight: 500; }

/* Rename buttons that show on hover next to sidebar items */
.project-head-row,
.conv-row {
  display: flex;
  align-items: center;
  position: relative;
}

.project-head-row > .project-head,
.conv-row > .conv-item,
.conv-row > .conv-new {
  flex: 1;
}

.row-action {
  background: transparent;
  border: none;
  padding: 4px 6px;
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
  border-radius: 4px;
}

.project-head-row:hover .row-action,
.conv-row:hover .row-action {
  opacity: 0.7;
}

.row-action:hover { opacity: 1 !important; background: var(--bg-elev-2); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px;
}

.sidebar-footer a {
  color: var(--text-muted);
  text-decoration: none;
}

.sidebar-footer a:hover { color: var(--text); }

.sidebar-footer .sep { color: var(--border); margin: 0 4px; }

.sidebar-footer .user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  gap: 8px;
}

.sidebar-footer .user-row #user-email {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

button.ghost.small {
  padding: 4px 8px;
  font-size: 12px;
}

/* Main pane */
#main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100vh;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
}

#project-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.project-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.project-name {
  flex: 1;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 16px;
  font-weight: 500;
  color: var(--text);
  min-width: 0;
}

.project-name:hover { border-style: solid; }
.project-name:focus { border-color: var(--accent); border-style: solid; outline: none; }

/* Conversation sub-header */
.conv-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}

.conv-name {
  flex: 1;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  min-width: 0;
}

.conv-name:hover { border-style: solid; color: var(--text); }
.conv-name:focus { border-color: var(--accent); border-style: solid; outline: none; color: var(--text); }

.conv-usage {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.conv-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.export-wrap { position: relative; }

.export-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px;
  min-width: 160px;
  z-index: 5;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.export-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 6px 10px;
  font-size: 13px;
  border-radius: 4px;
  cursor: pointer;
}

.export-menu button:hover { background: var(--bg-elev-2); }

.project-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Conversation */
.conversation {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

.empty-conv {
  text-align: center;
  color: var(--text-muted);
  margin-top: 40px;
  font-size: 14px;
}

.msg-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.msg-head .role {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.msg-head .usage {
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.msg-actions {
  display: flex;
  gap: 4px;
  margin-top: 6px;
  opacity: 0;
  transition: opacity 0.15s;
}

.message:hover .msg-actions { opacity: 1; }

.msg-action {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text-muted);
}

.msg-action:hover { background: var(--bg-elev-2); color: var(--text); }

.thinking {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  margin-bottom: 8px;
  font-size: 13px;
}

.thinking summary {
  cursor: pointer;
  color: var(--text-muted);
  user-select: none;
}

.thinking[open] summary { margin-bottom: 6px; }

.thinking-content {
  white-space: pre-wrap;
  color: var(--text-muted);
  font-size: 13px;
  border-top: 1px solid var(--border);
  padding-top: 6px;
}

.message .body {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message.user .body {
  background: var(--user-bubble);
  padding: 12px 14px;
  border-radius: var(--radius);
}

.message.assistant .body { padding: 0; }

.message .files {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.message .file-chip {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  color: var(--text-muted);
}

.tool-event {
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

/* Files bar (above composer) */
.files-bar {
  padding: 8px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 13px;
  background: var(--bg-elev);
}

.files-bar-label { color: var(--text-muted); }

.files-bar ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.files-bar li {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.files-bar li button {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  font-size: 14px;
  line-height: 1;
}

/* Composer */
.composer {
  display: flex;
  align-items: end;
  gap: 8px;
  padding: 12px 20px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.composer textarea {
  flex: 1;
  resize: none;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font: inherit;
  max-height: 240px;
  min-height: 40px;
  line-height: 1.4;
}

.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Buttons */
button, select {
  font: inherit;
  color: var(--text);
  cursor: pointer;
}

button.primary {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: white;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-weight: 500;
}

button.primary:hover { background: var(--accent-hover); }
button.primary:disabled { opacity: 0.5; cursor: not-allowed; }

button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

button.ghost:hover { background: var(--bg-elev-2); }

button.danger { color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: rgba(214, 86, 86, 0.1); }

button.icon {
  background: none;
  border: none;
  font-size: 18px;
  padding: 4px;
  color: var(--text-muted);
}

button.icon:hover { color: var(--text); }

select {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 13px;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.toggle input { margin: 0; }

.toggle:has(input:disabled) {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Settings dialog */
dialog {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  max-width: 560px;
  width: 90%;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

.settings-form {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.settings-form h3 {
  margin: 0 0 4px;
  font-size: 16px;
}

.settings-form h4 {
  margin: 12px 0 0;
  font-size: 14px;
}

.settings-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--text-muted);
}

.settings-form textarea, .settings-form input[type="text"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  color: var(--text);
  font: inherit;
  resize: vertical;
}

.settings-form menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 8px 0 0;
  padding: 0;
}

#file-library {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

#file-library li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

#file-library li:last-child { border-bottom: none; }

#file-library .file-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#file-library .file-meta {
  color: var(--text-muted);
  font-size: 12px;
  margin-right: 8px;
}

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

.error {
  color: var(--danger);
  background: rgba(214, 86, 86, 0.1);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 50;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

.toast.error { color: var(--danger); }

/* Mobile */
@media (max-width: 720px) {
  body { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 80%;
    max-width: 280px;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
}
