:root {
  --bg: #0b0f14;
  --surface: #141a22;
  --surface-2: #1b232d;
  --border: #263140;
  --text: #e6edf3;
  --muted: #7c8b9c;
  --accent: #3ddc84;
  --danger: #ef5a5a;
  --warning: #f0a93c;
  --dev: #a78bfa;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.hidden { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent);
}

.brand-logo {
  height: 32px;
  width: 32px;
}

.flow-diagram {
  width: 100%;
  max-width: 460px;
  height: auto;
  margin-top: 1rem;
}

.flow-node circle {
  fill: var(--surface-2);
  stroke: var(--border);
  stroke-width: 1.5;
}

.flow-node text {
  fill: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.flow-node-sub {
  fill: var(--muted) !important;
  font-size: 10px !important;
  font-weight: 400 !important;
}

.flow-line {
  stroke: var(--accent);
  stroke-width: 2.5;
}

.flow-arrowhead {
  fill: var(--accent);
}

.flow-label {
  fill: var(--accent);
  font-size: 11px;
  font-weight: 600;
}

.energyflow-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.stat-label {
  font-size: 0.8rem;
  color: var(--muted);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
}

.stat-sub {
  font-size: 0.75rem;
  color: var(--warning);
}

.consumer-breakdown {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin-top: 0.3rem;
}

.consumer-breakdown-row {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.forecast-section {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
  margin-top: 1.5rem;
}

.forecast-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem;
}

.forecast-card-title {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.6rem;
}

.forecast-sparkline {
  width: 100%;
  height: 60px;
}

.forecast-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}

.forecast-card-values {
  font-size: 0.78rem;
  color: var(--text);
  margin-bottom: 0.4rem;
  min-height: 1.1em;
}

.forecast-card-xaxis {
  display: flex;
  justify-content: space-between;
  margin-top: 0.3rem;
  font-size: 0.7rem;
  color: var(--muted);
}

.forecast-card-source {
  margin-top: 0.6rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.72rem;
  color: var(--muted);
}

.loadmgmt-banner {
  background: rgba(240, 169, 60, 0.12);
  border: 1px solid var(--warning);
  color: var(--warning);
  border-radius: 8px;
  padding: 0.7rem 1rem;
  margin-bottom: 1.25rem;
  font-size: 0.9rem;
  font-weight: 600;
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.9rem;
  border-radius: 6px;
  cursor: pointer;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn-ghost.small { padding: 0.25rem 0.6rem; font-size: 0.8rem; }
.btn-ghost.danger:hover { border-color: var(--danger); color: var(--danger); }

.btn-primary {
  background: var(--accent);
  border: none;
  border-radius: 6px;
  padding: 0.45rem 1rem;
  font-weight: 600;
  color: #06210f;
  cursor: pointer;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-primary.small { padding: 0.25rem 0.6rem; font-size: 0.8rem; }
.btn-primary:disabled {
  background: var(--surface-2);
  color: var(--muted);
  cursor: not-allowed;
  filter: none;
}

.dev-card {
  border-color: var(--dev);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.25rem;
  max-width: 480px;
}
.dev-card h2 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--dev);
}

.btn-dev {
  background: var(--dev);
  border: none;
  border-radius: 6px;
  padding: 0.45rem 1rem;
  font-weight: 600;
  color: #1c1330;
  cursor: pointer;
}
.btn-dev:hover { filter: brightness(1.08); }

.danger-card {
  border-color: var(--danger);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.25rem;
  max-width: 480px;
}
.danger-card h2 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--danger);
}

.btn-danger {
  background: var(--danger);
  border: none;
  border-radius: 6px;
  padding: 0.45rem 1rem;
  font-weight: 600;
  color: #2b0d0d;
  cursor: pointer;
}
.btn-danger:hover { filter: brightness(1.08); }

.loadmgmt-dev-active {
  color: var(--dev);
  font-weight: 600;
}

.device-card-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.8rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}

.wallbox-amp-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.amp-input {
  width: 4.5rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  color: var(--text);
  font-size: 0.85rem;
}
.amp-input:focus { outline: none; border-color: var(--accent); }

.tabs {
  display: flex;
  gap: 0.4rem;
}
.tab-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); background: var(--surface-2); }

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.view.hidden { display: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
}

.login-card {
  max-width: 340px;
  margin: 3rem auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.login-card h1 { margin: 0 0 0.5rem; font-size: 1.3rem; text-align: center; }

.login-logo {
  width: 96px;
  height: 96px;
  margin: 0 auto;
  display: block;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}

input,
select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  color: var(--text);
  font-size: 0.95rem;
}
input:focus,
select:focus { outline: none; border-color: var(--accent); }

button[type="submit"] {
  background: var(--accent);
  border: none;
  border-radius: 6px;
  padding: 0.6rem;
  font-weight: 600;
  color: #06210f;
  cursor: pointer;
}
button[type="submit"]:hover { filter: brightness(1.08); }

.error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 0;
}

.dashboard-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1rem;
}
.dashboard-header h1 { margin: 0; font-size: 1.4rem; }

.header-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.device-form,
.settings-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.25rem;
  max-width: 480px;
}
.device-form h3,
.settings-form h2 { margin: 0; font-size: 1.05rem; }

.device-mgmt-card,
.user-mgmt-card {
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.version-card {
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}
.version-card h2 { margin: 0; font-size: 1.05rem; }

.debug-card {
  max-width: 900px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.debug-toolbar {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-wrap: wrap;
}

.debug-section h3 {
  margin: 0 0 0.4rem;
  font-size: 0.95rem;
}

.debug-output {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.7rem 0.85rem;
  font-family: ui-monospace, monospace;
  font-size: 0.78rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 420px;
  overflow-y: auto;
  margin: 0;
}

.device-mgmt-list,
.user-mgmt-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.device-mgmt-row,
.user-mgmt-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 0.7rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.device-mgmt-info,
.user-mgmt-info {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
}

.device-mgmt-name,
.user-mgmt-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.device-mgmt-meta,
.user-mgmt-meta {
  font-size: 0.78rem;
  color: var(--muted);
}

.device-mgmt-actions,
.user-mgmt-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.form-row {
  display: flex;
  gap: 1rem;
}
.form-row label { flex: 1; }

.checkbox-label {
  flex-direction: row !important;
  align-items: center;
  gap: 0.5rem !important;
}
.checkbox-label input { width: auto; }

.device-config-fields {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.config-field-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.config-field-title {
  font-size: 0.85rem;
  color: var(--muted);
}

.list-rows {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.list-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.list-row input {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  color: var(--text);
  font-size: 0.85rem;
}
.list-row input:focus { outline: none; border-color: var(--accent); }

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

textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  color: var(--text);
  font-family: ui-monospace, monospace;
  font-size: 0.85rem;
  resize: vertical;
}
textarea:focus { outline: none; border-color: var(--accent); }

.form-actions {
  display: flex;
  gap: 0.6rem;
}

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

.device-card-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.8rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}

.entities-card {
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.entities-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.entities-header h2 { margin: 0; font-size: 1.05rem; }

.entities-toolbar {
  display: flex;
  gap: 0.6rem;
  align-items: center;
}

#ha-entity-search {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  color: var(--text);
  font-size: 0.9rem;
}
#ha-entity-search:focus { outline: none; border-color: var(--accent); }

.entity-list {
  max-height: 360px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.entity-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.45rem 0.6rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.entity-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.entity-id {
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  color: var(--accent);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.entity-state {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
}

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

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem;
}

.device-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.6rem;
}

.device-card.collapsed .device-card-head { margin-bottom: 0; }

.device-head-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.collapse-toggle {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.75rem;
  padding: 0.1rem 0.2rem;
  line-height: 1;
}
.collapse-toggle:hover { color: var(--accent); }

.device-summary {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.3rem;
}

.device-power {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--accent);
}

.device-state-text {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.onoff-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  letter-spacing: 0.02em;
}
.onoff-badge.on { background: rgba(61, 220, 132, 0.15); color: var(--accent); }
.onoff-badge.off { background: rgba(124, 139, 156, 0.15); color: var(--muted); }

.device-card.collapsed .device-card-controls {
  margin-top: 0.6rem;
  padding-top: 0;
  border-top: none;
}

.device-name { font-weight: 600; }

.device-meta {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 0.6rem;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 0.4rem;
}
.status-dot.online { background: var(--accent); }
.status-dot.offline { background: var(--danger); }

.device-error {
  color: var(--danger);
  font-size: 0.82rem;
}

.detail-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.detail-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  border-top: 1px solid var(--border);
  padding-top: 0.25rem;
}
.detail-list li:first-child { border-top: none; padding-top: 0; }
.detail-key { color: var(--muted); }
