/* ─── Reset & Variables ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #080a12;
  --bg2:       #0c0f1a;
  --surface:   #10131e;
  --surface2:  #171b28;
  --surface3:  #1d2132;
  --border:    #1c2030;
  --border2:   #252b3f;
  --text:      #d4dcee;
  --text2:     #8e98b8;
  --muted:     #4e5870;
  --green:     #22c55e;
  --red:       #f04545;
  --amber:     #f59e0b;
  --blue:      #4f8ef7;
  --purple:    #a855f7;
  --radius:    11px;
  --radius-sm: 7px;
  --font:      'Inter', system-ui, -apple-system, sans-serif;
  --shadow:    0 1px 2px rgba(0,0,0,.5), 0 4px 18px rgba(0,0,0,.3);
  --glow-green: 0 0 0 1px rgba(34,197,94,.18), 0 4px 24px rgba(34,197,94,.07);
  --glow-amber: 0 0 0 1px rgba(245,158,11,.22), 0 4px 24px rgba(245,158,11,.07);
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 2rem;
  max-width: 1180px;
  margin: 0 auto;
  -webkit-font-smoothing: antialiased;
}

/* ─── Scrollbar ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar              { width: 5px; }
::-webkit-scrollbar-track        { background: transparent; }
::-webkit-scrollbar-thumb        { background: var(--border2); border-radius: 3px; }

/* ─── Header ─────────────────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2.25rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.8rem;
}

.header-logo {
  width: 34px; height: 34px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 9px;
  display: grid;
  place-items: center;
  font-size: 1rem;
  flex-shrink: 0;
  box-shadow: var(--shadow);
}

.header-title {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

header h1 {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  color: #e6ecf8;
  line-height: 1;
}

.header-sub {
  font-size: 0.7rem;
  color: var(--muted);
  font-weight: 400;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

/* WS indicator pill */
.ws-indicator {
  display: flex;
  align-items: center;
  gap: 0.38rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.22rem 0.6rem 0.22rem 0.45rem;
  transition: color 0.3s, border-color 0.3s;
}

.ws-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
  transition: background 0.3s, box-shadow 0.3s;
}
.ws-dot.connected    { background: var(--green); box-shadow: 0 0 6px rgba(34,197,94,.7); }
.ws-dot.disconnected { background: var(--red);   box-shadow: 0 0 5px rgba(240,69,69,.6); }
.ws-indicator.connected    { color: var(--text2); border-color: rgba(34,197,94,.2); }
.ws-indicator.disconnected { color: var(--red);   border-color: rgba(240,69,69,.2); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.45; }
}
.ws-dot.connected { animation: pulse 2.8s ease-in-out infinite; }

#clock {
  font-size: 0.8rem;
  color: var(--text2);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.22rem 0.7rem;
  letter-spacing: 0.02em;
}

/* ─── Layout ─────────────────────────────────────────────────────────────── */
main { display: flex; flex-direction: column; gap: 1.75rem; }

/* ─── Groups ─────────────────────────────────────────────────────────────── */
.group { display: flex; flex-direction: column; gap: 0.65rem; }

.group-label {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.11em;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.group-count {
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.68rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.05rem 0.45rem;
  border-radius: 999px;
  color: var(--text2);
}

/* ─── Downstream grid ────────────────────────────────────────────────────── */
#downstreams {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 0.65rem;
}

/* ─── Cards ──────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: border-color 0.25s, box-shadow 0.25s;
}

.card-active {
  border-color: rgba(34,197,94,.22);
  box-shadow: var(--glow-green);
}
.card-warn {
  border-color: rgba(245,158,11,.28);
  box-shadow: var(--glow-amber);
}

/* ── Card head ── */
.card-head {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.8rem 1rem;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}

.node-name {
  font-weight: 700;
  font-size: 0.875rem;
  color: #e4ebf8;
  flex: 1;
  letter-spacing: -0.01em;
}

.card-badges {
  display: flex;
  gap: 0.3rem;
  align-items: center;
  flex-wrap: wrap;
}

/* ── Stats ── */
.card-stats {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.6rem 1rem;
  border-right: 1px solid var(--border);
}
.stat:last-child { border-right: none; }
.stat-wide       { flex: 2; }

.stat-label {
  font-size: 0.61rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stat-value {
  font-size: 0.81rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  word-break: break-all;
}

/* ── Controls ── */
.card-controls {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.control-group {
  flex: 1;
  padding: 0.75rem 1rem;
  border-right: 1px solid var(--border);
}
.control-group:last-child { border-right: none; }

.control-label {
  font-size: 0.61rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

/* ── Advanced footer ── */
.card-advanced {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.5rem 1rem;
  background: var(--bg2);
}

.advanced-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text2);
}

.advanced-hint {
  font-size: 0.65rem;
  color: var(--muted);
  margin-left: auto;
}

/* ── Upstream card ── */
.card-upstream .card-controls {
  display: block;
  border-bottom: none;
}

.url-row {
  display: flex;
  gap: 0.45rem;
  align-items: center;
}

.url-input {
  flex: 1;
  padding: 0.38rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border2);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.8rem;
  font-family: var(--font);
  transition: border-color 0.15s;
}
.url-input:focus        { outline: none; border-color: var(--blue); }
.url-input::placeholder { color: var(--muted); }

/* ─── Badges ─────────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.12rem 0.45rem;
  border-radius: 999px;
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.badge-online,
.badge-on    { background: rgba(34,197,94,.11);  color: var(--green);  border: 1px solid rgba(34,197,94,.22); }
.badge-offline,
.badge-off   { background: rgba(240,69,69,.1);   color: var(--red);    border: 1px solid rgba(240,69,69,.2);  }
.badge-nav   { background: rgba(168,85,247,.11); color: var(--purple); border: 1px solid rgba(168,85,247,.25);}
.badge-sub   { background: rgba(79,142,247,.11); color: var(--blue);   border: 1px solid rgba(79,142,247,.25);}
.badge-warn  { background: rgba(245,158,11,.11); color: var(--amber);  border: 1px solid rgba(245,158,11,.25);}

/* ─── Buttons ────────────────────────────────────────────────────────────── */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.btn {
  cursor: pointer;
  font-size: 0.74rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: opacity 0.15s, transform 0.1s, filter 0.15s;
  white-space: nowrap;
  font-family: var(--font);
  letter-spacing: 0.01em;
  line-height: 1.45;
}
.btn:active               { transform: scale(0.96); }
.btn:hover:not(:disabled) { filter: brightness(1.12); }
.btn:disabled             { opacity: 0.22; cursor: not-allowed; transform: none; }
.btn-sm                   { padding: 0.24rem 0.55rem; font-size: 0.7rem; }

.btn-primary { background: var(--blue);     color: #fff; }
.btn-danger  { background: var(--red);      color: #fff; }
.btn-success { background: var(--green);    color: #06160d; font-weight: 700; }
.btn-ghost   { background: var(--surface2); border-color: var(--border2); color: var(--text); }
.btn-purple  { background: var(--purple);   color: #fff; }
.btn-amber   { background: var(--amber);    color: #160f00; font-weight: 700; }

/* ─── Toggle ─────────────────────────────────────────────────────────────── */
.toggle {
  position: relative;
  display: inline-block;
  width: 30px; height: 16px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute; inset: 0;
  background: var(--border2);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s;
}
.toggle-slider::before {
  content: "";
  position: absolute;
  width: 10px; height: 10px;
  left: 3px; top: 3px;
  background: var(--muted);
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}
.toggle input:checked + .toggle-slider                { background: rgba(245,158,11,.3); }
.toggle input:checked + .toggle-slider::before        { transform: translateX(14px); background: var(--amber); }

/* ─── Connection dot ─────────────────────────────────────────────────────── */
.connected-dot {
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-green {
  background: var(--green);
  box-shadow: 0 0 0 2px rgba(34,197,94,.18);
  animation: pulse 2.8s ease-in-out infinite;
}
.dot-red { background: var(--red); box-shadow: 0 0 5px rgba(240,69,69,.5); }

/* ─── Modal ──────────────────────────────────────────────────────────────── */
dialog {
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  min-width: 340px;
  box-shadow: 0 32px 80px rgba(0,0,0,.75);
  overflow: hidden;
}

dialog::backdrop {
  background: rgba(0,0,0,.72);
  backdrop-filter: blur(6px);
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 1.15rem 1.35rem 0;
  margin-bottom: 1.15rem;
}
.modal-icon { font-size: 1.05rem; }
dialog h3   { font-size: 0.9rem; font-weight: 700; }

dialog form { padding: 1.15rem 1.35rem 1.35rem; }

dialog label {
  display: flex;
  flex-direction: column;
  gap: 0.38rem;
  font-size: 0.62rem;
  color: var(--muted);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.5rem;
}

dialog input[type="time"] {
  padding: 0.48rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--border2);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1rem;
  width: 100%;
  font-variant-numeric: tabular-nums;
  font-family: var(--font);
}
dialog input[type="time"]:focus { outline: none; border-color: var(--blue); }

.hint {
  font-size: 0.73rem;
  color: var(--amber);
  min-height: 1.1em;
  margin-bottom: 1rem;
  line-height: 1.45;
}

dialog menu {
  list-style: none;
  display: flex;
  gap: 0.45rem;
  justify-content: flex-end;
  padding-top: 0.2rem;
}

/* ─── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 680px) {
  body { padding: 1rem; }
  #downstreams { grid-template-columns: 1fr; }
  .card-stats, .card-controls { flex-direction: column; }

  .stat, .control-group {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .stat:last-child, .control-group:last-child { border-bottom: none; }
  .stat-wide { flex: 1; }
  .header-sub { display: none; }
}

.header-left {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

header h1 {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: #fff;
}

.header-sub {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 400;
}

#clock {
  font-size: 0.9rem;
  color: var(--muted2);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.3rem 0.75rem;
}

/* ───────────────────────────────────────────────────────────────────────────
   Layout
─────────────────────────────────────────────────────────────────────────── */
main {
  display: flex;
  flex-direction: column;
  gap: 2.25rem;
}

/* ───────────────────────────────────────────────────────────────────────────
   Groups (section wrappers)
─────────────────────────────────────────────────────────────────────────── */
.group {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.group-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  padding-left: 0.1rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.group-count {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--muted);
}

/* ───────────────────────────────────────────────────────────────────────────
   Cards
─────────────────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color 0.2s;
}

.card-active { border-color: rgba(34, 197, 94, 0.3); }
.card-warn   { border-color: rgba(245, 158, 11, 0.35); }

/* ── Card head (title row) ── */
.card-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.9rem 1.25rem;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}

.node-name {
  font-weight: 700;
  font-size: 0.95rem;
  color: #fff;
  flex: 1;
}

.card-badges {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  flex-wrap: wrap;
}

/* ── Stats row ── */
.card-stats {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
}

.stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.75rem 1.25rem;
  border-right: 1px solid var(--border);
}
.stat:last-child { border-right: none; }
.stat-wide       { flex: 2; }

.stat-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
}

.stat-value {
  font-size: 0.875rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  word-break: break-all;
}

/* ── Controls row ── */
.card-controls {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
}

.control-group {
  flex: 1;
  padding: 0.9rem 1.25rem;
  border-right: 1px solid var(--border);
}
.control-group:last-child { border-right: none; }

.control-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  margin-bottom: 0.55rem;
}

/* ── Advanced/Override footer ── */
.card-advanced {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.65rem 1.25rem;
  background: var(--bg);
}

.advanced-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted2);
}

.advanced-hint {
  font-size: 0.7rem;
  color: var(--muted);
  margin-left: auto;
}

/* ── Upstream card ── */
.card-upstream .card-stats { border-bottom: none; }

/* ───────────────────────────────────────────────────────────────────────────
   Badges
─────────────────────────────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.badge-online,
.badge-on  { background: rgba(34,197,94,.15);  color: var(--green);  border: 1px solid rgba(34,197,94,.3); }
.badge-offline,
.badge-off { background: rgba(239,68,68,.12);  color: var(--red);    border: 1px solid rgba(239,68,68,.25);}
.badge-nav { background: rgba(168,85,247,.15); color: var(--purple); border: 1px solid rgba(168,85,247,.3);}
.badge-sub { background: rgba(79,142,247,.15); color: var(--blue);   border: 1px solid rgba(79,142,247,.3);}
.badge-warn{ background: rgba(245,158,11,.15); color: var(--amber);  border: 1px solid rgba(245,158,11,.3);}

/* ───────────────────────────────────────────────────────────────────────────
   Buttons
─────────────────────────────────────────────────────────────────────────── */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.btn {
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.35rem 0.8rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: opacity 0.15s, transform 0.1s;
  white-space: nowrap;
}
.btn:active   { transform: scale(0.97); }
.btn:hover    { opacity: 0.85; }
.btn:disabled { opacity: 0.28; cursor: not-allowed; transform: none; }

.btn-primary { background: var(--blue);   color: #fff; }
.btn-danger  { background: var(--red);    color: #fff; }
.btn-success { background: var(--green);  color: #000; }
.btn-ghost   { background: var(--surface2); border-color: var(--border2); color: var(--text); }
.btn-purple  { background: var(--purple); color: #fff; }
.btn-amber   { background: var(--amber);  color: #000; }

/* ───────────────────────────────────────────────────────────────────────────
   Toggle switch
─────────────────────────────────────────────────────────────────────────── */
.toggle {
  position: relative;
  display: inline-block;
  width: 34px;
  height: 18px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0;
  background: var(--border2);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s;
}
.toggle-slider::before {
  content: "";
  position: absolute;
  width: 12px; height: 12px;
  left: 3px; top: 3px;
  background: var(--muted2);
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}
.toggle input:checked + .toggle-slider { background: rgba(245,158,11,.3); }
.toggle input:checked + .toggle-slider::before {
  transform: translateX(16px);
  background: var(--amber);
}

/* ───────────────────────────────────────────────────────────────────────────
   Connected dot
─────────────────────────────────────────────────────────────────────────── */
.connected-dot {
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-green { background: var(--green); box-shadow: 0 0 5px var(--green); }
.dot-red   { background: var(--red);   box-shadow: 0 0 5px var(--red); }

/* ───────────────────────────────────────────────────────────────────────────
   Modal / dialog
─────────────────────────────────────────────────────────────────────────── */
dialog {
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  min-width: 340px;
  box-shadow: 0 32px 72px rgba(0,0,0,.65);
  overflow: hidden;
}

dialog::backdrop {
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(4px);
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 1.25rem 1.5rem 0;
  margin-bottom: 1.25rem;
}

.modal-icon { font-size: 1.2rem; }
dialog h3   { font-size: 1rem; font-weight: 600; }

dialog form {
  padding: 1.25rem 1.5rem 1.5rem;
}

dialog label {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.78rem;
  color: var(--muted2);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.5rem;
}

dialog input[type="time"] {
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border2);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1.05rem;
  width: 100%;
  font-variant-numeric: tabular-nums;
}
dialog input[type="time"]:focus {
  outline: none;
  border-color: var(--blue);
}

.hint {
  font-size: 0.78rem;
  color: var(--amber);
  min-height: 1.1em;
  margin-bottom: 1rem;
  line-height: 1.4;
}

dialog menu {
  list-style: none;
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  padding-top: 0.25rem;
}

/* ───────────────────────────────────────────────────────────────────────────
   Responsive
─────────────────────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
  body { padding: 1rem; }

  .card-stats,
  .card-controls { flex-direction: column; }

  .stat, .control-group {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .stat:last-child,
  .control-group:last-child { border-bottom: none; }

  .stat-wide { flex: 1; }
}