:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1f2933;
  --muted: #6b7280;
  --border: #e3e6ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --good: #15803d;
  --good-bg: #dcfce7;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --warn-bg: #fef3c7;
  --warn: #92400e;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); line-height: 1.45; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
h4 { font-size: .9rem; margin: 0 0 .4rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
main { max-width: 1150px; margin: 0 auto; padding: 1.25rem; }
section { margin-bottom: 1.5rem; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  background: #111827; color: #fff; padding: .6rem 1.25rem;
}
.topbar .brand a { color: #fff; font-weight: 700; font-size: 1.1rem; }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.topbar nav a { color: #cbd5e1; padding: .35rem .6rem; border-radius: 6px; }
.topbar nav a:hover { background: #1f2937; color: #fff; text-decoration: none; }
.topbar nav a.active { background: #374151; color: #fff; }
.topbar nav a.preview-link { margin-left: auto; color: #fde68a; }
.topbar .logout { display: flex; gap: .5rem; align-items: center; margin-left: auto; }
.topbar .logout .muted { color: #9ca3af; }
.topbar .logout button { color: #cbd5e1; }
.preview-banner { background: #fde68a; color: #78350f; padding: .5rem 1.25rem; text-align: center; }
.preview-banner a { margin-left: 1rem; font-weight: 600; color: #78350f; }

/* Layout */
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head .buttons { display: flex; gap: .5rem; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; margin-bottom: 1rem; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.cards .card { margin: 0; }
.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1.25rem; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-value { font-size: 1.6rem; font-weight: 700; margin: .2rem 0; font-variant-numeric: tabular-nums; }
.highlight-card { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary) inset; }
.login-box { max-width: 360px; margin: 4rem auto; }

/* Text helpers */
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.nowrap { white-space: nowrap; }
.amount { font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount.neg { color: var(--bad); }
.amount.pos, .pos { color: var(--good); }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; background: #e5e7eb; color: #374151; }
.badge-good { background: var(--good-bg); color: var(--good); }
.badge-bad { background: var(--bad-bg); color: var(--bad); }

/* Flash messages */
.flash { padding: .6rem .9rem; border-radius: 8px; margin-bottom: .75rem; background: var(--good-bg); color: var(--good); }
.flash-error { background: var(--bad-bg); color: var(--bad); }
.flash-warn { background: var(--warn-bg); color: var(--warn); }

/* Tables */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .5rem .7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: #fafbfc; }
tr:last-child td { border-bottom: none; }
.num { text-align: right; }
td.actions { white-space: nowrap; text-align: right; }
td.actions > * { margin-left: .5rem; }
tr.highlight td { background: #fffbeb; }
table.compact td { padding: .3rem .4rem; }
table.compact tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
.card table th { background: transparent; }
ul.splits, ul.files, ul.components { list-style: none; margin: 0; padding: 0; }
ul.splits li { display: flex; justify-content: space-between; gap: 1rem; }
ul.components { font-size: .82rem; color: var(--muted); margin: .2rem 0 0 .75rem; }
ul.components li { display: flex; justify-content: space-between; gap: 1rem; max-width: 320px; }
ul.files li { display: flex; align-items: center; gap: .3rem; }

/* Meter tiles (tenant view): six per row on desktop */
.meter-tiles { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: .75rem; }
.meter-tiles .card { margin: 0; padding: .75rem .9rem; }
.meter-tiles .stat-value { font-size: 1.25rem; overflow-wrap: anywhere; }
@media (max-width: 1000px) { .meter-tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 560px) { .meter-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.order-buttons { display: inline-flex; gap: .25rem; }
.order-buttons button { padding: .1rem .45rem; }

/* Monthly summary */
.months { display: flex; flex-direction: column; gap: .5rem; }
details.month { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); }
details.month > summary {
  cursor: pointer; padding: .7rem 1rem; display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; list-style: none;
}
details.month > summary::-webkit-details-marker { display: none; }
details.month > summary::before { content: "▸"; color: var(--muted); margin-right: .25rem; }
details.month[open] > summary::before { content: "▾"; }
.month-name { font-weight: 600; text-transform: capitalize; flex: 1; }
.month-figures { display: flex; gap: 1.5rem; flex-wrap: wrap; font-variant-numeric: tabular-nums; }
.month-body { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.5rem; padding: .25rem 1rem 1rem; border-top: 1px solid var(--border); padding-top: .75rem; }

/* Forms */
label { display: flex; flex-direction: column; gap: .25rem; font-size: .88rem; font-weight: 500; }
label.check { flex-direction: row; align-items: center; gap: .4rem; font-weight: 400; }
input, select, textarea {
  font: inherit; padding: .45rem .6rem; border: 1px solid #cfd4da; border-radius: 6px; background: #fff; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
input[type=checkbox] { width: auto; }
.stack { display: flex; flex-direction: column; gap: .8rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .8rem 1rem; }
.grid .wide { grid-column: 1 / -1; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row > input, .row > select { flex: 1; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem; }
legend { font-weight: 600; padding: 0 .3rem; }
.repeater [data-rows] { display: flex; flex-direction: column; gap: .5rem; margin-bottom: .5rem; }
[data-sum-out].warn { color: var(--warn); }
.advance-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .3rem 1rem; margin: .5rem 0; }
.advance-list .amount { margin-left: auto; }
form.inline { display: inline; }
details.inline-form > summary { cursor: pointer; color: var(--primary); margin: .25rem 0; }
details.inline-form[open] > summary { margin-bottom: .6rem; }
details.card.inline-form > summary { font-weight: 600; }

/* Buttons */
button, .button {
  font: inherit; cursor: pointer; display: inline-block; padding: .45rem .9rem; border-radius: 6px;
  border: 1px solid #cfd4da; background: #fff; color: var(--text);
}
button:hover, .button:hover { background: #f3f4f6; text-decoration: none; }
button.primary, .button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover, .button.primary:hover { background: var(--primary-dark); }
button.link { border: none; background: none; padding: 0; color: var(--primary); }
button.link:hover { text-decoration: underline; background: none; }
button.danger, .danger { color: var(--bad); }

.show-mobile { display: none; }

@media (max-width: 640px) {
  main { padding: .75rem; }
  /* Tenant on a phone: summary only — brand on the left, user + logout on the right, last 3 months. */
  .tenant-view .topbar nav { display: none; }
  .tenant-view .topbar { flex-wrap: nowrap; }
  .tenant-view .months > details.month:nth-child(n+4) { display: none; }
  .tenant-view .hide-mobile { display: none; }
  .tenant-view .show-mobile { display: inline; }
  .topbar nav a.preview-link { margin-left: 0; }
  .month-figures { gap: .75rem; font-size: .9rem; }
}
