:root {
  --bg: var(--tg-theme-bg-color, #0e1621);
  --text: var(--tg-theme-text-color, #e7ecf1);
  --hint: var(--tg-theme-hint-color, #8b98a5);
  --btn: var(--tg-theme-button-color, #3390ec);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --card: var(--tg-theme-secondary-bg-color, #17212b);
  --line: rgba(127, 127, 127, 0.18);
  --bubble: var(--tg-theme-secondary-bg-color, #182533);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
button, input { font-family: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

#app {
  max-width: 720px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.screen {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
#editor.screen {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: 0;
}
.editor-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
}
h1 { margin: 0 0 4px; font-size: 22px; }
.hint { margin: 0 0 12px; color: var(--hint); font-size: 13px; }
.section-title {
  margin: 16px 0 8px;
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--hint);
}

#search {
  width: 100%;
  border: 0;
  background: var(--card);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 16px;
  outline: none;
  margin-bottom: 10px;
}

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px; }
.chip, .chip.active {
  flex: 0 0 auto;
  border: 0;
  border-radius: 999px;
  padding: 8px 12px;
  background: var(--card);
  color: var(--text);
  font-size: 13px;
}
.chip.active { background: var(--btn); color: var(--btn-text); }

.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--card);
  color: inherit;
  border-radius: 16px;
  padding: 12px 14px;
}
.item .row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.item .title { font-weight: 700; }
.item .meta { color: var(--hint); font-size: 13px; margin-top: 4px; }
.item .tag { font-size: 11px; color: var(--hint); }
.delete { border: 0; background: transparent; color: var(--hint); }

.editor-top { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; flex-shrink: 0; }
.ghost {
  width: 36px; height: 36px; border: 0; border-radius: 12px;
  background: var(--card); color: var(--text); font-size: 18px;
}

.toolbar { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; }
.toolbar button {
  min-width: 38px; height: 38px; border: 0; border-radius: 11px;
  background: var(--card); color: var(--text);
}

.composer {
  min-height: 180px;
  background: var(--card);
  border-radius: 16px;
  padding: 14px;
  outline: none;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.preview-wrap { margin-top: 14px; }
.preview-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.preview-label { color: var(--hint); font-size: 12px; }
.text-btn {
  border: 0; background: transparent; color: var(--btn);
  font-size: 13px; font-weight: 650; padding: 0;
}
.bubble {
  background: var(--bubble);
  border-radius: 16px 16px 16px 6px;
  padding: 12px 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 72px;
  max-height: 180px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.preview-wrap.expanded .bubble { max-height: none; }
.bubble blockquote {
  margin: 8px 0;
  padding-left: 10px;
  border-left: 3px solid var(--btn);
  color: var(--hint);
}
.bubble tg-spoiler, .bubble .tg-spoiler {
  background: rgba(127,127,127,0.35);
  border-radius: 4px;
}
.bubble h1 { font-size: 22px; margin: 0 0 8px; }
.bubble h2 { font-size: 18px; margin: 12px 0 6px; }
.bubble h3 { font-size: 16px; margin: 10px 0 6px; }
.bubble p { margin: 0 0 8px; }
.bubble ul, .bubble ol { margin: 0 0 8px; padding-left: 20px; }
.bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}
.bubble th, .bubble td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: left;
}
.bubble th { font-weight: 700; }
.bubble hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.bubble footer { color: var(--hint); font-size: 12px; margin-top: 10px; }
.bubble mark { background: rgba(255, 214, 10, 0.35); }
.bubble aside {
  text-align: center;
  font-style: italic;
  margin: 12px 0;
  padding: 8px;
}
.bubble details { margin: 8px 0; }
.bubble summary { cursor: pointer; font-weight: 650; }
.composer h1 { font-size: 22px; margin: 0 0 8px; }
.composer h2 { font-size: 18px; margin: 8px 0 6px; }
.composer table { width: 100%; border-collapse: collapse; }
.composer td, .composer th { border: 1px solid var(--line); padding: 6px; }

.fallback {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--card);
  color: var(--hint);
  font-size: 13px;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1.35fr;
  gap: 8px;
  flex-shrink: 0;
  margin: 0 -16px;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
  z-index: 12;
}
.actions button {
  height: 46px; border: 0; border-radius: 14px; font-weight: 700;
}
.actions button:disabled { opacity: 0.55; }
.banner {
  position: fixed;
  left: 12px;
  right: 12px;
  top: calc(10px + env(safe-area-inset-top));
  z-index: 40;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.28);
}
.save-sheet { padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
.save-ok { width: 100%; height: 46px; border: 0; border-radius: 14px; font-weight: 700; }
.primary { background: var(--btn); color: var(--btn-text); }
.secondary { background: var(--card); color: var(--text); }
.warn { color: #ef9a9a; }

.block-list { display: flex; flex-direction: column; gap: 10px; }
.section-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 12px 12px;
}
.section-card.dragging { opacity: 0.45; }
.section-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.drag-handle {
  cursor: grab;
  color: var(--hint);
  font-size: 16px;
  padding: 4px 6px;
  user-select: none;
}
.section-kind { flex: 1; font-size: 12px; color: var(--hint); font-weight: 650; }
.icon-btn {
  border: 0; background: transparent; color: var(--hint);
  width: 28px; height: 28px; border-radius: 8px; font-size: 14px;
}
.field {
  width: 100%;
  border: 0;
  background: var(--bg);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  margin-bottom: 8px;
  font-family: inherit;
}
.field.tall { min-height: 72px; resize: vertical; }
.rich {
  min-height: 48px;
  line-height: 1.4;
  white-space: pre-wrap;
}
.mini-label { display: block; font-size: 11px; color: var(--hint); margin: 0 0 4px; }
.add-section {
  width: 100%;
  margin-top: 10px;
  height: 44px;
  border: 1px dashed var(--line);
  border-radius: 14px;
  background: transparent;
  color: var(--text);
  font-weight: 700;
}
.thumb-row { display: flex; gap: 6px; overflow-x: auto; margin: 0 0 8px; }
.thumb-row img, .thumb-row video {
  height: 64px; width: 88px; object-fit: cover; border-radius: 8px; background: #000;
}
.bubble img, .bubble video {
  display: block; width: 100%; max-height: 140px; object-fit: cover;
  border-radius: 12px; margin: 6px 0;
}
.bubble audio { width: 100%; margin: 6px 0; }
.bubble figure { margin: 8px 0; }
.bubble figcaption, .bubble cite { color: var(--hint); font-size: 12px; display: block; }
.mosaic {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin: 8px 0;
}
.mosaic img { width: 100%; height: 90px; object-fit: cover; border-radius: 8px; margin: 0; }
.picker {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: flex-end; z-index: 20;
}
.picker-sheet {
  width: 100%; max-height: 82vh; overflow: auto;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 14px 16px 28px;
}
.picker-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.picker-group { margin: 12px 0 6px; font-size: 12px; color: var(--hint); text-transform: uppercase; }
.lib-item {
  width: 100%; text-align: left;
  border: 1px solid var(--line); background: var(--card); color: inherit;
  border-radius: 14px; padding: 10px 12px; margin-bottom: 8px;
}
.lib-item .title { font-weight: 700; }
.lib-item .meta { color: var(--hint); font-size: 12px; margin-top: 3px; }
