:root {
  --bg: #f7f7f8;
  --panel-bg: #ffffff;
  --border: #e0e0e5;
  --text: #1c1c22;
  --muted: #6b6b76;
  --accent: #3457d5;
  --accent-contrast: #ffffff;
  --danger: #c0392b;
  --badge-draft: #8a8a92;
  --badge-validated: #b58900;
  --badge-committed: #1f8a4c;
  --badge-archived: #6b6b76;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181c;
    --panel-bg: #1f2024;
    --border: #33343a;
    --text: #e8e8ec;
    --muted: #9a9aa4;
    --accent: #6a86ff;
    --accent-contrast: #10121a;
  }
}

:root[data-theme="dark"] {
  --bg: #17181c;
  --panel-bg: #1f2024;
  --border: #33343a;
  --text: #e8e8ec;
  --muted: #9a9aa4;
  --accent: #6a86ff;
  --accent-contrast: #10121a;
}

:root[data-theme="light"] {
  --bg: #f7f7f8;
  --panel-bg: #ffffff;
  --border: #e0e0e5;
  --text: #1c1c22;
  --muted: #6b6b76;
  --accent: #3457d5;
  --accent-contrast: #ffffff;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel-bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0.45rem 0.9rem;
  font-size: 0.9rem;
}

button:hover {
  border-color: var(--accent);
}

button[type="submit"], .btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

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

.btn-link {
  border: none;
  background: none;
  color: var(--muted);
  text-decoration: underline;
  padding: 0;
}

.btn-icon {
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  line-height: 1;
}

input, textarea {
  font-family: inherit;
  font-size: 0.9rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-bg);
  color: var(--text);
  width: 100%;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

.error-text {
  color: var(--danger);
  font-size: 0.85rem;
}

/* --- Login page --- */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.login-card {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 320px;
}

.login-card h1 {
  font-size: 1.3rem;
  margin: 0 0 0.25rem;
}

.login-card .subtitle {
  margin: 0 0 1.25rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.login-card label {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.5rem;
}

/* --- App shell --- */

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel-bg);
  flex-shrink: 0;
}

.topbar-brand {
  font-weight: 600;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.app-body {
  flex: 1;
  display: grid;
  grid-template-columns: 220px 320px 1fr 380px;
  overflow: hidden;
}

.rail {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 0.75rem;
}

.rail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.rail-header h2 {
  font-size: 0.95rem;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--muted);
}

.main-panel {
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.empty-state {
  color: var(--muted);
  font-size: 0.9rem;
  padding: 2rem 1rem;
  text-align: center;
}

/* --- Project list --- */

.project-list, .change-list, .knowledge-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.project-list li, .change-list li {
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.88rem;
  border: 1px solid transparent;
}

.project-list li:hover, .change-list li:hover {
  background: var(--bg);
}

.project-list li.selected, .change-list li.selected {
  border-color: var(--accent);
  background: var(--bg);
}

.inline-form {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin: 0.6rem 0;
}

.inline-form[hidden] {
  display: none;
}

.form-actions {
  display: flex;
  gap: 0.4rem;
}

/* --- Workspace / knowledge --- */

.workspace-header h2 {
  font-size: 1.1rem;
  margin: 0 0 0.2rem;
}

.workspace-status-badge {
  display: inline-block;
  font-size: 0.75rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  margin-bottom: 0.4rem;
}

.chat-placeholder {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  margin: 0.75rem 0;
}

.chat-placeholder h3, .new-change-section h3, .knowledge-section h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--muted);
  margin: 0 0 0.4rem;
}

.knowledge-list li {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem;
  font-size: 0.85rem;
}

.knowledge-list li .knowledge-title {
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}

.knowledge-list li .knowledge-content {
  color: var(--muted);
  margin-top: 0.25rem;
  white-space: pre-wrap;
}

/* --- Changes panel --- */

.status-badge {
  font-size: 0.7rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  color: white;
  text-transform: capitalize;
}

.status-draft { background: var(--badge-draft); }
.status-validated { background: var(--badge-validated); }
.status-committed { background: var(--badge-committed); }
.status-archived { background: var(--badge-archived); }

.change-detail {
  margin-top: 1rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

.change-detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.change-detail-header h3 {
  font-size: 1rem;
  margin: 0;
}

.tab-bar {
  display: flex;
  gap: 0.25rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

.tab-btn {
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.tab-btn.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.tab-panel {
  display: none;
  font-size: 0.85rem;
  max-height: 40vh;
  overflow-y: auto;
}

.tab-panel.active {
  display: block;
}

.tab-panel :is(h1, h2, h3) {
  font-size: 0.95rem;
}

.tab-panel pre {
  background: var(--bg);
  padding: 0.5rem;
  border-radius: var(--radius);
  overflow-x: auto;
}

.change-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.validate-result {
  margin-top: 0.5rem;
  padding: 0.5rem;
  border-radius: var(--radius);
  font-size: 0.82rem;
}

.validate-result.pass {
  background: color-mix(in srgb, var(--badge-committed) 15%, transparent);
  border: 1px solid var(--badge-committed);
}

.validate-result.fail {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  border: 1px solid var(--danger);
}

.history-entry {
  border-bottom: 1px solid var(--border);
  padding: 0.4rem 0;
  font-size: 0.8rem;
}

.history-entry .hash {
  font-family: ui-monospace, monospace;
  color: var(--muted);
}

/* --- Chat --- */

.chat-list-section, .new-change-section, .knowledge-section {
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
}

.main-panel-chat {
  display: block;
  padding: 0;
}

.chat-window {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.chat-window[hidden] {
  display: none;
}

.chat-window-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
}

.chat-window-header h3 {
  margin: 0;
  font-size: 0.95rem;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.chat-bubble {
  max-width: 75%;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.4;
}

.chat-bubble p {
  margin: 0.3rem 0;
}

.chat-bubble-user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-contrast);
}

.chat-bubble-assistant {
  align-self: flex-start;
  background: var(--panel-bg);
  border: 1px solid var(--border);
}

.chat-bubble-system {
  align-self: center;
  background: none;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: 0.8rem;
}

.artifact-card {
  align-self: stretch;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  font-size: 0.85rem;
}

.artifact-card-header {
  font-weight: 600;
  margin-bottom: 0.3rem;
}

.artifact-card pre {
  background: var(--bg);
  padding: 0.5rem;
  border-radius: var(--radius);
  max-height: 200px;
  overflow-y: auto;
}

.chat-input-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
}

.chat-input-form textarea {
  flex: 1;
  resize: vertical;
}

/* --- Pending draft banner (Changes panel tabs) --- */

.draft-banner {
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  font-size: 0.82rem;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.draft-banner-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.4rem;
}

/* --- Audit log modal --- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(900px, 92vw);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

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

.modal-header h2 {
  font-size: 1rem;
  margin: 0;
}

.modal-body {
  padding: 0.75rem 1rem;
  overflow-y: auto;
}

.audit-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.audit-table th, .audit-table td {
  text-align: left;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.audit-table th {
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
}

.audit-table code {
  font-size: 0.75rem;
}
