:root {
  color-scheme: dark;
  --bg: #090d12;
  --surface: #121923;
  --surface-soft: #17212d;
  --surface-strong: #222e3d;
  --line: #293544;
  --line-strong: #3d4d60;
  --text: #edf3f8;
  --muted: #93a4b8;
  --muted-strong: #b8c5d3;
  --brand: #36c5b5;
  --brand-dark: #22a69a;
  --brand-soft: rgba(54, 197, 181, 0.16);
  --blue: #6aa2ff;
  --blue-soft: rgba(106, 162, 255, 0.16);
  --amber: #f3b75a;
  --amber-soft: rgba(243, 183, 90, 0.15);
  --danger: #ff766d;
  --danger-soft: rgba(255, 118, 109, 0.14);
  --ok: #5ee28f;
  --ok-soft: rgba(94, 226, 143, 0.14);
  --shadow: 0 22px 54px rgba(0, 0, 0, 0.38);
  --shadow-soft: 0 14px 34px rgba(0, 0, 0, 0.24);
  --radius: 8px;
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--text);
  background:
    linear-gradient(135deg, rgba(54, 197, 181, 0.14), transparent 36%),
    linear-gradient(315deg, rgba(106, 162, 255, 0.1), transparent 42%),
    var(--bg);
}

button, input, select { font: inherit; }
button { border: 0; }

.app {
  min-height: 100vh;
}

.hidden { display: none !important; }

.auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
}

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  display: grid;
  place-items: center;
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.22);
  font-weight: 800;
  letter-spacing: -0.04em;
}

.auth-panel-wrap {
  display: grid;
  align-items: center;
  width: min(100%, 520px);
}

.auth-panel {
  width: min(100%, 520px);
  margin: 0 auto;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(18,25,35,0.88);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.auth-panel .auth-brand .brand-mark {
  color: #fff;
  background: var(--brand);
  border-color: transparent;
}

.auth-panel h2,
.section-title h2 {
  margin: 0;
  font-size: 1.18rem;
  line-height: 1.2;
  letter-spacing: -0.025em;
}

.muted {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.5;
}

.form-grid {
  display: grid;
  gap: 14px;
  margin-top: 24px;
}

.field {
  display: grid;
  gap: 7px;
}

.field label {
  color: var(--muted-strong);
  font-size: 0.77rem;
  font-weight: 700;
}

.field input,
.field select,
.settings-popover input,
.settings-popover select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 11px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus,
.field select:focus,
.settings-popover input:focus,
.settings-popover select:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-soft);
}

.field input.mono {
  font-family: "JetBrains Mono", monospace;
  font-size: 0.82rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--muted-strong);
  font-size: 0.86rem;
  font-weight: 600;
}

.check-row input { width: 16px; height: 16px; }

.button {
  min-height: 40px;
  border-radius: var(--radius);
  padding: 0 14px;
  cursor: pointer;
  font-weight: 800;
  font-size: 0.86rem;
  transition: transform 0.13s ease, box-shadow 0.13s ease, background 0.13s ease, color 0.13s ease;
}

.button:hover { transform: translateY(-1px); }
.button:active { transform: translateY(0); }
.button:focus-visible { outline: 3px solid var(--brand-soft); outline-offset: 2px; }

.button.primary {
  color: #fff;
  background: var(--brand);
  box-shadow: 0 10px 24px rgba(54, 197, 181, 0.22);
}

.button.primary:hover { background: var(--brand-dark); }

.button.secondary {
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
}

.button.secondary:hover { border-color: var(--line-strong); box-shadow: var(--shadow-soft); }

.button.ghost {
  min-height: 34px;
  color: var(--muted-strong);
  background: transparent;
}

.button.danger {
  color: var(--danger);
  background: var(--danger-soft);
}

.dashboard {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 20px;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(9, 13, 18, 0.82);
  backdrop-filter: blur(16px);
}

.brand-inline {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-inline .brand-mark {
  color: #fff;
  background: var(--brand);
  border-color: transparent;
  flex: 0 0 auto;
}

.brand-title {
  min-width: 0;
}

.brand-title h1 {
  margin: 0;
  font-size: 1.02rem;
  letter-spacing: -0.02em;
}

.topic-line {
  margin: 2px 0 0;
  color: var(--muted);
  font-family: "JetBrains Mono", monospace;
  font-size: 0.75rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted-strong);
  font-size: 0.82rem;
  font-weight: 800;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--line-strong);
}

.status-pill.ok { color: var(--ok); border-color: rgba(22, 128, 60, 0.25); background: var(--ok-soft); }
.status-pill.ok .status-dot { background: var(--ok); box-shadow: 0 0 0 5px rgba(22, 128, 60, 0.12); }
.status-pill.warn { color: var(--amber); border-color: rgba(200, 121, 18, 0.28); background: var(--amber-soft); }
.status-pill.warn .status-dot { background: var(--amber); box-shadow: 0 0 0 5px rgba(200, 121, 18, 0.12); }
.status-pill.error { color: var(--danger); border-color: rgba(192, 54, 44, 0.25); background: var(--danger-soft); }
.status-pill.error .status-dot { background: var(--danger); box-shadow: 0 0 0 5px rgba(192, 54, 44, 0.12); }

.workspace {
  width: min(1440px, calc(100% - 36px));
  margin: 0 auto;
  padding: 24px 0 38px;
}

.overview {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(340px, 0.8fr);
  gap: 16px;
  margin-bottom: 18px;
}

.hero-panel,
.ops-panel,
.channel-card,
.empty-state {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(18,25,35,0.9);
  box-shadow: var(--shadow-soft);
}

.hero-panel {
  display: grid;
  gap: 18px;
  align-content: space-between;
  min-height: 190px;
  padding: 22px;
  overflow: hidden;
  position: relative;
}

.hero-panel::after {
  content: "";
  position: absolute;
  inset: auto -80px -120px auto;
  width: 280px;
  height: 280px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(54,197,181,0.17), transparent 68%);
  pointer-events: none;
}

.section-title {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.stats-grid {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.stat {
  padding: 12px;
  border-radius: var(--radius);
  background: var(--surface-soft);
  border: 1px solid var(--line);
}

.stat label {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stat strong {
  display: block;
  margin-top: 7px;
  font-size: 1.02rem;
  line-height: 1.1;
}

.ops-panel {
  display: grid;
  gap: 12px;
  padding: 18px;
}

.ops-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 16px 0;
}

.channel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin: 18px 0 12px;
}

.channel-header h2 {
  margin: 0;
  font-size: 1rem;
  letter-spacing: -0.015em;
}

.channel-count {
  color: var(--muted);
  font-family: "JetBrains Mono", monospace;
  font-size: 0.78rem;
  font-weight: 700;
}

.channels {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.channel-card {
  position: relative;
  display: grid;
  gap: 14px;
  min-height: 184px;
  padding: 16px;
  overflow: visible;
  transition: border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

.channel-card[data-settings-open="1"] {
  z-index: 20;
}

.channel-card:hover,
.channel-card:focus-within {
  border-color: var(--line-strong);
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}

.channel-top {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}

.channel-indicator {
  --level: 0%;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 50% 50%, rgba(237,243,248,0.95), rgba(237,243,248,0.28) 34%, transparent 35%),
    conic-gradient(var(--brand) var(--level), var(--surface-strong) 0);
  box-shadow: 0 0 calc(4px + (var(--level-number, 0) * 0.14px)) rgba(54, 197, 181, 0.32);
  border: 1px solid var(--line);
  transition: background 0.25s ease, box-shadow 0.25s ease;
}

.channel-card[data-kind="relay"] .channel-indicator {
  background:
    radial-gradient(circle at 50% 50%, rgba(237,243,248,0.95), rgba(237,243,248,0.26) 34%, transparent 35%),
    conic-gradient(var(--blue) var(--level), var(--surface-strong) 0);
  box-shadow: 0 0 calc(4px + (var(--level-number, 0) * 0.14px)) rgba(106, 162, 255, 0.27);
}

.channel-name {
  min-width: 0;
}

.channel-name strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.98rem;
  letter-spacing: -0.015em;
}

.channel-name span {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.channel-value {
  min-width: 58px;
  text-align: right;
  font-family: "JetBrains Mono", monospace;
  font-size: 1.05rem;
  font-weight: 800;
}

.channel-control {
  display: grid;
  gap: 12px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 7px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--brand) var(--fill, 0%), var(--surface-strong) var(--fill, 0%));
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text);
  border: 3px solid var(--brand);
  box-shadow: 0 4px 12px rgba(54,197,181,0.25);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text);
  border: 3px solid var(--brand);
  cursor: pointer;
}

.quick-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  min-height: 32px;
  min-width: 42px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--muted-strong);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 800;
}

.chip:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }

.relay-actions { display: none; }
.channel-card[data-kind="relay"] .driver-actions,
.channel-card[data-kind="relay"] input[type="range"] { display: none; }
.channel-card[data-kind="relay"] .relay-actions { display: flex; }

.card-tools {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 6px;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
  pointer-events: none;
}

.channel-card:hover .card-tools,
.channel-card:focus-within .card-tools {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.icon-button {
  width: 32px;
  height: 32px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(18,25,35,0.94);
  color: var(--muted-strong);
  cursor: pointer;
  font-weight: 900;
  line-height: 1;
  box-shadow: 0 6px 14px rgba(24,33,45,0.08);
}

.icon-button:hover { color: var(--brand); border-color: var(--brand); }
.icon-button.danger:hover { color: var(--danger); border-color: var(--danger); }

.settings-popover {
  position: absolute;
  z-index: 40;
  top: 48px;
  right: 10px;
  width: min(310px, calc(100vw - 44px));
  display: none;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.channel-card[data-settings-open="1"] .settings-popover {
  display: grid;
}

.settings-popover h3 {
  margin: 0;
  font-size: 0.9rem;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 220px;
  padding: 26px;
  text-align: center;
  color: var(--muted);
}

.add-channel-card {
  min-height: 184px;
  border: 1px dashed var(--line-strong);
  border-radius: 14px;
  background: rgba(18,25,35,0.58);
  color: var(--muted-strong);
  cursor: pointer;
  display: grid;
  place-items: center;
  gap: 8px;
  padding: 20px;
  text-align: center;
  transition: border-color 0.15s ease, color 0.15s ease, transform 0.15s ease, background 0.15s ease;
}

.add-channel-card:hover {
  transform: translateY(-1px);
  border-color: var(--brand);
  background: var(--brand-soft);
  color: var(--text);
}

.add-channel-card strong {
  display: block;
  font-size: 2rem;
  line-height: 1;
}

.empty-state strong {
  display: block;
  margin-bottom: 6px;
  color: var(--text);
  font-size: 1rem;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: none;
  place-items: center;
  padding: 20px;
  background: rgba(15, 23, 42, 0.34);
  backdrop-filter: blur(6px);
}

.modal-backdrop.open { display: grid; }

.modal {
  width: min(100%, 560px);
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 22px;
}

.modal-head {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.modal-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.file-input { display: none; }
.debug-log { display: none; }

@media (max-width: 1080px) {
  .overview { grid-template-columns: 1fr; }
  .channels { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 780px) {
  .topbar { grid-template-columns: 1fr; align-items: start; }
  .top-actions { justify-content: flex-start; }
  .tool-grid { grid-template-columns: 1fr 1fr; }
  .channels { grid-template-columns: 1fr; }
  .section-title { display: grid; }
}

@media (max-width: 520px) {
  .workspace { width: min(100% - 24px, 1440px); }
  .form-row, .stats-grid, .ops-grid, .tool-grid { grid-template-columns: 1fr; }
  .channel-top { grid-template-columns: auto minmax(0, 1fr); }
  .channel-value { grid-column: 1 / -1; text-align: left; }
  .card-tools { opacity: 1; transform: none; pointer-events: auto; }
}