/* Mini App «Книжная полка».
   Оформление приложения — из темы Telegram, текст книги — из своих
   переменных (ночной режим, шрифт, фон). */

:root {
  --tg-bg: var(--tg-theme-bg-color, #ffffff);
  --tg-text: var(--tg-theme-text-color, #111111);
  --tg-hint: var(--tg-theme-hint-color, #707579);
  --tg-link: var(--tg-theme-link-color, #2481cc);
  --tg-button: var(--tg-theme-button-color, #2481cc);
  --tg-button-text: var(--tg-theme-button-text-color, #ffffff);
  --tg-card: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --reader-bg: #ffffff;
  --reader-fg: #1b1b1b;
  --reader-font: Georgia, "Times New Roman", serif;
  --reader-size: 18px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}

#app { min-height: 100%; }

.state { padding: 48px 24px; text-align: center; color: var(--tg-hint); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--tg-bg);
  border-bottom: 1px solid rgba(127, 127, 127, .18);
}
.topbar .title { font-size: 18px; font-weight: 700; }
.topbar .actions { display: flex; gap: 4px; }

.iconbtn {
  border: none;
  background: transparent;
  color: var(--tg-text);
  font-size: 18px;
  line-height: 1;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  cursor: pointer;
}
.iconbtn:active { background: var(--tg-card); }
.iconbtn.wide { width: auto; padding: 0 10px; font-size: 16px; font-weight: 600; }

/* === ПОЛКА === */

.library { padding: 12px; display: grid; gap: 10px; }

.book {
  display: flex;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 14px;
  background: var(--tg-card);
  color: var(--tg-text);
  text-align: left;
  cursor: pointer;
}
.book:active { opacity: .85; }
.book-cover {
  width: 56px;
  height: 80px;
  flex: none;
  border-radius: 8px;
  object-fit: cover;
  background: rgba(127, 127, 127, .18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
}
.book-body { flex: 1; min-width: 0; }
.book-title { font-weight: 700; font-size: 16px; margin: 0 0 2px; }
.book-author { color: var(--tg-hint); font-size: 13px; margin: 0 0 8px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-meta { display: flex; justify-content: space-between; font-size: 12px;
  color: var(--tg-hint); margin-top: 6px; }
.bar { height: 4px; border-radius: 2px; background: rgba(127, 127, 127, .22); overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--tg-button); }

.empty { padding: 56px 24px; text-align: center; }
.empty-icon { font-size: 44px; margin-bottom: 12px; }
.empty h2 { font-size: 18px; margin: 0 0 8px; }
.empty p { color: var(--tg-hint); margin: 0; }

/* === ЧИТАЛКА === */

.reader {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--reader-bg);
  color: var(--reader-fg);
  z-index: 10;
}
.reader-top, .reader-bottom {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  background: var(--tg-bg);
  color: var(--tg-text);
  border-color: rgba(127, 127, 127, .18);
}
.reader-top { border-bottom: 1px solid rgba(127, 127, 127, .18); }
.reader-bottom { border-top: 1px solid rgba(127, 127, 127, .18); }
.reader-title {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reader-content {
  position: relative;
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 18px 18px 48px;
  font-family: var(--reader-font);
  font-size: var(--reader-size);
  line-height: 1.65;
  background: var(--reader-bg);
  color: var(--reader-fg);
  overflow-wrap: break-word;
}
.reader-content > * { max-width: 720px; margin-left: auto; margin-right: auto; }
.reader-content p { margin: 0 auto .85em; text-indent: 1.1em; }
.reader-content p:first-of-type { text-indent: 0; }
.reader-content h1, .reader-content h2, .reader-content h3 {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  line-height: 1.3;
  margin: 1.1em auto .6em;
}
.reader-content h1 { font-size: 1.45em; }
.reader-content h2 { font-size: 1.28em; }
.reader-content h3 { font-size: 1.12em; }
.reader-content img { display: block; max-width: 100%; height: auto; margin: 1em auto; }
.reader-content blockquote {
  margin: 1em auto;
  padding: .2em 0 .2em 1em;
  border-left: 3px solid rgba(127, 127, 127, .4);
  font-style: italic;
}
.reader-content hr { border: none; border-top: 1px solid rgba(127, 127, 127, .3); margin: 1.4em 0; }
.reader-content table { border-collapse: collapse; width: 100%; font-size: .95em; }
.reader-content td, .reader-content th { border: 1px solid rgba(127, 127, 127, .3); padding: 4px 6px; }
.reader-content sup, .reader-content sub { font-size: .7em; }

.navbtn {
  border: none;
  background: var(--tg-card);
  color: var(--tg-text);
  width: 42px;
  height: 38px;
  border-radius: 10px;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.navbtn:disabled { opacity: .35; }
.chapter-label {
  flex: 1;
  text-align: center;
  font-size: 12px;
  color: var(--tg-hint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 8px;
}

/* === ШТОРКИ === */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  z-index: 20;
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  max-height: 82vh;
  overflow-y: auto;
  padding: 12px 14px calc(18px + env(safe-area-inset-bottom));
  background: var(--tg-bg);
  color: var(--tg-text);
  border-radius: 18px 18px 0 0;
  transform: translateY(105%);
  transition: transform .22s ease;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .18);
}
.sheet.open { transform: translateY(0); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-title { font-size: 17px; font-weight: 700; }

.setting { padding: 12px 2px; border-bottom: 1px solid rgba(127, 127, 127, .15); }
.setting:last-child { border-bottom: none; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.setting-label { font-size: 15px; }
.setting-hint { color: var(--tg-hint); font-size: 12px; margin-top: 4px; }

.switch {
  width: 48px;
  height: 28px;
  border-radius: 14px;
  border: none;
  background: rgba(127, 127, 127, .35);
  position: relative;
  cursor: pointer;
  transition: background .15s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s;
}
.switch.on { background: var(--tg-button); }
.switch.on::after { transform: translateX(20px); }

.segments { display: flex; gap: 6px; margin-top: 8px; }
.seg {
  flex: 1;
  padding: 10px 6px;
  border: 1px solid rgba(127, 127, 127, .3);
  border-radius: 10px;
  background: transparent;
  color: var(--tg-text);
  font-size: 13px;
  cursor: pointer;
}
.seg.active { background: var(--tg-button); border-color: var(--tg-button); color: var(--tg-button-text); }

.stepper { display: flex; align-items: center; gap: 10px; }
.stepper button {
  width: 40px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid rgba(127, 127, 127, .3);
  background: transparent;
  color: var(--tg-text);
  font-size: 20px;
  cursor: pointer;
}
.stepper span { min-width: 42px; text-align: center; font-weight: 600; }

.swatches { display: flex; gap: 10px; margin-top: 10px; }
.swatch {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid rgba(127, 127, 127, .3);
  cursor: pointer;
  padding: 0;
}
.swatch.active { border-color: var(--tg-button); box-shadow: 0 0 0 2px var(--tg-button); }

/* === ЗАКЛАДКИ === */

.bookmark {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  padding: 12px 4px;
  border: none;
  border-bottom: 1px solid rgba(127, 127, 127, .15);
  background: transparent;
  color: var(--tg-text);
  text-align: left;
  cursor: pointer;
}
.bookmark-body { flex: 1; min-width: 0; }
.bookmark-title { font-weight: 600; font-size: 14px; }
.bookmark-meta { color: var(--tg-hint); font-size: 12px; margin-top: 2px; }
.bookmark-snippet { color: var(--tg-hint); font-size: 13px; margin-top: 4px; }
.bookmark-del {
  border: none;
  background: transparent;
  color: var(--tg-hint);
  font-size: 18px;
  padding: 2px 6px;
  cursor: pointer;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  z-index: 40;
  max-width: 86%;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(20, 20, 20, .92);
  color: #fff;
  font-size: 14px;
  text-align: center;
}