:root {
  --bg: var(--tg-theme-bg-color, #fff);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #9aa3ad);
  --link: var(--tg-theme-link-color, #2f86eb);
  --button: var(--tg-theme-button-color, #2f86eb);
  --button-text: var(--tg-theme-button-text-color, #fff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f3f5);
  --card: var(--tg-theme-section-bg-color, #fff);
  --border: color-mix(in srgb, var(--hint) 24%, transparent);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text);
  font: 17px/1.5 -apple-system, "Segoe UI", Roboto, sans-serif; }
body { display: flex; flex-direction: column; min-height: 100vh; }

/* ---------- верхняя панель ---------- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; background: var(--bg); border-bottom: 1px solid var(--border); }
.icon-btn { width: 36px; height: 36px; flex: none; border: none; background: transparent;
  color: var(--text); font-size: 26px; line-height: 1; border-radius: 50%; cursor: pointer; }
.icon-btn:active { background: var(--secondary-bg); }
.topbar-spacer { flex: 1; }
.topbar-title { flex: 1; min-width: 0; }
.topbar-sub { font-size: 12px; color: var(--hint); }
.topbar-name { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-next { flex: none; padding: 8px 18px; font-size: 15px; font-weight: 600; color: var(--button-text);
  background: var(--button); border: none; border-radius: 20px; cursor: pointer; }
.btn-next:disabled { opacity: .5; }

main { flex: 1; padding: 12px 14px 90px; }
.editor { padding-left: 20px; }   /* место под ручку блока слева */
.status { min-height: 18px; margin-top: 6px; font-size: 13px; color: var(--hint); }
.status.ok { color: #2e9e5b; }
.status.err { color: #e0524d; }

/* ---------- блок (без рамок) ---------- */
.block { position: relative; margin: 2px 0; }
.block.dragging { opacity: .4; }
.drag-handle { position: absolute; left: -22px; top: 3px; width: 20px; height: 24px;
  display: flex; align-items: center; justify-content: center; color: var(--hint);
  font-size: 15px; cursor: grab; opacity: .3; border-radius: 6px; transition: opacity .12s; user-select: none; }
.block:hover > .drag-handle, .block:focus-within > .drag-handle { opacity: .7; }
.drag-handle:active { background: var(--secondary-bg); }
.block-body { min-width: 0; }

/* ---------- editable ---------- */
.editable { outline: none; min-height: 1.5em; white-space: pre-wrap; word-break: break-word; padding: 3px 0; }
.editable.is-empty::before { content: attr(data-ph); color: var(--hint); pointer-events: none; }
.editable.heading { font-weight: 700; line-height: 1.25; padding: 4px 0; }
.editable.h1 { font-size: 25px; }
.editable.h2 { font-size: 21px; }
.editable.h3 { font-size: 18px; }
.block[data-type="quote"] .block-body { border-left: 3px solid var(--border); padding-left: 12px; }
.block[data-type="quote"] .editable { color: var(--text); }

.editable code { background: var(--secondary-bg); padding: 1px 5px; border-radius: 5px;
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9em; }
.editable a { color: var(--link); }
.editable .spoiler { background: var(--hint); color: var(--hint); border-radius: 4px; }

/* ---------- списки ---------- */
.list { display: flex; flex-direction: column; gap: 1px; }
.list-item { display: flex; align-items: baseline; gap: 8px; }
.list-item .editable { flex: 1; }
.block[data-type="bulleted_list"] .list-item::before { content: "•"; color: var(--hint); }
.block[data-type="numbered_list"] .list { counter-reset: li; }
.block[data-type="numbered_list"] .list-item { counter-increment: li; }
.block[data-type="numbered_list"] .list-item::before { content: counter(li) "."; color: var(--hint); min-width: 20px; }
.li-check { margin: 0; align-self: center; width: 19px; height: 19px; flex: none; }
.add-item { margin-top: 4px; background: transparent; border: none; color: var(--link);
  font-size: 14px; padding: 4px 2px; cursor: pointer; }

/* ---------- код ---------- */
.code-lang { width: 100%; margin-bottom: 6px; padding: 7px 10px; font-size: 13px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 8px; outline: none; }
.code-text { width: 100%; padding: 10px 12px; font: 13px/1.45 ui-monospace, Menlo, Consolas, monospace;
  color: var(--text); background: var(--secondary-bg); border: none; border-radius: 10px;
  outline: none; resize: vertical; }
.divider-line { height: 0; border-top: 2px solid var(--border); margin: 10px 0; }

/* ---------- картинка / медиа ---------- */
.img-preview { margin-bottom: 6px; }
.img-preview img { max-width: 100%; border-radius: 12px; display: block; }
.img-controls, .media-controls { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.img-url, .media-url { flex: 1; min-width: 120px; padding: 8px 12px; font-size: 14px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 10px; outline: none; }
.img-caption, .field-mini { width: 100%; margin-top: 8px; padding: 8px 12px; font-size: 14px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 10px; outline: none; }
.img-status, .media-status { margin-top: 6px; font-size: 13px; color: var(--hint); }
.btn-mini { display: inline-block; padding: 8px 14px; font-size: 14px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 10px; cursor: pointer; }
.audio-apply { margin-top: 8px; color: var(--link); }
.media-preview { margin: 8px 0; }
.media-preview video { max-width: 100%; border-radius: 12px; display: block; }
.media-preview audio { width: 100%; }

/* ---------- сворачиваемый ---------- */
.details-summary { width: 100%; margin-bottom: 6px; padding: 8px 12px; font-size: 16px; font-weight: 600;
  color: var(--text); background: var(--secondary-bg); border: none; border-radius: 10px; outline: none; }
.details-body { padding: 4px 12px; border-left: 3px solid var(--border); }

/* ---------- таблица ---------- */
.table-wrap { overflow-x: auto; border-radius: 10px; }
table.tbl-edit { border-collapse: collapse; width: 100%; }
table.tbl-edit td { border: 1px solid var(--border); padding: 0; min-width: 56px; vertical-align: top; }
.tbl-cell { padding: 7px 9px; min-height: 1.4em; }
.tbl-header-toggle { display: flex; align-items: center; gap: 6px; margin-top: 8px; font-size: 13px; color: var(--hint); }
.tbl-controls { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.tg-math { display: inline-block; padding: 0 5px; font-family: ui-monospace, Menlo, Consolas, monospace;
  background: color-mix(in srgb, var(--link) 16%, transparent); border-radius: 5px; }
.tg-emoji { display: inline-block; }

/* ---------- нижний тулбар ---------- */
.bottombar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; align-items: center;
  gap: 4px; padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); background: var(--bg);
  border-top: 1px solid var(--border); }
.bb-btn { width: 44px; height: 40px; border: none; background: transparent; color: var(--text);
  font-size: 19px; border-radius: 10px; cursor: pointer; }
.bb-btn:active { background: var(--secondary-bg); }
.bb-btn[data-bb="add"] { color: var(--link); font-size: 24px; }

/* ---------- меню блока (popover) ---------- */
.popover { position: fixed; z-index: 60; display: flex; flex-direction: column; min-width: 160px;
  padding: 6px; background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0,0,0,.18); }
.popover[hidden] { display: none; }
.popover button { text-align: left; padding: 10px 12px; font-size: 15px; color: var(--text);
  background: transparent; border: none; border-radius: 8px; cursor: pointer; }
.popover button:active { background: var(--secondary-bg); }
.popover button.danger { color: #e0524d; }

/* ---------- инлайн-тулбар ---------- */
.inline-toolbar { position: fixed; z-index: 50; display: flex; gap: 1px; padding: 4px;
  background: #2b2f36; border-radius: 12px; box-shadow: 0 6px 22px rgba(0,0,0,.32); }
.inline-toolbar[hidden] { display: none; }
.inline-toolbar button { min-width: 32px; height: 32px; border: none; background: transparent;
  color: #fff; font-size: 14px; border-radius: 8px; cursor: pointer; }
.inline-toolbar button:active { background: rgba(255,255,255,.16); }

/* ---------- нижние листы ---------- */
.sheet { position: fixed; inset: 0; z-index: 60; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-height: 80vh; overflow-y: auto;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 8px 14px calc(16px + env(safe-area-inset-bottom)); }
.sheet-handle { width: 38px; height: 4px; margin: 6px auto 12px; background: var(--border); border-radius: 3px; }
.sheet-title { font-size: 15px; font-weight: 600; margin: 2px 2px 12px; }
.sheet-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sheet-grid button { display: flex; align-items: center; gap: 10px; padding: 13px 12px; font-size: 15px;
  text-align: left; color: var(--text); background: var(--secondary-bg); border: none; border-radius: 12px; cursor: pointer; }
.sheet-grid .gi { display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px;
  flex: none; font-size: 14px; font-weight: 600; color: var(--link);
  background: color-mix(in srgb, var(--link) 14%, transparent); border-radius: 7px; }
.sheet-cancel { width: 100%; margin-top: 12px; padding: 13px; font-size: 15px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 12px; cursor: pointer; }
.channel-select { width: 100%; padding: 12px; font-size: 16px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 12px; outline: none; margin-bottom: 10px; }

.btn { width: 100%; margin-top: 8px; padding: 14px; font-size: 16px; font-weight: 600;
  border: none; border-radius: 12px; cursor: pointer; }
.btn-primary { background: var(--button); color: var(--button-text); }
.btn-secondary { background: var(--secondary-bg); color: var(--text); }
.btn:disabled { opacity: .5; }

/* ---------- модалки ---------- */
.modal { position: fixed; inset: 0; z-index: 70; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.modal-panel { position: relative; width: 100%; max-width: 360px; background: var(--bg); border-radius: 16px; padding: 16px; }
.modal-title { font-size: 15px; font-weight: 600; margin-bottom: 10px; }
.modal-input { width: 100%; padding: 11px 12px; font-size: 16px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 10px; outline: none; }
.modal-actions { display: flex; gap: 8px; margin-top: 12px; }
.modal-actions .btn { margin-top: 0; }

/* ---------- палитра эмодзи ---------- */
.emoji-palette { position: fixed; z-index: 55; display: flex; flex-wrap: wrap; gap: 4px; max-width: 240px;
  padding: 8px; background: var(--card); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.22); }
.emoji-palette[hidden] { display: none; }
.emoji-palette button { min-width: 36px; height: 36px; font-size: 20px; border: none; background: var(--secondary-bg); border-radius: 9px; cursor: pointer; }
.emoji-empty { font-size: 13px; color: var(--hint); max-width: 200px; padding: 2px; }

/* ---------- гейт подписки ---------- */
.gate { position: fixed; inset: 0; z-index: 80; display: flex; align-items: center; justify-content: center;
  padding: 24px; background: var(--bg); }
.gate[hidden] { display: none; }
.gate-card { max-width: 360px; width: 100%; text-align: center; }
.gate-emoji { font-size: 46px; }
.gate-title { font-size: 21px; font-weight: 700; margin: 10px 0 6px; }
.gate-text { color: var(--hint); margin-bottom: 18px; }
.gate-status { margin-top: 10px; text-align: center; }

/* ---------- экраны / меню / списки ---------- */
.screen { display: flex; flex-direction: column; min-height: 100vh; }
.screen[hidden] { display: none; }
.menu-wrap { width: 100%; max-width: 480px; margin: 0 auto; padding: 24px 18px;
  display: flex; flex-direction: column; gap: 12px; }
.menu-title { font-size: 22px; font-weight: 700; text-align: center; margin: 16px 0 20px; }
.menu-btn { width: 100%; padding: 18px; font-size: 17px; font-weight: 600; text-align: left;
  color: var(--text); background: var(--secondary-bg); border: none; border-radius: 14px; cursor: pointer; }
.menu-btn.primary { background: var(--button); color: var(--button-text); }
.menu-btn:active { opacity: .85; }
.bb-spacer { flex: 1; }

.list-title { font-size: 17px; font-weight: 600; }
.list-items { display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; }
.list-empty { color: var(--hint); text-align: center; padding: 48px 16px; }
.doc-card { display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  background: var(--secondary-bg); border-radius: 12px; }
.doc-main { flex: 1; min-width: 0; cursor: pointer; }
.doc-preview { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-date { font-size: 12px; color: var(--hint); margin-top: 2px; }
.doc-actions { display: flex; gap: 6px; flex: none; }
.art-item { width: 100%; text-align: left; padding: 12px 14px; font-size: 15px; color: var(--text);
  background: var(--secondary-bg); border: none; border-radius: 12px; cursor: pointer; }
.art-item:active { opacity: .85; }
