:root {
  --bg: var(--tg-theme-secondary-bg-color, #f3efe6);
  --card: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #231d14);
  --hint: var(--tg-theme-hint-color, #8b8273);
  --accent: #c48a24;
  --accent-ink: #7a520c;
  --accent-soft: rgba(196, 138, 36, .14);
  --line: rgba(120, 100, 70, .16);
  --ok: #2e8b4f;
  --bad: #c4473a;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 4px 16px rgba(40, 30, 10, .05);
  color-scheme: light dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  max-width: 820px;
  margin: 0 auto;
}
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- шапка ---------- */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  font-size: 22px; background: linear-gradient(140deg, #e8b54d, #b77b17); box-shadow: var(--shadow);
}
.logo.big { width: 64px; height: 64px; font-size: 36px; border-radius: 18px; margin: 0 auto 8px; }
.title { font-weight: 700; font-size: 17px; letter-spacing: -.2px; }
.subtitle { font-size: 12.5px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#period {
  border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 8px 10px; font-size: 14px; max-width: 46%;
}
.custom { display: flex; gap: 8px; padding: 0 16px 10px; flex-wrap: wrap; }
.custom label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--hint); flex: 1; }
.custom input { flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 7px 8px; }

/* ---------- вкладки ---------- */
.tabs {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  padding: 8px 16px; background: var(--bg);
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: 0 0 auto; border: 0; background: var(--card); color: var(--text);
  padding: 9px 15px; border-radius: 999px; font-weight: 600; font-size: 14px;
  box-shadow: var(--shadow); cursor: pointer;
}
.tabs button.active { background: var(--accent); color: #fff; }

main { padding: 6px 16px 0; }
.loading, .empty { text-align: center; color: var(--hint); padding: 40px 10px; }
.empty .big { font-size: 34px; display: block; margin-bottom: 6px; }

/* ---------- карточки ---------- */
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 14px; }
@media (min-width: 640px) { .cards { grid-template-columns: repeat(4, 1fr); } }
.card {
  background: var(--card); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); min-width: 0;
}
.card .k { font-size: 12.5px; color: var(--hint); }
.card .v { font-size: 19px; font-weight: 700; margin-top: 2px; letter-spacing: -.3px; overflow-wrap: anywhere; }
.card .s { font-size: 12px; color: var(--hint); margin-top: 2px; }
.card.wide { grid-column: 1 / -1; }
.card.accent { background: linear-gradient(140deg, #d9a23c, #a96f12); color: #fff; }
.card.accent .k, .card.accent .s { color: rgba(255,255,255,.85); }
.v.ok { color: var(--ok); } .v.bad { color: var(--bad); }
.card.accent .v.ok, .card.accent .v.bad { color: #fff; }

.section { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.section h3 { margin: 0; padding: 12px 14px 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .4px; color: var(--hint); font-weight: 700; }
.section-head { display: flex; justify-content: space-between; align-items: center; padding-right: 10px; }

table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
table.t th, table.t td { padding: 9px 10px; text-align: right; border-top: 1px solid var(--line); white-space: nowrap; }
table.t th { font-size: 12px; color: var(--hint); font-weight: 600; border-top: 0; padding-top: 4px; }
table.t th:first-child, table.t td:first-child { text-align: left; white-space: normal; }
table.t tr.total td { font-weight: 700; }
table.t tr.click { cursor: pointer; }
table.t tr.sel td { background: var(--accent-soft); }
.tscroll { overflow-x: auto; }

.sheet-link {
  display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text);
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px; margin-bottom: 14px; font-weight: 600;
}
.sheet-note { flex: 1; text-align: right; font-size: 12.5px; color: var(--hint); font-weight: 400; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-err { color: var(--bad); }

/* культурные блоки на сводке */
.crop { padding: 12px 14px; border-top: 1px solid var(--line); }
.crop:first-of-type { border-top: 0; }
.crop-name { font-weight: 700; margin-bottom: 6px; display: flex; justify-content: space-between; gap: 8px; }
.crop-rows { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 13.5px; }
.crop-rows .l { color: var(--hint); }
.crop-rows .r { text-align: right; }
.pill { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-ink); white-space: nowrap; }

/* ---------- фильтры ---------- */
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px 12px; padding: 0 16px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; border: 1px solid var(--line); background: var(--card); padding: 6px 12px;
  border-radius: 999px; font-size: 13.5px; cursor: pointer;
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }

/* ---------- список операций ---------- */
.op { display: flex; gap: 12px; align-items: center; padding: 11px 14px; border-top: 1px solid var(--line); cursor: pointer; }
.op:active { background: var(--accent-soft); }
.op-main { flex: 1; min-width: 0; }
.op-meta { display: flex; gap: 8px; align-items: center; font-size: 12px; color: var(--hint); margin-bottom: 2px; }
.op-no { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; padding: 1px 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.op-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op-sub { font-size: 13px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op-amt { font-weight: 700; white-space: nowrap; text-align: right; }
.sort-btn { border: 0; background: transparent; color: var(--accent-ink); font-size: 13px; font-weight: 600; cursor: pointer; padding: 6px; }
@media (prefers-color-scheme: dark) { :root:not(.tg) { --bg:#16130e; --card:#211c15; --text:#efe8da; --hint:#9d9383; --accent-ink:#f0c46b; --line: rgba(255,240,210,.09);} }
.tg-dark { --accent-ink: #f0c46b; }

/* ---------- кнопка добавления ---------- */
.fab {
  position: fixed; right: 16px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 10;
  border: 0; border-radius: 999px; padding: 14px 22px; font-weight: 700; font-size: 15px;
  background: var(--accent); color: #fff; box-shadow: 0 6px 20px rgba(150, 100, 20, .35); cursor: pointer;
}

/* ---------- форма ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(20, 14, 5, .45); }
.sheet-body {
  position: relative; width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  background: var(--card); border-radius: 20px 20px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .5; } to { transform: none; opacity: 1; } }
.sheet-body h2 { margin: 0 0 2px; font-size: 18px; }
.sheet-body .sub { color: var(--hint); font-size: 13px; margin-bottom: 12px; }
.f { margin-bottom: 12px; }
.f > label { display: block; font-size: 13px; color: var(--hint); margin-bottom: 5px; font-weight: 600; }
.f input, .f select, .f textarea {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 11px 12px; font-size: 16px;
}
.f textarea { min-height: 64px; resize: vertical; }
.f input:focus, .f select:focus, .f textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.f .req { color: var(--bad); }
.f .hint { font-size: 12.5px; color: var(--hint); margin-top: 4px; min-height: 1em; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg input { display: none; }
.seg label { border: 1px solid var(--line); background: var(--bg); padding: 7px 11px; border-radius: 999px; font-size: 14px; cursor: pointer; }
.seg input:checked + label { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.actions { display: flex; gap: 8px; margin-top: 16px; }
.btn { border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 700; cursor: pointer; background: var(--bg); }
.btn.primary { background: var(--accent); color: #fff; flex: 1; }
.btn.danger { color: var(--bad); }
.form-err { color: var(--bad); font-size: 14px; min-height: 1.2em; margin-top: 8px; }

/* ---------- вход ---------- */
.login { position: fixed; inset: 0; z-index: 30; background: var(--bg); display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 340px; text-align: center; background: var(--card); padding: 26px 20px; border-radius: 20px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 20px; margin: 4px 0 6px; }
.login-card p { color: var(--hint); font-size: 14px; margin: 0 0 16px; }
.login-card input { width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 12px; margin-bottom: 10px; font-size: 16px; }
.login-card .btn { width: 100%; }
.login-err { color: var(--bad); font-size: 13px; margin-top: 8px; min-height: 1em; }

.toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #2b2418; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 40; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
