:root {
  --bg: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --page: var(--tg-theme-secondary-bg-color, #f1f2f6);
  --text: var(--tg-theme-text-color, #16181d);
  --hint: var(--tg-theme-hint-color, #667085);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #c4320a);
  --line: color-mix(in srgb, var(--text) 12%, transparent);
  --safe-bottom: max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));
  --nav-height: 0px;
  color-scheme: light dark;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

button[aria-busy='true'] {
  opacity: 0.6;
}

body.loading::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  width: 40%;
  height: 2px;
  background: var(--accent);
  animation: progress 1s ease-in-out infinite;
}

@keyframes progress {
  from { transform: translateX(-100%); }
  to { transform: translateX(250%); }
}

.skeleton {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.skeleton-card {
  height: 88px;
  border-radius: 14px;
  background: var(--bg);
  animation: pulse 1.2s ease-in-out infinite;
}

.skeleton-card.tall {
  height: 168px;
}

@keyframes pulse {
  50% { opacity: 0.5; }
}

@media (prefers-reduced-motion: reduce) {
  body.loading::before, .skeleton-card {
    animation: none;
  }
}

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 76px);
  z-index: 10;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--page);
  text-align: center;
}

.fold summary {
  min-height: 24px;
  font-weight: 600;
  cursor: pointer;
}

.fold-body {
  margin-top: 12px;
}

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body.has-tabs {
  --nav-height: 56px;
}

h1, h2, h3, p, ul {
  margin: 0;
  padding: 0;
}

ul {
  list-style: none;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

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

#view {
  max-width: 560px;
  margin: 0 auto;
  padding: 8px 16px calc(88px + var(--nav-height) + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hint {
  color: var(--hint);
  font-size: 14px;
}

.overdue {
  color: var(--danger);
}

.amount {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.card {
  background: var(--bg);
  border-radius: 14px;
  padding: 16px;
}

.card h2 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}

.card h3 {
  color: var(--hint);
  font-size: 14px;
  font-weight: 500;
  margin-top: 12px;
}

.card-button {
  display: block;
  width: 100%;
  text-align: left;
}

.card-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.form-note {
  margin-bottom: 12px;
}

.month {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.month h1 {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  overflow-wrap: anywhere;
}

.month button {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  font-size: 24px;
  line-height: 1;
}

.month button:disabled {
  opacity: 0.3;
  cursor: default;
}

.segments {
  display: flex;
  gap: 2px;
  padding: 2px;
  border-radius: 10px;
  background: var(--line);
  overflow-x: auto;
}

.segments button {
  flex: 1;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 8px;
  font-size: 14px;
  white-space: nowrap;
}

.segments button[aria-pressed='true'] {
  background: var(--bg);
  font-weight: 600;
}

.total {
  font-size: 34px;
  font-weight: 700;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.stats {
  margin-top: 12px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.card > .stats:first-child {
  margin-top: 0;
  border-top: 0;
  padding-top: 0;
}

.stats li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
}

.stats li .amount {
  white-space: normal;
  text-align: right;
}

.people li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
}

.people-values {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.category {
  display: block;
  width: calc(100% + 16px);
  min-height: 44px;
  padding: 8px;
  margin: 0 -8px;
  border-radius: 10px;
  text-align: left;
}

.category[aria-pressed='true'] {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.category-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.category-name {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.share {
  color: var(--hint);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.bar {
  display: block;
  height: 4px;
  margin-top: 6px;
  border-radius: 0 4px 4px 0;
  background: var(--accent);
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 52px;
  padding: 6px 0;
  text-align: left;
}

.plain-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 6px 0;
}

.rows li + li {
  border-top: 1px solid var(--line);
}

/* Строки за пределами экрана не отрисовываются, поэтому длинные месяцы прокручиваются плавно */
.rows li {
  content-visibility: auto;
  contain-intrinsic-size: auto 56px;
}

.row-emoji {
  flex: none;
  font-size: 20px;
  width: 28px;
  text-align: center;
}

.row-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* Выписки дают очень длинные описания: в списке хватит двух строк, полный текст есть в форме */
.row-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.row-with-action {
  display: flex;
  align-items: center;
  gap: 8px;
}

.row-with-action .row {
  flex: 1;
  min-width: 0;
}

.row-action {
  flex: none;
  min-height: 36px;
  padding: 0 10px;
  border-radius: 10px;
  background: var(--line);
  font-size: 14px;
}

.row-action.accent {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
  font-weight: 600;
}

.link-button {
  min-height: 44px;
  padding: 0;
  color: var(--accent);
  font-size: 14px;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.table th,
.table td {
  padding: 6px 0 6px 6px;
  text-align: right;
  border-top: 1px solid var(--line);
  overflow-wrap: anywhere;
}

.table th:first-child {
  padding-left: 0;
  text-align: left;
  font-weight: 400;
}

.table thead th {
  border-top: 0;
  color: var(--hint);
  font-weight: 500;
}

.calc-result:empty {
  display: none;
}

.empty {
  text-align: center;
  padding: 28px 16px;
}

.add-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--nav-height) + var(--safe-bottom));
  padding: 12px 16px;
  background: linear-gradient(transparent, var(--page) 35%);
  display: flex;
  justify-content: center;
  gap: 8px;
}

.add-bar .primary {
  max-width: 528px;
}

.primary {
  display: block;
  width: 100%;
  min-height: 48px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.primary.secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
}

.editor {
  width: min(100% - 24px, 480px);
  max-height: calc(100dvh - 24px);
  border: 0;
  border-radius: 16px;
  background: var(--bg);
  color: var(--text);
  padding: 20px 16px;
}

.editor::backdrop {
  background: rgb(0 0 0 / 45%);
}

.editor h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 12px;
  overflow-wrap: anywhere;
}

.field {
  margin-bottom: 12px;
}

.field label {
  display: block;
  color: var(--hint);
  font-size: 14px;
  margin-bottom: 4px;
}

.field input, .field select {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--page);
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  margin-bottom: 12px;
}

.check input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.editor-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
}

.editor-actions button {
  min-height: 44px;
  padding: 0 8px;
}

.danger {
  color: var(--danger);
}

.error {
  color: var(--danger);
  font-size: 14px;
  margin-bottom: 12px;
}

.error:empty {
  display: none;
}

.message {
  padding: 48px 16px;
  text-align: center;
}

.message h1 {
  font-size: 20px;
  margin-bottom: 8px;
}

.message .primary {
  margin-top: 20px;
}

#tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}

#tabs[hidden] {
  display: none;
}

#tabs button {
  flex: 1;
  min-width: 0;
  height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--hint);
  font-size: 11px;
}

.tab-icon {
  font-size: 18px;
  line-height: 1.2;
}

#tabs button[aria-current='page'] {
  color: var(--accent);
  font-weight: 600;
}
