/* Управляющий — layout and components. Needs tokens.css and crystal.css.
   Three shells from one markup: compact window (< 900 px), wide window (>= 900 px), Telegram (.tg, tabs). */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: var(--t-body); font-feature-settings: "tnum" 1; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; text-align: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }

.app { max-width: 560px; margin: 0 auto; padding: var(--s3) var(--pad) calc(var(--s6) + env(safe-area-inset-bottom)); }
.col { display: contents; }

/* top line: name, state chip, freshness */
.top { display: flex; align-items: center; gap: var(--s2); min-height: 36px; }
.brand { font-weight: 650; letter-spacing: var(--track-tight); }
.stamp { margin-left: auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--faint); font: var(--t-small); }
.stamp.warn { color: var(--warn); }

/* status chip */
.chip { --c: var(--faint); display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 10px 0 8px; border-radius: var(--r-chip);
  font: 500 13px/1 var(--font); color: var(--c); background: color-mix(in srgb, var(--c) 12%, transparent); white-space: nowrap; }
.chip i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.chip.live { --c: var(--accent); }
.chip.ok { --c: var(--ok); }
.chip.warn { --c: var(--warn); }
.chip.neg { --c: var(--neg); }
.chip.muted { --c: var(--muted); }

/* hero: the crystal and one sentence about now */
.hero { display: grid; justify-items: center; text-align: center; gap: var(--s3); padding: var(--s4) 0 var(--s5); }
.cr-wrap { width: min(184px, 48vw); aspect-ratio: 1; }
.now h1 { font: 650 24px/1.2 var(--font); letter-spacing: var(--track-tight); text-wrap: balance; }
.now p { margin-top: var(--s1); color: var(--muted); text-wrap: balance; max-width: 36ch; }

.banner { margin-bottom: var(--s3); padding: var(--s3) var(--s4); border-radius: var(--r-row); font: var(--t-small); }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.neg { background: var(--neg-soft); color: var(--neg); }

/* task card: one per thing he is doing right now (0–3) */
.tasks { display: grid; gap: var(--s2); margin-bottom: var(--s3); }
.task { display: grid; grid-template-columns: 28px 1fr auto; gap: var(--s3); align-items: center; padding: var(--s3) var(--s4);
  border-radius: var(--r-row); background: var(--surface); box-shadow: inset 3px 0 0 var(--accent); }
.task h3 { font: var(--t-row); }
.task p { color: var(--muted); font: var(--t-small); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.task-meta { font: 600 15px/1 var(--font); color: var(--accent); }
.task-i { width: 28px; height: 28px; border-radius: 8px; background: color-mix(in srgb, var(--accent) 14%, transparent); position: relative; }
.task-i::after { content: ""; position: absolute; inset: 9px; border-radius: 2px; background: var(--accent); transform: rotate(45deg); }
.task.k-voice .task-i::after { border-radius: 50%; transform: none; inset: 8px; }
.task.k-send .task-i::after { clip-path: polygon(0 0, 100% 50%, 0 100%); transform: none; border-radius: 0; }
.task.k-read .task-i::after { inset: 8px 7px; transform: none; height: 3px; box-shadow: 0 5px 0 var(--accent), 0 10px 0 var(--accent); background: var(--accent); }

/* queue: three counts, zeros recede */
.queue { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; margin-bottom: var(--s3); border-radius: var(--r-row); overflow: hidden; background: var(--line); }
.q { display: grid; gap: 2px; padding: var(--s3); background: var(--surface); }
.q b { font: 600 26px/1.1 var(--font); letter-spacing: var(--track-tight); }
.q span { font: var(--t-small); color: var(--muted); }
.q.warn b { color: var(--warn); }
.q.neg b { color: var(--neg); }
.q.accent b { color: var(--accent); }
.q.zero b { color: var(--faint); }

/* health: one line of six services; in the wide window each gets its note */
.health { display: flex; flex-wrap: wrap; gap: var(--s1) var(--s3); margin-bottom: var(--s4); padding: var(--s2) 0; }
.hl { display: inline-flex; align-items: center; gap: 6px; font: var(--t-small); color: var(--muted); }
.hl i { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.hl.bad { color: var(--neg); font-weight: 600; }
.hl.bad i { background: var(--neg); box-shadow: 0 0 0 3px var(--neg-soft); }
.hl-note { display: none; }
.hl.bad .hl-note { display: inline; font-weight: 400; }

/* refresh: idle → requested (waits for the next snapshot) → done */
.actions { margin-bottom: var(--s5); }
.refresh { display: inline-flex; align-items: center; justify-content: center; gap: var(--s2); width: 100%; height: 46px; border-radius: 12px;
  font-weight: 600; background: var(--accent); color: var(--accent-ink); transition: transform .15s var(--ease), opacity .2s; }
.refresh:active { transform: scale(.98); }
.refresh-i { width: 14px; height: 14px; border: 2px solid currentColor; border-radius: 3px; transform: rotate(45deg); }
.refresh[data-state="requested"] { opacity: .75; cursor: default; }
.refresh[data-state="requested"] .refresh-i { animation: turn 1.6s linear infinite; }
.refresh[data-state="done"] { background: var(--ok); color: #fff; }
@keyframes turn { to { transform: rotate(405deg); } }

/* blocks of the wide window and the mini app tabs */
.block { margin-bottom: var(--s5); }
.block h2 { font: var(--t-title); letter-spacing: var(--track-tight); margin-bottom: var(--s3); }
.empty { padding: var(--s4); border-radius: var(--r-row); background: var(--surface); color: var(--muted); font: var(--t-small); }
.foot { margin-top: var(--s3); color: var(--faint); font: var(--t-small); }
.note { margin: var(--s3) 0; padding: var(--s2) var(--s3); border-radius: var(--r-row); font: var(--t-small); }
.note.warn { background: var(--warn-soft); color: var(--warn); }

/* error row: the text, how many times, when */
.err-row { display: grid; grid-template-columns: 8px 1fr auto; gap: var(--s3); align-items: baseline; padding: var(--s3) 0; border-bottom: 1px solid var(--line); }
.err-row i { width: 8px; height: 8px; border-radius: 2px; background: var(--neg); transform: rotate(45deg) translateY(-1px); }
.err-row p { font: var(--t-small); color: var(--ink); }
.err-meta { display: grid; justify-items: end; gap: 2px; font: var(--t-small); color: var(--faint); }
.times { color: var(--neg); font-weight: 700; }

/* last replies: who, how they asked, the question and the start of the answer */
.reply { padding: var(--s3) 0; border-bottom: 1px solid var(--line); }
.reply header { display: flex; align-items: baseline; gap: var(--s2); font: var(--t-small); color: var(--faint); }
.reply header b { color: var(--ink); font-weight: 600; font-size: 14px; }
.reply .via.voice { color: var(--accent); }
.grow { flex: 1; }
.reply .rq { margin-top: var(--s1); font: var(--t-row); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.reply .ra { margin-top: 2px; color: var(--muted); font: var(--t-small); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

/* feed */
.ev { display: grid; grid-template-columns: 64px 1fr; gap: 2px var(--s3); padding: var(--s3) 0; border-bottom: 1px solid var(--line); }
.ev-kind { font: 600 13px/1.35 var(--font); color: var(--muted); }
.ev.k-request .ev-kind { color: var(--accent); }
.ev.k-invoice .ev-kind { color: var(--ok); }
.ev p { font: var(--t-small); font-size: 14px; }
.ev-meta { grid-column: 2; display: flex; align-items: center; gap: var(--s2); font: var(--t-small); color: var(--faint); }

/* money: the number large, the currency small; overdue in red */
.money { display: inline-flex; align-items: baseline; gap: 4px; white-space: nowrap; }
.money b { font: 600 17px/1.1 var(--font); letter-spacing: var(--track-tight); }
.money small { font: 500 11px/1 var(--font); color: var(--faint); }
.money.neg b { color: var(--neg); }
.money.big b { font: var(--t-amount); }
.totals { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s2); margin-bottom: var(--s3); }
.totals.three { grid-template-columns: repeat(3, 1fr); }
.total { display: grid; gap: var(--s1); padding: var(--s3) var(--s4); border-radius: var(--r-panel); background: var(--surface); min-width: 0; }
.total > span:first-child { font: var(--t-small); color: var(--muted); }
.total .money b { font: 600 clamp(22px, 6.4vw, 30px)/1.05 var(--font); letter-spacing: var(--track-tight); }
.totals.three .money b { font-size: 19px; }
.total .count { color: var(--faint); font: var(--t-small); }

/* debtor and invoice rows */
.rows { display: grid; border-radius: var(--r-panel); background: var(--surface); overflow: hidden; }
.row { display: flex; align-items: center; gap: var(--s3); width: 100%; padding: var(--s3) var(--s4); min-height: 56px; }
.row + .row { border-top: 1px solid var(--line); }
.row:hover { background: color-mix(in srgb, var(--ink) 4%, transparent); }
.row-main { flex: 1; display: grid; gap: 2px; min-width: 0; }
.row-t { font: var(--t-row); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-s { display: flex; flex-wrap: wrap; align-items: center; gap: 4px var(--s2); font: var(--t-small); color: var(--muted); }
.debtor.late .row-s { color: var(--neg); }
.row-s .chip { height: 20px; font-size: 12px; }

/* detail screen (debtor, invoice): full screen in the mini app, a side sheet in the wide window */
.sheet { position: fixed; inset: 0; z-index: 10; overflow: auto; padding: var(--s3) var(--pad) var(--s6); background: var(--bg);
  transform: translateX(100%); transition: transform .26s var(--ease); }
.sheet.open { transform: none; }
.sheet-top { min-height: 36px; display: flex; align-items: center; margin-bottom: var(--s3); }
.back { color: var(--accent); font-weight: 600; padding: var(--s2) 0; }
.back::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 8px; border: solid currentColor; border-width: 0 0 2px 2px; transform: rotate(45deg) translateY(-1px); }
.tg .sheet-top { display: none; } /* Telegram shows its own back button */
.sheet-kind { color: var(--muted); font: var(--t-small); }
.sheet h2 { font: 650 24px/1.2 var(--font); letter-spacing: var(--track-tight); margin-top: 2px; overflow-wrap: anywhere; }
.sheet-sub { color: var(--muted); margin-top: 2px; }
.sheet-sum { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s2) var(--s3); margin: var(--s5) 0; }
.sheet h3 { font: 600 15px/1.3 var(--font); color: var(--muted); margin-bottom: var(--s2); }
.items { width: 100%; border-collapse: collapse; font: var(--t-small); font-size: 14px; }
.items th { text-align: left; font-weight: 500; color: var(--muted); padding: var(--s2) 0; border-bottom: 1px solid var(--line); }
.items td { padding: var(--s2) 0; border-bottom: 1px solid var(--line); }
.items :is(th, td):not(:first-child) { text-align: right; padding-left: var(--s3); }

/* no access: neutral, no details */
.gate { min-height: 100vh; display: grid; place-content: center; justify-items: center; gap: var(--s2); padding: var(--s5); text-align: center; color: var(--muted); }
.gate-mark { width: 56px; height: 56px; color: var(--faint); margin-bottom: var(--s3); }
.gate h1 { font: var(--t-title); color: var(--ink); }

/* loading: quiet placeholders */
.sk { height: 52px; border-radius: var(--r-row); background: var(--surface); opacity: .7; }
.sk + .sk { margin-top: var(--s2); }
.queue .sk { margin: 0; border-radius: 0; height: 64px; }
.loading .crystal { opacity: .5; }

/* ---------- shell: compact window shows status only ---------- */
.wide-only, .tabs { display: none; }

/* ---------- shell: wide window, three columns that scroll on their own ---------- */
@media (min-width: 900px) {
  .app:not(.tg) { max-width: none; height: 100vh; display: grid; grid-template-columns: minmax(300px, 340px) 1fr 1fr; grid-template-rows: auto 1fr;
    gap: 0 var(--s5); padding-bottom: 0; }
  .app:not(.tg) .top { grid-column: 1 / -1; padding-bottom: var(--s2); border-bottom: 1px solid var(--line); }
  .app:not(.tg) .col { display: block; overflow: auto; padding: var(--s4) 2px var(--s5); scrollbar-width: thin; }
  .app:not(.tg) .wide-only { display: block; }
  .app:not(.tg) .hero { padding-top: 0; }
  .app:not(.tg) .cr-wrap { width: 200px; }
  .app:not(.tg) .health { display: grid; gap: var(--s2); }
  .app:not(.tg) .hl-note { display: inline; margin-left: auto; color: var(--faint); font-weight: 400; }
  /* totals become label-and-amount lines in the narrow side column */
  .app:not(.tg) :is(.totals, .totals.three) { grid-template-columns: 1fr; }
  .app:not(.tg) .total { grid-template-columns: 1fr auto; align-items: baseline; padding: var(--s3) var(--s4); }
  .app:not(.tg) .total .count { grid-column: 2; text-align: right; }
  .app:not(.tg) .total .money b { font-size: 24px; }
  .sheet:not(.tg ~ .sheet) { left: auto; width: 420px; box-shadow: -24px 0 48px -24px rgb(0 0 0 / .35); }
}

/* ---------- shell: Telegram mini app, four tabs ---------- */
.tg { padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
.tg .wide-only { display: block; }
.tg[data-tab="status"] :is(.panel-debts, .panel-invoices, .panel-feed),
.tg[data-tab="debts"] :is(.panel-status, .panel-invoices, .panel-feed),
.tg[data-tab="invoices"] :is(.panel-status, .panel-debts, .panel-feed),
.tg[data-tab="feed"] :is(.panel-status, .panel-debts, .panel-invoices) { display: none; }
.tg:not([data-tab="status"]) .banner { margin-top: var(--s2); }
.tg:not([data-tab="status"]) .block { margin-top: var(--s3); }
.tg .hero { grid-template-columns: 112px 1fr; justify-items: start; text-align: left; align-items: center; gap: var(--s4); padding: var(--s3) 0 var(--s4); }
.tg .cr-wrap { width: 112px; }
.tg .now h1 { font-size: 21px; }
.tg .tabs { display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); }
.tabs button { height: 44px; border-radius: 10px; text-align: center; font: 500 13px/1 var(--font); color: var(--muted); }
.tabs button[aria-current] { color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); font-weight: 650; }

@media (prefers-reduced-motion: reduce) {
  .sheet, .refresh, .crystal { transition: none; }
  .refresh[data-state="requested"] .refresh-i { animation: none; }
}
