/* Story Radar — панель владельца (Telegram Mini App). Режим «операция»: быстрый взгляд и разбор.
   Мир: карточка выдачи со штампами. Тема берётся из Telegram (светлая/тёмная); данные — в проверенной палитре dataviz. */

:root {
  color-scheme: light dark;
  --bg: #fcfcfb;
  --bg-2: #f1f0ec;          /* дорожки, пустые ячейки, поля ввода */
  --seg-on: #ffffff;        /* выбранный пункт сегмента */
  --text: #0b0b0b;
  --text-2: #52514e;
  --hint: #6b6a64;          /* подписи: ≥ 4.5:1 на фоне */
  --line: #e1e0d9;
  --axis: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.10);
  --ink: #4a3d99;           /* «чернила штампа»: акцент интерфейса, не данных */
  --ink-bg: rgba(74, 61, 153, 0.08);
  --danger: #b42f2f;
  --s1: #2a78d6;            /* серия 1: синий */
  --s2: #eb6834;            /* серия 2: оранжевый */
  --o1: #86b6ef;            /* порядковая шкала интереса: слабый → высокий */
  --o2: #2a78d6;
  --o3: #104281;
  --h1: #cde2fb;            /* последовательная шкала тепловой карты */
  --h2: #9ec5f4;
  --h3: #5598e7;
  --h4: #256abf;
  --h5: #104281;
  --meter-track: #cde2fb;
  --shadow-seg: 0 1px 2px rgba(11, 11, 11, 0.14);
  --shadow-pop: 0 6px 20px rgba(11, 11, 11, 0.18);
  --backdrop: rgba(11, 11, 11, 0.45);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  --tabbar: 58px;
  --gutter: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    --bg: #1a1a19; --bg-2: #262624; --seg-on: #3a3a37; --text: #f0efec; --text-2: #c3c2b7; --hint: #9c9a92;
    --line: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10); --ink: #aca2f5; --ink-bg: rgba(172, 162, 245, 0.12);
    --danger: #ec7b7b; --s1: #3987e5; --s2: #d95926; --o1: #1c5cab; --o2: #3987e5; --o3: #9ec5f4;
    --h1: #184f95; --h2: #256abf; --h3: #3987e5; --h4: #6da7ec; --h5: #9ec5f4; --meter-track: #184f95;
    --shadow-seg: 0 1px 2px rgba(0, 0, 0, 0.5); --shadow-pop: 0 6px 20px rgba(0, 0, 0, 0.5); --backdrop: rgba(0, 0, 0, 0.6);
  }
}

:root[data-theme="dark"] {
  --bg: #1a1a19; --bg-2: #262624; --seg-on: #3a3a37; --text: #f0efec; --text-2: #c3c2b7; --hint: #9c9a92;
  --line: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10); --ink: #aca2f5; --ink-bg: rgba(172, 162, 245, 0.12);
  --danger: #ec7b7b; --s1: #3987e5; --s2: #d95926; --o1: #1c5cab; --o2: #3987e5; --o3: #9ec5f4;
  --h1: #184f95; --h2: #256abf; --h3: #3987e5; --h4: #6da7ec; --h5: #9ec5f4; --meter-track: #184f95;
  --shadow-seg: 0 1px 2px rgba(0, 0, 0, 0.5); --shadow-pop: 0 6px 20px rgba(0, 0, 0, 0.5); --backdrop: rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font: 15px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent; overscroll-behavior-y: contain; accent-color: var(--ink); caret-color: var(--ink);
}
::selection { background: var(--ink-bg); color: var(--text); }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.icon { flex: none; }
* { scrollbar-width: thin; scrollbar-color: var(--axis) transparent; }

/* ---------- каркас ---------- */
#app { max-width: 640px; margin: 0 auto; min-height: 100dvh; padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom, 0px) + 16px); }
main { padding: 0 var(--gutter); transition: opacity 0.15s linear; }
main[aria-busy="true"] { opacity: 0.55; }

.filters {
  position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center;
  padding: 10px var(--gutter); background: var(--bg); border-bottom: 1px solid var(--line);
}

.seg-ctl { display: inline-flex; padding: 2px; border-radius: 10px; background: var(--bg-2); flex: 1 1 auto; min-width: 0; }
.seg-ctl label { flex: 1 1 0; position: relative; }
.seg-ctl input { position: absolute; inset: -4px 0; width: 100%; height: calc(100% + 8px); opacity: 0; margin: 0; cursor: pointer; }
.seg-ctl span {
  display: grid; place-items: center; min-height: 36px; padding: 0 10px; border-radius: 8px; color: var(--text-2);
  font-weight: 500; white-space: nowrap; transition: background 0.15s, color 0.15s;
}
.seg-ctl input:checked + span { background: var(--seg-on); color: var(--text); font-weight: 600; box-shadow: var(--shadow-seg), 0 0 0 1px var(--ring); }
.seg-ctl input:focus-visible + span { outline: 2px solid var(--ink); outline-offset: 1px; }

.switch { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; color: var(--text-2); font-weight: 500; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { position: relative; width: 38px; height: 22px; border-radius: 11px; background: var(--bg-2); box-shadow: inset 0 0 0 1px var(--ring); transition: background 0.15s; }
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--hint);
  transition: transform 0.2s var(--ease), background 0.15s;
}
.switch input:checked + .track { background: var(--ink); box-shadow: none; }
.switch input:checked + .track::after { transform: translateX(16px); background: var(--bg); }
.switch input:focus-visible + .track { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ---------- заголовки и текст ---------- */
.h2 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: 0; }
.section { margin-top: 28px; }
.section > .h2, .section-head { margin-bottom: 8px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.muted { color: var(--hint); }
.insight { margin: 16px 0 0; color: var(--text-2); max-width: 65ch; }
.note { margin: 8px 0 0; color: var(--hint); font-size: 13px; max-width: 65ch; }
.link-btn { position: relative; padding: 6px 0; border: 0; background: none; color: var(--ink); font-weight: 500; font-size: 13px; min-height: 32px; }
.link-btn::after { content: ""; position: absolute; inset: -8px -8px; }   /* зона касания ≥ 44 px без изменения вида */
.link-btn:hover { text-decoration: underline; text-underline-offset: 3px; }

/* ---------- итоговая строка ---------- */
.totals { display: grid; grid-template-columns: repeat(4, 1fr); margin-top: 16px; }
.totals > div { padding: 2px 0 2px 12px; border-left: 1px solid var(--line); min-width: 0; }
.totals > div:first-child { padding-left: 0; border-left: 0; }
.totals b { display: block; font-size: 20px; font-weight: 600; line-height: 1.2; }
.totals span { display: block; color: var(--hint); font-size: 12px; }
.coverage-line { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; margin-top: 10px; color: var(--hint); font-size: 13px; }

/* ---------- строка зрителя ---------- */
.rows { list-style: none; margin: 0; padding: 0; }
.row {
  display: grid; grid-template-columns: 48px 1fr auto; gap: 12px; align-items: center; width: 100%; min-height: 66px;
  padding: 9px 0; border: 0; border-bottom: 1px solid var(--line); background: none; text-align: left;
}
.row:hover { background: color-mix(in srgb, var(--bg-2) 55%, transparent); }
.row:active { background: var(--bg-2); }
.row-main { min-width: 0; }
.row-name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; color: var(--text-2); font-size: 13px; overflow-wrap: anywhere; }
.row-lvl { display: grid; justify-items: end; gap: 3px; min-width: 66px; font-size: 12px; color: var(--text-2); }

/* штамп с датой: фирменный приём */
.stamp {
  display: grid; grid-template-rows: auto 1fr; width: 46px; height: 50px; border: 1.5px solid var(--ink); border-radius: 6px;
  color: var(--ink); text-align: center; overflow: hidden; background: var(--ink-bg);
}
.stamp small { padding: 2px 0 1px; border-bottom: 1px solid var(--ink); font-size: 10px; line-height: 1.2; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.stamp b { display: grid; place-items: center; font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* уровень интереса: три ступеньки (форма) + слово */
.lvl { display: inline-flex; align-items: flex-end; gap: 2px; height: 14px; }
.lvl i { width: 4px; border-radius: 2px 2px 0 0; background: var(--line); }
.lvl i:nth-child(1) { height: 6px; } .lvl i:nth-child(2) { height: 10px; } .lvl i:nth-child(3) { height: 14px; }
.lvl-1 i:nth-child(-n+1) { background: var(--o1); }
.lvl-2 i:nth-child(-n+2) { background: var(--o2); }
.lvl-3 i { background: var(--o3); }

/* ---------- графики ---------- */
.chart { margin: 0; }
.chart-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.chart-note { margin: 6px 0 0; color: var(--hint); font-size: 12px; }
.fig-plot, .fig-table { min-width: 0; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 0 0 6px; padding: 0; list-style: none; color: var(--text-2); font-size: 13px; }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.plot { position: relative; border-radius: 8px; outline-offset: 4px; touch-action: pan-y; }
.plot-svg { display: block; width: 100%; height: auto; overflow: visible; }
.tick { fill: var(--hint); font-size: 11px; font-family: var(--font); }
.grid-line { stroke: var(--line); stroke-width: 1; shape-rendering: crispEdges; }
.axis-line { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.hover-band { fill: var(--bg-2); opacity: 0.7; }
.s1 { fill: var(--s1); background: var(--s1); }
.s2 { fill: var(--s2); background: var(--s2); }
.o1 { background: var(--o1); } .o2 { background: var(--o2); } .o3 { background: var(--o3); }
.hm-0 { fill: var(--bg-2); background: var(--bg-2); }
.hm-1 { fill: var(--h1); background: var(--h1); } .hm-2 { fill: var(--h2); background: var(--h2); }
.hm-3 { fill: var(--h3); background: var(--h3); } .hm-4 { fill: var(--h4); background: var(--h4); } .hm-5 { fill: var(--h5); background: var(--h5); }
.hm-on { stroke: var(--text); stroke-width: 1.5; }
.scale { display: flex; align-items: center; gap: 4px; margin-top: 8px; color: var(--hint); font-size: 12px; }
.scale .swatch { width: 18px; height: 10px; }

.seg { display: flex; gap: 2px; height: 8px; margin: 8px 0 6px; }
.seg-part { min-width: 6px; border-radius: 4px; }
.seg + .legend { margin-top: 6px; }
.legend .count { color: var(--hint); font-variant-numeric: tabular-nums; }

.meter { height: 8px; border-radius: 4px; background: var(--meter-track); overflow: hidden; margin: 8px 0 6px; }
.meter-fill { display: block; height: 100%; border-radius: 4px; background: var(--s1); }

.table-wrap { overflow-x: auto; border-top: 1px solid var(--line); }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 8px 6px; text-align: left; border-bottom: 1px solid var(--line); font-weight: 400; white-space: nowrap; }
.table thead th { color: var(--hint); font-weight: 500; }
.table tbody th { font-weight: 500; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

.tip {
  position: fixed; z-index: 30; min-width: 140px; max-width: 260px; padding: 8px 10px; border-radius: 10px; pointer-events: none;
  background: var(--bg); box-shadow: var(--shadow-pop), 0 0 0 1px var(--ring); font-size: 13px;
}
.tip-title { color: var(--hint); font-size: 12px; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; }
.tip-key { width: 12px; height: 3px; border-radius: 2px; flex: none; }
.tip-name { flex: 1; color: var(--text-2); }
.tip-value { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- фильтры списка ---------- */
.search { position: relative; margin-top: 14px; }
.search .icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--hint); pointer-events: none; }
.search input {
  width: 100%; min-height: 44px; padding: 0 12px 0 38px; border: 0; border-radius: 12px; background: var(--bg-2); color: var(--text);
  font-size: 16px;   /* меньше 16 px — iOS увеличивает страницу при фокусе */
}
.search input::placeholder { color: var(--hint); opacity: 1; }
.tools { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: center; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 14px; border: 0; border-radius: 22px;
  background: var(--bg-2); color: var(--text-2); font-weight: 500; font-size: 14px;
}
.chip[aria-pressed="true"] { background: var(--ink-bg); color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.chip .icon { width: 14px; height: 14px; }
.select {
  min-height: 44px; padding: 0 10px; border: 0; border-radius: 12px; background: var(--bg-2); color: var(--text); font-size: 16px;
}
.list-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 12px; margin-top: 6px; }
.count-line { flex: 1 1 auto; min-width: 0; color: var(--hint); font-size: 13px; }
.more { display: block; width: 100%; margin-top: 12px; min-height: 44px; border: 0; border-radius: 12px; background: var(--bg-2); color: var(--ink); font-weight: 600; }

/* ---------- истории ---------- */
.story { display: grid; grid-template-columns: 48px 1fr; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.story-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.story-top b { font-weight: 600; }
.story-top span, .story-sub { color: var(--hint); font-size: 13px; }
.bar { height: 8px; margin: 6px 0 4px; }
.bar span { display: block; height: 100%; min-width: 3px; border-radius: 0 4px 4px 0; background: var(--s1); }

/* ---------- нижние вкладки ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; justify-content: center;
  padding-bottom: env(safe-area-inset-bottom, 0px); background: var(--bg); border-top: 1px solid var(--line);
}
.tabbar-in { display: flex; width: 100%; max-width: 640px; }
.tab {
  flex: 1; display: grid; justify-items: center; align-content: center; gap: 2px; min-height: var(--tabbar); border: 0; background: none;
  color: var(--hint); font-size: 11px; font-weight: 500;
}
.tab[aria-current="page"] { color: var(--ink); font-weight: 600; }

/* ---------- лист снизу ---------- */
.sheet {
  width: 100%; max-width: 640px; max-height: 90dvh; margin: auto auto 0; padding: 0; border: 0; border-radius: 16px 16px 0 0;
  background: var(--bg); color: var(--text); box-shadow: 0 0 0 1px var(--ring); transform: translateY(100%);
  transition: transform 0.28s var(--ease); overflow: hidden;
}
.sheet.open { transform: none; }
.sheet::backdrop { background: var(--backdrop); opacity: 0; transition: opacity 0.2s linear; }
.sheet.open::backdrop { opacity: 1; }
.sheet-in { display: flex; flex-direction: column; max-height: 90dvh; }
.grab { align-self: center; width: 36px; height: 4px; margin: 8px 0 0; border-radius: 2px; background: var(--axis); }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 8px var(--gutter) 10px; }
.sheet-head h2 { margin: 0; font-size: 18px; font-weight: 600; overflow-wrap: anywhere; }
.sheet-head p { margin: 2px 0 0; color: var(--hint); font-size: 13px; }
.icon-btn { display: grid; place-items: center; flex: none; width: 44px; height: 44px; border: 0; border-radius: 20px; background: var(--bg-2); color: var(--text-2); }
.sheet-body { overflow-y: auto; padding: 0 var(--gutter) calc(20px + env(safe-area-inset-bottom, 0px)); overscroll-behavior: contain; }

.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px 0; margin: 14px 0 0; }
.facts > div { padding-left: 12px; border-left: 1px solid var(--line); min-width: 0; }
.facts > div:nth-child(3n+1) { padding-left: 0; border-left: 0; }
.facts dt { color: var(--hint); font-size: 12px; }
.facts dd { margin: 0; font-weight: 600; }
.why { margin: 6px 0 0; padding: 0; list-style: none; display: grid; gap: 4px; color: var(--text-2); }
.why li { display: flex; gap: 8px; }
.why li::before { content: ""; flex: none; width: 5px; height: 5px; margin-top: 8px; border-radius: 50%; background: var(--ink); }
.level-line { display: flex; align-items: center; gap: 8px; margin-top: 14px; font-weight: 600; }
.stamps { list-style: none; margin: 0; padding: 0; }
.stamp-item { display: grid; grid-template-columns: 48px 1fr auto; gap: 12px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.stamp-item .stamp { width: 44px; height: 48px; }
.stamp-item p { margin: 0; }
.stamp-item .sub { color: var(--hint); font-size: 13px; }
.tags { display: grid; justify-items: end; gap: 4px; }
.tag { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 10px; background: var(--bg-2); color: var(--text-2); font-size: 12px; }
.tag .icon { width: 12px; height: 12px; }
.dl dt { margin-top: 14px; font-weight: 600; }
.dl dd { margin: 2px 0 0; color: var(--text-2); max-width: 65ch; }
.btn-row { display: flex; gap: 8px; margin-top: 14px; }
.btn { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 14px; border: 0; border-radius: 12px; background: var(--bg-2); color: var(--ink); font-weight: 600; }

/* ---------- действия ---------- */
.actions { list-style: none; margin: 0; padding: 0; }
.action { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.action-main { min-width: 0; }
.action-main b { display: block; font-weight: 600; }
.action-sub { display: block; color: var(--text-2); font-size: 13px; }
.action .meter { margin: 8px 0 0; }
.btn.primary { background: var(--ink); color: var(--bg); }
.btn:disabled { opacity: 0.45; cursor: default; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom, 0px) + 12px); z-index: 40; transform: translateX(-50%);
  width: max-content; max-width: min(92vw, 420px); padding: 10px 14px; border-radius: 12px; background: var(--bg); color: var(--text);
  box-shadow: var(--shadow-pop), 0 0 0 1px var(--ring); font-size: 14px;
}
.toast.bad { color: var(--danger); }

/* ---------- состояния ---------- */
.notice { display: grid; justify-items: center; gap: 10px; margin: 56px auto 0; max-width: 34ch; text-align: center; color: var(--text-2); }
.notice h1 { margin: 0; font-size: 18px; color: var(--text); }
.notice p { margin: 0; }
.notice.err h1 { color: var(--danger); }
.empty { padding: 20px 0; color: var(--hint); }

/* единственный выразительный момент: штампы «ставятся» при открытии карточки */
@keyframes press { from { opacity: 0; transform: scale(1.22) rotate(-5deg); } to { opacity: 1; transform: none; } }
.sheet.open .stamp-item:nth-child(-n+8) .stamp { animation: press 0.34s var(--ease) both; animation-delay: calc(var(--i, 0) * 32ms + 120ms); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  .sheet { transform: none; }
}

@media (min-width: 560px) { .sheet { margin-bottom: 0; } }
@media (max-width: 400px) { .switch .only { display: none; } .seg-ctl span { padding: 0 8px; } }
@media (max-width: 340px) { .totals b { font-size: 17px; } .row { grid-template-columns: 44px 1fr auto; gap: 8px; } .seg-ctl span { padding: 0 5px; font-size: 14px; } }
@media (forced-colors: active) {
  .stamp, .chip[aria-pressed="true"], .seg-ctl input:checked + span { border: 1px solid CanvasText; }
}
