/* Tally — the household energy ledger.
   Direction: quiet, legible, mobile-first. Numbers are the content, so figures
   use tabular monospace ("accounting tape") and everything else stays disciplined.
   One accent (teal). Semantic green for deficit-on-track, amber for surplus. */

:root {
  --bg: #eef2f0;
  --surface: #ffffff;
  --surface-2: #f6f8f7;
  --ink: #15242a;
  --muted: #5d6c71;
  --faint: #8a989d;
  --line: #e0e6e4;
  --line-strong: #cfd8d5;

  --accent: #0f7d66;
  --accent-ink: #0a5f4d;
  --deficit: #0f7d66;   /* on-track: eating under expenditure */
  --surplus: #b8642a;   /* over expenditure */
  --danger: #b23b3b;

  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(20,40,42,.05), 0 6px 20px rgba(20,40,42,.06);
  --tap: 44px;

  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "SFMono-Regular", "Menlo", "Cascadia Code", "Roboto Mono", "Consolas", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  line-height: 1.45;
}
[hidden] { display: none !important; }
button { font-family: inherit; }

.num { font-family: var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.error { color: var(--danger); font-size: .9rem; }

/* ---------- Login ---------- */
.login {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px;
}
.login-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px 24px; width: 100%; max-width: 340px;
  display: flex; flex-direction: column; gap: 10px; text-align: center;
}
.login-mark { display: grid; place-items: center; margin-bottom: 2px; }
.login-card h1 { margin: 0; font-size: 1.5rem; letter-spacing: -.02em; }
.login-card p { margin: 0; }
.login-card label { text-align: left; font-size: .82rem; color: var(--muted); margin-top: 8px; }
.login-card input {
  width: 100%; padding: 12px 14px; font-size: 1rem; border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm); background: var(--surface-2);
}

/* ---------- App chrome ---------- */
#app { max-width: 620px; margin: 0 auto; padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
.topbar {
  position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, #fff);
  backdrop-filter: saturate(1.1) blur(8px);
  border-bottom: 1px solid var(--line);
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
}
.topbar-row { display: flex; align-items: center; gap: 10px; }
.datebar { justify-content: center; margin-top: 8px; }
.icon-btn {
  min-width: var(--tap); height: 38px; padding: 0 10px; border: 1px solid var(--line);
  background: var(--surface); border-radius: 10px; color: var(--ink); font-size: 1.1rem;
  cursor: pointer; display: inline-grid; place-items: center;
}
.icon-btn:active { transform: translateY(1px); }
.date-label {
  border: none; background: transparent; font-weight: 600; font-size: 1rem; cursor: pointer;
  padding: 6px 12px; border-radius: 8px; min-width: 180px;
}
.date-label:hover { background: var(--surface-2); }
.chip {
  border: 1px solid var(--accent); color: var(--accent-ink); background: transparent;
  border-radius: 999px; padding: 5px 12px; font-size: .8rem; font-weight: 600; cursor: pointer;
}

.segmented {
  display: inline-flex; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 999px; padding: 3px; flex: 1; min-width: 0;
}
.segmented button {
  flex: 1; border: none; background: transparent; padding: 7px 10px; border-radius: 999px;
  font-weight: 600; color: var(--muted); cursor: pointer; font-size: .92rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.segmented button[aria-selected="true"] {
  background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(20,40,42,.12);
}

/* ---------- Views ---------- */
.view { padding: 14px 14px 8px; display: flex; flex-direction: column; gap: 14px; }
.view:focus { outline: none; }
.view-title { font-size: 1.05rem; font-weight: 700; letter-spacing: -.01em; margin: 2px 2px -2px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.card + .card { margin-top: 0; }
.card-h { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 0 0 10px; font-weight: 700; }

/* Balance hero (the signature element) */
.balance {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 18px 14px;
}
.balance-label { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); font-weight: 700; }
.balance-big { font-size: 2.6rem; font-weight: 700; margin: 2px 0 0; letter-spacing: -.02em; }
.balance-big.deficit { color: var(--deficit); }
.balance-big.surplus { color: var(--surplus); }
.balance-tag { font-size: .95rem; font-weight: 600; }
.balance-tag.deficit { color: var(--deficit); }
.balance-tag.surplus { color: var(--surplus); }
.ledger {
  margin-top: 14px; border-top: 1px dashed var(--line-strong); padding-top: 12px;
  display: grid; gap: 8px;
}
.ledger-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.ledger-row .k { color: var(--muted); }
.ledger-row .v { font-weight: 600; }
.ledger-row.total { border-top: 1px solid var(--line); padding-top: 8px; margin-top: 2px; }
.ledger-sub { padding-left: 14px; font-size: .92rem; color: var(--muted); }

.rowlist { display: flex; flex-direction: column; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 4px; border-top: 1px solid var(--line);
}
.item:first-child { border-top: none; }
.item .grow { flex: 1; min-width: 0; }
.item .t { font-weight: 600; }
.item .s { font-size: .85rem; color: var(--muted); }
.item .kcal { font-weight: 700; white-space: nowrap; }
.linkbtn { background: none; border: none; color: var(--accent-ink); font-weight: 600; cursor: pointer; padding: 6px; font-size: .9rem; }

.empty { text-align: center; color: var(--muted); padding: 18px 6px; }

/* quick actions */
.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.quick button {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 14px 8px;
  border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius-sm);
  cursor: pointer; font-weight: 600; color: var(--ink); font-size: .82rem;
}
.quick button .qi { font-size: 1.2rem; color: var(--accent); }
.quick button:active { transform: translateY(1px); }

/* buttons */
.btn {
  min-height: var(--tap); padding: 11px 16px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--ink); font-weight: 600; font-size: 1rem; cursor: pointer;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:active { background: var(--accent-ink); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

/* forms */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: .84rem; color: var(--muted); font-weight: 600; }
.field input, .field select {
  padding: 12px 14px; font-size: 1rem; border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--ink); width: 100%;
}
.field .hint { font-size: .78rem; color: var(--faint); }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.seg-choice { display: flex; gap: 8px; }
.seg-choice button {
  flex: 1; padding: 10px; border: 1px solid var(--line-strong); background: var(--surface-2);
  border-radius: var(--radius-sm); font-weight: 600; color: var(--muted); cursor: pointer;
}
.seg-choice button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

/* stats grid */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 12px; }
.stat .k { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--faint); font-weight: 700; }
.stat .v { font-size: 1.4rem; font-weight: 700; margin-top: 2px; }
.stat .v small { font-size: .8rem; font-weight: 600; color: var(--muted); }

/* period selector */
.periods { display: flex; gap: 8px; }
.periods button {
  flex: 1; padding: 8px; border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  font-weight: 600; color: var(--muted); cursor: pointer; font-size: .85rem;
}
.periods button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

/* bars */
.bar { display: grid; grid-template-columns: 96px 1fr auto; align-items: center; gap: 10px; padding: 7px 0; }
.bar .lab { font-size: .9rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .track { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar .fill { height: 100%; background: var(--accent); border-radius: 999px; }
.bar .val { font-weight: 700; font-size: .9rem; white-space: nowrap; }

svg.chart { width: 100%; height: auto; display: block; }

.note {
  font-size: .82rem; color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px;
}
.note strong { color: var(--ink); }

.badge-warn {
  display: inline-block; font-size: .72rem; font-weight: 700; color: var(--surplus);
  background: color-mix(in srgb, var(--surplus) 12%, #fff); border: 1px solid color-mix(in srgb, var(--surplus) 30%, var(--line));
  padding: 2px 8px; border-radius: 999px;
}

/* history rows */
.hrow { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; padding: 12px 4px; border-top: 1px solid var(--line); cursor: pointer; }
.hrow:first-child { border-top: none; }
.hrow .hd { font-weight: 600; }
.hrow .hs { font-size: .82rem; color: var(--muted); }
.hrow .hb { font-weight: 700; text-align: right; }
.hrow .hb.deficit { color: var(--deficit); }
.hrow .hb.surplus { color: var(--surplus); }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--surface) 92%, #fff);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  max-width: 620px; margin: 0 auto;
}
.tab {
  border: none; background: none; padding: 9px 0 7px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: .68rem; font-weight: 600; cursor: pointer;
}
.tab .tab-i { font-size: 1.15rem; line-height: 1; }
.tab[aria-selected="true"] { color: var(--accent-ink); }

/* ---------- Sheet ---------- */
.sheet-host { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(15,30,32,.4); }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; margin: 0 auto; max-width: 620px;
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: 0 -8px 30px rgba(20,40,42,.2);
  padding: 8px 18px calc(20px + env(safe-area-inset-bottom)); max-height: 92dvh; overflow-y: auto;
  animation: sheet-up .22s ease;
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle { width: 40px; height: 4px; background: var(--line-strong); border-radius: 999px; margin: 6px auto 4px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; font-size: 1.15rem; letter-spacing: -.01em; }
.sheet-body { padding-bottom: 4px; }

.menu-list { display: flex; flex-direction: column; }
.menu-list button {
  text-align: left; background: none; border: none; border-top: 1px solid var(--line);
  padding: 15px 4px; font-size: 1rem; color: var(--ink); cursor: pointer; display: flex; justify-content: space-between; gap: 10px;
}
.menu-list button:first-child { border-top: none; }
.menu-list button.danger { color: var(--danger); }
.menu-list .mi-sub { color: var(--faint); font-size: .85rem; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 999px; font-size: .9rem; z-index: 60;
  box-shadow: var(--shadow); max-width: 90vw; text-align: center;
}

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

@media (prefers-reduced-motion: reduce) {
  .sheet { animation: none; }
  * { transition: none !important; }
}

@media (min-width: 640px) {
  .quick { grid-template-columns: repeat(3, 1fr); }
}
