/* Только тёмная тема: системную настройку не учитываем намеренно. */
:root {
  color-scheme: dark;
  --bg: #1b1d21;
  --surface: #23262b;
  --surface-2: #2a2e34;
  --surface-3: #33383f;
  --border: #383d45;
  --border-strong: #4d545e;
  --text: #e7e9ec;
  --muted: #a8afb8;
  --faint: #8f97a1;
  --accent: #8ab4ff;
  --accent-ink: #0f1520;
  --accent-bg: #22314a;
  --danger: #ff8f87;
  --danger-bg: #3d2427;
  --warn: #f2c66d;
  --warn-bg: #3a3122;
  --ok: #7fd49b;
  --focus: #8ab4ff;
  --radius: 6px;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "JetBrains Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 var(--sans);
}

button, input, select { font: inherit; color: inherit; }
b { font-weight: 600; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.mono { font-family: var(--mono); font-size: 13px; letter-spacing: 0; }
.muted { color: var(--muted); }
.warn-text { color: var(--warn); display: inline-flex; align-items: center; gap: 4px; }
.error-text { color: var(--danger); }
.mobile-only { display: none; }

/* ---------- кнопки ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface-2); color: var(--text);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-3); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { background: #a3c4ff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.sm { min-height: 28px; padding: 0 10px; font-size: 13px; }
.btn.wide { width: 100%; min-height: 40px; }

.link-btn {
  background: none; border: 0; padding: 0; cursor: pointer;
  color: var(--accent); text-decoration: underline; text-underline-offset: 2px;
}

.icon-btn {
  flex: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  width: 28px; height: 28px; padding: 0;
  border: 1px solid transparent; border-radius: var(--radius);
  background: transparent; color: var(--faint); cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-3); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn.copied { color: var(--ok); }
.icon-btn.danger:hover:not(:disabled) { color: var(--danger); }
.icon-btn.armed {
  width: auto; padding: 0 8px; font-size: 12px; font-weight: 600;
  color: var(--danger); background: var(--danger-bg); border-color: var(--danger);
}
.btn-label { display: none; }

/* ---------- шапка и панель ---------- */

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.topbar h1 { margin: 0; font-size: 16px; font-weight: 600; }
.topbar-actions { display: flex; gap: 4px; flex-wrap: wrap; }

.tabs { display: flex; gap: 4px; }
.tabs a {
  display: inline-flex; align-items: center; min-height: 34px; padding: 0 12px;
  border-radius: var(--radius); color: var(--muted); text-decoration: none; font-weight: 600;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a[aria-current="page"] { background: var(--accent-bg); color: var(--accent); }

.provider-select { width: 100%; max-width: 200px; min-height: 32px; }
.provider-select.unset { color: var(--faint); }
.c-account { min-width: 280px; }
.c-provider { min-width: 200px; }

/* ---------- сметы ---------- */

.budget-main { max-width: 860px; margin: 0 auto; }
.state-msg { margin: 0; padding: 28px 0; text-align: center; }
.state-msg .btn { margin-left: 8px; }
.months { display: grid; gap: 16px; }
.month-card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; }
.month-card.current { border-color: #3b5580; }
.month-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.month-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { padding: 8px 10px; background: var(--surface-2); border-radius: var(--radius); min-width: 0; }
.stat-label { display: block; color: var(--muted); font-size: 12px; margin-bottom: 2px; }
.stat-value { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat .val { padding: 0 4px; margin: 0 -4px; display: inline-block; }
.stat .cell-input { font-size: 16px; }
.auto-hint { color: var(--faint); font-size: 11px; font-weight: 400; margin-left: 4px; }
.neg { color: var(--danger); }
.pos { color: var(--ok); }

.quick-add { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.quick-add input {
  min-height: 34px; padding: 0 10px; background: var(--bg);
  border: 1px solid var(--border-strong); border-radius: var(--radius);
}
.quick-add input:focus { border-color: var(--accent); outline: none; }
.quick-add .qa-name { flex: 1 1 180px; min-width: 0; }
.quick-add .qa-amount { flex: 0 1 110px; width: 110px; font-variant-numeric: tabular-nums; }
.quick-add .qa-date { flex: 0 0 auto; color-scheme: dark; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip {
  min-height: 28px; padding: 0 10px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 14px; color: var(--text);
}
.chip:hover { background: var(--surface-3); }
.chip small { color: var(--muted); margin-left: 4px; }

.breakdown { margin-bottom: 8px; color: var(--muted); }
.breakdown summary { cursor: pointer; padding: 4px 0; }
.breakdown-list { display: grid; grid-template-columns: 1fr auto auto; gap: 2px 14px; padding: 4px 0 8px; font-variant-numeric: tabular-nums; }
.breakdown-list span:nth-child(3n+2), .breakdown-list span:nth-child(3n) { text-align: right; }
.breakdown-list span:nth-child(3n+1) { color: var(--text); }

.pay-row {
  display: grid; grid-template-columns: 130px 1fr 110px 36px; align-items: center; gap: 6px;
  padding: 2px 0; border-top: 1px solid var(--border);
}
.pay-row .pay-date {
  min-height: 30px; padding: 0 4px; background: transparent; border: 1px solid transparent;
  border-radius: 4px; color: var(--muted); color-scheme: dark; font: inherit;
}
.pay-row .pay-date:hover { background: var(--surface-3); }
.pay-row .amount { text-align: right; font-variant-numeric: tabular-nums; }
.pay-row .amount.val { display: block; }
.pay-empty { padding: 10px 0 2px; color: var(--faint); }

.import-panel { margin-bottom: 16px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.import-panel[hidden] { display: none; }
.import-panel label { display: block; margin-bottom: 8px; color: var(--muted); }
.import-panel textarea {
  width: 100%; padding: 8px 10px; resize: vertical; font: 13px/1.4 var(--mono);
  background: var(--bg); border: 1px solid var(--border-strong); border-radius: var(--radius);
}
.import-panel textarea:focus { border-color: var(--accent); outline: none; }
.import-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 8px; }

main { padding: 14px 20px 80px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }

.search { position: relative; flex: 1 1 280px; max-width: 460px; }
.search input {
  width: 100%; min-height: 34px; padding: 0 34px 0 10px;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius);
}
.search input::placeholder { color: var(--faint); }
.search kbd {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  padding: 0 6px; border: 1px solid var(--border-strong); border-radius: 4px;
  font: 12px var(--mono); color: var(--muted); pointer-events: none;
}
.search input:focus + kbd { display: none; }

select {
  min-height: 34px; padding: 0 8px; max-width: 220px;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius);
}
select.active { border-color: var(--accent); }

#add-btn { margin-left: auto; }

.summary {
  display: flex; flex-wrap: wrap; gap: 6px 18px; align-items: center;
  min-height: 22px; margin-bottom: 8px; color: var(--muted);
}
.summary b { color: var(--text); }

/* ---------- таблица ---------- */

.table-wrap {
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  overflow-x: auto;
}
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; vertical-align: middle; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: 0; }

th { padding: 0; background: var(--surface-2); font-weight: 600; font-size: 13px; color: var(--muted); white-space: nowrap; }
th button {
  width: 100%; padding: 8px 10px; text-align: left; background: none; border: 0; cursor: pointer;
  color: inherit; font-weight: inherit;
}
th button:hover { color: var(--text); }
th[aria-sort="ascending"], th[aria-sort="descending"] { color: var(--text); }
th .th-label { display: block; padding: 8px 10px; }
.arrow { color: var(--accent); }

td { padding: 3px 6px; }
tbody tr:hover td { background: #262a30; }

.c-region { min-width: 130px; }
.c-ip { min-width: 150px; }
.c-domain { min-width: 220px; }
.c-price { min-width: 100px; }
.c-hosting { min-width: 120px; }
.c-password { min-width: 170px; }
.c-actions { width: 1%; white-space: nowrap; }

.cell { display: flex; align-items: center; gap: 2px; min-height: 30px; }

.val {
  flex: 0 1 auto; min-width: 0; padding: 4px 6px; border-radius: 4px;
  cursor: text; overflow-wrap: anywhere;
  border: 1px solid transparent;
}
.val:hover { background: var(--surface-3); }
.val.empty { color: var(--faint); }
.val.saving { opacity: .6; }
.val.failed { border-color: var(--danger); background: var(--danger-bg); }
.val.dup { color: var(--warn); background: var(--warn-bg); }
.val.revealed { color: var(--accent); }
.dup-icon { display: inline-flex; vertical-align: -2px; margin-right: 4px; }
.dup-icon svg { width: 14px; height: 14px; }

.c-price .val { font-variant-numeric: tabular-nums; }

.cell-input, .draft-input {
  flex: 1 1 auto; width: 100%; min-width: 0; min-height: 30px; padding: 4px 6px;
  background: var(--bg); border: 1px solid var(--accent); border-radius: 4px; outline: none;
}
.draft-input { border-color: var(--border-strong); }
.draft-input:focus { border-color: var(--accent); }
.draft-input::placeholder { color: var(--faint); }

.meta { padding: 0 7px 2px; font-size: 11px; color: var(--faint); }
.badge {
  display: inline-block; margin-left: 4px; padding: 0 5px; border-radius: 3px;
  background: var(--accent-bg); color: var(--accent); font-weight: 600; font-size: 11px;
}

.actions { display: flex; gap: 2px; justify-content: flex-end; align-items: center; }

tr.draft td { background: var(--accent-bg); padding: 6px; }
.draft-error { color: var(--danger); font-size: 12px; margin-top: 4px; white-space: normal; }

.state-row td { padding: 28px 16px; text-align: center; color: var(--muted); }
.state-row .btn { margin-left: 8px; }

.spinner {
  display: inline-block; width: 12px; height: 12px; margin-right: 6px; vertical-align: -1px;
  border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

/* ---------- уведомления ---------- */

.toasts {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 10;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  width: max-content; max-width: calc(100vw - 32px);
}
.toast {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; border-radius: var(--radius);
  background: #3a4048; color: var(--text); border: 1px solid var(--border-strong);
  box-shadow: 0 4px 16px rgb(0 0 0 / 40%);
}
.toast.error { background: var(--danger-bg); border-color: var(--danger); }
.toast.warn { background: var(--warn-bg); border-color: var(--warn); }
.toast-action {
  background: none; border: 0; padding: 4px 2px; cursor: pointer;
  color: var(--accent); font-weight: 600;
}
.toast-close { background: none; border: 0; cursor: pointer; color: var(--muted); font-size: 18px; line-height: 1; padding: 0 2px; }

.clipboard-proxy { position: fixed; top: 0; left: 0; opacity: 0; pointer-events: none; }

/* ---------- вход ---------- */

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; padding: 24px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.login-card h1 { margin: 0 0 18px; font-size: 18px; font-weight: 600; }
.field { display: block; margin-bottom: 12px; }
.field span { display: block; margin-bottom: 4px; color: var(--muted); font-size: 13px; }
.field input {
  width: 100%; min-height: 40px; padding: 0 10px;
  background: var(--bg); border: 1px solid var(--border-strong); border-radius: var(--radius);
}
.field input:focus { border-color: var(--accent); outline: none; }
.form-error { min-height: 20px; margin: 0 0 10px; color: var(--danger); font-size: 13px; }

/* ---------- телефон: строки превращаются в карточки ---------- */

@media (max-width: 760px) {
  .topbar { padding: 4px 4px 4px 16px; gap: 0; flex-wrap: wrap; }
  .topbar h1 { font-size: 14px; white-space: nowrap; }
  .tabs a { min-height: 44px; padding: 0 8px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stat:last-child { grid-column: 1 / -1; }
  .quick-add input { min-height: 44px; font-size: 16px; }
  .quick-add .qa-name { flex: 1 1 100%; }
  .quick-add .qa-amount { flex: 1 1 40%; width: auto; }
  .quick-add .qa-date { flex: 1 1 40%; }
  .quick-add .btn { flex: 1 1 100%; }
  .chip { min-height: 40px; }
  .pay-row { grid-template-columns: 1fr auto 44px; grid-template-areas: "name amount del" "date date date"; padding: 6px 0; }
  .pay-row .pay-date { grid-area: date; min-height: 36px; justify-self: start; }
  .pay-row .name { grid-area: name; }
  .pay-row .amount { grid-area: amount; }
  .pay-row .actions { grid-area: del; }
  .breakdown summary { min-height: 40px; display: flex; align-items: center; }
  .provider-select { max-width: none; min-height: 44px; }
  .topbar-actions { flex-wrap: nowrap; gap: 0; margin-left: auto; }
  .topbar .btn { padding: 0 6px; }
  .desktop-only { display: none; }
  main { padding: 12px 16px 96px; }

  .toolbar { gap: 8px; }
  .search { flex: 1 1 100%; max-width: none; }
  .search input, select, .btn { min-height: 44px; }
  .search kbd { display: none; }
  select { flex: 1 1 40%; max-width: none; }
  .mobile-only { display: block; }
  #add-btn { margin-left: 0; flex: 1 1 40%; }

  .table-wrap { border: 0; background: none; overflow: visible; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }

  tbody tr {
    margin-bottom: 10px; padding: 6px 4px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  }
  tbody tr:hover td { background: none; }
  tbody tr.is-new { border-color: #3b5580; }
  td { border: 0; padding: 0 8px; display: grid; grid-template-columns: 76px 1fr; align-items: center; min-width: 0; }
  td::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  td > .meta { grid-column: 2; padding-bottom: 4px; }

  .c-region .val { font-weight: 600; }
  .cell { min-height: 44px; gap: 4px; }
  .val { flex: 1 1 auto; padding: 8px 6px; min-height: 40px; display: flex; align-items: center; }
  .icon-btn { width: 44px; height: 44px; }
  .icon-btn svg { width: 18px; height: 18px; }
  .cell-input, .draft-input { min-height: 40px; font-size: 16px; }

  td.c-actions { display: block; width: 100%; padding: 4px 8px 2px; }
  td.c-actions::before { content: none; }
  .actions { justify-content: stretch; gap: 8px; }
  .actions .icon-btn { flex: 1; width: auto; border-color: var(--border-strong); color: var(--muted); }
  .actions .btn { flex: 1; min-height: 44px; }
  .btn-label { display: inline; }

  .state-row { padding: 0; }
  .state-row td { display: block; padding: 24px 16px; }
  .state-row td::before { content: none; }
  tr.state-row { background: var(--surface); }
  tr.draft td { padding: 4px 8px; }
}
