/* Electricity Split: the house ledger notebook. */

:root {
  /* cover + binding */
  --cover: #2443a6;
  --cover-deep: #182f7e;
  --cover-edge: #3a58bd;
  --staple-hi: #eef1f6;
  --staple-lo: #8b94a8;

  /* paper */
  --paper: #fbfcfe;
  --rule-c: #c5d3ef;
  --margin-c: #e2636b;

  /* inks */
  --ink: #1f3fa6;          /* ballpoint: everything the user writes */
  --ink-strong: #142d85;
  --print: #1b1f2e;        /* what the notebook printed */
  --print-2: #4b5269;
  --pencil: #5f6679;       /* working, hints, previous readings */
  --pencil-hint: #6a7183;  /* placeholders: 4.8:1 on paper */
  --pencil-faint: #a9b0c0; /* leader dots only, never text */
  --red: #c22a35;          /* red pen: corrections only */
  --stamp: #3340b3;
  --hl: #fff09a;           /* highlighter */

  --f-print: "Archivo", "Arial Narrow", system-ui, sans-serif;
  --f-ink: "Kalam", "Segoe Print", "Bradley Hand", cursive;

  --rule: 44px;
  --mx: 54px;              /* text starts right of the margin */
  --gutter: 16px;

  --ease-out: cubic-bezier(.16, 1, .3, 1);

  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-color: #8ea2dd var(--cover-deep);
  background: var(--cover);
}

body {
  margin: 0;
  display: flow-root;      /* stop .book's margin collapsing through body */
  min-height: 100dvh;
  font-family: var(--f-print);
  color: var(--print);
  background-color: var(--cover);
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 .22 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(120% 80% at 50% 0%, var(--cover-edge), var(--cover) 55%, var(--cover-deep));
  background-attachment: fixed;
}

::selection { background: var(--hl); color: var(--print); }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
  border-radius: 4px;
}

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- the notebook ---------- */

.book {
  position: relative;
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  perspective: 1600px;
}

.binding {
  position: relative;
  height: 26px;
  flex: none;
  background:
    linear-gradient(to bottom, rgba(255,255,255,.12), transparent 40%),
    var(--cover);
  z-index: 2;
}
/* closed staple seen from the front: just the flat wire crown across the binding edge */
.staple {
  position: absolute;
  bottom: -2px;
  width: 36px;
  height: 3px;
  border-radius: 1.5px;
  background: linear-gradient(to bottom, var(--staple-hi) 0 45%, var(--staple-lo) 100%);
  box-shadow: 0 1px 0 rgba(10, 20, 60, .35);
}
.staple:first-child { left: 24%; }
.staple:nth-child(2) { right: 24%; }

/* sync status, printed small on the cover */
.sync {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  font: 600 10.5px/1 var(--f-print);
  font-stretch: 80%;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, .82);
  white-space: nowrap;
}
.sync[data-state="error"] { color: #ffd2d5; }
.sync[data-state="saving"] { color: rgba(255, 255, 255, .62); }

.page {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: var(--paper);
  transform-origin: 50% 0;
}
.page[hidden] { display: none; }

@media (min-width: 640px) {
  .book {
    margin: 36px auto 48px;
    min-height: calc(100dvh - 84px);
  }
  .binding { border-radius: 4px 4px 0 0; }
  .page {
    border-radius: 0 0 4px 4px;
    box-shadow:
      0 2px 3px rgba(8, 18, 60, .28),
      0 28px 60px -18px rgba(6, 14, 50, .6);
  }
  /* the pages beneath */
  .book::after {
    content: "";
    position: absolute;
    left: 6px; right: 6px; bottom: -6px;
    height: 12px;
    background: #e9edf6;
    border-radius: 0 0 5px 5px;
    box-shadow: 0 6px 14px -6px rgba(6, 14, 50, .5);
    z-index: -1;
  }
}

/* printed header band */
.page-head {
  padding: 18px var(--gutter) 12px var(--mx);
  border-bottom: 3px double var(--margin-c);
}
.head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
}
.title {
  margin: 0;
  font: 800 30px/1 var(--f-print);
  font-stretch: 68%;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--print);
}
.title:focus { outline: none; }
.head-sub {
  margin-top: 8px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  min-height: 34px;
}
.head-sub .printed { padding-bottom: 6px; }
.head-note {
  font: 400 18px/1.2 var(--f-ink);
  color: var(--ink);
  padding-bottom: 2px;
}
.back-row { display: flex; align-items: center; gap: 4px; margin-left: -12px; }

/* ruled area */
.ruled {
  position: relative;
  flex: 1;
  padding: 0 var(--gutter) calc(var(--rule) * 2) var(--mx);
  background-image: linear-gradient(to bottom, transparent calc(var(--rule) - 1px), var(--rule-c) calc(var(--rule) - 1px));
  background-size: 100% var(--rule);
  min-height: calc(var(--rule) * 8);
}
.ruled::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0;
  left: calc(var(--mx) - 16px);
  width: 1px;
  background: var(--margin-c);
  box-shadow: 4px 0 0 var(--margin-c);
}

.line {
  position: relative;
  height: var(--rule);
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding-bottom: 6px;
}
.line.tall { height: calc(var(--rule) * 2); align-items: center; padding-bottom: 0; }

/* ---------- type ---------- */

.printed {
  font: 600 12.5px/1 var(--f-print);
  font-stretch: 82%;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--print-2);
}
.sec {
  margin: 0;
  font: 800 15px/1 var(--f-print);
  font-stretch: 72%;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--print);
  padding-bottom: 4px;
}
.label {
  font: 500 16px/1.1 var(--f-print);
  font-stretch: 88%;
  color: var(--print);
  padding-bottom: 3px;
  white-space: nowrap;
}
.hand { font: 400 22px/1.1 var(--f-ink); color: var(--ink); }
.hand.bold { font-weight: 700; }
.pencil { font: 400 17px/1.15 var(--f-ink); color: var(--pencil); }
.num { font-variant-numeric: tabular-nums; }
.grow { flex: 1; min-width: 0; }
.right { margin-left: auto; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.leader {
  flex: 1;
  min-width: 12px;
  align-self: flex-end;
  margin-bottom: 7px;
  border-bottom: 2px dotted var(--pencil-faint);
}

/* ---------- written inputs ---------- */

.ink-in {
  height: 34px;
  margin-bottom: -7px;     /* sit the handwriting on the rule, like the names */
  min-width: 0;
  padding: 0 4px;
  border: 0;
  border-radius: 2px;
  background: transparent;
  font: 400 23px/1 var(--f-ink);
  color: var(--ink);
  caret-color: var(--ink);
  text-align: right;
  width: 8ch;
}
.ink-in.wide { width: 100%; text-align: left; }
.ink-in::placeholder { color: var(--pencil-hint); opacity: 1; }
.ink-in:focus,
.ink-in:focus-visible {
  outline: none;
  background: linear-gradient(to bottom, transparent 48%, var(--hl) 48%, var(--hl) 92%, transparent 92%);
}
.ink-in[type="month"] {
  width: auto;
  text-align: left;
  font-size: 21px;
  padding-right: 0;
}
.ink-in[type="month"]::-webkit-calendar-picker-indicator {
  opacity: .55;
  cursor: pointer;
  filter: invert(18%) sepia(80%) saturate(2400%) hue-rotate(222deg);
}

.field { position: relative; display: flex; align-items: flex-end; }
.field.err::after {
  content: "";
  position: absolute;
  left: 2px; right: 2px; bottom: -4px;
  height: 6px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='6'%3E%3Cpath d='M0 3 Q3 0 6 3 T12 3' fill='none' stroke='%23c22a35' stroke-width='1.6'/%3E%3C/svg%3E") repeat-x;
  pointer-events: none;
}
.unit-tag { font: 400 17px/1 var(--f-ink); color: var(--pencil); padding-bottom: 7px; }

/* red pen corrections */
.err-line {
  font: 400 17px/1.15 var(--f-ink);
  color: var(--red);
}
.err-line .mark {
  position: absolute;
  left: calc(-1 * var(--mx) + 12px);
  bottom: 8px;
  width: 20px; height: 20px;
  color: var(--red);
}
.err-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- present tick box ---------- */

.tick {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  margin-bottom: -6px;
  padding: 0 2px 0 8px;
  cursor: pointer;
  user-select: none;
}
.tick input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.tick .box {
  width: 24px; height: 24px;
  border: 1.6px solid var(--print-2);
  border-radius: 3px 4px 3px 5px;
  transform: rotate(-3deg);
  display: grid;
  place-items: center;
  background: var(--paper);
}
.tick svg { width: 26px; height: 26px; overflow: visible; }
.tick svg path {
  fill: none;
  stroke: var(--ink);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
  transition: stroke-dashoffset .28s var(--ease-out);
}
.tick input:checked + .box svg path { stroke-dashoffset: 0; }
.tick input:focus-visible + .box { outline: 2px solid var(--ink); outline-offset: 3px; }
.tick .printed { min-width: 4.4em; }
.tick input:not(:checked) ~ .printed { color: var(--pencil); }

/* ---------- buttons ---------- */

.icon-btn {
  width: 44px; height: 44px;
  display: inline-grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--print);
  flex: none;
  transition: background-color .2s;
}
.icon-btn:hover { background: rgba(31, 63, 166, .08); }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn.sm { width: 40px; height: 40px; margin-bottom: -4px; }
.icon-btn.sm svg { width: 20px; height: 20px; }

.ico {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.pen-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  margin-bottom: -4px;
  padding: 0 6px;
  border: 0;
  border-radius: 3px;
  background: transparent;
  font: 700 13.5px/1 var(--f-print);
  font-stretch: 80%;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink);
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 5px;
  text-decoration-color: color-mix(in srgb, var(--ink) 40%, transparent);
  transition: text-decoration-color .2s, background-color .2s;
}
.pen-btn:first-child { margin-left: -6px; }
.pen-btn:hover { text-decoration-color: var(--ink); background: rgba(31, 63, 166, .06); }
.pen-btn svg { width: 16px; height: 16px; }
.pen-btn.danger { color: var(--red); text-decoration-color: color-mix(in srgb, var(--red) 40%, transparent); }
.pen-btn.danger:hover { text-decoration-color: var(--red); background: rgba(194, 42, 53, .06); }
.pen-btn.quiet { color: var(--print-2); text-decoration-color: color-mix(in srgb, var(--print-2) 35%, transparent); }

/* the rubber stamp */
.stamp {
  --stamp-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='s'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.75' numOctaves='2' seed='7'/%3E%3CfeColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 -2.4 2.15'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23s)'/%3E%3C/svg%3E");
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: calc(var(--rule) * 2 - 22px);
  padding: 0 26px;
  border: 4px double var(--stamp);
  border-radius: 7px;
  background: transparent;
  color: var(--stamp);
  font: 800 23px/1 var(--f-print);
  font-stretch: 66%;
  letter-spacing: .22em;
  text-transform: uppercase;
  transform: rotate(-2.2deg);
  -webkit-mask-image: var(--stamp-mask);
  mask-image: var(--stamp-mask);
  transition: transform .18s var(--ease-out), background-color .2s;
}
.stamp svg { width: 22px; height: 22px; }
.stamp:hover { background: rgba(51, 64, 179, .07); }
.stamp:active { transform: rotate(-2.2deg) scale(.95); }
.stamp.thump { animation: thump .38s var(--ease-out); }
@keyframes thump {
  0% { transform: rotate(-2.2deg) scale(1.08); }
  45% { transform: rotate(-2.2deg) scale(.94); }
  100% { transform: rotate(-2.2deg) scale(1); }
}
.stamp:focus-visible { outline-offset: 6px; }

/* ---------- entry view ---------- */

.room-name { font: 700 22px/1.1 var(--f-ink); color: var(--ink); }
.prev { white-space: nowrap; }
.prev b { font-weight: 400; color: var(--print-2); }
.arrow { width: 22px; height: 14px; color: var(--pencil); flex: none; margin-bottom: 8px; }
.usage-out { min-width: 4.5em; text-align: right; white-space: nowrap; }
.usage-out.bad { color: var(--red); }

.sum-line .pencil b { font-weight: 700; color: var(--ink); }
.sum-line .pencil.bad b { color: var(--red); }

.hint { color: var(--pencil); }

/* ---------- results view ---------- */

.payee {
  width: 100%;
  border: 0;
  background: transparent;
  padding: 0 0 6px;
  text-align: left;
  border-radius: 0;
}
.payee:hover .pay-name { text-decoration: underline; text-decoration-color: var(--pencil-faint); text-underline-offset: 6px; }
.payee:focus-visible { outline-offset: -2px; }
.pay-name { font: 700 23px/1.1 var(--f-ink); color: var(--ink); max-width: 40%; }
.pay-kwh { flex: none; white-space: nowrap; padding-bottom: 1px; }
.chev { width: 18px; height: 18px; color: var(--pencil); flex: none; margin-bottom: 6px; transition: transform .3s var(--ease-out); }
.payee[aria-expanded="true"] .chev { transform: rotate(90deg); }

.amount {
  position: relative;
  display: inline-block;
  padding: 0 10px 0 16px;
  font: 700 25px/1 var(--f-ink);
  color: var(--ink-strong);
  white-space: nowrap;
}
.amount .rm { font-size: 16px; font-weight: 400; color: var(--pencil); margin-right: 4px; }
.amount .ring {
  position: absolute;
  left: -2px; top: -11px; bottom: -11px;
  width: calc(100% + 6px);
  height: calc(100% + 22px);
  overflow: visible;
  pointer-events: none;
}
.amount .ring path {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1.7;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}
/* rings are always a closed loop; they only ink in (any mid-frame is still a whole ring) */
.page.turn .amount .ring {
  animation: ink-in .32s var(--ease-out) backwards;
  animation-delay: calc(120ms + min(var(--i, 0), 5) * 40ms);
}
/* no opacity step: even a frozen first frame shows the whole ring */
@keyframes ink-in { from { filter: blur(1.2px); transform: scale(1.08) rotate(-2deg); } }

.work { padding-left: 14px; }
.work .pencil { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.work .pencil b { font-weight: 700; color: var(--ink); }
.work.total .pencil { color: var(--ink); }
.work.adj .pencil { color: var(--red); }

.val { font: 400 22px/1 var(--f-ink); color: var(--ink); white-space: nowrap; }
.val small { font-size: 15px; color: var(--pencil); margin-left: 3px; }

.grand .val {
  font-weight: 700;
  font-size: 25px;
  padding-bottom: 2px;
  border-bottom: 4px double var(--ink);
  line-height: 1.05;
}
.checked { display: inline-flex; align-items: center; gap: 4px; color: var(--ink); }
.checked svg { width: 22px; height: 22px; }

.check-bad { color: var(--red); }

/* ---------- settings view ---------- */

.struck {
  font: 400 21px/1.1 var(--f-ink);
  color: var(--pencil);
  text-decoration: line-through;
  text-decoration-color: var(--red);
  text-decoration-thickness: 2px;
}
.hist-row {
  width: 100%;
  border: 0;
  background: transparent;
  padding: 0 0 6px;
  text-align: left;
}
.hist-row:hover .hand { text-decoration: underline; text-decoration-color: var(--pencil-faint); text-underline-offset: 6px; }
.inline-label { min-width: 7.5em; padding-bottom: 4px; }
.grand .label { flex: 0 1 auto; }
.grand .checked { margin-left: auto; padding-left: 10px; white-space: nowrap; flex: none; }

@media (max-width: 440px) {
  .hist-row .pencil { display: none; }
}

/* ---------- unsaved-changes slip (a torn-off note laid on the page) ---------- */

.slip {
  width: min(340px, calc(100vw - 32px));
  padding: 18px 20px 12px 30px;
  border: 0;
  border-radius: 3px;
  color: var(--print);
  background-color: var(--paper);
  background-image: linear-gradient(to bottom, transparent calc(var(--rule) - 1px), var(--rule-c) calc(var(--rule) - 1px));
  background-size: 100% var(--rule);
  background-position: 0 6px;
  box-shadow: 0 2px 4px rgba(8, 18, 60, .25), 0 22px 48px -12px rgba(6, 14, 50, .55);
  transform: rotate(-1deg);
}
.slip::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: 16px;
  width: 1px;
  background: var(--margin-c);
}
.slip::backdrop { background: rgba(12, 22, 62, .45); }
.slip-text {
  margin: 14px 0 6px;
  font: 400 19px/1.35 var(--f-ink);
  color: var(--red);
}
.slip-actions { display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center; }
.slip-actions .pen-btn:first-child { margin-left: -6px; }
.slip[open] { animation: slip-in .24s var(--ease-out); }
@keyframes slip-in { from { transform: rotate(-3deg) translateY(10px); } }

/* ---------- motion: turning to a new page ---------- */

.page.turn { animation: turn .62s var(--ease-out); }
@keyframes turn {
  from { transform: rotateX(-24deg) translateY(-10px); filter: brightness(.94); }
  to { transform: none; filter: none; }
}
.strike-out { position: relative; }
.strike-out::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 52%;
  height: 2px;
  background: var(--red);
  transform-origin: 0 50%;
  animation: strike .4s var(--ease-out) forwards;
}
@keyframes strike { from { transform: scaleX(0); } to { transform: scaleX(1); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-delay: 0ms !important; transition-duration: .01ms !important; }
  .page.turn .amount .ring path { stroke-dashoffset: 0; }
}

/* ---------- small phones ---------- */

@media (max-width: 380px) {
  :root { --mx: 44px; --gutter: 12px; }
  .ruled::before { left: calc(var(--mx) - 13px); }
  .err-line .mark { left: calc(-1 * var(--mx) + 8px); }
  .title { font-size: 27px; }
  .label { font-size: 15px; }
  .pay-name { font-size: 21px; }
  .amount { font-size: 23px; }
  .prev .printed { display: none; }
}
