:root {
  --bg: #f6f2e9;
  --surface: #fffdf7;
  --ink: #2b2118;
  --ink-2: #5c4f41;
  --ink-3: #8d7f6e;
  --line: #e7ddcc;
  --accent: #b06028;
  --accent-dark: #8a4a1e;
  --accent-soft: #f7e6d3;
  --plan: #93a7bb;
  --plan-soft: #eaeff3;
  --ordered: #b07a1f;
  --ordered-soft: #f7ecd7;
  --danger: #b23c2e;
  --danger-soft: #f9e4e1;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(59, 42, 24, .05), 0 4px 16px rgba(59, 42, 24, .07);
  --font: 'Geologica', 'Segoe UI', system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; scrollbar-gutter: stable; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.55;
  min-height: 100dvh;
}
/* полупрозрачное фото участка фоном, поверх — молочная вуаль для читаемости */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background: url('/bg-house.jpg') center / cover no-repeat;
  opacity: .45;
}
body::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background: linear-gradient(180deg, rgba(246, 242, 233, .35), rgba(246, 242, 233, .68));
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

/* ---------- вход ---------- */
.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 40px 32px;
  width: 100%;
  max-width: 380px;
}
.login-card .logo { font-size: 40px; text-align: center; }
.login-card h1 { font-size: 22px; font-weight: 600; text-align: center; margin: 8px 0 4px; }
.login-card p.sub { color: var(--ink-3); text-align: center; font-size: 14px; margin-bottom: 24px; }

/* ---------- каркас ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 253, 247, .92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.tabs { display: flex; gap: 2px; margin-left: 8px; overflow-x: auto; }
.tab {
  border: none;
  background: none;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-2);
  white-space: nowrap;
  transition: background .15s, color .15s;
}
.tab:hover { background: var(--plan-soft); }
.tab.active { background: var(--accent-soft); color: var(--accent-dark); }
.topbar .spacer { flex: 1; }
.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--ink-2);
}
.user-chip .avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 13px;
}
.btn-logout {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--ink-2);
  transition: background .15s;
}
.btn-logout:hover { background: var(--plan-soft); }

.main { max-width: 1120px; margin: 0 auto; padding: 20px 16px 80px; }

/* ---------- карточки статистики ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.stat-card .label { font-size: 13px; color: var(--ink-3); margin-bottom: 6px; }
.stat-card .value { font-size: 24px; font-weight: 700; letter-spacing: -.02em; }
.stat-card .extra { font-size: 13px; color: var(--ink-2); margin-top: 4px; }
.stat-card .value.green { color: var(--accent-dark); }
.stat-card .value.red { color: var(--danger); }
.stat-card.clickable { cursor: pointer; transition: transform .12s, box-shadow .15s; }
.stat-card.clickable:hover { transform: translateY(-2px); box-shadow: 0 2px 4px rgba(59, 42, 24, .07), 0 8px 22px rgba(59, 42, 24, .12); }
.month-bar { cursor: pointer; }
.month-bar:hover rect { fill: var(--accent-dark); }
.bar-row.clickable { cursor: pointer; border-radius: 8px; padding: 2px 6px; margin: 0 -6px; transition: background .12s; }
.bar-row.clickable:hover { background: var(--accent-soft); }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}
.panel h2 { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.panel .hint { font-size: 13px; color: var(--ink-3); margin-top: 10px; }

/* ---------- графики ---------- */
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 170px) 1fr; gap: 10px; align-items: center; }
.bar-row .cat-name { font-size: 13px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { display: flex; flex-direction: column; gap: 3px; }
.bar-line { display: flex; align-items: center; gap: 8px; height: 14px; }
.bar-fill { height: 100%; border-radius: 4px; min-width: 2px; transition: width .3s ease-out; }
.bar-fill.plan { background: var(--plan); }
.bar-fill.fact { background: var(--accent); }
.bar-val { font-size: 11px; color: var(--ink-3); white-space: nowrap; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--ink-2); margin-bottom: 14px; }
.legend span::before {
  content: '';
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: -1px;
}
.legend .l-plan::before { background: var(--plan); }
.legend .l-fact::before { background: var(--accent); }

.chart-scroll { overflow-x: auto; }

/* ---------- топ перерасходов ---------- */
.top-list { display: flex; flex-direction: column; }
.top-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
}
.top-item:last-child { border-bottom: none; }
.top-item .t-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-item .t-cat { color: var(--ink-3); font-size: 12px; white-space: nowrap; }
.top-item .t-diff { font-weight: 600; color: var(--danger); white-space: nowrap; }
.top-item .t-diff.good { color: var(--accent-dark); }

/* ---------- смета ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
  align-items: center;
}
.search-box {
  flex: 1;
  min-width: 180px;
  position: relative;
}
.search-box input {
  width: 100%;
  padding: 10px 14px 10px 38px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 15px;
}
.search-box::before {
  content: '';
  position: absolute;
  left: 13px; top: 50%;
  width: 15px; height: 15px;
  transform: translateY(-50%);
  background: var(--ink-3);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.4'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.8-3.8'/%3E%3C/svg%3E") center/contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.4'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.8-3.8'/%3E%3C/svg%3E") center/contain no-repeat;
}
.filter-select {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 14px;
}
.btn {
  border: none;
  border-radius: 12px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  transition: background .15s, transform .05s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: var(--surface); border: 1px solid var(--line); color: var(--ink-2); }
.btn-ghost:hover { background: var(--plan-soft); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-danger:hover { background: #f3d3cf; }

.cat-block {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 14px;
  overflow: hidden;
}
.cat-head {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  background: none;
  border: none;
  text-align: left;
}
.cat-head:hover { background: #f8f3e8; }
.cat-head .chev {
  width: 20px; height: 20px;
  flex: none;
  background: var(--ink-3);
  transition: transform .2s;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") center/contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") center/contain no-repeat;
}
.cat-head.open .chev { transform: rotate(180deg); }
.cat-head .cname { font-weight: 600; font-size: 15px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-head .cmeta { font-size: 12px; color: var(--ink-3); white-space: nowrap; }
.cat-head .csums { display: flex; gap: 14px; font-size: 13px; white-space: nowrap; }
.cat-head .csums b { font-weight: 600; }
.cat-head .csums .s-plan { color: var(--ink-3); }
.cat-head .csums .s-fact { color: var(--accent-dark); }
.cat-head .csums .s-diff.over { color: var(--danger); }
.cat-head .csums .s-diff.under { color: var(--accent); }
.cat-progress {
  height: 4px;
  background: var(--plan-soft);
  border-radius: 2px;
  margin: 0 18px 12px;
  overflow: hidden;
}
.cat-progress i { display: block; height: 100%; background: var(--accent); border-radius: 2px; }

.items { border-top: 1px solid var(--line); }
.item-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background .12s;
}
.item-row:last-child { border-bottom: none; }
.item-row:hover { background: #f8f3e8; }
.item-name { font-size: 14.5px; font-weight: 500; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.item-sums { text-align: right; font-size: 13.5px; white-space: nowrap; }
.item-sums .fact { font-weight: 600; }
.item-sums .plan { color: var(--ink-3); font-size: 12px; }
.item-meta {
  grid-column: 1 / -1;
  font-size: 12px;
  color: var(--ink-3);
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.item-meta a { color: var(--accent); text-decoration: none; }
.item-meta a:hover { text-decoration: underline; }

.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 20px;
  white-space: nowrap;
}
.badge.plan { background: var(--plan-soft); color: #5b6d80; }
.badge.ordered { background: var(--ordered-soft); color: var(--ordered); }
.badge.bought { background: var(--accent-soft); color: var(--accent-dark); }

.empty-note { padding: 40px 20px; text-align: center; color: var(--ink-3); font-size: 14px; }

/* ---------- модальное окно ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(43, 33, 24, .45);
  z-index: 50;
  display: grid;
  place-items: end center;
  padding: 0;
}
@media (min-width: 640px) {
  .modal-backdrop { place-items: center; padding: 24px; }
}
.modal {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  overflow-y: auto;
  padding: 24px;
}
@media (min-width: 640px) { .modal { border-radius: 20px; } }
.modal h3 { font-size: 18px; font-weight: 600; margin-bottom: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12.5px; color: var(--ink-2); margin-bottom: 5px; font-weight: 500; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 15px;
  background: #fff;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea { resize: vertical; min-height: 60px; }
.form-section {
  grid-column: 1 / -1;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--ink-3);
  margin-top: 6px;
}
.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  flex-wrap: wrap;
}
.form-actions .btn { flex: 1; min-width: 110px; }
.form-error {
  grid-column: 1 / -1;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
}
.meta-line { font-size: 12px; color: var(--ink-3); grid-column: 1 / -1; }
.form-note {
  background: var(--ordered-soft);
  color: var(--ink-2);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 14px;
}
.form-grid.has-pack .unit-only { display: none; }
.form-grid:not(.has-pack) .pack-only { display: none; }

/* ---------- автодополнение названия ---------- */
.suggest-wrap { position: relative; }
.suggest-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 8px 28px rgba(59, 42, 24, .16);
  max-height: 280px;
  overflow-y: auto;
  z-index: 5;
}
.suggest-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.suggest-item:last-child { border-bottom: none; }
.suggest-item:hover, .suggest-item:focus-visible { background: var(--accent-soft); }
.suggest-item .s-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggest-item .s-meta { color: var(--ink-3); font-size: 12px; white-space: nowrap; flex: none; }
.suggest-new { justify-content: flex-start; color: var(--accent-dark); font-weight: 600; background: var(--accent-soft); }

/* ---------- пользователи ---------- */
.user-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.user-row:last-child { border-bottom: none; }
.user-row .u-login { font-weight: 600; font-size: 15px; }
.user-row .u-role { font-size: 12px; color: var(--ink-3); }
.user-row .u-actions { margin-left: auto; display: flex; gap: 8px; }
.btn-mini {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.btn-mini:hover { background: var(--plan-soft); }
.btn-mini.red { color: var(--danger); border-color: #ecc9c4; }
.btn-mini.red:hover { background: var(--danger-soft); }

/* ---------- тост ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: #fff;
  border-radius: 12px;
  padding: 12px 22px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 100;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--danger); }

.loading { padding: 60px 20px; text-align: center; color: var(--ink-3); }

@media (max-width: 640px) {
  .cat-head { flex-wrap: wrap; }
  .cat-head .csums { width: 100%; padding-left: 32px; flex-wrap: wrap; row-gap: 2px; column-gap: 10px; font-size: 12px; white-space: normal; }
  .cat-head .csums span { white-space: nowrap; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .stat-card .value { font-size: 19px; }
  .form-grid { grid-template-columns: 1fr; }
  .topbar-inner { flex-wrap: wrap; }
  .tabs { order: 3; width: 100%; margin-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
