:root {
  --bg: #0a1219;
  --bg-soft: #10202d;
  --panel: #132938;
  --panel-2: #173549;
  --text: #d9ecff;
  --muted: #8fb2cc;
  --accent: #30c67c;
  --accent-2: #f4a524;
  --danger: #ff646a;
  --border: #2c4960;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 10% 0%, #1b3f57 0%, transparent 35%),
    radial-gradient(circle at 90% 20%, #264b65 0%, transparent 40%),
    linear-gradient(160deg, var(--bg) 0%, #071018 100%);
  font-family: "Segoe UI", "Trebuchet MS", sans-serif;
}

.shell {
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px;
}

.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.auth-panel,
.panel,
.metric-card,
.center-card {
  border: 1px solid var(--border);
  background: linear-gradient(165deg, rgba(19, 41, 56, 0.95), rgba(10, 25, 35, 0.96));
  border-radius: 14px;
  box-shadow: 0 14px 30px rgba(2, 8, 12, 0.45);
}

.auth-panel {
  width: min(460px, 100%);
  padding: 24px;
}

.auth-panel h1 {
  margin: 0;
  font-size: 1.6rem;
}

.auth-form {
  margin-top: 16px;
  display: grid;
  gap: 10px;
}

.auth-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.9rem;
}

input {
  background: #081521;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
}

button {
  border: 1px solid transparent;
  background: linear-gradient(120deg, #1ca86a, #2ed58a);
  color: #041007;
  border-radius: 8px;
  font-weight: 700;
  padding: 9px 12px;
  cursor: pointer;
}

button.ghost {
  background: #0c1c28;
  color: var(--text);
  border-color: var(--border);
}

button.danger {
  background: linear-gradient(120deg, #d84a4f, #ff6e73);
  color: #fff3f4;
  border-color: #b84346;
}

button:hover {
  filter: brightness(1.05);
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
}

.err {
  color: var(--danger);
  margin-top: 10px;
}

.sticky {
  margin-bottom: 16px;
}

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

.topbar h1 {
  margin: 0;
  font-size: 1.7rem;
}

.topbar p {
  margin: 5px 0 0;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ws-pill {
  background: #10283b;
  border-color: #35607d;
}

.actions-panel,
.log-panel {
  padding: 14px;
  margin-bottom: 14px;
}

.flow-panel {
  padding: 14px;
  margin-bottom: 14px;
}

.flow-panel h2 {
  margin: 0 0 10px;
  font-size: 1rem;
  color: #b7d7ee;
}

.flow-board {
  position: relative;
  --side-gap: clamp(10px, 2vw, 18px);
  --node-w: clamp(124px, 18vw, 168px);
  --node-h: clamp(96px, 12vw, 120px);
  --inv-w: clamp(154px, 20vw, 190px);
  --inv-h: clamp(108px, 13vw, 128px);
  min-height: clamp(420px, 58vw, 520px);
  border-radius: 14px;
  border: 1px solid #2f5268;
  background:
    radial-gradient(circle at 50% 52%, rgba(22, 116, 163, 0.18), transparent 45%),
    linear-gradient(150deg, #0d1d29 0%, #091823 100%);
  overflow: hidden;
}

.flow-node {
  position: absolute;
  width: var(--node-w);
  min-height: var(--node-h);
  border: 1px solid #32586f;
  border-radius: 12px;
  background: linear-gradient(150deg, rgba(23, 55, 75, 0.95), rgba(13, 31, 44, 0.96));
  padding: clamp(8px, 1vw, 10px);
  display: grid;
  gap: 4px;
  text-align: center;
  box-shadow: 0 10px 20px rgba(4, 10, 14, 0.35);
}

.node-icon {
  font-size: clamp(0.62rem, 0.9vw, 0.74rem);
  color: #88c5ea;
  letter-spacing: 0.09em;
}

.flow-node strong {
  font-size: clamp(0.85rem, 1.2vw, 0.98rem);
}

.flow-node small {
  font-size: clamp(0.82rem, 1.1vw, 0.95rem);
  color: #9dc5de;
}

.node-solar {
  left: 50%;
  top: var(--side-gap);
  transform: translateX(-50%);
}

.node-battery {
  left: var(--side-gap);
  top: 50%;
  transform: translateY(-50%);
}

.node-grid {
  right: var(--side-gap);
  top: 50%;
  transform: translateY(-50%);
}

.node-output {
  left: 50%;
  bottom: var(--side-gap);
  transform: translateX(-50%);
}

.node-inverter {
  left: 50%;
  top: 50%;
  width: var(--inv-w);
  min-height: var(--inv-h);
  transform: translate(-50%, -50%);
  border-color: #3e7ea3;
  background: linear-gradient(155deg, rgba(29, 78, 107, 0.96), rgba(14, 40, 57, 0.98));
  animation: inverter-glow 2.4s ease-in-out infinite;
}

@keyframes inverter-glow {
  0%, 100% { box-shadow: 0 0 0 rgba(73, 164, 214, 0.12), 0 10px 20px rgba(4, 10, 14, 0.35); }
  50% { box-shadow: 0 0 24px rgba(73, 164, 214, 0.28), 0 12px 24px rgba(4, 10, 14, 0.42); }
}

.flow-rail {
  position: absolute;
  --rail-color: #ffce55;
}

.flow-rail .rail-line {
  position: absolute;
  opacity: 0.45;
  background: linear-gradient(180deg, rgba(118, 160, 188, 0.25), rgba(118, 160, 188, 0.75));
}

.flow-rail .rail-arrow {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
}

.flow-rail.idle .rail-arrow {
  display: none;
}

.flow-rail.to-center .rail-line,
.flow-rail.from-center .rail-line {
  opacity: 0.95;
}

.rail-solar {
  left: 50%;
  top: calc(var(--side-gap) + var(--node-h));
  width: 20px;
  height: calc(50% - (var(--inv-h) / 2) - (var(--side-gap) + var(--node-h)));
  transform: translateX(-50%);
}

.rail-solar .rail-line {
  left: 9px;
  top: 0;
  width: 2px;
  height: 100%;
}

.rail-output {
  left: 50%;
  bottom: calc(var(--side-gap) + var(--node-h));
  width: 20px;
  height: calc(50% - (var(--inv-h) / 2) - (var(--side-gap) + var(--node-h)));
  transform: translateX(-50%);
}

.rail-output .rail-line {
  left: 9px;
  top: 0;
  width: 2px;
  height: 100%;
}

.rail-battery {
  left: calc(var(--side-gap) + var(--node-w));
  right: calc(50% + (var(--inv-w) / 2));
  top: 50%;
  height: 20px;
  transform: translateY(-50%);
}

.rail-battery .rail-line {
  left: 0;
  top: 9px;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, rgba(118, 160, 188, 0.75), rgba(118, 160, 188, 0.25));
}

.rail-grid {
  left: calc(50% + (var(--inv-w) / 2));
  right: calc(var(--side-gap) + var(--node-w));
  top: 50%;
  height: 20px;
  transform: translateY(-50%);
}

.rail-grid .rail-line {
  left: 0;
  top: 9px;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, rgba(118, 160, 188, 0.25), rgba(118, 160, 188, 0.75));
}

.flow-rail .a1 { animation-delay: 0s; }
.flow-rail .a2 { animation-delay: 0.4s; }
.flow-rail .a3 { animation-delay: 0.8s; }

.rail-solar .rail-arrow,
.rail-output .rail-arrow {
  left: 4px;
}

.rail-battery .rail-arrow,
.rail-grid .rail-arrow {
  top: 4px;
}

.rail-solar .a1,
.rail-output .a1 { top: 15%; }
.rail-solar .a2,
.rail-output .a2 { top: 45%; }
.rail-solar .a3,
.rail-output .a3 { top: 75%; }

.rail-battery .a1,
.rail-grid .a1 { left: 15%; }
.rail-battery .a2,
.rail-grid .a2 { left: 45%; }
.rail-battery .a3,
.rail-grid .a3 { left: 75%; }

.rail-solar.to-center .rail-arrow {
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 10px solid var(--rail-color);
  animation: move-down 1.3s linear infinite;
}

.rail-solar.from-center .rail-arrow {
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 10px solid var(--rail-color);
  animation: move-up 1.3s linear infinite;
}

.rail-output.from-center .rail-arrow {
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 10px solid #58dd93;
  animation: move-down 1.3s linear infinite;
}

.rail-output.to-center .rail-arrow {
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 10px solid #58dd93;
  animation: move-up 1.3s linear infinite;
}

.rail-battery.to-center .rail-arrow,
.rail-grid.from-center .rail-arrow {
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 10px solid #69d4ff;
  animation: move-right 1.3s linear infinite;
}

.rail-battery.from-center .rail-arrow,
.rail-grid.to-center .rail-arrow {
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 10px solid #69d4ff;
  animation: move-left 1.3s linear infinite;
}

@keyframes move-down {
  0% { transform: translateY(-12px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translateY(12px); opacity: 0; }
}

@keyframes move-up {
  0% { transform: translateY(12px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translateY(-12px); opacity: 0; }
}

@keyframes move-right {
  0% { transform: translateX(-12px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translateX(12px); opacity: 0; }
}

@keyframes move-left {
  0% { transform: translateX(12px); opacity: 0; }
  30% { opacity: 1; }
  100% { transform: translateX(-12px); opacity: 0; }
}

.actions-panel h2,
.log-panel h2 {
  margin: 0 0 10px;
  font-size: 1rem;
  color: #b7d7ee;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.state-pill {
  background: #0b2030;
  border: 1px solid var(--border);
  color: #d6e9f8;
  border-radius: 999px;
  padding: 9px 12px;
  font-size: 0.85rem;
}

.metrics-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.metric-card {
  padding: 12px;
}

.metric-card > header {
  font-weight: 700;
  margin-bottom: 10px;
  color: #d0e7ff;
}

.metric-grid {
  display: grid;
  gap: 8px;
}

.metric-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid #244459;
  border-radius: 8px;
  background: #0d1f2b;
  padding: 8px 10px;
}

.metric-item.metric-item-eta {
  align-items: flex-start;
}

.metric-item.metric-item-eta .metric-value {
  text-align: right;
  max-width: 72%;
  white-space: normal;
  line-height: 1.25;
}

.cell-pills-row {
  display: grid;
  grid-template-columns: repeat(8, minmax(44px, 1fr));
  gap: 6px;
  padding-top: 8px;
  overflow-x: auto;
}

.cell-pill {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #2b4a61;
  border-radius: 999px;
  background: #0c1d29;
  color: #a7cce3;
  font-size: 0.64rem;
  line-height: 1;
  padding: 5px 6px;
  white-space: nowrap;
  text-align: center;
}

.cell-pill-live {
  color: #cbe7ff;
  border-color: #365e79;
}

.cell-pill-alert {
  color: #ffd4d6;
  border-color: #a3474b;
  background: #2a1317;
}

.metric-label {
  color: var(--muted);
}

.metric-value {
  color: #e6f4ff;
}

.metric-value.metric-charge {
  color: #56e18f;
}

.metric-value.metric-discharge {
  color: #ff7d82;
}

.battery-flow-label {
  font-weight: 700;
}

.battery-flow-label.battery-flow-charge {
  color: #56e18f;
}

.battery-flow-label.battery-flow-discharge {
  color: #ff7d82;
}

.log-panel ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 8px;
}

.log-panel li {
  display: grid;
  gap: 4px;
  border: 1px solid #25455b;
  border-radius: 8px;
  background: #0c1d29;
  padding: 8px 10px;
}

.log-panel li span {
  color: var(--muted);
  font-size: 0.82rem;
}

.log-panel li em {
  color: #9ec2da;
  font-style: normal;
}

@media (max-width: 900px) {
  .metrics-layout {
    grid-template-columns: 1fr;
  }

  .topbar {
    flex-direction: column;
  }

  .flow-board {
    min-height: clamp(460px, 95vw, 560px);
  }
}
