:root {
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --text: #1C1C1E;
  --secondary: #6C6C70;
  --muted: #8E8E93;
  --blue: #007AFF;
  --green: #34C759;
  --red: #FF3B30;
  --sep: #D1D1D6;
  --fill: #F2F2F7;
  --fill-2: #E9E9EE;
  --nav: rgba(249, 249, 249, 0.88);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1C1C1E;
    --text: #FFFFFF;
    --secondary: #EBEBF5;
    --muted: #8E8E93;
    --blue: #0A84FF;
    --green: #30D158;
    --red: #FF453A;
    --sep: #38383A;
    --fill: #2C2C2E;
    --fill-2: #3A3A3C;
    --nav: rgba(28, 28, 30, 0.88);
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

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

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px calc(96px + env(safe-area-inset-bottom));
  min-height: 100vh;
  min-height: 100dvh;
}

/* ---------- шапка ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 0 10px;
  background: linear-gradient(to bottom, var(--bg) 70%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.topbar-t {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.6px;
}

.topbar-s {
  font-size: 13px;
  color: var(--muted);
  margin-top: 1px;
}

.icon-btn {
  border: 0;
  background: var(--fill);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 19px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.15s ease;
}
.icon-btn:active { transform: scale(0.94); }

.topbar-actions { display: flex; align-items: center; gap: 8px; }
.logout-btn {
  border: 0;
  background: transparent;
  color: var(--blue);
  font-size: 16px;
  padding: 8px 4px;
  cursor: pointer;
}
.logout-btn:active { opacity: 0.5; }

.back-btn {
  border: 0;
  background: transparent;
  color: var(--blue);
  font-size: 17px;
  padding: 0 8px 6px 0;
  cursor: pointer;
}

/* ---------- карточки ---------- */

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}

.section-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.3px;
  margin: 18px 2px 8px;
}

.muted { color: var(--muted); font-size: 13px; }
.green { color: var(--green); }
.red { color: var(--red); }

/* ---------- фонд ---------- */

.fund-label {
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.fund-value {
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -1px;
  margin: 2px 0 10px;
}

.fund-split {
  display: flex;
  gap: 10px;
  padding-top: 12px;
  border-top: 1px solid var(--sep);
}

.fund-split > div { flex: 1; }
.fund-split span { display: block; font-size: 12px; color: var(--muted); }
.fund-split b { font-size: 17px; font-weight: 600; }

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

.segmented {
  display: flex;
  gap: 2px;
  background: var(--fill-2);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 12px;
  overflow-x: auto;
  scrollbar-width: none;
}
.segmented::-webkit-scrollbar { display: none; }

.segmented button {
  flex: 1 0 auto;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.segmented button.on {
  background: var(--card);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

/* ---------- период отчётов ---------- */

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 12px; }
.chips button {
  border: 0;
  background: var(--fill-2);
  color: var(--text);
  font-size: 14px;
  padding: 7px 13px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.chips button.on { background: var(--blue); color: #fff; font-weight: 600; }
.period-dates { display: flex; gap: 10px; }
.period-dates .field { flex: 1; margin-bottom: 0; }
.period-dates .field input { padding: 10px 12px; font-size: 15px; }

/* ---------- плитки разделов ---------- */

.tiles {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

@media (min-width: 560px) { .tiles { grid-template-columns: repeat(4, 1fr); } }

.tile {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--card);
  border: 0;
  border-radius: 14px;
  padding: 14px;
  text-align: left;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease;
}
.tile:active { transform: scale(0.97); }

.tile-ico {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  background: var(--blue);
}
.tile-ico.g { background: var(--green); }
.tile-ico.r { background: var(--red); }
.tile-ico.n { background: var(--muted); }

.tile-label { font-size: 15px; font-weight: 600; }
.tile-count { font-size: 12px; color: var(--muted); margin-top: -4px; }

/* ---------- список строк ---------- */

.list {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--sep);
  cursor: pointer;
  background: var(--card);
  width: 100%;
  text-align: left;
  border-left: 0;
  border-right: 0;
  border-top: 0;
}
.row:last-child { border-bottom: 0; }
.row:active { background: var(--fill); }
.row[hidden] { display: none; }

.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 16px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: 13px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.receipt-row { margin-top: 4px; white-space: normal; }
.receipt-link {
  display: inline-block;
  margin-right: 10px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--blue);
  font: inherit;
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
}
.row-value { font-size: 16px; font-weight: 600; white-space: nowrap; }
.row-values { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; white-space: nowrap; }
.row-line { display: flex; align-items: baseline; gap: 6px; }
.row-line span { font-size: 11px; color: var(--muted); }
.row-line b { font-size: 14px; font-weight: 600; }
.row-line b.green { color: var(--green); }
.row-line b.red { color: var(--red); }

.empty {
  padding: 34px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.search {
  width: 100%;
  border: 0;
  background: var(--fill-2);
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 16px;
  margin-bottom: 12px;
  outline: none;
}
.search::placeholder { color: var(--muted); }

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

.btn {
  border: 0;
  border-radius: 12px;
  padding: 13px 18px;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  background: var(--fill-2);
  color: var(--text);
  width: 100%;
  transition: transform 0.12s ease, opacity 0.2s;
}
.btn:active { transform: scale(0.985); }
.btn.primary { background: var(--blue); color: #fff; }
.btn.danger { background: transparent; color: var(--red); }
.btn.ghost { background: transparent; color: var(--blue); }
.btn:disabled { opacity: 0.5; }

/* ---------- поля ---------- */

.field { display: block; margin-bottom: 12px; }
.field > span {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 5px 2px;
}
.field input,
.field select {
  width: 100%;
  border: 0;
  background: var(--fill);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 16px;
  outline: none;
  appearance: none;
}
.field input:focus, .field select:focus { box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.25); }
.field input[disabled] { color: var(--muted); }

.date-wrap { position: relative; }
.date-wrap .pick {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  font-size: 18px;
  padding: 4px 8px;
  cursor: pointer;
}
.date-wrap input[type="date"] { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.kv { padding: 10px 0; border-bottom: 1px solid var(--sep); display: flex; gap: 12px; justify-content: space-between; }
.kv:last-child { border-bottom: 0; }
.kv span { color: var(--muted); font-size: 14px; }
.kv b { font-weight: 500; text-align: right; word-break: break-word; }

/* ---------- модальные карточки ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0);
  transition: background 0.25s ease;
}
.modal-backdrop.open { background: rgba(0, 0, 0, 0.38); }

.modal {
  width: 100%;
  max-width: 520px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--card);
  border-radius: 22px 22px 0 0;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
}
.modal-backdrop.open .modal { transform: translateY(0); }

@media (min-width: 640px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 22px; transform: translateY(14px) scale(0.97); opacity: 0; transition: transform 0.25s ease, opacity 0.2s ease; }
  .modal-backdrop.open .modal { transform: none; opacity: 1; }
}

.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-title { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; }
.modal-close { border: 0; background: var(--fill); color: var(--muted); width: 30px; height: 30px; border-radius: 15px; cursor: pointer; font-size: 15px; }
.modal-body { margin-bottom: 4px; }
.modal-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }

/* ---------- нижняя панель ---------- */

.nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  justify-content: center;
  background: var(--nav);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--sep);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}

.nav-inner { display: flex; width: 100%; max-width: 720px; }
.nav-item {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 5px 0 3px;
  cursor: pointer;
}
.nav-item .ico { font-size: 21px; line-height: 1; }
.nav-item.on { color: var(--blue); }

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

.login {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 18px;
  padding: 24px 4px 40px;
}

.login-brand { text-align: center; }
.logo {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  border-radius: 18px;
  display: block;
  object-fit: cover;
  background: transparent;
  box-shadow: 0 10px 24px rgba(14, 122, 52, 0.28);
}
.login-brand h1 { margin: 0; font-size: 25px; letter-spacing: -0.5px; }
.login-brand p { margin: 5px 0 0; color: var(--muted); font-size: 14px; }
.login-card { display: flex; flex-direction: column; }
.login-error { color: var(--red); font-size: 14px; margin: 6px 2px 0; }

/* ---------- тост ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(92px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  background: rgba(28, 28, 30, 0.94);
  color: #fff;
  font-size: 14px;
  padding: 11px 18px;
  border-radius: 14px;
  z-index: 60;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100% - 40px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--sep);
  border-top-color: var(--blue);
  animation: spin 0.8s linear infinite;
  margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Чек поверх страницы: без перехода на Google Drive */
.receipt-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  background: rgba(0, 0, 0, 0.72);
}
.receipt-modal-box {
  display: flex;
  flex-direction: column;
  width: min(960px, 100%);
  height: min(90vh, 100%);
  overflow: hidden;
  border-radius: 16px;
  background: var(--card, #fff);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}
.receipt-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--sep, #e5e5ea);
}
.receipt-modal-title { font-weight: 600; }
.receipt-modal-actions { display: flex; align-items: center; gap: 12px; }
.receipt-modal-ext { font-size: 13px; color: var(--blue, #0a84ff); text-decoration: none; }
.receipt-modal-close {
  border: 0;
  background: none;
  padding: 0 4px;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  color: inherit;
}
.receipt-modal-body {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(127, 127, 127, 0.12);
}
.receipt-frame { width: 100%; height: 100%; border: 0; background: #fff; }
.receipt-image { max-width: 100%; max-height: 100%; object-fit: contain; }

/* Прикрепление чека в форме */
.receipt-pick { display: flex; align-items: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.receipt-pick-btn { padding: 8px 12px; font-size: 14px; }
.receipt-pick .muted { font-size: 13px; }
