/* Оформление веб-приложения Sensonio: тёмный технический вид без градиентов; цвета заданы техническим заданием. */
:root {
  --bg: #101822; --panel: #1B2939; --active: #294868;
  --primary: #3B82F6; --accent: #60A5FA; --text: #E2E8F0;
  --muted: #8193A8; --success: #4ADE80; --danger: #F87171;
  --border: #293747; --chart-grid: #34475C;
  --user-bubble: #254768;

  --radius: 16px; --radius-small: 12px; --gap: 12px; --pad: 16px; --touch: 44px;
  --safe-top: calc(var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px)) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: calc(var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)) + var(--tg-content-safe-area-inset-bottom, 0px));
  --safe-left: calc(var(--tg-safe-area-inset-left, env(safe-area-inset-left, 0px)) + var(--tg-content-safe-area-inset-left, 0px));
  --safe-right: calc(var(--tg-safe-area-inset-right, env(safe-area-inset-right, 0px)) + var(--tg-content-safe-area-inset-right, 0px));
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
h1 { margin: 0; font-size: 20px; font-weight: 600; }
h2 { margin: 0; font-size: 20px; font-weight: 600; }
p { margin: 0; }
small, .muted { color: var(--muted); font-size: 12px; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { cursor: default; opacity: .45; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg.icon { width: 22px; height: 22px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.grow { flex: 1; min-width: 0; }

#app { display: flex; flex-direction: column; height: var(--app-height, 100dvh); overflow: hidden; padding-left: var(--safe-left); padding-right: var(--safe-right); }

.gate { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--gap); padding: 24px; text-align: center; }
.gate p { color: var(--muted); max-width: 340px; }

/* Верхняя строка */
.top { flex: none; display: flex; align-items: center; gap: 8px; padding: calc(var(--safe-top) + 6px) var(--pad) 6px 8px; border-bottom: 1px solid var(--border); }
.top h1 { flex: 1; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: var(--touch); height: var(--touch); border-radius: var(--radius-small); flex: none; }
.icon-btn.primary { background: var(--primary); color: #fff; }
.online { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.online i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.online.on i { background: var(--success); }
.online.off i { background: var(--danger); }

.banner { flex: none; display: flex; align-items: center; justify-content: space-between; gap: var(--gap); padding: 8px var(--pad); background: var(--active); }

main { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
main > section { display: flex; flex-direction: column; gap: var(--gap); padding: var(--pad); max-width: 1200px; margin: 0 auto; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--pad); display: flex; flex-direction: column; gap: var(--gap); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--gap); flex-wrap: wrap; }
.section-title { margin-top: 4px; }
.state { color: var(--muted); text-align: center; padding: 24px 8px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: var(--touch); padding: 0 16px; border-radius: var(--radius-small); background: var(--active); font-weight: 600; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.danger { background: var(--danger); color: var(--bg); }
.btn.ghost { background: transparent; border: 1px solid var(--border); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.segmented { display: inline-flex; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-small); padding: 2px; gap: 2px; }
.segmented.wide { display: flex; }
.segmented button { min-height: var(--touch); min-width: var(--touch); padding: 0 12px; border-radius: 10px; color: var(--muted); font-weight: 600; flex: 1; }
.segmented button[aria-pressed="true"] { background: var(--active); color: var(--text); }

/* Показатели и график */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--gap); }
.kpi { gap: 4px; }
.kpi .label { color: var(--muted); display: flex; align-items: center; gap: 8px; }
.kpi .label svg { width: 16px; height: 16px; color: var(--accent); }
.kpi .value { font-size: 30px; font-weight: 600; line-height: 1.2; }
.kpi .value.na { font-size: 16px; color: var(--muted); line-height: 2.25; }

.chart { position: relative; min-height: 170px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 170px; overflow: visible; }
.chart .grid { stroke: var(--chart-grid); stroke-width: 1; stroke-dasharray: 3 4; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--accent); fill-opacity: .18; stroke: none; }
.chart .cross { stroke: var(--muted); stroke-width: 1; }
.chart .dot { fill: var(--accent); stroke: var(--panel); stroke-width: 2; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .tip { position: absolute; top: 0; transform: translateX(-50%); pointer-events: none; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 4px 8px; font-size: 12px; white-space: nowrap; }
.chart .empty { position: absolute; inset: 0 0 20px; display: flex; align-items: center; justify-content: center; color: var(--muted); pointer-events: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* Службы */
.services { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--gap); align-items: start; }
.service { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.service summary { display: flex; align-items: center; gap: var(--gap); padding: var(--gap) var(--pad); min-height: 64px; cursor: pointer; list-style: none; }
.service summary::-webkit-details-marker { display: none; }
.service summary .chevron { color: var(--muted); transition: transform .15s; }
.service[open] summary .chevron { transform: rotate(180deg); }
.badge-icon { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: var(--radius-small); background: var(--active); color: var(--accent); flex: none; }
.service .name { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.pill { font-size: 12px; font-weight: 600; white-space: nowrap; color: var(--muted); }
.pill.up { color: var(--success); }
.pill.down { color: var(--danger); }
.pill.starting { color: var(--accent); }
.service dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; padding: 0 var(--pad) var(--pad); border-top: 1px solid var(--border); padding-top: var(--gap); }
.service dt { color: var(--muted); }
.service dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.service .actions { padding: 0 var(--pad) var(--pad); }

/* Диалог */
.dialog { display: flex; flex-direction: column; gap: var(--gap); width: 100%; max-width: 760px; margin: 0 auto; }
.msg { max-width: 86%; display: flex; flex-direction: column; gap: 8px; padding: 10px 12px; border-radius: var(--radius); }
.msg.user { align-self: flex-end; background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border-bottom-left-radius: 4px; }
.msg .who { font-size: 12px; color: var(--accent); font-weight: 600; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .transcript { font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .meta { display: flex; align-items: center; justify-content: flex-end; gap: 8px; font-size: 12px; color: var(--muted); }
.msg.user .meta { color: var(--text); opacity: .75; }
.msg .meta .failed { color: var(--danger); font-weight: 600; }
.permission { display: flex; flex-direction: column; gap: 8px; }
.permission pre { margin: 0; padding: 8px; max-height: 140px; overflow: auto; background: var(--bg); border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; }

/* Звуковая карточка */
.audio { display: grid; grid-template-columns: var(--touch) minmax(0, 1fr); gap: 4px 10px; align-items: center; width: min(280px, 62vw); }
.audio .play { grid-row: span 2; display: inline-flex; align-items: center; justify-content: center; width: var(--touch); height: var(--touch); border-radius: 50%; background: var(--primary); color: #fff; }
.audio .bars { position: relative; display: flex; align-items: center; gap: 2px; height: 28px; cursor: pointer; }
.audio .bars i { flex: 1; min-width: 1px; border-radius: 2px; background: var(--muted); height: 3px; }
.audio .bars i.played { background: var(--accent); }
.audio .info { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); }
.msg.user .audio .info { color: var(--text); opacity: .75; }

/* Поле ввода и запись */
.composer { flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 8px var(--pad); border-top: 1px solid var(--border); background: var(--bg); }
.composer textarea { flex: 1; min-height: var(--touch); max-height: 120px; resize: none; padding: 11px 14px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-small); font-size: 16px; line-height: 20px; }

.recorder { flex: none; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 8px var(--pad); border-top: 1px solid var(--border); background: var(--bg); }
.live { display: flex; align-items: center; gap: var(--gap); width: 100%; max-width: 420px; height: 44px; }
.live canvas { flex: 1; min-width: 0; height: 36px; }
.live span { font-size: 20px; font-variant-numeric: tabular-nums; min-width: 52px; text-align: right; }
#draft { width: 100%; max-width: 420px; min-height: 44px; display: flex; justify-content: center; }
.rec-row { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: var(--gap); width: 100%; max-width: 420px; }
.rec-row > .btn:first-child { justify-self: end; }
.rec-row > .btn:last-child { justify-self: start; }
.rec-row > .btn { min-width: 84px; }
.ring { width: 110px; height: 110px; border-radius: 50%; border: 2px solid var(--active); display: flex; align-items: center; justify-content: center; }
#mic { width: 86px; height: 86px; border-radius: 50%; background: var(--primary); color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; font-weight: 600; }
#mic svg { width: 34px; height: 34px; }
.recorder.recording #mic { background: var(--danger); color: var(--bg); }
.recorder.recording #mic svg { width: 26px; height: 26px; fill: currentColor; }
.recorder.recording .ring { border-color: var(--danger); }
#rec-label { color: var(--muted); text-align: center; min-height: 20px; }
.recorder.error #rec-label { color: var(--danger); }
.fallback { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; }
.fallback p { color: var(--muted); }

/* Переключатель */
.autoplay { flex-direction: row; align-items: center; }
.autoplay .grow { display: flex; flex-direction: column; }
/* Карточка голосового экрана стоит над прокручиваемой историей. */
.voice-top { flex: none; width: 100%; max-width: 792px; margin: 0 auto; padding: var(--gap) var(--pad) 0; }
.switch { position: relative; display: inline-flex; align-items: center; width: 52px; height: var(--touch); flex: none; cursor: pointer; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { width: 52px; height: 30px; border-radius: 15px; background: var(--border); transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 10px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: var(--text); transition: transform .15s; }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(22px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Журнал */
.logs { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.logs li { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-small); padding: 10px 12px; overflow-wrap: anywhere; }
.logs li.bad { border-color: var(--danger); }
.logs .head { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); }
.logs .error { color: var(--danger); }
.note { padding: 0 4px; }

/* Нижняя навигация */
.nav { flex: none; display: grid; grid-template-columns: repeat(4, 1fr); border-top: 1px solid var(--border); background: var(--bg); padding-bottom: var(--safe-bottom); }
.nav button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 56px; font-size: 12px; color: var(--muted); }
.nav button[aria-current="page"] { color: var(--accent); }
.keyboard .nav { display: none; }

/* Боковое меню */
.scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .55); }
.drawer { position: fixed; top: 0; bottom: 0; left: 0; z-index: 21; width: min(280px, 80vw); display: flex; flex-direction: column; gap: 4px; overflow-y: auto; padding: calc(var(--safe-top) + var(--pad)) 8px calc(var(--safe-bottom) + var(--pad)) calc(var(--safe-left) + 8px); background: var(--panel); border-right: 1px solid var(--border); transform: translateX(-100%); visibility: hidden; transition: transform .2s ease, visibility 0s .2s; }
.drawer.open { transform: none; visibility: visible; transition: transform .2s ease; }
.drawer h2 { padding: 0 8px 8px; }
.drawer button { display: flex; align-items: center; gap: var(--gap); min-height: var(--touch); padding: 0 var(--gap); border-radius: var(--radius-small); text-align: left; }
.drawer button[aria-current="page"] { background: var(--active); color: var(--accent); }

.modal { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center; padding: 24px; background: rgba(0, 0, 0, .6); }
.modal .card { max-width: 360px; width: 100%; }
.modal .actions { justify-content: flex-end; }

/* Низкий экран: панель записи компактнее, кнопка микрофона остаётся заданного размера */
@media (max-height: 640px) {
  .recorder { gap: 2px; padding-top: 4px; padding-bottom: 4px; }
  .live { height: 32px; }
  .live canvas { height: 26px; }
  .autoplay { padding-top: 8px; padding-bottom: 8px; }
  .autoplay small { display: none; }
}

/* Широкий экран */
@media (min-width: 700px) {
  .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .services { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
  section[data-view="settings"], section[data-view="logs"] { max-width: 760px; }
  .nav { grid-template-columns: repeat(4, 140px); justify-content: center; }
}

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