:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #15181d;
  --muted: #5c6472;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-contrast: #ffffff;
  --accent-soft: #eef0ff;
  --danger: #c0392b;
  --danger-contrast: #ffffff;
  --danger-soft: #fdeeee;
  --success: #1d7a4c;
  --success-soft: #e6f4ec;
  --warn: #9a5b00;
  --warn-soft: #fff3df;
  --info-soft: #eaf2ff;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 12px;
  --radius-sm: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1014;
    --surface: #161920;
    --surface-2: #1d212a;
    --border: #2a2f3a;
    --text: #e7e9ee;
    --muted: #9aa2b1;
    --accent: #8b87ff;
    --accent-hover: #a29fff;
    --accent-contrast: #12131a;
    --accent-soft: #23234a;
    --danger: #ff7b72;
    --danger-contrast: #1a0d0c;
    --danger-soft: #3a1d1d;
    --success: #4cc38a;
    --success-soft: #16301f;
    --warn: #f0b45b;
    --warn-soft: #3a2a12;
    --info-soft: #1a2640;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1rem; font-weight: 650; }
h3 { font-size: 0.95rem; font-weight: 650; }
p { margin: 0; }
code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 0.85em; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.optional { color: var(--muted); font-weight: 400; font-size: 0.8rem; margin-left: 4px; }
.hint { color: var(--muted); font-size: 0.82rem; }

/* Forms */
.stack { display: flex; flex-direction: column; gap: 14px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 0.88rem; font-weight: 550; }
input[type="text"], input[type="email"], input[type="password"], textarea, select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 400;
}
textarea { resize: vertical; min-height: 96px; }
select { cursor: pointer; text-overflow: ellipsis; }
select option, select optgroup { background: var(--surface); color: var(--text); }
input:focus-visible, textarea:focus-visible, button:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 450; }
label.check input { width: 16px; height: 16px; accent-color: var(--accent); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit; font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.btn.btn-armed, .btn.btn-armed:hover { background: var(--danger); border-color: var(--danger); color: var(--danger-contrast); }
.inline-panel { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; display: flex; flex-direction: column; gap: 10px; background: var(--surface-2); }
.btn-ghost { background: transparent; }
.btn-sm { padding: 5px 12px; font-size: 0.85rem; }
.btn-block { width: 100%; }
.btn-link { background: none; border: none; padding: 4px 0; color: var(--muted); font-weight: 500; text-decoration: underline; }
.btn-link:hover { background: none; color: var(--text); }

/* Alerts */
.alert { padding: 10px 12px; border-radius: var(--radius-sm); font-size: 0.88rem; }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-info { background: var(--info-soft); }

/* Brand */
.brand { display: flex; align-items: center; gap: 12px; }
.brand img { border-radius: 10px; flex: none; }
.brand-name { font-weight: 700; font-size: 1.05rem; letter-spacing: -0.01em; white-space: nowrap; }

/* Login */
.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex; flex-direction: column; gap: 20px;
}
.fine-print { color: var(--muted); font-size: 0.8rem; text-align: center; }

/* App shell */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-meta { display: flex; align-items: center; gap: 12px; min-width: 0; margin-left: auto; }
a.brand { color: inherit; text-decoration: none; }
.nav { display: flex; gap: 4px; margin-left: 12px; }
.nav a {
  padding: 6px 12px; border-radius: var(--radius-sm);
  color: var(--muted); text-decoration: none; font-weight: 600; font-size: 0.9rem; white-space: nowrap;
}
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }
a { color: var(--accent); }
a.btn { text-decoration: none; }
.topbar-meta form { margin: 0; }
.chip {
  padding: 2px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 0.78rem; font-weight: 600; white-space: nowrap;
}
.user { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.layout {
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  gap: 20px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
  align-items: start;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}
.compose { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 76px; }

.login-box {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.login-box[open] { padding-bottom: 14px; }
.login-box summary { cursor: pointer; font-weight: 600; font-size: 0.9rem; }
.login-fields { margin-top: 12px; }

/* History */
.history { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--border); padding-top: 16px; }
.run-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; max-height: 260px; overflow-y: auto; }
.run-item {
  width: 100%;
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px;
  padding: 8px 10px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: none; color: var(--text); font: inherit; font-size: 0.88rem; text-align: left;
  cursor: pointer;
}
.run-item:hover { background: var(--surface-2); }
.run-item.selected { background: var(--accent-soft); border-color: var(--accent-soft); }
.run-item-task { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot-running, .dot-stopping { background: var(--accent); }
.dot-paused, .dot-waiting_for_human { background: var(--warn); }
.dot-done { background: var(--success); }
.dot-failed { background: var(--danger); }

/* Run panel */
.empty-state { text-align: center; padding: 72px 24px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.empty-state img { border-radius: 14px; opacity: 0.9; }
.empty-state p { max-width: 440px; }

#run-view { display: flex; flex-direction: column; gap: 16px; }
.run-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.run-head-info { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.controls { display: flex; gap: 8px; }
.run-task { font-size: 1.02rem; font-weight: 550; white-space: pre-wrap; overflow-wrap: anywhere; }

.status {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  font-size: 0.8rem; font-weight: 650;
  background: var(--surface-2); color: var(--muted);
}
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-running, .status-stopping { background: var(--accent-soft); color: var(--accent); }
.status-running::before { animation: pulse 1.4s ease-in-out infinite; }
.status-paused, .status-waiting_for_human { background: var(--warn-soft); color: var(--warn); }
.status-done { background: var(--success-soft); color: var(--success); }
.status-failed { background: var(--danger-soft); color: var(--danger); }
@keyframes pulse { 50% { opacity: 0.3; } }

.question {
  border: 1px solid var(--warn);
  background: var(--warn-soft);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex; flex-direction: column; gap: 8px;
}
.question-label { font-weight: 700; color: var(--warn); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.question-text { font-weight: 550; white-space: pre-wrap; overflow-wrap: anywhere; }
.question-row { display: flex; gap: 8px; }
.question-row input { flex: 1; min-width: 0; }
.live-embed { display: flex; flex-direction: column; gap: 6px; }
.live-embed iframe {
  width: 100%; aspect-ratio: 1366 / 900; border: 0;
  border-radius: var(--radius-sm); background: #000;
}
.live-shot-link { display: block; cursor: zoom-in; }

.result { border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--border); }
.result-ok { background: var(--success-soft); border-color: transparent; }
.result-bad { background: var(--danger-soft); border-color: transparent; }
.result-label { font-weight: 700; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.result-ok .result-label { color: var(--success); }
.result-bad .result-label { color: var(--danger); }
.result-text { white-space: pre-wrap; overflow-wrap: anywhere; }

.live { margin: 0; display: flex; flex-direction: column; gap: 6px; }
.live img {
  width: 100%; height: auto; display: block;
  max-height: 70vh; object-fit: contain; object-position: top;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.live figcaption { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.timeline-title { padding-top: 4px; }
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.step, .note {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 8px;
  min-width: 0;
}
.note { background: var(--surface-2); font-size: 0.88rem; flex-direction: row; gap: 10px; overflow-wrap: anywhere; }
.note-time { color: var(--muted); flex: none; }
.step-head { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.step-num { font-weight: 700; font-size: 0.85rem; color: var(--accent); flex: none; }
.step-url { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step-goal { font-weight: 550; overflow-wrap: anywhere; }
.actions { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.action { display: flex; gap: 8px; font-size: 0.86rem; min-width: 0; }
.action-name { background: var(--accent-soft); color: var(--accent); padding: 0 6px; border-radius: 4px; flex: none; }
.action-params { color: var(--muted); overflow-wrap: anywhere; min-width: 0; }
.step-details summary { cursor: pointer; color: var(--muted); font-size: 0.82rem; }
.step-details p { font-size: 0.85rem; margin-top: 6px; white-space: pre-wrap; overflow-wrap: anywhere; }
.step-results:empty { display: none; }
.step-results { display: flex; flex-direction: column; gap: 4px; }
.result-line { font-size: 0.84rem; padding: 6px 8px; border-radius: 6px; background: var(--surface-2); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 160px; overflow-y: auto; }
.result-line.error { background: var(--danger-soft); color: var(--danger); }

/* Shared page pieces */
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.intro { margin: 6px 0 16px; }
.button-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.run-row .btn-primary { flex: 1; }
.prompt-text {
  margin: 0; font-size: 0.88rem; color: var(--muted);
  white-space: pre-wrap; overflow-wrap: anywhere;
  max-height: 6.2em; overflow: hidden;
}
.prompt-meta { margin: 0; overflow-wrap: anywhere; }
.input-with-button { display: flex; gap: 8px; font-weight: 400; }
.input-with-button input { flex: 1; min-width: 0; }
.chip-muted { background: var(--surface-2); color: var(--muted); }
.chip-warn { background: var(--warn-soft); color: var(--warn); }
.mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 0.88rem; }
.empty-state.compact { padding: 32px 16px; }

/* Accounts */
.account-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.account-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.account-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.account-title { display: flex; align-items: center; gap: 12px; min-width: 0; }
.account-names { min-width: 0; }
.account-names a { overflow-wrap: anywhere; }
.account-label { font-weight: 650; overflow-wrap: anywhere; }
.avatar {
  width: 38px; height: 38px; border-radius: 10px; flex: none;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-weight: 700;
}
.account-fields { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 16px; margin: 0; font-size: 0.9rem; }
.account-fields dt { color: var(--muted); }
.account-fields dd { margin: 0; overflow-wrap: anywhere; }
.password-cell { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.sessions { border-top: 1px solid var(--border); margin-top: 20px; padding-top: 16px; display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }

/* System Rules */
.layout-wide { grid-template-columns: minmax(0, 1fr) minmax(260px, 340px); }
#rules-text { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 0.86rem; line-height: 1.55; min-height: 420px; }
.side-notes { display: flex; flex-direction: column; gap: 10px; position: sticky; top: 76px; }
.side-notes h3 + p { margin-top: -2px; }
.tips { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }

@media (max-width: 860px) {
  .layout, .layout-wide { grid-template-columns: minmax(0, 1fr); padding: 16px; gap: 16px; }
  .compose, .side-notes { position: static; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .nav { order: 3; width: 100%; margin-left: 0; overflow-x: auto; }
  .user { display: none; }
}
@media (max-width: 480px) {
  .topbar .chip { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .status-running::before { animation: none; }
}
