@import url('https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@400;500;600;700&family=Quattrocento:wght@400;700&display=swap');

:root {
  --bg: #f2f2f2;
  --surface: #ffffff;
  --surface2: #f8f8f8;
  --border: #e1e4e6;
  --border-strong: rgba(0, 0, 0, 0.12);
  --text: #000000;
  --text-secondary: #626262;
  --muted: #626262;
  --accent: #000000;
  --accent-hover: rgba(0, 0, 0, 0.72);
  --accent-soft: #e1e4e6;
  --accent-soft-hover: #c5cbcf;
  --danger: #b00020;
  --ok: #2e7d32;
  --radius: 2px;
  --font-display: "Josefin Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  --font-body: "Quattrocento", Georgia, "Times New Roman", serif;
  --font-ui: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font-body);
  font-size: 16px;
  font-weight: 400;
  line-height: 1.75;
  letter-spacing: 0.02em;
  background: var(--bg);
  color: var(--text-secondary);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, .topbar, .tabs, .login-card button, #chat-send, #save-page-btn, .brand-tagline {
  font-family: var(--font-display);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.small { font-size: 0.85rem; padding: 0.35rem 0.6rem; }

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: var(--bg);
}

.login-card {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 2.5rem 2rem;
  display: grid;
  gap: 0.85rem;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
}

.login-card h1 {
  margin: 0;
  color: var(--text);
  font-size: 1.75rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.login-card .brand-mark {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  margin-bottom: 0.25rem;
}

.login-card .brand-mark img {
  height: 44px;
  width: auto;
  max-width: 260px;
  object-fit: contain;
}

.login-card label {
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
}

.login-card input,
textarea,
input,
select,
button {
  font: inherit;
}

.login-card input,
textarea,
.wiki-editor input,
.wiki-editor select,
#wiki-filter {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0.65rem 0.8rem;
  font-family: var(--font-ui);
  font-size: 14px;
}

.login-card input:focus,
textarea:focus,
.wiki-editor input:focus,
#wiki-filter:focus {
  outline: none;
  border-color: var(--text);
}

.login-card button,
#chat-send,
#save-page-btn {
  background: var(--accent);
  color: #ffffff;
  border: none;
  border-radius: var(--radius);
  padding: 0.85rem 1.2rem;
  cursor: pointer;
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  transition: background 200ms ease;
}

.login-card button:hover,
#chat-send:hover,
#save-page-btn:hover {
  background: var(--accent-hover);
}

.ghost {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0.45rem 0.8rem;
  cursor: pointer;
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  transition: opacity 200ms ease;
}

.ghost:hover { opacity: 0.65; }

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--surface);
  max-width: 1400px;
  margin: 0 auto;
  box-shadow: 0 0 0 1px var(--border);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  background: var(--surface);
}

.brand {
  display: flex;
  gap: 0.85rem;
  align-items: center;
}

.brand img {
  height: 38px;
  width: auto;
  max-width: 240px;
  object-fit: contain;
}

.brand strong {
  display: block;
  color: var(--text);
  font-size: 1.15rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.brand-tagline {
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.tabs { display: flex; gap: 0.15rem; }

.tab {
  background: transparent;
  border: none;
  color: var(--text);
  padding: 0.55rem 1rem;
  cursor: pointer;
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  opacity: 0.55;
  position: relative;
  transition: opacity 200ms ease;
}

.tab:hover { opacity: 0.8; }

.tab.active {
  opacity: 1;
  background: transparent;
}

.tab.active::after {
  content: "";
  position: absolute;
  left: 1rem;
  right: 1rem;
  bottom: 0;
  height: 1px;
  background: rgba(0, 0, 0, 0.4);
}

.tab.disabled { opacity: 0.3; cursor: not-allowed; }

.app-footer {
  padding: 0.85rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface2);
  font-family: var(--font-body);
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}

.app-footer a {
  color: var(--text);
  opacity: 0.7;
  border-bottom: 1px solid rgba(0, 0, 0, 0.35);
  text-decoration: none;
}

main { min-height: 0; background: var(--surface); }
.panel { display: none; height: calc(100vh - 112px); }
.panel.active { display: block; }

.agent-layout { display: grid; grid-template-columns: 220px 1fr 300px; height: 100%; }

.conv-sidebar,
.bg-agent-sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface2);
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
  overflow: hidden;
}

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

.sidebar-head h3 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.sidebar-actions { display: flex; gap: 0.25rem; }

.conv-list,
.bg-agent-list {
  list-style: none;
  margin: 0;
  padding: 0.35rem 0;
  overflow: auto;
}

.conv-list button,
.bg-agent-list li {
  width: 100%;
  text-align: left;
}

.conv-list button {
  border: none;
  background: transparent;
  padding: 0.65rem 1rem;
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text-secondary);
  border-left: 3px solid transparent;
}

.conv-list button:hover { background: var(--surface); color: var(--text); }
.conv-list button.active {
  background: var(--surface);
  color: var(--text);
  border-left-color: var(--text);
}

.conv-list .conv-title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-list .conv-date {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-top: 0.15rem;
}

.bg-agent-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem 0.55rem 1rem;
  cursor: pointer;
  border-left: 3px solid transparent;
}

.bg-agent-list li:hover { background: var(--surface); }
.bg-agent-list li.active {
  background: var(--surface);
  border-left-color: var(--text);
}

.bg-agent-list .agent-select {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  text-align: left;
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text);
  padding: 0;
}

.bg-agent-list .agent-select .agent-name {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bg-agent-list .agent-select .agent-schedule {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-top: 0.1rem;
}

.heartbeat-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100%;
  min-height: 0;
}

.bg-agent-main {
  min-height: 0;
  overflow: auto;
  background: var(--surface);
}

.bg-agent-detail {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100%;
  min-height: 0;
}

.bg-agent-form {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: grid;
  gap: 0.65rem;
}

.bg-agent-form-head {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.bg-agent-form-head input {
  flex: 1;
  font-family: var(--font-ui);
  font-size: 16px;
  font-weight: 600;
}

.field-label {
  font-family: var(--font-display);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.bg-agent-form textarea,
.bg-agent-form input,
.bg-agent-form select {
  font-family: var(--font-ui);
}

.bg-agent-schedule {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: end;
}

.field-group { display: grid; gap: 0.25rem; min-width: 120px; }
.field-grow { flex: 1; min-width: 180px; }

.bg-agent-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.bg-agent-actions .danger { color: #8b2e2e; }

.bg-agent-log {
  padding: 1rem 1.25rem;
  overflow: auto;
  min-height: 0;
}

.bg-agent-log h3 {
  margin: 0 0 0.75rem;
  font-family: var(--font-display);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.bg-agent-runs { display: grid; gap: 0.65rem; }

.run-item {
  border: 1px solid var(--border);
  padding: 0.75rem 0.9rem;
  background: var(--surface2);
  font-family: var(--font-ui);
  font-size: 13px;
}

.run-item .run-head {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.run-item .run-status {
  font-family: var(--font-display);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.run-item .run-status.completed { color: #2d6a4f; }
.run-item .run-status.failed { color: #8b2e2e; }
.run-item .run-status.running { color: var(--muted); }

.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.toggle-switch input { display: none; }

.toggle-slider {
  width: 36px;
  height: 20px;
  background: var(--border-strong);
  border-radius: 999px;
  position: relative;
  transition: background 0.15s;
}

.toggle-slider::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--surface);
  border-radius: 50%;
  transition: transform 0.15s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--text);
}

.toggle-switch input:checked + .toggle-slider::after {
  transform: translateX(16px);
}

.chat-pane { display: grid; grid-template-rows: 1fr auto; min-height: 0; position: relative; }

.chat-log {
  overflow: auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  background: var(--surface);
}

.msg {
  max-width: 900px;
  padding: 0.9rem 1rem;
  line-height: 1.6;
  white-space: pre-wrap;
  font-family: var(--font-body);
  color: var(--text-secondary);
}

.msg.user {
  background: var(--accent-soft);
  color: var(--text);
  align-self: flex-end;
  font-family: var(--font-ui);
  font-size: 15px;
}

.msg.assistant {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.msg.system {
  color: var(--muted);
  font-size: 0.9rem;
  font-family: var(--font-ui);
}

.markdown-body { line-height: 1.75; color: var(--text-secondary); }
.markdown-body h1, .markdown-body h2, .markdown-body h3 {
  margin: 0.75rem 0 0.35rem;
  font-family: var(--font-body);
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0;
  text-transform: none;
}
.markdown-body p { margin: 0.45rem 0; }
.markdown-body ul, .markdown-body ol { margin: 0.35rem 0 0.35rem 1.2rem; }
.markdown-body code {
  background: var(--surface2);
  padding: 0.1rem 0.35rem;
  border-radius: 2px;
  font-size: 0.9em;
  font-family: var(--font-ui);
}
.markdown-body pre {
  background: var(--surface2);
  padding: 0.75rem;
  border: 1px solid var(--border);
  overflow: auto;
}
.markdown-body pre code { background: transparent; padding: 0; }
.markdown-body a {
  color: inherit;
  opacity: 0.75;
  border-bottom: 1px solid rgba(0, 0, 0, 0.45);
  text-decoration: none;
}
.markdown-body a:hover { opacity: 1; }
.markdown-body blockquote {
  border-left: 3px solid var(--border);
  margin: 0.5rem 0;
  padding: 0.5rem 0 0.5rem 0.85rem;
  background: rgba(225, 228, 230, 0.35);
  color: var(--text-secondary);
}

.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.88);
  border: 2px dashed rgba(0, 0, 0, 0.25);
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
  pointer-events: none;
}

.staged-files { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-bottom: 0.5rem; }
.staged-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.25rem 0.6rem;
  font-family: var(--font-ui);
  font-size: 0.82rem;
  color: var(--text);
}
.staged-chip button {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.chat-form {
  border-top: 1px solid var(--border);
  padding: 0.85rem 1.5rem 1rem;
  background: var(--surface);
}

.chat-form textarea {
  width: 100%;
  resize: vertical;
  min-height: 72px;
  font-family: var(--font-ui);
  font-size: 15px;
  color: var(--text);
}

.chat-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.5rem;
}

#agent-status {
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.tool-pane {
  border-left: 1px solid var(--border);
  padding: 1rem;
  overflow: auto;
  background: var(--surface2);
}

.tool-pane h3 {
  margin-top: 0;
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
}

.tool-item {
  font-size: 0.85rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-ui);
}

.wiki-layout { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.wiki-sidebar {
  border-right: 1px solid var(--border);
  padding: 0.75rem;
  overflow: auto;
  background: var(--surface2);
}

.wiki-sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wiki-sidebar-head h3 {
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
  margin: 0;
}

#wiki-filter { width: 100%; margin: 0.5rem 0; }
.wiki-list { list-style: none; padding: 0; margin: 0; }

.wiki-list li button {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 0.55rem 0.35rem;
  cursor: pointer;
  font-family: var(--font-ui);
}

.wiki-list li button:hover,
.wiki-list li button.active {
  background: var(--surface);
  color: var(--text);
}

.wiki-list .type {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-family: var(--font-display);
}

.wiki-editor { display: grid; grid-template-rows: auto auto 1fr auto; min-height: 0; }

.wiki-editor-bar {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 0.75rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
}

.wiki-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
  padding: 0.45rem 1rem;
  flex: 1;
  min-width: 0;
}

.wiki-view-toggle {
  display: flex;
  align-self: center;
  flex-shrink: 0;
  margin-right: 1rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}

.view-toggle-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 0.4rem 0.85rem;
  cursor: pointer;
  font-family: var(--font-display);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  line-height: 1.2;
}

.view-toggle-btn:hover {
  color: var(--text);
  background: var(--surface2);
}

.view-toggle-btn.active {
  background: var(--text);
  color: var(--surface);
}

.wiki-editor-body {
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-rows: 1fr;
}

.wiki-editor-pane,
.wiki-preview-pane {
  min-height: 0;
  overflow: hidden;
  height: 100%;
}

.wiki-editor-pane:not(.hidden),
.wiki-preview-pane:not(.hidden) {
  display: block;
}

.tb-btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  padding: 0.35rem 0.55rem;
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 13px;
  border-radius: var(--radius);
  line-height: 1.2;
}

.tb-btn:hover {
  background: var(--surface);
  border-color: var(--border-strong);
}

.tb-sep {
  width: 1px;
  height: 1.25rem;
  background: var(--border);
  margin: 0 0.25rem;
}

#page-body {
  width: 100%;
  height: 100%;
  border: none;
  background: var(--surface);
  color: var(--text);
  padding: 1rem 1.25rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
}

.wiki-preview {
  overflow: auto;
  padding: 1rem 1.25rem;
  background: var(--surface);
  min-height: 0;
}

.wiki-preview:empty::before {
  content: "Preview will appear here…";
  color: var(--muted);
  font-style: italic;
}

.markdown-body h1 { font-size: 1.65rem; }
.markdown-body h2 { font-size: 1.35rem; }
.markdown-body h3 { font-size: 1.15rem; }
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5rem 0;
  font-family: var(--font-ui);
  font-size: 14px;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.55rem;
  text-align: left;
}
.markdown-body th { background: var(--surface2); color: var(--text); }

.wiki-editor-head {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.wiki-editor-head input,
.wiki-editor-head select {
  min-width: 120px;
  flex: 1;
  font-family: var(--font-ui);
}

#save-reason { flex: 2; min-width: 200px; }

.page-meta {
  padding: 0.5rem 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  background: var(--surface2);
}

.placeholder {
  padding: 2rem 1.5rem;
  color: var(--text-secondary);
}

.placeholder h2 {
  font-family: var(--font-body);
  color: var(--text);
  font-weight: 700;
}

@media (max-width: 900px) {
  .agent-layout, .wiki-layout, .heartbeat-layout { grid-template-columns: 1fr; }
  .tool-pane, .wiki-sidebar, .conv-sidebar, .bg-agent-sidebar { display: none; }
  .wiki-editor-bar { flex-direction: column; align-items: stretch; gap: 0; }
  .wiki-view-toggle { align-self: flex-end; margin: 0.5rem 1rem 0.65rem; }
  .app { box-shadow: none; }
}
