/* Rent Collection — mobile first, theme aware.
   Token system and component patterns carried over from the household-expenses
   sibling app; chart/category-specific rules dropped, a status-badge set added. */

:root {
  color-scheme: light;
  --surface-0: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --border: #e2e1dc;
  --border-strong: #cdccc5;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #78776f;

  --series-1: #1baf7a;
  --series-2: #2a78d6;

  --good: #0a7a35;
  --bad: #c02525;
  --warn: #b9770e;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(11, 11, 11, .06), 0 6px 20px rgba(11, 11, 11, .06);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --border: #333331;
    --border-strong: #464643;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #96958c;

    --series-1: #199e70;
    --series-2: #3987e5;

    --good: #4bb96f;
    --bad: #e66767;
    --warn: #e0a545;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 6px 20px rgba(0, 0, 0, .4);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #232322;
  --border: #333331;
  --border-strong: #464643;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #96958c;
  --series-1: #199e70;
  --series-2: #3987e5;
  --good: #4bb96f;
  --bad: #e66767;
  --warn: #e0a545;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must beat any class that sets `display`. Author rules
   outrank the UA's own `[hidden] { display: none }` regardless of specificity,
   so `.field { display: block }` was quietly keeping hidden fields on screen
   (the Building-use field showed for a Villa). Everything in this app that
   toggles visibility does it through `hidden`, so it needs to actually win. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 400 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface-1) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -.01em;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  border: 0;
  background: none;
  color: var(--text-secondary);
  font-size: 19px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- layout ---------- */
main { padding: 14px 12px 0; max-width: 780px; margin: 0 auto; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card > h2 {
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 650;
}
.card > .sub {
  margin: 0 0 12px;
  color: var(--text-muted);
  font-size: 13px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 10px;
}
.card-head h2 { margin: 0; font-size: 15px; font-weight: 650; }
.card-head .sub { margin: 1px 0 0; font-size: 12.5px; color: var(--text-muted); }

/* ---------- stat tiles ---------- */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.tile-label {
  display: block;
  font-size: 12px;
  font-weight: 550;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
}
.tile-value {
  display: block;
  margin-top: 4px;
  font-size: clamp(15px, 4.6vw, 21px);
  font-weight: 680;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
}
.tile-value small { font-size: 12.5px; font-weight: 550; color: var(--text-muted); white-space: nowrap; }
.tile-value.is-good { color: var(--good); }
.tile-value.is-bad { color: var(--bad); }

/* ---------- row lists (properties, tenants, payment history) ---------- */
.rows { display: flex; flex-direction: column; gap: 1px; }
.row-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  width: 100%;
  padding: 11px 4px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
/* Title block claims all the room it can, then everything after it (figure,
   badge) wraps onto its own line rather than being clipped when a row has
   both a long figure and a status badge on a narrow screen. */
.row-item > :first-child { flex: 1 1 140px; min-width: 0; margin-right: auto; }
.row-item:last-child { border-bottom: 0; }
.row-item:active { background: var(--surface-2); }
.row-item[disabled] { opacity: .55; cursor: default; }
.row-title { font-size: 14.5px; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: block; font-size: 11.5px; color: var(--text-muted); font-weight: 400; }
.row-figure { font-variant-numeric: tabular-nums; font-size: 13.5px; font-weight: 600; white-space: nowrap; }
.row-chev { color: var(--text-muted); font-size: 15px; }

/* ---------- unit grid ---------- */
.unit-legend {
  display: flex;
  gap: 14px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--text-muted);
}
.unit-legend span { display: inline-flex; align-items: center; gap: 5px; }
.unit-dot {
  width: 9px; height: 9px; border-radius: 3px; display: inline-block;
}
.unit-dot.occupied { background: var(--bad); }
.unit-dot.available { background: var(--good); }

.unit-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
}
.unit {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 2px;
  min-height: 62px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid;
  font: inherit;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
}
.unit:active { transform: scale(.97); }
.unit-num {
  font-size: 13.5px;
  font-weight: 680;
  letter-spacing: -.01em;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.unit-who {
  font-size: 11px;
  font-weight: 550;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: .85;
}
.unit.occupied {
  color: var(--bad);
  background: color-mix(in srgb, var(--bad) 13%, transparent);
  border-color: color-mix(in srgb, var(--bad) 34%, transparent);
}
.unit.available {
  color: var(--good);
  background: color-mix(in srgb, var(--good) 13%, transparent);
  border-color: color-mix(in srgb, var(--good) 34%, transparent);
}

/* ---------- unit editor (Manage units dialog) ---------- */
.unit-editor { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.unit-edit-row { display: flex; align-items: center; gap: 8px; }
.unit-edit-row input { flex: 1 1 auto; min-width: 0; }
.unit-edit-meta {
  flex: 0 1 auto;
  max-width: 34%;
  font-size: 11.5px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.unit-edit-meta.occupied { color: var(--bad); font-weight: 600; }
.unit-edit-remove { flex: 0 0 auto; font-size: 15px; }
.unit-edit-remove[disabled] { opacity: .3; cursor: default; }

/* ---------- status badges ---------- */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 650;
  white-space: nowrap;
}
.status-badge.paid {
  color: var(--good);
  background: color-mix(in srgb, var(--good) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--good) 32%, transparent);
}
.status-badge.partial {
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 16%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 36%, transparent);
}
.status-badge.overdue {
  color: var(--bad);
  background: color-mix(in srgb, var(--bad) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 32%, transparent);
}
.status-badge.upcoming {
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 28%, transparent);
}
.status-badge.unpaid, .status-badge.inactive {
  color: var(--text-muted);
  background: var(--surface-0);
  border: 1px solid var(--border);
}

/* ---------- dashboard sections ---------- */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 18px 0 8px;
  font-size: 13px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
}
.section-title .count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: var(--text-muted);
}
.section-title.is-overdue .count { background: var(--bad); }
.section-title.is-upcoming .count { background: var(--warn); }
.section-title.is-paid .count { background: var(--good); }
.section-title:first-child { margin-top: 0; }

/* ---------- toggles ---------- */
.seg {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  background: var(--surface-0);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.seg-btn {
  border: 0;
  background: none;
  color: var(--text-secondary);
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  padding: 7px 13px;
  border-radius: 7px;
  cursor: pointer;
  min-height: 36px;
}
.seg-btn.is-on {
  background: var(--surface-2);
  color: var(--text-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .08);
}

/* ---------- docked action bar ---------- */
.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  padding: 9px 12px calc(9px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface-1) 93%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  border-top: 1px solid var(--border);
}
.actionbar:has(#fab[hidden]) { display: none; }

#fab {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  box-shadow: 0 2px 10px color-mix(in srgb, var(--series-1) 34%, transparent);
}
#fab .ico { width: 19px; height: 19px; flex: 0 0 auto; }
#fab:active { transform: scale(.985); }

/* ---------- dialogs ---------- */
.sheet {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 520px;
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: var(--radius) var(--radius) 0 0;
  margin: auto auto 0;
  box-shadow: var(--shadow);
  max-height: 92vh;
  max-height: 92dvh;
  overflow-y: auto;
}
.sheet::backdrop { background: rgba(0, 0, 0, .45); }
.sheet form, .sheet > * { padding: 0; }
.sheet form { padding: 0 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet > .sheet-head, .sheet > .meta, .sheet > .menu-actions, .sheet > .hint {
  padding-left: 16px;
  padding-right: 16px;
}
.sheet > .menu-actions { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

@media (min-width: 560px) {
  .sheet { border-radius: var(--radius); margin: auto; }
}

.sheet-head {
  position: sticky;
  top: 0;
  background: var(--surface-1);
  display: flex;
  align-items: center;
  padding-top: 14px;
  padding-bottom: 6px;
  z-index: 1;
}
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 650; flex: 1; }

.field { display: block; margin: 14px 0 0; }
.field > span {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 5px;
}
.field > span small { font-weight: 400; color: var(--text-muted); }

input[type="text"], input[type="number"], input[type="date"], select {
  width: 100%;
  padding: 12px;
  font: inherit;
  font-size: 16px; /* 16px keeps iOS from zooming on focus */
  color: var(--text-primary);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  min-height: 46px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 1px;
}

/* Day / month / year rollers, in place of a native date input's calendar grid.
   iOS renders each <select> as a scrolling wheel; desktop gets dropdowns. */
.date-roller { display: flex; gap: 8px; }
.date-roller select { width: auto; flex: 1 1 0; min-width: 0; }
.date-roller .roll-day { flex-grow: .8; }
.date-roller .roll-year { flex-grow: 1.1; }

.hint { margin: 6px 0 0; font-size: 12.5px; color: var(--text-muted); }
.err {
  margin: 12px 0 0;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--bad);
  background: color-mix(in srgb, var(--bad) 9%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 28%, transparent);
  border-radius: var(--radius-sm);
}
.warn {
  margin: 6px 0 0;
  padding: 9px 11px;
  font-size: 12.5px;
  color: var(--text-secondary);
  background: color-mix(in srgb, var(--warn) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 34%, transparent);
  border-radius: var(--radius-sm);
}

.sheet-actions {
  display: flex;
  gap: 9px;
  margin-top: 20px;
}
.btn {
  flex: 1;
  padding: 13px 16px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-primary);
  cursor: pointer;
  min-height: 48px;
}
.btn-primary {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}
.btn-primary[disabled] { opacity: .6; cursor: default; }
.btn-quiet { background: var(--surface-2); }
.btn-danger { color: var(--bad); }

.menu-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.menu-actions .btn { flex: none; }

.meta { margin: 8px 0 0; font-size: 13px; }
.meta div { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.meta dt { color: var(--text-muted); }
.meta dd { margin: 0 0 0 auto; text-align: right; word-break: break-word; }

/* ---------- misc ---------- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 60px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}
.spinner {
  width: 17px; height: 17px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--series-1);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { color: var(--text-muted); font-size: 13.5px; text-align: center; padding: 18px 0; }

.splash { text-align: center; padding: 36px 18px; }
.splash h2 { font-size: 21px; margin: 0 0 8px; letter-spacing: -.02em; }
.splash p { color: var(--text-secondary); font-size: 14.5px; margin: 0 auto 20px; max-width: 42ch; }
.splash .btn { flex: none; display: inline-block; min-width: 220px; }
.splash ol { text-align: left; max-width: 46ch; margin: 0 auto 20px; color: var(--text-secondary); font-size: 14px; line-height: 1.6; }
.splash code {
  font-size: 12.5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  word-break: break-all;
}

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(92px + env(safe-area-inset-bottom));
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 11px 17px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 550;
  box-shadow: var(--shadow);
  z-index: 40;
  max-width: calc(100vw - 32px);
  text-align: center;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
