:root {
  color-scheme: light;
  --ink: #17211c;
  --muted: #5c6b62;
  --paper: #f3f6f2;
  --card: #fffcf8;
  --line: #d7e0d8;
  --green: #146b43;
  --green-deep: #0e3d2c;
  --amber: #9a5b12;
  --shadow: 0 16px 40px rgba(20, 40, 30, 0.06);
  --field: #ffffff;
  --soft: #e7efe8;
  --soft-2: #f4f7f4;
  --glow: #d9efe3;
  --danger: #8d2b2b;
  --pill: #f3ece3;
  --on-green: #f4fff8;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
}

html[data-theme="light"] { color-scheme: light; }

html[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e7f3ec;
  --muted: #a9bdb2;
  --paper: #101916;
  --card: #1b2822;
  --line: #31443b;
  --green: #8ed4ae;
  --green-deep: #146b43;
  --amber: #e7c08a;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.38);
  --field: #121c18;
  --soft: #24342c;
  --soft-2: #20302a;
  --glow: rgba(46, 120, 78, 0.45);
  --danger: #ffb4ae;
  --pill: #3a2c20;
}

@media (prefers-color-scheme: dark) {
  html[data-theme="system"] {
    color-scheme: dark;
    --ink: #e7f3ec;
    --muted: #a9bdb2;
    --paper: #101916;
    --card: #1b2822;
    --line: #31443b;
    --green: #8ed4ae;
    --green-deep: #146b43;
    --amber: #e7c08a;
    --shadow: 0 16px 40px rgba(0, 0, 0, 0.38);
    --field: #121c18;
    --soft: #24342c;
    --soft-2: #20302a;
    --glow: rgba(46, 120, 78, 0.45);
    --danger: #ffb4ae;
    --pill: #3a2c20;
  }
}

html[lang="th"] {
  font-family: Thonburi, "Noto Sans Thai", "Avenir Next", "Segoe UI", sans-serif;
}

html[lang="th"] svg text {
  font-family: Thonburi, "Noto Sans Thai", "Avenir Next", sans-serif;
}

html[lang="th"] .kicker {
  letter-spacing: 0;
}

* { box-sizing: border-box; }

html { background: var(--paper); }

input:not([type="radio"]):not([type="checkbox"]):not([type="range"]),
select,
textarea {
  color: var(--ink);
  background: var(--field);
}

input::placeholder { color: var(--muted); opacity: 1; }

input[type="radio"],
input[type="checkbox"] { accent-color: var(--green); }

[hidden] { display: none !important; }

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 10% -10%, var(--glow), transparent 60%),
    var(--paper);
}

.login[hidden],
#app[hidden] {
  display: none;
}

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

.login-card {
  width: min(440px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px;
  box-shadow: var(--shadow);
}

.login h1 { max-width: none; font-size: 40px; }

.field {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  margin: 14px 0;
  font-weight: 650;
}

.field input {
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
}

.login button {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  background: var(--green-deep);
  color: var(--on-green);
  cursor: pointer;
}

.login-card button[type="submit"] { width: 100%; margin-top: 6px; }

.login-card .text-link {
  display: block;
  width: auto;
  margin: 18px auto 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--green-deep);
  font: inherit;
  font-weight: 650;
  text-decoration: underline;
  cursor: pointer;
}

.hint { font-size: 14px; }
#ob-app { color: var(--muted); }

.error {
  color: var(--danger);
  font-weight: 650;
  margin: 0 0 8px;
}

.top {
  position: relative;
  z-index: 5;
  display: grid;
  gap: 12px;
  padding: 32px 28px 8px;
  max-width: 1080px;
  margin: 0 auto;
}

.top-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.top-copy { min-width: 0; flex: 1; }
.account-slot { flex: none; }

.lang-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
}

.lang-select {
  font: inherit;
  font-weight: 700;
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 12px;
  cursor: pointer;
}

.account { position: relative; }
.account-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.account-btn:hover { border-color: var(--green); }
.account-first { display: none; }
.avatar {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: var(--green-deep);
  color: var(--on-green);
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  flex: none;
}
.account-panel {
  position: absolute;
  z-index: 6;
  top: calc(100% + 8px);
  right: 0;
  width: min(320px, calc(100vw - 32px));
  max-height: min(72vh, 560px);
  overflow: auto;
  padding: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow), 0 8px 24px rgba(20, 40, 30, 0.08);
}
.account-who { padding: 10px 12px 4px; }
.account-fullname { margin: 0; font-size: 18px; font-weight: 700; }
.account-email,
.account-role { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.account-role { font-weight: 700; color: var(--green); }
.account-label {
  margin: 12px 12px 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
html[lang="th"] .account-label { letter-spacing: 0; text-transform: none; font-size: 13px; }
.seg {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--soft);
  border-radius: 16px;
}
.seg button {
  flex: 1;
  min-width: 0;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  padding: 8px 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  cursor: pointer;
}
.seg button[aria-pressed="true"] {
  background: var(--green-deep);
  color: var(--on-green);
}
.account-item {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 12px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
.account-item:hover,
.seg button:hover { background: var(--soft); }
.seg button[aria-pressed="true"]:hover { background: var(--green-deep); color: var(--on-green); }
.account-signout {
  margin-top: 8px;
  border-top: 1px solid var(--line);
  border-radius: 0 0 14px 14px;
  color: var(--green);
}
.account-btn:focus-visible,
.account-panel button:focus-visible,
.sheet-close:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 2px;
}
@media (min-width: 800px) {
  .account-btn:has(.account-first:not(:empty)) { padding: 4px 14px 4px 4px; }
  .account-first:not(:empty) { display: inline; }
}

.site-list { display: grid; gap: 12px; }
.site-row { display: flex; gap: 8px; align-items: stretch; }
.site-row .site-card { flex: 1; }
.remove-customer {
  font: inherit;
  font-weight: 700;
  border-radius: 999px;
  padding: 12px 16px;
  background: var(--field);
  color: var(--danger);
  border: 1px solid var(--line);
  cursor: pointer;
}

.site-card {
  display: grid;
  gap: 4px;
  text-align: left;
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 18px 20px;
  box-shadow: var(--shadow);
  font: inherit;
  cursor: pointer;
}

.site-card strong { font-size: 22px; }
.site-card span { color: var(--muted); }

.ha-connect { margin-bottom: 16px; }
.ha-connect .field { grid-template-columns: 1fr; }
.dash {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  margin-bottom: 16px;
  align-items: start;
}

.flow {
  margin-bottom: 16px;
  background:
    radial-gradient(90% 55% at 50% 0%, rgba(196, 78, 48, 0.22), transparent 62%),
    linear-gradient(180deg, #321c32 0%, #1c1222 100%);
  color: #fffaf7;
  border-radius: 22px;
  padding: 14px 8px 6px;
  box-shadow: var(--shadow);
}

.flow-title {
  margin: 0 14px 2px;
  font-size: 18px;
  font-weight: 700;
}

.flow svg {
  display: block;
  width: 100%;
  height: auto;
  font-family: inherit;
}

.flow .tracks path {
  stroke: rgba(255, 255, 255, 0.38);
  stroke-width: 2.4;
  stroke-linecap: round;
}

.flow .flowlines path {
  stroke: transparent;
  stroke-width: 4;
  stroke-linecap: round;
  stroke-dasharray: 0.4 10;
}

.flow .flowlines path.on { animation: march 1.15s linear infinite; }
.flow .flowlines path.rev { animation-direction: reverse; }
#wire-solar.on { stroke: #ffb020; }
#wire-grid.on { stroke: #7eb6ff; }
#wire-home.on { stroke: #ff8a5b; }
#wire-car.on { stroke: #f7f4ef; }
#wire-battery.on { stroke: #5ddea0; }
#wire-battery.on.charge { stroke: #ffb020; }

.flow .pills rect { fill: #140c12; fill-opacity: 0.88; }

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

.dash.solo { grid-template-columns: minmax(0, 1fr); }

.choices { margin-top: 8px; }
.choice {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: start;
  padding: 12px 0;
  border-top: 1px solid var(--line);
  cursor: pointer;
}
.choice input[type="radio"] { margin-top: 4px; }
.choice strong { display: block; font-size: 16px; }
.choice small { display: block; margin-top: 3px; color: var(--muted); font-size: 13px; line-height: 1.4; }
.choice:has(input:checked) strong { color: var(--green); }
#battery-floor {
  width: 4.2em;
  font: inherit;
  font-weight: 700;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px 6px;
  color: var(--ink);
  background: var(--field);
}
.policy-line { margin: 14px 0 0; font-weight: 700; }
.charger-panel .note { margin-top: 6px; }

.password-panel { margin-bottom: 16px; }
.sheet .password-panel { margin: 0; }
.password-panel button {
  font: inherit;
  font-weight: 700;
  border-radius: 999px;
  padding: 12px 18px;
  cursor: pointer;
}
.password-form button {
  border: 0;
  background: var(--green-deep);
  color: var(--on-green);
}
.sheet {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 16px;
}
.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(12, 22, 18, 0.48);
}
.sheet-card {
  position: relative;
  z-index: 1;
  width: min(480px, 100%);
  max-height: min(88vh, 740px);
  overflow: auto;
}
.sheet-close {
  background: var(--field);
  color: var(--ink);
  border: 1px solid var(--line);
}
body:has(.sheet:not([hidden])) { overflow: hidden; }
.admin-reset-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin: 10px 0;
}
.admin-reset-row button {
  background: var(--field);
  color: var(--green);
  border: 1px solid var(--line);
}
.add-customer {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  margin-bottom: 16px;
  background: var(--green-deep);
  color: white;
  cursor: pointer;
}
.onboard { margin-bottom: 16px; }
.onboard-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.onboard-grid fieldset {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 16px;
  border: 0;
  margin: 6px 0 0;
  padding: 0;
}
.onboard-grid legend { font-weight: 700; padding: 10px 0 2px; }
#ob-car-account {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 16px;
}
.onboard select {
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
}
#plant-fit { grid-column: 1 / -1; margin: 8px 0 0; }
.onboard-save { grid-column: 1 / -1; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.onboard-save .note, .onboard-save .error { flex-basis: 100%; }
.onboard button {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  background: var(--green-deep);
  color: var(--on-green);
  cursor: pointer;
}
.onboard button.remove-customer {
  background: var(--field);
  color: var(--danger);
  border: 1px solid var(--line);
}

.energy { margin-bottom: 16px; }
.energy header { flex-wrap: wrap; }
.energy-heading { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.period { display: flex; gap: 6px; }
.period button {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  padding: 8px 12px;
  background: var(--soft);
  color: var(--ink);
  cursor: pointer;
}
.period button[aria-pressed="true"] { background: var(--green-deep); color: var(--on-green); }
.meter { display: grid; gap: 12px; margin: 14px 0; }
.meter-row {
  display: grid;
  grid-template-columns: 88px 1fr auto;
  gap: 10px;
  align-items: center;
}
.trackbar { height: 12px; background: var(--soft); border-radius: 999px; overflow: hidden; }
.self-sufficiency { display: grid; gap: 2px; margin: 4px 0 16px; }
.self-figure {
  margin: 0;
  font-size: 48px;
  font-weight: 700;
  letter-spacing: -0.04em;
  line-height: 1;
  color: var(--green);
  font-variant-numeric: tabular-nums;
}
.self-label { margin: 0; color: var(--muted); font-weight: 650; font-size: 15px; }
#energy-sources .meter { margin-top: 0; }
.trackbar span { display: block; height: 100%; border-radius: 999px; }
.meter-row.solar .trackbar span { background: #e39b12; }
.meter-row.battery .trackbar span { background: #1f8a5b; }
.meter-row.grid .trackbar span { background: #3c7dd9; }
.meter-row b { font-variant-numeric: tabular-nums; }
.energy .hint { margin: 0; }

.charger-panel header { align-items: center; }
.rows { margin: 12px 0 0; }
.rows div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}
.rows dt { text-transform: none; letter-spacing: 0; font-size: 15px; min-width: 0; }
.rows dd { margin: 0; font-size: 15px; text-align: right; flex: none; }

#surplus-toggle {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  background: var(--soft);
  color: var(--ink);
  cursor: pointer;
}
#surplus-toggle[aria-checked="true"] {
  background: var(--green-deep);
  color: var(--on-green);
}

.ha-connect button {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  background: var(--green-deep);
  color: var(--on-green);
  cursor: pointer;
}

.identity {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}

.facts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0 0;
}

.facts div {
  background: var(--soft-2);
  border-radius: 14px;
  padding: 12px;
}

.facts dd { font-size: 16px; }

.logo-mark {
  display: block;
  width: min(100%, 300px);
  height: auto;
  margin: 4px auto 14px;
  border-radius: 18px;
}

.top .logo-mark {
  width: 84px;
  margin: 0 0 10px;
  border-radius: 14px;
}

h1 {
  margin: 0;
  font-size: clamp(32px, 4vw, 48px);
  line-height: 1.05;
  letter-spacing: -0.03em;
  max-width: 16ch;
}

.lede {
  margin: 0;
  max-width: 62ch;
  color: var(--muted);
  font-size: 17px;
  line-height: 1.45;
}

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px 28px 48px;
}

.mode-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 18px;
  margin-bottom: 18px;
}

.switch {
  display: flex;
  padding: 4px;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 999px;
}

.switch button {
  border: 0;
  background: transparent;
  color: var(--ink);
  border-radius: 999px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 650;
  cursor: pointer;
}

.switch button[aria-pressed="true"] {
  background: var(--green-deep);
  color: var(--on-green);
}

.status {
  margin: 0;
  font-size: 18px;
  font-weight: 650;
}

.presets {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 18px;
}

.presets p {
  margin: 0 6px 0 0;
  color: var(--muted);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.presets button {
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--ink);
  border-radius: 999px;
  padding: 7px 12px;
  font: inherit;
  cursor: pointer;
}

.presets button:hover { border-color: var(--green); }

.grid {
  display: grid;
  grid-template-columns: 1.15fr 0.85fr;
  gap: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.card header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

h2, h3 { margin: 0; }
.heading-aside {
  margin-left: 8px;
  color: var(--muted);
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 0;
}

h3 {
  margin-top: 22px;
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.pill {
  border-radius: 999px;
  padding: 4px 10px;
  background: var(--pill);
  color: var(--amber);
  font-size: 12px;
  font-weight: 700;
}

.note, .explanation {
  color: var(--muted);
  line-height: 1.45;
}

label {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  margin: 14px 0;
  font-weight: 650;
}

label output {
  font-variant-numeric: tabular-nums;
  color: var(--green);
}

input[type="range"] {
  grid-column: 1 / -1;
  accent-color: var(--green);
}

.metrics {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 12px;
  margin: 8px 0 0;
}

.metrics div {
  background: var(--soft-2);
  border-radius: 14px;
  padding: 12px;
}

dt {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

dd {
  margin: 4px 0 0;
  font-size: 22px;
  font-weight: 700;
  line-height: 1.25;
}

#window {
  font-size: 16px;
}

.amps {
  margin: 8px 0 0;
  font-size: 84px;
  line-height: 0.9;
  letter-spacing: -0.04em;
  color: var(--green);
}

.amps span {
  font-size: 28px;
  margin-left: 4px;
}

.cable, .connector {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.connector select, .cable input {
  font: inherit;
}

#log {
  margin: 10px 0 0;
  padding-left: 18px;
  color: var(--ink);
}

#log li { margin: 6px 0; }

#log .when {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.empty { color: var(--muted); list-style: none; margin-left: -18px; }

@media (prefers-reduced-motion: reduce) {
  .flow .flowlines path.on {
    animation: none;
    stroke-dasharray: none;
    stroke-width: 2.5;
  }
}

@media (max-width: 800px) {
  .grid, .identity, .facts, .dash, .onboard-grid, .onboard-grid fieldset, #ob-car-account { grid-template-columns: 1fr; }
  .top, main { padding-left: 16px; padding-right: 16px; }
  .amps { font-size: 68px; }
  .self-figure { font-size: 42px; }
}
