:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6a66;
  --border: #e3e1dc;
  --accent: #1f6f5c;
  --accent-text: #ffffff;
  --accent-soft: #e4f1ed;
  --energy: #c77d12;
  --water: #2a6fb0;
  --trash: #5d7a2f;
  --gas: #be3455;
  --internet: #7c3aed;
  --other: #6b6a66;
  --ok: #1f7a3e;
  --ok-soft: #e5f4ea;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --danger: #b3261e;
  --danger-soft: #fbe7e5;
  --radius: 10px;
  color-scheme: light;
}

/* Dark theme: chosen in the profile ("dark"), or following the device ("auto"). Light is the default. */
:root[data-theme="dark"] {
  --bg: #161615;
  --surface: #1f1f1d;
  --text: #ecebe7;
  --muted: #a3a19b;
  --border: #34332f;
  --accent: #4fb89a;
  --accent-text: #0d1f1a;
  --accent-soft: #1c302a;
  --energy: #e6a33e;
  --water: #6aa7e0;
  --trash: #95b85c;
  --gas: #f07a93;
  --internet: #a78bfa;
  --ok: #5cc97f;
  --ok-soft: #18291e;
  --warn: #e6a33e;
  --warn-soft: #2e2414;
  --danger: #ef7a72;
  --danger-soft: #331b19;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #161615;
    --surface: #1f1f1d;
    --text: #ecebe7;
    --muted: #a3a19b;
    --border: #34332f;
    --accent: #4fb89a;
    --accent-text: #0d1f1a;
    --accent-soft: #1c302a;
    --energy: #e6a33e;
    --water: #6aa7e0;
    --trash: #95b85c;
    --gas: #f07a93;
    --internet: #a78bfa;
    --ok: #5cc97f;
    --ok-soft: #18291e;
    --warn: #e6a33e;
    --warn-soft: #2e2414;
    --danger: #ef7a72;
    --danger-soft: #331b19;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1040px; margin: 0 auto; padding: .6rem 16px;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); margin-right: auto; }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; align-items: center; }
.nav a, .nav button {
  text-decoration: none; color: var(--muted); padding: .35rem .6rem; border-radius: 6px;
  background: none; border: 0; font: inherit; cursor: pointer;
}
.nav a:hover, .nav button:hover, .nav a.active { color: var(--text); background: var(--bg); }

main { max-width: 1040px; margin: 0 auto; padding: 1.5rem 16px 3rem; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem 1.25rem; margin-bottom: 1rem;
}
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.6rem; font-weight: 650; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .55rem .5rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--bg); }
tfoot td { font-weight: 600; }

.badge {
  display: inline-block; padding: .1rem .5rem; border-radius: 99px; font-size: .78rem; font-weight: 600;
  background: var(--bg); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.type { font-weight: 600; white-space: nowrap; }
.type::before { content: ""; display: inline-block; width: .6rem; height: .6rem; border-radius: 50%; margin-right: .4rem; background: var(--other); }
.type-energy::before { background: var(--energy); }
.type-water::before { background: var(--water); }
.type-trash::before { background: var(--trash); }
.type-gas::before { background: var(--gas); }
.type-internet::before { background: var(--internet); }

.btn {
  display: inline-flex; align-items: center; gap: .35rem; justify-content: center;
  padding: .5rem .95rem; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 550;
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

form.stack { display: grid; gap: .9rem; }
.form-grid { display: grid; gap: .9rem 1rem; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
label { display: block; font-weight: 550; font-size: .9rem; margin-bottom: .25rem; }
.hint { font-size: .82rem; color: var(--muted); margin-top: .2rem; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], select, textarea, input[type=file] {
  width: 100%; padding: .5rem .6rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.check { display: flex; gap: .5rem; align-items: center; font-weight: 400; }
.check input { width: auto; }
.inline-form { display: inline-flex; gap: .4rem; align-items: center; margin: 0; }
.inline-form input[type=text] { width: 7rem; padding: .3rem .45rem; }

.alert { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.danger { background: var(--danger-soft); color: var(--danger); }
.alert ul { margin: 0; padding-left: 1.1rem; }

.filters { display: flex; gap: .35rem; flex-wrap: wrap; }
.filters a { text-decoration: none; padding: .3rem .75rem; border-radius: 99px; border: 1px solid var(--border); color: var(--muted); background: var(--surface); }
.filters a.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1.25rem; margin: 0; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; font-weight: 550; }

.pdf-frame { width: 100%; height: 75vh; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }

.bars { display: flex; align-items: flex-end; gap: 6px; height: 130px; padding-top: 1rem; }
.bars .bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.bars .bar span.fill { width: 100%; max-width: 38px; background: var(--accent); border-radius: 4px 4px 0 0; opacity: .55; }
.bars .bar.current span.fill { opacity: 1; }
.bars .bar.estimate span.fill { opacity: .3; }
.bars .bar small { font-size: .7rem; color: var(--muted); margin-top: .25rem; white-space: nowrap; }
.bars .bar b { font-size: .7rem; font-weight: 600; color: var(--muted); }

.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; }

.pagination { display: flex; gap: .3rem; flex-wrap: wrap; margin-top: 1rem; }

@media (max-width: 640px) {
  .hide-sm { display: none; }
  main { padding-top: 1rem; }
  .card { padding: .9rem 1rem; }
}

/* Stacked bar chart (stats) */
.stack-chart { display: flex; align-items: flex-end; gap: 6px; height: 200px; padding-top: 1.2rem; }
.stack-chart .col { flex: 1; min-width: 0; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.stack-chart .stack { width: 100%; max-width: 46px; display: flex; flex-direction: column-reverse; border-radius: 4px 4px 0 0; overflow: hidden; min-height: 0; }
.stack-chart .seg { display: block; min-height: 2px; flex-basis: 0; }
.stack-chart .col > b { font-size: .7rem; font-weight: 600; color: var(--muted); white-space: nowrap; }
.stack-chart .col > small { font-size: .72rem; color: var(--muted); margin-top: .3rem; white-space: nowrap; }
.seg-energy { background: var(--energy); }
.seg-gas { background: var(--gas); }
.seg-water { background: var(--water); }
.seg-internet { background: var(--internet); }
.seg-trash { background: var(--trash); }
.seg-other { background: var(--other); }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; margin-top: .9rem; font-size: .85rem; }
.legend .type { font-weight: 500; }

/* Calendar */
.cal-head { display: flex; align-items: center; gap: .6rem; margin-bottom: .8rem; flex-wrap: wrap; }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-top: 1px solid var(--border); border-left: 1px solid var(--border); }
.cal-wd { font-size: .75rem; text-transform: uppercase; color: var(--muted); padding: .3rem .4rem; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); font-weight: 600; }
.cal-day { min-height: 92px; padding: .25rem .3rem; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.cal-day.out { background: var(--bg); opacity: .55; }
.cal-day.today .cal-num { background: var(--accent); color: var(--accent-text); border-radius: 99px; }
.cal-day.today .cal-num a { color: var(--accent-text); }
.cal-num { font-size: .8rem; font-weight: 600; align-self: flex-start; min-width: 1.6em; text-align: center; padding: 0 .3em; }
.cal-num a { color: inherit; text-decoration: none; }
.cal-item { display: block; font-size: .72rem; line-height: 1.3; padding: 1px 4px; border-radius: 4px; text-decoration: none; color: var(--text); background: var(--bg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.cal-item.task { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.cal-item.task.late { background: var(--danger-soft); color: var(--danger); }
.cal-item.task.done { text-decoration: line-through; opacity: .6; }
.cal-item.bill::before { width: .45rem; height: .45rem; margin-right: .25rem; }
.task-row { display: flex; justify-content: space-between; gap: .75rem; align-items: flex-start; padding: .6rem 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.task-row:last-child { border-bottom: 0; }

@media (max-width: 640px) {
  .cal-day { min-height: 58px; padding: 2px; }
  .cal-item { font-size: 0; padding: 0; height: 6px; border-radius: 3px; }
  .cal-item.bill { background: var(--other); }
  .cal-item.bill.type-energy { background: var(--energy); }
  .cal-item.bill.type-gas { background: var(--gas); }
  .cal-item.bill.type-water { background: var(--water); }
  .cal-item.bill.type-internet { background: var(--internet); }
  .cal-item.bill.type-trash { background: var(--trash); }
  .cal-item.task { background: var(--accent); }
  .cal-item.task.late { background: var(--danger); }
  .cal-item.bill::before { display: none; }
}

/* ---------- Phones ---------- */
.tabbar, .show-sm { display: none; }
.grid > .card { margin: 0; }
.grid { margin-bottom: 1rem; }

@media (max-width: 720px) {
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .topbar .nav { display: none; }
  .topbar-inner { padding: .7rem 16px; }
  .brand { font-size: 1.05rem; }
  main { padding: 1rem 12px 2rem; }
  h1 { font-size: 1.35rem; }
  .page-head { margin-bottom: 1rem; align-items: flex-start; }
  .card { padding: .85rem .9rem; }
  .stat .value { font-size: 1.35rem; }
  .grid { gap: .6rem; grid-template-columns: 1fr; }
  .grid:has(> .stat) { grid-template-columns: 1fr 1fr; }
  .grid > .card.wide, .grid > .card:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .hide-sm { display: none !important; }
  .show-sm { display: block; }
  .filters { flex-wrap: nowrap; overflow-x: auto; padding-bottom: .25rem; scrollbar-width: none; margin-left: -12px; margin-right: -12px; padding-left: 12px; padding-right: 12px; }
  .filters a { white-space: nowrap; }
  .btn { padding: .55rem .9rem; }
  .pdf-frame { display: none; }

  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
    background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar > a, .tabbar summary {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    height: 60px; color: var(--muted); text-decoration: none; font-size: .7rem; font-weight: 600;
    list-style: none; cursor: pointer;
  }
  .tabbar summary::-webkit-details-marker { display: none; }
  .tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .tabbar > a.active, .tabmenu[data-active] > summary, .tabmenu[open] > summary { color: var(--accent); }
  .tabmenu { flex: 1; display: flex; }
  .tabmenu > summary { width: 100%; }
  .tabmenu-panel {
    position: fixed; left: 8px; right: 8px; bottom: calc(68px + env(safe-area-inset-bottom));
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    box-shadow: 0 10px 30px rgba(0,0,0,.18); padding: .4rem; display: flex; flex-direction: column;
  }
  .tabmenu-panel a, .tabmenu-panel button {
    display: block; width: 100%; text-align: left; padding: .8rem .9rem; border-radius: 10px;
    color: var(--text); text-decoration: none; font: inherit; font-weight: 550; background: none; border: 0; cursor: pointer;
  }
  .tabmenu-panel a:hover, .tabmenu-panel button:hover { background: var(--bg); }
  .tabmenu-panel form { margin: 0; border-top: 1px solid var(--border); margin-top: .3rem; padding-top: .3rem; }
  .tabmenu-panel button { color: var(--danger); }

  /* Tables become stacked, labelled rows */
  table.rtable thead { display: none; }
  table.rtable, .rtable tbody, .rtable tfoot, .rtable tr { display: block; width: 100%; }
  .rtable tr { padding: .55rem 0; border-bottom: 1px solid var(--border); }
  .rtable tr:last-child { border-bottom: 0; }
  .rtable td { display: flex; justify-content: space-between; align-items: center; gap: 1rem; border: 0; padding: .18rem 0; text-align: right; }
  .rtable td::before { content: attr(data-label); color: var(--muted); font-size: .82rem; text-align: left; font-weight: 500; }
  .rtable td.rt-title { font-weight: 650; justify-content: flex-start; text-align: left; font-size: 1rem; }
  .rtable td.rt-title::before { display: none; }
  .rtable td:empty { display: none; }
  .rtable td.hide-sm:not(:empty) { display: flex !important; }
  .rtable tfoot tr { border-top: 2px solid var(--border); }

  /* Bill list (dashboard) */
  .bill-list { margin: -.3rem 0; }
  .bill-item { display: flex; justify-content: space-between; gap: .75rem; padding: .7rem 0; border-bottom: 1px solid var(--border); text-decoration: none; color: inherit; }
  .bill-item:last-child { border-bottom: 0; }
  .bill-item .bi-main { min-width: 0; }
  .bill-item .bi-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .bill-item .bi-sub { font-size: .8rem; color: var(--muted); }
  .bill-item .bi-side { text-align: right; flex-shrink: 0; display: flex; flex-direction: column; align-items: flex-end; gap: .2rem; }
  .bill-item .bi-amount { font-weight: 650; font-variant-numeric: tabular-nums; }

  .task-row .actions { width: 100%; }
  .cal-head h2 { font-size: 1rem; }
}

/* Month agenda under the calendar */
.agenda { margin-top: 1rem; border-top: 1px solid var(--border); }
.agenda-item { display: flex; gap: .75rem; align-items: baseline; padding: .55rem 0; border-bottom: 1px solid var(--border); text-decoration: none; color: inherit; }
.agenda-item:last-child { border-bottom: 0; }
.agenda-date { flex: 0 0 3.6rem; font-weight: 650; font-variant-numeric: tabular-nums; color: var(--muted); font-size: .85rem; }
.agenda-date.today { color: var(--accent); }
.agenda-text { flex: 1; min-width: 0; }
.agenda-text small { display: block; color: var(--muted); }
.agenda-item.late .agenda-text strong { color: var(--danger); }
.agenda-item.done .agenda-text strong { text-decoration: line-through; opacity: .6; }

.matrix th { text-transform: none; letter-spacing: 0; }
@media (max-width: 720px) {
  .matrix th, .matrix td { padding: .45rem .25rem; font-size: .8rem; }
  .matrix input[type=checkbox] { width: 20px; height: 20px; }
}
