:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --ink: #1c2430;
  --muted: #6b7585;
  --line: #e6e9ef;
  --line-strong: #d5dae3;
  --accent: #2f6fed;
  --accent-soft: #eaf1fd;
  --ok: #1f9d63;
  --warn: #c28a12;
  --danger: #d14b4b;
  --radius: 14px;
  --font: "Source Sans 3", system-ui, sans-serif;
  --display: "Sora", "Source Sans 3", sans-serif;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --max: 1080px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 80% -10%, rgba(47, 111, 237, 0.08), transparent 55%),
    linear-gradient(180deg, #fbfcfd 0%, var(--bg) 100%);
  background-attachment: fixed;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

code {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 0.9em;
  color: var(--accent);
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.app {
  max-width: var(--max);
  margin: 0 auto;
  padding: 20px 20px 56px;
  min-height: 100vh;
}

/* Header */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 14px;
  background: rgba(255, 255, 255, 0.8);
  border: 1px solid var(--line);
  border-radius: 18px;
  backdrop-filter: blur(12px);
  animation: fade-up 0.5s var(--ease) both;
}

.top-left,
.top-right {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

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

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(145deg, #4b84f5, var(--accent));
}

.brand-version {
  margin: 2px 0 0;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.brand-name {
  margin: 0;
  font-family: var(--display);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.brand-sub {
  margin: 1px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 500;
}

.nav {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: #f1f3f7;
  border-radius: 12px;
}

.nav-item {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.92rem;
  padding: 8px 14px;
  border-radius: 9px;
  transition: background 0.2s, color 0.2s;
}

.nav-item:hover {
  color: var(--ink);
}

.nav-item.is-active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 0 rgba(28, 36, 48, 0.04);
}

.conn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 999px;
  background: #f3f5f8;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #b0b8c6;
}

.conn[data-state="ok"] {
  color: var(--ok);
  background: #eaf7f0;
}

.conn[data-state="ok"] .conn-dot {
  background: var(--ok);
}

.conn[data-state="error"] {
  color: var(--danger);
  background: #fdeeee;
}

.conn[data-state="error"] .conn-dot {
  background: var(--danger);
}

.conn[data-state="busy"] .conn-dot {
  background: var(--warn);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 11px 16px;
  font-weight: 650;
  transition: background 0.2s, border-color 0.2s, color 0.2s, transform 0.15s;
}

.btn:active {
  transform: translateY(1px);
}

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

.btn-sm {
  padding: 7px 12px;
  font-size: 0.85rem;
}

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

.btn-primary:hover {
  background: #255fd6;
}

.btn-secondary {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink);
}

.btn-secondary:hover {
  border-color: #c3cad6;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
}

.btn-ghost:hover {
  color: var(--ink);
  background: #eef1f6;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
}

/* Main */
.main {
  margin-top: 28px;
}

.page-head {
  margin-bottom: 22px;
  animation: fade-up 0.55s var(--ease) both;
}

.page-head h1 {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(1.6rem, 3vw, 2rem);
  font-weight: 700;
  letter-spacing: -0.04em;
}

.page-sub {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.98rem;
}

.view {
  animation: fade-up 0.45s var(--ease) both;
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* Hero */
.hero {
  padding: clamp(28px, 5vw, 44px);
  border: 1px solid var(--line);
  border-radius: 22px;
  background:
    linear-gradient(135deg, rgba(47, 111, 237, 0.06), transparent 42%),
    var(--surface);
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--accent);
  font-family: var(--display);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.hero-title {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(1.9rem, 4.5vw, 2.8rem);
  font-weight: 700;
  letter-spacing: -0.045em;
  line-height: 1.12;
  max-width: 14ch;
}

.hero-lead {
  margin: 14px 0 0;
  max-width: 42ch;
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.55;
}

.hero-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 24px;
}

/* Stats / entities */
.stat-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

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

.stat-label {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.stat-value {
  display: block;
  font-family: var(--display);
  font-size: 0.98rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  word-break: break-all;
  line-height: 1.3;
}

.section-block {
  margin-top: 16px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.section-head h3 {
  margin: 0;
  font-family: var(--display);
  font-size: 1rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.entity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
}

.entity {
  padding: 12px 14px;
  border-radius: 12px;
  background: #f6f8fb;
  border: 1px solid transparent;
}

.entity-name {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.entity-state {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Panel */
.dash-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}

.dash-search {
  flex: 1 1 220px;
}

.dash-search input,
.field input[type="url"],
.field input[type="text"],
.field input[type="password"],
.field select,
#cfgServerSelect {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  background: var(--surface);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  font: inherit;
  color: inherit;
}

.dash-search input:focus,
.field input:focus,
.field select:focus,
#cfgServerSelect:focus {
  border-color: #9bb6f2;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.12);
}

#cfgServerSelect {
  flex: 1;
  min-width: 0;
}

.dash-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex: 2 1 260px;
}

.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.chip:hover {
  color: var(--ink);
}

.chip.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: #c9d9fb;
}

.dash-toolbar-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.dash-status {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

.dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.dash-card {
  display: grid;
  gap: 8px;
  text-align: left;
  min-height: 124px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  color: inherit;
  transition: border-color 0.2s, transform 0.2s var(--ease), background 0.2s;
}

.dash-card.is-action {
  cursor: pointer;
}

.dash-card.is-action:hover {
  transform: translateY(-2px);
  border-color: #c9d3e2;
}

.dash-card.is-on {
  background: var(--accent-soft);
  border-color: #c9d9fb;
}

.dash-card.is-busy {
  opacity: 0.6;
}

.dash-domain {
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.dash-name {
  font-family: var(--display);
  font-size: 0.98rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  line-height: 1.25;
  margin-top: auto;
  word-break: break-word;
}

.dash-state {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.dash-card.is-off .dash-state {
  color: var(--muted);
}

/* Settings */
.settings-form {
  display: grid;
  gap: 14px;
}

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

.span-2 {
  grid-column: 1 / -1;
}

fieldset {
  margin: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
}

legend {
  padding: 0 6px;
  font-family: var(--display);
  font-size: 0.9rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.field {
  display: grid;
  gap: 7px;
  margin-top: 12px;
}

.field:first-of-type {
  margin-top: 2px;
}

.field > span:first-child {
  font-size: 0.88rem;
  font-weight: 650;
}

.hint {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
}

.input-row {
  display: flex;
  gap: 8px;
}

.input-row input {
  flex: 1;
}

.field.check {
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 10px;
}

.field.check input {
  width: 17px;
  height: 17px;
  accent-color: var(--accent);
}

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

.form-status {
  white-space: pre-wrap;
  min-height: 1.3em;
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.form-status.is-ok {
  color: var(--ok);
}

.form-status.is-error {
  color: var(--danger);
}

.help-box {
  padding: 14px 16px;
  border-radius: 14px;
  background: #f1f4f9;
  border: 1px solid var(--line);
}

.help-box h3 {
  margin: 0 0 6px;
  font-family: var(--display);
  font-size: 0.92rem;
}

.help-box h3 + p + h3,
.help-box p + h3 {
  margin-top: 14px;
}

.help-box p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.help-box .code-block {
  margin: 10px 0 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: #e8edf5;
  border: 1px solid var(--line);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.45;
  overflow: auto;
  white-space: pre;
}

.table-wrap {
  overflow: auto;
  max-height: 520px;
  border: 1px solid var(--line);
  border-radius: 12px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.data-table th,
.data-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

.data-table th {
  position: sticky;
  top: 0;
  background: #f3f5f8;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.data-table td:nth-child(n + 3) {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.data-table tr.is-now {
  background: var(--accent-soft);
}

.data-table tr.is-now td {
  color: var(--accent);
}

.elec-now {
  margin-top: 0;
}

.chart-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}

.chart-controls label {
  display: grid;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 650;
  color: var(--muted);
}

.chart-controls select {
  min-width: 150px;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  font: inherit;
  color: var(--ink);
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 4px 0 8px;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}

.legend-item i {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

.chart-box {
  width: 100%;
  min-height: 220px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fcfdff;
  padding: 8px 4px 4px;
  overflow: hidden;
}

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

.flow-wrap {
  width: 100%;
  min-height: 360px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background:
    radial-gradient(circle at 50% 50%, rgba(251, 146, 60, 0.06), transparent 45%),
    #fcfdff;
  padding: 8px;
  overflow: hidden;
}

.flow-svg {
  width: 100%;
  height: auto;
  display: block;
  max-height: 520px;
}

.field input[type="number"] {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  background: var(--surface);
  outline: none;
}

.field input[type="number"]:focus {
  border-color: #9bb6f2;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.12);
}

.mobile-only {
  display: none;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(28, 36, 48, 0.25);
  z-index: 20;
}

@media (max-width: 900px) {
  .top {
    flex-wrap: wrap;
  }

  .nav {
    order: 3;
    width: 100%;
    justify-content: space-between;
  }

  .nav-item {
    flex: 1;
    text-align: center;
  }

  .stat-row,
  .settings-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 640px) {
  .mobile-only {
    display: inline-flex;
  }

  #btnMenu {
    display: none;
  }

  .stat-row,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .top-right .btn-ghost {
    display: none;
  }

  .dash-toolbar-actions {
    margin-left: 0;
    width: 100%;
  }
}
