/* Tela de leitura: painel Aa, dicionário, rodapé (estilo Kindle). */

/* Tokens próprios, derivados do tema de leitura (--bg/--fg/--accent). */
:root {
  --rk-surface: color-mix(in oklab, var(--fg) 5%, var(--bg));
  --rk-surface-2: color-mix(in oklab, var(--fg) 9%, var(--bg));
  --rk-elev: color-mix(in oklab, var(--fg) 2.5%, var(--bg));
  --rk-line: color-mix(in oklab, var(--fg) 13%, var(--bg));
  --rk-line-strong: color-mix(in oklab, var(--fg) 26%, var(--bg));
  --rk-muted: color-mix(in oklab, var(--fg) 58%, var(--bg));
  --rk-accent-soft: color-mix(in oklab, var(--accent) 14%, var(--bg));
  --rk-accent-fg: color-mix(in oklab, var(--accent) 82%, var(--fg));
  --rk-serif: 'Literata', Georgia, serif;
  --rk-shadow: 0 1px 3px rgb(0 0 0 / .08), 0 14px 40px rgb(0 0 0 / .18);
}
:root[data-dark="1"] {
  --rk-elev: color-mix(in oklab, var(--fg) 8%, var(--bg));
  --rk-shadow: 0 1px 3px rgb(0 0 0 / .4), 0 14px 40px rgb(0 0 0 / .55);
}

/* ============ Barras do leitor ============ */
.reader .rk-top, .reader .rk-bottom {
  background: color-mix(in oklab, var(--bg) 84%, transparent);
  -webkit-backdrop-filter: blur(16px) saturate(1.15);
  backdrop-filter: blur(16px) saturate(1.15);
  border-color: color-mix(in oklab, var(--fg) 9%, transparent);
  color: var(--fg);
}
.reader .rk-top {
  display: grid;
  grid-template-columns: minmax(44px, 1fr) minmax(0, 560px) minmax(max-content, 1fr);
  gap: 8px;
  height: calc(54px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
}
.rk-left { display: flex; align-items: center; }
.rk-titles { text-align: center; padding: 0; min-width: 0; }
.rk-titles .r-book { font-size: .88rem; font-weight: 600; letter-spacing: -.005em; }
.rk-titles .r-chapter { font-size: .74rem; color: var(--rk-muted); margin-top: 1px; }
.rk-titles .r-chapter:empty { display: none; }
.rk-actions { justify-content: flex-end; align-items: center; gap: 0; }
.reader .rk-top .icon-btn, .reader .rk-bottom .icon-btn { color: color-mix(in oklab, var(--fg) 78%, var(--bg)); border-radius: 10px; }
.reader .rk-top .icon-btn:hover, .reader .rk-bottom .icon-btn:hover { background: color-mix(in oklab, var(--fg) 7%, transparent); color: var(--fg); }
.reader .rk-top .icon-btn.active { color: var(--accent); background: var(--rk-accent-soft); }
.rk-aa { font-family: var(--rk-serif); font-weight: 600; line-height: 1; gap: 0; align-items: baseline !important; padding-top: 4px; }
.rk-aa span:first-child { font-size: 1.22rem; }
.rk-aa span:last-child { font-size: .92rem; }

.reader .rk-bottom { height: calc(52px + env(safe-area-inset-bottom)); padding: 0 max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left)); gap: 8px; }
.rk-pct { color: var(--rk-muted); font-size: .76rem; min-width: 44px; }

/* Slider de progresso fino, sem cara de formulário */
.rk-slider, .aa-bright input[type=range], .ms-row input[type=range] {
  -webkit-appearance: none; appearance: none; background: transparent; height: 28px; cursor: pointer; margin: 0;
}
.rk-slider::-webkit-slider-runnable-track, .aa-bright input[type=range]::-webkit-slider-runnable-track, .ms-row input[type=range]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 4px;
  background: linear-gradient(to right, var(--accent) var(--p, 50%), var(--rk-line-strong) var(--p, 50%));
}
.rk-slider::-moz-range-track, .aa-bright input[type=range]::-moz-range-track, .ms-row input[type=range]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--rk-line-strong); }
.rk-slider::-moz-range-progress, .aa-bright input[type=range]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--accent); }
.rk-slider::-webkit-slider-thumb, .aa-bright input[type=range]::-webkit-slider-thumb, .ms-row input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px; margin-top: -7px; border-radius: 50%;
  background: #fff; border: 0; box-shadow: 0 0 0 1px rgb(0 0 0 / .14), 0 1px 4px rgb(0 0 0 / .25);
}
.rk-slider::-moz-range-thumb, .aa-bright input[type=range]::-moz-range-thumb, .ms-row input[type=range]::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%; background: #fff; border: 0; box-shadow: 0 0 0 1px rgb(0 0 0 / .14), 0 1px 4px rgb(0 0 0 / .25);
}
.ms-row input[type=range]::-webkit-slider-runnable-track { background: var(--rk-line-strong); }

/* Rodapé discreto, como no Kindle */
.reader .r-footer { font-size: .74rem; color: var(--rk-muted); padding-bottom: max(4px, env(safe-area-inset-bottom)); }
.reader .r-header { font-size: .74rem; color: var(--rk-muted); }

/* ============ Painel "Aa" ============ */
.aa-scrim { position: absolute; inset: 0; z-index: 39; background: transparent; }
.aa-box {
  position: absolute; z-index: 70; width: min(372px, calc(100vw - 16px));
  max-height: calc(100dvh - 80px); overflow-y: auto; overscroll-behavior: contain;
  background: var(--rk-elev); color: var(--fg);
  border: 1px solid var(--rk-line); border-radius: 16px; box-shadow: var(--rk-shadow);
  padding: 4px 18px 12px;
  opacity: 0; transform: translateY(-6px) scale(.985); transform-origin: var(--caret, 50%) 0;
  transition: opacity .16s ease, transform .18s ease;
  scrollbar-width: thin;
}
.aa-box.show { opacity: 1; transform: none; }
.aa-grab { display: none; }

.aa-box.sheet {
  left: 0; right: 0; bottom: 0; top: auto !important; width: auto;
  max-height: min(78dvh, 640px);
  border-radius: 20px 20px 0 0; border-width: 1px 0 0;
  padding: 0 max(16px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  transform: translateY(100%); opacity: 1;
  transition: transform .26s cubic-bezier(.2, .8, .2, 1);
}
.aa-box.sheet.show { transform: none; }
.aa-box.sheet .aa-grab {
  display: flex; justify-content: center; align-items: center; height: 26px; margin: 0 -16px; cursor: grab; touch-action: none;
  position: sticky; top: 0; z-index: 1; background: var(--rk-elev);
}
.aa-box.sheet .aa-grab i { width: 38px; height: 4px; border-radius: 4px; background: var(--rk-line-strong); }
@media (max-width: 600px) {
  .aa-scrim { background: rgb(0 0 0 / 0); transition: background .2s; }
  .aa-scrim.show { background: rgb(0 0 0 / .16); }
}

.aa { display: flex; flex-direction: column; }
.aa-sec { padding: 11px 0; border-top: 1px solid var(--rk-line); }
.aa-sec:first-child { border-top: 0; }
.aa-label { font-size: .76rem; font-weight: 600; color: var(--rk-muted); margin-bottom: 9px; letter-spacing: .01em; }
.aa-label-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.aa-val { font-weight: 500; font-variant-numeric: tabular-nums; color: var(--rk-muted); opacity: .85; }

/* Temas */
.aa-themes {
  display: flex; gap: 10px; overflow-x: auto; scroll-snap-type: x proximity; scrollbar-width: none;
  margin: 0 -18px; padding: 4px 18px 2px;
  -webkit-mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 calc(100% - 22px), transparent);
          mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 calc(100% - 22px), transparent);
}
.aa-themes::-webkit-scrollbar, .aa-fonts::-webkit-scrollbar { display: none; }
.aa-theme { flex: none; display: flex; flex-direction: column; align-items: center; gap: 5px; border: 0; background: transparent; padding: 0; width: 56px; scroll-snap-align: start; color: var(--rk-muted); }
.aa-theme-dot {
  width: 50px; height: 50px; border-radius: 50%; display: grid; place-items: center;
  font-size: 1.12rem; font-weight: 600; line-height: 1;
  box-shadow: inset 0 0 0 1px rgb(127 127 127 / .35);
  transition: box-shadow .15s, transform .12s;
}
.aa-theme:hover .aa-theme-dot { transform: translateY(-1px); }
.aa-theme.on .aa-theme-dot { box-shadow: inset 0 0 0 1px rgb(127 127 127 / .3), 0 0 0 2px var(--rk-elev), 0 0 0 4px var(--accent); }
.aa-theme-name { font-size: .68rem; white-space: nowrap; max-width: 60px; overflow: hidden; text-overflow: ellipsis; }
.aa-theme.on .aa-theme-name { color: var(--fg); font-weight: 600; }

/* Tamanho da letra */
.aa-size { display: flex; align-items: center; gap: 6px; }
.aa-a {
  flex: none; width: 40px; height: 40px; border-radius: 10px; border: 0; background: transparent; color: var(--fg);
  font-family: var(--rk-serif); font-weight: 600; line-height: 1; display: grid; place-items: center;
}
.aa-a:hover { background: var(--rk-surface-2); }
.aa-a:active { transform: scale(.94); }
.aa-a.small { font-size: .92rem; }
.aa-a.big { font-size: 1.6rem; }
.aa-steps { flex: 1; display: flex; gap: 3px; height: 40px; align-items: center; border-radius: 8px; outline-offset: 2px; }
.aa-steps i { flex: 1; height: 100%; position: relative; cursor: pointer; }
.aa-steps i::after {
  content: ''; position: absolute; left: 0; right: 0; top: 50%; height: 8px; margin-top: -4px; border-radius: 2px;
  background: var(--rk-line); transition: background .12s;
}
.aa-steps i:first-child::after { border-radius: 5px 2px 2px 5px; }
.aa-steps i:last-child::after { border-radius: 2px 5px 5px 2px; }
.aa-steps i.on::after { background: var(--accent); }
.aa-steps i:hover::after { background: color-mix(in oklab, var(--accent) 55%, var(--rk-line)); }
.aa-steps i.on:hover::after { background: color-mix(in oklab, var(--accent) 85%, var(--fg)); }

/* Fontes */
.aa-fonts {
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; scroll-snap-type: x proximity;
  margin: 0 -18px; padding: 2px 18px;
  -webkit-mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 calc(100% - 22px), transparent);
          mask-image: linear-gradient(to right, transparent 0, #000 14px, #000 calc(100% - 22px), transparent);
}
.aa-font {
  flex: none; height: 40px; padding: 0 14px; border-radius: 10px; white-space: nowrap; scroll-snap-align: start;
  border: 1px solid var(--rk-line); background: transparent; color: var(--fg); font-size: 1rem; line-height: 1;
  transition: background .12s, border-color .12s;
}
.aa-font:hover { background: var(--rk-surface); }
.aa-font.on { border-color: var(--accent); background: var(--rk-accent-soft); color: var(--rk-accent-fg); box-shadow: inset 0 0 0 1px var(--accent); }

/* Segmentados */
.kseg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: var(--rk-surface-2); }
.kseg.full { display: flex; width: 100%; }
.kseg button {
  flex: 1; min-width: 34px; height: 34px; padding: 0 10px; border: 0; border-radius: 8px; background: transparent;
  color: var(--rk-muted); font-size: .84rem; font-weight: 500; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  white-space: nowrap; transition: background .12s, color .12s;
}
.kseg button:hover { color: var(--fg); }
.kseg button.on { background: var(--bg); color: var(--fg); box-shadow: 0 1px 3px rgb(0 0 0 / .14); }
:root[data-dark="1"] .kseg button.on { background: color-mix(in oklab, var(--fg) 18%, var(--bg)); }
.aa-row { display: flex; gap: 8px; align-items: center; }
.aa-row > .kseg:first-child { flex: 1; }

.aa-layout { display: grid; grid-template-columns: 1.5fr 1.5fr 1fr; gap: 8px; }
.aa-lcol { display: flex; flex-direction: column; align-items: stretch; gap: 5px; min-width: 0; }
.aa-lcol .kseg { display: flex; }
.aa-lcol .kseg button { padding: 0; min-width: 0; }
.aa-lcol small { text-align: center; font-size: .68rem; color: var(--rk-muted); }

/* Brilho */
.aa-bright { display: flex; align-items: center; gap: 10px; color: var(--rk-muted); }
.aa-bright input { flex: 1; min-width: 0; }

.aa-more {
  align-self: center; margin-top: 4px; border: 0; background: transparent; color: var(--rk-accent-fg);
  font-size: .84rem; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; border-radius: 8px;
}
.aa-more:hover { background: var(--rk-accent-soft); }

/* ============ "Mais ajustes" (painel lateral e modal da biblioteca) ============ */
.more-set { display: flex; flex-direction: column; gap: 22px; padding-top: 4px; }
.ms-group h4 { margin: 0 0 10px; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--rk-muted); }
.ms-group > .aa { border: 1px solid var(--rk-line); border-radius: 14px; padding: 2px 16px 6px; overflow: hidden; }
.ms-group > .aa .aa-themes, .ms-group > .aa .aa-fonts { margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.ms-rows { display: flex; flex-direction: column; gap: 14px; }
.ms-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.ms-row.stack { flex-direction: column; align-items: stretch; gap: 8px; }
.ms-row.stack .ms-text { flex-direction: row; justify-content: space-between; }
.ms-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.ms-text > span { font-size: .9rem; font-weight: 500; }
.ms-text small { font-size: .76rem; color: var(--rk-muted); }
.ms-val { font-size: .82rem; color: var(--rk-muted); font-variant-numeric: tabular-nums; }
.ms-row .kseg button { font-size: .8rem; padding: 0 6px; }

.ms-toggle { cursor: pointer; }
.ms-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.ms-toggle i { flex: none; width: 42px; height: 26px; border-radius: 999px; background: var(--rk-line-strong); position: relative; transition: background .18s; }
.ms-toggle i::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / .25); transition: transform .18s; }
.ms-toggle input:checked + i { background: var(--accent); }
.ms-toggle input:checked + i::after { transform: translateX(16px); }
.ms-toggle input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }

.ms-theme {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; padding: 6px 8px; margin: -4px -8px;
  border: 0; border-radius: 10px; background: transparent; font-size: .9rem; color: var(--fg);
}
.ms-theme:hover { background: var(--rk-surface); }
.ms-theme > span:nth-child(2) { flex: 1; }
.ms-theme .ic { color: var(--rk-muted); }
.ms-theme.on > span:nth-child(2) { font-weight: 600; }
.ms-swatch { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; font-family: var(--rk-serif); font-weight: 600; font-size: .85rem; box-shadow: inset 0 0 0 1px rgb(127 127 127 / .35); }
.ms-link {
  align-self: flex-start; display: inline-flex; align-items: center; gap: 6px; border: 0; background: transparent;
  color: var(--rk-accent-fg); font-weight: 600; font-size: .88rem; padding: 6px 8px; margin: -6px -8px; border-radius: 8px;
}
.ms-link:hover { background: var(--rk-accent-soft); }
.ms-foot { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; padding-top: 14px; border-top: 1px solid var(--rk-line); }
.ms-keys { margin: 0; font-size: .76rem; line-height: 1.5; color: var(--rk-muted); }

/* ============ Popup de seleção + mini dicionário ============ */
.sel-actions { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.sel-pop { background: var(--rk-elev); border-color: var(--rk-line); box-shadow: var(--rk-shadow); }

.sel-pop.dict-pop {
  flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: 0; padding: 0;
  max-width: none; border-radius: 16px; overflow: hidden;
}
.dict-pop .dict-card { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 10px 16px 10px; scrollbar-width: thin; }
.dict-pop .sel-actions { flex: none; padding: 6px 8px; border-top: 1px solid var(--rk-line); background: color-mix(in oklab, var(--fg) 2%, var(--rk-elev)); }

.dict-tabs { display: flex; gap: 4px; margin: 0 0 8px -2px; }
.dict-tabs button {
  border: 0; background: transparent; color: var(--rk-muted); font-size: .74rem; font-weight: 600; height: 26px; padding: 0 10px; border-radius: 999px;
}
.dict-tabs button:hover { color: var(--fg); background: var(--rk-surface); }
.dict-tabs button.on { background: var(--rk-accent-soft); color: var(--rk-accent-fg); }

.dict-head { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; }
.dict-word { font-family: var(--rk-serif); font-weight: 700; font-size: 1.35rem; line-height: 1.2; letter-spacing: -.01em; overflow-wrap: anywhere; }
.dict-pos {
  display: inline-block; font-size: .66rem; font-weight: 600; letter-spacing: .02em; padding: 2px 8px; border-radius: 999px;
  background: var(--rk-surface-2); color: var(--rk-muted); white-space: nowrap; text-transform: lowercase;
}
.dict-pos.sub { margin: 10px 0 2px; }
.dict-lemma { font-size: .84rem; color: var(--rk-muted); margin-top: 2px; }
.dict-lemma strong { color: var(--fg); font-family: var(--rk-serif); }
.dict-group ol { margin: 8px 0 0; padding-left: 1.35em; }
.dict-group li { font-size: .9rem; line-height: 1.48; margin: 0 0 7px; padding-left: 2px; }
.dict-group li::marker { color: var(--rk-muted); font-size: .82rem; font-variant-numeric: tabular-nums; }
.dict-ex { display: block; margin-top: 2px; font-style: italic; color: var(--rk-muted); font-size: .84rem; font-family: var(--rk-serif); }
.dict-more {
  border: 0; background: transparent; padding: 2px 0; margin: 0 0 2px; color: var(--rk-accent-fg); font-weight: 600; font-size: .84rem;
}
.dict-more:hover { text-decoration: underline; }
.dict-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 8px; padding-top: 8px;
  border-top: 1px solid var(--rk-line); font-size: .74rem; color: var(--rk-muted);
}
.dict-link {
  display: inline-flex; align-items: center; gap: 4px; border: 0; background: transparent; padding: 2px 0;
  color: var(--rk-accent-fg); font-weight: 600; font-size: .8rem;
}
.dict-link:hover span { text-decoration: underline; }
.dict-msg { padding: 6px 0 4px; }
.dict-msg p { margin: 6px 0 10px; font-size: .9rem; color: color-mix(in oklab, var(--fg) 80%, var(--bg)); }
.dict-retry {
  height: 32px; padding: 0 14px; border-radius: 9px; border: 1px solid var(--rk-line-strong); background: transparent; font-size: .84rem; font-weight: 600;
}
.dict-retry:hover { background: var(--rk-surface); }

.dict-skel { padding: 6px 0 4px; }
.dict-skel i {
  display: block; height: 10px; border-radius: 5px; margin: 10px 0;
  background: linear-gradient(90deg, var(--rk-surface-2) 0%, var(--rk-surface) 40%, var(--rk-surface-2) 80%) 0 0 / 300% 100%;
  animation: rk-shimmer 1.2s linear infinite;
}
.dict-skel .w90 { width: 90%; } .dict-skel .w85 { width: 85%; } .dict-skel .w75 { width: 75%; } .dict-skel .w50 { width: 50%; }
@keyframes rk-shimmer { from { background-position: 100% 0; } to { background-position: -50% 0; } }
@media (prefers-reduced-motion: reduce) { .dict-skel i { animation: none; } }

.wiki-top { display: flex; gap: 12px; align-items: flex-start; }
.wiki-main { flex: 1; min-width: 0; }
.wiki-img { flex: none; width: 76px; height: 76px; object-fit: cover; border-radius: 10px; background: var(--rk-surface-2); margin-top: 4px; }
.wiki-text {
  margin: 6px 0 4px; font-size: .9rem; line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden;
}
.wiki-text.open { display: block; -webkit-line-clamp: unset; }

/* Celular: dicionário vira sheet de ponta a ponta, sem cobrir a palavra */
.sel-pop.dict-pop.as-sheet {
  left: calc(8px + env(safe-area-inset-left)); right: calc(8px + env(safe-area-inset-right)); width: auto;
  border-radius: 18px;
}
.sel-pop.dict-pop.at-bottom { bottom: calc(8px + env(safe-area-inset-bottom)); top: auto; transform: translateY(12px); }
.sel-pop.dict-pop.at-top { top: calc(8px + env(safe-area-inset-top)); bottom: auto; transform: translateY(-12px); }
.sel-pop.dict-pop.as-sheet.show { transform: none; }
.dict-pop.as-sheet .sel-actions { justify-content: space-between; padding: 6px 10px; }
.dict-pop.as-sheet .pop-color { width: 30px; height: 30px; }
.dict-pop.as-sheet .icon-btn.sm { width: 38px; height: 38px; }
.dict-pop.as-sheet .dict-card { padding: 12px 16px 10px; }

/* ============ Responsivo da barra ============ */
@media (max-width: 600px) {
  .reader .rk-top { grid-template-columns: auto minmax(0, 1fr) auto; gap: 2px; }
  .rk-actions .rk-wide { display: none; }
  .reader .rk-actions .icon-btn { width: 38px; }
  .rk-titles .r-book { font-size: .84rem; }
  .reader .rk-bottom { gap: 2px; }
}
@media (max-width: 360px) {
  .reader .rk-actions .icon-btn { width: 34px; }
}
