/*
 * Цвета интерфейса — из темы Telegram (--tg-theme-*), запасные значения — для обычного браузера
 * (светлые и тёмные по prefers-color-scheme). Цвета данных — проверенные шаги палитры
 * (dataviz: акцент #2a78d6 / #3987e5, серый контекст), отдельно для светлой и тёмной темы;
 * в Telegram тему выбирает клиент через data-theme на <html>.
 */
:root {
  color-scheme: light;
  --fb-page: #f1f1ef;
  --fb-card: #ffffff;
  --fb-text: #0b0b0b;
  --fb-hint: #6f6e69;
  --fb-link: #2a78d6;
  --fb-button: #2a78d6;
  --fb-button-text: #ffffff;

  --series: #2a78d6;
  --context: #94938d;
  --up: #006300;
  --down: #c4302b;
  --alert-wash: rgba(250, 178, 25, 0.2);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --fb-page: #0d0d0d;
    --fb-card: #1a1a19;
    --fb-text: #ffffff;
    --fb-hint: #a3a29b;
    --fb-link: #5598e7;
    --fb-button: #3987e5;
    --fb-button-text: #ffffff;

    --series: #3987e5;
    --context: #78776f;
    --up: #3cc13c;
    --down: #ff7a7a;
    --alert-wash: rgba(250, 178, 25, 0.16);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --fb-page: #0d0d0d;
  --fb-card: #1a1a19;
  --fb-text: #ffffff;
  --fb-hint: #a3a29b;
  --fb-link: #5598e7;
  --fb-button: #3987e5;
  --fb-button-text: #ffffff;

  --series: #3987e5;
  --context: #78776f;
  --up: #3cc13c;
  --down: #ff7a7a;
  --alert-wash: rgba(250, 178, 25, 0.16);
}

:root {
  --page: var(--tg-theme-secondary-bg-color, var(--fb-page));
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-card)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --button: var(--tg-theme-button-color, var(--fb-button));
  --button-text: var(--tg-theme-button-text-color, var(--fb-button-text));
  --bar-bg: var(--tg-theme-bottom-bar-bg-color, var(--card));
  --line: rgba(128, 128, 128, 0.22);
  --soft: rgba(128, 128, 128, 0.14);
  --radius: 14px;
  --gutter: 12px;
  --tabs-h: 58px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* --- основа ------------------------------------------------------------------------------- */

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--page);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

button {
  font: inherit;
  color: inherit;
}

h1,
h2,
p {
  margin: 0;
}

/* --- шапка, плашка, вкладки --------------------------------------------------------------- */

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px var(--gutter) 8px;
  background: var(--page);
}

.top-text {
  flex: 1;
  min-width: 0;
}

.top-title {
  font-size: 17px;
  font-weight: 600;
}

.top-stamp {
  min-height: 18px;
  color: var(--hint);
  font-size: 13px;
}

.refresh {
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: var(--card);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.refresh:disabled {
  cursor: default;
}

.refresh-icon {
  display: inline-block;
}

.refresh.is-busy .refresh-icon {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.banner {
  position: sticky;
  top: 62px;
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 var(--gutter) 10px;
  padding: 8px 8px 8px 12px;
  border-radius: 12px;
  background: var(--card);
  background-image: linear-gradient(var(--alert-wash), var(--alert-wash));
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}

.banner-text {
  flex: 1;
  min-width: 0;
  font-weight: 600;
}

.banner-btn {
  border: 0;
  border-radius: 9px;
  padding: 6px 12px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  cursor: pointer;
}

.banner-close {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--hint);
  cursor: pointer;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 2px var(--gutter) calc(var(--tabs-h) + var(--safe-bottom) + 20px);
  transition: opacity 0.2s;
}

.app.is-stale {
  opacity: 0.6;
}

.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  display: flex;
  height: calc(var(--tabs-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bar-bg);
  border-top: 1px solid var(--line);
}

.tabs button {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 0 2px;
  border: 0;
  background: transparent;
  color: var(--hint);
  font-size: 11px;
  cursor: pointer;
}

.tabs button > span:last-child {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 340px) {
  .tabs button {
    font-size: 10px;
  }
}

.tabs-icon {
  font-size: 20px;
  line-height: 1.1;
  filter: grayscale(1);
  opacity: 0.7;
}

.tabs button[aria-selected="true"] {
  color: var(--link);
  font-weight: 600;
}

.tabs button[aria-selected="true"] .tabs-icon {
  filter: none;
  opacity: 1;
}

.toast {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabs-h) + var(--safe-bottom) + 14px);
  z-index: 7;
  width: -webkit-fit-content;
  width: fit-content;
  max-width: calc(100% - 2 * var(--gutter));
  margin: 0 auto;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(20, 20, 20, 0.88);
  color: #fff;
  font-size: 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
}

.toast.is-on {
  opacity: 1;
  transform: none;
}

/* --- экраны загрузки, ошибки, доступа ----------------------------------------------------- */

.screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 80px);
  padding: 24px var(--gutter);
}

.screen-box {
  max-width: 340px;
  text-align: center;
}

.screen-icon {
  font-size: 44px;
  margin-bottom: 8px;
}

.screen-title {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 8px;
}

.screen-text {
  font-size: 17px;
  margin-bottom: 8px;
}

.screen-hint {
  color: var(--hint);
  margin-bottom: 18px;
}

.user-id {
  -webkit-user-select: all;
  user-select: all;
  font-variant-numeric: tabular-nums;
}

.spinner {
  width: 28px;
  height: 28px;
  margin: 0 auto 12px;
  border: 3px solid var(--soft);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.btn {
  border: 0;
  border-radius: 12px;
  padding: 11px 22px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  cursor: pointer;
}

.link-btn {
  flex: none;
  border: 0;
  padding: 4px 0;
  background: transparent;
  color: var(--link);
  font-size: 14px;
  cursor: pointer;
}

/* --- карточки и заголовки ----------------------------------------------------------------- */

.section-head {
  margin: 6px 4px 10px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
}

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

.note {
  margin: -2px 4px 12px;
}

.card {
  min-width: 0;
  margin-bottom: 12px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--card);
}

.card-head {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 10px;
}

.card-head-text {
  flex: 1;
  min-width: 0;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
}

.card > .card-title + .card-sub,
.card > .card-title {
  margin-bottom: 8px;
}

.empty {
  color: var(--hint);
  text-align: center;
  padding: 18px 0;
}

/* --- плитки ------------------------------------------------------------------------------- */

.tiles {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.tile {
  min-width: 0;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--card);
}

.tile.is-hero {
  grid-column: 1 / -1;
}

.tile-label {
  color: var(--hint);
  font-size: 13px;
}

.tile-value {
  margin: 2px 0 6px;
  font-size: 21px;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tile.is-hero .tile-value {
  font-size: 40px;
  line-height: 1.1;
}

.tile.is-muted .tile-value,
.tile.is-pending .tile-value {
  color: var(--hint);
  font-size: 16px;
  font-weight: 500;
  padding: 3px 0 2px;
}

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

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

.delta-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 6px;
  font-size: 13px;
}

.tile.is-hero .delta-row {
  justify-content: flex-start;
  gap: 10px;
}

.tile.is-hero .delta-label {
  min-width: 52px;
}

.delta-label {
  color: var(--hint);
}

.delta {
  font-weight: 600;
  white-space: nowrap;
}

.delta.up {
  color: var(--up);
}

.delta.down {
  color: var(--down);
}

.delta.flat {
  color: var(--hint);
}

.badge {
  flex: none;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

/* --- графики ------------------------------------------------------------------------------ */

.chart {
  position: relative;
  -webkit-user-select: none;
  user-select: none;
  touch-action: pan-y;
}

.chart svg {
  display: block;
  max-width: 100%;
  overflow: visible;
}

.chart svg:focus {
  outline: none;
}

.chart svg:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 4px;
  border-radius: 4px;
}

.chart-name {
  fill: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.chart-head-value {
  fill: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.chart-head-date {
  fill: var(--hint);
  font-weight: 400;
}

.chart-grid {
  stroke: var(--hint);
  stroke-opacity: 0.2;
  stroke-width: 1;
}

.chart-base {
  stroke: var(--hint);
  stroke-opacity: 0.45;
  stroke-width: 1;
}

.chart-tick {
  fill: var(--hint);
  font-size: 10.5px;
  font-variant-numeric: tabular-nums;
  paint-order: stroke;
  stroke: var(--card);
  stroke-width: 3px;
  stroke-linejoin: round;
}

.chart-empty {
  fill: var(--hint);
  font-size: 13px;
}

.chart-line-path {
  fill: none;
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.chart-area {
  fill-opacity: 0.1;
  stroke: none;
}

.chart-dot {
  stroke: var(--card);
  stroke-width: 2;
}

.chart-cross {
  stroke: var(--text);
  stroke-opacity: 0.35;
  stroke-width: 1;
}

.chart-bar-main {
  fill: var(--series);
}

.chart-bar-context {
  fill: var(--context);
}

.chart-bar-main.is-hot,
.chart-bar-context.is-hot {
  fill-opacity: 0.7;
}

.chart-bar-label {
  fill: var(--text);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.chart-tip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  padding: 7px 10px;
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18), 0 0 0 1px var(--line);
  font-size: 13px;
  white-space: nowrap;
  pointer-events: none;
}

.chart-tip-title {
  color: var(--hint);
  font-size: 12px;
  margin-bottom: 2px;
}

.chart-tip-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.chart-tip-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.chart-tip-name {
  color: var(--hint);
  font-size: 12px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin: 0 4px 10px;
  color: var(--hint);
  font-size: 13px;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 12px;
  height: 8px;
  border-radius: 0 3px 3px 0;
}

.swatch.is-main {
  background: var(--series);
}

.swatch.is-context {
  background: var(--context);
}

/* --- таблицы ------------------------------------------------------------------------------ */

.table-scroll {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.data-table,
.plan-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.data-table th,
.plan-table th {
  padding: 4px 4px 6px;
  color: var(--hint);
  font-size: 12px;
  font-weight: 500;
  text-align: left;
  white-space: nowrap;
}

.data-table td,
.plan-table td {
  padding: 8px 4px;
  border-top: 1px solid var(--line);
  vertical-align: top;
}

.num {
  text-align: right !important;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.data-table td:first-child,
.data-table th:first-child,
.plan-table td:first-child,
.plan-table th:first-child {
  padding-left: 0;
}

.data-table td:last-child,
.data-table th:last-child,
.plan-table td:last-child,
.plan-table th:last-child {
  padding-right: 0;
}

/* --- вкладка «Производство» --------------------------------------------------------------- */

.hero {
  margin-bottom: 14px;
}

.hero-label {
  color: var(--hint);
  font-size: 13px;
}

.hero-value {
  font-size: 40px;
  font-weight: 600;
  line-height: 1.15;
}

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

.control {
  margin-bottom: 12px;
}

.control-label {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  color: var(--hint);
  font-size: 13px;
}

.range-value {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.control-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.seg {
  display: flex;
  gap: 2px;
  padding: 2px;
  border-radius: 11px;
  background: var(--soft);
}

.seg button {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 6px 4px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.seg button[aria-pressed="true"] {
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
}

input[type="range"] {
  display: block;
  width: 100%;
  height: 32px;
  margin: 0;
  accent-color: var(--button);
  cursor: pointer;
}

.card-table {
  padding-top: 10px;
}

.plan-table .row {
  cursor: pointer;
}

.name-btn {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.name-btn:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 2px;
  border-radius: 4px;
}

.plan-table .name {
  position: relative;
  width: 100%;
  min-width: 96px;
}

.plan-table .name-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.plan-table .row.is-open .name-text {
  display: block;
  -webkit-line-clamp: unset;
}

.plan-table td.name:first-child {
  padding-left: 14px;
}

.chev {
  position: absolute;
  left: 0;
  top: 13px;
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--hint);
  border-bottom: 1.5px solid var(--hint);
  transform: rotate(-45deg);
  transition: transform 0.15s;
}

.row.is-open .chev {
  transform: rotate(45deg);
  top: 11px;
}

.plan-table .plan {
  font-weight: 600;
}

.plan-table tfoot td {
  border-top: 1.5px solid var(--line);
  font-weight: 600;
}

.plan-table .detail td {
  border-top: 0;
  padding: 2px 0 12px;
}

.detail-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.detail-meta {
  min-width: 0;
  color: var(--hint);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.detail-meta b {
  color: var(--text);
}

.mono {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
}

.plan-table .detail .legend {
  margin: 6px 0 0;
  font-size: 12px;
}

.table-note {
  margin-top: 8px;
}

.day-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 4px;
  margin: 4px 0;
}

.day-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 4px 0;
  border-radius: 6px;
  background: var(--soft);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.day-cell.is-recent {
  box-shadow: inset 0 -2px 0 var(--series);
}

.day-date {
  color: var(--hint);
  font-size: 10px;
  white-space: nowrap;
}

/* --- воронка, отмены ---------------------------------------------------------------------- */

.funnel-stage,
.funnel-week {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.funnel-stage {
  padding: 4px 0;
}

.funnel-value {
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
}

.funnel-step {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 8px;
  margin: 2px 0;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--soft);
  font-size: 13px;
}

.funnel-arrow {
  color: var(--hint);
}

.funnel-conv {
  font-weight: 600;
}

.funnel-weeks {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.funnel-weeks .card-sub {
  margin-bottom: 4px;
}

.funnel-week {
  padding: 3px 0;
  font-size: 14px;
}

.funnel-week .delta {
  font-size: 13px;
}

.kv {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 6px 12px;
  margin: 0;
}

.kv dt {
  color: var(--hint);
}

.kv dd {
  margin: 0;
  text-align: right;
  font-weight: 600;
}

/* --- тренды и сборка ---------------------------------------------------------------------- */

.trend {
  padding: 10px 0;
  border-top: 1px solid var(--line);
}

.card-title + .trend {
  border-top: 0;
  padding-top: 2px;
}

.trend-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}

.trend-name {
  min-width: 0;
  overflow-wrap: anywhere;
}

.pack-list {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: pack;
}

.pack-list li {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
  counter-increment: pack;
}

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

.pack-list li::before {
  content: counter(pack);
  flex: none;
  min-width: 18px;
  color: var(--hint);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.pack-name {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.pack-units {
  flex: none;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.more-btn {
  margin-top: 4px;
}

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