:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d1d1f;
  --muted: #7a7a80;
  --line: #e2e0db;
  --accent: #e0362c;
  --primary: #1d1d1f;
  --primary-text: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141416;
    --surface: #1d1d20;
    --text: #ececef;
    --muted: #8b8b93;
    --line: #2e2e33;
    --accent: #ff5a4e;
    --primary: #ececef;
    --primary-text: #141416;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

/* Отступы под чёлку и полосу «Домой», когда приложение открыто с экрана «Домой» */
body {
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: calc(14px + env(safe-area-inset-top)) 20px 14px;
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: -0.01em; }
.file-name {
  flex: 1;
  min-width: 0;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
}
.ghost:hover { border-color: var(--muted); }

.link {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
}

main {
  flex: 1;
  min-height: 0; /* иначе текст на паузе растянет страницу, а не прокрутится внутри */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

/* Library */
.library {
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.books h2 {
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  margin: 0 0 10px;
}
.books ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.books li {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.book-open {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px 12px;
  padding: 12px 16px;
  background: none;
  border: none;
  text-align: left;
}
.book-name {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-percent {
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.book-progress {
  grid-column: 1 / -1;
  height: 3px;
  background: var(--line);
  border-radius: 2px;
  overflow: hidden;
}
.book-progress span { display: block; height: 100%; background: var(--accent); }
.book-remove {
  width: 44px;
  background: none;
  border: none;
  border-left: 1px solid var(--line);
  color: var(--muted);
  font-size: 20px;
}
.book-remove:hover { color: var(--accent); }

/* Drop zone */
.dropzone {
  border: 2px dashed var(--line);
  border-radius: 16px;
  padding: 64px 24px;
  text-align: center;
  transition: border-color .15s, background .15s;
}
.dropzone.over { border-color: var(--accent); background: var(--surface); }
.drop-title { font-size: 22px; font-weight: 600; margin-bottom: 8px; }
.drop-sub { color: var(--muted); }
.drop-status { margin-top: 16px; min-height: 1.4em; color: var(--muted); }
.drop-status.error { color: var(--accent); }

/* Reader */
.reader {
  width: 100%;
  max-width: 820px;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chapters {
  display: flex;
  gap: 8px;
  align-items: center;
}
.chapters select {
  flex: 1;
  min-width: 0;
  height: 36px;
  padding: 0 10px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.chapters button {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 18px;
}
.chapters button:disabled { opacity: .35; cursor: default; }

.stage {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  height: 200px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  cursor: pointer;
}

/* Marks above/below the pivot letter */
.guide {
  position: absolute;
  left: 50%;
  width: 2px;
  height: 18px;
  background: var(--accent);
  opacity: .5;
  transform: translateX(-50%);
}
.guide.top { top: 40px; }
.guide.bottom { bottom: 40px; }

.word {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  font-size: clamp(32px, 7vw, 56px);
  font-family: "SF Mono", Menlo, Consolas, monospace;
  line-height: 1;
  white-space: pre;
}
.word .left { text-align: right; }
.word .pivot { color: var(--accent); }
.word .right { text-align: left; }

/* Во время чтения слово занимает всё свободное место, на паузе — уступает его тексту */
.reader.is-playing .stage { flex: 1; min-height: 200px; }

.textview {
  flex: 1;
  min-height: 240px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.find { display: flex; gap: 8px; }
.find input {
  flex: 1;
  min-width: 0;
  height: 36px;
  padding: 0 12px;
  font: inherit;
  font-size: 16px; /* меньше 16px — iPhone приближает страницу при вводе */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.find button {
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
}
.find-status { color: var(--muted); font-size: 13px; }

.context {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px 20px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 17px;
  line-height: 1.6;
  -webkit-overflow-scrolling: touch;
}
.context p { margin: 0.7em 0; }
.context h3 {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  margin: 1.6em 0 0.6em;
  color: var(--muted);
}
.context span { cursor: pointer; border-radius: 3px; }
.context .current { background: color-mix(in srgb, var(--accent) 28%, transparent); }
.context .found { box-shadow: 0 2px 0 var(--accent); }

/* Напоминание о перерыве — вместо слова; окно подстраивается под текст */
.reader.on-break .stage { height: auto !important; min-height: 160px; }
.reader.on-break .word,
.reader.on-break .guide { display: none; }
.break-notice {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px;
  text-align: center;
  background: var(--surface);
  border-radius: 16px;
  cursor: default;
}
.break-title { font-size: 20px; font-weight: 600; }
.break-text { color: var(--muted); max-width: 420px; }
.break-notice button {
  margin-top: 6px;
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  background: var(--primary);
  color: var(--primary-text);
}

/* Панель настроек */
.settings-toggle { display: flex; justify-content: center; }
.settings-toggle .ghost { font-size: 13px; padding: 4px 12px; }
.settings-toggle .ghost[aria-expanded="true"] { border-color: var(--muted); }

.settings {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.settings-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.settings-head b { color: var(--text); font-weight: 600; }

.setting-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  min-height: 36px;
}
.setting-name small { display: block; color: var(--muted); font-size: 12px; }
.setting-ms {
  min-width: 64px;
  text-align: right;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.stepper { display: flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.stepper b { min-width: 48px; text-align: center; font-weight: 600; }
.stepper button {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg);
  font-size: 16px;
  line-height: 1;
}
.stepper button:disabled { opacity: .35; cursor: default; }
.miss {
  height: 48px;
  margin-bottom: 4px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  transition: transform .1s, filter .15s;
}
.miss.flash { transform: scale(0.97); filter: brightness(1.2); }
.setting-row select {
  grid-column: 2 / -1;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 6px;
}

.progress input { width: 100%; accent-color: var(--accent); }
.progress-info {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}
.controls > button, .wpm button {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 16px;
}
.controls > button.primary {
  width: 60px;
  height: 60px;
  background: var(--primary);
  color: var(--primary-text);
  border: none;
  font-size: 22px;
}
.wpm {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: 16px;
  font-variant-numeric: tabular-nums;
}
.wpm button { width: 36px; height: 36px; font-size: 18px; }

.hint { text-align: center; color: var(--muted); font-size: 13px; }

@media (max-width: 520px) {
  .topbar { gap: 8px; padding-left: 16px; padding-right: 16px; }
  .brand { display: none; }
  main { justify-content: flex-start; padding-top: 16px; }
  .reader { gap: 12px; }
  .stage { height: 140px; }
  .reader:not(.is-playing) .stage { height: 96px; }
  .reader.is-playing .stage { min-height: 120px; }
  .settings { padding: 8px 12px; gap: 2px; }
  .setting-name small { display: none; }
  .setting-row { gap: 6px; min-height: 34px; }
  .setting-ms { min-width: 52px; font-size: 12px; }
  .reader:not(.is-playing) .guide { display: none; }
  .context { padding: 2px 16px; }
  .wpm { margin-left: 0; width: 100%; justify-content: center; }
  .hint { display: none; }
}
