/* A day of energy at home: a clay diorama over one sunny day, with the electricity laid out below.
   One deliberate look (warm clay and paper), so colours are set explicitly rather than themed. */
:root {
  --ink: #33261d;
  --ink-soft: #6e5a4c;
  --paper: #fbf5ec;
  --card: rgba(252, 247, 240, 0.96);
  --line: #e6d8c6;
  --accent: #b9603f;
  --shadow: 0 24px 60px rgba(40, 24, 14, 0.28);
  --ease: cubic-bezier(.22, .8, .24, 1);

  /* energy series */
  --solar: #d9951a;      --solar-a: #f6d78f;
  --battery: #7a5cc2;    --battery-a: #cdbdf0;
  --import: #c9502f;     --import-a: #f2bba7;
  --export: #2a8fb0;
  --home: #33261d;
  --price: #e8a33b;

  --sky: #e9dccb;        /* page backdrop tint, set from the time of day */
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--sky);
  transition: background-color 1.2s linear;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden; overflow-y: auto;
  display: flex; flex-direction: column; height: 100vh; height: 100dvh;
}
h1, h2, .steps button, .tile b, .clock { font-family: Fraunces, Georgia, serif; }

#backdrop {
  position: fixed; inset: -40px; width: calc(100% + 80px); height: calc(100% + 80px);
  filter: blur(36px) saturate(1.05); opacity: .75; pointer-events: none;
}

/* ---------- header ---------- */
.top {
  position: relative; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding-top: max(14px, env(safe-area-inset-top, 0px));
  padding-left: max(24px, env(safe-area-inset-left, 0px));
  padding-right: max(24px, env(safe-area-inset-right, 0px));
}
.kicker { margin: 0; font-size: 11px; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; color: var(--fg-soft, var(--ink-soft)); }
h1 { margin: 2px 0 0; font-size: clamp(22px, 2.4vw, 30px); font-weight: 600; line-height: 1.1; color: var(--fg, var(--ink)); text-wrap: balance; }
.by { display: flex; align-items: center; gap: 12px; font-size: 13px; color: var(--fg-soft, var(--ink-soft)); white-space: nowrap; }
.by strong { color: var(--fg, var(--ink)); font-weight: 600; }
body.night .top { --fg: #f6ecdf; --fg-soft: #cbbba8; }
.btn { display: inline-flex; align-items: center; gap: 7px; border: 0; border-radius: 999px; padding: 8px 14px; font: 600 14px/1 Inter, system-ui, sans-serif; text-decoration: none; cursor: pointer; }
.btn-li { background: rgba(255, 252, 247, .94); color: #0a66c2; box-shadow: 0 6px 18px rgba(40, 24, 14, .2); }
.btn:focus-visible, .steps button:focus-visible, .play:focus-visible, .legend button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- layout ---------- */
.layout {
  position: relative; z-index: 1;
  flex: 1 0 auto; min-height: 0;
  /* the house fills what the panel leaves, but never less than ~40% of the screen: on short screens the page scrolls instead */
  display: grid; grid-template-rows: minmax(max(240px, 40dvh), 1fr) auto; grid-template-columns: minmax(0, 1fr); gap: 12px;
  padding: 8px max(20px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(20px, env(safe-area-inset-left, 0px));
}
.stage-wrap { position: relative; min-height: 0; min-width: 0; display: grid; place-items: center; }
.stage {
  position: relative; aspect-ratio: 16 / 9; max-width: 100%; max-height: 100%;
  border-radius: 22px; overflow: hidden; box-shadow: var(--shadow); background: #2a2420;
}
.stage > canvas, .stage > svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.clock {
  position: absolute; left: 14px; top: 12px; display: flex; align-items: center; gap: 8px;
  padding: 7px 13px 7px 9px; border-radius: 999px; background: rgba(252, 247, 240, .9);
  font-size: 17px; font-weight: 600; box-shadow: 0 6px 18px rgba(40, 24, 14, .18); font-variant-numeric: tabular-nums;
}
.clock-icon { width: 20px; height: 20px; display: grid; place-items: center; }
.price-badge {
  position: absolute; right: 14px; top: 12px; padding: 7px 13px; border-radius: 999px;
  background: var(--price); color: #3a2208; font: 700 12px/1 Inter, system-ui, sans-serif; letter-spacing: .05em; text-transform: uppercase;
  box-shadow: 0 0 0 0 rgba(232, 163, 59, .6); animation: beacon 1.6s ease-out infinite;
}
@keyframes beacon { 70% { box-shadow: 0 0 0 12px rgba(232, 163, 59, 0); } 100% { box-shadow: 0 0 0 0 rgba(232, 163, 59, 0); } }
.loading { position: absolute; inset: 0; display: grid; place-items: center; background: #2a2420; transition: opacity .5s; }
.loading.done { opacity: 0; pointer-events: none; }
.loading span { width: 30px; height: 30px; border-radius: 50%; border: 3px solid rgba(255, 248, 236, .35); border-top-color: #fff8ec; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(1turn); } }

/* flow overlay */
#flows .wire { fill: none; stroke-linecap: round; stroke-dasharray: 2 6; transition: stroke-opacity .4s; }
#flows .dot { filter: drop-shadow(0 0 3px rgba(255, 255, 255, .9)); }
#flows .tag rect { fill: rgba(252, 247, 240, .93); }
#flows .tag text { font: 600 13px Inter, system-ui, sans-serif; fill: var(--ink); }
#flows .tag .k { font-weight: 700; }
#flows .ring { fill: none; stroke: #fff6d6; stroke-width: 3; animation: ring 1.4s var(--ease) forwards; }
@keyframes ring { from { r: 6; opacity: 1; } to { r: 70; opacity: 0; } }

/* ---------- panel ---------- */
.panel {
  width: min(1180px, 100%); min-width: 0; margin: 0 auto; background: var(--card); border-radius: 22px; box-shadow: var(--shadow);
  padding: 14px 18px 10px; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.steps { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.steps::-webkit-scrollbar { display: none; }
.steps button {
  flex: 1 0 auto; display: flex; align-items: center; gap: 8px; border: 0; border-radius: 14px; padding: 8px 12px;
  background: transparent; color: var(--ink-soft); font-size: 15px; font-weight: 600; cursor: pointer; white-space: nowrap;
  transition: background .2s, color .2s;
}
.steps button .n {
  width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; flex: none;
  font: 700 12px/1 Inter, system-ui, sans-serif; background: #efe4d5; color: var(--ink-soft);
}
.steps button:hover { background: #f4ebde; color: var(--ink); }
.steps button[aria-current="step"] { background: var(--ink); color: var(--paper); }
.steps button[aria-current="step"] .n { background: var(--accent); color: #fff; }
.steps button.done .n { background: #d9cbb7; color: var(--ink); }

/* the About view and the chapter view share one cell, so the panel (and the house above it) keeps its size */
.panel-body { display: grid; }
.panel-body > * { grid-area: 1 / 1; min-width: 0; transition: opacity .22s ease, visibility .22s; }
.panel-body > :not(.off) { transition-delay: .2s; }      /* out, then in: the two never overlap */
.panel-body > .off { opacity: 0; visibility: hidden; pointer-events: none; }
.about { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 26px; padding-top: 14px; align-content: start; }
.about .explain { font-size: 15px; max-width: 58ch; }
.about-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 4px; }
.btn-go { background: var(--ink); color: var(--paper); padding: 11px 18px; font-size: 15px; box-shadow: 0 8px 20px rgba(40, 24, 14, .22); transition: transform .2s var(--ease); }
.btn-go:hover { transform: translateX(2px); }
.btn-line { background: transparent; color: #0a66c2; padding: 10px 16px; box-shadow: inset 0 0 0 1.5px #c9d9ea; }
.btn-line:hover { box-shadow: inset 0 0 0 1.5px #0a66c2; }
.about-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; counter-reset: a; }
.about-list li {
  counter-increment: a; position: relative; padding: 10px 12px 10px 44px; border-radius: 14px; background: #f4ebdf;
  font-size: 13.5px; line-height: 1.5; color: var(--ink-soft);
}
.about-list li::before {
  content: counter(a); position: absolute; left: 12px; top: 10px; width: 22px; height: 22px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: #fff; font: 700 11px/1 Inter, system-ui, sans-serif;
}
.about-list b { color: var(--ink); font-weight: 600; }
.steps button.about-step .n { background: #f6dccd; color: var(--accent); }
.steps button.about-step[aria-current="step"] .n { background: var(--accent); color: #fff; }

/* zoomed in on the front door: the energy overlay steps aside */
.stage #flows, .stage .clock, .stage .price-badge { transition: opacity .45s ease; }
.stage.zoomed #flows, .stage.zoomed .clock, .stage.zoomed .price-badge { opacity: 0; }

.panel-grid { display: grid; grid-template-columns: minmax(280px, 1fr) minmax(0, 1.9fr); gap: 22px; padding-top: 12px; }
.story { min-width: 0; }
.eyebrow { margin: 0; font-size: 11px; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); }
h2 { margin: 4px 0 6px; font-size: 26px; font-weight: 600; line-height: 1.1; text-wrap: balance; }
.explain { margin: 0 0 12px; font-size: 14px; line-height: 1.55; color: var(--ink-soft); max-width: 62ch; }
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.tile { border-radius: 14px; background: #f4ebdf; padding: 8px 11px; min-width: 0; }
.tile span { display: block; font-size: 11px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-soft); }
.tile b { display: block; font-size: 22px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile b small { font: 500 12px Inter, system-ui, sans-serif; color: var(--ink-soft); margin-left: 2px; }
.tile i { display: block; font-style: normal; font-size: 11.5px; color: var(--ink-soft); margin-top: 1px; }
.tile i.up { color: var(--import); }
.tile i.down { color: #2f8a4e; }

.chart-col { min-width: 0; display: flex; flex-direction: column; }
.chart-head { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.play {
  flex: none; width: 32px; height: 32px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--ink); color: var(--paper); cursor: pointer;
}
.play .i-play { display: none; }
.play.paused .i-play { display: block; }
.play.paused .i-pause { display: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.legend button {
  display: inline-flex; align-items: center; gap: 6px; border: 0; background: none; padding: 3px 2px; cursor: pointer;
  font: 500 12.5px Inter, system-ui, sans-serif; color: var(--ink);
}
.legend button.off { opacity: .35; }
.legend .sw { width: 16px; height: 10px; border-radius: 3px; flex: none; }
.legend .sw.line { height: 3px; border-radius: 2px; }
.legend .sw.dash { height: 0; border-top: 3px dashed; background: none !important; }
.chart { position: relative; flex: 1; min-height: 190px; }
#graph { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: pan-y; cursor: ew-resize; }
#graph text { font: 500 11px Inter, system-ui, sans-serif; fill: var(--ink-soft); }
#graph .grid line { stroke: #eadfcf; }
#graph .now { stroke: var(--ink); stroke-width: 1.5; }
#graph .price-band { fill: rgba(232, 163, 59, .16); }
#graph .price-label { font-weight: 700; fill: #a36612; letter-spacing: .04em; }
#graph .night { fill: rgba(60, 64, 96, .06); }
#graph .area { transition: opacity .3s; }
#graph .ln { fill: none; stroke-linejoin: round; stroke-linecap: round; }
#graph .soc-bg { fill: #efe6da; }
#graph .self-label { font: italic 600 11.5px Fraunces, Georgia, serif; fill: #5b4632; paint-order: stroke; stroke: rgba(255, 250, 240, .85); stroke-width: 3px; }
.tip {
  position: absolute; top: 4px; pointer-events: none; z-index: 3; min-width: 168px;
  background: rgba(40, 30, 24, .94); color: #fbf3e8; border-radius: 12px; padding: 8px 10px; font-size: 12px; line-height: 1.45;
  box-shadow: 0 10px 30px rgba(20, 12, 6, .3);
}
.tip b { font-family: Fraunces, Georgia, serif; font-size: 14px; display: block; margin-bottom: 2px; }
.tip .row { display: flex; justify-content: space-between; gap: 12px; }
.tip .row span:first-child { display: inline-flex; align-items: center; gap: 6px; }
.tip .row i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.tip .row.sub { opacity: .75; padding-left: 14px; }
.fine { margin: 8px 0 0; font-size: 11px; color: var(--ink-soft); line-height: 1.4; }

/* ---------- smaller screens: everything stacks and the page scrolls ---------- */
@media (max-width: 900px), (max-height: 640px) {
  body { overflow: auto; height: auto; min-height: 100dvh; }
  .layout { flex: none; grid-template-rows: auto auto; }
  .stage { width: 100%; max-height: none; }
  /* the house stays pinned while you read and scrub the chart below it */
  .stage-wrap { position: sticky; top: max(6px, env(safe-area-inset-top, 0px)); z-index: 3; }
  .panel-grid { grid-template-columns: 1fr; gap: 12px; }
  .about { grid-template-columns: 1fr; gap: 14px; }
  .chart { min-height: 220px; height: 240px; }
}
@media (max-width: 600px) {
  .top { padding-left: 16px; padding-right: 16px; align-items: flex-start; }
  .by > span { display: none; }
  .btn-li span { display: none; }
  .btn-li { padding: 9px; }
  .layout { padding-left: max(12px, env(safe-area-inset-left, 0px)); padding-right: max(12px, env(safe-area-inset-right, 0px)); }
  .stage { border-radius: 16px; }
  .clock { font-size: 14px; left: 8px; top: 8px; padding: 5px 10px 5px 7px; }
  .price-badge { right: 8px; top: 8px; font-size: 10px; padding: 6px 10px; }
  .panel { padding: 12px 12px 10px; border-radius: 18px; }
  .steps button { padding: 7px 9px; font-size: 14px; }
  h2 { font-size: 22px; }
  #flows .tag text { font-size: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  .price-badge { animation: none; }
  #flows .ring { animation: none; opacity: 0; }
}

/* ---------- desktop: title, chapters and story in a left sidebar; the house on the right with the chart beneath it ----------
   The wrappers (main, panel, about, chapter) dissolve with display: contents, so their pieces sit straight on the page grid:
     side  | main
     a top | house
     b nav | house
     c text| house
     d text| chart / about list
     e fine| chart / about list                                                                            */
@media (min-width: 901px) and (min-height: 641px) {
  body {
    display: grid;
    grid-template-columns: [side] clamp(330px, 27vw, 410px) [main] minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto auto;
    column-gap: 20px;
    padding: max(16px, env(safe-area-inset-top, 0px)) max(20px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(20px, env(safe-area-inset-left, 0px));
    overflow: hidden;
  }
  .layout, .panel, .panel-body, #about, #chapter { display: contents; }

  /* the sidebar card sits behind its pieces */
  body::before {
    content: ""; grid-column: side; grid-row: 1 / -1; z-index: 1;
    background: var(--card); border-radius: 22px; box-shadow: var(--shadow);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  }
  .top, #steps, .about-copy, .story, .fine { grid-column: side; z-index: 2; min-width: 0; }

  .top { grid-row: 1; flex-direction: column; align-items: flex-start; gap: 10px; padding: 20px 22px 0; }
  .top, body.night .top { --fg: var(--ink); --fg-soft: var(--ink-soft); }
  h1 { font-size: clamp(24px, 2vw, 29px); }
  .btn-li { box-shadow: inset 0 0 0 1.5px #c9d9ea; background: transparent; }

  #steps { grid-row: 2; flex-direction: column; gap: 2px; overflow: visible; margin: 14px 14px 0; padding: 12px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
  .steps button { flex: none; padding: 6px 10px; }

  .about-copy, .story { grid-row: 3 / 5; overflow-y: auto; overscroll-behavior: contain; padding: 16px 22px 8px; }
  .fine { grid-row: 5; margin: 0; padding: 6px 22px 16px; }

  .stage-wrap { grid-column: main; grid-row: 1 / 4; z-index: 2; }

  .about-list, .chart-col {
    grid-column: main; grid-row: 4 / 6; z-index: 2; margin-top: 14px;
    height: var(--lower-h, clamp(200px, 28vh, 290px)); background: var(--card); border-radius: 22px; box-shadow: var(--shadow);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  }
  .chart-col { padding: 12px 18px 8px; }
  .chart { min-height: 0; }
  .about-list { grid-template-columns: repeat(3, minmax(0, 1fr)); align-content: center; gap: 14px; padding: 18px; }
  .about-list li { height: 100%; padding: 14px 16px 14px 48px; font-size: 14px; }
  .about-list li::before { top: 14px; left: 14px; }

  /* the About and chapter pieces swap in place: out, then in */
  #about > *, #chapter > * { visibility: visible; transition: opacity .22s ease .2s, visibility 0s linear .2s; }
  #about.off > *, #chapter.off > * { opacity: 0; visibility: hidden; pointer-events: none; transition: opacity .22s ease, visibility 0s linear .22s; }
}

/* shorter desktop screens: the chapter list folds into a row of numbers (the chapter's name is right below it) */
@media (min-width: 901px) and (min-height: 641px) and (max-height: 820px) {
  #steps { flex-direction: row; justify-content: space-between; gap: 4px; padding: 10px 2px; }
  .steps button { font-size: 0; gap: 0; padding: 4px; border-radius: 50%; }
  .steps button .n { width: 30px; height: 30px; font-size: 13px; }
  .top { gap: 8px; padding-top: 18px; }
  .about-copy, .story { padding-top: 12px; }
}
