:root {
  --bg: #ffffff;
  --bg-subtle: #f6f6f6;
  --bg-hover: #eeeeee;
  --fg: #111111;
  --fg-muted: #555555;
  --border: #dcdcdc;
  --border-strong: #111111;
  --accent: #000000;
  --accent-fg: #ffffff;
  --danger: #b00020;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
}

body {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100dvh;
  overflow: hidden;
}

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

button {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  padding: 0.35rem 0.7rem;
  cursor: pointer;
  border-radius: 3px;
}

button:hover {
  background: var(--bg-hover);
}

button[type="submit"] {
  background: var(--accent);
  color: var(--accent-fg);
}

button[type="submit"]:hover {
  opacity: 0.88;
}

button.danger-btn {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--bg);
}

button.danger-btn:hover {
  background: var(--danger);
  color: #ffffff;
}

input[type="text"],
input[type="password"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg);
}

input:focus, select:focus, textarea:focus {
  outline: 1px solid var(--border-strong);
  border-color: var(--border-strong);
}

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

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

#sidebar-header h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

#sidebar-header menu {
  display: flex;
  gap: 0.35rem;
  list-style: none;
  margin: 0;
  padding: 0;
}

#close-sidebar-btn,
#open-sidebar-btn {
  display: none;
}

#conversation-nav {
  flex: 1;
  overflow-y: auto;
  border-bottom: 1px solid var(--border);
}

#conversation-list {
  list-style: none;
  margin: 0;
  padding: 0.35rem;
}

#conversation-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.25rem;
  border-radius: 3px;
}

#conversation-list li.active-conv {
  background: var(--bg);
  outline: 1px solid var(--border-strong);
  font-weight: 600;
}

#conversation-list a {
  flex: 1;
  padding: 0.45rem 0.5rem;
  color: inherit;
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#conversation-list button.del-conv-btn {
  border: none;
  background: transparent;
  color: var(--fg-muted);
  padding: 0.25rem 0.45rem;
  font-size: 0.85rem;
}

#conversation-list button.del-conv-btn:hover {
  color: var(--danger);
}

#context-controls {
  padding: 0.65rem 0.75rem;
  overflow-y: auto;
  max-height: 46%;
  font-size: 0.85rem;
}

#conv-settings-form {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

#conv-settings-form fieldset {
  border: 1px solid var(--border);
  padding: 0.4rem 0.5rem;
  margin: 0;
  border-radius: 3px;
}

#conv-settings-form fieldset.inline-fields {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  border: none;
  padding: 0;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
}

.checklist {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

#active-project-badge {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  padding: 0.15rem 0.35rem;
  background: var(--bg);
  border: 1px solid var(--border);
}

/* Main Chat Pane */
#chat-pane {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

#chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border);
}

#chat-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  flex: 1;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#chat-stats {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--fg-muted);
}

#error-banner {
  display: block;
  background: #fff0f0;
  color: var(--danger);
  border-bottom: 1px solid var(--danger);
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
}

#error-banner[hidden] {
  display: none;
}

#messages-stream {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#history-controls {
  text-align: center;
}

#older-messages-list,
#active-messages-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#older-messages-list article.message {
  opacity: 0.65;
}

#context-divider {
  border: none;
  border-top: 1px dashed var(--fg-muted);
  margin: 0.75rem 0;
  position: relative;
  text-align: center;
  overflow: visible;
}

#context-divider::after {
  content: attr(data-label);
  background: var(--bg);
  color: var(--fg-muted);
  padding: 0 0.6rem;
  font-size: 0.75rem;
  font-family: var(--font-mono);
  position: relative;
  top: -0.65rem;
}

article.message {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.75rem 1rem;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  background: var(--bg);
}

article.message[data-role="user"] {
  background: var(--bg-subtle);
}

article.message > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  font-size: 0.8rem;
  color: var(--fg-muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.35rem;
}

article.message > header strong {
  color: var(--fg);
  text-transform: capitalize;
}

button.msg-ref-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  padding: 0.1rem 0.35rem;
}

article.message > header menu {
  display: flex;
  gap: 0.35rem;
  list-style: none;
  margin: 0;
  padding: 0;
}

article.message > header menu button {
  padding: 0.1rem 0.4rem;
  font-size: 0.75rem;
  border-color: var(--border);
}

/* Thoughts & Tool Calls */
details.thoughts,
details.tool-call {
  margin: 0.4rem 0;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg-subtle);
  font-size: 0.85rem;
}

details.thoughts > summary,
details.tool-call > summary {
  padding: 0.35rem 0.6rem;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--fg-muted);
  user-select: none;
}

details.thoughts > pre,
details.tool-call pre {
  margin: 0;
  padding: 0.5rem 0.6rem;
  border-top: 1px solid var(--border);
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  background: var(--bg);
  max-height: 360px;
  overflow-y: auto;
}

.tool-call-controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.35rem 0.6rem;
  border-top: 1px solid var(--border);
  font-size: 0.78rem;
}

/* Message Content & Markdown */
.message-body {
  word-break: break-word;
}

.message-body p {
  margin: 0.5rem 0;
}

.message-body p:first-child {
  margin-top: 0;
}

.message-body p:last-child {
  margin-bottom: 0;
}

.message-body pre {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.65rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  position: relative;
}

.message-body code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-subtle);
  padding: 0.1rem 0.25rem;
  border-radius: 2px;
}

.message-body pre code {
  background: transparent;
  padding: 0;
}

.copy-code-btn {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  font-size: 0.72rem;
  padding: 0.15rem 0.4rem;
  border-color: var(--border);
}

.message-body blockquote {
  margin: 0.5rem 0;
  padding-left: 0.75rem;
  border-left: 3px solid var(--border-strong);
  color: var(--fg-muted);
}

.message-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5rem 0;
  font-size: 0.9rem;
}

.message-body th,
.message-body td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  text-align: left;
}

/* Attachments */
footer.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.65rem;
}

figure.attachment-card {
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.5rem;
  background: var(--bg-subtle);
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

figure.attachment-card img {
  max-width: 100%;
  max-height: 220px;
  object-fit: contain;
  cursor: zoom-in;
  border: 1px solid var(--border);
  background: var(--bg);
}

figure.attachment-card audio {
  width: 260px;
  max-width: 100%;
}

figure.attachment-card figcaption {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.78rem;
}

/* Composer */
#composer {
  border-top: 1px solid var(--border);
  padding: 0.65rem 1rem;
  background: var(--bg);
}

#composer-attachments {
  list-style: none;
  margin: 0 0 0.4rem 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

#composer-attachments li {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.5rem;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  font-size: 0.8rem;
  border-radius: 3px;
}

#composer-row {
  border: none;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

#file-upload-label {
  border: 1px solid var(--border-strong);
  padding: 0.45rem 0.65rem;
  border-radius: 3px;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

#file-upload-label:hover {
  background: var(--bg-hover);
}

#composer-input {
  flex: 1;
  resize: vertical;
  min-height: 2.6rem;
  max-height: 14rem;
}

#composer-submit {
  padding: 0.45rem 1rem;
}

/* Dialogs */
dialog {
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  padding: 1rem;
  max-width: 760px;
  width: 94vw;
  max-height: 90dvh;
  background: var(--bg);
  color: var(--fg);
}

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

dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
  margin-bottom: 0.75rem;
}

dialog header h2,
dialog header h3 {
  margin: 0;
}

dialog form {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

dialog footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

#image-dialog {
  max-width: 92vw;
}

#image-dialog figure {
  margin: 0;
  text-align: center;
}

#image-dialog img {
  max-width: 100%;
  max-height: 78dvh;
  object-fit: contain;
}

#settings-tabs {
  display: flex;
  gap: 0.35rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

#settings-tabs button.active-tab {
  background: var(--accent);
  color: var(--accent-fg);
}

.mono-textarea {
  font-family: var(--font-mono);
  font-size: 0.82rem;
}

#models-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin-top: 0.5rem;
}

#models-table th,
#models-table td {
  border: 1px solid var(--border);
  padding: 0.3rem 0.4rem;
  text-align: left;
}

#models-table input[type="number"] {
  width: 5rem;
  padding: 0.2rem;
}

@media (max-width: 820px) {
  body {
    grid-template-columns: 1fr;
  }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 290px;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.18s ease-out;
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.2);
  }

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

  #close-sidebar-btn,
  #open-sidebar-btn {
    display: inline-block;
  }
}
