:root {
  color-scheme: dark;
  --bg: #080d14;
  --surface: #0d1520;
  --surface-2: #111c29;
  --surface-3: #162333;
  --line: #223244;
  --line-strong: #31465d;
  --text: #f3f7fb;
  --muted: #8fa0b4;
  --muted-2: #66788d;
  --blue: #4a8cff;
  --blue-soft: rgba(74, 140, 255, 0.13);
  --green: #34c985;
  --green-soft: rgba(52, 201, 133, 0.12);
  --amber: #f0ad4e;
  --amber-soft: rgba(240, 173, 78, 0.12);
  --red: #ef6676;
  --red-soft: rgba(239, 102, 118, 0.12);
  --violet: #9c78f3;
  --shadow: 0 18px 54px rgba(0, 0, 0, 0.28);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f6f9;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --surface-3: #edf2f7;
  --line: #dbe3eb;
  --line-strong: #c5d1dd;
  --text: #152231;
  --muted: #617287;
  --muted-2: #7f8da0;
  --blue-soft: rgba(39, 110, 241, 0.1);
  --green-soft: rgba(21, 155, 96, 0.1);
  --amber-soft: rgba(210, 133, 26, 0.12);
  --red-soft: rgba(213, 65, 83, 0.1);
  --shadow: 0 18px 54px rgba(30, 50, 70, 0.1);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body { min-height: 100vh; letter-spacing: 0; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: 0.55; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 242px minmax(0, 1fr); }
.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  padding: 22px 16px; border-right: 1px solid var(--line); background: var(--surface);
  z-index: 20;
}
.sidebar-brand, .brand-lockup { display: flex; align-items: center; gap: 12px; }
.sidebar-brand img, .brand-lockup img {
  object-fit: contain; object-position: center; flex: 0 0 auto;
}
.sidebar-brand div, .brand-lockup div { display: grid; gap: 2px; }
.sidebar-brand strong, .brand-lockup strong { font-size: 17px; }
.sidebar-brand span, .brand-lockup span { color: var(--muted); font-size: 12px; }
.sidebar-close { display: none; margin-left: auto; }
.side-nav { display: grid; gap: 4px; margin-top: 32px; }
.side-nav p { margin: 18px 10px 7px; color: var(--muted-2); font-size: 10px; font-weight: 800; }
.nav-item {
  width: 100%; height: 42px; border: 1px solid transparent; border-radius: 7px;
  padding: 0 10px; display: grid; grid-template-columns: 25px 1fr auto; align-items: center;
  gap: 8px; color: var(--muted); background: transparent; text-align: left; font-weight: 650;
}
.nav-item:hover { color: var(--text); background: var(--surface-2); }
.nav-item.is-active { color: var(--text); border-color: var(--line-strong); background: var(--blue-soft); }
.nav-icon { width: 22px; text-align: center; color: var(--blue); font-weight: 900; }
.nav-item b { min-width: 22px; padding: 2px 6px; border-radius: 999px; color: var(--muted); background: var(--surface-3); font-size: 11px; text-align: center; }
.sidebar-footer {
  margin-top: auto; border: 1px solid var(--line); border-radius: 7px; padding: 11px;
  display: grid; grid-template-columns: 10px 1fr; gap: 9px; align-items: center; background: var(--surface-2);
}
.sidebar-footer div { display: grid; gap: 3px; }
.sidebar-footer strong { font-size: 12px; }
.sidebar-footer small { color: var(--muted); font-size: 10px; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 5px var(--green-soft); }

.workspace { min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 15; height: 76px; padding: 0 28px;
  display: flex; align-items: center; gap: 16px; border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 91%, transparent); backdrop-filter: blur(18px);
}
.topbar h1 { margin: 2px 0 0; font-size: 20px; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 9px; position: relative; }
.last-refresh { color: var(--muted); font-size: 11px; }
.profile-button {
  border: 1px solid var(--line); border-radius: 7px; min-height: 42px; padding: 5px 9px;
  display: flex; align-items: center; gap: 9px; color: var(--text); background: var(--surface);
}
.profile-button > span:first-child {
  width: 29px; height: 29px; border-radius: 6px; display: grid; place-items: center;
  color: #fff; background: var(--blue); font-weight: 800;
}
.profile-button > span:last-child { display: grid; min-width: 95px; text-align: left; }
.profile-button strong { font-size: 12px; overflow: hidden; text-overflow: ellipsis; max-width: 150px; }
.profile-button small { color: var(--muted); font-size: 10px; text-transform: capitalize; }
.profile-menu {
  position: absolute; right: 0; top: 50px; width: 230px; padding: 8px;
  border: 1px solid var(--line-strong); border-radius: 7px; background: var(--surface); box-shadow: var(--shadow);
}
.profile-menu p { padding: 8px; margin: 0; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.profile-menu button { width: 100%; border: 0; border-radius: 6px; padding: 10px; color: var(--red); background: var(--red-soft); text-align: left; }
.menu-toggle { display: none !important; }

.content { padding: 28px; max-width: 1540px; margin: 0 auto; }
.view { display: none; }
.view.is-active { display: block; }
.view-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 22px; }
.view-heading h2 { margin: 4px 0 7px; font-size: clamp(24px, 3vw, 34px); line-height: 1.08; }
.view-heading p:not(.eyebrow) { margin: 0; color: var(--muted); font-size: 14px; }
.eyebrow { margin: 0; color: var(--blue); font-size: 10px; font-weight: 850; letter-spacing: 0.16em; }

.button, .icon-button {
  border: 1px solid var(--line-strong); border-radius: 7px; min-height: 40px;
  color: var(--text); background: var(--surface-2); font-weight: 750;
}
.button { padding: 0 15px; }
.button:hover, .icon-button:hover { border-color: var(--blue); }
.button.primary { border-color: var(--blue); color: #fff; background: #2f73e6; }
.button.secondary { background: var(--surface); }
.button.danger { border-color: var(--red); color: #fff; background: #be4353; }
.button.wide { width: 100%; }
.icon-button { width: 40px; padding: 0; display: inline-grid; place-items: center; font-size: 19px; }
.sidebar .sidebar-close { display: none; }

.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.metric {
  min-height: 126px; padding: 17px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface);
  display: grid; grid-template-columns: 1fr auto; gap: 8px;
}
.metric > span:first-child { color: var(--muted); font-size: 12px; }
.metric strong { grid-column: 1; font-size: 28px; align-self: end; }
.metric small { grid-column: 1; color: var(--muted); }
.metric .metric-icon {
  grid-column: 2; grid-row: 1 / 3; width: 38px; height: 38px; border-radius: 7px;
  display: grid; place-items: center; color: var(--blue); background: var(--blue-soft); font-weight: 900;
}
.metric.is-good .metric-icon { color: var(--green); background: var(--green-soft); }
.metric.is-warning .metric-icon { color: var(--amber); background: var(--amber-soft); }
.metric.is-danger .metric-icon { color: var(--red); background: var(--red-soft); }

.dashboard-grid { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(300px, 0.7fr); gap: 12px; margin-top: 12px; }
.panel { border: 1px solid var(--line); border-radius: 7px; background: var(--surface); overflow: hidden; }
.panel-header { min-height: 62px; padding: 14px 17px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--line); }
.panel-header h3 { margin: 0; font-size: 15px; }
.panel-header span { color: var(--muted); font-size: 11px; }
.health-list, .agent-list, .release-list, .audit-list { display: grid; }
.health-row, .agent-row, .release-row, .audit-row {
  min-height: 58px; padding: 11px 17px; display: grid; align-items: center; gap: 12px;
  border-bottom: 1px solid var(--line);
}
.health-row:last-child, .agent-row:last-child, .release-row:last-child, .audit-row:last-child { border-bottom: 0; }
.health-row { grid-template-columns: 12px minmax(150px, 1fr) 100px 85px 130px; }
.health-row > span:first-child { width: 8px; height: 8px; border-radius: 50%; background: var(--muted-2); }
.health-row > span:first-child.healthy, .health-row > span:first-child.online { background: var(--green); }
.health-row > span:first-child.down { background: var(--red); }
.health-row strong, .agent-row strong, .release-row strong { font-size: 13px; }
.health-row small, .agent-row small, .release-row small, .audit-row small { color: var(--muted); }
.health-row > div, .agent-row > div { min-width: 0; display: grid; gap: 3px; }
.agent-row { grid-template-columns: minmax(150px, 1fr) 90px 115px; }
.release-row { grid-template-columns: minmax(120px, .8fr) 100px 100px minmax(140px, 1fr); }
.audit-row { grid-template-columns: 150px minmax(130px, .8fr) minmax(160px, 1fr); }

.status-pill, .priority-pill {
  width: fit-content; max-width: 100%; padding: 4px 8px; border-radius: 999px; white-space: nowrap;
  font-size: 10px; font-weight: 800; color: var(--muted); background: var(--surface-3);
}
.status-pill.healthy, .status-pill.online, .status-pill.completed { color: var(--green); background: var(--green-soft); }
.status-pill.down, .status-pill.failed, .priority-pill.critical { color: var(--red); background: var(--red-soft); }
.status-pill.in_progress, .status-pill.claimed { color: var(--blue); background: var(--blue-soft); }
.status-pill.queued, .status-pill.blocked, .priority-pill.high { color: var(--amber); background: var(--amber-soft); }

.task-toolbar { margin-bottom: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.filter-button { min-height: 34px; padding: 0 11px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted); background: var(--surface); }
.filter-button.is-active { color: var(--text); border-color: var(--blue); background: var(--blue-soft); }
.task-list { display: grid; gap: 8px; }
.task-row {
  border: 1px solid var(--line); border-radius: 7px; padding: 15px 16px; background: var(--surface);
  display: grid; grid-template-columns: minmax(230px, 1.4fr) 120px 110px minmax(160px, .7fr) 38px; gap: 16px; align-items: center;
}
.task-copy { min-width: 0; }
.task-copy strong { display: block; margin-bottom: 5px; font-size: 14px; }
.task-copy p { margin: 0; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.task-progress { display: grid; grid-template-columns: 1fr 34px; gap: 8px; align-items: center; color: var(--muted); font-size: 11px; }
.task-progress progress { width: 100%; height: 6px; border: 0; border-radius: 99px; overflow: hidden; background: var(--surface-3); }
.task-progress progress::-webkit-progress-bar { background: var(--surface-3); }
.task-progress progress::-webkit-progress-value { background: var(--blue); }
.task-progress progress::-moz-progress-bar { background: var(--blue); }
.table-panel { overflow-x: auto; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); }
.data-table { width: 100%; border-collapse: collapse; min-width: 780px; }
.data-table th, .data-table td { padding: 13px 15px; border-bottom: 1px solid var(--line); text-align: left; font-size: 12px; vertical-align: middle; }
.data-table th { color: var(--muted); font-size: 10px; text-transform: uppercase; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table td strong { display: block; font-size: 13px; }
.data-table td small { color: var(--muted); }
.empty-state, .loading-state {
  min-height: 220px; border: 1px dashed var(--line-strong); border-radius: 7px; display: grid;
  place-items: center; padding: 28px; color: var(--muted); text-align: center;
}

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: var(--bg); }
.login-panel { width: min(100%, 440px); padding: 30px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.login-copy { margin: 48px 0 24px; }
.login-copy h1 { margin: 7px 0 11px; font-size: 30px; }
.login-copy > p:last-child { margin: 0; color: var(--muted); line-height: 1.55; }
.stack-form { display: grid; gap: 14px; }
.stack-form label, .dialog-grid label { display: grid; gap: 7px; }
.stack-form label > span, .dialog-grid label > span { color: var(--muted); font-size: 11px; font-weight: 750; }
input, select, textarea {
  width: 100%; border: 1px solid var(--line-strong); border-radius: 7px; padding: 11px 12px;
  color: var(--text); background: var(--bg); outline: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
textarea { resize: vertical; }
.security-note { margin: 18px 0 0; color: var(--muted-2); font-size: 11px; line-height: 1.5; }
.form-error { margin: 0; padding: 9px 11px; border-radius: 6px; color: var(--red); background: var(--red-soft); font-size: 12px; }

.ops-dialog {
  width: min(94vw, 650px); max-height: 88vh; padding: 0; border: 1px solid var(--line-strong);
  border-radius: 8px; color: var(--text); background: var(--surface); box-shadow: var(--shadow);
}
.ops-dialog::backdrop { background: rgba(2, 7, 12, 0.72); backdrop-filter: blur(5px); }
.ops-dialog form > header, .ops-dialog form > footer { padding: 17px 19px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.ops-dialog form > header { border-bottom: 1px solid var(--line); }
.ops-dialog form > header h2 { margin: 4px 0 0; font-size: 20px; }
.ops-dialog form > footer { border-top: 1px solid var(--line); justify-content: flex-end; }
.dialog-grid { padding: 19px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.dialog-grid .span-2 { grid-column: span 2; }
.ops-dialog .form-error { margin: 0 19px 16px; }
.danger-callout { margin: 18px 19px 0; padding: 12px; border: 1px solid color-mix(in srgb, var(--amber) 40%, var(--line)); border-radius: 7px; display: grid; gap: 3px; background: var(--amber-soft); }
.danger-callout strong { font-size: 12px; }
.danger-callout span { color: var(--muted); font-size: 11px; }

.toast-region { position: fixed; right: 20px; bottom: 20px; z-index: 100; display: grid; gap: 8px; }
.toast { width: min(360px, calc(100vw - 40px)); padding: 12px 14px; border: 1px solid var(--line-strong); border-radius: 7px; background: var(--surface); box-shadow: var(--shadow); font-size: 12px; }
.toast.error { border-color: var(--red); color: var(--red); }

@media (max-width: 1080px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dashboard-grid { grid-template-columns: 1fr; }
  .task-row { grid-template-columns: minmax(200px, 1fr) 110px 110px 38px; }
  .task-row .task-progress { grid-column: 1 / 4; }
  .last-refresh { display: none; }
}

@media (max-width: 760px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; left: 0; transform: translateX(-102%); width: min(86vw, 300px); transition: transform 160ms ease; box-shadow: var(--shadow); }
  .sidebar.is-open { transform: translateX(0); }
  .sidebar-close, .menu-toggle { display: inline-grid !important; }
  .topbar { height: 68px; padding: 0 14px; }
  .topbar .button.secondary, .profile-button > span:last-child { display: none; }
  .profile-button { min-width: 40px; padding: 5px; }
  .content { padding: 20px 14px 40px; }
  .view-heading { align-items: flex-start; flex-direction: column; }
  .view-heading .button { width: 100%; }
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { min-height: 112px; padding: 12px; }
  .metric strong { font-size: 23px; }
  .metric .metric-icon { width: 32px; height: 32px; }
  .health-row { grid-template-columns: 12px minmax(150px, 1fr) 70px; }
  .health-row > :nth-child(4), .health-row > :nth-child(5) { display: none; }
  .task-row { grid-template-columns: 1fr 38px; gap: 10px; }
  .task-row > :not(.task-copy):not(.icon-button):not(.task-progress) { display: none; }
  .task-row .task-progress { grid-column: 1; }
  .dialog-grid { grid-template-columns: 1fr; }
  .dialog-grid .span-2 { grid-column: span 1; }
  .login-panel { padding: 24px 20px; }
}

@media (max-width: 340px) {
  .metric-grid { grid-template-columns: 1fr; }
}

@media (prefers-color-scheme: light) {
  html[data-theme="system"] {
    color-scheme: light;
    --bg: #f3f6f9; --surface: #fff; --surface-2: #f7f9fb; --surface-3: #edf2f7;
    --line: #dbe3eb; --line-strong: #c5d1dd; --text: #152231; --muted: #617287; --muted-2: #7f8da0;
    --blue-soft: rgba(39,110,241,.1); --green-soft: rgba(21,155,96,.1);
    --amber-soft: rgba(210,133,26,.12); --red-soft: rgba(213,65,83,.1); --shadow: 0 18px 54px rgba(30,50,70,.1);
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
