:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #f7f7fa;
  --text: #111114;
  --text-2: #5c5c66;
  --text-3: #8e8e96;
  --line: rgba(60, 60, 67, 0.14);
  --accent: #5b4cf0;
  --accent-2: #8b5cf6;
  --accent-soft: rgba(91, 76, 240, 0.11);
  --danger: #e5484d;
  --ok: #2f9e63;
  --warn: #c98a00;
  --bar: rgba(249, 249, 251, 0.86);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.05);
  --radius: 16px;
  --top-h: 52px;
  --tabs-h: 56px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --surface: #1c1c1f;
    --surface-2: #26262a;
    --text: #f4f4f6;
    --text-2: #b0b0b8;
    --text-3: #7d7d86;
    --line: rgba(255, 255, 255, 0.1);
    --accent: #8d81ff;
    --accent-2: #b394ff;
    --accent-soft: rgba(141, 129, 255, 0.16);
    --danger: #ff6369;
    --ok: #4cc38a;
    --warn: #f0b429;
    --bar: rgba(22, 22, 24, 0.86);
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #000000;
  --surface: #1c1c1f;
  --surface-2: #26262a;
  --text: #f4f4f6;
  --text-2: #b0b0b8;
  --text-3: #7d7d86;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #8d81ff;
  --accent-2: #b394ff;
  --accent-soft: rgba(141, 129, 255, 0.16);
  --danger: #ff6369;
  --ok: #4cc38a;
  --warn: #f0b429;
  --bar: rgba(22, 22, 24, 0.86);
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }

/* ───── каркас ───── */
.top {
  position: fixed; inset: 0 0 auto 0; z-index: 10;
  height: calc(var(--top-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  background: var(--bar);
  -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--line);
}
.top h1 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 56vw; }
.top-left { justify-self: start; }
.top-right { justify-self: end; display: flex; align-items: center; gap: 2px; }
.nav-btn {
  display: inline-flex; align-items: center; gap: 2px; min-height: 44px; padding: 0 8px;
  background: none; border: 0; color: var(--accent); font-size: 17px;
}
.nav-btn.icon { width: 44px; justify-content: center; padding: 0; }
.sync {
  width: 44px; height: 44px; border: 0; background: none; display: grid; place-items: center; padding: 0;
}
.sync::before {
  content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--text-3); transition: background .2s;
}
.sync.ok::before { background: var(--ok); }
.sync.error::before { background: var(--danger); }
.sync.offline::before { background: var(--warn); }
.sync.busy::before {
  width: 16px; height: 16px; background: none; border: 2px solid var(--accent-soft); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

main {
  min-height: 100%;
  padding: calc(var(--top-h) + env(safe-area-inset-top) + 12px) max(16px, env(safe-area-inset-right)) calc(var(--tabs-h) + env(safe-area-inset-bottom) + 24px) max(16px, env(safe-area-inset-left));
  max-width: 760px; margin: 0 auto; outline: none;
}

.tabs {
  position: fixed; inset: auto 0 0 0; z-index: 10;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--bar);
  -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--line);
}
.tab {
  border: 0; background: none; color: var(--text-3);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 10.5px; font-weight: 500;
}
.tab svg { width: 26px; height: 26px; }
.tab.active { color: var(--accent); }

#toast {
  position: fixed; left: 50%; z-index: 30; transform: translate(-50%, 20px);
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 14px);
  max-width: min(92vw, 460px); padding: 11px 16px; border-radius: 14px;
  background: rgba(30, 30, 34, 0.94); color: #fff; font-size: 14.5px; line-height: 1.35;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ───── общие элементы ───── */
h2.section {
  margin: 22px 4px 8px; font-size: 13px; font-weight: 600; letter-spacing: .02em;
  text-transform: uppercase; color: var(--text-3);
}
h2.section:first-child { margin-top: 4px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.pad { padding: 14px 16px; }
.muted { color: var(--text-3); }
.small { font-size: 13.5px; }
.center { text-align: center; }
.stack > * + * { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px; border-radius: 12px; border: 0;
  background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 16px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: none; color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14.5px; border-radius: 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 0 12px;
  border-radius: 999px; border: 0; background: var(--surface-2); color: var(--text-2); font-size: 14.5px;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }

.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 52px; padding: 10px 16px;
  background: none; border: 0; text-align: left; position: relative;
}
.list > li + li .row::before {
  content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 0.5px solid var(--line);
}
.list > li + li .row.has-icon::before { left: 60px; }
.row:active { background: var(--surface-2); }
.row .body { flex: 1; min-width: 0; }
.row .t { font-size: 16px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.row .s { display: block; font-size: 13px; color: var(--text-3); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .chev { color: var(--text-3); flex: none; }
.row .badge { flex: none; min-width: 24px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); font-size: 13px; text-align: center; }
.row .badge.hot { background: var(--accent); color: #fff; }
.ico {
  flex: none; width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-size: 17px;
}
.ico svg { width: 18px; height: 18px; }

.search {
  display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 12px; margin-bottom: 4px;
  background: var(--surface); border-radius: 12px; box-shadow: var(--shadow);
}
.search input { flex: 1; border: 0; background: none; outline: none; font-size: 16px; min-width: 0; }
.search svg { flex: none; color: var(--text-3); }

.empty { padding: 28px 16px; text-align: center; color: var(--text-3); }

/* ───── «Записать» ───── */
.compose { padding: 12px; }
.compose textarea {
  width: 100%; min-height: 128px; border: 0; outline: none; resize: none; background: none;
  font-size: 17px; line-height: 1.45; padding: 4px 4px 8px; display: block;
}
.compose .actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding-top: 8px; border-top: 0.5px solid var(--line); }
.compose .actions .grow { flex: 1; }
.compose .actions .btn { margin-left: auto; }
.toggle { display: inline-flex; align-items: center; gap: 8px; font-size: 14.5px; color: var(--text-2); min-height: 34px; user-select: none; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }

.link-card { display: flex; gap: 10px; align-items: center; margin: 4px 0 10px; padding: 8px; border-radius: 12px; background: var(--surface-2); }
.link-card img { width: 96px; height: 54px; object-fit: cover; border-radius: 8px; flex: none; background: var(--line); }
.link-card .lt { font-size: 14px; color: var(--text-2); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.voice { padding: 22px 16px 18px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.mic {
  --level: 0;
  position: relative; width: 84px; height: 84px; border-radius: 50%; border: 0;
  background: linear-gradient(145deg, var(--accent), var(--accent-2)); color: #fff;
  display: grid; place-items: center;
  box-shadow: 0 6px 20px rgba(91, 76, 240, 0.35);
}
.mic svg { width: 34px; height: 34px; }
.mic::after {
  content: ""; position: absolute; inset: -6px; border-radius: 50%; border: 3px solid var(--accent);
  opacity: 0; transform: scale(calc(1 + var(--level) * 0.35)); transition: transform .08s linear;
}
.mic.rec { background: var(--danger); box-shadow: 0 6px 20px rgba(229, 72, 77, 0.35); }
.mic.rec::after { opacity: .5; border-color: var(--danger); }
.voice .timer { font-variant-numeric: tabular-nums; font-size: 22px; font-weight: 600; }
.voice .hint { font-size: 14.5px; color: var(--text-3); text-align: center; }

.status { flex: none; font-size: 12.5px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.status.done { color: var(--ok); }
.status.error { color: var(--danger); }
.status.sending { color: var(--accent); }
.status.queued { color: var(--warn); }

/* ───── «База» ───── */
.topics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.topic {
  display: flex; flex-direction: column; align-items: flex-start; gap: 8px; min-height: 104px; padding: 14px;
  border: 0; text-align: left; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
}
.topic:active { transform: scale(.98); }
.topic .ico { width: 34px; height: 34px; font-size: 18px; }
.topic .n { font-weight: 600; font-size: 15.5px; line-height: 1.25; overflow-wrap: anywhere; }
.topic .c { font-size: 13px; color: var(--text-3); margin-top: auto; }
.banner { display: flex; align-items: center; gap: 12px; padding: 12px 14px; }
.banner .body { flex: 1; min-width: 0; }

/* ───── страница ───── */
.page-meta { display: flex; flex-wrap: wrap; gap: 8px; margin: 2px 0 14px; }
.doc { padding: 6px 18px 18px; overflow-wrap: anywhere; font-size: 16.5px; line-height: 1.55; }
.doc h1, .doc h2, .doc h3, .doc h4, .doc h5, .doc h6 { line-height: 1.25; margin: 1.3em 0 .45em; }
.doc h2 { font-size: 21px; }
.doc h3 { font-size: 18px; }
.doc h4, .doc h5, .doc h6 { font-size: 16.5px; }
.doc > :first-child { margin-top: 12px; }
.doc p { margin: .6em 0; }
.doc ul, .doc ol { margin: .5em 0; padding-left: 1.35em; }
.doc li { margin: .25em 0; }
.doc li > ul, .doc li > ol { margin: .2em 0; }
.doc blockquote { margin: .8em 0; padding: 4px 14px; border-left: 3px solid var(--accent); color: var(--text-2); background: var(--accent-soft); border-radius: 0 10px 10px 0; }
.doc code { font: 14px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; }
.doc pre { background: var(--surface-2); padding: 12px; border-radius: 12px; overflow-x: auto; }
.doc pre code { background: none; padding: 0; }
.doc img { max-width: 100%; height: auto; border-radius: 10px; }
.doc hr { border: 0; border-top: 0.5px solid var(--line); margin: 1.2em 0; }
.doc .table { overflow-x: auto; margin: .8em 0; }
.doc table { border-collapse: collapse; font-size: 15px; min-width: 100%; }
.doc th, .doc td { border-bottom: 0.5px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: top; }
.doc th { color: var(--text-2); font-weight: 600; }
.doc .check { color: var(--accent); }
.player { position: relative; aspect-ratio: 16 / 9; border-radius: var(--radius); overflow: hidden; background: #000; margin-bottom: 12px; }
.player iframe, .player img { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; object-fit: cover; }
.player .play { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.25); border: 0; color: #fff; }
.player .play svg { width: 64px; height: 64px; filter: drop-shadow(0 2px 8px rgba(0,0,0,.4)); }

.skeleton { height: 14px; border-radius: 7px; background: var(--surface-2); margin: 12px 0; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; } }

/* ───── «Видео» ───── */
.videos { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.video { border: 0; padding: 0; text-align: left; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.video .thumb { position: relative; aspect-ratio: 16 / 9; background: linear-gradient(135deg, var(--accent), var(--accent-2)); display: grid; place-items: center; color: #fff; }
.video .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.video .thumb svg { width: 30px; height: 30px; opacity: .9; }
.video .tag { position: absolute; left: 6px; top: 6px; padding: 2px 7px; border-radius: 999px; background: rgba(0,0,0,.6); color: #fff; font-size: 11.5px; }
.video .vb { padding: 8px 10px 10px; }
.video .vt { font-size: 14.5px; font-weight: 500; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.video .vs { font-size: 12.5px; color: var(--text-3); margin-top: 3px; }

.video .tag.ok { background: rgba(47, 158, 99, 0.88); }
.video .dur { position: absolute; right: 6px; bottom: 6px; padding: 1px 6px; border-radius: 6px; background: rgba(0,0,0,.72); color: #fff; font-size: 11.5px; font-variant-numeric: tabular-nums; }

.seg {
  display: flex; gap: 2px; padding: 2px; margin: 10px 0; border-radius: 11px; background: var(--surface-2);
  border: 0.5px solid var(--line);
}
.seg button {
  flex: 1; min-height: 34px; border: 0; border-radius: 9px; background: none; color: var(--text-2);
  font-size: 13.5px; font-weight: 500; padding: 0 4px; white-space: nowrap; min-width: 0;
}
@media (max-width: 380px) { .seg button { font-size: 12.5px; } .seg button span { font-size: 11.5px; } }
.seg button span { color: var(--text-3); font-size: 12.5px; font-weight: 400; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.chips.filters { flex-wrap: nowrap; overflow-x: auto; margin: 0 -16px 12px; padding: 0 16px 2px; scrollbar-width: none; }
.chips.filters::-webkit-scrollbar { display: none; }
.chips.filters .chip { flex: none; }
.chip.on { background: var(--accent); color: #fff; }
select.chip { appearance: none; -webkit-appearance: none; border: 0; padding-right: 26px; max-width: 60vw;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 14px) 52%, calc(100% - 9px) 52%; background-size: 5px 5px; background-repeat: no-repeat; }
select.chip:not(.on) { background-color: var(--surface-2); }
.vmeta { margin: 0 4px 2px; }
.notice { margin: 10px 0; line-height: 1.4; }
.notice .small { margin-top: 2px; }
.doc a[href*="youtube.com/watch"][href*="t="] {
  display: inline-block; padding: 0 6px; margin-right: 2px; border-radius: 6px; background: var(--accent-soft);
  font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.6;
}
.doc .check.tap { cursor: pointer; font-size: 19px; line-height: 1; vertical-align: -2px; user-select: none; }
.doc li.done { color: var(--text-3); text-decoration: line-through; }
.doc li.done .check { text-decoration: none; }

/* ───── чат ───── */
.chat { display: flex; flex-direction: column; gap: 10px; padding-bottom: 76px; }
.msg { max-width: 88%; padding: 10px 14px; border-radius: 18px; font-size: 16px; line-height: 1.45; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.model { align-self: flex-start; background: var(--surface); box-shadow: var(--shadow); border-bottom-left-radius: 6px; }
.msg.model .doc { padding: 0; font-size: 16px; }
.msg.model .doc > :first-child { margin-top: 0; }
.msg.model .doc > :last-child { margin-bottom: 0; }
.msg.error { align-self: flex-start; background: none; color: var(--danger); font-size: 14.5px; box-shadow: none; padding: 4px 6px; }
.msg .src { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.msg .src .chip { min-height: 28px; font-size: 13px; background: var(--accent-soft); color: var(--accent); }
.msg .tools { margin-top: 8px; display: flex; gap: 6px; }
.msg .tools .chip { min-height: 28px; font-size: 13px; }
.typing { align-self: flex-start; display: flex; gap: 5px; padding: 14px 16px; background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer {
  position: fixed; z-index: 9; left: 0; right: 0;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: var(--bar); -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--line);
}
.composer .ctx { max-width: 760px; margin: 0 auto 6px; }
.composer form { display: flex; align-items: flex-end; gap: 8px; max-width: 760px; margin: 0 auto; }
.composer textarea {
  flex: 1; min-height: 40px; max-height: 140px; resize: none; border: 0.5px solid var(--line); outline: none;
  border-radius: 20px; padding: 9px 14px; background: var(--surface); font-size: 16px; line-height: 1.35;
}
.send { flex: none; width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; }
.send:disabled { opacity: .4; }
.send svg { width: 20px; height: 20px; }

/* ───── настройки ───── */
.field { display: block; padding: 10px 16px; }
.field + .field { border-top: 0.5px solid var(--line); }
.field span { display: block; font-size: 13px; color: var(--text-3); margin-bottom: 4px; }
.field.toggle { display: flex; color: var(--text); font-size: 16px; }
.field.toggle input { flex: none; width: 20px; }
.field input:not([type="checkbox"]), .field select { width: 100%; border: 0; outline: none; background: none; font-size: 16px; padding: 2px 0; }
details.help { padding: 0; }
details.help summary { list-style: none; padding: 14px 16px; font-weight: 500; cursor: pointer; }
details.help summary::-webkit-details-marker { display: none; }
details.help summary::after { content: "›"; float: right; color: var(--text-3); transition: transform .2s; }
details.help[open] summary::after { transform: rotate(90deg); }
details.help .doc { padding-top: 0; font-size: 15px; }
.code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; overflow-wrap: anywhere; user-select: all; }

@media (min-width: 640px) {
  .topics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
