*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a1a;
  --surface: #242424;
  --border: #ff0000;
  --text: #e8e8e8;
  --text-muted: #888;
  --user-bg: #2d4a7a;
  --user-text: #d6e8ff;
  --assistant-bg: #2a2a2a;
  --error-bg: #4a2020;
  --error-text: #ffaaaa;
  --accent: #ff0000;
  --radius: 12px;
}

html, body { height: 100%; }

/* --- Login landing page --- */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: 600;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 4px;
}

.login-error {
  background: var(--error-bg);
  border: 1px solid #6a3030;
  color: var(--error-text);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
}

.login-card input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  padding: 12px 14px;
  transition: border-color 0.15s;
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-card input::placeholder { color: var(--text-muted); }

.login-card button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
  padding: 12px;
  cursor: pointer;
  transition: opacity 0.15s;
}

.login-card button:hover { opacity: 0.88; }

.login-form { display: flex; flex-direction: column; gap: 12px; }

.login-btn {
  display: block;
  text-align: center;
  text-decoration: none;
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
  padding: 12px;
  transition: opacity 0.15s;
}

.login-btn:hover { opacity: 0.88; }

/* --- Dashboard --- */
.dashboard {
  flex: 1;
  overflow-y: auto;
  padding: 28px 32px;
  max-width: 1000px;
  width: 100%;
  margin: 0 auto;
}

.dashboard-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 18px;
}

.dashboard-head h1 { font-size: 20px; font-weight: 600; }

.dashboard-scope { font-size: 13px; color: var(--text-muted); }

.dashboard-empty { color: var(--text-muted); font-style: italic; }

.table-wrap { overflow-x: auto; }

table.runs {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

table.runs th,
table.runs td {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid #333;
  white-space: nowrap;
}

table.runs th {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}

table.runs tbody tr:hover { background: rgba(255, 255, 255, 0.03); }

.run-status {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 4px;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.06);
}

.run-status.done { color: #9be29b; background: rgba(60, 180, 60, 0.16); }
.run-status.error { color: var(--error-text); background: var(--error-bg); }
.run-status.running { color: #ffcf99; background: rgba(255, 140, 0, 0.16); }
.run-status.awaiting_input { color: #9ecbff; background: rgba(80, 140, 255, 0.18); }

.run-row { cursor: pointer; }
.run-row:focus { outline: 1px solid var(--accent); outline-offset: -1px; }

/* --- Run detail modal --- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.6);
}
.modal[hidden] { display: none; }

.modal-card {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 780px;
  max-height: 85vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 16px; font-weight: 600; }

.modal-close {
  flex-shrink: 0;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }

.run-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.meta-k {
  color: var(--text-muted);
  margin-right: 6px;
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.04em;
}

.run-log {
  flex: 1;
  margin: 0;
  padding: 16px 20px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--text);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  display: flex;
  flex-direction: column;
}

/* Cleaner, theme-matching scrollbars */
* { scrollbar-width: thin; scrollbar-color: #3a3a3a transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: #3a3a3a;
  border-radius: 8px;
  border: 3px solid transparent;   /* inset padding -> slimmer thumb */
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
  border: 3px solid transparent;
  background-clip: padding-box;
}
::-webkit-scrollbar-corner { background: transparent; }

.layout {
  display: flex;
  flex: 1;
  min-height: 0;
}

.sidebar {
  flex-shrink: 0;
  width: 200px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  overflow-y: auto;
}

.sidebar.right {
  border-left: 1px solid var(--border);
}

.sidebar.left {
  border-right: 1px solid var(--border);
}

.sidebar-title {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0 8px 12px;
}

.automation-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.automation-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.automation-btn:hover {
  background: rgba(255, 0, 0, 0.08);
  border-color: var(--accent);
}

.automation-btn:active { background: rgba(255, 0, 0, 0.16); }

.automation-btn:disabled { opacity: 0.5; cursor: default; }

.automation-label { flex: 1; }

.automation-mode {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  color: var(--text-muted);
}

/* --- Hover summary card --- */
.automation-popup {
  position: fixed;
  z-index: 1000;
  width: 280px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

.automation-popup[hidden] { display: none; }

.automation-popup h1,
.automation-popup h2,
.automation-popup h3 {
  font-size: 14px;
  margin: 0 0 6px;
  line-height: 1.3;
}

.automation-popup p { margin: 0 0 6px; }
.automation-popup p:last-child { margin-bottom: 0; }
.automation-popup ul,
.automation-popup ol { margin: 4px 0 0 1.2em; }
.automation-popup li { margin-bottom: 2px; }
.automation-popup code {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 4px;
  padding: 0.1em 0.35em;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.85em;
}

.sidebar-footer {
  margin-top: auto;
  padding: 12px 8px 0;
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
}

/* --- User panel (right sidebar) --- */
.user-panel {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 14px;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.user-avatar {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 15px;
}

.user-info { min-width: 0; }  /* enables ellipsis truncation of children */

.user-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-email {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  display: inline-block;
  margin-top: 4px;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.06);
}

.user-role.admin { color: #ffb3b3; background: rgba(255, 0, 0, 0.16); }

/* --- Job queue (right sidebar) --- */
.job-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.job-empty {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  padding: 4px 8px;
}

.job-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.02);
}

.job-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.job-label { font-size: 13px; font-weight: 500; }

.job-badge {
  flex-shrink: 0;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 4px;
  color: #ffcf99;
  background: rgba(255, 140, 0, 0.16);
}

.job-item.running .job-badge { animation: pulse 1.2s ease-in-out infinite; }
.job-item.done .job-badge { color: #9be29b; background: rgba(60, 180, 60, 0.16); }
.job-item.error .job-badge { color: var(--error-text); background: var(--error-bg); }
.job-item.waiting .job-badge {
  color: #9ecbff;
  background: rgba(80, 140, 255, 0.18);
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse { 50% { opacity: 0.5; } }

.job-detail {
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-muted);
  font-family: "Cascadia Code", Consolas, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#app {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  max-width: 780px;
  margin: 0 auto;
}

/* --- Top header bar --- */
#topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.title {
  font-weight: 600;
  font-size: 16px;
  color: var(--text);
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 4px;
  margin-right: auto;
}

.tab {
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--text-muted);
  text-decoration: none;
  transition: color 0.15s, background 0.15s;
}

.tab:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }

.tab.active {
  color: var(--text);
  background: rgba(255, 0, 0, 0.12);
}

.signout-form { display: inline; }

#topbar button {
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

#topbar button:hover { color: var(--text); border-color: #555; }

main {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: 78%;
}

.message.user { align-self: flex-end; align-items: flex-end; }
.message.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 16px;
  border-radius: var(--radius);
  word-break: break-word;
}

.message.user .bubble { white-space: pre-wrap; }

/* Markdown elements inside assistant bubbles */
.message.assistant .bubble p { margin-bottom: 0.6em; }
.message.assistant .bubble p:last-child { margin-bottom: 0; }
.message.assistant .bubble ul,
.message.assistant .bubble ol { margin: 0.4em 0 0.6em 1.4em; }
.message.assistant .bubble li { margin-bottom: 0.2em; }
.message.assistant .bubble code {
  background: rgba(255,255,255,0.08);
  border-radius: 4px;
  padding: 0.15em 0.4em;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.88em;
}
.message.assistant .bubble pre {
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.5em 0;
}
.message.assistant .bubble pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
}
.message.assistant .bubble blockquote {
  border-left: 3px solid var(--border);
  margin: 0.5em 0;
  padding: 0.2em 0.8em;
  color: var(--text-muted);
}
.message.assistant .bubble h1,
.message.assistant .bubble h2,
.message.assistant .bubble h3 { margin: 0.6em 0 0.3em; line-height: 1.3; }
.message.assistant .bubble a { color: var(--accent); }
.message.assistant .bubble table { border-collapse: collapse; margin: 0.5em 0; width: 100%; }
.message.assistant .bubble th,
.message.assistant .bubble td { border: 1px solid var(--border); padding: 6px 10px; }
.message.assistant .bubble th { background: rgba(255,255,255,0.05); }

.message.user .bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 3px;
}

.message.assistant .bubble {
  background: var(--assistant-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 3px;
}

.message.assistant .bubble.error {
  background: var(--error-bg);
  border-color: #6a3030;
  color: var(--error-text);
}

.message.assistant .bubble.automation-output {
  white-space: pre-wrap;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.85em;
}

/* Interactive automation prompt */
.bubble.prompt .prompt-q { margin-bottom: 10px; }
.prompt-row { display: flex; gap: 8px; }
.prompt-input,
.bubble.prompt select {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  padding: 8px 10px;
}
.prompt-input:focus,
.bubble.prompt select:focus { outline: none; border-color: var(--accent); }
.prompt-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.prompt-send:disabled { opacity: 0.5; cursor: default; }
.prompt-answer { margin-top: 8px; color: var(--text-muted); font-size: 13px; }

.cursor::after {
  content: "▋";
  animation: blink 0.8s step-end infinite;
  font-size: 0.9em;
  opacity: 0.7;
}

@keyframes blink { 50% { opacity: 0; } }

.label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
  padding: 0 4px;
}

footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#chat-form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

textarea {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  padding: 10px 14px;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
  transition: border-color 0.15s;
}

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

textarea::placeholder { color: var(--text-muted); }

#send-btn {
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
  white-space: nowrap;
}

#send-btn:disabled { opacity: 0.45; cursor: default; }
#send-btn:not(:disabled):hover { opacity: 0.88; }

#reset-btn {
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
}

#reset-btn:hover { color: var(--text); border-color: #555; }
