:root {
  --bg-a: #fff8e8;
  --bg-b: #f1edff;
  --ink: #1d2230;
  --muted: #74839b;
  --line: #ddd6eb;
  --panel: #ffffff;
  --panel-soft: #f8f5ff;
  --brand: #2b225f;
  --brand-2: #5845cc;
  --brand-glow: rgba(88, 69, 204, 0.12);
  --red: #ed3027;
  --green: #287d5f;
  --gold: #ea991b;
  --teal: #369f79;
  --surface: rgba(255, 255, 255, 0.7);
  --surface-hover: rgba(255, 255, 255, 0.9);
  --surface-active: #f0ebff;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 3px rgba(43, 34, 95, 0.06);
  --shadow-md: 0 4px 14px rgba(43, 34, 95, 0.08);
  --shadow-active: 0 0 0 2px var(--brand-2), 0 4px 14px rgba(88, 69, 204, 0.15);
}

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

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 100dvh;
  color: var(--ink);
  font-family: Inter, "Segoe UI", system-ui, sans-serif;
  background: linear-gradient(135deg, var(--bg-a), var(--bg-b));
}

button, input, select { font: inherit; }

button {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--brand);
  font-weight: 600;
  font-size: 12px;
  padding: 7px 14px;
  cursor: pointer;
  transition: all 0.15s ease;
}

button:hover {
  border-color: var(--brand-2);
  box-shadow: var(--shadow-sm);
}

button.success { color: var(--green); }
button.success:hover { border-color: var(--green); }
button.danger { color: var(--red); }
button.danger:hover { border-color: var(--red); }
button.warning { color: var(--gold); }
button.warning:hover { border-color: var(--gold); }

/* ─── Topbar ─────────────────────────────────────────────────────── */

.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.brand, .top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand img, .login-logo {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  object-fit: cover;
}

.brand strong {
  font-size: 14px;
  letter-spacing: 0.02em;
}

.brand span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.queue-health {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}

.queue-health strong,
.queue-health span,
.queue-health small {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.queue-health strong {
  color: var(--brand);
}

.queue-health span {
  padding: 4px 7px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.7);
}

.queue-health small {
  color: var(--muted);
  font-size: 10px;
  font-weight: 600;
}

.queue-health.healthy span { color: var(--green); }
.queue-health.active span { color: var(--gold); }
.queue-health.danger span,
.queue-health.warning span,
.queue-health .queue-stale {
  color: var(--red);
  border-color: rgba(237, 48, 39, 0.32);
  background: rgba(237, 48, 39, 0.08);
}

/* ─── Main Shell ─────────────────────────────────────────────────── */

.shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

/* ─── Sidebar ────────────────────────────────────────────────────── */

.sidebar {
  min-height: 0;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.45);
  padding: 12px;
  overflow: auto;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: rgba(43, 34, 95, 0.12); border-radius: 4px; }

.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin-bottom: 12px;
  padding: 3px;
  background: rgba(43, 34, 95, 0.05);
  border-radius: var(--radius);
}

.side-tab {
  border: none;
  border-radius: 7px;
  padding: 7px 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  transition: all 0.15s ease;
}

.side-tab:hover {
  color: var(--brand);
  background: rgba(255, 255, 255, 0.6);
}

.side-tab.active {
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  box-shadow: 0 2px 8px rgba(43, 34, 95, 0.2);
}

.pipeline-list {
  display: grid;
  gap: 6px;
}

/* ─── Pipeline Cards ─────────────────────────────────────────────── */

.pipeline-card {
  width: 100%;
  text-align: left;
  display: grid;
  gap: 4px;
  border-radius: var(--radius);
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid transparent;
  box-shadow: var(--shadow-sm);
  transition: all 0.15s ease;
  cursor: pointer;
}

.pipeline-card:hover {
  box-shadow: var(--shadow-md);
}

.pipeline-card.active {
  border-color: var(--brand-2);
  background: var(--surface-active);
  box-shadow: var(--shadow-active);
}

.pipeline-head {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--brand);
  padding: 0;
  border: none;
  background: none;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  box-shadow: none;
}

.pipeline-head:hover {
  background: none;
  border: none;
  box-shadow: none;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
  opacity: 0.4;
}

.dot.running {
  background: var(--teal);
  opacity: 1;
  box-shadow: 0 0 6px rgba(54, 159, 121, 0.45);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.dot.success { background: var(--green); opacity: 0.8; }
.dot.error { background: var(--red); opacity: 1; }

.pipeline-card small {
  color: var(--muted);
  line-height: 1.4;
  font-size: 11px;
}

.card-actions {
  display: flex;
  gap: 4px;
  margin-top: 4px;
}

.card-actions button {
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 500;
}

/* ─── Workspace ──────────────────────────────────────────────────── */

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.tabs {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
  padding: 12px 20px;
}

h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.tab {
  font-size: 12px;
  border: none;
  padding: 6px 14px;
  color: var(--muted);
  background: transparent;
  border-radius: 7px;
}

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

.tab.active {
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  box-shadow: 0 2px 8px rgba(43, 34, 95, 0.2);
}

/* ─── Panels ─────────────────────────────────────────────────────── */

.panel {
  display: none;
  min-height: 0;
  margin: 0 16px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.86);
  overflow: auto;
}

.panel.active {
  display: block;
}

#logsPanel.active {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  background: #1d2230;
}

.log-source-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  background: #171c28;
}

.log-source-filter {
  min-height: 28px;
  padding: 5px 10px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 6px;
  color: #aeb9cf;
  background: rgba(255,255,255,0.04);
  font-size: 12px;
  font-weight: 600;
}

.log-source-filter:hover,
.log-source-filter.active {
  color: #f5f7fb;
  border-color: rgba(82, 179, 217, 0.72);
  background: rgba(82, 179, 217, 0.18);
}

.logs {
  align-self: stretch;
  height: 100%;
  max-height: calc(150 * 1.55em + 28px);
  min-height: 0;
  margin: 0;
  padding: 16px 20px;
  overflow: auto;
  color: #d7def5;
  background: #1d2230;
  font: 12px/1.65 Consolas, "SFMono-Regular", monospace;
  white-space: pre-wrap;
}

.logs::-webkit-scrollbar { width: 5px; }
.logs::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 4px; }

#logsPanel {
  overflow: hidden;
}

#historyPanel.active,
#settingsPanel.active {
  overflow: auto;
}

.log-error { color: #ff9b9b; }
.log-warning { color: #ffd37b; }
.log-success { color: #91dfa9; }
.log-dim { color: #8e9bb1; }
.log-timestamp { color: #6f7c96; }
.log-source {
  display: inline-block;
  min-width: 78px;
  margin-right: 6px;
  color: #9db4d8;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.log-source-webhook { color: #ffca7a; }
.log-source-scheduled { color: #8ed6ff; }
.log-source-manual { color: #c6b5ff; }
.log-source-backfill { color: #91dfa9; }
.log-source-system { color: #8e9bb1; }

/* ─── Tables ─────────────────────────────────────────────────────── */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th, td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--brand);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--panel-soft);
}

/* ─── Pipeline Actions Bar ───────────────────────────────────────── */

.pipeline-actions-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.62);
  backdrop-filter: blur(8px);
}

.run-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.run-controls .check {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.run-controls input[type="date"], .schedule-input {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 10px;
  background: #fff;
  color: var(--ink);
  font-size: 12px;
}

/* ─── Settings Panel ─────────────────────────────────────────────── */

.settings-panel {
  background: transparent;
  border: 0;
  padding-bottom: 0;
}

.settings-grid {
  display: grid;
  gap: 14px;
  padding: 0;
}

.settings-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.86);
  overflow: auto;
}

.settings-card h2 {
  margin: 0;
  padding: 12px 14px;
  color: var(--brand);
  font-size: 14px;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
  background: var(--panel-soft);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 12px;
  padding: 14px;
}

.form-grid.compact {
  grid-template-columns: 180px minmax(260px, 1fr);
  padding: 0;
}

.form-grid label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.form-grid label.wide {
  grid-column: span 2;
}

.form-grid input,
.form-grid select,
.editor-label textarea {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  background: #fff;
  color: var(--ink);
  font-size: 13px;
}

.form-grid input:focus,
.form-grid select:focus,
.editor-label textarea:focus {
  outline: none;
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px var(--brand-glow);
}

.config-editor {
  display: grid;
  gap: 12px;
  padding: 14px;
}

.config-meta {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.editor-label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.editor-label textarea {
  width: 100%;
  min-height: 90px;
  resize: vertical;
  line-height: 1.45;
  text-transform: none;
  letter-spacing: 0;
}

.editor-label .code-editor {
  min-height: 260px;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 12px;
}

.editor-label .prompt-editor {
  min-height: 420px;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 12px;
}

.category-editor {
  display: grid;
  gap: 10px;
}

.category-row,
.rule-set-card,
.mapping-card {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.rule-set-editor,
.mapping-editor {
  display: grid;
  gap: 12px;
}

.mapping-card {
  background: var(--panel-soft);
}

.category-row-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--brand);
  text-transform: none;
  letter-spacing: 0;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(160px, 1fr));
  gap: 10px;
}

.category-grid label {
  display: grid;
  gap: 5px;
}

.category-grid label.wide {
  grid-column: span 3;
}

.category-grid input,
.category-grid select,
.category-grid textarea {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  background: #fff;
  color: var(--ink);
  font-size: 13px;
  text-transform: none;
  letter-spacing: 0;
}

.category-grid textarea {
  min-height: 70px;
  resize: vertical;
  line-height: 1.45;
}

.category-grid input:focus,
.category-grid select:focus,
.category-grid textarea:focus {
  outline: none;
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px var(--brand-glow);
}

.empty-note {
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  background: rgba(255, 255, 255, 0.72);
  text-transform: none;
  letter-spacing: 0;
}

.settings-actions {
  display: flex;
  gap: 8px;
  padding: 0 14px 14px;
}

.sticky-actions {
  position: sticky;
  bottom: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.9);
}

/* ─── Toast ──────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  padding: 12px 16px;
  border-radius: var(--radius);
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  background: var(--brand);
  box-shadow: 0 12px 30px rgba(43, 34, 95, 0.2);
}

/* ─── Login ──────────────────────────────────────────────────────── */

.login-body {
  display: grid;
  place-items: center;
  padding: 18px;
}

.login-card {
  width: min(420px, 100%);
  padding: 32px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  text-align: center;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 22px 70px rgba(43, 34, 95, 0.16);
}

.login-card p {
  color: var(--muted);
}

.login-form {
  display: grid;
  gap: 14px;
  margin-top: 20px;
  text-align: left;
}

.login-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
  font-size: 12px;
}

.login-form input {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
}

.login-form input:focus {
  outline: none;
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px var(--brand-glow);
}

.login-form button {
  color: #fff;
  font-weight: 600;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border: none;
  padding: 11px;
}

.login-error {
  margin-top: 12px;
  border-radius: var(--radius);
  padding: 10px 12px;
  color: #8a1515;
  background: #fff0ef;
}

/* ─── Responsive ─────────────────────────────────────────────────── */

@media (max-width: 820px) {
  .topbar, .tabs { align-items: flex-start; flex-direction: column; height: auto; }
  .top-actions { flex-wrap: wrap; }
  .shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(128px, 32dvh) minmax(0, 1fr);
  }
  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .panel { margin-inline: 10px; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid label.wide { grid-column: span 1; }
  .category-grid { grid-template-columns: 1fr; }
  .category-grid label.wide { grid-column: span 1; }
}
