/* FB Stats — styles. Light and dark themes share one set of role tokens; the dark values are
 * declared twice on purpose: for the OS setting (media query) and for the explicit toggle
 * (data-theme), which must win in both directions. No external resources are loaded. */

:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f3f3f0;
  --surface-raised: #ffffff;
  --text: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6f6e69;
  --border: rgba(11, 11, 11, 0.11);
  --border-strong: #c3c2b7;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --hover: rgba(11, 11, 11, 0.05);
  --accent: #2a78d6;
  --accent-strong: #1c5cab;
  --accent-ink: #ffffff;
  --link: #1c5cab;
  /* ink on coloured fills (badges, danger buttons, switch knob, distribution bar, swatches) */
  --on-accent: #ffffff;
  --on-bad: #ffffff;
  --on-fill: #ffffff;
  --on-fill-light: #0b0b0b;
  --shadow: 0 8px 28px rgba(11, 11, 11, 0.14);

  /* status roles: fill colour + readable ink of the same family */
  --ok: #0ca30c;       --ok-ink: #006300;
  --warn: #fab219;     --warn-ink: #7a5200;
  --serious: #ec835a;  --serious-ink: #9c4118;
  --bad: #d03b3b;      --bad-ink: #b02a2a;
  --info: #2a78d6;     --info-ink: #1c5cab;
  --muted: #898781;    --muted-ink: #52514e;

  /* chart series (categorical slots, fixed order) */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;

  /* account status groups in the distribution bar */
  --st-active: #0ca30c;
  --st-disabled: #d03b3b;
  --st-unsettled: #ec835a;
  --st-grace: #fab219;
  --st-review: #2a78d6;
  --st-closed: #6f6e69;
  --st-other: #c3c2b7;

  --radius: 10px;
  --sidebar-w: 228px;

  /* Scale tokens (plan 2.1): type, radius, space, control and bar heights, safe areas. The
   * existing rules still carry their literal values; new rules use these. */
  --fs-xs: 11px; --fs-sm: 12px; --fs-md: 13px; --fs-base: 14px; --fs-lg: 16px; --fs-xl: 18px; --fs-kpi: 22px;
  --r-sm: 6px; --r-md: 8px; --r-lg: 10px; --r-pill: 999px;
  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 20px; --s-6: 24px;
  --control-h: 34px; --control-h-sm: 28px; --tap: 44px;
  --appbar-h: 48px; --tabbar-h: 56px;
  /* What a fixed bar at the bottom of the screen must clear: the phone tab bar (0 elsewhere). */
  --tabbar-space: 0px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --view-pad: 20px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #222220;
    --surface-raised: #262624;
    --text: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #9a988f;
    --border: rgba(255, 255, 255, 0.11);
    --border-strong: #4a4a46;
    --grid: #2c2c2a;
    --axis: #45453f;
    --hover: rgba(255, 255, 255, 0.07);
    --accent: #3987e5;
    --accent-strong: #5598e7;
    --accent-ink: #ffffff;
    --link: #6da7ec;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
    --ok: #0ca30c;       --ok-ink: #41c741;
    --warn: #fab219;     --warn-ink: #fab219;
    --serious: #ec835a;  --serious-ink: #f09a78;
    --bad: #d03b3b;      --bad-ink: #ef7d7d;
    --info: #3987e5;     --info-ink: #6da7ec;
    --muted: #898781;    --muted-ink: #c3c2b7;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --st-review: #3987e5;
    --st-closed: #6f6e69;
    --st-other: #898781;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #222220;
  --surface-raised: #262624;
  --text: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #9a988f;
  --border: rgba(255, 255, 255, 0.11);
  --border-strong: #4a4a46;
  --grid: #2c2c2a;
  --axis: #45453f;
  --hover: rgba(255, 255, 255, 0.07);
  --accent: #3987e5;
  --accent-strong: #5598e7;
  --accent-ink: #ffffff;
  --link: #6da7ec;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
  --ok: #0ca30c;       --ok-ink: #41c741;
  --warn: #fab219;     --warn-ink: #fab219;
  --serious: #ec835a;  --serious-ink: #f09a78;
  --bad: #d03b3b;      --bad-ink: #ef7d7d;
  --info: #3987e5;     --info-ink: #6da7ec;
  --muted: #898781;    --muted-ink: #c3c2b7;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --st-review: #3987e5;
  --st-closed: #6f6e69;
  --st-other: #898781;
}

/* ------------------------------------------------------------------ base */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
}
h1, h2, h3, p { margin: 0; }
a { color: var(--link); text-decoration: none; }
@media (hover: hover) { a:hover { text-decoration: underline; } }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
img { max-width: 100%; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.skip-link { position: absolute; left: -9999px; top: 8px; z-index: 100; padding: 8px 12px; background: var(--surface-raised); border-radius: 6px; }
.skip-link:focus { left: 8px; }
.noscript { padding: 24px; text-align: center; }

.muted { color: var(--text-muted); }
.mono { font-family: var(--mono); font-size: 12px; }
.icon {
  width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round; vertical-align: -3px;
}
.is-spinning { animation: spin 1.1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* tones: one fill colour and one ink per status role */
.tone-ok { --tone: var(--ok); --tone-ink: var(--ok-ink); }
.tone-warn { --tone: var(--warn); --tone-ink: var(--warn-ink); }
.tone-serious { --tone: var(--serious); --tone-ink: var(--serious-ink); }
.tone-bad { --tone: var(--bad); --tone-ink: var(--bad-ink); }
.tone-info { --tone: var(--info); --tone-ink: var(--info-ink); }
.tone-muted { --tone: var(--muted); --tone-ink: var(--muted-ink); }

/* ----------------------------------------------------------------- shell */

.app { display: flex; min-height: 100vh; min-height: 100dvh; }
.sidebar {
  position: sticky; top: 0; align-self: flex-start; flex: none; width: var(--sidebar-w); height: 100vh;
  display: flex; flex-direction: column; padding: 14px 10px; background: var(--surface);
  border-right: 1px solid var(--border); overflow-y: auto;
  height: 100dvh;            /* the phone's visible height; 100vh above is the fallback */
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; color: var(--text); font-weight: 700; font-size: 16px; }
@media (hover: hover) { .brand:hover { text-decoration: none; } }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 700; letter-spacing: 0.02em;
}
/* Three groups (Работа · Активы · Сервис) under small captions; «Сервис» sits at the bottom. */
.nav { display: flex; flex: 1 0 auto; flex-direction: column; gap: 2px; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-group + .nav-group { margin-top: 12px; }
.nav > .nav-group-service { margin-top: auto; padding-top: 12px; }
.nav-caption {
  padding: 0 10px 4px; color: var(--text-muted); font-size: 11px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase; white-space: nowrap;
}
.nav-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-secondary); font-weight: 500;
}
@media (hover: hover) { .nav-link:hover { background: var(--hover); color: var(--text); text-decoration: none; } }
.nav-link.is-active { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--text); }
.nav-link.is-active .icon { color: var(--accent); }
.nav-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-badge {
  min-width: 20px; padding: 1px 6px; border-radius: 10px; background: var(--bad); color: var(--on-bad);
  font-size: 11px; font-weight: 700; text-align: center;
}
/* The unread events of «Внимание», shown only while nothing needs action (one badge per item). */
.nav-badge.is-info { background: var(--accent); color: var(--on-accent); }
.sidebar-foot { margin-top: auto; padding: 12px 10px 2px; color: var(--text-muted); font-size: 12px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.demo-banner {
  padding: 8px 20px; background: color-mix(in srgb, var(--warn) 22%, var(--surface)); color: var(--text);
  font-size: 13px; border-bottom: 1px solid var(--border);
}
.demo-banner strong { font-weight: 600; }
.demo-banner .demo-banner-close { display: none; }
html.demo-closed .demo-banner { display: none; }   /* closed once (theme.js, before the first paint) */
.topbar {
  position: sticky; top: 0; z-index: 30; display: grid; align-items: center; gap: 8px 16px;
  /* V-03 / V-08: the title column is capped, so the search starts at the same x on every route; every
   * row is one control high and nothing wraps, so the bar has the same height on every route with
   * the same rows (one row without the period, two with it). */
  grid-template-columns: minmax(0, 320px) minmax(0, 1fr); grid-template-areas: "title sync";
  grid-auto-rows: var(--control-h);
  padding: 10px 20px; background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.view-title { grid-area: title; font-size: 18px; font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-sync { grid-area: sync; display: flex; align-items: center; justify-content: flex-end; gap: 8px; min-width: 0; }
.topbar-sync > .btn, .topbar-sync > .btn-icon { flex: none; }
.topbar-sync > .select { flex: 0 1 180px; min-width: 128px; }      /* the profile filter of a view without the period: «Все профили» stays whole */
.topbar-filters { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.topbar-filters[hidden] { display: none; }
.topbar-filters .select { flex: 0 1 160px; min-width: 96px; max-width: 240px; }
/* Phone-only parts of the top bar (the app bar, plan 1.2), the period chip under it, the tab bar. */
.topbar .appbar-back, .topbar .appbar-sub, .topbar .appbar-icon, .period-chip-row, .tabbar { display: none; }
.sync-info {
  display: inline-flex; align-items: center; gap: 7px; flex: none; min-width: max-content; padding: 6px 8px; border: 0; border-radius: 8px;
  background: none; color: var(--text-secondary); font-size: 12px; cursor: pointer;
}
/* Below 1100 px the status is the dot alone; the whole text stays in the tooltip and the log. */
@media (max-width: 1099px) {
  /* Too little room for the dates next to the period buttons («2…», review F-12): they go to the
   * tooltip of the buttons (paintPeriodDates) instead of being cut. */
  .topbar-filters .period-dates { display: none; }
  .sync-info { min-width: 0; }
  .sync-info .sync-text, #sync-btn .sync-btn-text { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
  #sync-btn { padding: 0 10px; }
}
@media (hover: hover) { .sync-info:hover { background: var(--hover); } }
.sync-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* The running status («Проверка токена · <профиль>») is capped, so a sync does not squeeze the
 * search (review RD-4b, V-03); the whole line is in the tooltip and the sync log. */
@media (min-width: 1100px) {
  .sync-info { min-width: 0; max-width: 200px; }
}
#sync-btn.is-running { padding: 0 10px; }
#sync-btn.is-running .sync-btn-text { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
.sync-progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 3px; overflow: hidden; background: var(--hover); }
.sync-progress-bar { height: 100%; background: var(--accent); transition: width 0.4s ease; }
.sync-progress-bar.is-indeterminate { width: 35% !important; animation: slide 1.4s ease-in-out infinite; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(290%); } }

.view { flex: 1; padding: 18px 20px 40px; outline: none; }
.view-body { display: flex; flex-direction: column; gap: 16px; transition: opacity 0.15s; }
.view-narrow { max-width: 900px; }
.is-stale { opacity: 0.7; transition: opacity 150ms; }   /* the route progress bar is the main cue (plan5 3.1) */

.dot { display: inline-block; flex: none; width: 9px; height: 9px; border-radius: 50%; background: var(--tone, var(--muted)); }

/* -------------------------------------------------------------- controls */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: var(--control-h); padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface-raised); color: var(--text);
  font-weight: 500; line-height: 1.2; cursor: pointer; white-space: nowrap; text-decoration: none;
}
@media (hover: hover) { .btn:hover { background: var(--surface-2); text-decoration: none; } }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-sm { min-height: var(--control-h-sm); padding: 0 9px; font-size: 12px; border-radius: 7px; }
.btn-square { padding: 0 7px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
@media (hover: hover) { .btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); } }
.btn-danger { background: var(--bad); border-color: var(--bad); color: var(--on-bad); }
@media (hover: hover) { .btn-danger:hover { background: var(--bad-ink); } }
.btn-danger-ghost { color: var(--bad-ink); border-color: transparent; background: none; }
@media (hover: hover) { .btn-danger-ghost:hover { background: color-mix(in srgb, var(--bad) 12%, transparent); } }
.btn-icon {
  display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; flex: none;
  border: 0; border-radius: 8px; background: none; color: var(--text-secondary); cursor: pointer;
}
@media (hover: hover) { .btn-icon:hover { background: var(--hover); color: var(--text); } }

.input, .select {
  min-height: var(--control-h); padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface-raised);
  color: var(--text); min-width: 0;
}
.input::placeholder { color: var(--text-muted); }
textarea.input { resize: vertical; min-height: 56px; padding: 7px 10px; }
.select { padding-right: 26px; max-width: 220px; }
.select-sm { min-height: var(--control-h-sm); padding: 0 22px 0 8px; font-size: 12px; }
.input:disabled, .select:disabled { opacity: 0.55; }
.input-num { width: 150px; }
.input-code { width: 84px; text-transform: uppercase; }

.seg {
  display: inline-flex; flex: 0 1 auto; min-width: 0; min-height: var(--control-h); border: 1px solid var(--border-strong); border-radius: 8px;
  overflow-x: auto; scrollbar-width: none; background: var(--surface-raised);
}
.seg::-webkit-scrollbar { display: none; }
.seg-btn { flex: none; padding: 0 10px; border: 0; border-right: 1px solid var(--border); background: none; cursor: pointer; font-size: 13px; white-space: nowrap; }
/* V-05: the ring is drawn inside the button, so the rounded group does not cut it off. */
.seg-btn:focus-visible { outline-offset: -3px; }
.seg-btn.is-active:focus-visible { outline-color: var(--on-accent); }
.tab:focus-visible, .table-wrap a:focus-visible, .table-wrap button:focus-visible { outline-offset: -2px; }
.seg-btn:last-child { border-right: 0; }
@media (hover: hover) { .seg-btn:hover:not(:disabled) { background: var(--hover); } }
.seg-btn.is-active { background: var(--accent); color: var(--accent-ink); }
.seg-btn:disabled { opacity: 0.5; cursor: default; }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.toolbar-spacer { flex: 1; }
.toolbar-text { color: var(--text-secondary); font-size: 13px; max-width: 720px; }
.toolbar-chips { display: flex; }
.search {
  display: inline-flex; align-items: center; gap: 6px; min-height: var(--control-h); padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: 8px; background: var(--surface-raised); color: var(--text-muted);
}
/* V-16: a long placeholder ends with an ellipsis instead of a letter cut in half. */
.search .input { min-height: calc(var(--control-h) - 2px); border: 0; padding: 0; background: none; width: 250px; outline: none; text-overflow: ellipsis; }
.search:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.check { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; white-space: nowrap; }
.check input { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); }

.fchips { display: flex; flex-wrap: wrap; gap: 6px; }
.fchip {
  display: inline-flex; align-items: center; gap: 6px; min-height: var(--control-h); padding: 0 12px; border: 1px solid var(--border-strong);
  border-radius: var(--r-pill); background: var(--surface-raised); cursor: pointer; font-size: 13px; white-space: nowrap;
}
@media (hover: hover) { .fchip:hover { background: var(--surface-2); } }
/* V-15: the selected chip has the accent tint of the menu; the solid accent is for .seg only. */
.fchip.is-active { background: color-mix(in srgb, var(--accent) 14%, var(--surface-raised)); border-color: var(--accent); color: var(--text); font-weight: 600; }
.fchip-count { font-size: 11px; opacity: 0.75; font-variant-numeric: tabular-nums; }

.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 40; display: grid; gap: 2px; min-width: 210px;
  max-height: 62vh; overflow: auto; padding: 8px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-raised); box-shadow: var(--shadow);
}
.menu-item { padding: 4px 6px; border-radius: 6px; }
@media (hover: hover) { .menu-item:hover { background: var(--hover); } }
.menu-reset { margin-top: 6px; }

.switch { position: relative; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 12px; color: var(--text-secondary); }
.switch input { position: absolute; opacity: 0; width: 34px; height: 20px; margin: 0; cursor: pointer; }
.switch-track { position: relative; width: 34px; height: 20px; border-radius: 10px; background: var(--border-strong); transition: background 0.15s; }
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--on-fill); transition: transform 0.15s; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
.switch input:checked + .switch-track { background: var(--ok); }
.switch input:checked + .switch-track::after { transform: translateX(14px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled + .switch-track { opacity: 0.5; }

/* ---------------------------------------------------------------- pieces */

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; line-height: 1.35;
  white-space: nowrap; background: color-mix(in srgb, var(--tone, var(--muted)) 17%, transparent); color: var(--tone-ink, var(--text-secondary));
}
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chips-cell { max-width: 260px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; max-width: 100%; min-width: 0; padding: 1px 8px; border-radius: 10px; background: var(--hover);
  color: var(--text-secondary); font-size: 12px; white-space: nowrap;
}
.chip-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; }
/* V-01: a long profile name ends with an ellipsis inside its chip (the title carries the whole name). */
.chip-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.copy-id {
  display: inline-flex; align-items: center; gap: 4px; margin-left: -4px; padding: 0 4px; border: 0; border-radius: 5px; background: none;
  color: var(--text-muted); font-family: var(--mono); font-size: 12px; cursor: pointer;
}
.copy-id .icon { width: 12px; height: 12px; opacity: 0; }
@media (hover: hover) { .copy-id:hover { background: var(--hover); color: var(--text); } }
.copy-id:focus-visible .icon { opacity: 1; }
@media (hover: hover) { .copy-id:hover .icon { opacity: 1; } }
.ext-link { display: inline-flex; align-items: center; gap: 3px; font-size: 12px; }
.ext-link .icon { width: 12px; height: 12px; }
.sev { display: inline-flex; flex: none; color: var(--tone-ink); }
.sev .icon { width: 18px; height: 18px; }

.meter { height: 4px; min-width: 64px; margin-top: 5px; border-radius: 2px; background: var(--hover); overflow: hidden; }
.meter-fill { height: 100%; border-radius: 2px; background: var(--tone); }

.delta { display: inline-flex; align-items: center; gap: 3px; font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.delta-arrow { font-size: 9px; }
.delta-good { color: var(--ok-ink); }
.delta-bad { color: var(--bad-ink); }
.delta-neutral { color: var(--text-secondary); }

.card { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); min-width: 0; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px 0; }
.card-title { font-size: 14px; font-weight: 700; margin-right: auto; }
.card-actions { display: flex; align-items: center; gap: 8px; }
.card-link { font-size: 12px; }
.card-body { padding: 12px 16px 16px; }
.card-body > .table-wrap { margin: 0 -16px -16px; border: 0; border-top: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius); }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(172px, 1fr)); gap: 12px; }
.kpi { padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); min-width: 0; }
.kpi[class*="tone-"] { box-shadow: inset 3px 0 0 var(--tone); }
.kpi-label { color: var(--text-secondary); font-size: 12px; }
.kpi-value { margin-top: 2px; font-size: 22px; font-weight: 700; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-value a { color: inherit; }
/* A tile with a target is one link (App.ui.kpi href, review F-04): the whole tile is the tap. */
a.kpi-link { display: block; color: inherit; text-decoration: none; transition: border-color 0.12s; }
@media (hover: hover) { a.kpi-link:hover { border-color: var(--border-strong); text-decoration: none; } }
@media (hover: hover) { a.kpi-link:hover .kpi-value { color: var(--link); } }
a.kpi-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.kpi-sub { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; min-height: 18px; margin-top: 2px; }
.kpi-note { color: var(--text-muted); font-size: 12px; }


.note-banner {
  display: flex; align-items: flex-start; gap: 8px; padding: 9px 12px; border-radius: 8px; font-size: 13px;
  background: color-mix(in srgb, var(--tone, var(--muted)) 14%, transparent); color: var(--text);
}
.note-banner .icon { margin-top: 2px; color: var(--tone-ink); }

.empty { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 40px 20px; text-align: center; }
.empty-title { font-size: 15px; font-weight: 700; }
.empty-text { max-width: 520px; color: var(--text-secondary); }
.card-body > .empty { padding: 20px 10px; }
.error-box {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border: 1px solid color-mix(in srgb, var(--bad) 40%, transparent);
  border-radius: var(--radius); background: color-mix(in srgb, var(--bad) 8%, var(--surface));
}
.error-box > .icon { width: 22px; height: 22px; color: var(--bad-ink); }
.error-body { flex: 1; min-width: 0; }
.error-title { font-weight: 700; }
.error-text { color: var(--text-secondary); overflow-wrap: anywhere; }
.all-clear { display: flex; align-items: center; gap: 8px; color: var(--ok-ink); font-weight: 600; }

.skeleton { padding: 8px 0; }
.sk-line { height: 14px; margin: 12px 0; border-radius: 5px; background: var(--hover); animation: pulse 1.3s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
/* Layout-matched skeletons (App.ui.skeleton kinds, plan5 3.2) are at the end of this file. */

.event-list, .plain-list { list-style: none; margin: 0; padding: 0; }
.event { display: flex; align-items: flex-start; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.event:last-child { border-bottom: 0; padding-bottom: 0; }
.event:first-child { padding-top: 0; }
.event-body { flex: 1; min-width: 0; }
.event-title { color: var(--text); font-weight: 500; overflow-wrap: anywhere; }
.event.is-unseen .event-title { font-weight: 700; }
.event-meta { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 2px; color: var(--text-muted); font-size: 12px; overflow-wrap: anywhere; }
.unseen-dot { flex: none; width: 8px; height: 8px; margin-top: 6px; border-radius: 50%; background: var(--accent); }
.more { display: flex; justify-content: center; }
.plain-list li { padding: 3px 0; }

/* ---------------------------------------------------------------- tables */

/* position: relative keeps absolutely positioned descendants (.visually-hidden) inside the scroll box */
.table-wrap { position: relative; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.table-tall { max-height: calc(100vh - 190px); max-height: calc(100dvh - 190px); }
.table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
.table th {
  position: sticky; top: 0; z-index: 2; padding: 8px 10px; background: var(--surface-2); color: var(--text-secondary);
  font-size: 12px; font-weight: 600; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border);
}
.table td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table tbody tr:last-child td { border-bottom: 0; }
.table .is-right { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table td.is-right .meter { margin-left: auto; width: 84px; }
.table tr.is-clickable { cursor: pointer; }
@media (hover: hover) { .table tbody tr:hover > td { background: var(--hover); } }
.table tfoot td {
  position: sticky; bottom: 0; z-index: 2; background: var(--surface-2); border-top: 1px solid var(--border-strong);
  border-bottom: 0; font-weight: 600; white-space: nowrap;
}
.table-compact th, .table-compact td { padding: 6px 10px; }
.th-sort { display: inline-flex; align-items: center; gap: 4px; padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer; }
.th-sort.is-active { color: var(--text); }
@media (hover: hover) { .th-sort:hover { color: var(--text); } }
.sort-arrow { width: 8px; font-size: 8px; }
.cell-title { font-weight: 600; overflow-wrap: anywhere; }
.cell-sub { margin-top: 2px; color: var(--text-muted); font-size: 12px; white-space: normal; }
.cell-sub[class*="tone-"] { color: var(--tone-ink); }
.cell-sub.nowrap { white-space: nowrap; }
.acc-name { font-weight: 600; color: var(--text); overflow-wrap: anywhere; }
.acc-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; margin: 2px 0 4px; color: var(--text-muted); font-size: 12px; }

.with-avatar { display: flex; align-items: flex-start; gap: 10px; min-width: 0; }
.avatar { flex: none; width: 32px; height: 32px; border-radius: 50%; object-fit: cover; background: var(--hover); }
.avatar-lg { width: 40px; height: 40px; }
.thumb { flex: none; width: 48px; height: 48px; border-radius: 6px; object-fit: cover; background: var(--hover); }

.detail-row > td { padding: 16px; background: var(--surface-2); }
@media (hover: hover) { .table tbody tr.detail-row:hover > td { background: var(--surface-2); } }
.detail {
  position: sticky; left: 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)); gap: 16px 24px;
  width: min(100%, calc(100vw - var(--sidebar-w) - 2 * var(--view-pad) - 34px));
}
.detail .table-wrap { max-width: 100%; }
.detail-wide { grid-column: 1 / -1; }
.detail-title { margin-bottom: 6px; font-size: 13px; font-weight: 700; }
.table tr.is-expanded > td { background: var(--hover); }

.tree-name { display: flex; align-items: flex-start; gap: 6px; min-width: 260px; }
.tree-toggle {
  display: inline-flex; align-items: center; justify-content: center; flex: none; width: 22px; height: 22px; padding: 0;
  border: 0; border-radius: 6px; background: none; color: var(--text-secondary); cursor: pointer;
}
@media (hover: hover) { .tree-toggle:hover { background: var(--hover); color: var(--text); } }
.tree-spacer { flex: none; width: 22px; }
.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px 20px; margin: 0; }
.fact dt { color: var(--text-muted); font-size: 12px; }
.fact dd { margin: 2px 0 0; min-width: 0; overflow-wrap: anywhere; }
.fact .meter { max-width: 150px; }
.fact .chip { white-space: normal; max-width: 100%; overflow-wrap: anywhere; }   /* «Профили и их права»: long names wrap in a narrow column */
.fact .chip .chip-label { white-space: normal; overflow-wrap: anywhere; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; margin-bottom: -1px; border-bottom: 2px solid transparent;
  color: var(--text-secondary); font-weight: 500; white-space: nowrap;
}
@media (hover: hover) { .tab:hover { color: var(--text); text-decoration: none; } }
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); font-weight: 700; }
.tab-count { padding: 0 6px; border-radius: 9px; background: var(--hover); color: var(--text-secondary); font-size: 11px; font-weight: 600; }

/* ---------------------------------------------------------------- charts */

.chart-host, .chart { position: relative; min-width: 0; }
.chart-svg { display: block; overflow: visible; outline-offset: 4px; }
.chart-grid { stroke: var(--grid); stroke-width: 1; }
.chart-base { stroke: var(--axis); stroke-width: 1; }
.chart-tick { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-title { fill: var(--text-secondary); font-size: 12px; font-weight: 600; }
.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { opacity: 0.12; }
.chart-dot, .chart-end { stroke: var(--surface); stroke-width: 2; }
.chart-cross { stroke: var(--axis); stroke-width: 1; }
.chart-band { fill: var(--text); opacity: 0.06; }
.chart-bars.has-hover .chart-bar { opacity: 0.45; }
.chart-bars.has-hover .chart-bar.is-hover { opacity: 1; }
.chart-overlay { fill: transparent; pointer-events: all; }
/* Touch (plan 2.7): a sideways drag scrubs the days, a vertical swipe scrolls the page. */
.chart-svg, .chart-overlay, .hbar-row, .stack-bar { touch-action: pan-y; }
.chart-empty { padding: 48px 10px; color: var(--text-muted); text-align: center; }
.chart-note { margin-top: 8px; font-size: 12px; }
.chart-tip {
  position: absolute; z-index: 10; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-raised); box-shadow: var(--shadow); font-size: 12px; white-space: nowrap; pointer-events: none;
}
.chart-tip-head { margin-bottom: 3px; color: var(--text-secondary); }
.chart-tip-row { display: flex; align-items: center; gap: 6px; }
.chart-tip-key { flex: none; width: 8px; height: 8px; border-radius: 2px; }
.chart-tip-value { font-variant-numeric: tabular-nums; }
.chart-tip-name { color: var(--text-secondary); }
.chart-table { border: 0; border-radius: 0; max-height: 340px; }

.hbar-list { display: flex; flex-direction: column; gap: 2px; }
.hbar-row { display: grid; grid-template-columns: minmax(90px, 210px) minmax(0, 1fr); align-items: center; gap: 12px; padding: 3px 4px; border-radius: 6px; }
.hbar-row:focus-visible { background: var(--hover); }
@media (hover: hover) { .hbar-row:hover { background: var(--hover); } }
.hbar-label { color: var(--text-secondary); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { display: flex; align-items: center; gap: 8px; min-width: 0; }
.hbar-bar { flex: none; height: 14px; border-radius: 0 4px 4px 0; }
.hbar-value { font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }

.stack-bar { display: flex; gap: 2px; height: 30px; border-radius: 6px; overflow: hidden; }
.stack-part { display: flex; align-items: center; justify-content: center; min-width: 3px; flex-basis: 0; color: var(--on-fill); font-size: 12px; font-weight: 700; overflow: hidden; }
.stack-part.ink-dark { color: var(--on-fill-light); }
.chart-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 12px 0 0; padding: 0; list-style: none; }
.chart-legend-link { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); font-size: 12px; }
@media (hover: hover) { a.chart-legend-link:hover { color: var(--text); text-decoration: none; } }
.chart-legend-swatch { width: 10px; height: 10px; border-radius: 3px; }
.chart-legend-value { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

.spark { display: inline-block; vertical-align: middle; }
.spark-svg { display: block; }
.spark-base { stroke: var(--grid); stroke-width: 1; }
.spark-line { fill: none; stroke: var(--series-1); stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.spark-end { fill: var(--series-1); }

/* ---------------------------------------------------- dialogs and toasts */

.dialog {
  width: min(540px, calc(100vw - 32px)); max-height: calc(100vh - 48px); padding: 0; border: 1px solid var(--border);
  border-radius: 14px; background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  max-height: calc(100dvh - 48px);
}
.dialog[open] { display: flex; flex-direction: column; }
.dialog-wide { width: min(860px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px 20px; }
.dialog-title { flex: 1; font-size: 16px; font-weight: 700; }
.dialog-body { display: flex; flex-direction: column; gap: 12px; padding: 4px 20px 16px; overflow: auto; overscroll-behavior: contain; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--border); }
.dialog-text { color: var(--text-secondary); }
.log {
  max-height: 46vh; margin: 0; padding: 12px; overflow: auto; border-radius: 8px; background: var(--surface-2);
  font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere;
}

.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast {
  display: flex; align-items: flex-start; gap: 8px; padding: 10px 8px 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-raised); box-shadow: var(--shadow), inset 3px 0 0 var(--tone);
}
.toast > .icon { margin-top: 2px; color: var(--tone-ink); }
.toast-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.toast .btn-icon { width: 22px; height: 22px; }
.toast .toast-close { flex: none; }

/* ----------------------------------------------------------------- forms */

.form { display: flex; flex-direction: column; gap: 14px; }
.form-row { display: flex; flex-direction: column; align-items: flex-start; gap: 5px; }
.form-row > .input:not(.input-num):not(.input-code), .form-row > .select { width: 100%; max-width: 100%; }
.card-body > .form-row + .form-row { margin-top: 14px; }
.form-label { font-weight: 600; font-size: 13px; }
.form-hint { color: var(--text-muted); font-size: 12px; }
.form-error { padding: 9px 12px; border-radius: 8px; background: color-mix(in srgb, var(--bad) 13%, transparent); color: var(--bad-ink); font-weight: 500; overflow-wrap: anywhere; }
.form-actions { display: flex; gap: 8px; }
.form-hint[class*="tone-"] { color: var(--tone-ink); }

/* ------------------------------------------------------------ responsive */

/* Icon rail: tablet and narrower (641–1023 px, plan D2); the phone hides it (≤ 640 block). */
@media (max-width: 1023px) {
  :root { --sidebar-w: 56px; --view-pad: 14px; }
  .sidebar { padding: 14px 8px; }
  .brand { padding: 4px 5px 16px; }
  .brand-name, .sidebar-foot { display: none; }
  /* Hidden visually only: the link keeps its accessible name (see .visually-hidden). */
  .nav-label { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
  .nav-link { position: relative; justify-content: center; padding: 10px 0; }
  .nav-badge { position: absolute; top: 2px; right: 2px; min-width: 16px; padding: 0 4px; font-size: 10px; }
  .nav-caption { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
  .nav-group + .nav-group { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
  .nav > .nav-group-service { margin-top: auto; }
  .view { padding: 14px 14px 32px; }
  .topbar { padding: 10px 14px; }
  .search .input { width: 170px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
.table tr.is-target > td { background: color-mix(in srgb, var(--accent) 11%, var(--surface)); }
.facts-rows { grid-template-columns: minmax(0, 1fr); gap: 5px; font-size: 13px; }
.facts-rows .fact { display: grid; grid-template-columns: clamp(96px, 38%, 170px) minmax(0, 1fr); gap: 10px; align-items: baseline; }
.facts-rows .fact dd { margin: 0; }
@media (max-width: 980px) {
  .tree-name { min-width: 210px; }
}

/* -------------------- accounts: state view (web/acct*.js) -------------------- */

.tone-text { color: var(--tone-ink); }
.select[hidden] { display: none; }
.note-banner .plain-list { margin-top: 4px; }
.note-banner .plain-list li { padding: 2px 0; overflow-wrap: anywhere; }

/* ------ token and access: profiles, Business Managers, pages, events (web/access.js) ------ */

.has-hint { text-decoration: underline dotted; text-underline-offset: 3px; cursor: help; }
.toolbar[hidden] { display: none; }
.toolbar-label { color: var(--text-muted); font-size: 13px; }

/* ---------------------------------------------- insights: period control, statistics (web/period.js, web/stats.js) */

.period-more { flex: none; gap: 4px; padding: 0 10px; font-size: 13px; font-weight: 400; }
.period-more .icon { width: 14px; height: 14px; color: var(--text-muted); }
.period-more.is-active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.period-more.is-active .icon { color: inherit; }
.period-menu[hidden], .period-dates[hidden] { display: none; }
.period-pop { left: 0; right: auto; min-width: 232px; }
.period-item { display: block; width: 100%; padding: 6px 8px; border: 0; background: none; color: var(--text); font: inherit; font-size: 13px; text-align: left; cursor: pointer; }
.period-item.is-active { background: var(--accent); color: var(--accent-ink); }
.period-own { display: grid; gap: 6px; margin-top: 6px; padding: 8px 2px 2px; border-top: 1px solid var(--border); }
.period-own-title { color: var(--text-secondary); font-size: 12px; font-weight: 600; }
.period-date { display: grid; grid-template-columns: 22px minmax(0, 1fr); align-items: center; gap: 6px; font-size: 13px; color: var(--text-secondary); }
.period-date .input { padding: 5px 8px; font-size: 13px; }
.period-own .form-error { padding: 6px 8px; font-size: 12px; }
.period-own .btn { justify-self: start; }
.period-dates {
  flex: 0 100 auto;          /* gives its room up first: the period buttons stay whole as long as possible */ min-width: 0; overflow: hidden; text-overflow: ellipsis; color: var(--text-secondary); font-size: 12px;
  white-space: nowrap; font-variant-numeric: tabular-nums;
}
.table .group-row th {
  position: static; padding-top: 10px; background: none; color: var(--text); font-size: 12px; font-weight: 700;
  white-space: normal; border-bottom: 1px solid var(--border);
}
@media (hover: hover) { .table tbody tr.group-row:hover > th { background: none; } }

/* ------------------------------ presentation pass (web/present.js): attention, search, links */

/* The sidebar counter of «Требует внимания»: amber while nothing is critical. */
.nav-badge.is-warn { background: color-mix(in srgb, var(--warn) 38%, var(--surface)); color: var(--text); box-shadow: inset 0 0 0 1px var(--warn); }

/* Global search in the top bar. */
.gsearch { position: relative; flex: 0 1 360px; min-width: 120px; margin-right: auto; }
.gsearch-bar { display: flex; align-items: center; gap: 8px; min-width: 0; }
.gsearch-field { display: flex; flex: 1; width: 100%; min-width: 0; }
.gsearch-cancel { display: none; }            /* the phone sheet's «Отмена» (present.js) */
.gsearch-field .input { flex: 1; min-width: 0; width: auto; }
.gsearch-key {
  flex: none; padding: 0 6px; border: 1px solid var(--border-strong); border-radius: 5px; color: var(--text-muted);
  font: 11px/16px var(--mono); background: var(--surface-2);
}
.gsearch-field:focus-within .gsearch-key { display: none; }
.gsearch-pop {
  position: absolute; left: 0; top: calc(100% + 6px); z-index: 60; width: max(100%, 460px); max-width: calc(100vw - var(--sidebar-w) - 2 * var(--view-pad));
  max-height: min(70vh, 560px); overflow: auto; overscroll-behavior: contain; padding: 6px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-raised); box-shadow: var(--shadow);
}
.gsearch-pop[hidden] { display: none; }
.gsearch-group + .gsearch-group { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); }
.gsearch-title { padding: 4px 8px; color: var(--text-muted); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.gsearch-item {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 0 10px; padding: 6px 8px; border-radius: 6px; color: var(--text);
}
.gsearch-item.is-active { background: var(--hover); text-decoration: none; }
@media (hover: hover) { .gsearch-item:hover { background: var(--hover); text-decoration: none; } }
.gsearch-item.is-active { outline: 2px solid var(--accent); outline-offset: -2px; }
.gsearch-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gsearch-id { color: var(--text-muted); font-size: 12px; white-space: nowrap; }
.gsearch-sub { grid-column: 1 / -1; color: var(--text-secondary); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gsearch-more { display: block; padding: 4px 8px; font-size: 12px; }
.gsearch-empty { padding: 10px; color: var(--text-secondary); font-size: 13px; }

/* Links to related objects that should not shout: they read as text until hovered. */
.quiet-link { color: inherit; }
@media (hover: hover) { .quiet-link:hover { color: var(--link); } }
a.chip { color: inherit; }
@media (hover: hover) { a.chip:hover { text-decoration: none; border-color: var(--border-strong); background: var(--hover); } }

/* Tablet and small laptop (up to 1279 px): a narrower title column leaves the search its room. */
@media (max-width: 1279px) {
  .topbar { grid-template-columns: minmax(0, 220px) minmax(0, 1fr); }
}
/* A view without the period puts the profile filter into row 1 (syncFiltersVisibility): up to
 * 1439 px the sync button keeps its icon only, so the search and the filter keep readable widths. */
@media (min-width: 1100px) and (max-width: 1439px) {
  .topbar-sync:has(> .select) #sync-btn { padding: 0 10px; }
  .topbar-sync:has(> .select) #sync-btn .sync-btn-text { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
}
@media (max-width: 1023px) {
  .gsearch-key { display: none; }
  .gsearch-pop { width: min(460px, calc(100vw - var(--sidebar-w) - 2 * var(--view-pad))); }
}

/* ---------------------------------------- phone and tablet kit (plan 2.1–2.5, web/app.js) */
/* Breakpoints: phone ≤ 640 px, tablet 641–1023 px, desktop ≥ 1024 px (App.layout mirrors them).
 * Everything below changes nothing on a desktop: its rules live in the phone block, under
 * (hover: none), or on classes that only the kit adds. */

/* Phone-only parts of a card table (App.ui.dataTable). */
.table > caption.m-sort, .table td.m-more { display: none; }
.m-sort-label { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; color: var(--text-secondary); font-size: var(--fs-md); }
/* The select goes to its own row when it would cut its value next to «Сортировка» (review F-12). */
.m-sort-label .select { flex: 1 1 250px; max-width: none; }
.m-more-btn {
  display: inline-flex; align-items: center; gap: 4px; min-height: var(--tap); padding: 0; border: 0; background: none;
  color: var(--link); font: inherit; font-size: var(--fs-md); cursor: pointer;
}
.m-more-btn .icon { width: 14px; height: 14px; }

/* Tablet tier of a card table (641–1023 px, plan 2.3 critic, review F-01): title, badge and the
 * first 3 metrics stay in the row; «+N» opens a row listing the other columns; the «Сортировка»
 * select stands in for the hidden headers. Nothing of it shows on a desktop or a phone. */
.table .t-more, .table tr.t-detail-row { display: none; }
.t-more-btn {
  display: inline-flex; align-items: center; justify-content: center; min-width: 36px; min-height: var(--control-h-sm); padding: 0 8px;
  border: 1px solid var(--border-strong); border-radius: var(--r-pill); background: var(--surface-raised); color: var(--link);
  font: inherit; font-size: var(--fs-sm); font-weight: 600; cursor: pointer;
}
.t-detail { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 8px 16px; margin: 0; }
.t-detail-item { min-width: 0; }
.t-detail-item dt { color: var(--text-muted); font-size: var(--fs-xs); font-weight: 500; }
.t-detail-item dd { margin: 0; overflow-wrap: anywhere; }
@media (min-width: 641px) and (max-width: 1023px) {
  .table.is-cards .t-hide { display: none; }
  .table.is-cards th.t-more, .table.is-cards td.t-more { display: table-cell; width: 1%; text-align: right; }
  .table.is-cards tr.t-detail-row.is-open { display: table-row; }
  .table.is-cards tr.t-detail-row > td { background: var(--surface-2); white-space: normal; }
  .table.is-cards tr.is-tmore > td { border-bottom-color: transparent; }
  .table.is-cards.has-thide > caption.m-sort { display: table-caption; padding: 8px 12px; text-align: left; }
  .table.is-cards.has-thide .m-sort-label .select { flex: 0 1 auto; }
  /* The kept columns share the width: they wrap instead of pushing the table past the edge. */
  .table.is-cards.has-thide { width: 100%; font-size: 13px; }
  .table.is-cards.has-thide > * > tr > :is(th, td):not(.t-more) { min-width: 0; max-width: none; white-space: normal; padding-left: 6px; padding-right: 6px; }
  .table.is-cards.has-thide > * > tr > .t-more { padding-left: 0; padding-right: 4px; }
  .table.is-cards.has-thide .copy-id { overflow-wrap: anywhere; }
  .table.is-cards.has-thide :is(.tree-name, .acc-name, .cell-title) { min-width: 0; }
  .table.is-cards.has-thide .meter { min-width: 0; width: auto; max-width: 64px; margin-left: auto; }
  .table.is-cards.has-thide .th-sort { white-space: normal; text-align: inherit; }
}
/* A touch tablet scrolls the page, never a box inside it (no nested scroller, review F-01). */
@media (pointer: coarse) and (min-width: 641px) {
  .table-tall { max-height: none; }
}

/* Tap-to-reveal hints (App.ui.hint): the mouse keeps the tooltip, a finger gets a popover. */
.hint { cursor: help; }
.hint-wrap { display: inline-flex; align-items: center; gap: 2px; min-width: 0; max-width: 100%; }
.hint-btn {
  display: none; align-items: center; justify-content: center; flex: none; width: var(--tap); height: var(--tap);
  margin: -12px -12px -12px -8px; padding: 0; border: 0; background: none; color: var(--text-muted); cursor: pointer;
}
.hint-btn .icon { width: 14px; height: 14px; }
/* A popover in viewport coordinates (closes when the page scrolls); a phone gets a sheet instead. */
.hint-pop {
  position: fixed; z-index: 300; max-width: min(360px, calc(100vw - 16px)); padding: 8px 10px;
  border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface-raised); box-shadow: var(--shadow);
  color: var(--text); font-size: var(--fs-md); white-space: pre-line; overflow-wrap: anywhere;
}
@media (hover: none) {
  .hint { text-decoration: underline dotted; text-underline-offset: 3px; }
  .hint-btn { display: inline-flex; }
}

/* Section tabs (App.ui.sectionTabs): one row that scrolls sideways, faded where it continues. */
.section-tabs { scrollbar-width: none; }
.section-tabs::-webkit-scrollbar { display: none; }
.section-tabs.is-scroll-end { -webkit-mask-image: linear-gradient(to right, black calc(100% - 28px), transparent); mask-image: linear-gradient(to right, black calc(100% - 28px), transparent); }
.section-tabs.is-scroll-start { -webkit-mask-image: linear-gradient(to left, black calc(100% - 28px), transparent); mask-image: linear-gradient(to left, black calc(100% - 28px), transparent); }
.section-tabs.is-scroll-start.is-scroll-end {
  -webkit-mask-image: linear-gradient(to right, transparent, black 28px, black calc(100% - 28px), transparent);
  mask-image: linear-gradient(to right, transparent, black 28px, black calc(100% - 28px), transparent);
}
.tab-count[class*="tone-"] { background: color-mix(in srgb, var(--tone) 17%, transparent); color: var(--tone-ink); }

/* Blocks the screen tracks build with (contract classes). */
.kpi-group { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.kpi-group-title { color: var(--text-secondary); font-size: var(--fs-sm); font-weight: 600; }
.kpi-group > .kpis { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.facts-key { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.toolbar-sheet-btn { display: none; }
.dialog-badge { flex: none; }

@media (max-width: 640px) {
  /* iOS zooms into any field under 16 px: no field is smaller on a phone. */
  .input, .select, .select-sm, .search .input, input.mono, textarea,
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select { font-size: 16px !important; }
  .toolbar-sheet-btn { display: inline-flex; }
  .facts-key { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* The top bar must not be a containing block for the fixed sheets inside it (menus, search). */
  .topbar { -webkit-backdrop-filter: none; backdrop-filter: none; background: var(--bg); }

  /* Card tables: each row is a card; one page scroll, no scroll trap, no sticky column. */
  .table-wrap:has(> .table.is-cards) { max-height: none; overflow: visible; border: 0; border-radius: 0; background: none; }
  .card-body > .table-wrap:has(> .table.is-cards) { margin: 0; border-top: 0; }
  .table.is-cards { display: flex; flex-direction: column; gap: 8px; }
  .table.is-cards > thead { display: none; }
  .table.is-cards > caption.m-sort { display: block; order: -2; text-align: left; }
  /* «Сортировка» above a full-width select: «Цена результата · ↓ больше» is not cut at 360 px. */
  .table.is-cards .m-sort-label .select { flex-basis: 100%; }
  .table.is-cards > tfoot { display: block; order: -1; }
  .table.is-cards > tbody { display: flex; flex-direction: column; gap: 8px; }
  .table.is-cards tr {
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 12px; padding: 12px;
    border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface);
  }
  .table.is-cards > tfoot tr { background: var(--surface-2); border-color: var(--border-strong); }
  .table.is-cards td {
    display: block; position: static; min-width: 0; max-width: none; padding: 0; border: 0; background: none;
    box-shadow: none; text-align: left; white-space: normal; font-weight: inherit;
  }
  .table.is-cards tr.is-expanded > td, .table.is-cards tr.is-target > td { background: none; }
  @media (hover: hover) { .table.is-cards tbody tr:hover > td { background: none; } }
  /* The desktop sticky name columns (screens.css, account.css) paint their own hover/target background; not in a card. */
  .table.is-cards tbody tr.is-expanded > td.col-tree, .table.is-cards tbody tr.is-target > td.col-tree { background: none; }
  @media (hover: hover) { .table.is-cards tbody tr:hover > td.col-account, .table.is-cards tbody tr:hover > td.col-tree { background: none; } }
  .table.is-cards tr.is-clickable:active { background: var(--hover); }
  .table.is-cards tr.is-target { border-color: var(--accent); }
  .table.is-cards tr.health-bad { box-shadow: inset 3px 0 0 var(--bad); }
  .table.is-cards tr.health-warn { box-shadow: inset 3px 0 0 var(--warn); }
  /* One bar per card: the row-level bar above, not the first-cell bar of the desktop rule (screens.css). */
  .table.is-cards tr[class*="health-"] > td:first-child { box-shadow: none; }
  .table.is-cards td.m-title { grid-column: 1 / -1; order: -3; }
  .table.is-cards tr:has(> td.m-badge) > td.m-title { grid-column: 1 / 3; }
  .table.is-cards td.m-badge { grid-column: 3; grid-row: 1; justify-self: end; text-align: right; }
  /* A long name never runs under the badge or past the card (round-5 review L3): the title column
   * breaks anywhere, its avatar row may shrink, and a badge wider than its third wraps inside it. */
  .table.is-cards td.m-title, .table.is-cards td.m-sub { overflow-wrap: anywhere; }
  .table.is-cards td.m-title .with-avatar > :not(.avatar):not(.thumb) { min-width: 0; }
  .table.is-cards td.m-badge { max-width: calc(100% + 12px); }   /* + the column gap, never the title's column */
  .table.is-cards td.m-badge .badge { max-width: 100%; white-space: normal; overflow-wrap: break-word; text-align: center; }
  .table.is-cards td.m-sub { grid-column: 1 / -1; order: -2; }
  .table.is-cards td.m-metric { order: 0; }
  .table.is-cards td.m-hide { display: none; order: 1; }
  .table.is-cards tr.is-more > td.m-hide { display: block; }
  .table.is-cards td.m-more { display: block; grid-column: 1 / -1; order: 2; margin: -8px 0 -12px; }
  .table.is-cards td.m-metric::before, .table.is-cards td.m-hide::before {
    content: attr(data-label); display: block; color: var(--text-muted); font-size: var(--fs-xs); font-weight: 500;
  }
  .table.is-cards td.m-metric .meter { margin-left: 0; }
  .table.is-cards tr.detail-row, .table.is-cards tr.group-row { display: block; padding: 0; border: 0; background: none; box-shadow: none; }
  .table.is-cards tr.detail-row > td { padding: 12px; border-radius: var(--r-lg); background: var(--surface-2); }
  .table.is-cards .detail { position: static; width: auto; }
  .table.is-cards td.t-more, .table.is-cards tr.t-detail-row { display: none; }

  /* Sheets (App.openDialog sheet: 'full' | 'bottom'). */
  .dialog.dialog-sheet {
    width: 100%; max-width: 100%; height: 100vh; height: 100dvh; max-height: 100vh; max-height: 100dvh;
    margin: 0; border: 0; border-radius: 0; box-shadow: none;
  }
  /* Anchored to the bottom edge explicitly (plan5 1.1): no strip of page colour under it. */
  .dialog.dialog-bottom {
    width: 100%; max-width: 100%; max-height: 85vh; max-height: 85dvh; margin: 0; inset: auto 0 0 0; border-bottom: 0;
    border-radius: 12px 12px 0 0;
  }
  .dialog-sheet .dialog-head, .dialog-bottom .dialog-head {
    position: sticky; top: 0; z-index: 1; flex: none; padding: max(6px, var(--safe-top)) 6px 6px 16px; background: var(--surface);
  }
  .dialog-sheet .dialog-head { border-bottom: 1px solid var(--border); }
  /* A bottom sheet starts mid-screen: no notch inset above its title (review F-02); one
   * without a footer keeps its last row out of the home-indicator zone. */
  .dialog-bottom .dialog-head { padding-top: 18px; touch-action: none; }   /* 18 px: the grabber above the title; the drag zone */
  .dialog-bottom .dialog-body:last-child { padding-bottom: max(16px, calc(var(--safe-bottom) + 12px)); }
  .dialog-sheet .dialog-title, .dialog-bottom .dialog-title {
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; overflow-wrap: anywhere;
  }
  .dialog-sheet .dialog-close, .dialog-bottom .dialog-close { width: var(--tap); height: var(--tap); }
  .dialog-sheet .dialog-body, .dialog-bottom .dialog-body { flex: 1 1 auto; min-height: 0; padding: 12px 16px 16px; overflow-y: auto; }
  .dialog-sheet .dialog-foot, .dialog-bottom .dialog-foot {
    position: sticky; bottom: 0; flex: none; flex-wrap: wrap; padding: 12px 16px max(12px, var(--safe-bottom)); background: var(--surface);
  }
  .dialog-sheet .dialog-foot .btn, .dialog-bottom .dialog-foot .btn { flex: 1 1 100%; min-height: 48px; }
  .dialog-sheet .dialog-foot .btn-primary, .dialog-bottom .dialog-foot .btn-primary,
  .dialog-sheet .dialog-foot .btn-danger, .dialog-bottom .dialog-foot .btn-danger { order: -1; }
  .dialog-sheet .dialog-foot .btn:not(.btn-primary):not(.btn-danger),
  .dialog-bottom .dialog-foot .btn:not(.btn-primary):not(.btn-danger) { border-color: transparent; background: none; color: var(--link); }
  .dialog-sheet .facts:not(.facts-rows) { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* Label-left rows (pixel / BM sheets) stay one row per fact (review F-08). */
  .dialog-sheet .facts-rows { grid-template-columns: minmax(0, 1fr); }
  .dialog-sheet .facts-rows .fact { grid-template-columns: minmax(0, 40%) minmax(0, 1fr); }
  .dialog-sheet .facts-rows .fact dd { overflow-wrap: break-word; }
  .dialog-sheet .log { max-height: none; overflow: visible; }

  /* Menus (details.menu) and hints open as bottom-sheet dialogs on a phone (plan5 1.3): see
   * «Menus and hints in a sheet» at the end of this file. */

  /* Global search as a full-screen sheet (Present.openSearch toggles .is-sheet). It follows the
   * visual viewport (App.historyLayer {el}: --vvh, --vv-top), so the keyboard never pushes the
   * input off the screen; only the results scroll (plan5 1.4). */
  .gsearch.is-sheet {
    position: fixed; left: 0; right: 0; top: var(--vv-top, 0px); bottom: auto; height: var(--vvh, 100%); z-index: 220;
    display: flex; flex-direction: column; gap: 8px; max-width: none; margin: 0;
    padding: max(8px, var(--safe-top)) 12px max(12px, var(--safe-bottom)); background: var(--bg);
  }
  /* Below the visual viewport (keyboard accessory bar, Safari's toolbar) the page must not show
   * through: the sheet's colour continues under its bottom edge (round 5, seen on the simulator). */
  /* A shadow, not a box: it never counts as overflow, so iOS has nothing to pan to with the keyboard up. */
  .gsearch.is-sheet { box-shadow: 0 50vh 0 50vh var(--bg); }
  .gsearch.is-sheet .gsearch-bar { flex: none; }
  .gsearch.is-sheet .gsearch-pop {
    position: static; flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
    width: auto; max-width: none; max-height: none; border: 0; box-shadow: none; background: none;
  }
  .gsearch.is-sheet .gsearch-item { min-height: 56px; align-content: center; }
  .gsearch.is-sheet .gsearch-field { min-height: var(--tap); }
  .gsearch.is-sheet .gsearch-key { display: none; }
  .gsearch.is-sheet .gsearch-cancel { display: inline-flex; flex: none; min-height: var(--tap); border-color: transparent; background: none; color: var(--link); }
}

/* ------------------------------------------------ phone shell (plan 1.2, 2.2, 2.8; web/app.js) */
/* The sidebar gives way to a bottom tab bar, the top bar becomes a 48 px app bar
 * [back] [title / subtitle] [search] [sync], the period and the profile filter move into a chip
 * under it that opens a sheet. Desktop and tablet are untouched by this block. */

/* Period sheet, «Ещё» sheet (built by app.js; only ever opened from the phone shell). */
.sheet-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.sheet-choice { min-height: var(--tap); white-space: normal; }
.sheet-choice.is-active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.sheet-own, .sheet-section { display: grid; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.sheet-section-title { color: var(--text-secondary); font-size: var(--fs-sm); font-weight: 600; }
.sheet-date { display: grid; grid-template-columns: 28px minmax(0, 1fr); align-items: center; gap: 8px; color: var(--text-secondary); }
.sheet-date .input { width: 100%; min-height: var(--tap); }
.sheet-apply { width: 100%; min-height: 48px; }
.sheet-caption { color: var(--text-muted); font-size: var(--fs-sm); }
.radio-rows { display: flex; flex-direction: column; }
.radio-row { display: flex; align-items: center; gap: 10px; min-height: var(--tap); padding: 0 4px; border-radius: var(--r-md); cursor: pointer; }
@media (hover: hover) { .radio-row:hover { background: var(--hover); } }
.radio-row input { flex: none; width: 20px; height: 20px; margin: 0; accent-color: var(--accent); }
.radio-row-text { min-width: 0; overflow-wrap: anywhere; }
.more-group { display: flex; flex-direction: column; }
.more-group + .more-group { margin-top: 4px; padding-top: 8px; border-top: 1px solid var(--border); }
.more-caption { padding: 0 8px 2px; color: var(--text-muted); font-size: var(--fs-xs); font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.more-row {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 48px; padding: 0 8px; border: 0; border-radius: var(--r-md);
  background: none; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
@media (hover: hover) { .more-row:hover { background: var(--hover); text-decoration: none; } }
.more-row .icon { width: 20px; height: 20px; color: var(--text-secondary); }
.more-row > .icon:last-child { width: 16px; height: 16px; color: var(--text-muted); }
.more-row-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.more-version { padding: 4px 8px 0; color: var(--text-muted); font-size: var(--fs-sm); }

@media (max-width: 640px) {
  :root { --sidebar-w: 0px; --view-pad: 12px; --tabbar-space: calc(var(--tabbar-h) + var(--safe-bottom)); }
  html, body { overflow-x: clip; }          /* a safety net only: the probe must still find no overflow */
  .sidebar { display: none; }

  /* App bar: 48 px plus the notch. The title block is one button (title over the subtitle) that
   * opens the sync log; the search and sync icons sit on the right. */
  .topbar {
    grid-template-columns: auto minmax(0, 1fr) auto auto; grid-template-rows: auto; grid-auto-rows: auto;
    grid-template-areas: "back title search sync";
    gap: 0; min-height: calc(var(--appbar-h) + var(--safe-top));
    padding: var(--safe-top) max(2px, var(--safe-right)) 0 max(2px, var(--safe-left)); align-items: stretch;
  }
  .topbar .appbar-back { display: inline-flex; grid-area: back; align-self: center; width: var(--tap); height: var(--tap); color: var(--text); }
  .appbar-back .icon { width: 22px; height: 22px; }
  .view-title {
    grid-area: title; z-index: 1; align-self: start; padding: 5px 4px 0 10px; font-size: 17px; line-height: 22px;
    pointer-events: none;
  }
  .topbar.has-back .view-title { padding-left: 2px; }
  .topbar .appbar-sub {
    display: flex; grid-area: title; align-items: flex-end; gap: 6px; min-width: 0; min-height: var(--appbar-h);
    padding: 0 4px 6px 10px; border: 0; border-radius: 0; background: none; color: var(--text-secondary);
    font: inherit; font-size: var(--fs-sm); line-height: 16px; text-align: left; cursor: pointer;
  }
  .topbar.has-back .appbar-sub { padding-left: 2px; }
  .appbar-sub .dot { width: 7px; height: 7px; margin-bottom: 4px; }
  .appbar-sub-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar .appbar-icon { display: inline-flex; align-self: center; width: var(--tap); height: var(--tap); color: var(--text); }
  .appbar-search { grid-area: search; }
  .appbar-sync { grid-area: sync; }
  .appbar-icon .icon { width: 22px; height: 22px; }
  .appbar-sync.is-running { color: var(--accent); }
  /* The desktop parts of the bar leave it; the search stays in the DOM for its full-screen sheet. */
  .topbar-sync { display: contents; }
  .topbar-sync > :not(.gsearch), .topbar .topbar-filters { display: none; }
  .gsearch:not(.is-sheet) { display: none; }

  /* The demo note is one line that can be closed. */
  .demo-banner { display: flex; align-items: center; gap: 4px; padding: 0 0 0 max(12px, var(--safe-left)); font-size: var(--fs-sm); }
  .demo-banner[hidden] { display: none; }
  .demo-banner-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .demo-banner-more { display: none; }
  .demo-banner .demo-banner-close { display: inline-flex; flex: none; width: var(--tap); height: 40px; }
  /* The app bar comes first (it owns the notch inset), the note right under it and scrolling
   * away with the page: no text under the notch, no blank band (review F-11). */
  .main > .topbar { order: -1; }

  /* Period and profile: one chip in the flow under the app bar, scrolling away with the page. */
  .period-chip-row { display: block; padding: 10px max(12px, var(--safe-right)) 0 max(12px, var(--safe-left)); }
  .period-chip-row[hidden] { display: none; }
  .period-chip {
    display: inline-flex; align-items: center; gap: 6px; max-width: 100%; min-height: var(--tap); padding: 0 14px;
    border: 1px solid var(--border-strong); border-radius: var(--r-pill); background: var(--surface-raised); color: var(--text);
    font: inherit; font-size: var(--fs-md); cursor: pointer;
  }
  .period-chip-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .period-chip .icon { width: 14px; height: 14px; color: var(--text-muted); }

  .view { padding: 12px max(12px, var(--safe-right)) calc(var(--tabbar-space) + 24px) max(12px, var(--safe-left)); }

  /* Bottom tab bar: Обзор · Внимание · Аккаунты · Показ · Ещё. */
  .tabbar {
    /* z-index under the app bar's 30: the search sheet lives inside the app bar and must cover the tab bar. */
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 29; display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
    height: calc(var(--tabbar-h) + var(--safe-bottom)); padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
    background: var(--surface); border-top: 1px solid var(--border);
  }
  .tabbar-link {
    position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    min-width: 0; height: var(--tabbar-h); padding: 0 2px; border: 0; background: none; color: var(--text-muted);
    font: inherit; font-size: var(--fs-xs); font-weight: 500; line-height: 1.2; cursor: pointer;
  }
  @media (hover: hover) { .tabbar-link:hover { text-decoration: none; } }
  .tabbar-link .icon { width: 22px; height: 22px; }
  .tabbar-link.is-active { color: var(--text); font-weight: 700; }
  .tabbar-link.is-active .icon { color: var(--accent); }
  .tabbar-label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabbar-badge {
    position: absolute; top: 4px; left: calc(50% + 6px); min-width: 18px; padding: 0 5px; border-radius: 9px;
    background: var(--bad); color: var(--on-bad); font-size: 10px; font-weight: 700; line-height: 16px; text-align: center;
  }
  .tabbar-badge.is-info { background: var(--accent); color: var(--on-accent); }
  .tabbar-badge.is-warn { background: color-mix(in srgb, var(--warn) 38%, var(--surface)); color: var(--text); box-shadow: inset 0 0 0 1px var(--warn); }

  /* Toasts sit above the tab bar, with a 44 px close. */
  .toasts {
    left: max(12px, var(--safe-left)); right: max(12px, var(--safe-right)); bottom: calc(var(--tabbar-space) + 12px);
    max-width: none;
  }
  .toast { align-items: center; padding: 2px 0 2px 12px; }
  .toast .toast-close { width: var(--tap); height: var(--tap); }

  /* Global wrap rules (2.8, V-18): nothing makes the page wider than the screen. */
  .card-head { flex-wrap: wrap; }
  .card-head > .card-actions { flex: 1 1 100%; flex-wrap: wrap; min-width: 0; max-width: 100%; }
  .check { white-space: normal; }
  .kpis, .kpi-group > .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi { padding: 12px; }
  .hbar-row { grid-template-columns: minmax(0, 1fr); gap: 2px; }
  .hbar-label { white-space: normal; overflow-wrap: anywhere; }
  .select { max-width: 100%; }
  .toolbar > .search { flex: 1 1 100%; }
  .search .input { flex: 1; width: auto; }
  .detail { width: auto; }
}

/* A phone held sideways (844 × 390) is a tablet by width: the top bar scrolls away and the demo
 * note is one line, so at least half of the 390 px shows the view; notches stay clear. */
@media (max-height: 500px) and (min-width: 641px) {
  .topbar { position: static; }
  .demo-banner-text { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .demo-banner-more { display: none; }
  .demo-banner { padding-top: 4px; padding-bottom: 4px; }
  .sidebar { width: calc(var(--sidebar-w) + var(--safe-left)); padding-left: max(8px, var(--safe-left)); }
  .view { padding-left: max(14px, var(--safe-left)); padding-right: max(14px, var(--safe-right)); }
  .topbar { padding-right: max(14px, var(--safe-right)); }
}

/* Touch (plan 2.7): every control at least 44 px, icon buttons 44 × 44 (their glyph unchanged). */
@media (pointer: coarse) {
  :root { --control-h: 44px; --control-h-sm: 44px; }
  /* iOS zooms into a field under 16 px in landscape too (review F-07): every touch screen. */
  .input, .select, .select-sm, .search .input, input.mono, textarea,
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select { font-size: 16px !important; }
  .menu-item, .nav-link, .tab, .m-more-btn, .seg-btn, .th-sort { min-height: var(--tap); }
  /* iOS draws a native select at its own ~29 px height whatever min-height says (round 5, seen on the
   * simulator): on touch screens the field is ours, the arrow two gradients in the theme colour. */
  select.select, select.select-sm {
    -webkit-appearance: none; appearance: none; padding-right: 30px;
    background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%),
      linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
    background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
    background-size: 5px 5px; background-repeat: no-repeat;
  }
  /* Landscape and touch tablets (review F-13): the same 44 px as the phone portrait. */
  .th-sort { min-width: var(--tap); }
  #sync-btn { min-width: var(--tap); }
  .sync-info { min-height: var(--tap); }
  .search .input { min-height: var(--tap); margin: -1px 0; }
  /* Small inline controls keep their look; an invisible 44 px hit area is laid over them. */
  .copy-id, a.chip, .ext-link, .card-link, .hint { position: relative; }
  .copy-id::after, a.chip::after, .ext-link::after, .card-link::after, .hint::after {
    content: ""; position: absolute; left: 50%; top: 50%; width: max(100%, var(--tap)); height: max(100%, var(--tap));
    transform: translate(-50%, -50%);
  }
  .btn-icon, .tree-toggle, .tree-info, .dialog-close, .toast-close { min-width: var(--tap); min-height: var(--tap); }
  .check, .switch { min-height: var(--tap); }
  .fchips { gap: 8px; }
  .t-more-btn, .sync-info { min-width: var(--tap); min-height: var(--tap); justify-content: center; }
  .topbar-sync > .select { min-width: 150px; }       /* «Все профили» whole at 16 px */
  .gsearch-item { min-height: var(--tap); }
  /* a count alone in a fact («Пользователи 3») is wide enough to hit */
  .fact dd > a:not([class]):only-child { display: inline-block; min-width: var(--tap); }
  /* Text links and small inline controls (review F-08 / F-13, portrait and landscape): a
   * transparent 15 px border above and below makes the target 44 px, the negative margin keeps
   * the layout, `padding-box` keeps the look (a chip or badge background stays its own size). */
  :is(.view, .dialog) :is(a:not([class]), a.quiet-link, a.back-link, a.attention-name, a.attention-chip, a.attention-ok,
    a.attention-link, a.inv-item, a.no-rate, .copy-id, a.ext-link, a.chip, a.card-link, .hint, button.fold-toggle) {
    border-top: 15px solid transparent; border-bottom: 15px solid transparent; margin-top: -15px; margin-bottom: -15px;
    background-clip: padding-box;
  }
  /* the inner (padding-box) radius is the outer one minus the border: keep the pill round */
  :is(.view, .dialog) :is(a.chip, a.attention-chip, .hint.badge) { border-radius: 25px; }
}
/* The icon rail on a touch tablet: 44 px wide links (review F-13). */
@media (pointer: coarse) and (min-width: 641px) and (max-width: 1023px) {
  :root { --sidebar-w: 60px; }
  .sidebar { padding-left: 8px; padding-right: 8px; }
  .nav-link, .brand { min-width: var(--tap); justify-content: center; }
}
@media (hover: none) {
  .tree-info { opacity: 1; }
}


/* ======================================================= round 5: iPhone feel (plan5 §1, §3) */

/* The page background is the root's: the status bar of the home-screen app and the strip under
 * Safari's toolbar take it, and it follows data-theme (the tokens live on :root). While a bottom
 * sheet is open in a Safari tab the band under the sheet continues the sheet's colour. */
html {
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;              /* pinch zoom stays, double-tap zoom on controls goes */
}
:root.has-bottom-sheet:not(.is-standalone) { background: var(--surface); }

/* App shell (plan5 1.1). The @media below is App.SHELL_QUERY in web/app.js, character for
 * character (a test keeps them equal): the document never scrolls, `.app` does, the tab bar stays
 * fixed outside it, so Safari's floating toolbar never collapses and never moves the bar.
 * Never put transform / filter / contain / will-change on .app: it would capture fixed children. */
@media (max-width: 640px), (hover: none) and (pointer: coarse) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  .app {
    position: fixed; inset: 0; min-height: 0; align-items: flex-start;
    overflow-x: hidden; overflow-y: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch;
  }
  .main { min-height: 100%; }
  html.is-locked .app { overflow: hidden; }
}
/* Overlay lock without the shell (a desktop with a mouse): App.lock() pads the body by the
 * scrollbar's width, so nothing shifts. */
html.is-locked { overflow: hidden; }
/* A layer inside .app (the phone search sheet) sits under the tab bar's stacking level. */
html.has-layer .tabbar { visibility: hidden; }

/* Focus rings only for the keyboard (plan5 1.5): App tracks the last input in html[data-input]. */
:root[data-input="touch"] :focus-visible:not(input, textarea, select) { outline: none; }
.dialog:focus { outline: none; }

/* No sticky hover on touch: every :hover above sits in @media (hover: hover). The active segment
 * keeps its colours whatever the pointer does. */
.seg-btn.is-active { background: var(--accent); color: var(--accent-ink); }
@media (hover: hover) { .seg-btn.is-active:hover { background: var(--accent); color: var(--accent-ink); } }
/* Press feedback on touch instead of hover. */
@media (hover: none) {
  .btn:not(.btn-primary):not(.btn-danger):active, .btn-icon:active, .more-row:active, .tabbar-link:active,
  .menu-item:active, .table.is-cards tr.is-clickable:active, .radio-row:active { background: var(--hover); }
  .btn-primary:active { background: var(--accent-strong); border-color: var(--accent-strong); }
  .btn-danger:active { background: var(--bad-ink); }
}
/* Chrome is not text: a long press selects nothing and opens no callout. Ids, facts and tables stay selectable. */
.tabbar, .topbar, .more-row, .seg, .btn, .btn-icon, .chip, .fchip, .tab, .period-chip, .dialog-head {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}

/* iOS date fields have an intrinsic minimum width: they must shrink to their cell (plan5 1.7). */
.sheet-date .input, input[type="date"].input {
  display: flex; align-items: center; min-width: 0; max-width: 100%; -webkit-appearance: none; appearance: none;
}
/* Without native appearance iOS puts the value at the top of a tall field: centre it (round 5, simulator). */
input[type="date"]::-webkit-date-and-time-value { text-align: left; margin: 0; }

/* Bottom-sheet grabber (a real element, the drag handle of the swipe-down). */
.dialog-grabber { display: none; }
@media (max-width: 640px) {
  .dialog-bottom .dialog-grabber {
    display: block; position: absolute; top: 7px; left: 50%; width: 36px; height: 4px; margin-left: -18px;
    border-radius: 2px; background: var(--border-strong);
  }
}

/* Menus and hints in a sheet (plan5 1.3): the .menu-pop node of a details.menu moves into the
 * dialog body while the sheet is open; a hint is a short text. */
.dialog .menu-pop {
  position: static; display: grid; gap: 2px; min-width: 0; max-height: none; overflow: visible; padding: 0;
  border: 0; border-radius: 0; background: none; box-shadow: none;
}
.dialog .menu-pop .menu-item { display: flex; align-items: center; min-height: var(--tap); }
.dialog .hint-text { color: var(--text); font-size: 15px; white-space: pre-line; overflow-wrap: anywhere; }

/* Route progress (plan5 3.1): 2 px under the top bar, indeterminate, only after 150 ms. */
.route-progress {
  position: absolute; left: 0; right: 0; bottom: -1px; z-index: 1; height: 2px; overflow: hidden; pointer-events: none;
  opacity: 0; transition: opacity 200ms ease-out;
}
.route-progress.is-on { opacity: 1; }
.route-progress-bar { width: 35%; height: 100%; background: var(--accent); animation: slide 1.1s ease-in-out infinite; }
@media (max-height: 500px) and (min-width: 641px) {
  .topbar { position: relative; }        /* the static landscape bar still anchors the bars */
}

/* Busy buttons (App.ui.busy): disabled, a spinning icon, the width kept. */
.is-busy { cursor: progress; }
.btn.is-busy:disabled, .btn-icon.is-busy:disabled { opacity: 0.8; }
.btn.is-busy-solo, .btn-icon.is-busy-solo { position: relative; color: transparent; }
.is-busy-solo > .busy-spin { position: absolute; left: 50%; top: 50%; margin: -8px 0 0 -8px; color: var(--text); }
.btn-primary.is-busy-solo > .busy-spin, .btn-danger.is-busy-solo > .busy-spin { color: var(--accent-ink); }

/* Skeletons (App.ui.skeleton, plan5 3.2): each block has the height of what it stands for. They
 * appear only when a load takes longer than 120 ms (no flicker on fast answers). */
.skeleton { animation: sk-appear 160ms ease-out 120ms both; }
@keyframes sk-appear { from { opacity: 0; } }
.skeleton { display: flex; flex-direction: column; gap: 12px; }
.sk-bar, .sk-block, .sk-thumb, .sk-input {
  display: block; border-radius: 5px; background: var(--hover); animation: pulse 1.3s ease-in-out infinite;
}
.sk-bar { height: 12px; }
/* Default widths (the static skeleton of index.html has no style attributes). */
.sk-kpi .sk-label, .sk-card > .sk-title { width: 40%; }
.sk-kpi .sk-value { width: 60%; }
.sk-card > .sk-text { width: 88%; }
.sk-card > .sk-text:nth-of-type(3n) { width: 74%; }
.sk-card > .sk-text:nth-of-type(3n + 1) { width: 60%; }
.sk-title { height: 14px; }
.sk-label { height: 10px; }
.sk-value { height: 22px; margin-top: 8px; }
.sk-text { height: 12px; margin-top: 10px; }
/* The phone period chip is in the boot page already (its row keeps its place while the app starts,
 * review L2); app.js fills it or hides the row for a screen without a period. */
.sk-chip { width: 180px; height: 12px; }
.sk-pill { width: 72px; height: 22px; margin-top: 10px; border-radius: var(--r-pill); }
.sk-pill-lg { flex: none; width: 96px; height: 36px; border-radius: var(--r-pill); }
.sk-metric { width: 64px; height: 12px; }
.sk-lines .sk-line:first-child { margin-top: 4px; }
.sk-kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.sk-kpi {
  height: 88px; padding: 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
}
.sk-card { padding: 14px 16px 16px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.sk-chart { height: 300px; }
.sk-chart .sk-block { height: 220px; margin-top: 18px; }
.sk-header { min-height: 120px; }
.sk-facts { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px 20px; margin-top: 16px; }
.sk-list { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.sk-list-head { height: 36px; background: var(--surface-2); border-bottom: 1px solid var(--border); }
.sk-row { display: flex; align-items: center; gap: 12px; height: 44px; padding: 0 12px; border-bottom: 1px solid var(--border); }
.sk-row:last-child { border-bottom: 0; }
.sk-row-main { flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; }
.sk-row-main .sk-text { display: none; }
.sk-row-main .sk-title { flex: 0 1 auto; }
.sk-row-metrics { display: flex; gap: 24px; }
.sk-thumb { flex: none; width: 40px; height: 40px; border-radius: 6px; }
.has-thumb .sk-row { height: 56px; }
.sk-pills { display: flex; gap: 8px; overflow: hidden; }
.sk-pills .sk-pill-lg:nth-child(2n) { width: 120px; }
.sk-form .sk-field { margin-top: 14px; }
.sk-input { height: 44px; margin-top: 6px; border-radius: var(--r-md); }
@media (max-width: 640px) {
  .sk-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .sk-kpi { height: 76px; padding: 12px; }
  .sk-value { margin-top: 6px; }
  .sk-chart { height: 260px; }
  .sk-chart .sk-block { height: 200px; }
  .sk-header { min-height: 150px; }
  .sk-facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sk-list { display: flex; flex-direction: column; gap: 8px; border: 0; background: none; overflow: visible; }
  .sk-list-head { display: none; }
  .sk-row {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: start; height: 96px; padding: 12px;
    border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface);
  }
  .sk-row:last-child { border-bottom: 1px solid var(--border); }
  .sk-row-main { display: block; grid-column: 1 / -1; }
  .sk-row-main .sk-text { display: block; }
  .sk-row-metrics { grid-column: 1 / -1; gap: 16px; margin-top: 4px; }
  .has-thumb .sk-row { grid-template-columns: 48px minmax(0, 1fr); height: 96px; }
  .has-thumb .sk-thumb { width: 48px; height: 48px; grid-row: 1 / 3; }
  .has-thumb .sk-row-main, .has-thumb .sk-row-metrics { grid-column: 2; }
}

/* Motion (plan5 3.4, 3.6): enter-only animations of transform and opacity. */
:root { --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1); }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes view-forward { from { opacity: 0; transform: translateX(24px); } }
@keyframes view-back { from { opacity: 0; transform: translateX(-24px); } }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes sheet-down { to { transform: translateY(100%); } }
@keyframes sheet-in-right { from { transform: translateX(100%); } }
@keyframes sheet-out-right { to { transform: translateX(100%); } }
@keyframes dialog-in { from { opacity: 0; transform: scale(0.98); } }
.is-entering { animation: fade-in 150ms ease-out; }
.is-entering.is-quick { animation-duration: 120ms; }
.view.is-entering { animation: fade-in 150ms ease-out; }
@media (max-width: 640px) {
  html.is-shell .view.is-entering[data-dir="forward"] { animation: view-forward 220ms var(--ease-out); }
  html.is-shell .view.is-entering[data-dir="back"] { animation: view-back 220ms var(--ease-out); }
  html.is-shell .view.is-entering[data-dir="lateral"] { animation: fade-in 160ms ease-out; }
}
.dialog.is-opening { animation: dialog-in 160ms var(--ease-out); }
.dialog.is-opening::backdrop { animation: fade-in 200ms ease-out; }
.dialog.dialog-bottom.is-opening { animation: sheet-up 260ms var(--ease-out); }
.dialog.dialog-sheet.is-opening { animation: sheet-in-right 280ms var(--ease-out); }
.dialog.is-closing { animation: fade-out 120ms ease-in forwards; }
/* A closing sheet takes no taps (a tab tapped during its 200 ms exit hit the sheet's own buttons, round 5). */
.dialog.is-closing, .dialog.is-closing::backdrop { pointer-events: none; }
/* The exit is played by a ghost (App animateClose, review M1): the real modal dialog is closed at
 * once, so the next tap reaches the page; the ghost and its backdrop never take a tap. */
.dialog-ghost, .dialog-ghost-backdrop { pointer-events: none; }
.dialog-ghost { z-index: 990; }
.dialog-ghost-backdrop { position: fixed; inset: 0; z-index: 989; background: rgba(0, 0, 0, 0.45); animation: fade-out 200ms ease-in forwards; }
.dialog.is-closing::backdrop { animation: fade-out 200ms ease-in forwards; }
.dialog.dialog-bottom.is-closing { animation: sheet-down 200ms ease-in forwards; }
.dialog.dialog-sheet.is-closing { animation: sheet-out-right 220ms ease-in forwards; }
/* Swipe-down: the sheet follows the finger (inline transform); the backdrop fades with --drag. */
.dialog.is-dragging { transition: none; }
.dialog.is-dragging::backdrop { opacity: calc(1 - var(--drag, 0)); }
.dialog.is-settling { transition: transform 200ms var(--ease-out); }

@media (prefers-reduced-motion: reduce) {
  .skeleton, .is-entering, .view.is-entering, .dialog.is-opening, .dialog.is-opening::backdrop,
  .dialog.is-closing, .dialog.is-closing::backdrop, .sk-bar, .sk-block, .sk-thumb, .sk-input, .route-progress-bar {
    animation: none !important;
  }
  .dialog.is-settling, .route-progress, .is-stale { transition: none; }
}

/* Offline screen (plan5 2.4): the cached shell when the server cannot be reached. */
.offline { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 64px 20px; text-align: center; }
.offline .brand-mark { width: 48px; height: 48px; border-radius: 12px; font-size: 18px; }
.offline-title { font-size: 18px; font-weight: 700; }
.offline-text { max-width: 420px; color: var(--text-secondary); }
.offline .btn { min-width: 160px; }

/* «Установить как приложение»: three steps (plan5 2.4). */
.install-steps { margin: 0; padding-left: 22px; display: flex; flex-direction: column; gap: 10px; }
.install-steps li { padding-left: 4px; }
.install-steps .icon { width: 18px; height: 18px; vertical-align: -4px; color: var(--accent); }
