/* Switchboard — пульт агентов. Mobile-first; внутри Telegram берёт цвета темы Mini App. */
:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1e222b;
  --line: #262b36;
  --text: #e8eaef;
  --muted: #8a92a3;
  --accent: #6ea8fe;
  --accent-text: #0b1220;
  --mine: #23406e;
  --mine-text: #eaf1ff;
  --lamp-on: #34d399;
  --lamp-wait: #f5b942;
  --lamp-off: #525a6b;
  --danger: #f87171;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f3f4f7; --surface: #ffffff; --surface-2: #eef0f4; --line: #e1e4ea; --text: #151821; --muted: #6b7280;
    --accent: #2f6fe0; --accent-text: #ffffff; --mine: #dbe8ff; --mine-text: #10213f; --lamp-off: #b6bcc8; color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f3f4f7; --surface: #ffffff; --surface-2: #eef0f4; --line: #e1e4ea; --text: #151821; --muted: #6b7280;
  --accent: #2f6fe0; --accent-text: #ffffff; --mine: #dbe8ff; --mine-text: #10213f; --lamp-off: #b6bcc8; color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.4 var(--font); -webkit-text-size-adjust: 100%; }
body { overscroll-behavior-y: none; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
#app { min-height: 100dvh; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; }

/* --- шапка --- */
.bar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); min-height: 54px; }
.bar h1 { font-size: 17px; margin: 0; font-weight: 650; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { border: 0; background: transparent; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; cursor: pointer; flex: none; color: var(--text); }
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }

/* --- фильтры --- */
.filters { display: flex; gap: 6px; padding: 10px 12px 4px; overflow-x: auto; scrollbar-width: none; }
.chip { border: 1px solid var(--line); background: var(--surface); padding: 6px 12px; border-radius: 999px; white-space: nowrap; cursor: pointer; font-size: 13px; }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.search { margin: 6px 12px; display: flex; }
.search input { flex: 1; padding: 9px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); outline: none; }
.search input:focus { border-color: var(--accent); }

/* --- список топиков --- */
.list { padding: 4px 8px calc(16px + var(--safe-b)); }
.row { display: grid; grid-template-columns: 14px 1fr auto; gap: 10px; align-items: center; padding: 12px 10px; border-radius: var(--radius); cursor: pointer; text-decoration: none; color: inherit; }
.row:hover, .row:focus-visible { background: var(--surface); outline: none; }
.row + .row { border-top: 1px solid var(--line); }
.row:hover + .row { border-top-color: transparent; }
.row .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .meta { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.row .right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.row .time { font-size: 12px; color: var(--muted); }
.badges { display: flex; gap: 4px; }
.badge { min-width: 22px; height: 22px; padding: 0 7px; border-radius: 999px; font-size: 12px; font-weight: 650; display: inline-grid; place-items: center; background: var(--surface-2); color: var(--text); }
.badge.unread { background: var(--accent); color: var(--accent-text); }
.badge.owed { background: transparent; border: 1px solid var(--lamp-wait); color: var(--lamp-wait); }
.badge.urgent { background: var(--danger); color: #fff; }
.wake-btn { border: 0; border-radius: 999px; padding: 5px 11px; font-size: 12.5px; font-weight: 650; background: var(--lamp-wait); color: #1d1503; cursor: pointer; }
.wake-btn[disabled] { opacity: .55; cursor: default; }

.lamp { width: 10px; height: 10px; border-radius: 50%; background: var(--lamp-off); box-shadow: 0 0 0 3px color-mix(in srgb, var(--lamp-off) 20%, transparent); }
.lamp.listening { background: var(--lamp-on); box-shadow: 0 0 0 3px color-mix(in srgb, var(--lamp-on) 25%, transparent), 0 0 10px var(--lamp-on); }
.lamp.busy { background: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.lamp.waking { background: var(--lamp-wait); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--lamp-wait) 0%, transparent); opacity: .6; } }
@media (prefers-reduced-motion: reduce) { .lamp.waking { animation: none; } }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.section-label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 14px 12px 4px; }

/* --- чат --- */
.chat { position: fixed; inset: 0; max-width: 860px; margin: 0 auto; display: flex; flex-direction: column; background: var(--bg); }
.chat .bar { position: static; }
.banner { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: color-mix(in srgb, var(--lamp-wait) 14%, var(--surface)); border-bottom: 1px solid var(--line); font-size: 13.5px; }
.banner .grow { flex: 1; }
.banner.stop { background: color-mix(in srgb, var(--danger) 14%, var(--surface)); }
.feed { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 10px 8px; display: flex; flex-direction: column; gap: 6px; overscroll-behavior: contain; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--surface); padding: 3px 10px; border-radius: 999px; margin: 8px 0; }
.msg { max-width: min(86%, 620px); padding: 8px 11px 6px; border-radius: 16px; background: var(--surface); position: relative; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in { align-self: flex-end; background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 6px; }
.msg.out { align-self: flex-start; border-bottom-left-radius: 6px; }
.msg.service { align-self: center; background: transparent; border: 1px dashed var(--line); color: var(--muted); font-size: 13px; max-width: 92%; }
.msg.withdrawn { opacity: .5; }
.relay-banner { width: calc(100% - 24px); margin: 8px 12px 0; border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer; text-align: left; color: var(--text); }
.relay { padding: 10px; border-radius: 12px; background: var(--surface-2); margin: 0 4px 10px; }
.relay-head { font-size: 14px; margin-bottom: 6px; }
.relay-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; max-height: 40dvh; overflow-y: auto; margin-bottom: 10px; }
.rule { display: grid; grid-template-columns: 1fr auto 1fr; gap: 6px; align-items: center; margin-bottom: 8px; }
.rule select { padding: 7px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); min-width: 0; }
.rule select[data-ra] { grid-column: 1 / 3; }
.rule .btn { padding: 6px 10px; }
.msg.focus { outline: 2px solid var(--lamp-wait); outline-offset: 2px; transition: outline-color 1s; }
.file small { margin-left: auto; flex: none; }
.msg.withdrawn .body { text-decoration: line-through; }
.msg .who { font-size: 12px; font-weight: 650; color: var(--accent); margin-bottom: 2px; }
.msg.in .who { color: inherit; opacity: .75; }
.msg .body { white-space: pre-wrap; }
.msg .body pre { white-space: pre-wrap; background: color-mix(in srgb, var(--bg) 70%, transparent); padding: 8px; border-radius: 8px; font: 12.5px/1.45 var(--mono); margin: 4px 0; overflow-x: auto; }
.msg .body code { font: 13px var(--mono); background: color-mix(in srgb, var(--bg) 55%, transparent); padding: 1px 4px; border-radius: 4px; }
.msg .body blockquote { margin: 4px 0; padding: 2px 0 2px 9px; border-left: 3px solid var(--accent); opacity: .9; }
.msg .body tg-spoiler, .msg .body .spoiler { background: var(--muted); color: transparent; border-radius: 3px; cursor: pointer; }
.msg .body tg-spoiler.open { background: transparent; color: inherit; }
.msg .foot { display: flex; justify-content: flex-end; align-items: center; gap: 6px; font-size: 11.5px; opacity: .7; margin-top: 3px; }
.msg .reply { font-size: 12.5px; border-left: 3px solid currentColor; padding: 2px 8px; margin-bottom: 4px; opacity: .75; border-radius: 4px; background: color-mix(in srgb, currentColor 7%, transparent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg img.media, .msg video.media { display: block; max-width: 100%; max-height: 360px; border-radius: 10px; margin: 2px 0 4px; background: var(--surface-2); }
.file { display: flex; gap: 8px; align-items: center; padding: 8px; border-radius: 10px; background: color-mix(in srgb, var(--bg) 40%, transparent); text-decoration: none; color: inherit; margin: 2px 0 4px; font-size: 13.5px; }
.file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kb { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.kb button { flex: 1 1 auto; border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px; padding: 7px 10px; cursor: pointer; font-size: 13.5px; }
.kb button.chosen { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.kb button:disabled:not(.chosen) { opacity: .45; cursor: default; }
.stage { font-size: 11.5px; }
.err { color: var(--danger); }
.more { align-self: center; border: 0; background: var(--surface); padding: 6px 14px; border-radius: 999px; color: var(--muted); cursor: pointer; font-size: 13px; }

.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 8px calc(8px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg); }
.composer textarea { flex: 1; resize: none; max-height: 40dvh; min-height: 42px; padding: 10px 12px; border-radius: 20px; border: 1px solid var(--line); background: var(--surface); outline: none; line-height: 1.35; }
.composer textarea:focus { border-color: var(--accent); }
.send { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; cursor: pointer; flex: none; }
.send svg { width: 20px; height: 20px; }
.replying { display: flex; align-items: center; gap: 8px; padding: 6px 12px; font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); }
.replying span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- нижние листы --- */
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .45); z-index: 30; display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: min(560px, 100%); background: var(--surface); border-radius: 18px 18px 0 0; padding: 8px 8px calc(12px + var(--safe-b)); max-height: 85dvh; overflow-y: auto; animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(24px); opacity: .6; } }
.sheet .grip { width: 38px; height: 4px; border-radius: 4px; background: var(--line); margin: 4px auto 10px; }
.sheet h3 { margin: 4px 10px 10px; font-size: 15px; }
.sheet .item { display: flex; align-items: center; gap: 12px; width: 100%; padding: 13px 12px; border: 0; background: transparent; border-radius: 12px; text-align: left; cursor: pointer; text-decoration: none; color: var(--text); }
.sheet .item:hover { background: var(--surface-2); }
.sheet .item small { display: block; color: var(--muted); font-size: 12.5px; }
.sheet .item.danger { color: var(--danger); }

/* --- админка --- */
.tabs { display: flex; gap: 4px; padding: 8px 12px; overflow-x: auto; scrollbar-width: none; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: transparent; padding: 7px 12px; border-radius: 10px; cursor: pointer; white-space: nowrap; color: var(--muted); }
.tab[aria-selected="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; }
.panel { padding: 12px 12px calc(24px + var(--safe-b)); display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.card h4 { margin: 0 0 10px; font-size: 14px; }
.kv { display: grid; grid-template-columns: minmax(110px, 38%) 1fr; gap: 6px 10px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; }
.field > span { color: var(--muted); }
.field input, .field textarea, .field select { padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); outline: none; width: 100%; }
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; margin-bottom: 10px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }
.btn { border: 1px solid var(--line); background: var(--surface-2); padding: 8px 14px; border-radius: 10px; cursor: pointer; font-size: 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.checks { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; font-size: 13.5px; }
.checks li { display: flex; gap: 8px; }
.route { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.route:first-of-type { border-top: 0; }
.route .u { font: 12px var(--mono); color: var(--muted); overflow-wrap: anywhere; }
.route .st { font-size: 12px; text-align: right; }
.ok { color: var(--lamp-on); }
.bad { color: var(--danger); }
.logs { font: 12px/1.45 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg); border-radius: 10px; padding: 10px; max-height: 60dvh; overflow-y: auto; }
.logs .warn { color: var(--lamp-wait); }
.logs .error { color: var(--danger); }
.mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }

/* --- вход --- */
.login { margin: auto; width: min(360px, 100% - 32px); padding: 28px 0; display: flex; flex-direction: column; gap: 14px; }
.login h1 { margin: 0; font-size: 22px; }
.login p { margin: 0; color: var(--muted); font-size: 14px; }

/* --- просмотр медиа --- */
.media-btn { display: block; border: 0; padding: 0; background: none; cursor: zoom-in; max-width: 100%; }
.media-wrap { position: relative; }
.media-wrap .expand { position: absolute; top: 6px; right: 6px; border: 0; border-radius: 8px; background: rgb(0 0 0 / .55); color: #fff; width: 32px; height: 32px; cursor: pointer; font-size: 16px; }
.kb-link { flex: 1 1 auto; border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px; padding: 7px 10px; font-size: 13.5px; text-align: center; text-decoration: none; }
#viewer { position: fixed; inset: 0; z-index: 60; background: rgb(0 0 0 / .94); color: #fff; display: flex; flex-direction: column; }
#viewer[hidden] { display: none; }
.viewer-bar { display: flex; align-items: center; gap: 6px; padding: calc(8px + env(safe-area-inset-top, 0px)) 8px 8px 14px; }
.viewer-bar span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.viewer-bar .icon-btn { color: #fff; text-decoration: none; font-size: 18px; }
.viewer-body { flex: 1; min-height: 0; display: grid; place-items: center; padding: 0 0 calc(8px + var(--safe-b)); touch-action: pan-y; }
.viewer-body img, .viewer-body video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-body iframe { width: 100%; height: 100%; border: 0; background: #fff; }
.viewer-text { width: 100%; height: 100%; margin: 0; overflow: auto; padding: 12px; font: 12.5px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; color: #e8eaef; background: #0f1115; }
.viewer-nav { position: absolute; top: 50%; transform: translateY(-50%); border: 0; background: rgb(255 255 255 / .12); color: #fff; width: 42px; height: 64px; border-radius: 12px; font-size: 28px; cursor: pointer; }
.viewer-nav.prev { left: 6px; } .viewer-nav.next { right: 6px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 4px; padding: 0 6px 10px; }
.tile { position: relative; aspect-ratio: 1; border: 0; padding: 0; border-radius: 8px; overflow: hidden; background: var(--surface-2); cursor: pointer; }
.tile img, .tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .play { position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font-size: 26px; text-shadow: 0 1px 6px #000; }
.doclist .item svg { flex: none; }

#toast { position: fixed; left: 50%; bottom: calc(84px + var(--safe-b)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 12px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; max-width: calc(100% - 32px); }
#toast.show { opacity: .95; }

@media (min-width: 720px) {
  .list { padding: 8px 16px; }
  .msg { max-width: 72%; }
}
