:root {
  --ink-950: #0b1220;
  --ink-900: #111827;
  --ink-700: #334155;
  --field-500: #0369a1;
  --field-400: #0ea5e9;
  --sand-50: #f4f7f5;
  --sand-100: #e8efeb;
  --line: #d7e0da;
  --danger: #b42318;
  --ok: #067647;
  --shadow: 0 18px 40px rgba(11, 18, 32, 0.08);
  --radius: 18px;
  --sidebar: 232px;
}

* { box-sizing: border-box; }

body.app-body,
body.app-login-body {
  margin: 0;
  min-height: 100vh;
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  color: var(--ink-900);
  background:
    radial-gradient(circle at top left, rgba(14, 165, 233, 0.12), transparent 32%),
    linear-gradient(160deg, #f7faf8 0%, #eef3f0 48%, #e7eeea 100%);
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar) 1fr;
  min-height: 100vh;
}

.app-sidebar {
  background: linear-gradient(180deg, #0c4a6e 0%, #0f172a 100%);
  color: #e5e7eb;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  position: sticky;
  top: 0;
  align-self: start;
  width: var(--sidebar);
  height: 100vh;
  height: 100dvh;
  max-height: 100vh;
  max-height: 100dvh;
  min-height: 0;
  overflow: hidden;
  box-sizing: border-box;
}

.sidebar-brand {
  display: flex;
  gap: 0.85rem;
  align-items: center;
  flex-shrink: 0;
}

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  font-family: "Space Grotesk", sans-serif;
  font-weight: 700;
  background: linear-gradient(135deg, var(--field-400), var(--field-500));
  color: #0c4a6e;
}

.sidebar-brand strong,
.login-brand strong,
.app-header h1,
.page-intro h2,
.metric-value {
  font-family: "Space Grotesk", sans-serif;
}

.sidebar-brand small,
.login-brand small,
.eyebrow,
.metric-hint {
  display: block;
  opacity: 0.7;
  font-size: 0.78rem;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1 1 0%;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-right: 0.25rem;
  scrollbar-width: thin;
}

.nav-link {
  color: #cbd5e1;
  text-decoration: none;
  padding: 0.75rem 0.9rem;
  border-radius: 12px;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
  flex-shrink: 0;
}

.nav-link:hover,
.nav-link.is-active {
  background: rgba(14, 165, 233, 0.16);
  color: #fff;
  transform: translateX(2px);
}

.sidebar-foot {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  border-top: 1px solid rgba(255,255,255,0.08);
  padding-top: 1rem;
  margin-top: auto;
  flex-shrink: 0;
}

.user-chip {
  font-size: 0.85rem;
  word-break: break-all;
}

.app-main {
  padding: 1.25rem 1.5rem 2rem;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.app-header h1 {
  margin: 0.15rem 0 0;
  font-size: 1.7rem;
}

.menu-toggle {
  display: none;
  border: 0;
  background: #fff;
  border-radius: 12px;
  width: 42px;
  height: 42px;
  box-shadow: var(--shadow);
}

.app-content {
  animation: rise 0.35s ease;
}

.page-intro {
  margin-bottom: 1rem;
}

.page-intro h2 { margin: 0 0 0.25rem; }
.page-intro p { margin: 0; color: var(--ink-700); }
.muted { color: var(--ink-700); }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.metric-grid.single {
  grid-template-columns: minmax(0, 320px);
}

.metric-card,
.panel-block,
.login-panel,
.state-box {
  background: rgba(255,255,255,0.88);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.metric-card {
  padding: 1.1rem 1.2rem;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}

.metric-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 22px 44px rgba(11, 18, 32, 0.12);
}

.metric-label {
  margin: 0;
  color: var(--ink-700);
  font-size: 0.9rem;
}

.metric-value {
  margin: 0.35rem 0;
  font-size: 1.85rem;
  font-weight: 700;
}

.metric-hint { margin: 0; }

.panel-block {
  padding: 1rem 1.15rem 1.2rem;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.85rem;
}

.panel-head h3 { margin: 0; }
.panel-head a { color: var(--field-500); text-decoration: none; font-weight: 600; }

.table-wrap { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.data-table th,
.data-table td {
  text-align: left;
  padding: 0.75rem 0.65rem;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

.data-table th {
  color: var(--ink-700);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.state-box {
  padding: 1.5rem;
  text-align: center;
}

.state-error { border-color: #fecdca; color: var(--danger); }
.state-empty { color: var(--ink-700); }

.spinner {
  width: 28px;
  height: 28px;
  margin: 0 auto 0.75rem;
  border-radius: 50%;
  border: 3px solid #d1fae5;
  border-top-color: var(--field-500);
  animation: spin 0.8s linear infinite;
}

.toast {
  margin-bottom: 1rem;
  padding: 0.85rem 1rem;
  border-radius: 12px;
  animation: rise 0.3s ease;
}

.toast-error { background: #fef3f2; color: var(--danger); border: 1px solid #fecdca; }
.toast-success { background: #ecfdf3; color: var(--ok); border: 1px solid #abefc6; }
.toast-info { background: #eff8ff; color: #175cd3; border: 1px solid #b2ddff; }
.toast-warning { background: #fffaeb; color: #b54708; border: 1px solid #fedf89; }

.toast-host {
  position: sticky;
  top: 0.75rem;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
  margin: 0 1.25rem;
}

.toast-host .toast {
  pointer-events: auto;
  box-shadow: var(--shadow);
  margin-bottom: 0;
}

.toast-live.is-leaving {
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.offline-banner {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  padding: 0.7rem 1.1rem;
  background: #0c4a6e;
  color: #f8fafc;
  font-size: 0.92rem;
}

.offline-banner[hidden] { display: none !important; }

.live-chip {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #e2e8f0;
  color: #334155;
}

.live-chip::before {
  content: '';
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: currentColor;
}

.live-chip[data-state="connected"] { background: #d1fae5; color: #067647; }
.live-chip[data-state="polling"] { background: #fef3c7; color: #b54708; }
.live-chip[data-state="offline"],
.live-chip[data-state="disconnected"],
.live-chip[data-state="error"] { background: #fee4e2; color: #b42318; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.badge-success { background: #ecfdf3; color: #067647; }
.badge-warning { background: #fffaeb; color: #b54708; }
.badge-danger { background: #fef3f2; color: #b42318; }
.badge-info { background: #eff8ff; color: #175cd3; }
.badge-neutral { background: #f1f5f9; color: #475569; }
.badge-replacement { background: #e0f2fe; color: #0369a1; }

.row-replacement td:first-child {
  box-shadow: inset 3px 0 0 #0ea5e9;
}

.alert-banner {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
  padding: 1rem 1.1rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: #fff;
}

.alert-banner a { color: var(--field-500); font-weight: 600; width: fit-content; }
.alert-warning { border-color: #fedf89; background: #fffaeb; }
.alert-info { border-color: #b2ddff; background: #eff8ff; }

.legend-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.85rem;
}

.skeleton-stack {
  display: grid;
  gap: 0.85rem;
}

.skeleton-card {
  padding: 1rem;
  border-radius: var(--radius);
  background: rgba(255,255,255,0.7);
  border: 1px solid var(--line);
}

.skeleton-line {
  height: 0.7rem;
  border-radius: 999px;
  margin-bottom: 0.55rem;
  background: linear-gradient(90deg, #e2e8f0 0%, #f8fafc 50%, #e2e8f0 100%);
  background-size: 200% 100%;
  animation: shimmer 1.2s ease-in-out infinite;
}

.skeleton-line.w-40 { width: 40%; }
.skeleton-line.w-55 { width: 55%; }
.skeleton-line.w-70 { width: 70%; }

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.btn-primary,
.btn-ghost,
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 12px;
  padding: 0.75rem 1rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

.btn-primary,
.btn-primary {
  background: linear-gradient(135deg, var(--field-400), var(--field-500));
  color: #0c4a6e;
}

.btn-ghost {
  background: transparent;
  color: #cbd5e1;
  border: 1px solid rgba(255,255,255,0.12);
}

.login-stage {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-panel {
  width: min(100%, 420px);
  padding: 1.5rem;
}

.login-brand {
  display: flex;
  gap: 0.85rem;
  align-items: center;
  margin-bottom: 1.25rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.login-form label,
.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.92rem;
}

.login-form input,
.field input {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.8rem 0.9rem;
  font: inherit;
  background: #fff;
}

.login-form em { font-style: normal; opacity: 0.65; font-size: 0.8rem; }

.alert {
  padding: 0.8rem 0.9rem;
  border-radius: 12px;
  margin-bottom: 1rem;
}

.alert-error {
  background: #fef3f2;
  color: var(--danger);
  border: 1px solid #fecdca;
}

.settings-grid {
  display: grid;
  gap: 1rem;
}

.kv-list {
  margin: 0;
  display: grid;
  gap: 0.65rem;
}

.kv-list div {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.5rem;
}

.kv-list dt { color: var(--ink-700); }
.kv-list dd { margin: 0; font-weight: 600; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(11, 18, 32, 0.45);
  display: grid;
  place-items: center;
}

.modal-dialog {
  background: #fff;
  border-radius: var(--radius);
  padding: 1.25rem;
  width: min(92vw, 420px);
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 1100px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, var(--sidebar));
    transform: translateX(-105%);
    transition: transform 0.25s ease;
    z-index: 40;
  }
  .app-shell.is-open .app-sidebar { transform: translateX(0); }
  .menu-toggle { display: inline-grid; place-items: center; }
  .metric-grid { grid-template-columns: 1fr; }
}
