:root {
  color-scheme: light dark;

  /* Fallback tokens (light) */
  --bg-canvas: #f4f6fb;
  --bg-sidebar: #ffffff;
  --bg-surface: #ffffff;
  --bg-subtle: #eef2f7;
  --bg-code: #0f172a;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --border-subtle: #dbe2ec;
  --accent: #d97706;
  --accent-hover: #b45309;
  --accent-text: #ffffff;
  --emerald: #059669;
  --emerald-bg: rgba(16, 185, 129, 0.14);
  --amber: #d97706;
  --amber-bg: rgba(245, 158, 11, 0.15);
  --rose: #dc2626;
  --rose-bg: rgba(239, 68, 68, 0.14);
  --sky: #0284c7;
  --sky-bg: rgba(14, 165, 233, 0.14);

  @media (prefers-color-scheme: dark) {
    --bg-canvas: #0b0f19;
    --bg-sidebar: #0e1422;
    --bg-surface: #121826;
    --bg-subtle: #1b2336;
    --bg-code: #080b12;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border-subtle: #242e44;
    --accent: #f59e0b;
    --accent-hover: #fbbf24;
    --accent-text: #0b0f19;
    --emerald: #10b981;
    --emerald-bg: rgba(16, 185, 129, 0.14);
    --amber: #f59e0b;
    --amber-bg: rgba(245, 158, 11, 0.15);
    --rose: #f87171;
    --rose-bg: rgba(239, 68, 68, 0.15);
    --sky: #38bdf8;
    --sky-bg: rgba(56, 189, 248, 0.14);
  }

  @supports (color: light-dark(white, black)) {
    --bg-canvas: light-dark(#f4f6fb, #0b0f19);
    --bg-sidebar: light-dark(#ffffff, #0e1422);
    --bg-surface: light-dark(#ffffff, #121826);
    --bg-subtle: light-dark(#eef2f7, #1b2336);
    --bg-code: light-dark(#0f172a, #080b12);
    --text-primary: light-dark(#0f172a, #f8fafc);
    --text-secondary: light-dark(#475569, #94a3b8);
    --border-subtle: light-dark(#dbe2ec, #242e44);
    --accent: light-dark(#d97706, #f59e0b);
    --accent-hover: light-dark(#b45309, #fbbf24);
    --accent-text: light-dark(#ffffff, #0b0f19);
    --emerald: light-dark(#059669, #10b981);
    --amber: light-dark(#d97706, #f59e0b);
    --rose: light-dark(#dc2626, #f87171);
    --sky: light-dark(#0284c7, #38bdf8);
  }

  accent-color: var(--accent);
}

:root[data-theme="dark"] { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-canvas);
  color: var(--text-primary);
  line-height: 1.5;
}

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}

/* App Shell */
.app-shell {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-subtle);
  padding: 1.25rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.25rem 0.5rem;
}

.brand-badge {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, #f59e0b, #ea580c);
  color: #0b0f19;
  display: grid;
  place-items: center;
  font-weight: 800;
  box-shadow: 0 4px 14px rgba(245, 158, 11, 0.35);
}

.brand-name { font-size: 1.08rem; font-weight: 700; letter-spacing: -0.02em; }
.brand-tag { font-size: 0.7rem; color: var(--text-secondary); }

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.nav-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  width: 100%;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background-color 0.15s, color 0.15s;
}

.nav-btn:hover {
  background-color: var(--bg-subtle);
  color: var(--text-primary);
}

.nav-btn[aria-current="page"] {
  background-color: var(--bg-subtle);
  border-color: var(--border-subtle);
  color: var(--text-primary);
  font-weight: 600;
}

.nav-btn[aria-current="page"] .nav-icon {
  color: var(--accent);
}

.nav-left { display: flex; align-items: center; gap: 0.65rem; }
.nav-count {
  font-family: ui-monospace, monospace;
  font-size: 0.72rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--bg-canvas);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
}

.sidebar-footer {
  margin-top: auto;
  padding: 0.75rem;
  border-radius: 10px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-subtle);
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* Main Workspace */
.workspace {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-sidebar);
}

.topbar-title h1 { margin: 0; font-size: 1.2rem; font-weight: 700; letter-spacing: -0.02em; }
.topbar-title p { margin: 0.15rem 0 0; font-size: 0.8rem; color: var(--text-secondary); }
.topbar-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }

.infra-pill {
  font-size: 0.72rem;
  font-family: ui-monospace, monospace;
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-subtle);
  color: var(--text-secondary);
}

.main-content {
  padding: 1.35rem 1.5rem 2.5rem;
  max-width: 1320px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* KPI Strip */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.9rem;
}

.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  padding: 1rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.kpi-label { font-size: 0.78rem; color: var(--text-secondary); font-weight: 500; }
.kpi-row { display: flex; align-items: baseline; gap: 0.6rem; }
.kpi-value { font-size: 1.7rem; font-weight: 700; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 0.74rem; color: var(--text-secondary); }

.gate-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.3rem 0.75rem;
  border-radius: 10px;
  font-family: ui-monospace, monospace;
  font-size: 0.95rem;
  font-weight: 700;
}
.gate-open { background: var(--emerald-bg); color: var(--emerald); border: 1px solid rgba(16,185,129,0.35); }
.gate-paused { background: var(--rose-bg); color: var(--rose); border: 1px solid rgba(239,68,68,0.35); }
.pulse-dot { width: 9px; height: 9px; border-radius: 50%; background: currentColor; box-shadow: 0 0 10px currentColor; }

/* View Grid (Main Panel + Optional Right Lifecycle Sidebar) */
.split-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
  gap: 1.15rem;
  align-items: start;
}

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  padding: 1.2rem 1.3rem;
}

.card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.card-title { margin: 0; font-size: 1.05rem; font-weight: 700; }
.card-desc { margin: 0.25rem 0 0; font-size: 0.82rem; color: var(--text-secondary); }

/* 4-Step Visual Onboarding Stepper */
.stepper {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.5rem;
  margin: 1rem 0 1.25rem;
  padding: 0.75rem;
  border-radius: 12px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-subtle);
}

.step-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.step-num {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.step-done { color: var(--emerald); }
.step-done .step-num { background: var(--emerald-bg); border-color: var(--emerald); color: var(--emerald); }
.step-active { color: var(--accent); }
.step-active .step-num { background: var(--amber-bg); border-color: var(--accent); color: var(--accent); }

/* Status Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  background: var(--bg-subtle);
  color: var(--text-secondary);
  border: 1px solid var(--border-subtle);
  white-space: nowrap;
}
.badge-ok { background: var(--emerald-bg); color: var(--emerald); border-color: rgba(16,185,129,0.3); }
.badge-warn { background: var(--amber-bg); color: var(--amber); border-color: rgba(245,158,11,0.3); }
.badge-err { background: var(--rose-bg); color: var(--rose); border-color: rgba(239,68,68,0.3); }
.badge-info { background: var(--sky-bg); color: var(--sky); border-color: rgba(56,189,248,0.3); }

/* Buttons & Forms */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 38px;
  padding: 0.45rem 0.9rem;
  border-radius: 9px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border-subtle);
  background: var(--bg-subtle);
  color: var(--text-primary);
  transition: opacity 0.15s, background-color 0.15s;
}
.btn:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { min-height: 30px; padding: 0.25rem 0.6rem; font-size: 0.75rem; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-ghost { background: transparent; }
.btn-dashed { background: transparent; border-style: dashed; border-color: var(--accent); color: var(--accent); }
.btn-danger { background: var(--rose); border-color: var(--rose); color: #ffffff; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.form-full { grid-column: 1 / -1; }
label { display: block; font-size: 0.8rem; font-weight: 600; margin-bottom: 0.25rem; }
.field-hint { display: block; font-size: 0.74rem; color: var(--text-secondary); margin-bottom: 0.35rem; }

input, select, textarea {
  width: 100%;
  font: inherit;
  font-size: max(1rem, 0.875rem);
  min-height: 42px;
  padding: 0.5rem 0.75rem;
  border-radius: 9px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-canvas);
  color: var(--text-primary);
}

input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

:is(input, select, textarea):user-invalid { border-color: var(--rose); background: var(--rose-bg); }
:is(input, select, textarea):user-valid { border-color: var(--emerald); }
.field-error { display: none; font-size: 0.76rem; color: var(--rose); margin-top: 0.25rem; }
:is(input, select, textarea):user-invalid + .field-error { display: block; }

/* Responsive Table */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  container-type: inline-size;
  border: 1px solid var(--border-subtle);
  border-radius: 11px;
}

.table-wrapper table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  text-align: left;
  font-size: 0.82rem;
}

.table-wrapper thead {
  position: sticky;
  inset-block-start: 0;
  background: var(--bg-subtle);
  z-index: 1;
}

.table-wrapper th, .table-wrapper td {
  padding: 0.72rem 0.9rem;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: top;
}

.table-wrapper tr:last-child td { border-bottom: none; }
.table-row-hover:hover { background: var(--bg-subtle); cursor: pointer; }

@container (width < 600px) {
  .table-wrapper table.stack-mobile,
  .table-wrapper table.stack-mobile thead,
  .table-wrapper table.stack-mobile tbody,
  .table-wrapper table.stack-mobile tr,
  .table-wrapper table.stack-mobile th,
  .table-wrapper table.stack-mobile td { display: block; }
  .table-wrapper table.stack-mobile thead { display: none; }
  .table-wrapper table.stack-mobile tr { border-bottom: 2px solid var(--border-subtle); padding: 0.5rem 0; }
  .table-wrapper table.stack-mobile td[data-label]::before {
    content: attr(data-label) ": ";
    font-weight: 700;
    color: var(--text-secondary);
  }
}

/* Lifecycle Timeline */
.timeline {
  list-style: none;
  margin: 0;
  padding: 0 0 0 1.25rem;
  border-left: 2px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.timeline-item { position: relative; }
.timeline-dot {
  position: absolute;
  left: -1.62rem;
  top: 0.25rem;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--emerald);
  box-shadow: 0 0 8px var(--emerald);
}
.timeline-dot.warn { background: var(--amber); box-shadow: 0 0 8px var(--amber); }
.timeline-dot.err { background: var(--rose); box-shadow: 0 0 8px var(--rose); }
.timeline-title { font-size: 0.85rem; font-weight: 600; }
.timeline-time { font-size: 0.72rem; font-family: ui-monospace, monospace; color: var(--text-secondary); }

code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.82em; }
pre {
  background: var(--bg-code);
  color: #e2e8f0;
  padding: 0.85rem;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 0.78rem;
  margin: 0.5rem 0 0;
}

.domain-chip {
  font-family: ui-monospace, monospace;
  font-size: 0.85rem;
  padding: 0.25rem 0.65rem;
  border-radius: 7px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-subtle);
  display: inline-block;
}

.banner {
  border-radius: 11px;
  padding: 0.9rem 1.05rem;
  border: 1px solid var(--border-subtle);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.banner-ok { background: var(--emerald-bg); border-color: rgba(16,185,129,0.4); }
.banner-warn { background: var(--amber-bg); border-color: rgba(245,158,11,0.4); }

/* Native Modal Dialogs */
dialog:modal {
  overscroll-behavior: contain;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  padding: 1.5rem;
  max-width: min(36rem, calc(100vw - 2rem));
  width: 100%;
  margin: auto;
}
dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgba(2, 6, 23, 0.7);
  backdrop-filter: blur(3px);
}
.dialog-title { margin: 0; font-size: 1.1rem; font-weight: 700; }
.dialog-desc { margin: 0.5rem 0 1rem; font-size: 0.85rem; color: var(--text-secondary); }
.secret-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem;
  border-radius: 10px;
  background: var(--emerald-bg);
  border: 1px solid rgba(16,185,129,0.4);
}
.secret-box code { word-break: break-all; font-weight: 700; }
.dialog-footer { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.25rem; }

/* Manual Popover Toasts */
[popover].toast-card {
  position: fixed;
  inset: auto 1.25rem 1.25rem auto;
  margin: 0;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 0.8rem 1rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  max-width: 24rem;
}
[popover].toast-card:is(:popover-open, .\:popover-open) {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

@media (max-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .split-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border-subtle); }
  .nav-list { flex-direction: row; overflow-x: auto; }
  .sidebar-footer { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .stepper { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
