/* Accounting Mini App. Colours come from Telegram theme params; fallbacks only for plain browsers. */

:root {
  --bg: var(--tg-theme-secondary-bg-color, #efeff4);
  --section: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #8a8a8f);
  --subtle: var(--tg-theme-subtitle-text-color, var(--hint));
  --accent: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, #2481cc));
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --header-text: var(--tg-theme-section-header-text-color, var(--hint));
  --separator: var(--tg-theme-section-separator-color, rgba(60, 60, 67, 0.18));
  --danger: var(--tg-theme-destructive-text-color, #e53935);
  --positive: #23a05a;
  --warning: #d98a00;
  --fill-track: color-mix(in srgb, var(--hint) 22%, transparent);
  --pressed: color-mix(in srgb, var(--text) 7%, transparent);
  --field: color-mix(in srgb, var(--hint) 12%, var(--section));

  --inset-top: env(safe-area-inset-top, 0px);
  --inset-bottom: env(safe-area-inset-bottom, 0px);
  --inset-left: env(safe-area-inset-left, 0px);
  --inset-right: env(safe-area-inset-right, 0px);

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", Roboto, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --radius: 12px;
  --gutter: 16px;
  --tabbar: 56px;
  --mainbar: 64px;
  color-scheme: light;
}

:root[data-scheme="dark"] {
  color-scheme: dark;
  --positive: #3fc77a;
  --warning: #f0a830;
}

:root[data-scheme="dark"]:not(.tg-themed) {
  --bg: #0f0f10;
  --section: #1c1c1e;
  --text: #ffffff;
  --hint: #8d8e93;
  --accent: #5ea9ee;
  --link: #5ea9ee;
  --button: #3a8fdc;
  --separator: rgba(255, 255, 255, 0.12);
  --danger: #ff5a52;
}

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

html {
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
}

/* Browser fallback main button is fixed at the bottom: keep focused fields above it. */
html:has(body.has-main) {
  scroll-padding-bottom: calc(var(--mainbar) + var(--inset-bottom) + 16px);
}

body {
  margin: 0;
  min-height: var(--vh-stable, 100vh);
  background: var(--bg);
  color: var(--text);
  font: 400 16px/1.35 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  overscroll-behavior-y: none;
}

::selection {
  background: color-mix(in srgb, var(--accent) 30%, transparent);
}

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

button,
select,
input {
  touch-action: manipulation;
}

@media (hover: hover) {
  button.cell:hover,
  .quick button:hover,
  .more:hover,
  .icon-button:hover {
    background-color: color-mix(in srgb, var(--section) 94%, var(--text));
  }
  .icon-button:hover {
    background-color: var(--pressed);
  }
  .text-button:hover:not(:disabled) {
    text-decoration: underline;
    text-underline-offset: 3px;
  }
}

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

[hidden] {
  display: none !important;
}

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

.icon {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

/* ---------- Layout ---------- */

#app {
  padding: calc(var(--inset-top) + 8px) calc(var(--inset-right) + 0px) calc(var(--tabbar) + var(--inset-bottom) + 24px)
    calc(var(--inset-left) + 0px);
  max-width: 640px;
  margin: 0 auto;
}

body.screen-mode #app {
  padding-bottom: calc(var(--inset-bottom) + 32px);
}

body.screen-mode.has-main #app {
  padding-bottom: calc(var(--mainbar) + var(--inset-bottom) + 24px);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 44px;
  padding: 0 8px 0 var(--gutter);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
  min-width: 0;
}

.screen-title {
  margin: 4px var(--gutter) 12px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

#fallback-back {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin: 0 0 0 4px;
  padding: 8px 10px 8px 4px;
  border: 0;
  background: none;
  color: var(--link);
  font-size: 17px;
  cursor: pointer;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--link);
  cursor: pointer;
}

.icon-button:active {
  background: var(--pressed);
}

/* ---------- Sections and cells (Telegram grouped lists) ---------- */

.section {
  margin: 0 12px 20px;
}

.section-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 var(--gutter) 6px;
  padding: 0 4px;
  color: var(--header-text);
  font-size: 14px;
  font-weight: 500;
}

.section-header .meta {
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  color: var(--hint);
  text-align: right;
}

.list {
  background: var(--section);
  border-radius: var(--radius);
  overflow: hidden;
}

.section-footer {
  margin: 6px var(--gutter) 0;
  padding: 0 4px;
  color: var(--hint);
  font-size: 13px;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 48px;
  padding: 10px var(--gutter);
  border: 0;
  background: none;
  text-align: left;
  color: var(--text);
}

button.cell,
a.cell {
  cursor: pointer;
  text-decoration: none;
}

button.cell:active,
a.cell:active {
  background: var(--pressed);
}

.cell + .cell::before,
.cell + .field::before,
.field + .cell::before,
.field + .field::before {
  content: "";
  position: absolute;
  top: 0;
  left: var(--gutter);
  right: 0;
  border-top: 0.5px solid var(--separator);
}

.cell-body {
  flex: 1;
  min-width: 0;
}

.cell-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cell-sub {
  display: block;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--subtle);
  font-size: 14px;
}

.cell-after {
  flex: none;
  max-width: 55%;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.cell-after .cell-sub {
  text-align: right;
}

.cell-after .equiv {
  font-size: 12px;
  color: var(--hint);
}

.cell .chevron {
  width: 18px;
  height: 18px;
  margin-right: -4px;
  color: var(--hint);
}

.cell.action {
  color: var(--link);
}

.cell.action .icon {
  width: 22px;
  height: 22px;
}

.cell.danger {
  color: var(--danger);
  justify-content: center;
}

.cell.muted .cell-title,
.cell.muted .amount {
  color: var(--hint);
}

.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
  font-size: 12px;
  font-weight: 500;
  vertical-align: 1px;
}

.badge.danger {
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  color: var(--danger);
}

.badge.neutral {
  background: var(--fill-track);
  color: var(--subtle);
}

.glyph {
  display: grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--fill-track);
  color: var(--subtle);
}

.glyph .icon {
  width: 18px;
  height: 18px;
}

.glyph.income {
  background: color-mix(in srgb, var(--positive) 16%, transparent);
  color: var(--positive);
}

.glyph.transfer {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
}

/* ---------- Money ---------- */

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

.amount-unit {
  color: var(--hint);
  font-weight: 400;
}

.amount.positive {
  color: var(--positive);
}

.amount.positive .amount-unit {
  color: color-mix(in srgb, var(--positive) 70%, var(--hint));
}

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

.amount.void {
  color: var(--hint);
  text-decoration: line-through;
}

.total-value {
  display: block;
  font-size: 44px;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.total-value[data-size="s"] {
  font-size: 38px;
}

.total-value[data-size="xs"] {
  font-size: 32px;
}

.total-note {
  display: block;
  margin-top: 6px;
  color: var(--hint);
  font-size: 14px;
}

.total-note.warn {
  color: var(--warning);
}

/* Quick actions under the total: short verbs, like a wallet. */
.quick {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 0 12px 20px;
}

.quick button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-height: 64px;
  padding: 10px 4px 8px;
  border: 0;
  border-radius: var(--radius);
  background: var(--section);
  color: var(--link);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.quick button:disabled {
  color: var(--hint);
  cursor: default;
}

.quick button:active {
  background: color-mix(in srgb, var(--section) 88%, var(--text));
}

/* ---------- Envelope fill line: the one signature element ---------- */

.fill {
  position: absolute;
  left: var(--gutter);
  right: var(--gutter);
  bottom: 6px;
  height: 3px;
  border-radius: 2px;
  background: var(--fill-track);
  overflow: hidden;
}

.fill > span {
  display: block;
  width: var(--fill, 0%);
  max-width: 100%;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

.fill.warn > span {
  background: var(--warning);
}

.fill.over > span {
  background: var(--danger);
}

.cell.has-fill {
  padding-bottom: 16px;
}

/* ---------- Banner (pending drafts, errors) ---------- */

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 12px 20px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--section);
}

.banner .icon {
  color: var(--accent);
}

.banner.error .icon {
  color: var(--danger);
}

.banner-text {
  flex: 1;
  min-width: 0;
  font-size: 15px;
}

.banner-text small {
  display: block;
  color: var(--hint);
  font-size: 13px;
}

.text-button {
  flex: none;
  padding: 6px 2px;
  border: 0;
  background: none;
  color: var(--link);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.text-button:disabled {
  color: var(--hint);
  cursor: default;
}

/* ---------- Filters (history) ---------- */

.month-nav {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 8px;
  margin-bottom: 8px;
}

.month-nav h1 {
  flex: 1;
  margin: 0;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
}

.month-nav .icon-button:disabled {
  color: var(--hint);
  opacity: 0.4;
}

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0 12px 16px;
}

.select-wrap {
  position: relative;
  min-width: 0;
}

.select-wrap::after {
  content: "";
  position: absolute;
  right: 13px;
  top: 13px;
  width: 6px;
  height: 6px;
  border-right: 1.6px solid var(--hint);
  border-bottom: 1.6px solid var(--hint);
  transform: rotate(45deg);
  pointer-events: none;
}

.select-wrap select {
  width: 100%;
  min-width: 0;
  height: 36px;
  padding: 0 30px 0 12px;
  border: 0;
  border-radius: 10px;
  background: var(--section);
  -webkit-appearance: none;
  appearance: none;
  font-size: 15px;
  text-overflow: ellipsis;
  cursor: pointer;
}

.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 0 12px 20px;
  background: var(--section);
  border-radius: var(--radius);
}

.summary > div {
  padding: 10px 14px;
  min-width: 0;
}

.summary > div + div {
  border-left: 0.5px solid var(--separator);
}

.summary dt {
  color: var(--hint);
  font-size: 13px;
}

.summary dd {
  margin: 2px 0 0;
  font-size: 18px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.summary dd .amount {
  white-space: normal;
}

/* ---------- Segmented control ---------- */

.segmented {
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  gap: 2px;
  margin: 0 12px 16px;
  padding: 2px;
  border-radius: 10px;
  background: var(--fill-track);
}

.segmented button {
  min-height: 32px;
  padding: 4px 6px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.segmented button[aria-pressed="true"] {
  background: var(--section);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

/* ---------- Forms ---------- */

.field {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  padding: 0 var(--gutter);
}

.field > label,
.field > .label {
  flex: none;
  max-width: 60%;
  color: var(--text);
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.field input,
.field select,
.field textarea {
  flex: 1;
  min-width: 0;
  height: 46px;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: right;
  font-size: 16px;
  outline: none;
}

.field select {
  -webkit-appearance: none;
  appearance: none;
  text-align-last: right;
  color: var(--link);
  cursor: pointer;
}

.field input::placeholder {
  color: var(--hint);
}

.field input[type="date"] {
  color: var(--link);
  display: flex;
  justify-content: flex-end;
}

.field input[type="date"]::-webkit-date-and-time-value {
  text-align: right;
}

.field.big {
  min-height: 64px;
}

.field.big input {
  height: 62px;
  font-size: 28px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.field .unit {
  flex: none;
  color: var(--hint);
  font-size: 16px;
}

.field.big .unit {
  font-size: 20px;
}

.field.invalid label,
.field.invalid .label {
  color: var(--danger);
}

.field:focus-within {
  background: color-mix(in srgb, var(--accent) 5%, transparent);
}

.field-error {
  margin: 6px var(--gutter) 0;
  padding: 0 4px;
  color: var(--danger);
  font-size: 13px;
}

.toggle {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none !important;
  width: 51px !important;
  height: 31px !important;
  border-radius: 16px;
  background: var(--fill-track) !important;
  cursor: pointer;
  transition: background 0.2s;
}

.toggle::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s ease-out;
}

.toggle:checked {
  background: var(--button) !important;
}

.toggle:checked::after {
  transform: translateX(20px);
}

.period {
  display: flex;
  flex: 1;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.period input {
  flex: 0 0 3.2em !important;
  text-align: right;
}

.period select {
  flex: 0 1 auto !important;
}

/* ---------- Form error/status line ---------- */

.form-error {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin: 0 12px 16px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 10%, var(--section));
  color: var(--text);
  font-size: 15px;
}

.form-error .icon {
  color: var(--danger);
  width: 20px;
  height: 20px;
  margin-top: 1px;
}

.form-error .text-button {
  margin-left: auto;
  padding: 0;
}

/* ---------- Bottom tabs and fallback main button ---------- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 0 var(--inset-right) var(--inset-bottom) var(--inset-left);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  border-top: 0.5px solid var(--separator);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
}

body.screen-mode .tabbar {
  display: none;
}

.tabbar button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: var(--tabbar);
  border: 0;
  background: none;
  color: var(--hint);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
}

.tabbar button[aria-current="page"] {
  color: var(--accent);
}

#fallback-main {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(var(--inset-bottom) + 10px);
  z-index: 11;
  max-width: 616px;
  height: 50px;
  margin: 0 auto;
  border: 0;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

#fallback-main:disabled {
  opacity: 0.55;
  cursor: default;
}

#fallback-main.busy {
  color: transparent;
  background-image: linear-gradient(90deg, transparent, color-mix(in srgb, var(--button-text) 25%, transparent), transparent);
  background-size: 50% 100%;
  background-repeat: no-repeat;
  animation: sweep 1s linear infinite;
}

@keyframes sweep {
  from {
    background-position: -100% 0;
  }
  to {
    background-position: 200% 0;
  }
}

/* ---------- States ---------- */

.skeleton {
  display: block;
  height: 14px;
  width: var(--w, 60%);
  border-radius: 6px;
  background: var(--fill-track);
  animation: pulse 1.2s ease-in-out infinite;
}

.skeleton.tall {
  height: 34px;
  width: 55%;
}

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

.empty {
  padding: 28px var(--gutter);
  text-align: center;
  color: var(--hint);
  font-size: 15px;
}

.empty strong {
  display: block;
  margin-bottom: 4px;
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
}

.empty .text-button {
  margin-top: 8px;
}

.full-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 70vh;
  padding: 24px 32px;
  text-align: center;
  color: var(--hint);
}

.full-state h1 {
  margin: 0;
  color: var(--text);
  font-size: 20px;
  font-weight: 700;
}

.full-state p {
  margin: 0;
  max-width: 32ch;
}

.full-state .icon {
  width: 40px;
  height: 40px;
  color: var(--hint);
}

.more {
  display: block;
  width: calc(100% - 24px);
  margin: 0 12px 20px;
  padding: 12px;
  border: 0;
  border-radius: var(--radius);
  background: var(--section);
  color: var(--link);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.lead {
  margin: 0 calc(var(--gutter) + 12px) 16px;
  color: var(--hint);
  font-size: 15px;
}

.preview {
  margin: 0;
  padding: 0;
  list-style: none;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

@media (max-width: 370px) {
  :root {
    --gutter: 14px;
  }
  .total-value {
    font-size: 38px;
  }
  .total-value[data-size="s"] {
    font-size: 32px;
  }
  .total-value[data-size="xs"] {
    font-size: 27px;
  }
}

/* Centered total; the settings button is out of flow so it cannot shift the center. */
.total {
  position: relative;
  padding: 20px calc(var(--gutter) + 40px) 24px;
  text-align: center;
}

.total .icon-button {
  position: absolute;
  top: 4px;
  right: 8px;
}

.total-main {
  min-width: 0;
}

.total-missing {
  display: block;
  font-size: 22px;
  font-weight: 600;
  color: var(--hint);
  letter-spacing: -0.01em;
}

.cell-row {
  display: flex;
  align-items: baseline;
  gap: 12px;
  min-width: 0;
}

.cell-row .cell-title {
  flex: 1;
  min-width: 0;
}

.cell-row .cell-after {
  max-width: none;
}

/* ---------- Investments ---------- */

.warn-text {
  color: var(--warning);
}

.quick.four {
  grid-template-columns: repeat(4, 1fr);
}

.quick.two {
  grid-template-columns: repeat(2, 1fr);
}

.portfolio-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: -4px var(--gutter) 18px;
  text-align: center;
}


.text-button.refresh {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  font-size: 14px;
}

.text-button.refresh .icon {
  width: 16px;
  height: 16px;
}

.field .segmented {
  flex: 1;
  margin: 0;
}

.picker:empty {
  display: none;
}

.picker .cell {
  width: 100%;
}
