:root {
  --bg: var(--tg-theme-bg-color, #FFFFFF);
  --sec: var(--tg-theme-secondary-bg-color, #F1F1F1);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #8E8E93);
  --accent: var(--tg-theme-button-color, #007AFF);
  --accent-text: var(--tg-theme-button-text-color, #FFFFFF);
  --destr: var(--tg-theme-destructive-text-color, #FF3B30);
  --sep: var(--tg-theme-section-separator-color, rgba(0, 0, 0, 0.1));
  --soft: color-mix(in srgb, var(--accent) 14%, transparent);
  --scrim: rgba(0, 0, 0, 0.4);
  --tabbar-h: 66px;
  /* Fullscreen Telegram draws Close / ⋯ over the page: device inset + Telegram's own controls. */
  --top-inset: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --bottom-inset: var(--tg-safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}
button { font: inherit; color: inherit; background: none; border: none; padding: 0; cursor: pointer; }
select, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.app {
  min-height: 100%;
  padding-top: calc(var(--top-inset) + 10px);
  padding-bottom: calc(var(--tabbar-h) + var(--bottom-inset) + 8px);
}

/* Segmented controls */
.seg {
  display: flex;
  gap: 2px;
  padding: 2px;
  border-radius: 12px;
  background: var(--sec);
}
.seg button {
  flex: 1 1 0;
  height: 40px;
  min-width: 44px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
}
.seg button.on { background: var(--soft); color: var(--accent); font-weight: 600; }
.seg.seg-sm { border-radius: 10px; }
.seg.seg-sm button { height: 32px; border-radius: 8px; font-size: 13px; color: var(--hint); }
.seg.seg-sm button.on {
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}
.periods { padding: 8px 16px 0; }

.screen { padding: 0 0 24px; }
.screen-title { margin: 0; padding: 20px 16px 8px; font-size: 28px; font-weight: 700; }
.section-label {
  margin: 0;
  padding: 0 16px 8px;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--hint);
}
.block { margin-top: 28px; }
.card { margin: 0 16px; border-radius: 16px; background: var(--sec); overflow: hidden; }
.hint { margin: 0; padding: 8px 32px 0; font-size: 13px; line-height: 1.4; color: var(--hint); }
.empty-copy { margin: 0; font-size: 16px; line-height: 1.4; text-align: center; color: var(--hint); max-width: 260px; }

/* 01 Period */
.hero { padding: 28px 16px 0; }
.hero-label { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 8px; font-size: 15px; }
.hero-label .k { color: var(--hint); }
.hero-label .v { font-weight: 600; }
.hero-total { margin-top: 2px; font-size: 56px; line-height: 1.1; font-weight: 700; letter-spacing: -0.02em; }
.hero-note { margin-top: 6px; font-size: 15px; line-height: 1.35; color: var(--hint); }

.cats { padding: 24px 0 4px; display: flex; flex-direction: column; gap: 20px; }
.cats.empty { padding: 24px 16px 28px; align-items: center; }
.ring-wrap { position: relative; width: 256px; height: 256px; align-self: center; }
.ring-wrap.empty-ring { width: 184px; height: 184px; }
.ring-svg { position: absolute; left: 36px; top: 36px; display: block; overflow: visible; }
.empty-ring .ring-svg { position: static; }
.ring-track { fill: none; stroke: var(--accent); stroke-width: 24px; opacity: 0.08; }
.ring-seg {
  fill: none;
  stroke: var(--accent);
  stroke-width: 24px;
  transform-origin: 92px 92px;
  cursor: pointer;
  transition: stroke-opacity 0.2s ease-out, transform 0.2s ease-out;
}
.ring-empty { fill: none; stroke: var(--hint); stroke-width: 1.5px; stroke-dasharray: 4 5; opacity: 0.35; }
.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 2px;
  padding: 0 36px;
  text-align: center;
  pointer-events: none;
}
.ring-center .l1,
.ring-center .l2 {
  max-width: 116px;
  min-width: 0;
  overflow-wrap: break-word;
}
.ring-center .l1 { font-size: 17px; font-weight: 600; line-height: 1.2; }
.ring-center .l1.small {
  font-size: 13px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}
.ring-center .l2 { font-size: 13px; line-height: 1.2; color: var(--hint); }
.ring-label {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  pointer-events: none;
}

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  margin-left: 16px;
  padding-right: 16px;
}
.row + .row { border-top: 0.5px solid var(--sep); }
.cat-row { cursor: pointer; transition: background 0.2s ease-out; }
.cat-row.on { background: var(--soft); }
.row-name { font-size: 16px; line-height: 1.25; min-width: 0; flex: 1 1 auto; overflow-wrap: anywhere; }
.row-right { margin-left: auto; display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-shrink: 0; flex-wrap: wrap; }
.row-pct { font-size: 15px; color: var(--hint); min-width: 36px; text-align: right; }
.row-sum { font-size: 16px; font-weight: 500; min-width: 58px; text-align: right; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; background: var(--accent); }

/* 06b Transactions */
.list-head { padding: 24px 16px 8px; display: flex; flex-direction: column; gap: 4px; }
.list-head .meta { font-size: 15px; line-height: 1.3; color: var(--hint); }
.list-total { margin-top: 8px; font-size: 34px; font-weight: 700; line-height: 1.1; letter-spacing: -0.02em; }
.list-empty { margin: 0; padding: 32px 16px; font-size: 16px; line-height: 1.4; color: var(--hint); }
.day { margin-top: 24px; }
.day-head { display: flex; align-items: center; gap: 8px; height: 32px; padding: 0 16px; font-size: 13px; }
.day-head .d { font-weight: 600; }
.day-head .w, .day-head .s { color: var(--hint); }
.day-head .s { margin-left: auto; }
.txn {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 64px;
  padding: 0 16px;
  text-align: left;
}
.txn:active { background: var(--sec); }
.txn-icon {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 12px;
  background: var(--soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}
.txn-body { display: flex; align-items: center; gap: 12px; flex: 1 1 auto; min-width: 0; align-self: stretch; padding: 12px 0; }
.txn + .txn .txn-body { border-top: 0.5px solid var(--sep); }
.txn-left { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1 1 auto; }
.txn-cat { font-size: 16px; font-weight: 600; line-height: 1.25; }
.txn-sub { display: flex; gap: 6px; min-width: 0; font-size: 14px; line-height: 1.3; }
.txn-payer { flex-shrink: 0; opacity: 0.72; }
.txn-raw { color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.txn-right { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; flex-shrink: 0; }
.txn-usd { font-size: 17px; font-weight: 600; line-height: 1.25; }
.txn-orig { font-size: 13px; color: var(--hint); }

/* 08 Household */
.section-head { display: flex; align-items: center; justify-content: space-between; padding-right: 6px; }
.section-head .section-label { padding-bottom: 0; }
.link-btn { min-height: 40px; padding: 0 10px; font-size: 15px; color: var(--accent); }
.hint b { font-weight: 600; color: var(--text); white-space: nowrap; }
.ccy-row { display: flex; align-items: center; gap: 12px; width: 100%; min-height: 50px; padding: 0 14px 0 16px; text-align: left; }
.ccy-row + .ccy-row, .ccy-search, .ccy-found > * { border-top: 0.5px solid var(--sep); }
.ccy-code { width: 44px; flex: none; font-weight: 600; }
.ccy-name { flex: 1 1 auto; min-width: 0; font-size: 15px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.radio, .slot { width: 22px; height: 22px; flex: none; }
.radio { border-radius: 11px; border: 1.5px solid var(--hint); display: flex; align-items: center; justify-content: center; }
.radio.on { border-color: var(--accent); }
.radio.on::after { content: ""; width: 12px; height: 12px; border-radius: 6px; background: var(--accent); }
/* A 22px dot in a 44px target: the negative margin keeps the column where the dot is. */
.minus { position: relative; width: 44px; height: 44px; margin: -11px; flex: none; display: flex; align-items: center; justify-content: center; }
.minus::before { content: ""; width: 22px; height: 22px; border-radius: 11px; background: var(--destr); }
.minus::after { content: ""; position: absolute; width: 10px; height: 2px; border-radius: 1px; background: #FFFFFF; }
.tag { flex: none; padding: 2px 7px; border: 1px solid var(--sep); border-radius: 8px; font-size: 12px; color: var(--hint); }
.ccy-add { flex: none; font-size: 15px; color: var(--accent); }
.ccy-search { display: flex; align-items: center; gap: 6px; padding-left: 16px; color: var(--hint); }
.ccy-search svg { flex: none; }
.ccy-search input { flex: 1 1 auto; min-width: 0; padding: 14px 16px 14px 0; border: none; background: transparent; font-size: 16px; outline: none; }
.ccy-search input::placeholder { color: var(--hint); }
.ccy-none { margin: 0; padding: 12px 16px; font-size: 13px; line-height: 1.4; color: var(--hint); }
.people { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; margin: 0 16px; }
.tile { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; min-width: 0; padding: 14px; border-radius: 16px; background: var(--sec); text-align: left; }
.ava { width: 44px; height: 44px; border-radius: 22px; display: flex; align-items: center; justify-content: center; background: var(--soft); color: var(--accent); font-size: 18px; font-weight: 600; }
.tile-text { display: flex; flex-direction: column; gap: 2px; width: 100%; min-width: 0; }
.tile-name, .tile-sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-name { font-size: 17px; font-weight: 600; }
.tile-sub { font-size: 13px; color: var(--hint); }

/* Toast with an undo, above the tab bar */
.toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(var(--tabbar-h) + var(--bottom-inset) + 8px);
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  line-height: 1.35;
}
.toast span { flex: 1 1 auto; min-width: 0; }
.toast button { flex: none; min-height: 32px; font-weight: 600; color: color-mix(in srgb, var(--accent) 55%, var(--bg)); }

/* Sheets */
.scrim { position: fixed; inset: 0; background: var(--scrim); z-index: 20; touch-action: none; }
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: calc(100% - var(--top-inset) - 8px);
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  z-index: 21;
  padding: 0 0 calc(12px + var(--bottom-inset));
}
html.sheet-open, html.sheet-open body { overflow: hidden; }
.sheet-handle { width: 36px; height: 5px; border-radius: 3px; background: var(--hint); opacity: 0.45; margin: 8px auto 0; }
.sheet-bar { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px 8px 16px; }
.sheet-bar h2 { margin: 0; font-size: 20px; font-weight: 700; }
.icon-btn { width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; color: var(--hint); }
.icon-btn svg { stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; }
.sheet-main {
  display: block;
  margin: 16px;
  min-height: 48px;
  width: calc(100% - 32px);
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 17px;
  font-weight: 600;
}
.sheet-main:disabled { opacity: 0.4; cursor: default; }
.err-inline { margin: 0; padding: 8px 0 0; font-size: 14px; line-height: 1.4; color: var(--destr); text-align: center; }

/* 05 Calendar */
#sheet-cal .sheet-handle { margin: 12px auto 16px; }
.presets { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 16px 12px; }
.preset { display: inline-flex; align-items: center; height: 32px; padding: 0 14px; border-radius: 16px; font-size: 15px; background: var(--sec); }
.date-pair { display: flex; gap: 8px; padding: 0 16px; }
.date-box {
  flex: 1;
  border: 1.5px solid transparent;
  background: var(--sec);
  border-radius: 12px;
  padding: 10px 12px;
  min-height: 48px;
  text-align: left;
  font-size: 16px;
  font-weight: 500;
}
.date-box.on { border-color: var(--accent); }
.cal-nav { display: flex; align-items: center; justify-content: space-between; padding: 16px 8px 8px; }
.cal-nav h3 { margin: 0; font-size: 17px; font-weight: 600; }
.cal-nav .icon-btn { color: var(--accent); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); padding: 0 8px 16px; }
.cal-dow { font-size: 13px; color: var(--hint); text-align: center; padding: 8px 0; }
.cal-day {
  position: relative;
  width: 100%;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  border-radius: 10px;
}
.cal-day.in { background: var(--soft); border-radius: 0; }
.cal-day.end { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.cal-day.end.from-end { border-radius: 10px 0 0 10px; }
.cal-day.end.to-end { border-radius: 0 10px 10px 0; }
.cal-day.today { color: var(--accent); font-weight: 600; }
.cal-day.end.today { color: var(--accent-text); }
.cal-day.today::after {
  content: "";
  position: absolute;
  bottom: 6px;
  left: 50%;
  margin-left: -1.5px;
  width: 3px;
  height: 3px;
  border-radius: 2px;
  background: currentColor;
}
.cal-day:disabled { opacity: 0.35; text-decoration: line-through; cursor: default; }

/* 07 Edit */
.sheet-edit {
  max-height: min(94%, calc(100% - var(--top-inset) - 8px));
  border-radius: 20px 20px 0 0;
  padding: 0 20px calc(34px + var(--bottom-inset));
  transition: transform 0.25s ease;
}
.sheet-edit.dragging { transition: none; }
.drag-zone { touch-action: none; margin: 0 -20px; padding: 8px 20px 0; cursor: grab; }
.drag-zone .sheet-handle { margin: 0 auto; }
.raw-line { display: flex; align-items: center; gap: 8px; min-height: 44px; margin-top: 12px; }
.raw-text { flex: 1; min-width: 0; font-size: 20px; font-weight: 600; line-height: 1.3; overflow-wrap: anywhere; }
.raw-input {
  flex: 1;
  min-width: 0;
  height: 44px;
  border: none;
  border-bottom: 1.5px solid var(--accent);
  background: transparent;
  font-size: 20px;
  font-weight: 600;
  outline: none;
  padding: 0;
}
.raw-line .icon-btn.edit { margin-right: -10px; }
.raw-line .icon-btn.done { color: var(--accent); }
.amount-wrap { padding: 28px 0 20px; }
.amount-input {
  display: block;
  width: 100%;
  border: none;
  background: transparent;
  font-size: 52px;
  line-height: 1.1;
  font-weight: 700;
  letter-spacing: -0.02em;
  text-align: center;
  outline: none;
  padding: 0;
  font-variant-numeric: tabular-nums;
}
.ccy-chip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  width: max-content;
  height: 44px;
  margin: 16px auto 0;
  padding: 0 16px 0 18px;
  border-radius: 22px;
  background: var(--soft);
  color: var(--accent);
  font-size: 17px;
  font-weight: 600;
}
.type-seg { margin: 14px 0 6px; }
.edit-rows { border-top: 0.5px solid var(--sep); }
.edit-row { position: relative; display: flex; align-items: center; gap: 12px; min-height: 56px; }
.edit-row + .edit-row { border-top: 0.5px solid var(--sep); }
.edit-row .ico { color: var(--hint); display: flex; }
.edit-row .k { font-size: 16px; flex: 1; }
.edit-row .v { font-size: 16px; color: var(--hint); text-align: right; }
.edit-row .chev { opacity: 0.45; flex-shrink: 0; }
.edit-row input, .edit-row select, .ccy-chip select {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: none;
  cursor: pointer;
  font-size: 16px;
}
.payers { border-top: 0.5px solid var(--sep); padding-top: 20px; }
.edit-hint { margin: 12px 0 0; font-size: 12px; line-height: 1.4; color: var(--hint); text-align: center; }
.sheet-edit .sheet-main { margin: 16px 0 0; width: 100%; height: 52px; border-radius: 14px; }
.confirm { margin-top: 16px; }
.confirm-q { text-align: center; font-size: 15px; font-weight: 600; margin-bottom: 8px; }
.confirm-btns { display: flex; gap: 8px; }
.confirm-btns button { flex: 1; height: 48px; border-radius: 12px; background: var(--sec); font-size: 16px; font-weight: 600; }
.confirm-btns .destr { color: var(--destr); }

/* 08b Name */
.name-card { margin: 12px 16px 0; border-radius: 16px; background: var(--sec); overflow: hidden; }
.name-card .k { font-size: 13px; color: var(--hint); padding: 12px 16px 0; }
.name-input {
  display: block;
  width: 100%;
  min-height: 48px;
  padding: 6px 16px 12px;
  border: none;
  background: transparent;
  font-size: 17px;
  outline: none;
}
.hint.err { color: var(--destr); }

/* Tab bar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  padding: 6px 8px calc(var(--bottom-inset) + 8px);
  border-top: 0.5px solid var(--sep);
  background: var(--bg);
  z-index: 10;
}
.tabbar button {
  flex: 1 1 0;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--hint);
}
.tabbar button.on { color: var(--accent); }
.tabbar .pill { display: flex; align-items: center; justify-content: center; width: 56px; height: 32px; border-radius: 16px; }

/* Gate, errors, loading */
.gate {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 0 40px;
  text-align: center;
}
.gate-icon {
  width: 72px;
  height: 72px;
  border-radius: 36px;
  background: var(--sec);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--hint);
}
.gate-icon svg { stroke: currentColor; }
.gate h1 { margin: 0; font-size: 22px; font-weight: 700; color: var(--destr); }
.gate p { margin: 0; font-size: 16px; line-height: 1.45; color: var(--hint); }

.neterr { padding: 40px 24px; text-align: center; color: var(--hint); font-size: 16px; }
.neterr button {
  margin-top: 16px;
  min-height: 44px;
  padding: 0 20px;
  border-radius: 12px;
  background: var(--soft);
  color: var(--accent);
  font-weight: 600;
}

.skel { height: 14px; border-radius: 7px; background: var(--sec); margin: 8px 16px; }
.skel.lg { height: 40px; width: 160px; }
