/* ---------- Design tokens ---------- */
:root {
  --bg: #0b0d10;
  --bg-elevated: #14171c;
  --bg-elevated-2: #1b1f26;
  --border: #262b33;
  --border-soft: #1e222a;
  --text: #e8eaed;
  --text-muted: #8b93a1;
  --text-faint: #5b6270;
  --accent: #4f8cff;
  --accent-soft: rgba(79, 140, 255, 0.12);
  --good: #33c17f;
  --good-soft: rgba(51, 193, 127, 0.12);
  --bad: #f14c4c;
  --bad-soft: rgba(241, 76, 76, 0.12);
  --warn: #e0a83e;
  --warn-soft: rgba(224, 168, 62, 0.12);
  --radius: 10px;
  --radius-sm: 7px;
  --sidebar-w: 220px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

button, input, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

/* ---------- Layout shell ---------- */
.shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  padding: 20px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 20px;
}

.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  flex-shrink: 0;
}

.brand-name {
  font-weight: 600;
  font-size: 14.5px;
  letter-spacing: 0.2px;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  text-align: left;
  transition: background 0.12s ease, color 0.12s ease;
  position: relative;
}

.nav-item:hover {
  background: var(--bg-elevated-2);
  color: var(--text);
}

.nav-item.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}

.nav-icon {
  font-size: 14px;
  width: 16px;
  text-align: center;
  flex-shrink: 0;
}

.nav-badge {
  margin-left: auto;
  background: var(--bad);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 999px;
}

.conn-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  font-size: 12px;
  color: var(--text-faint);
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
  flex-shrink: 0;
}

.conn-status.is-connected .conn-dot {
  background: var(--good);
}

.conn-status.is-connected .conn-label {
  color: var(--good);
}

.conn-status.is-disconnected .conn-dot {
  background: var(--bad);
}

.conn-status.is-disconnected .conn-label {
  color: var(--bad);
}

.mobile-topbar {
  display: none;
}

.main {
  flex: 1;
  min-width: 0;
  padding: 28px 32px 60px;
}

/* ---------- Views ---------- */
.view {
  display: none;
}

.view.is-active {
  display: block;
  animation: fade-in 0.15s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.view-header h1 {
  font-size: 19px;
  font-weight: 600;
  margin: 0;
}

.view-subtitle {
  color: var(--text-faint);
  font-size: 12.5px;
}

/* ---------- Stat grid ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

.stat-card.is-good .stat-value { color: var(--good); }
.stat-card.is-bad .stat-value { color: var(--bad); }
.stat-card.is-warn .stat-value { color: var(--warn); }

.stat-bar {
  height: 4px;
  border-radius: 999px;
  background: var(--border-soft);
  overflow: hidden;
  margin-top: 2px;
}

.stat-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.4s ease, background 0.2s ease;
}

.stat-bar-fill.is-warn { background: var(--warn); }
.stat-bar-fill.is-bad { background: var(--bad); }

/* ---------- Panel / table ---------- */
.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-soft);
}

.panel-header h2 {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
}

.table-wrap {
  overflow-x: auto;
}

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

.table th {
  text-align: left;
  padding: 10px 18px;
  color: var(--text-faint);
  font-weight: 500;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  border-bottom: 1px solid var(--border-soft);
  white-space: nowrap;
}

.table td {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
  white-space: nowrap;
}

.table tbody tr:last-child td {
  border-bottom: none;
}

.table tbody tr:hover {
  background: var(--bg-elevated-2);
}

.col-actions {
  text-align: right;
}

.empty-state {
  padding: 32px 18px;
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  margin: 0;
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}

.badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.badge-online { background: var(--good-soft); color: var(--good); }
.badge-starting { background: var(--warn-soft); color: var(--warn); }
.badge-error, .badge-critical { background: var(--bad-soft); color: var(--bad); }
.badge-stopped, .badge-offline { background: var(--border-soft); color: var(--text-faint); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--bg-elevated-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
  white-space: nowrap;
}

.btn:hover {
  background: #232833;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: #3d7ce6;
}

.btn-danger {
  background: var(--bad);
  border-color: var(--bad);
  color: #fff;
}

.btn-danger:hover {
  background: #d43f3f;
}

.btn-ghost {
  background: transparent;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

.icon-btn {
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.icon-btn:hover {
  background: var(--bg-elevated-2);
  color: var(--text);
}

.row-actions {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.row-actions button {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-soft);
  background: var(--bg-elevated-2);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  transition: background 0.12s ease, color 0.12s ease;
}

.row-actions button:hover:not(:disabled) {
  background: #232833;
  color: var(--text);
}

.row-actions button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.row-actions button.is-danger:hover:not(:disabled) {
  color: var(--bad);
}

/* ---------- Segmented control ---------- */
.segmented {
  display: inline-flex;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.segmented-item {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 500;
}

.segmented-item.is-active {
  background: var(--accent);
  color: #fff;
}

/* ---------- Select ---------- */
.select {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  min-width: 200px;
}

/* ---------- Log viewer ---------- */
.log-viewer {
  margin: 0;
  padding: 18px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 65vh;
  overflow-y: auto;
}

/* ---------- Settings kv-list ---------- */
.kv-list {
  padding: 4px 18px;
}

.kv-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 11px 0;
  border-bottom: 1px solid var(--border-soft);
  font-size: 13px;
}

.kv-row:last-child {
  border-bottom: none;
}

.kv-key {
  color: var(--text-muted);
}

.kv-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  text-align: right;
  word-break: break-all;
}

.public-url-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

/* ---------- Overlay / Drawer / Modal ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
  animation: fade-in 0.15s ease;
}

.drawer {
  position: fixed;
  top: 0;
  right: -420px;
  width: 420px;
  max-width: 100vw;
  height: 100vh;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  z-index: 50;
  transition: right 0.2s ease;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.drawer.is-open {
  right: 0;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border-soft);
  flex-shrink: 0;
}

.drawer-header h2 {
  font-size: 15px;
  margin: 0;
  font-weight: 600;
}

.drawer-body {
  padding: 18px;
  overflow-y: auto;
  flex: 1;
}

.player-wrap {
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 18px;
  border: 1px solid var(--border-soft);
}

.player {
  width: 100%;
  aspect-ratio: 16 / 9;
  display: block;
  background: #000;
}

.player-status {
  margin: 0;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-faint);
}

.detail-list {
  margin: 0;
  display: grid;
  gap: 0;
}

.detail-list .kv-row {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 12px;
}

.detail-list .kv-value {
  text-align: left;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal[hidden] {
  display: none;
}

.modal-box {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 420px;
  max-width: 100%;
  box-shadow: var(--shadow);
  padding: 4px 0 18px;
  animation: fade-in 0.15s ease;
}

.modal-box-sm {
  width: 380px;
  padding: 18px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px 10px;
}

.modal-header h2 {
  font-size: 15px;
  margin: 0;
  font-weight: 600;
}

.form {
  padding: 4px 18px 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
  color: var(--text-muted);
}

.field input {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  color: var(--text);
}

.field input:focus {
  outline: none;
  border-color: var(--accent);
}

.form-error {
  color: var(--bad);
  font-size: 12.5px;
  margin: 0;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
  padding-top: 4px;
}

/* ---------- Toasts ---------- */
.toast-stack {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 320px;
}

.toast {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: toast-in 0.18s ease;
}

.toast.is-success { border-left-color: var(--good); }
.toast.is-error { border-left-color: var(--bad); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: -240px;
    top: 0;
    height: 100vh;
    z-index: 70;
    transition: left 0.2s ease;
    box-shadow: var(--shadow);
  }

  .sidebar.is-open {
    left: 0;
  }

  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-soft);
    background: var(--bg-elevated);
    position: sticky;
    top: 0;
    z-index: 30;
  }

  .mobile-menu-btn {
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
  }

  .main {
    padding: 20px 16px 48px;
  }

  .drawer {
    width: 100%;
    right: -100%;
  }

  .stat-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 861px) {
  .shell > .overlay-sidebar-trigger {
    display: none;
  }
}

/* ---------- Pantalla de acceso ---------- */
.gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.gate[hidden] {
  display: none;
}

.gate-box {
  width: 340px;
  max-width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.gate-brand {
  justify-content: center;
  padding: 0 0 4px;
}

.gate-box .btn-primary {
  justify-content: center;
  padding: 10px 14px;
}

/* ---------- Estadisticas ---------- */
.stats-row { cursor: pointer; }

.spark-cell { width: 110px; color: var(--accent); }
.spark { display: block; }
.spark-empty, .spark-loading { color: var(--text-faint); }

.stats-chart-wrap { padding: 18px; }

.chart-svg {
  width: 100%;
  height: auto;
  display: block;
}

.chart-grid { stroke: var(--border-soft); stroke-width: 1; }
.chart-ylabel { fill: var(--text-faint); font-size: 10px; text-anchor: end; }
.chart-xlabel { fill: var(--text-faint); font-size: 10px; }

.chart-area { fill: var(--accent-soft); }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }

.chart-caption {
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--text-faint);
  text-align: center;
}

/* ---------- Fuentes de respaldo (details colapsable en el form) ---------- */
.field-collapse {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.field-collapse summary {
  cursor: pointer;
  font-size: 12.5px;
  color: var(--text-muted);
  font-weight: 500;
  list-style: none;
}

.field-collapse summary::-webkit-details-marker { display: none; }

.field-collapse summary::before {
  content: "▸";
  display: inline-block;
  margin-right: 6px;
  transition: transform 0.12s ease;
}

.field-collapse[open] summary::before {
  transform: rotate(90deg);
}

.field-collapse .field-hint {
  margin: 8px 0 12px;
  font-size: 11.5px;
  color: var(--text-faint);
}

.field-collapse .field {
  margin-bottom: 10px;
}

.field-collapse .field:last-child {
  margin-bottom: 0;
}

/* ---------- Fuente activa (panel Ver) ---------- */
.source-slot {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: var(--border-soft);
  color: var(--text-muted);
}

.source-slot.is-active {
  background: var(--good-soft);
  color: var(--good);
}
