/* Mini App «СОТ» — стили по дизайн-системе МС (docs/company/style-system.md).
   Токены бренда — как в разделе 2; поверх них семантические переменные
   (--bg, --surface, --text…), которые тёмная тема переопределяет.
   Белый текст — только на --ms-green-700. Анимация — только transform/opacity. */

:root {
  --ms-green: #78BE20;  --ms-green-600: #5F9A19;  --ms-green-050: #EEF6E2;
  --ms-orange: #EA7600; --ms-orange-050: #FDEDDD;
  --ms-ink: #3D3935;    --ms-gray: #75787B;  --ms-gray-light: #E5E5E5;
  --ms-bg: #F7F7F8;     --ms-white: #FFFFFF;  --ms-error: #C53D3D;
  --ms-green-700: #4A7A12; --ms-orange-700: #A35200; --ms-gray-600: #6B6E71;
  --ms-radius: 8px;
  --ms-radius-sm: 4px;
  --ms-space: 8px;
  --ms-focus: 0 0 0 2px var(--surface), 0 0 0 4px var(--focus-ring);
  --ms-ease: cubic-bezier(0.23, 1, 0.32, 1);
  --ms-dur: 150ms;
  --ms-z-sticky: 10; --ms-z-dropdown: 20; --ms-z-modal: 30; --ms-z-toast: 40;
  --ms-font: "Gotham Pro", "Montserrat", -apple-system, "Segoe UI", Roboto, Arial, sans-serif;

  /* Семантика — светлая тема */
  --bg: var(--ms-bg);
  --surface: var(--ms-white);
  --line: var(--ms-gray-light);
  --text: var(--ms-ink);
  --text-2: var(--ms-gray-600);
  --accent-text: var(--ms-green-700);
  --accent-soft: var(--ms-green-050);
  --warn-text: var(--ms-orange-700);
  --warn-soft: var(--ms-orange-050);
  --error-line: var(--ms-error);
  --error-text: var(--ms-error);
  --track: var(--ms-gray-light);
  --skeleton: var(--ms-gray-light);
  --focus-ring: var(--ms-green-700);
  --backdrop: rgba(61, 57, 53, 0.45);
  --shadow-pop: 0 -8px 24px rgba(61, 57, 53, 0.18);
  --tabbar-h: 64px;
}

/* Тёмная тема — по Telegram.WebApp.colorScheme. Фирменные зелёный/оранжевый
   остаются; нейтральные поверхности — затемнённые ступени --ms-ink. */
html[data-theme="dark"] {
  --bg: #1F1D1B;
  --surface: #2B2826;
  --line: var(--ms-ink);
  --text: var(--ms-bg);
  --text-2: #ABADAF;
  --accent-text: var(--ms-green);
  --accent-soft: #33402A;
  --warn-text: var(--ms-orange);
  --warn-soft: #33261A;
  --error-line: var(--ms-error);
  --error-text: var(--ms-bg);
  --track: var(--ms-ink);
  --skeleton: var(--ms-ink);
  --focus-ring: var(--ms-green);
  --backdrop: rgba(0, 0, 0, 0.6);
  --shadow-pop: 0 -8px 24px rgba(0, 0, 0, 0.5);
}

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

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

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--ms-font);
  font-size: 15px;
  line-height: 1.45;
  /* Все числа — табличные: цифры не «прыгают» при обновлении. */
  font-variant-numeric: tabular-nums;
}

button {
  font: inherit;
  color: inherit;
  font-variant-numeric: tabular-nums;
}

[hidden] { display: none !important; }

:focus-visible { outline: none; box-shadow: var(--ms-focus); }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --- Заглушка вне Telegram и фатальные экраны ------------------------------ */

.stub {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
}
.stub-mark { font-size: 40px; line-height: 1; }
.stub-text {
  margin: 0;
  max-width: 320px;
  font-size: 16px;
  color: var(--text);
  text-wrap: balance;
}

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

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
}

.topbar { padding: 16px 16px 0; }

.screen {
  padding: 16px;
  outline: none;
}
.screen:focus-visible { box-shadow: none; }

.whose {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 4px 0 0;
  border-radius: var(--ms-radius);
  background: var(--accent-soft);
}
.whose-name {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: var(--ms-z-sticky);
  display: flex;
  max-width: 480px;
  margin: 0 auto;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 44px;
  border: 0;
  background: none;
  font-size: 11px;
  color: var(--text-2);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.tab-ic { font-size: 20px; line-height: 1; filter: grayscale(1); opacity: 0.6; }
.tab[aria-current="page"] { color: var(--accent-text); font-weight: 700; }
.tab[aria-current="page"] .tab-ic { filter: none; opacity: 1; }
.tab:active { opacity: 0.7; }

/* --- Общие элементы ------------------------------------------------------- */

.h1 {
  margin: 0 0 4px;
  font-size: 20px;
  line-height: 1.2;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.sub {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--text-2);
}

.seg {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin-bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--ms-radius);
}
.mode.seg, .mode { margin-bottom: 0; }
.seg-btn {
  flex: 1;
  min-height: 40px;
  border: 0;
  border-radius: var(--ms-radius-sm);
  background: none;
  font-size: 14px;
  color: var(--text-2);
  cursor: pointer;
}
.seg-btn:hover { color: var(--text); }
.seg-btn[aria-pressed="true"] {
  background: var(--ms-green-700);
  color: var(--ms-white);
  font-weight: 600;
}

.chips {
  display: flex;
  gap: 8px;
  /* 4px сверху и снизу — чтобы кольцо фокуса чипа не обрезалось прокруткой */
  margin: -4px -16px 12px;
  padding: 4px 16px;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--ms-radius-sm);
  background: var(--surface);
  font-size: 14px;
  color: var(--text-2);
  cursor: pointer;
  scroll-snap-align: end;
  white-space: nowrap;
}
.chip:hover { border-color: var(--ms-green); color: var(--text); }
.chip[aria-pressed="true"] {
  background: var(--ms-green-700);
  border-color: var(--ms-green-700);
  color: var(--ms-white);
  font-weight: 600;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--ms-green-700);
  border-radius: var(--ms-radius);
  background: var(--ms-green-700);
  color: var(--ms-white);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity var(--ms-dur) var(--ms-ease);
}
.btn:hover { opacity: 0.9; }
.btn:active { opacity: 0.8; }
.btn-secondary {
  background: transparent;
  color: var(--accent-text);
  border-color: var(--accent-text);
}
.btn[disabled], .btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: default;
}

.link-btn {
  min-height: 40px;
  min-width: 40px;
  padding: 0 8px;
  border: 0;
  background: none;
  color: var(--accent-text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border-radius: var(--ms-radius-sm);
}
.link-btn:hover { text-decoration: underline; }

.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: var(--ms-radius);
  background: none;
  color: var(--text-2);
  font-size: 18px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--ms-radius);
  padding: 12px 16px;
  margin-bottom: 8px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-2);
}
.section-head .link-btn { text-transform: none; letter-spacing: 0; }
.section-title { margin: 0; font: inherit; }

.tag {
  display: inline-block;
  margin-left: 4px;
  padding: 0 6px;
  border-radius: var(--ms-radius-sm);
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 11px;
  font-weight: 600;
  line-height: 18px;
  vertical-align: 1px;
}
.tag-warn { background: var(--warn-soft); color: var(--warn-text); }

/* Сообщения: предупреждение (данные не свежие), заметка, ошибка */
.banner {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 8px 12px;
  margin-bottom: 8px;
  border-radius: var(--ms-radius);
  background: var(--warn-soft);
  color: var(--warn-text);
  font-size: 13px;
}
.banner-note { background: var(--accent-soft); color: var(--accent-text); }
.banner-ic { flex: none; }

.state {
  padding: 16px;
  margin-bottom: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--ms-radius);
  text-align: left;
}
.state-error { border-left: 4px solid var(--error-line); }
.state-title { margin: 0 0 4px; font-weight: 700; font-size: 15px; }
.state-error .state-title { color: var(--error-text); }
.state-text { margin: 0 0 12px; color: var(--text-2); font-size: 14px; }
.state-text:last-child { margin-bottom: 0; }

/* Скелетон по форме будущего контента */
.skel {
  display: block;
  border-radius: var(--ms-radius);
  background: var(--skeleton);
  animation: pulse 1.2s ease-in-out infinite alternate;
}
.skel-hero { height: 156px; margin-bottom: 16px; }
.skel-tile { height: 92px; }
.skel-row { height: 52px; margin-bottom: 4px; }
.skel-card { height: 96px; margin-bottom: 8px; }
.skel-podium { height: 160px; margin-bottom: 16px; }
.skel-spark { height: 80px; margin-bottom: 16px; }
.skel-chips { height: 40px; margin-bottom: 16px; }
.skel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 16px; }

@keyframes pulse { from { opacity: 1; } to { opacity: 0.45; } }
@keyframes appear { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes rise { from { transform: scaleY(0); } to { transform: none; } }
@keyframes sheet-in { from { transform: translateY(100%); } to { transform: none; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.appear { animation: appear 240ms var(--ms-ease) both; }

/* --- Главная: карточка суммы ---------------------------------------------- */

.hero {
  background: var(--ms-green-700);
  color: var(--ms-white);
  border-radius: var(--ms-radius);
  padding: 16px;
}
.hero-label {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.hero-sum {
  margin: 4px 0 2px;
  font-size: 34px;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.hero-delta { margin: 0; font-size: 13px; }
.hero-bar {
  height: 6px;
  margin-top: 16px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.25);
  overflow: hidden;
}
.hero-legend {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
  font-size: 12px;
}
.hero-extra { margin: 4px 0 0; font-size: 12px; }

/* Полоса прогресса: заполнение — transform: scaleX(--p), без анимации ширины */
.fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--ms-green);
  transform: scaleX(var(--p, 0));
  transform-origin: left center;
}
.fill.anim { transition: transform 900ms var(--ms-ease); }
.hero .fill { background: var(--ms-white); }
.fill-warn { background: var(--ms-orange); }

.meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 8px 0 0;
  min-height: 44px;
}
.meta-text { margin: 0; font-size: 12px; color: var(--text-2); }
.meta .btn { min-height: 40px; padding: 0 12px; font-size: 14px; flex: none; }
.meta-msg { margin: 0 0 8px; font-size: 12px; color: var(--warn-text); }

/* --- Плитки «откуда деньги» ---------------------------------------------- */

.tiles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 16px 0 0;
}
.tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
  min-height: 92px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--ms-radius);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
}
.tile:hover { border-color: var(--ms-green); }
.tile:active { opacity: 0.85; }
.tile-ic { font-size: 18px; line-height: 1.2; }
.tile-title { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.tile-value { font-size: 17px; font-weight: 700; white-space: nowrap; }
.tile-sub {
  font-size: 12px;
  color: var(--text-2);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* --- Лента начислений и строки шторки ------------------------------------ */

.list {
  margin: 0;
  padding: 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--ms-radius);
  overflow: hidden;
}
.list > li + li { border-top: 1px solid var(--line); }
.op {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  padding: 8px 12px;
  border: 0;
  background: none;
  text-align: left;
}
button.op { cursor: pointer; }
/* Строки — во всю ширину списка с overflow: hidden: кольцо фокуса внутрь. */
.op:focus-visible { box-shadow: inset 0 0 0 2px var(--focus-ring); }
button.op:hover { background: var(--bg); }
.op-av {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: var(--ms-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  background: var(--accent-soft);
}
.op-av.warm { background: var(--warn-soft); }
.op-main { flex: 1; min-width: 0; }
.op-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.op-desc { font-size: 12px; color: var(--text-2); }
.op-amount {
  flex: none;
  font-size: 14px;
  font-weight: 700;
  color: var(--accent-text);
  text-align: right;
  white-space: nowrap;
}
.op-amount.neg { color: var(--text); }
.op-go { flex: none; color: var(--text-2); font-size: 16px; }

.empty-inline {
  padding: 16px;
  color: var(--text-2);
  font-size: 14px;
}

/* --- Шторка -------------------------------------------------------------- */

.sheet {
  position: fixed;
  inset: auto 0 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 85dvh;
  margin: 0 auto;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  overflow: visible;
  z-index: var(--ms-z-modal);
}
.sheet::backdrop { background: var(--backdrop); animation: fade-in 200ms var(--ms-ease); }
.sheet-panel {
  display: flex;
  flex-direction: column;
  max-height: 85dvh;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-radius: var(--ms-radius) var(--ms-radius) 0 0;
  box-shadow: var(--shadow-pop);
}
.sheet[open] .sheet-panel { animation: sheet-in 240ms var(--ms-ease); }
.sheet.closing .sheet-panel {
  transform: translateY(100%);
  transition: transform 200ms var(--ms-ease);
}
.sheet.closing::backdrop { opacity: 0; transition: opacity 200ms var(--ms-ease); }
.sheet-grab {
  width: 40px;
  height: 4px;
  margin: 0 auto 8px;
  border-radius: 2px;
  background: var(--line);
}
.sheet-head { display: flex; align-items: flex-start; gap: 8px; }
.sheet-title {
  flex: 1;
  margin: 8px 0 0;
  font-size: 17px;
  line-height: 1.3;
  font-weight: 700;
}
.sheet-sub { margin: 0 0 8px; font-size: 13px; color: var(--text-2); }
.sheet-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0 -16px;
  padding: 0 16px;
}
.sheet-body .list { border-radius: var(--ms-radius); }

.kpi-row { padding: 12px; }
.kpi-row .op-desc { margin-top: 2px; }

/* --- Планы --------------------------------------------------------------- */

.plan-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.plan-name { font-size: 15px; font-weight: 600; }
.plan-pct { font-size: 20px; font-weight: 700; color: var(--accent-text); white-space: nowrap; }
.plan-pct.warm { color: var(--warn-text); }
.bar {
  height: 8px;
  margin: 8px 0 4px;
  border-radius: 4px;
  background: var(--track);
  overflow: hidden;
}
.plan-fact { margin: 0; font-size: 13px; }
.plan-hint { margin: 2px 0 0; font-size: 12px; color: var(--text-2); text-wrap: pretty; }

/* --- Рейтинг ------------------------------------------------------------- */

.podium {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 8px;
  height: 168px;
  margin: 0 0 16px;
}
.pod {
  width: 64px;
  height: calc(var(--h, 0.5) * 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding-top: 8px;
  border-radius: var(--ms-radius) var(--ms-radius) 0 0;
  background: var(--track);
  /* --text, а не --text-2: на сером треке вторичный текст не дотягивает до 4.5:1 */
  color: var(--text);
  font-size: 12px;
  transform-origin: bottom center;
}
.pod.rise { animation: rise 700ms var(--ms-ease) both; }
.pod-place { font-size: 12px; }
.pod-who { font-size: 16px; font-weight: 700; color: var(--text); }
.pod-score { font-size: 12px; }
.pod.me { background: var(--ms-green-700); color: var(--ms-white); }
.pod.me .pod-who { color: var(--ms-white); }

.me-card {
  padding: 12px 16px;
  margin-bottom: 8px;
  border: 1px solid var(--ms-green);
  border-radius: var(--ms-radius);
  background: var(--accent-soft);
}
.me-title { margin: 0; font-size: 16px; font-weight: 700; }
.me-text { margin: 4px 0 0; font-size: 14px; }
.low-card {
  padding: 12px 16px;
  margin-bottom: 8px;
  border: 1px dashed var(--ms-gray);
  border-radius: var(--ms-radius);
  background: var(--surface);
}
.kv { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; padding: 2px 0; }
.kv b { font-weight: 700; }
.small { margin: 4px 0 0; font-size: 12px; color: var(--text-2); }

.rank-row { display: flex; align-items: center; gap: 12px; padding: 8px 12px; min-height: 56px; }
.rank-row.me { box-shadow: inset 3px 0 0 var(--ms-green); }
.rank-place { flex: none; width: 24px; font-weight: 700; color: var(--text-2); text-align: right; }
.rank-score { flex: none; font-weight: 700; text-align: right; }

/* --- История ------------------------------------------------------------- */

.spark {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 80px;
  margin: 0 0 16px;
  padding: 0 4px;
  border-bottom: 1px solid var(--line);
}
.spark-bar {
  flex: 1;
  height: calc(var(--h, 0) * 100%);
  min-height: 2px;
  border-radius: 3px 3px 0 0;
  background: var(--ms-green);
  transform-origin: bottom center;
}
.spark-bar.rise { animation: rise 800ms var(--ms-ease) both; }
/* Текущий месяц «ещё идёт»: светлая заливка с пунктирной рамкой (без градиентов) */
.spark-bar.cur {
  background: var(--accent-soft);
  border: 1px dashed var(--accent-text);
  border-bottom: 0;
}
.spark-bar.none { background: var(--track); }

.hm-main { flex: 1; min-width: 0; }
.hm-name { font-size: 15px; font-weight: 600; }
.hm-value { flex: none; font-weight: 700; white-space: nowrap; }
.hm-value.live { color: var(--accent-text); }

/* --- Отдел --------------------------------------------------------------- */

.total-card {
  padding: 16px;
  margin-bottom: 16px;
  border-radius: var(--ms-radius);
  background: var(--ms-green-700);
  color: var(--ms-white);
}
.total-label { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.total-note { margin: 4px 0 0; font-size: 13px; }
.total-sum { margin: 4px 0 0; font-size: 28px; font-weight: 700; white-space: nowrap; }

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

@media (max-width: 340px) {
  .hero-sum { font-size: 28px; }
  .tile-value { font-size: 15px; }
}
