:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 8px 24px rgba(15, 23, 42, 0.06);
  font-synthesis: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: #f1f5f9;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.brand {
  font-weight: 800;
  font-size: 1.2rem;
  letter-spacing: -0.02em;
  color: var(--ink);
}
.brand span {
  font-weight: 500;
  color: var(--muted);
  margin-left: 0.4em;
  font-size: 0.8rem;
}

/* ── Кнопки ─────────────────────────────── */
.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.55rem 0.95rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s, background 0.15s, border-color 0.15s;
  font-family: inherit;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-ghost:hover { background: #f8fafc; }
.btn-danger { background: var(--red-bg); color: var(--red); }
.btn-danger:hover { filter: brightness(0.97); }
.btn-sm { padding: 0.35rem 0.6rem; font-size: 0.8rem; border-radius: 8px; }

/* ── Логин ──────────────────────────────── */
.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
  background: radial-gradient(1200px 600px at 50% -10%, #1e293b, #0f172a);
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border-radius: 18px;
  padding: 1.75rem 1.5rem;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.login-card .brand { font-size: 1.4rem; margin-bottom: 0.25rem; }
label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.82rem; font-weight: 600; color: var(--muted); }
input, select {
  font: inherit;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.error { color: var(--red); font-size: 0.82rem; min-height: 1em; margin: 0; font-weight: 600; }
.hint { color: var(--muted); font-size: 0.76rem; margin: 0; line-height: 1.4; }

/* ── Топбар ─────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1.1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar-right { display: flex; align-items: center; gap: 0.6rem; }
.badge {
  font-size: 0.74rem;
  font-weight: 700;
  padding: 0.3rem 0.6rem;
  border-radius: 999px;
  background: var(--green-bg);
  color: var(--green);
  white-space: nowrap;
}
.badge.guest { background: var(--amber-bg); color: var(--amber); }

.container { max-width: 1000px; margin: 0 auto; padding: 1.1rem; }

/* ── Боковое меню ───────────────────────── */
.app-shell { display: flex; align-items: flex-start; }
.sidebar {
  width: 210px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 1rem 0.7rem;
  min-height: calc(100dvh - 58px);
}
.side-nav { display: flex; flex-direction: column; gap: 0.25rem; }
.side-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  font: inherit;
  font-weight: 600;
  font-size: 0.92rem;
  color: var(--muted);
  cursor: pointer;
}
.side-item .ico { font-size: 1.05rem; line-height: 1; }
.side-item:hover { background: #f1f5f9; color: var(--ink); }
.side-item.active { background: var(--ink); color: #fff; }
.content { flex: 1; min-width: 0; padding: 1.1rem; }
.placeholder {
  background: var(--panel);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 3rem 1.5rem;
  text-align: center;
  color: var(--muted);
}
.placeholder-ico { font-size: 2.5rem; margin-bottom: 0.4rem; }
.placeholder h2 { margin: 0.2rem 0; color: var(--ink); }
.placeholder p { margin: 0; font-size: 0.9rem; }
.chart-wrap { position: relative; height: 340px; padding: 1rem 1.1rem 1.2rem; }
.stats-total { font-size: 0.9rem; color: var(--muted); }
.stats-total b { color: var(--ink); font-weight: 800; }
@media (max-width: 520px) { .chart-wrap { height: 280px; } }

/* панель периода (пресеты + кнопка «Произвольный период») */
.period-bar { position: relative; display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; margin-bottom: 0.9rem; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 0.55rem 0.7rem; box-shadow: var(--shadow); }
.period-label { font-size: 0.82rem; color: var(--muted); font-weight: 700; padding: 0 0.3rem; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 0.45rem 0.85rem; font: inherit; font-weight: 600; font-size: 0.82rem; color: var(--muted); cursor: pointer; white-space: nowrap; }
.chip:hover { background: #f8fafc; color: var(--ink); }
.chip.active { background: #f59e0b; color: #fff; border-color: #f59e0b; }
.chip-custom { margin-left: auto; }

/* попап календаря */
.cal-popup { position: absolute; top: calc(100% + 8px); left: 0; z-index: 60; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 20px 60px rgba(15, 23, 42, 0.25); padding: 1rem; }
.cal-popup.hidden { display: none; }
.cal-months { display: flex; gap: 1.4rem; }
.cal-month { width: 230px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; font-weight: 700; margin-bottom: 0.5rem; }
.cal-nav { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; width: 28px; height: 28px; cursor: pointer; font-size: 1rem; line-height: 1; color: var(--ink); }
.cal-nav:hover { background: #f1f5f9; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal-wd { text-align: center; font-size: 0.7rem; color: var(--muted); font-weight: 700; padding: 0.3rem 0; }
.cal-wd.we { color: #ef4444; }
.cal-day { border: none; background: transparent; border-radius: 8px; padding: 0.45rem 0; font: inherit; font-size: 0.82rem; color: var(--ink); cursor: pointer; }
.cal-day.we { color: #ef4444; }
.cal-day:hover { background: #e2e8f0; }
.cal-day.in-range { background: #fef0d6; border-radius: 0; color: var(--ink); }
.cal-day.edge { background: #f59e0b; color: #fff; border-radius: 8px; }
.cal-footer { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 0.9rem; padding-top: 0.8rem; border-top: 1px solid var(--line); }
.cal-inputs { display: flex; align-items: center; gap: 0.5rem; }
.cal-inputs input { width: 118px; padding: 0.5rem 0.6rem; border: 1px solid var(--line); border-radius: 10px; font: inherit; }
.cal-actions { display: flex; gap: 0.5rem; }
@media (max-width: 720px) {
  .cal-popup { left: 0; right: 0; }
  .cal-months { flex-direction: column; gap: 0.8rem; }
  .cal-month { width: 100%; }
  .cal-footer { flex-direction: column; align-items: stretch; }
  .chip-custom { margin-left: 0; }
}

@media (max-width: 720px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    min-height: auto;
    border-right: none;
    border-bottom: 1px solid var(--line);
    padding: 0.5rem;
  }
  .side-nav { flex-direction: row; }
  .side-item { flex: 1; justify-content: center; }
  .content { padding: 0.9rem; }
}

/* ── Карточки сводки ────────────────────── */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
  gap: 0.75rem;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  box-shadow: var(--shadow);
}
.card .k { font-size: 0.76rem; color: var(--muted); font-weight: 600; }
.card .v { font-size: 1.35rem; font-weight: 800; letter-spacing: -0.02em; margin-top: 0.3rem; }
.card .sub { font-size: 0.72rem; color: var(--muted); margin-top: 0.2rem; }
.card.accent { background: linear-gradient(180deg, #1e293b, #0f172a); border-color: transparent; }
.card.accent .k, .card.accent .sub { color: #94a3b8; }
.card.accent .v { color: #fff; }
.v.pos { color: var(--green); }
.v.neg { color: var(--red); }
.card.avail { background: linear-gradient(180deg, #ecfdf5, #d1fae5); border-color: #a7f3d0; }
.card.avail .k { color: #15803d; }
.card.avail .v { color: #15803d; }
.card.avail .sub { color: #16a34a; }
.card.avail.neg { background: linear-gradient(180deg, #fef2f2, #fee2e2); border-color: #fecaca; }
.card.avail.neg .k, .card.avail.neg .v, .card.avail.neg .sub { color: #b91c1c; }

.status-line {
  margin-top: 0.85rem;
  padding: 0.8rem 1rem;
  border-radius: var(--radius);
  font-weight: 700;
  font-size: 0.95rem;
}
.status-line.owe { background: var(--red-bg); color: var(--red); }
.status-line.advance { background: var(--amber-bg); color: var(--amber); }
.status-line.clear { background: var(--green-bg); color: var(--green); }

/* ── Вкладки ────────────────────────────── */
.tabs { display: flex; gap: 0.4rem; margin: 1.4rem 0 0.8rem; flex-wrap: wrap; }
.tab {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 999px;
  padding: 0.5rem 0.95rem;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--muted);
}
.tab.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ── Панель и таблица ───────────────────── */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 0.9rem 1rem; border-bottom: 1px solid var(--line); }
.panel-head h2 { margin: 0; font-size: 1rem; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 0.7rem 1rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); background: #f8fafc; }
tbody tr:last-child td { border-bottom: none; }
td.amount, th.amount { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
td.comment { white-space: normal; color: var(--muted); max-width: 320px; }
td.actions { text-align: right; }
td.actions .btn { margin-left: 0.35rem; }
.empty { text-align: center; color: var(--muted); padding: 1.6rem; margin: 0; }

/* ── Модальное окно (обычный div) ───────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(15, 23, 42, 0.55);
}
.modal.hidden { display: none !important; }
.modal-card {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 520px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.35);
  max-height: 92dvh;
  overflow-y: auto;
}
.modal-card h3 { margin: 0; padding: 1.2rem 1.4rem 0; }
#txForm { padding: 1rem 1.4rem 1.4rem; display: flex; flex-direction: column; gap: 0.8rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.4rem; }
.field-date { display: flex; flex-direction: column; gap: 0.35rem; }
.field-label { font-size: 0.82rem; font-weight: 600; color: var(--muted); }
.date-field {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  width: 100%; padding: 0.6rem 0.7rem;
  border: 1px solid var(--line); border-radius: 10px;
  background: #fff; font: inherit; color: var(--ink); cursor: pointer;
}
.date-field:hover { border-color: var(--accent); }
.date-field-ico { opacity: 0.7; }
.form-cal { border: 1px solid var(--line); border-radius: 12px; padding: 0.55rem 0.7rem; margin-top: 0.1rem; background: var(--panel); box-shadow: var(--shadow); }
.form-cal .cal-month { width: 100%; }
.form-cal .cal-head { margin-bottom: 0.4rem; }
.form-cal .cal-day { padding: 0.5rem 0; font-size: 0.9rem; }

/* гость не видит админских элементов */
body.role-guest .admin-only { display: none !important; }

@media (max-width: 520px) {
  .card .v { font-size: 1.2rem; }
  th, td { padding: 0.6rem 0.7rem; }
}
