:root{
  --bg:#eef1f5; --surface:#ffffff; --ink:#1a2230; --ink2:#5b6676; --ink3:#8a94a3;
  --line:#e7ebf0; --brand:#1AAA4F; --brand-dk:#138f41; --brand-soft:#e6f6ec;
  --gold:#b88a2e; --gold-soft:#f7efdd; --blue:#3066d0; --blue-soft:#e8f0fd;
  --radius:12px; --radius-sm:9px;
  --shadow:0 1px 2px rgba(16,24,40,.06),0 4px 14px rgba(16,24,40,.06);
  --shadow-sm:0 1px 2px rgba(16,24,40,.08);
}
* { box-sizing: border-box; }
body { margin: 0; font-family: 'Inter', -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
       font-size: 14px; height: 100vh; display: flex; flex-direction: column;
       background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased; }
button { font-family: inherit; cursor: pointer; }
select, button, input[type=range] { font-size: 14px; }

/* ===== ВЕРХНЯЯ ПАНЕЛЬ — светлая ===== */
header { display: flex; align-items: center; gap: 18px; padding: 9px 16px;
         background: var(--surface); color: var(--ink); border-bottom: 1px solid var(--line);
         box-shadow: var(--shadow-sm); flex-wrap: wrap; z-index: 20; }
header .brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 17px;
         color: var(--ink); letter-spacing: -.2px; }
header .brand img { height: 30px; width: auto; display: block; }
header .brand .wordmark { color: var(--brand); }
.bookTitle { font-weight: 600; font-size: 14px; color: var(--ink); max-width: 320px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.buildwrap { display: inline-flex; align-items: center; gap: 6px; }
.epubbadge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.epubbadge.stale { background: #fff3d6; color: #8a6a12; }
.epubbadge.fresh { background: var(--brand-soft); color: var(--brand-dk); }
.hzone { display: flex; align-items: center; gap: 7px; flex-wrap: nowrap; }
.hsep { width: 1px; height: 24px; background: var(--line); }
.hflex { flex: 1; }
select { padding: 6px 10px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); }
header select { background: var(--bg); border: 1px solid var(--line); max-width: 230px; font-weight: 600; }
/* базовая кнопка (контекстные панели и пр.) */
button { padding: 7px 13px; border-radius: var(--radius-sm); border: 1px solid var(--line);
         background: var(--surface); color: var(--ink); }
button:hover { background: var(--bg); }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600;
  box-shadow: 0 2px 8px rgba(26,170,79,.28); }
button.primary:hover { background: var(--brand-dk); }
button:disabled { opacity: .5; cursor: default; }
/* кнопки шапки — «призрачные» */
.hbtn { display: inline-flex; align-items: center; gap: 6px; background: transparent;
  color: var(--ink2); border: none; border-radius: var(--radius-sm); padding: 8px 13px;
  font-size: 13px; font-weight: 600; text-decoration: none; }
.hbtn:hover { background: var(--bg); color: var(--ink); }
.hbtn.on { background: var(--brand-soft); color: var(--brand-dk); }
.hbtn.secondary { background: var(--surface); border: 1px solid var(--line); }
.hbtn.secondary:hover { background: var(--bg); }
.hbtn.primary { background: var(--brand); color: #fff; box-shadow: 0 2px 8px rgba(26,170,79,.28); }
.hbtn.primary:hover { background: var(--brand-dk); }
.hbtn.disabled { opacity: .45; pointer-events: none; }
.hbtn.plus { width: 36px; padding: 8px 0; justify-content: center; background: var(--bg); font-size: 18px; }
.seg { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; }
.seg .hbtn { border-radius: 7px; padding: 6px 14px; color: var(--ink2); }
.seg .hbtn.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); }
.qwrap { position: relative; }
.qwrap::before { content: '🔍'; position: absolute; left: 10px; top: 8px; font-size: 12px; opacity: .55; }
#searchBox { background: var(--bg); border: 1px solid var(--line); color: var(--ink);
  border-radius: 10px; padding: 7px 12px 7px 30px; font-size: 13px; width: 200px; }
#searchBox::placeholder { color: var(--ink3); }
.pill { display: flex; align-items: center; gap: 8px; background: var(--brand-soft);
  border-radius: 999px; padding: 6px 13px; font-size: 13px; color: var(--brand-dk);
  max-width: 240px; cursor: pointer; font-weight: 600; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--brand); flex: 0 0 auto; }
.pill.warn { background: #fff3d6; color: #8a6a12; } .pill.warn .dot { background: #e0a517; }
.pill.err { background: #fdecec; color: #b3261e; } .pill.err .dot { background: #e5484d; }
.dirty { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pop { display: none; position: absolute; top: 46px; left: 0; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 30px rgba(16,24,40,.18);
  padding: 8px; z-index: 60; font-size: 13px; min-width: 250px; }
.pop.show { display: block; }
.pop.popright { left: auto; right: 0; }
.pop .sect { font-size: 11px; color: var(--ink3); text-transform: uppercase; letter-spacing: .05em;
  margin: 4px 0 6px; padding: 0 4px; }
.pop .popnote { font-size: 12px; color: var(--ink2); margin: 0 4px 4px; }
.pop label { display: flex; gap: 9px; align-items: center; padding: 8px 10px; border-radius: 9px; cursor: pointer; }
.pop label:hover { background: var(--bg); }
.pop .item { display: flex; gap: 8px; align-items: center; padding: 9px 10px; border-radius: 9px;
  cursor: pointer; color: var(--ink); text-decoration: none; }
.pop .item:hover { background: var(--bg); }
.pop .go { margin-top: 6px; width: 100%; background: var(--brand); color: #fff; border: none;
  border-radius: 9px; padding: 9px; font-size: 13px; font-weight: 600; }
.statuslog { font: 12px/1.7 ui-monospace, Consolas, monospace; color: var(--ink2); max-width: 360px;
  max-height: 220px; overflow: auto; white-space: pre-wrap; }
.vbtn { margin-left: auto; font-size: 12px; border: 1px solid var(--line); background: var(--surface);
  color: var(--ink2); border-radius: 8px; padding: 4px 11px; cursor: pointer; font-weight: 600;
  display: inline-flex; gap: 5px; align-items: center; }
.vbtn + .vbtn { margin-left: 6px; }
.vbtn.on { background: var(--brand-soft); color: var(--brand-dk); border-color: transparent; }
#viewrow { display: none; background: var(--surface); border-radius: 10px; box-shadow: var(--shadow-sm);
  padding: 8px 12px; font-size: 12px; color: var(--ink2); margin-bottom: 10px; gap: 12px;
  align-items: center; flex-wrap: wrap; }
#viewrow.show { display: flex; }
#viewrow label.small { color: var(--ink2); }
#viewrow .note { color: var(--ink3); font-size: 11px; }
#coverUpBtn { margin-bottom: 10px; background: var(--brand); color: #fff; border: none; box-shadow: var(--shadow-sm); }
@media (max-width: 1100px) { header { gap: 8px; } .hflex { flex-basis: 100%; height: 0; padding: 0; } }

/* ===== РАБОЧАЯ ОБЛАСТЬ — карточки-панели ===== */
main { flex: 1; display: flex; min-height: 0; gap: 12px; padding: 12px; }
.pane { display: flex; flex-direction: column; flex: 0 0 auto; min-width: 130px; overflow: hidden;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.pane.flexpane { flex: 1 1 auto; min-width: 220px; width: auto !important; }
.paneHead { flex: 0 0 38px; font-size: 12px; font-weight: 600; color: var(--ink2);
  background: var(--surface); padding: 0 14px; cursor: grab; user-select: none;
  border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: center;
  border-radius: var(--radius) var(--radius) 0 0; }
.paneHead .grip { color: var(--ink3); }
.paneHead:active { cursor: grabbing; }
.paneBody { flex: 1; min-height: 0; overflow: auto; }
.splitter { flex: 0 0 12px; cursor: col-resize; background: transparent; align-self: stretch;
  position: relative; }
.splitter::before { content: ''; position: absolute; left: 5px; top: 0; bottom: 0; width: 2px;
  border-radius: 2px; background: transparent; }
.splitter:hover::before, .splitter.drag::before { background: var(--brand); }
#files { padding: 6px; }
#files div { padding: 9px 11px; font-size: 13.5px; cursor: pointer; white-space: nowrap;
             overflow: hidden; text-overflow: ellipsis; border-radius: 10px; margin: 2px 0; font-weight: 500; }
#files div:hover { background: var(--bg); }
#files div.active { background: var(--brand-soft); color: var(--brand-dk); font-weight: 600; }
#editorPane { display: flex; min-width: 0; flex-direction: column; }
#editor { flex: 1; border: none; outline: none; resize: none; padding: 16px;
          font: 13px/1.6 ui-monospace, Consolas, 'Courier New', monospace; color: var(--ink); }

/* ===== блочный редактор ===== */
#blocksView { flex: 1; overflow: auto; background: #f6f8fa; padding: 10px; }
.brow { position: relative; display: flex; flex-direction: column; gap: 5px; background: var(--surface);
  border-radius: 10px; box-shadow: var(--shadow-sm); padding: 9px 13px 11px 15px; margin-bottom: 8px;
  border-left: 3px solid transparent; transition: box-shadow .12s; }
/* верхняя полоса блока: тип + действия; текст идёт под ней во всю ширину */
.brow .bhead { display: flex; align-items: center; gap: 8px; }
.brow:hover { box-shadow: var(--shadow); }
.brow.selected { border-left-color: var(--brand); box-shadow: 0 0 0 1px var(--brand), var(--shadow); }
.brow .chip { flex: 0 0 auto; font-size: 11px; font-weight: 600; border: none; border-radius: 7px;
  padding: 4px 9px; cursor: pointer; color: var(--ink2); background: var(--bg);
  appearance: none; -webkit-appearance: none; text-align: center; }
.brow .chip.t-heading { background: var(--blue-soft); color: #2451a8; }
.brow .chip.t-quran { background: var(--brand-soft); color: var(--brand-dk); }
.brow .chip.t-hadith { background: var(--gold-soft); color: #8a6a18; }
.brow .chip.t-imprint, .brow .chip.t-poetry { background: #efeaf8; color: #5d4d8f; }
.brow .chip.t-footnote { background: #eceef3; color: #5b6676; }
.brow.t-heading { border-left-color: var(--blue); }
.brow.t-quran { border-left-color: var(--brand); }
.brow.t-hadith { border-left-color: var(--gold); }
/* строка-сноска: с отступом под родительским блоком */
.brow.fnrow { margin-left: 30px; border-left: 2px solid var(--line); background: #fbfcfd; }
.brow.fnrow .chip { cursor: default; }
.brow.fnrow .btext { font-size: 13px; color: var(--ink2); }
.brow .acts button.bookmark { filter: grayscale(1); opacity: .4; }
.brow .acts button.bookmark.on { filter: none; opacity: 1; color: #e0a517; }
.brow .btext { width: 100%; border: none; outline: none; resize: none; overflow: hidden;
  font: 14px/1.55 'Inter', Arial, sans-serif; color: var(--ink); padding: 2px 2px;
  min-height: 22px; background: transparent; }
.brow .btext[dir=rtl] { font-size: 17px; color: #0a5c36; }
.brow .acts { flex: 0 0 auto; display: flex; gap: 1px; opacity: 0; transition: opacity .12s; margin-left: auto; }
.brow:hover .acts, .brow.selected .acts { opacity: 1; }
.brow .acts button { background: transparent; border: none; color: var(--ink3); font-size: 14px;
  padding: 5px 6px; border-radius: 7px; cursor: pointer; line-height: 1; }
.brow .acts button:hover { background: var(--bg); color: var(--ink); }
.brow .acts button.on { color: var(--brand); }
.brow .acts button.bmark { filter: grayscale(1); opacity: .4; }
.brow .acts button.bmark.on { filter: none; opacity: 1; }
.brow .meta { display: flex; gap: 8px; align-items: center;
  font-size: 12px; color: var(--ink2); padding: 8px 0 2px 2px; margin-top: 4px;
  border-top: 1px dashed var(--line); }
.brow .meta input { width: 60px; font-size: 12px; padding: 4px 7px;
  border: 1px solid var(--line); border-radius: 6px; font-family: inherit; }
.binfo { font-size: 13px; color: var(--ink3); text-align: center; padding: 16px; }

/* ===== предпросмотр / телефон ===== */
#previewPane { display: flex; flex-direction: column; align-items: center;
               background: #dde3ea; padding: 14px; }
#phone { background: #0e1116; border-radius: 30px; padding: 12px; box-shadow: 0 12px 40px rgba(16,24,40,.32); }
#preview { border: none; background: #fff; border-radius: 22px; display: block; }
.hint { font-size: 12px; color: var(--ink2); margin-top: 8px; text-align: center; max-width: 420px; }
label.small { font-size: 13px; color: var(--ink2); display: flex; align-items: center; gap: 5px; }

/* ===== контекстные панели разметки ===== */
#imgbar, #textbar { display: none; align-items: center; gap: 7px; flex-wrap: wrap;
  background: var(--surface); border-radius: var(--radius); padding: 9px 12px; margin-bottom: 10px;
  font-size: 13px; box-shadow: var(--shadow); }
#imgbar { border-top: 3px solid var(--brand); }
#textbar { border-top: 3px solid var(--blue); }
#imgbar button, #textbar button { background: var(--bg); color: var(--ink); border: 1px solid var(--line);
  padding: 5px 10px; border-radius: 8px; font-weight: 500; }
#imgbar button.on { background: var(--brand); color: #fff; border-color: var(--brand); }
#textbar button.on { background: var(--blue); color: #fff; border-color: var(--blue); }
#imgbar button.danger, #textbar button.danger { color: #c0392b; }
#imgbar button.danger:hover, #textbar button.danger:hover { background: #c0392b; color: #fff; border-color: #c0392b; }
#imgbar .grp, #textbar .grp { display: flex; gap: 3px; align-items: center; }
#imgbar input[type=number], #textbar input { width: 54px; padding: 5px; border: 1px solid var(--line);
  border-radius: 7px; font-family: inherit; }
#imgbar .ttl, #textbar .ttl { font-weight: 700; color: var(--ink); }
header .grp button { border-radius: 0; }

/* ===== каталог книг (стартовый экран) ===== */
#catalog { position: fixed; inset: 0; background: var(--bg); z-index: 100; overflow: auto;
  display: flex; flex-direction: column; }
#catalog .catbar { display: flex; align-items: center; gap: 14px; padding: 12px 24px;
  background: var(--surface); border-bottom: 1px solid var(--line); box-shadow: var(--shadow-sm); }
#catalog .catbar img { height: 30px; }
#catalog .catbar .flex { flex: 1; }
#catalog .catwrap { max-width: 1080px; margin: 0 auto; padding: 28px 24px 60px; width: 100%; }
#catalog h1 { font-size: 22px; font-weight: 700; margin: 4px 0 20px; }
.catgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 18px; }
.bookcard { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; cursor: pointer; transition: transform .12s, box-shadow .12s; }
.bookcard:hover { transform: translateY(-3px); box-shadow: 0 6px 22px rgba(16,24,40,.12); }
.bookcard .cov { aspect-ratio: 3/4; background: #dde3ea center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; color: var(--ink3); font-size: 13px; }
.bookcard .ttl { padding: 11px 13px; font-size: 13.5px; font-weight: 600; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.bookcard.add { border: 2px dashed var(--line); box-shadow: none; display: flex;
  flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 220px;
  color: var(--brand-dk); font-weight: 600; }
.bookcard.add:hover { border-color: var(--brand); transform: none; }
.bookcard.add .plus { font-size: 34px; line-height: 1; }
#catAdd { display: none; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; margin-bottom: 22px; }
#catAdd.show { display: block; }
#catAdd label { display: inline-flex; gap: 6px; align-items: center; margin-right: 18px; font-size: 13px; }

/* ===== панель оглавления ===== */
#tocPanel { display: none; position: fixed; top: 0; right: 0; bottom: 0; width: 440px;
  background: var(--surface); z-index: 30; box-shadow: -8px 0 40px rgba(16,24,40,.2);
  overflow: auto; font-size: 14px; }
#tocPanel .tochead { position: sticky; top: 0; background: var(--surface); color: var(--ink);
  padding: 14px 16px; font-weight: 700; display: flex; gap: 8px; align-items: center;
  border-bottom: 1px solid var(--line); }
#tocPanel .tochead span { flex: 1; }
#tocPanel .tochead button { background: var(--bg); border: 1px solid var(--line); }
#tocItems { padding: 8px 8px 0; }
#tocItems .tocitem { display: flex; align-items: center; gap: 8px; padding: 9px 10px;
  border-radius: 9px; }
#tocItems .tocitem:hover { background: var(--bg); }
#tocItems .tocitem .lbl { flex: 1; cursor: pointer; }
#tocItems .tocitem .lbl:hover { color: var(--brand-dk); }
#tocItems .tocitem.lvl2 .lbl { padding-left: 18px; }
#tocItems .tocitem.lvl3 .lbl { padding-left: 36px; }
#tocItems .tocitem.hiddenEntry .lbl { opacity: .45; text-decoration: line-through; }
#tocItems .tocitem button { padding: 4px 8px; font-size: 12px; background: var(--bg);
  color: var(--ink); border: 1px solid var(--line); border-radius: 7px; }
#tocItems .file { padding: 12px 12px 4px; font-size: 11px; color: var(--ink3);
  text-transform: uppercase; letter-spacing: .05em; }
#tocItems .mark-ico { color: var(--brand); }
#tocPanel .tocfoot { padding: 12px 16px; color: var(--ink2); font-size: 12px; }

/* ===== проверка структуры и исходников перед Pandoc ===== */
#sourceAuditCount:not(:empty) { min-width: 19px; padding: 1px 6px; border-radius: 999px;
  background: #fff3d6; color: #8a6a12; font-size: 11px; text-align: center; }
#sourceAuditPanel { display: none; position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 96vw);
  background: var(--surface); z-index: 35; box-shadow: -8px 0 40px rgba(16,24,40,.2); overflow: auto; }
#sourceAuditPanel .audithead { position: sticky; top: 0; z-index: 1; display: flex; gap: 8px;
  align-items: center; padding: 14px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
#sourceAuditPanel .audithead span { flex: 1; font-weight: 700; }
#sourceAuditPanel .audithead button { padding: 5px 9px; background: var(--bg); }
#sourceAuditContent { padding: 14px 16px 28px; }
.audit-summary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.audit-stat { padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.audit-stat strong { display: block; margin-bottom: 3px; font-size: 18px; }
.audit-stat span { color: var(--ink2); font-size: 12px; }
.audit-section { margin-top: 18px; }
.audit-section h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--ink2); }
.audit-row { padding: 10px 11px; margin-bottom: 6px; border: 1px solid var(--line); border-radius: 9px;
  cursor: pointer; }
.audit-row:hover { border-color: #b8c3d1; background: var(--bg); }
.audit-row.error { border-left: 4px solid #d33; }
.audit-row.warning { border-left: 4px solid #e0a517; }
.audit-row .audit-title { font-weight: 600; line-height: 1.35; }
.audit-row .audit-meta { margin-top: 4px; color: var(--ink3); font-size: 11px; }
.audit-row .audit-opening { margin-top: 5px; color: var(--ink2); font-size: 12px; line-height: 1.4; }
.audit-ok { padding: 16px; text-align: center; border-radius: 10px; background: var(--brand-soft);
  color: var(--brand-dk); }
.audit-loading, .audit-error { padding: 24px 8px; text-align: center; color: var(--ink2); }
.audit-error { color: #b3261e; }

/* ===== поиск ===== */
#searchResults { display: none; position: fixed; top: 56px; right: 16px; width: 440px;
  max-height: 65vh; overflow: auto; background: var(--surface); z-index: 40; border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(16,24,40,.2); border: 1px solid var(--line); font-size: 13px; }
#searchResults .sr { padding: 9px 13px; border-bottom: 1px solid var(--line); cursor: pointer; }
#searchResults .sr:hover { background: var(--bg); }
#searchResults .sr .f { color: var(--ink3); font-size: 11px; }
#searchResults .sr mark { background: #fde79a; border-radius: 3px; }

/* ===== PDF-панель ===== */
#pdfPane { background: #20242b; padding: 10px; }
#pdfPane img { width: 100%; margin-bottom: 8px; border-radius: 6px; box-shadow: 0 2px 8px rgba(0,0,0,.3); }
#pdfPane .pn { color: #9aa3ad; font-size: 11px; text-align: center; margin: -4px 0 8px; }
#files .row { display: flex; align-items: center; }
#files .row div { flex: 1; min-width: 0; }
#files .row input { margin-right: 8px; }
/* главы: перетаскивание за «ручку» слева; удаление — по правому клику (меню ниже) */
#files .drag-grip { flex: 0 0 auto; width: 16px; text-align: center; cursor: grab;
  color: var(--ink3); opacity: .6; user-select: none; font-size: 13px; }
#files .drag-grip:active { cursor: grabbing; }
#files .row.dragging { opacity: .4; }
#files .row.drop-before { box-shadow: inset 0 2px 0 0 var(--brand); }
#files .row.drop-after  { box-shadow: inset 0 -2px 0 0 var(--brand); }
/* контекстное меню главы (правый клик) */
#ctxmenu { position: fixed; z-index: 50; min-width: 150px; padding: 4px; display: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18); font-size: 13px; }
#ctxmenu .item { padding: 8px 12px; border-radius: 5px; cursor: pointer; white-space: nowrap; color: var(--ink); }
#ctxmenu .item.danger { color: #b00; }
#ctxmenu .item.danger:hover { background: #ffe3e3; }
