/* Telegram passes the user's theme as CSS variables; the fallbacks are Telegram's light theme. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #0f0f0f);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --danger: var(--tg-theme-destructive-text-color, #e53935);
  --sep: var(--tg-theme-section-separator-color, rgba(127, 127, 127, 0.22));
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg2); color: var(--text); font: 15px/1.4 var(--font); -webkit-text-size-adjust: 100%; }
body { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
button, input { font: inherit; color: inherit; }
a { color: var(--link); }
[hidden] { display: none !important; }

.app { max-width: 1120px; margin: 0 auto; padding: 12px 16px 24px; }
.boot, .empty { color: var(--hint); text-align: center; padding: 48px 16px; }
.hint { color: var(--hint); font-size: 13px; }
.danger { color: var(--danger); }

/* --- buttons ------------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--btn); color: var(--btn-text); font-weight: 600;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.secondary { background: var(--section); color: var(--link); box-shadow: inset 0 0 0 1px var(--sep); }
.btn.danger { background: transparent; color: var(--danger); box-shadow: inset 0 0 0 1px var(--sep); }
.btn.block { width: 100%; }
.icon-btn {
  width: 36px; height: 36px; border: 0; border-radius: 10px; background: transparent; color: var(--hint);
  cursor: pointer; font-size: 17px; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover:not(:disabled) { background: var(--bg2); color: var(--text); }
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* --- list view ----------------------------------------------------------------- */
.page-title { font-size: 24px; font-weight: 700; margin: 8px 0 4px; }
.section { background: var(--section); border-radius: var(--radius); margin: 14px 0; overflow: hidden; }
.section-title { color: var(--hint); font-size: 13px; text-transform: uppercase; letter-spacing: .02em; margin: 18px 4px 6px; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px 14px; border: 0; background: none;
  text-align: left; cursor: pointer; border-bottom: 1px solid var(--sep);
}
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { color: var(--hint); font-size: 13px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--bg2); color: var(--hint); white-space: nowrap; }
.badge.ok { color: #2e9e5b; }

/* --- editor --------------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  margin: -12px -16px 10px; padding: 10px 16px; background: var(--bg2);
}
.topbar .grow { flex: 1; min-width: 0; }
.topbar .title { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.save-state { font-size: 12px; color: var(--hint); }
.save-state.error { color: var(--danger); }

.segmented { display: inline-flex; background: var(--section); border-radius: 10px; padding: 3px; box-shadow: inset 0 0 0 1px var(--sep); }
.segmented button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; cursor: pointer; color: var(--hint); font-weight: 600; font-size: 13px; }
.segmented button.on { background: var(--btn); color: var(--btn-text); }
.format-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 8px; }
.notice { background: var(--section); border-radius: 12px; padding: 10px 12px; margin: 8px 0; font-size: 14px; }
.notice.error { color: var(--danger); }

.tabs { display: none; margin: 6px 0 10px; }
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 420px); gap: 18px; align-items: start; }
.preview-pane { position: sticky; top: 64px; }
@media (max-width: 860px) {
  .tabs { display: flex; }
  .tabs .segmented { width: 100%; }
  .tabs .segmented button { flex: 1; }
  .editor-grid { grid-template-columns: minmax(0, 1fr); }
  .editor-grid[data-tab="edit"] .preview-pane { display: none; }
  .editor-grid[data-tab="preview"] .blocks-pane { display: none; }
  .preview-pane { position: static; }
}

.card { background: var(--section); border-radius: var(--radius); padding: 10px 12px 12px; margin-bottom: 10px; }
.card.incomplete { box-shadow: inset 0 0 0 1px var(--sep); }
.card-head { display: flex; align-items: center; gap: 2px; margin-bottom: 6px; }
.card-head .label { flex: 1; font-size: 13px; font-weight: 600; color: var(--hint); }
.field-label { font-size: 12px; color: var(--hint); margin: 8px 2px 4px; }

.rt-wrap { position: relative; }
.rt {
  min-height: 40px; padding: 9px 11px; border-radius: 10px; background: var(--bg2); outline: none;
  white-space: pre-wrap; word-break: break-word;
}
.rt.single { min-height: 0; }
.rt:focus { box-shadow: inset 0 0 0 2px var(--btn); }
.rt:empty::before { content: attr(data-placeholder); color: var(--hint); }
.rt-tools { display: none; gap: 2px; flex-wrap: wrap; margin-top: 4px; }
.rt-wrap:focus-within .rt-tools { display: flex; }
.rt-tools button { min-width: 36px; height: 32px; border: 0; border-radius: 8px; background: var(--bg2); cursor: pointer; font-size: 14px; }
.rt-tools button b, .rt-tools button i, .rt-tools button u, .rt-tools button s { pointer-events: none; }
.input { width: 100%; padding: 10px 11px; border: 0; border-radius: 10px; background: var(--bg2); outline: none; }
.input:focus { box-shadow: inset 0 0 0 2px var(--btn); }
.input.bad { box-shadow: inset 0 0 0 2px var(--danger); }
.check { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 14px; }

.cemoji { display: inline-block; min-width: 1.2em; text-align: center; border-bottom: 1px dotted var(--hint); }
.cemoji img, .cemoji video { width: 1.25em; height: 1.25em; vertical-align: -0.25em; object-fit: contain; }
.cemoji.drawn { border-bottom: 0; }
.spoiler { background: rgba(127, 127, 127, .35); border-radius: 4px; }
.media-thumb { width: 100%; max-height: 260px; object-fit: cover; border-radius: 10px; display: block; background: var(--bg2); }
.mosaic-mini { display: inline-grid; gap: 0; line-height: 1; font-size: 14px; }

.add-bar { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; margin: 6px 0 12px; }
.add-bar button {
  padding: 10px; border: 0; border-radius: 12px; background: var(--section); cursor: pointer; text-align: left;
  font-weight: 600; font-size: 14px; color: var(--text);
}

.actions-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: flex; gap: 10px; justify-content: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg2); border-top: 1px solid var(--sep);
}
.actions-bar .btn { flex: 1; max-width: 360px; }

/* --- Telegram-like preview ---------------------------------------------------------- */
.tg-chat { background: var(--bg2); border-radius: var(--radius); padding: 14px 10px; }
.tg-post { background: var(--section); border-radius: 16px; overflow: hidden; max-width: 420px; margin: 0 auto; box-shadow: 0 1px 1px rgba(0, 0, 0, .08); }
.tg-media img, .tg-media video { display: block; width: 100%; max-height: 420px; object-fit: cover; }
.tg-media.placeholder { padding: 36px 0; text-align: center; color: var(--hint); background: var(--bg2); }
.tg-body { padding: 8px 12px 10px; }
.tg-body > * + * { margin-top: 8px; }
.tg-body > .tg-quote + .tg-quote { margin-top: 4px; }
.tg-p { white-space: pre-wrap; word-break: break-word; }
.tg-h { font-size: 20px; font-weight: 700; line-height: 1.25; white-space: pre-wrap; }
.tg-h.size-2 { font-size: 18px; }
.tg-h.size-3 { font-size: 16px; }
.tg-quote {
  position: relative; border-left: 3px solid var(--accent); border-radius: 4px 8px 8px 4px; padding: 4px 26px 4px 8px;
  background: color-mix(in srgb, var(--accent) 10%, transparent); white-space: pre-wrap; word-break: break-word;
}
.tg-quote .q-text.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.tg-quote .q-toggle { position: absolute; right: 4px; bottom: 2px; border: 0; background: none; color: var(--accent); cursor: pointer; font-size: 13px; }
.tg-details { border-radius: 8px; background: color-mix(in srgb, var(--accent) 8%, transparent); padding: 6px 10px; }
.tg-details summary { cursor: pointer; font-weight: 600; list-style: none; }
.tg-details summary::-webkit-details-marker { display: none; }
.tg-details summary::after { content: " ▾"; color: var(--accent); }
.tg-details[open] summary::after { content: " ▴"; }
.tg-details .d-body { margin-top: 6px; white-space: pre-wrap; }
.tg-divider { border: 0; border-top: 1px solid var(--sep); margin: 10px 0; }
.tg-mosaic { line-height: 1.15; font-size: 18px; white-space: pre; overflow-x: auto; }
.tg-post .cemoji { border-bottom: 0; }
.tg-mosaic .cemoji { min-width: 0; }
.tg-buttons { max-width: 420px; margin: 4px auto 0; display: grid; gap: 4px; }
.tg-buttons span { display: block; text-align: center; padding: 9px; border-radius: 10px; background: color-mix(in srgb, var(--section) 80%, transparent); font-weight: 600; font-size: 14px; }
.tg-meta { text-align: center; color: var(--hint); font-size: 12px; margin-top: 8px; }

/* --- sheets and toasts -------------------------------------------------------------------- */
.sheet-backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 560px; max-height: 85vh; overflow-y: auto; background: var(--bg2); border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(18px + env(safe-area-inset-bottom));
}
.sheet h3 { margin: 4px 2px 10px; font-size: 18px; }
.sheet .section { margin: 10px 0; }
.sheet .btn + .btn { margin-top: 8px; }
.toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30;
  background: rgba(20, 20, 20, .9); color: #fff; padding: 10px 14px; border-radius: 12px; font-size: 14px; max-width: 90vw;
}
.toast.error { background: #b3261e; }
