:root {
  --ink: #0c1411;
  --ink-2: #24312c;
  --mute: #5c6b64;
  --line: rgba(12, 20, 17, 0.12);
  --accent: #1a6b52;
  --accent-2: #239671;
  --sun: #c4a035;
  --copper: #b45324;
  --paper: #e7eee9;
  --panel: rgba(255, 255, 255, 0.78);
  --display: "Bricolage Grotesque", system-ui, sans-serif;
  --body: "Figtree", system-ui, sans-serif;
  --ease: cubic-bezier(0.2, 0.9, 0.2, 1);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--body);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }

.bg { position: fixed; inset: 0; z-index: 0; pointer-events: none; }
.bg-wash {
  position: absolute; inset: 0;
  background:
    radial-gradient(900px 500px at 0% 0%, rgba(26, 107, 82, 0.2), transparent 55%),
    radial-gradient(700px 480px at 100% 100%, rgba(180, 83, 36, 0.14), transparent 50%),
    linear-gradient(160deg, #f3f7f4 0%, #dde8e1 48%, #d5e0d9 100%);
}
.bg-grain {
  position: absolute; inset: 0; opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.45'/%3E%3C/svg%3E");
  mix-blend-mode: multiply;
}

.screen { position: relative; z-index: 1; min-height: 100vh; }
.logo {
  margin: 0;
  font-family: var(--display);
  font-weight: 800;
  font-size: clamp(2.8rem, 7vw, 4.5rem);
  letter-spacing: -0.045em;
  line-height: 0.9;
}
.logo.sm { font-size: 1.35rem; letter-spacing: -0.03em; }

/* LOGIN */
.login { display: grid; grid-template-columns: 1.1fr 0.9fr; min-height: 100vh; }
.login-panel {
  display: flex; flex-direction: column; justify-content: center;
  padding: clamp(2rem, 6vw, 5rem);
  animation: in 0.7s var(--ease) both;
}
.login-panel h1 {
  margin: 1.4rem 0 0.8rem;
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(2rem, 4.5vw, 3.2rem);
  line-height: 1.05;
  letter-spacing: -0.03em;
  max-width: 11ch;
}
.lede { margin: 0 0 2rem; color: var(--mute); font-size: 1.08rem; max-width: 34ch; line-height: 1.5; }
#login-form { display: grid; gap: 0.85rem; max-width: 320px; }
#login-form label { display: grid; gap: 0.35rem; }
#login-form label span {
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--mute);
}
#login-form input {
  border: 1.5px solid var(--line); background: var(--panel);
  border-radius: 10px; padding: 0.95rem 1rem; font: inherit; font-size: 1.05rem;
}
#login-form input:focus { outline: 2px solid rgba(26, 107, 82, 0.35); border-color: var(--accent); }
#login-btn {
  border: 0; border-radius: 10px; padding: 1rem 1.1rem;
  background: var(--ink); color: #eef5f1; font: inherit; font-weight: 700; cursor: pointer;
}
#login-btn:hover { background: var(--accent); }
#login-btn:disabled { opacity: 0.6; cursor: wait; }
.error { margin: 0; color: var(--copper); font-weight: 600; }
.login-aside {
  display: grid; place-items: center; padding: 2rem;
  background: linear-gradient(145deg, rgba(12, 20, 17, 0.92), rgba(26, 107, 82, 0.75));
  color: #e8f2ec;
}
.aside-battery {
  width: min(160px, 40vw); height: min(280px, 55vh);
  border: 3px solid rgba(255, 255, 255, 0.35); border-radius: 28px;
  padding: 10px; position: relative;
}
.aside-battery::before {
  content: ""; position: absolute; top: -14px; left: 50%; transform: translateX(-50%);
  width: 48px; height: 12px; border-radius: 6px 6px 0 0; background: rgba(255, 255, 255, 0.35);
}
.aside-fill {
  position: absolute; left: 10px; right: 10px; bottom: 10px; height: 62%;
  border-radius: 18px;
  background: linear-gradient(180deg, #2f9b78, #c4a035 70%, #b45324);
  animation: fillPulse 4s var(--ease) infinite alternate;
}
.login-aside p {
  margin-top: 1.5rem; font-size: 0.85rem; letter-spacing: 0.12em;
  text-transform: uppercase; opacity: 0.7;
}
@keyframes fillPulse { from { height: 48%; } to { height: 72%; } }
@keyframes in {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: none; }
}

/* CONSOLE CHROME */
.console { display: flex; flex-direction: column; min-height: 100vh; }
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.85rem clamp(1.2rem, 3vw, 2.4rem);
  border-bottom: 1px solid rgba(12, 20, 17, 0.06);
  animation: in 0.55s var(--ease) both;
}
.bar-left, .bar-right { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }

.nav {
  display: flex; gap: 0.2rem;
  padding: 0.2rem;
  background: rgba(12, 20, 17, 0.05);
  border-radius: 10px;
}
.nav-item {
  border: 0; background: transparent;
  padding: 0.55rem 0.95rem;
  border-radius: 8px;
  font: inherit; font-size: 0.9rem; font-weight: 650;
  color: var(--mute); cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}
.nav-item:hover { color: var(--ink); }
.nav-item.is-active {
  background: var(--ink);
  color: #eaf3ee;
}

.conn { font-size: 0.82rem; font-weight: 700; color: var(--mute); }
.conn::before {
  content: ""; display: inline-block; width: 8px; height: 8px;
  margin-right: 0.4rem; border-radius: 50%; background: currentColor;
}
.conn.on { color: var(--accent-2); }
.conn.bad { color: var(--copper); }
#clock { font-variant-numeric: tabular-nums; font-size: 0.9rem; color: var(--ink-2); }
.ghost {
  border: 0; background: none; font: inherit; font-weight: 650;
  color: var(--mute); cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}

.stage {
  flex: 1;
  padding: 1rem clamp(1.2rem, 3vw, 2.4rem);
  animation: in 0.65s var(--ease) both;
}
.page { display: none; }
.page.is-active { display: grid; }

/* LIVE MAP */
#page-live {
  grid-template-columns: 1fr 260px;
  gap: 1rem;
  align-items: stretch;
  min-height: calc(100vh - 7rem);
}

.map {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1.2fr 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    ". prod ."
    "grid hub load"
    ". ess .";
  gap: 0.7rem 1rem;
  padding: 1.1rem;
  border-radius: 28px;
  background: linear-gradient(165deg, rgba(255, 255, 255, 0.6), rgba(255, 255, 255, 0.25));
  border: 1px solid rgba(12, 20, 17, 0.08);
  overflow: hidden;
  min-height: 560px;
}

.wires {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 0;
}
.flow-line {
  fill: none;
  stroke: rgba(12, 20, 17, 0.12);
  stroke-width: 3.5;
  stroke-linecap: round;
  transition: stroke 0.3s ease, opacity 0.3s ease;
  opacity: 0.35;
}
.flow-line.active {
  opacity: 1;
  stroke-width: 4.5;
  stroke-dasharray: 12 14;
  animation: march 1.4s linear infinite;
}
.flow-line.active.green { stroke: #1a6b52; marker-end: url(#arrow-green); }
.flow-line.active.copper { stroke: #b45324; marker-end: url(#arrow-copper); }
.flow-line.active.sun { stroke: #c4a035; marker-end: url(#arrow-sun); }
.flow-line.active.ink { stroke: #0c1411; marker-end: url(#arrow-ink); }
.flow-line.active.reverse { animation-direction: reverse; }

@keyframes march { to { stroke-dashoffset: -52; } }

.packet {
  fill: #1a6b52;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.packet.on { opacity: 1; }
.packet.copper { fill: #b45324; }
.packet.sun { fill: #c4a035; }
.packet.ink { fill: #0c1411; }

.node {
  position: relative;
  z-index: 1;
  background: var(--panel);
  border: 1px solid rgba(12, 20, 17, 0.1);
  border-radius: 20px;
  padding: 1.1rem 1.2rem;
  backdrop-filter: blur(8px);
}
.node.active-src {
  border-color: rgba(26, 107, 82, 0.45);
  box-shadow: 0 0 0 3px rgba(26, 107, 82, 0.08);
}
.n-prod { grid-area: prod; justify-self: center; min-width: min(260px, 100%); text-align: center; }
.n-grid { grid-area: grid; align-self: center; }
.n-hub {
  grid-area: hub; align-self: center; text-align: center;
  background: var(--ink); color: #eaf3ee; border-color: transparent;
  padding: 1.5rem 1.25rem;
}
.n-load { grid-area: load; align-self: center; }
.n-ess { grid-area: ess; justify-self: center; width: min(420px, 100%); }

.dir-tag {
  display: inline-block;
  margin-bottom: 0.45rem;
  padding: 0.28rem 0.5rem;
  border-radius: 6px;
  font-size: 0.68rem;
  font-weight: 750;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: rgba(12, 20, 17, 0.06);
  color: var(--mute);
}
.dir-tag.on-green { background: rgba(26, 107, 82, 0.12); color: var(--accent); }
.dir-tag.on-copper { background: rgba(180, 83, 36, 0.12); color: var(--copper); }
.dir-tag.on-sun { background: rgba(196, 160, 53, 0.16); color: #8a6d12; }
.dir-tag.on-ink { background: rgba(12, 20, 17, 0.1); color: var(--ink); }

.k {
  margin: 0 0 0.15rem;
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--mute);
}
.n-hub .k { color: rgba(234, 243, 238, 0.55); }
.v {
  margin: 0;
  font-family: var(--display);
  font-weight: 750;
  font-size: clamp(1.55rem, 2.8vw, 2.2rem);
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}
.v.huge { font-size: clamp(2.2rem, 4.5vw, 3.3rem); }
.h { margin: 0.3rem 0 0; color: var(--mute); font-size: 0.9rem; }
.n-hub .h { color: rgba(234, 243, 238, 0.65); }

.src-pills {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 0.4rem;
  margin-top: 1rem;
}
.src-pill {
  padding: 0.35rem 0.55rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  background: rgba(255, 255, 255, 0.08);
  color: #cfe6da;
  border: 1px solid rgba(255, 255, 255, 0.12);
}
.src-pill.ess { color: #f0c4a8; border-color: rgba(180, 83, 36, 0.35); }
.src-pill.grid { color: #a8dfc8; border-color: rgba(35, 150, 113, 0.35); }
.src-pill.prod { color: #ead48a; border-color: rgba(196, 160, 53, 0.35); }

.ess-row { display: flex; align-items: center; gap: 1rem; }
.batt { display: flex; flex-direction: column; align-items: center; width: 58px; }
.batt-cap {
  width: 24px; height: 7px; border-radius: 3px 3px 0 0;
  background: var(--ink-2); margin-bottom: 3px;
}
.batt-body {
  width: 52px; height: 92px; border-radius: 10px;
  border: 3px solid var(--ink-2); position: relative; overflow: hidden;
  background: rgba(12, 20, 17, 0.06);
}
.batt-fill {
  position: absolute; left: 3px; right: 3px; bottom: 3px; height: 45%;
  border-radius: 6px;
  background: linear-gradient(180deg, var(--accent-2), var(--copper));
  transition: height 0.8s var(--ease);
}

.legend-panel {
  background: var(--panel);
  border: 1px solid rgba(12, 20, 17, 0.1);
  border-radius: 20px;
  padding: 1.15rem 1.2rem;
}
.legend-panel h2 {
  margin: 0 0 0.85rem;
  font-family: var(--display);
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}
.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.7rem; }
.legend li {
  display: grid; grid-template-columns: 14px 1fr; gap: 0.65rem;
  align-items: start; font-size: 0.88rem; color: var(--ink-2); line-height: 1.35;
}
.swatch {
  width: 12px; height: 12px; border-radius: 3px; margin-top: 0.25rem;
}
.swatch.green { background: var(--accent); }
.swatch.copper { background: var(--copper); }
.swatch.sun { background: var(--sun); }
.swatch.ink { background: var(--ink); }
.legend-note {
  margin: 1rem 0 0;
  font-size: 0.8rem;
  color: var(--mute);
  line-height: 1.45;
}

/* MIX PAGE */
.mix-layout, .plain-page { max-width: 980px; }
.page-head h1 {
  margin: 0;
  font-family: var(--display);
  font-size: clamp(1.6rem, 3vw, 2.1rem);
  letter-spacing: -0.03em;
}
.page-head p { margin: 0.4rem 0 1.4rem; color: var(--mute); max-width: 46ch; }

.mix-bar-wrap { margin-bottom: 1.25rem; }
.mix-bar {
  display: flex; height: 28px; border-radius: 10px; overflow: hidden;
  background: rgba(12, 20, 17, 0.08);
}
.mix-seg { height: 100%; transition: width 0.6s var(--ease); min-width: 0; }
.mix-seg.ess { background: var(--copper); }
.mix-seg.grid { background: var(--accent); }
.mix-seg.prod { background: var(--sun); }
.mix-labels {
  display: flex; flex-wrap: wrap; gap: 1rem;
  margin-top: 0.75rem; font-size: 0.85rem; color: var(--mute);
}
.mix-labels b { color: var(--ink); }

.mix-cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
}
.mix-card {
  background: var(--panel);
  border: 1px solid rgba(12, 20, 17, 0.1);
  border-radius: 16px;
  padding: 1rem 1.1rem;
}
.mix-card.dark { background: var(--ink); color: #eaf3ee; border-color: transparent; }
.mix-card.dark .k, .mix-card.dark .h { color: rgba(234, 243, 238, 0.55); }
.sync-line {
  margin: 1.25rem 0 0;
  font-size: 0.92rem;
  font-weight: 650;
  color: var(--ink-2);
  padding: 0.85rem 1rem;
  border-radius: 12px;
  background: rgba(26, 107, 82, 0.08);
  border: 1px solid rgba(26, 107, 82, 0.15);
}

.kv {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.65rem;
  margin: 0;
}
.kv > div {
  background: var(--panel);
  border: 1px solid rgba(12, 20, 17, 0.1);
  border-radius: 14px;
  padding: 0.95rem 1.05rem;
}
.kv dt {
  margin: 0 0 0.25rem;
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.1em;
  text-transform: uppercase; color: var(--mute);
}
.kv dd { margin: 0; font-family: var(--display); font-weight: 700; font-size: 1.1rem; }

.alarm-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.65rem; }
.alarm {
  display: grid; grid-template-columns: 12px 1fr; gap: 0.85rem;
  background: var(--panel); border: 1px solid rgba(12, 20, 17, 0.1);
  border-radius: 14px; padding: 1rem 1.1rem;
}
.alarm strong { display: block; margin-bottom: 0.2rem; }
.alarm p { margin: 0; color: var(--mute); font-size: 0.92rem; line-height: 1.4; }
.alarm-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 0.35rem; background: var(--accent-2); }

.foot {
  display: flex; justify-content: space-between;
  padding: 0.35rem clamp(1.2rem, 3vw, 2.4rem) 1rem;
  font-size: 0.8rem; color: var(--mute);
}

/* CONFIG */
.config-page { max-width: 920px; }
.config-form { display: grid; gap: 1rem; margin-bottom: 1.25rem; }
.cfg-block {
  margin: 0;
  border: 1px solid rgba(12, 20, 17, 0.1);
  border-radius: 16px;
  background: var(--panel);
  padding: 1rem 1.15rem 1.15rem;
}
.cfg-block legend {
  padding: 0 0.35rem;
  font-family: var(--display);
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.01em;
}
.cfg-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
.cfg-field {
  display: grid;
  gap: 0.35rem;
}
.cfg-field.wide { grid-column: 1 / -1; }
.cfg-field span {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--mute);
}
.cfg-field input,
.cfg-field select {
  border: 1.5px solid var(--line);
  background: rgba(255, 255, 255, 0.75);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  font: inherit;
  font-size: 0.98rem;
  color: var(--ink);
}
.cfg-field input:focus,
.cfg-field select:focus {
  outline: 2px solid rgba(26, 107, 82, 0.3);
  border-color: var(--accent);
}
.cfg-hint {
  margin: 0.75rem 0 0;
  font-size: 0.82rem;
  color: var(--mute);
  line-height: 1.45;
}
.cfg-hint code {
  font-size: 0.78rem;
  color: var(--ink-2);
}
.cfg-notice {
  margin-bottom: 1.25rem;
  padding: 1rem 1.1rem;
  border-radius: 12px;
  border: 1px solid rgba(180, 83, 36, 0.25);
  background: rgba(180, 83, 36, 0.06);
}
.cfg-notice strong { display: block; margin-bottom: 0.35rem; }
.cfg-notice p { margin: 0.35rem 0 0; font-size: 0.88rem; color: var(--mute); line-height: 1.45; }
.cfg-notice code { font-size: 0.82rem; color: var(--ink-2); }
.cfg-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}
.cfg-actions button {
  border: 0;
  border-radius: 10px;
  padding: 0.85rem 1.1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.btn-save { background: var(--ink); color: #eaf3ee; }
.btn-save:hover { background: var(--accent); }
.btn-apply { background: var(--accent); color: #eaf3ee; }
.btn-apply:hover { background: var(--accent-2); }
.btn-test {
  background: rgba(12, 20, 17, 0.06);
  color: var(--ink);
  border: 1px solid rgba(12, 20, 17, 0.12) !important;
}
.cfg-status {
  margin: 0.25rem 0 0;
  min-height: 1.25rem;
  font-size: 0.9rem;
  font-weight: 650;
  color: var(--ink-2);
}
.cfg-status.ok { color: var(--accent); }
.cfg-status.err { color: var(--copper); }
.cfg-status-kv { margin-top: 0.5rem; }

@media (max-width: 980px) {
  #page-live { grid-template-columns: 1fr; }
  .mix-cards { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 900px) {
  .login { grid-template-columns: 1fr; }
  .login-aside { min-height: 200px; }
  .map {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "prod prod"
      "hub hub"
      "grid load"
      "ess ess";
    min-height: auto;
  }
  .wires { opacity: 0.4; }
  .cfg-grid { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .nav { width: 100%; overflow-x: auto; }
  .map {
    grid-template-columns: 1fr;
    grid-template-areas: "prod" "hub" "grid" "load" "ess";
  }
  .mix-cards, .kv { grid-template-columns: 1fr; }
  .bar { align-items: flex-start; flex-direction: column; }
}
