:root {
  color-scheme: dark;
  --bg: #1d2129;
  --panel: #252a34;
  --panel-2: #2d333f;
  --line: #3a4150;
  --text: #ece6d8;
  --muted: #9aa1ad;
  --candle: #e0b65c;                      /* подготовлено / выбор */
  --candle-soft: rgba(224, 182, 92, 0.14);
  --tally: #e0483e;                       /* в эфире */
  --tally-soft: rgba(224, 72, 62, 0.12);
  --ui: "Noto Sans", "Segoe UI", system-ui, sans-serif;
  --read: "PT Serif", Cambria, Georgia, serif;

  /* цвета групп книг в навигаторе */
  --g-law: #c9925a;
  --g-history: #8d9e6b;
  --g-wisdom: #b98bb4;
  --g-prophets: #6f97c4;
  --g-gospels: #e0b65c;
  --g-acts: #d27c5c;
  --g-letters: #5fa9a0;
  --g-revelation: #c2675f;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--ui);
}

button, select, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.4; cursor: default; }
:focus-visible { outline: 2px solid var(--candle); outline-offset: 2px; }
.link { background: none; border: none; padding: 0; color: var(--candle); }
.danger { color: #f08a80; }

kbd {
  font: 11px var(--ui);
  padding: 0 5px;
  margin-left: 4px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--muted);
}

/* ================= Верхняя панель ================= */
.top {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.omni { position: relative; flex: 1; max-width: 860px; }
#q {
  width: 100%;
  padding: 11px 44px 11px 16px;
  font-size: 17px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
#q:focus { border-color: var(--candle); }
.omni-key { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); }
#q:focus + .omni-key { display: none; }

.suggest {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 50;
  max-height: 70vh;
  overflow-y: auto;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}
.sg {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 7px;
  padding: 8px 10px;
}
.sg.active, .sg:hover { background: var(--panel-2); }
.sg.active { box-shadow: inset 3px 0 0 var(--candle); }
.sg-kind { color: var(--muted); font-size: 12px; padding-top: 2px; }
.sg-body { display: flex; flex-direction: column; min-width: 0; }
.sg-label { font-weight: 500; }
.sg-sub {
  color: var(--muted);
  font: 14px/1.4 var(--read);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sg-empty { color: var(--muted); padding: 10px; }
mark { background: var(--candle-soft); color: var(--candle); border-radius: 2px; }

.proj-status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.proj-status .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--line); }
.proj-status.on { color: var(--text); }
.proj-status.on .dot { background: #5fbf7a; box-shadow: 0 0 8px #5fbf7a; }
.close-proj { border-color: rgba(240, 138, 128, 0.6); color: #f08a80; background: transparent; white-space: nowrap; }
.primary-outline { border-color: var(--candle); color: var(--candle); background: transparent; white-space: nowrap; }

/* ================= Раскладка ================= */
.layout {
  display: grid;
  grid-template-columns: 230px minmax(340px, 1fr) minmax(300px, 25%) minmax(300px, 22%);
  min-height: 0;
}

/* ================= Навигатор ================= */
.nav { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
.nav-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
  font-weight: 500;
}
/* Хлебные крошки: [книги] › книга › глава — всегда в одну строку */
.crumbs { display: flex; align-items: center; gap: 2px; flex: 1; min-width: 0; }
.crumb {
  flex: none;
  padding: 5px 7px;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  white-space: nowrap;
  line-height: 1.3;
}
button.crumb:hover { background: var(--panel-2); color: var(--text); }
.crumb.current { color: var(--text); }
/* Название книги сжимается первым: сначала сокращение (в JS), в крайнем случае — многоточие */
.crumb-book { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.crumb-home { display: inline-flex; align-items: center; gap: 7px; }
.crumb-home svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linejoin: round; }
.crumb-label[hidden] { display: none; }
.crumb-sep {
  flex: none;
  width: 6px;
  height: 6px;
  margin: 0 4px 0 2px;
  border-top: 1.5px solid var(--muted);
  border-right: 1.5px solid var(--muted);
  transform: rotate(45deg);
  opacity: 0.7;
}
.nav-body { overflow-y: auto; padding: 4px 10px 24px; }
/* После выбора книги: главы сверху, стихи снизу — поровну, у каждой половины своя прокрутка */
.nav-split { flex: 1; min-height: 0; display: grid; grid-template-rows: 1fr 1fr; }
.nav-split[hidden] { display: none; }
.nav-half { min-height: 0; overflow-y: auto; padding: 0 10px 12px; }
.nav-half + .nav-half { border-top: 1px solid var(--line); }
.nav-half .nav-section { position: sticky; top: 0; z-index: 1; background: var(--bg); }
.nav-empty { color: var(--muted); font-size: 13px; padding: 2px 2px 8px; }
.nav-section { color: var(--muted); font-size: 12px; padding: 12px 2px 6px; }
.tiles { display: grid; gap: 4px; }
.books-grid { grid-template-columns: repeat(4, 1fr); }
.num-grid { grid-template-columns: repeat(5, 1fr); }

.tile {
  padding: 7px 0;
  font-size: 13px;
  text-align: center;
  background: var(--panel);
  border-color: transparent;
}
.book-tile { border-bottom: 2px solid var(--gc, var(--line)); }
.book-tile[data-group="law"] { --gc: var(--g-law); }
.book-tile[data-group="history"] { --gc: var(--g-history); }
.book-tile[data-group="wisdom"] { --gc: var(--g-wisdom); }
.book-tile[data-group="prophets"] { --gc: var(--g-prophets); }
.book-tile[data-group="gospels"] { --gc: var(--g-gospels); }
.book-tile[data-group="acts"] { --gc: var(--g-acts); }
.book-tile[data-group="letters"] { --gc: var(--g-letters); }
.book-tile[data-group="revelation"] { --gc: var(--g-revelation); }
.tile:hover:not(:disabled) { border-color: var(--muted); }
.book-tile:hover { border-bottom-color: var(--gc); }
.tile.current { background: var(--candle-soft); color: var(--candle); }
.tile.selected { background: var(--candle); color: #1d2129; font-weight: 600; }
.tile.on-air { box-shadow: inset 0 -3px 0 var(--tally); }

/* ================= Текст главы ================= */
.reader { display: flex; flex-direction: column; min-height: 0; }
.reader-head {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
}
.reader-head h1 { flex: 1; margin: 0; font: 700 21px var(--read); text-align: center; }
.ch-step { padding: 2px 12px; font-size: 18px; line-height: 1.3; }

.verses { list-style: none; margin: 0; padding: 8px 14px 60px; overflow-y: auto; flex: 1; outline: none; }
.verse {
  position: relative;
  display: grid;
  grid-template-columns: 34px 1fr;
  padding: 7px 12px 7px 4px;
  border-left: 3px solid transparent;
  border-radius: 6px;
  font: 17px/1.55 var(--read);
  cursor: pointer;
  user-select: none;
}
.verse:hover { background: var(--panel); }
.verse .num { font: 12px/2.3 var(--ui); color: var(--muted); text-align: right; padding-right: 10px; }
.verse.selected { background: var(--candle-soft); border-left-color: var(--candle); }
.verse.on-air { border-left-color: var(--tally); }
.verse.on-air .num { color: var(--tally); font-weight: 700; }
.verse.saved::after {
  content: "";
  position: absolute;
  right: 6px;
  top: 14px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--candle);
}

.results { flex: 1; overflow-y: auto; padding: 0 14px 60px; }
.results-head {
  position: sticky;
  top: 0;
  display: flex;
  gap: 16px;
  align-items: center;
  padding: 12px 6px;
  background: var(--bg);
  color: var(--muted);
}
.result {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 10px 8px;
}
.result:hover { background: var(--panel); }
.result-ref { display: block; color: var(--candle); font-size: 13px; }
.result-text { font: 16px/1.45 var(--read); }

/* ================= Правая колонка ================= */
.side {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 12px 16px;
  border-left: 1px solid var(--line);
  overflow-y: auto;
  overflow-x: hidden;
}
.monitor-label { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; color: var(--muted); font-size: 13px; }
.monitor-ref { color: var(--text); text-align: right; }
.mini { --ar: 1.7778; width: min(100%, calc(30vh * var(--ar))); margin: 0 auto; aspect-ratio: 16 / 9; border-radius: 6px; border: 2px solid var(--line); }
#prepScreen { border-color: rgba(224, 182, 92, 0.6); }
.live.on-air .mini { border-color: var(--tally); }
.mini-wrap { position: relative; }
.mini-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 8px;
  pointer-events: none;
  font-size: 12px;
  color: var(--muted);
  opacity: 0;
  transition: opacity 0.3s;
}
.mini-overlay.show { opacity: 1; }
.mini-overlay span {
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(20, 24, 32, 0.85);
}
.live:not(.connected) .mini { filter: saturate(0.4) brightness(0.7); }

.row { display: flex; gap: 6px; margin-top: 8px; }
.btn-show { flex: 1; padding: 10px; font-weight: 600; background: var(--candle); border-color: var(--candle); color: #1d2129; }
.btn-show kbd { color: #1d2129; border-color: rgba(0, 0, 0, 0.3); }
.btn-add { white-space: nowrap; }

.tally { display: inline-flex; align-items: center; gap: 6px; }
.tally::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--line); }
.tally.on { color: var(--tally); font-weight: 600; }
.tally.on::before { background: var(--tally); box-shadow: 0 0 8px var(--tally); }

.live-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
#btnBlack.on { background: #000; border-color: var(--text); }

/* ================= Вкладки ================= */
.layout > * { min-width: 0; }
.tools {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 12px 16px;
  border-left: 1px solid var(--line);
}
.tabs { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.tab-buttons { display: flex; gap: 2px; border-bottom: 1px solid var(--line); }
.tab-buttons .tab { flex: 1; text-align: center; white-space: nowrap; }
.tab { background: none; border: none; border-bottom: 2px solid transparent; border-radius: 0; color: var(--muted); padding: 6px 10px; }
.tab.active { color: var(--text); border-bottom-color: var(--candle); }
.panel { padding-top: 10px; flex: 1; min-height: 0; overflow-y: auto; }
/* Панели прокручиваются и срезают всё, что выходит за край: рамку фокуса рисуем внутри */
.panel :focus-visible { outline-offset: -2px; }
.panel[hidden] { display: none; }
/* В коллекциях и истории панель кнопок стоит на месте, прокручивается только список */
.panel[data-panel="collections"], .panel[data-panel="history"] { display: flex; flex-direction: column; overflow: hidden; }
.panel[data-panel="collections"][hidden], .panel[data-panel="history"][hidden] { display: none; }
.col-items, .history { flex: 1; min-height: 0; overflow-y: auto; }

/* Коллекции */
.col-bar { display: flex; gap: 6px; }
.col-new { white-space: nowrap; }
.col-bar select { flex: 1; min-width: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
.menu { position: relative; }
.menu-list {
  position: fixed;
  z-index: 100;
  display: flex;
  flex-direction: column;
  min-width: 200px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
}
.menu-list[hidden] { display: none; }
.menu-list button { background: none; border: none; text-align: left; padding: 7px 10px; }
.menu-list button:hover { background: var(--panel-2); }

.col-items { list-style: none; margin: 8px 0 0; padding: 0; }
.col-items .empty { color: var(--muted); padding: 10px 4px; }
.col-item {
  position: relative;
  display: flex;
  align-items: stretch;
  border-radius: 7px;
  border-left: 3px solid transparent;
}
.col-item:hover { background: var(--panel); }
.col-item.on-air { border-left-color: var(--tally); background: var(--tally-soft); }
.col-item.dragging { opacity: 0.4; }
.col-item.drop { box-shadow: inset 0 2px 0 var(--candle); }
.col-main {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: 22px 1fr;
  text-align: left;
  background: none;
  border: none;
  padding: 7px 6px;
  cursor: grab;
}
.col-idx { grid-row: span 2; color: var(--muted); font-size: 12px; padding-top: 2px; }
.col-ref { font-weight: 500; }
.col-snippet { color: var(--muted); font: 13px/1.35 var(--read); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Кнопки записи (↑ ↓ ✕) в коллекциях и истории — видны при наведении и при фокусе с клавиатуры.
   Не :focus-within у всей записи: после клика мышью фокус остаётся на ней, и кнопки висели бы,
   когда мышь уже ушла. :focus-visible бывает только при фокусе с клавиатуры */
.row-tools {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 2px;
  padding: 2px;
  border-radius: 6px;
  background: var(--panel-2);
  opacity: 0;
  pointer-events: none;
}
.col-item:hover .row-tools, .hist-item:hover .row-tools,
.col-main:focus-visible + .row-tools, .hist-main:focus-visible + .row-tools,
.row-tools:focus-within { opacity: 1; pointer-events: auto; }
.row-tools button { padding: 2px 7px; font-size: 13px; background: none; }

/* История */
.history-bar { display: flex; justify-content: flex-end; }
.history-bar[hidden] { display: none; }
.history-clear { padding: 4px 8px; font-size: 13px; background: none; border: none; }
.history-clear:hover { background: var(--panel); }
.history { list-style: none; margin: 4px 0 0; padding: 0; }
.hist-item { position: relative; border-radius: 6px; }
.hist-item:hover { background: var(--panel); }
.hist-main { width: 100%; text-align: left; background: none; border: none; padding: 5px 8px; }
.history .empty { color: var(--muted); padding: 10px 4px; }

/* Оформление */
/* minmax(0, 1fr): иначе ширина списков и ползунков распирает колонку и поля обрезаются.
   Отступы по краям — чтобы рамка фокуса не срезалась прокруткой панели */
.settings { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px 14px; padding: 3px 4px 12px; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; color: var(--muted); font-size: 13px; }
.wide { grid-column: 1 / -1; }
.check { display: flex; align-items: flex-start; gap: 8px; }
.check input { margin: 3px 0 0; flex: none; }
.check small { display: block; color: var(--muted); font-size: 12px; }
.settings select { width: 100%; min-width: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px; }
.settings input[type="range"] { flex: 1; min-width: 0; margin: 0; accent-color: var(--candle); }

.range { display: flex; align-items: center; gap: 6px; }
.step { flex: none; width: 26px; height: 26px; padding: 0; border-radius: 50%; font-size: 15px; line-height: 1; color: var(--muted); }
.step:hover:not(:disabled) { color: var(--text); }

/* Плашки цвета — обычные кнопки; палитру открывает временный скрытый input (pickColor в app.js) */
.color-well {
  position: relative;
  display: block;
  width: 100%;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: inset 0 0 0 3px var(--panel);
  overflow: hidden;
}
.color-input { position: absolute; left: 50%; bottom: 0; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0; pointer-events: none; }
/* Кисть справа: на тёмной подложке, чтобы читалась на любом выбранном цвете */
.brush {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 19px;
  height: 19px;
  padding: 3px;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.45);
  fill: #f6f1e4;
}
.color-well .brush { top: 50%; right: 6px; transform: translateY(-50%); }

.swatches { display: grid; grid-template-columns: repeat(auto-fit, minmax(56px, 1fr)); gap: 6px; }
.swatch {
  position: relative;
  height: 46px;
  padding: 0;
  border: 2px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  cursor: pointer;
}
.swatch span {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 3px;
  font-size: 11px;
  color: #f6f1e4;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.swatch.active, .swatch.active:hover { border-color: var(--candle); }

/* ================= Подвал ================= */
.hints {
  display: flex;
  flex-wrap: wrap;
  gap: 18px;
  padding: 8px 16px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}
.hints kbd { margin: 0 2px 0 0; }
.toast { margin-left: auto; color: var(--candle); opacity: 0; transition: opacity 0.2s; }
.toast.show { opacity: 1; }
.toast.error { color: #f08a80; }

/* ================= Диалог (вместо prompt/confirm) ================= */
.dlg {
  width: min(440px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.dlg::backdrop { background: rgba(10, 12, 16, 0.55); }
.dlg form { display: flex; flex-direction: column; gap: 14px; padding: 18px 20px; }
.dlg-text { margin: 0; line-height: 1.45; }
.dlg input { padding: 8px 10px; font-size: 15px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; outline: none; }
.dlg input:focus { border-color: var(--candle); }
.dlg-buttons { display: flex; justify-content: flex-end; gap: 8px; }
.dlg-ok { font-weight: 600; background: var(--candle); border-color: var(--candle); color: #1d2129; }
.dlg-ok.danger { background: transparent; border-color: rgba(240, 138, 128, 0.6); color: #f08a80; }

/* Средний экран: четыре колонки, но уже */
@media (max-width: 1450px) {
  .layout { grid-template-columns: 190px minmax(300px, 1fr) 290px 270px; }
  .nav-head { padding: 6px 8px; }
  .crumbs { font-size: 13px; }
  .crumb { padding: 5px 5px; }
  .books-grid { grid-template-columns: repeat(3, 1fr); }
  .side, .tools { padding: 12px; }
  .col-bar { flex-wrap: wrap; }
  .col-bar select { flex: 1 1 100%; }
  .col-bar button { flex: 1; }
  .col-bar .menu { flex: 0 0 auto; }
  .tab { padding: 6px 4px; font-size: 13px; }
  .live-controls button { font-size: 13px; padding: 7px 6px; }
}

/* Узкий экран: вкладки уходят под мониторы */
@media (max-width: 1180px) {
  .layout {
    grid-template-columns: 220px minmax(320px, 1fr) minmax(330px, 32%);
    grid-template-rows: auto 1fr;
  }
  .nav, .reader { grid-row: 1 / span 2; }
  .side { grid-column: 3; grid-row: 1; overflow: visible; }
  .tools { grid-column: 3; grid-row: 2; border-top: 1px solid var(--line); }
  .mini { width: min(100%, calc(20vh * var(--ar))); }
  .books-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}
