:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --border: #e3e6eb;
  --primary: #2459d6;
  --primary-hover: #1c48b0;
  --primary-soft: #e8efff;
  --accent: #0e9f8e;
  --danger: #c4322a;
  --danger-soft: #fdecea;
  --success: #17803d;
  --success-soft: #e7f6ec;
  --info-soft: #eef2f7;
  --radius: 10px;
  --heat-1: #dbe6ff;
  --heat-2: #a9c2fb;
  --heat-3: #6f97f1;
  --heat-4: #2f62d9;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161d;
    --surface: #1b212b;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --border: #2c3442;
    --primary: #5b8cff;
    --primary-hover: #7aa2ff;
    --primary-soft: #1f2d4d;
    --accent: #2cc4b0;
    --danger: #f06b63;
    --danger-soft: #3a1f1f;
    --success: #4cc27a;
    --success-soft: #19301f;
    --info-soft: #222a36;
    --heat-1: #1f2f52;
    --heat-2: #28447f;
    --heat-3: #3460c0;
    --heat-4: #5b8cff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.req { color: var(--danger); }

/* Шапка */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { display: flex; align-items: center; gap: 20px; min-height: 56px; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); }
.nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.nav a { padding: 8px 12px; border-radius: 8px; color: var(--muted); white-space: nowrap; }
.nav a:hover { background: var(--info-soft); text-decoration: none; color: var(--text); }
.nav a[aria-current="page"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

main { padding-top: 24px; padding-bottom: 24px; }
.footer { color: var(--muted); font-size: .85rem; padding-bottom: 32px; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Кнопки */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
  cursor: pointer; white-space: nowrap; line-height: 1.2;
}
.btn:hover { text-decoration: none; border-color: var(--muted); }
.btn-sm { padding: 6px 12px; font-size: .9rem; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger-outline { color: var(--danger); border-color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--border); }
.link-danger { color: var(--danger); }
form.inline { display: inline; }

/* Карточки */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.narrow { max-width: 560px; }
.confirm h1 { margin-bottom: 12px; font-size: 1.3rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 { margin-bottom: 16px; }
.danger-zone p { margin: 12px 0 0; }

/* Показатели */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 8px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat-text { font-size: 1.05rem; }

/* Фильтры и формы */
.filters { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; margin: 16px 0; }
.filters .field { margin: 0; min-width: 160px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field > span { font-size: .9rem; font-weight: 500; }
input[type=text], input[type=password], input[type=date], input[type=datetime-local], select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; width: 100%;
  min-height: 40px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.has-error input, .has-error select, .has-error textarea { border-color: var(--danger); }
.field-error { color: var(--danger); font-size: .85rem; }
.form { max-width: 720px; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.row-2 .field { margin-bottom: 0; }
.hint { margin: 6px 0 16px; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.js .filters [data-custom-period].hidden { display: none; }

/* Сообщения */
.flash { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; border: 1px solid transparent; }
.flash-success { background: var(--success-soft); color: var(--success); border-color: currentColor; }
.flash-error { background: var(--danger-soft); color: var(--danger); border-color: currentColor; }
.flash-info { background: var(--info-soft); }
.flash a { color: inherit; text-decoration: underline; }

/* Таблицы */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table th { font-size: .8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }
.table tbody tr:last-child td { border-bottom: none; }
.table .num { text-align: right; }
.table .actions { text-align: right; white-space: nowrap; }
.table .actions a { margin-left: 12px; }
.clip { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { display: inline-block; font-size: .75rem; padding: 1px 8px; border-radius: 999px; background: var(--info-soft); color: var(--muted); vertical-align: middle; font-weight: 500; }

.bars td:first-child { width: 100%; }
.bar-track { height: 8px; background: var(--info-soft); border-radius: 4px; margin-top: 6px; overflow: hidden; }
.bar { height: 100%; background: var(--primary); border-radius: 4px; min-width: 0; }
.bar-alt { background: var(--accent); }

.heatmap th, .heatmap td { text-align: center; padding: 8px 6px; font-size: .85rem; border: 2px solid var(--surface); }
.heatmap thead th { text-transform: none; letter-spacing: 0; white-space: nowrap; font-weight: 500; }
.heatmap td { background: var(--info-soft); border-radius: 4px; min-width: 44px; }
.heatmap .heat-1 { background: var(--heat-1); }
.heatmap .heat-2 { background: var(--heat-2); }
.heatmap .heat-3 { background: var(--heat-3); color: #fff; }
.heatmap .heat-4 { background: var(--heat-4); color: #fff; font-weight: 700; }

.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: none; }

.details { display: grid; grid-template-columns: 180px 1fr; gap: 10px 16px; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; overflow-wrap: anywhere; }

.insights ul { margin: 0 0 12px; padding-left: 20px; }
.insights li { margin-bottom: 6px; }
.settings summary { cursor: pointer; font-weight: 600; }
.settings form { margin-top: 16px; }

.empty { text-align: center; padding: 40px 20px; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); margin-bottom: 16px; }
.empty h2 { margin-bottom: 8px; }
.empty p { color: var(--muted); margin: 0 0 16px; }
.empty-inline { border: none; padding: 24px 8px; margin: 0; }

.pager { display: flex; justify-content: center; align-items: center; gap: 16px; padding-top: 16px; }

/* Вкладки */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a[aria-current="page"] { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.trend td:first-child { white-space: nowrap; }
.occ-low { color: var(--danger); }

/* Телефоны и планшеты */
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  body { font-size: 15px; }
  .topbar-inner { gap: 8px 12px; padding-top: 8px; padding-bottom: 8px; }
  .nav { order: 3; flex-basis: 100%; flex-wrap: wrap; overflow: visible; gap: 2px; }
  .nav a { padding: 6px 9px; font-size: .92rem; }
  .topbar-add { margin-left: auto; }
  .topbar-user { order: 4; flex-basis: 100%; justify-content: space-between; border-top: 1px solid var(--border); padding-top: 6px; }
  main { padding-top: 16px; }
  h1 { font-size: 1.3rem; }
  .card { padding: 14px; }
  .stat-value { font-size: 1.2rem; }
  .row-2 { grid-template-columns: 1fr; gap: 0; }
  .row-2 .field { margin-bottom: 16px; }
  .filters .field { flex: 1 1 100%; }
  .filters .btn { flex: 1; }
  .hide-sm { display: none; }
  /* Таблица бронирований превращается в карточки */
  .table-stack thead { display: none; }
  .table-stack, .table-stack tbody { display: block; }
  .table-stack tr { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
  .table-stack tbody tr:last-child { border-bottom: none; }
  .table-stack td { display: block; padding: 0; border: none; white-space: normal; }
  .table-stack td[data-label] { grid-column: 1 / -1; }
  .table-stack td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: .85rem; }
  .table-stack td[data-label] .small { display: inline; margin-left: 4px; }
  .table-stack .cell-id { grid-column: 1; }
  .table-stack .cell-apt { grid-column: 2; font-weight: 600; }
  .table-stack .cell-amount { grid-column: 3; grid-row: 1; text-align: right; font-weight: 600; }
  .table-stack .cell-amount::before { content: none !important; }
  .table-stack .cell-note { grid-column: 1 / -1; max-width: none; }
  .table-stack .cell-note:empty, .table-stack .cell-note:has(span:empty:only-child) { display: none; }
  .details { grid-template-columns: 1fr; gap: 2px; }
  .details dd { margin-bottom: 10px; }
  .table th, .table td { padding: 8px 6px; }
}

/* Вход и пользователи */
.login { margin: 8vh auto 0; }
.login h1 { margin-bottom: 16px; }
.link-button { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
.link-button:hover { text-decoration: underline; }
.topbar-user { display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: .9rem; white-space: nowrap; }
.topbar-user strong { color: var(--text); }
.badge-admin { background: var(--primary-soft); color: var(--primary); }
/* Деньги и долги */
.badge-debt { background: var(--danger-soft); color: var(--danger); }
.badge-paid { background: var(--success-soft); color: var(--success); }
.text-debt { color: var(--danger); }
.flash-debt { background: var(--danger-soft); color: var(--danger); border-color: currentColor; }
.debt-banner { display: block; }
.debt-banner:hover { text-decoration: none; filter: brightness(.97); }
.payments li { display: flex; gap: 6px; flex-wrap: wrap; align-items: baseline; }
.pay-form { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); max-width: 560px; }
.pay-form h3 { margin: 0 0 12px; font-size: 1rem; }
.field-check { flex-direction: row; align-items: center; gap: 8px; min-height: 40px; }
.field-check input { width: 18px; height: 18px; }
.expense-list li { padding: 10px 0; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 2px; }
.expense-list li:last-child { border-bottom: 0; }
.trash-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 0; border-bottom: 1px solid var(--border); }
.trash-row:last-child, .user-row:last-of-type { border-bottom: 0; }
.trash-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.edit-link { margin-left: 6px; }
.user-row { padding: 12px 0; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 4px; }
.user-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 6px; }
.user-password { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.user-password input { width: 180px; min-height: 32px; padding: 5px 9px; }

/* Приложение на главном экране телефона: не залезать под вырез и полоску «Домой» */
.topbar { padding-top: env(safe-area-inset-top); }
.wrap { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
.footer { padding-bottom: max(32px, env(safe-area-inset-bottom)); }
