/*
 * Стили макета. Цвета — только из темы Telegram (--tg-theme-*, их ставит telegram-stub.js или настоящий Telegram)
 * плюс свои статусные цвета, подобранные отдельно под светлую и тёмную тему.
 */

/* Базовые цвета (bg, secondary_bg, text, hint, link, button) Telegram передаёт всегда, остальные — только новые
   клиенты: их запасной вариант — из базовых, иначе в тёмной теме старого клиента всплывут светлые линии. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg-2: var(--tg-theme-secondary-bg-color, #f0f2f5);
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, #0f1419);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --subtitle: var(--tg-theme-subtitle-text-color, var(--hint));
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --header: var(--tg-theme-header-bg-color, var(--bg));
  --bottom-bar: var(--tg-theme-bottom-bar-bg-color, var(--bg));
  --section-header: var(--tg-theme-section-header-text-color, var(--hint));
  --separator: var(--tg-theme-section-separator-color, var(--bg-2));
  --danger: var(--tg-theme-destructive-text-color, #d93838);

  --danger-soft: #fdecec;
  --ok: #1f9d55;
  --ok-soft: #e7f6ee;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --accent-soft: #e8f2fb;
  --stage: #dfe3e8;
  --shadow: 0 18px 50px rgba(15, 20, 25, 0.18);

  --radius: 14px;
  --radius-sm: 10px;
  --tap: 48px;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', Roboto, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  font-size: 16px;
  line-height: 1.35;
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

:root[data-scheme='dark'] {
  --danger-soft: #3a2228;
  --ok: #4cc77f;
  --ok-soft: #1c3329;
  --warn: #f0b14a;
  --warn-soft: #3a3020;
  --accent-soft: #1f3247;
  --stage: #070b10;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--stage);
  color: var(--text);
}

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

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

code {
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.9em;
}

[hidden] {
  display: none !important;
}

/* ---------- Сцена и «телефон» ---------- */

.stage {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px 16px;
}

.stage-note {
  margin: 0;
  max-width: 390px;
  font-size: 13px;
  color: var(--hint);
  text-align: center;
}

.phone {
  position: relative;
  width: 390px;
  height: min(844px, calc(100vh - 80px));
  min-height: 560px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-2);
  border-radius: 32px;
  box-shadow: var(--shadow);
  border: 1px solid var(--separator);
}

/* На телефоне макет занимает весь экран — как настоящее мини-приложение. Высота от страницы, а не 100vh:
   так он помещается и в рамку просмотрщика с отступами под системные панели. */
@media (max-width: 500px) {
  .stage {
    height: 100%;
    min-height: 0;
    padding: 0;
    gap: 0;
  }
  .stage-note {
    display: none;
  }
  .phone {
    width: 100%;
    height: 100%;
    min-height: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }
}

/* Внутри настоящего Telegram (класс tg-real ставит telegram-stub.js): во весь экран, без рамки телефона,
   шапку, нижнюю кнопку и окна рисует сам Telegram. */
:root.tg-real body {
  background: var(--bg-2);
}

:root.tg-real .stage {
  height: 100%;
  min-height: 0;
  padding: 0;
  gap: 0;
}

:root.tg-real .phone {
  width: 100%;
  height: 100%;
  min-height: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

:root.tg-real .stage-note,
:root.tg-real .tg-header,
:root.tg-real .tg-mainbar,
:root.tg-real .tg-popup {
  display: none !important;
}

/* ---------- Хром Telegram ---------- */

.tg-header {
  position: relative;
  flex: none;
  display: grid;
  grid-template-columns: 96px 1fr 96px;
  align-items: center;
  height: 52px;
  padding: 0 6px;
  background: var(--header);
  border-bottom: 1px solid var(--separator);
  z-index: 5;
}

.tg-header-title {
  grid-column: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}

.tg-header-title strong {
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.tg-header-title span {
  font-size: 12px;
  color: var(--hint);
}

.tg-header-button {
  grid-row: 1;
  grid-column: 1;
  justify-self: start;
  height: 40px;
  padding: 0 10px;
  border: 0;
  background: none;
  color: var(--link);
  font-size: 16px;
  border-radius: var(--radius-sm);
}

.tg-header-more {
  grid-column: 3;
  justify-self: end;
  width: 44px;
  font-size: 22px;
  font-weight: 700;
}

.tg-menu {
  position: absolute;
  top: 46px;
  right: 8px;
  min-width: 230px;
  padding: 6px;
  background: var(--section);
  border: 1px solid var(--separator);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.tg-menu button {
  text-align: left;
  border: 0;
  background: none;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
}

.tg-menu button:active {
  background: var(--bg-2);
}

.webview {
  position: relative;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: var(--bg-2);
}

.boot {
  padding: 32px 20px;
  color: var(--hint);
  line-height: 1.6;
}

.tg-mainbar {
  flex: none;
  padding: 8px 12px;
  background: var(--bottom-bar);
  border-top: 1px solid var(--separator);
}

.tg-mainbutton {
  width: 100%;
  min-height: 50px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  font-size: 16px;
}

.tg-mainbutton:disabled {
  opacity: 0.45;
  cursor: default;
}

.tg-popup {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.42);
}

.tg-popup-card {
  width: 100%;
  max-width: 320px;
  padding: 18px 18px 8px;
  background: var(--section);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.tg-popup-card p {
  margin: 0 0 12px;
  white-space: pre-line;
}

.tg-popup-actions {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}

.tg-popup-button {
  min-height: 44px;
  min-width: 64px;
  padding: 0 12px;
  border: 0;
  background: none;
  color: var(--link);
  font-weight: 600;
  border-radius: var(--radius-sm);
}

.tg-popup-button.is-destructive {
  color: var(--danger);
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 80px;
  transform: translateX(-50%);
  z-index: 15;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(20, 24, 28, 0.9);
  color: #ffffff;
  font-size: 14px;
  text-align: center;
  pointer-events: none;
}

/* ---------- Каркас экрана ---------- */

.screen {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.screen-body {
  flex: 1;
  padding: 12px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.screen-title {
  margin: 4px 4px 0;
  font-size: 22px;
  font-weight: 700;
}

.screen-lead {
  margin: -6px 4px 0;
  color: var(--subtitle);
  font-size: 14px;
}

.tabbar {
  position: sticky;
  bottom: 0;
  z-index: 4;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--bottom-bar);
  border-top: 1px solid var(--separator);
}

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

.tabbar button svg {
  width: 24px;
  height: 24px;
}

.tabbar button.is-active {
  color: var(--accent);
  font-weight: 600;
}

/* ---------- Дата ---------- */

.datebar {
  display: grid;
  grid-template-columns: var(--tap) 1fr var(--tap);
  align-items: center;
  background: var(--section);
  border-radius: var(--radius);
  min-height: 56px;
}

.datebar button {
  height: var(--tap);
  border: 0;
  background: none;
  color: var(--link);
  font-size: 26px;
  border-radius: var(--radius-sm);
}

.datebar button:disabled {
  color: var(--hint);
  opacity: 0.35;
  cursor: default;
}

.datebar-label {
  text-align: center;
}

.datebar-label strong {
  display: block;
  font-size: 17px;
}

.datebar-label span {
  font-size: 13px;
  color: var(--subtitle);
}

.datebar-label span.is-past {
  color: var(--warn);
  font-weight: 600;
}

/* ---------- Карточки и списки ---------- */

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

.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px 4px;
}

.card-head h2,
.card-head h3 {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--section-header);
}

.card-head .card-head-note {
  font-size: 13px;
  color: var(--hint);
}

.card-note {
  margin: 0;
  padding: 4px 14px 12px;
  font-size: 13px;
  color: var(--hint);
}

.card.is-danger {
  box-shadow: inset 4px 0 0 var(--danger);
}

.card.is-danger .card-head h2 {
  color: var(--danger);
}

.card.is-warn {
  box-shadow: inset 4px 0 0 var(--warn);
}

.card.is-warn .card-head h2 {
  color: var(--warn);
}

.card.is-ok {
  box-shadow: inset 4px 0 0 var(--ok);
}

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

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  padding: 8px 14px;
  border-top: 1px solid var(--separator);
}

.rows > .row:first-child,
.rows > li:first-child > .row,
.card > .row:first-child {
  border-top: 0;
}

/* красный / жёлтый текст внутри строк и подсказок (у карточек и кнопок — свои правила) */
.row-value strong.is-danger,
.row-sub .is-danger,
.field-hint .is-danger,
.screen-lead .is-danger {
  color: var(--danger);
}

.field-hint .is-warn {
  color: var(--warn);
}

button.row {
  width: 100%;
  text-align: left;
  border-left: 0;
  border-right: 0;
  border-bottom: 0;
  background: none;
}

button.row:active,
button.tile:active {
  background: var(--bg-2);
}

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

.row-title {
  display: block;
  font-weight: 500;
  overflow-wrap: anywhere;
}

.row-sub {
  display: block;
  font-size: 13px;
  color: var(--subtitle);
}

.row-sub.is-danger {
  color: var(--danger);
}

.row-sub.is-warn {
  color: var(--warn);
}

.row-sub.is-ok {
  color: var(--ok);
}

.row-value {
  flex: none;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.row-value strong {
  display: block;
  font-size: 18px;
}

.row-value span {
  font-size: 13px;
  color: var(--subtitle);
}

.row-chevron {
  flex: none;
  color: var(--hint);
  font-size: 20px;
}

.row.is-deleted .row-title,
.row.is-deleted .row-value {
  text-decoration: line-through;
  color: var(--hint);
}

.big {
  font-size: 22px;
  font-weight: 700;
}

.num {
  font-variant-numeric: tabular-nums;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--bg-2);
  color: var(--subtitle);
  white-space: nowrap;
}

.chip.is-danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.chip.is-warn {
  background: var(--warn-soft);
  color: var(--warn);
}

.chip.is-ok {
  background: var(--ok-soft);
  color: var(--ok);
}

.chip.is-accent {
  background: var(--accent-soft);
  color: var(--accent);
}

button.chip {
  border: 0;
  min-height: 32px;
  margin-top: 4px;
  padding: 0 12px;
}

/* полоска «остаток против минимума» */
.meter {
  margin-top: 6px;
  height: 4px;
  border-radius: 2px;
  background: var(--bg-2);
  overflow: hidden;
}

.meter i {
  display: block;
  height: 100%;
  background: var(--ok);
}

.meter.is-danger i {
  background: var(--danger);
}

/* ---------- Плитки главной ---------- */

.tiles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.tile {
  min-height: 108px;
  padding: 12px;
  border: 0;
  border-radius: var(--radius);
  background: var(--section);
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tile-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.tile-name {
  font-weight: 600;
}

.tile-plus {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
}

.tile-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.tile-sub {
  font-size: 13px;
  color: var(--subtitle);
}

.tile-sub.is-warn {
  color: var(--warn);
  font-weight: 600;
}

.tile-sub.is-ok {
  color: var(--ok);
  font-weight: 600;
}

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

.btn {
  min-height: var(--tap);
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.btn.is-primary {
  background: var(--button);
  color: var(--button-text);
}

.btn.is-danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.btn.is-block {
  width: 100%;
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.card-actions {
  display: flex;
  gap: 8px;
  padding: 4px 14px 14px;
}

.card-actions .btn {
  flex: 1;
}

.link-button {
  border: 0;
  background: none;
  color: var(--link);
  padding: 0;
  min-height: 32px;
  font-size: 14px;
  font-weight: 600;
}

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

.segmented {
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  gap: 4px;
  padding: 4px;
  background: var(--section);
  border-radius: var(--radius);
}

.segmented button {
  min-height: 44px;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--subtitle);
  font-weight: 600;
  font-size: 15px;
}

.segmented button.is-active {
  background: var(--button);
  color: var(--button-text);
}

/* четыре сегмента на 375 px: подпись в одну строку, число — бейджем */
.segmented button:first-child:nth-last-child(4),
.segmented button:first-child:nth-last-child(4) ~ button {
  padding: 0 2px;
  font-size: 14px;
  white-space: nowrap;
}

.seg-badge {
  display: inline-block;
  min-width: 18px;
  margin-left: 3px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  line-height: 18px;
}

.segmented button.is-active .seg-badge {
  background: rgba(255, 255, 255, 0.25);
  color: var(--button-text);
}

/* ---------- Степпер − N + ---------- */

.stepper {
  flex: none;
  display: grid;
  grid-template-columns: 46px 44px 46px;
  align-items: center;
  background: var(--bg-2);
  border-radius: var(--radius-sm);
}

.stepper button {
  height: var(--tap);
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 24px;
  font-weight: 600;
  border-radius: var(--radius-sm);
}

.stepper button:active {
  background: var(--accent-soft);
}

.stepper button:disabled {
  color: var(--hint);
  opacity: 0.4;
  cursor: default;
}

.stepper output {
  text-align: center;
  font-size: 19px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stepper.is-zero output {
  color: var(--hint);
  font-weight: 500;
}

.stepper.is-changed {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.stepper.is-over {
  box-shadow: inset 0 0 0 2px var(--danger);
}

.step-row {
  flex-wrap: wrap;
}

.step-row .row-main {
  min-width: 140px;
}

/* ---------- Поля ввода ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 14px;
  border-top: 1px solid var(--separator);
}

.field:first-child {
  border-top: 0;
}

.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--section-header);
}

.field-input {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 12px;
  background: var(--bg-2);
  border-radius: var(--radius-sm);
  border: 2px solid transparent;
}

.field-input:focus-within {
  border-color: var(--button);
}

.field-input.is-error {
  border-color: var(--danger);
}

.field-input input {
  flex: 1;
  min-width: 0;
  height: 44px;
  border: 0;
  background: none;
  outline: none;
  font-size: 18px;
}

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

.field-input .unit {
  color: var(--subtitle);
  font-weight: 600;
}

.field-hint {
  font-size: 13px;
  color: var(--subtitle);
}

.field-hint.is-danger {
  color: var(--danger);
}

.field-hint.is-warn {
  color: var(--warn);
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 999px;
  background: var(--bg-2);
  color: var(--subtitle);
  font-weight: 600;
  font-size: 14px;
  align-self: flex-start;
}

.toggle.is-on {
  background: var(--warn-soft);
  color: var(--warn);
}

#live-suggest {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.count-input {
  flex: none;
  width: 128px;
  padding: 0 10px;
}

.count-input input {
  font-size: 17px;
  text-align: right;
}

.suggest {
  list-style: none;
  margin: 0;
  padding: 0;
  border-radius: var(--radius-sm);
  background: var(--bg-2);
  overflow: hidden;
}

.suggest button {
  width: 100%;
  min-height: 48px;
  padding: 6px 12px;
  border: 0;
  border-top: 1px solid var(--separator);
  background: none;
  text-align: left;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.suggest li:first-child button {
  border-top: 0;
}

.suggest small {
  color: var(--subtitle);
  font-size: 12px;
}

.picked {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: var(--tap);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
}

.picked strong {
  overflow-wrap: anywhere;
}

.issues {
  margin: 0;
  padding: 8px 14px 12px 32px;
  color: var(--danger);
  font-size: 14px;
}

.banner {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--accent-soft);
  color: var(--text);
}

.banner.is-warn {
  background: var(--warn-soft);
}

.banner.is-danger {
  background: var(--danger-soft);
}

.banner strong {
  display: block;
  margin-bottom: 2px;
}

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

.total {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--separator);
  font-weight: 600;
}

.total span:last-child {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.audit {
  list-style: none;
  margin: 0;
  padding: 0 14px 12px;
  font-size: 13px;
  color: var(--subtitle);
}

.audit li {
  padding: 6px 0;
  border-top: 1px solid var(--separator);
}

.audit li:first-child {
  border-top: 0;
}

.preview {
  margin: 0;
  padding: 10px 14px 12px;
  border-top: 1px solid var(--separator);
  font-size: 14px;
}

.preview b {
  font-variant-numeric: tabular-nums;
}

.share-text {
  margin: 0;
  padding: 12px 14px;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 13px;
  white-space: pre-wrap;
  color: var(--text);
}

@media (prefers-reduced-motion: no-preference) {
  .toast {
    animation: toast-in 160ms ease-out;
  }
  @keyframes toast-in {
    from {
      opacity: 0;
      transform: translate(-50%, 6px);
    }
  }
}
