:root {
  --green: #4ade80;
  --green-d: #22a85a;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e6e9ee;
  --bg: #f4f6f8;
  --card: #ffffff;
  --tg-link: #168acd;
  --tg-mark: #fff2a8;
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 18px;
  background: var(--card); border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo {
  width: 38px; height: 38px; border-radius: 10px;
  display: grid; place-items: center; font-size: 20px; color: #053d22;
  background: linear-gradient(135deg, var(--green), var(--green-d));
  box-shadow: 0 2px 8px rgba(34,168,90,.35);
}
.brand-title { font-weight: 700; font-size: 15px; line-height: 1.1; }
.brand-sub { font-size: 12px; color: var(--muted); }
.brand-sub.live b { color: var(--green-d); }

.top-actions { display: flex; align-items: center; gap: 8px; }
.chat-field { display: flex; align-items: stretch; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: #fff; }
.chat-input { border: 0; padding: 8px 10px; font-size: 13px; width: 190px; outline: none; }
.chat-field .btn { border-radius: 0; border: 0; border-left: 1px solid var(--line); }

.btn {
  font: inherit; font-size: 13px; font-weight: 600;
  padding: 8px 14px; border-radius: 9px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); cursor: pointer; white-space: nowrap;
  transition: background .12s, transform .05s, box-shadow .12s;
}
.btn:hover { background: #f7f9fb; }
.btn:active { transform: translateY(1px); }
.btn.small { padding: 6px 10px; font-size: 12px; }
.btn.ghost { background: #fff; color: var(--muted); }
.btn.primary {
  background: linear-gradient(135deg, var(--green), var(--green-d));
  border-color: transparent; color: #04331c;
  box-shadow: 0 2px 10px rgba(34,168,90,.35);
}
.btn.primary:hover { filter: brightness(1.03); background: linear-gradient(135deg, var(--green), var(--green-d)); }
.btn.primary:disabled { background: #cbd5e1; color: #fff; box-shadow: none; cursor: not-allowed; }

/* mode switch */
.mode-switch { display: inline-flex; background: #eef2f6; border-radius: 9px; padding: 3px; gap: 2px; }
.mode-switch button {
  border: 0; background: transparent; font: inherit; font-size: 12.5px; font-weight: 600;
  color: var(--muted); padding: 6px 12px; border-radius: 7px; cursor: pointer;
}
.mode-switch button.active { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

/* target switch (в канал / себе в бота) */
.target-switch { display: inline-flex; background: #eef2f6; border-radius: 8px; padding: 2px; gap: 2px; }
.target-switch button {
  border: 0; background: transparent; font: inherit; font-size: 12px; font-weight: 600;
  color: var(--muted); padding: 5px 10px; border-radius: 6px; cursor: pointer; white-space: nowrap;
}
.target-switch button.active { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.1); }
.conn-self-hint { font-size: 12px; color: var(--muted); }
.conn-self-hint a { color: var(--green-d); }
.conn-self-hint.hidden, .chat-field.hidden { display: none; }

/* connection bar (token + chat target) */
.connbar { background: #fff; border-bottom: 1px solid var(--line); padding: 8px 18px; }
.conn-byo { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.conn-byo.hidden, .conn-demo.hidden { display: none; }
.conn-input {
  border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; font: inherit; font-size: 13px;
  width: 280px; outline: none; background: #fff;
}
.conn-input:focus { border-color: var(--green); }
.conn-status { font-size: 12px; color: var(--muted); }
.conn-status.ok { color: var(--green-d); font-weight: 600; }
.conn-status.err { color: #dc2626; font-weight: 600; }
.conn-note { font-size: 11.5px; color: #94a3b8; margin-left: auto; cursor: help; }
.conn-forget { border: 0; background: none; color: #94a3b8; font: inherit; font-size: 11.5px; text-decoration: underline; cursor: pointer; padding: 0; }
.conn-forget:hover { color: #dc2626; }
.conn-demo { font-size: 13px; color: var(--muted); }
.conn-demo b { color: var(--ink); }

.howto { grid-column: 1 / -1; background: #f7faf8; border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; font-size: 13px; color: #334155; }
.howto.hidden { display: none; }
.howto ol { margin: 6px 0 0; padding-left: 20px; }
.howto li { margin: 4px 0; }
.howto code { background: #eef2f6; border-radius: 4px; padding: 1px 5px; font-family: var(--mono); font-size: 12px; }
.chats-hint { font-size: 12px; color: var(--muted); }

/* ---------- settings ---------- */
.settings {
  background: #fff; border-bottom: 1px solid var(--line);
  padding: 14px 18px; display: grid; gap: 12px;
  grid-template-columns: 1fr 1fr; align-items: center;
}
.settings.hidden { display: none; }
.set-row { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.set-row.inline { flex-direction: row; align-items: center; gap: 8px; }
.set-row small { color: var(--muted); font-weight: 400; }
.set-row input[type="password"] { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; }
.set-row.buttons { flex-direction: row; gap: 8px; grid-column: 1 / -1; flex-wrap: wrap; }
.chats-list { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; }
.chats-list.hidden { display: none; }
.chat-chip { font-size: 12px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 20px; background: #f7f9fb; cursor: pointer; }
.chat-chip:hover { border-color: var(--green); }
.chat-chip small { color: var(--muted); }

/* ---------- workspace ---------- */
.workspace { flex: 1 1 auto; display: grid; grid-template-columns: 1fr 1fr; min-height: 0; }
.pane { min-height: 0; min-width: 0; display: flex; flex-direction: column; }
.editor-pane { border-right: 1px solid var(--line); background: #fff; }

/* toolbar */
.toolbar {
  display: flex; flex-wrap: wrap; gap: 3px; padding: 8px 10px;
  border-bottom: 1px solid var(--line); background: #fbfcfd; flex: 0 0 auto;
}
.tool {
  min-width: 30px; height: 30px; padding: 0 8px;
  border: 1px solid transparent; border-radius: 7px; background: transparent;
  font-size: 13px; color: #334155; cursor: pointer; display: grid; place-items: center;
}
.tool:hover { background: #eef2f6; border-color: var(--line); }
.tool b, .tool i, .tool u, .tool s { pointer-events: none; }
.tool-sep { width: 1px; align-self: stretch; background: var(--line); margin: 4px 4px; }

/* WYSIWYG editor — reuses .tg-content typography, but is editable */
#editor.editor {
  flex: 1 1 auto; width: 100%; overflow-y: auto; outline: none;
  padding: 18px 22px; background: #fff; font-size: 15px; line-height: 1.5; color: #1c2733;
}
#editor.editor:empty::before { content: attr(data-placeholder); color: #9aa6b2; }
#editor.editor:focus { outline: none; }
/* in the EDITOR a spoiler must stay visible & editable (it's hidden only in the preview) */
#editor.editor tg-spoiler { background: #e3e8ee; color: inherit; border-radius: 4px; padding: 0 2px; }
/* checklist inside the editor (preview uses real <input type=checkbox>) */
#editor.editor ul.checklist { list-style: none; padding-left: 2px; }
#editor.editor ul.checklist li { position: relative; }
#editor.editor .cb {
  display: inline-block; width: 18px; height: 18px; vertical-align: -4px; margin-right: 4px;
  border: 2px solid #c3ccd6; border-radius: 5px; cursor: pointer; user-select: none; text-align: center;
  line-height: 15px; font-size: 13px; color: #fff;
}
#editor.editor ul.checklist li[data-checked="1"] .cb { background: #3892d6; border-color: #3892d6; }
#editor.editor ul.checklist li[data-checked="1"] .cb::after { content: "✓"; }
#editor.editor table { cursor: text; }
#editor.editor td, #editor.editor th { min-width: 60px; }
#editor.editor img { cursor: default; }

.statusbar {
  flex: 0 0 auto; display: flex; gap: 14px; flex-wrap: wrap;
  padding: 8px 16px; border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted); background: #fbfcfd;
}
.stat b { color: var(--ink); font-weight: 600; }
.stat.warn b { color: #b45309; }
.stat.over b { color: #dc2626; }
.stat.warn, .stat.over { font-weight: 600; }

/* ---------- preview (telegram) ---------- */
.preview-pane { background: var(--bg); }
.wallpaper {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 26px 22px 40px;
  background:
    radial-gradient(120% 120% at 0% 0%, #cdeccf 0%, #a9dcb4 38%, #cfe8c8 100%);
}
.preview-hint { max-width: 520px; margin: 16px auto 0; text-align: center; font-size: 11.5px; color: #2f5d3f; opacity: .8; }

.tg-message {
  max-width: 520px; margin: 0 auto; background: #fff;
  border-radius: 14px; padding: 10px 13px 7px;
  box-shadow: 0 1px 2px rgba(0,0,0,.12); position: relative;
}
.tg-channel { font-size: 13px; font-weight: 600; color: #3892d6; margin-bottom: 4px; }
.tg-meta { text-align: right; font-size: 11px; color: #9aa6b2; margin-top: 2px; }

/* the compiled telegram html */
.tg-content { font-size: 15px; line-height: 1.46; color: #1c2733; word-wrap: break-word; }
.tg-content > :first-child { margin-top: 0; }
.tg-content > :last-child { margin-bottom: 0; }
.tg-content p { margin: 0 0 9px; }
.tg-content h1, .tg-content h2, .tg-content h3,
.tg-content h4, .tg-content h5, .tg-content h6 {
  margin: 14px 0 7px; line-height: 1.25; font-weight: 700;
}
.tg-content h1 { font-size: 21px; }
.tg-content h2 { font-size: 19px; }
.tg-content h3 { font-size: 17px; }
.tg-content h4, .tg-content h5, .tg-content h6 { font-size: 15.5px; }
.tg-content a { color: var(--tg-link); text-decoration: none; }
.tg-content a:hover { text-decoration: underline; }
.tg-content b, .tg-content strong { font-weight: 700; }
.tg-content mark { background: var(--tg-mark); border-radius: 3px; padding: 0 2px; }
.tg-content code {
  font-family: var(--mono); font-size: 13px;
  background: #f0f2f5; border-radius: 4px; padding: 1px 5px;
}
.tg-content pre {
  background: #f0f2f5; border-radius: 8px; padding: 10px 12px; margin: 8px 0;
  overflow-x: auto;
}
.tg-content pre code { background: none; padding: 0; font-size: 13px; }
.tg-content sub, .tg-content sup { font-size: 75%; }

.tg-content ul, .tg-content ol { margin: 6px 0 10px; padding-left: 24px; }
.tg-content li { margin: 3px 0; }
.tg-content li input[type="checkbox"] {
  appearance: none; -webkit-appearance: none; width: 17px; height: 17px;
  border: 2px solid #c3ccd6; border-radius: 5px; vertical-align: -3px; margin-right: 4px;
  position: relative; cursor: default;
}
.tg-content li input[type="checkbox"]:checked { background: #3892d6; border-color: #3892d6; }
.tg-content li input[type="checkbox"]:checked::after {
  content: ""; position: absolute; left: 4px; top: 1px; width: 4px; height: 8px;
  border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg);
}

.tg-content blockquote {
  margin: 8px 0; padding: 4px 12px; border-left: 3px solid var(--green-d);
  background: #f4f8f5; border-radius: 0 8px 8px 0;
}
.tg-content blockquote cite { display: block; font-style: normal; font-size: 13px; color: var(--muted); margin-top: 3px; }
.tg-content aside {
  margin: 12px 0; padding: 6px 16px; text-align: center; font-size: 18px; font-style: italic; color: #334155;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.tg-content aside cite { display: block; font-size: 13px; font-style: normal; color: var(--muted); margin-top: 4px; }

.tg-content table { border-collapse: collapse; margin: 10px 0; width: 100%; font-size: 14px; }
.tg-content th, .tg-content td { border: 1px solid #dfe4ea; padding: 6px 10px; text-align: left; }
.tg-content th { background: #f5f7f9; font-weight: 600; }

.tg-content figure { margin: 10px 0; }
.tg-content figure img, .tg-content img { max-width: 100%; border-radius: 10px; display: block; }
.tg-content video { max-width: 100%; border-radius: 10px; display: block; margin: 10px 0; }
.tg-content audio { width: 100%; display: block; margin: 10px 0; }
.tg-content figcaption { font-size: 12.5px; color: var(--muted); margin-top: 5px; }
.tg-content figcaption cite { font-style: normal; color: #94a3b8; }

.tg-content hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }

.tg-content details {
  margin: 8px 0; border: 1px solid var(--line); border-radius: 8px; padding: 4px 12px; background: #fafbfc;
}
.tg-content summary { cursor: pointer; font-weight: 600; color: #334155; padding: 4px 0; }

.tg-content tg-spoiler {
  background: #cfd6de; border-radius: 4px; color: transparent;
  cursor: pointer; transition: color .2s, background .2s; padding: 0 2px;
}
.tg-content tg-spoiler::selection { background: transparent; }
.tg-content tg-spoiler.revealed { background: rgba(207,214,222,.25); color: inherit; }
.tg-content tg-math {
  font-family: "Cambria Math", Georgia, serif; font-style: italic; background: #f5f7f9;
  border-radius: 4px; padding: 0 4px; display: inline-block;
}
.tg-content tg-math-block {
  display: block; text-align: center; font-family: "Cambria Math", Georgia, serif;
  font-style: italic; background: #f5f7f9; border-radius: 6px; padding: 10px; margin: 10px 0;
}
.tg-content tg-emoji { font-style: normal; }

/* ---------- toast & img menu ---------- */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 11px 18px; border-radius: 10px;
  font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,.25); z-index: 50; max-width: 90vw;
}
.toast.hidden { display: none; }
.toast.ok { background: #15803d; }
.toast.err { background: #b91c1c; }
.toast.warn { background: #b45309; }

/* help modal */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: grid; place-items: center; z-index: 60; padding: 20px; }
.modal.hidden { display: none; }
.modal-card {
  background: #fff; border-radius: 16px; max-width: 580px; width: 100%; max-height: 88vh; overflow-y: auto;
  padding: 26px 28px; position: relative; box-shadow: 0 24px 64px rgba(0,0,0,.32);
}
.modal-card h2 { margin: 0 0 10px; font-size: 20px; }
.modal-card h3 { margin: 18px 0 6px; font-size: 14.5px; }
.modal-card p { margin: 0 0 8px; font-size: 14px; line-height: 1.55; color: #334155; }
.modal-card ol { margin: 6px 0 8px; padding-left: 20px; font-size: 14px; line-height: 1.55; color: #334155; }
.modal-card li { margin: 5px 0; }
.modal-card a { color: var(--tg-link); }
.modal-card code { background: #eef2f6; border-radius: 4px; padding: 1px 5px; font-family: var(--mono); font-size: 12.5px; }
.modal-close { position: absolute; top: 14px; right: 16px; border: 0; background: none; font-size: 18px; color: #94a3b8; cursor: pointer; line-height: 1; }
.modal-close:hover { color: var(--ink); }
.modal-card .btn.primary { margin-top: 16px; }

.img-menu {
  position: fixed; z-index: 40; background: #fff; border: 1px solid var(--line);
  border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.14); overflow: hidden; min-width: 180px;
}
.img-menu.hidden { display: none; }
.img-menu button { display: block; width: 100%; text-align: left; border: 0; background: #fff; padding: 10px 14px; font: inherit; font-size: 13px; cursor: pointer; }
.img-menu button:hover { background: #f3f6f9; }

@media (max-width: 820px) {
  /* iOS Safari is fragile with contenteditable + fixed-height/overflow layouts. Force pure
     normal document flow with !important so the PAGE scrolls and the editor grows with content. */
  html { height: auto !important; overflow: visible !important; }
  body { height: auto !important; min-height: 100% !important; overflow: visible !important; display: block !important; }
  .workspace { display: block !important; height: auto !important; }
  .pane { display: block !important; height: auto !important; }
  .editor-pane { border-right: 0; border-bottom: 1px solid var(--line); }
  /* editor grows with what you type; page scrolls so all text is reachable */
  #editor.editor { min-height: 60vh; height: auto !important; overflow: visible !important; }
  .preview-pane { background: var(--bg); }
  .wallpaper { min-height: 0 !important; overflow: visible !important; padding: 20px 14px 40px; }
  .settings { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  /* topbar: brand on row 1, actions wrap to their own full-width row so «Опубликовать» is never cut */
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .brand-sub { display: none; }
  .brand-title { font-size: 14px; white-space: normal; }
  .top-actions { width: 100%; justify-content: flex-end; gap: 6px; flex-wrap: wrap; }
  .btn { padding: 7px 11px; font-size: 12.5px; }
  .mode-switch button { padding: 6px 10px; }

  /* connection bar: token full-width, fields fill */
  .connbar { padding: 8px 12px; }
  .conn-byo { gap: 8px; }
  .conn-input { width: 100%; }
  .chat-field { flex: 1 1 auto; }
  .chat-input { width: auto; flex: 1 1 auto; }
  .conn-note { margin-left: 0; }

  .toolbar { padding: 6px 8px; gap: 2px; }
  .tool { min-width: 32px; height: 32px; }
  .statusbar { gap: 10px; padding: 7px 12px; font-size: 11.5px; }
  .modal-card { padding: 20px 18px; }
}
