/* ============================================================
   HOCKHA — Reader
   1 tờ full rộng, canh giữa, tối giản. Cuộn dọc trong tờ,
   lật trang chỉ qua nút/phím/slider.
   ============================================================ */

html {
  overflow: hidden;
  overscroll-behavior: none;
}
.reader-body {
  height: 100vh; overflow: hidden; display: flex; flex-direction: column;
  background: var(--bg);
  touch-action: pan-y;
  overscroll-behavior-x: none;
}

/* ---------- Top bar ---------- */
.reader-bar {
  height: 60px; flex-shrink: 0; display: flex; align-items: center; gap: 12px; padding: 0 16px;
  padding-left: max(16px, env(safe-area-inset-left, 16px));
  padding-right: max(16px, env(safe-area-inset-right, 16px));
  background: rgba(251,252,251,.92); backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--line); z-index: 30;
}
.rb-back {
  display: inline-flex; align-items: center; gap: 7px; font-weight: 600; font-size: .9rem; color: var(--ink-soft);
  padding: 8px 12px; border-radius: var(--r-pill); transition: all .16s var(--ease); flex-shrink: 0;
}
.rb-back:hover { background: var(--surface-2); color: var(--ink); }
.rb-back span { font-size: 1.05rem; }
.rb-title { flex: 1; font-weight: 700; font-size: 1rem; color: var(--ink); letter-spacing:-0.02em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.rb-actions { display: flex; gap: 8px; flex-shrink: 0; min-width: 96px; justify-content: flex-end; }
.rb-btn {
  display: inline-flex; align-items: center; gap: 7px; height: 38px; padding: 0 15px; border-radius: var(--r-pill);
  background: var(--surface-2); color: var(--ink-soft); font-weight: 600; font-size: .88rem; transition: all .16s var(--ease);
}
.rb-btn:hover { background: var(--brand-50); color: var(--brand-700); }
.rb-btn.active { background: var(--ink); color: #fff; }

/* ---------- Stage ---------- */
/* ---------- Stage ---------- */
.reader-stage { flex: 1; display: flex; min-height: 0; position: relative; overflow: hidden; width: 100%; }

.book-scene {
  flex: 1; position: relative; min-width: 0; overflow: hidden;
  display: flex; justify-content: center; width: 100%; max-width: 100%;
}

.book-loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--ink-mute); }
.spinner {
  width: 42px; height: 42px; margin: 0 auto 14px; border: 3px solid var(--brand-100);
  border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Spread (1 tờ duy nhất) ---------- */
.spread {
  width: 100%; max-width: 860px; height: 100%; min-height: 0; display: flex;
  align-items: stretch; justify-content: center; overflow: hidden; box-sizing: border-box;
}
.spread.anim .sheet { animation: pageIn .28s var(--ease); }
@keyframes pageIn { from { transform: translateY(6px); } to { transform: none; } }

.sheet {
  flex: 1; min-width: 0; max-width: 100%; height: 100%; min-height: 0; background: var(--surface); display: flex; flex-direction: column;
  box-shadow: 0 0 0 1px var(--line); box-sizing: border-box; width: 100%;
}
.sheet.right { display: none; }

.sheet-inner {
  flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden;
  padding: 48px clamp(28px, 6vw, 80px) 36px;
  scroll-behavior: smooth; box-sizing: border-box; width: 100%;
}
.sheet-inner::-webkit-scrollbar { width: 9px; }
.sheet-inner::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 99px; border: 3px solid var(--surface); }

.sheet-foot {
  flex-shrink: 0; height: 48px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 clamp(28px, 6vw, 80px); border-top: 1px solid var(--line);
  font-size: .76rem; color: var(--ink-mute); width: 100%; box-sizing: border-box;
}
.page-foot-brand { font-weight: 700; color: var(--ink-soft); }
.page-foot-num { font-weight: 500; }

/* ---------- Nội dung ---------- */
.page-chip {
  display: inline-block; margin-bottom: 22px; padding: 6px 14px;
  background: var(--brand-50); color: var(--brand-700); border-radius: var(--r-pill);
  font-weight: 700; font-size: .82rem; letter-spacing: -0.01em;
}
.page-md { color: var(--ink); font-size: 1.04rem; line-height: 1.8; letter-spacing: -0.005em; overflow-x: hidden; word-break: break-word; }
.page-md > *:first-child { margin-top: 0; }
.page-md h1, .page-md h2, .page-md h3 { margin: 26px 0 12px; letter-spacing: -0.02em; font-weight: 760; line-height: 1.35; }
.page-md h1 {
  font-size: 1.2rem; color: var(--ink);
  padding: 0; background: transparent;
  border: 0; border-radius: 0;
}
.major-title.lesson-title {
  margin: 0 0 18px; font-size: 1.38rem; color: #b42318;
  padding: 10px 14px; background: #fff1f0;
  border-left: 4px solid #d92d20; border-radius: 0 var(--r-md) var(--r-md) 0;
  font-weight: 760; line-height: 1.35; letter-spacing: -0.02em;
}
.page-md h2 {
  font-size: 1.16rem; color: var(--ink);
  padding: 0; background: transparent;
  border-radius: 0; margin-top: 26px;
}
.page-md h3 {
  font-size: 1.05rem; color: var(--ink);
  padding-bottom: 0; border-bottom: 0;
}
.page-md h4 { font-size: 1rem; font-weight: 700; color: var(--ink); margin: 18px 0 8px; }
.page-md p { margin: 11px 0; }
.page-md ul, .page-md ol { padding-left: 22px; margin: 11px 0; }
.page-md li { margin: 6px 0; }
.page-md li::marker { color: var(--brand); }
.page-md code { background: var(--surface-2); padding: 2px 7px; border-radius: 6px; font-size: .9em; font-family: ui-monospace, 'Cascadia Code', Consolas, monospace; }
.page-md pre { background: #11150f; color: #e8f0ea; padding: 15px 17px; border-radius: var(--r-md); overflow-x: auto; max-width: 100%; }
.page-md pre code { background: none; color: inherit; padding: 0; }
.page-md strong { font-weight: 700; color: var(--ink); }
.page-md blockquote {
  border-left: 3px solid var(--brand); margin: 14px 0; padding: 6px 16px; color: var(--ink-soft);
  background: var(--surface-2); border-radius: 0 10px 10px 0;
}
.page-md table { display: block; overflow-x: auto; width: 100%; max-width: 100%; border-collapse: collapse; margin: 14px 0; font-size: .94rem; }
.page-md th, .page-md td { border: 1px solid var(--line-strong); padding: 8px 12px; text-align: left; }
.page-md th { background: var(--brand-50); font-weight: 700; color: var(--brand-700); }
.page-md tr:nth-child(even) td { background: var(--surface-2); }
.page-md hr { border: none; border-top: 1px solid var(--line); margin: 20px 0; }
.page-md img { max-width: 100%; height: auto; border-radius: 10px; margin: 12px 0; }
.page-md mjx-container { outline: none; max-width: 100% !important; overflow-x: auto; overflow-y: hidden; }
.page-md mjx-container[display="true"] { margin: 12px 0 !important; }

/* ---------- Flip nav ---------- */
.flip-nav {
  position: fixed; top: 50%; transform: translateY(-50%); width: 46px; height: 46px; border-radius: 50%;
  background: var(--surface); color: var(--ink); font-size: 24px; line-height: 1; z-index: 15;
  box-shadow: var(--shadow-md); transition: all .18s var(--ease); display: grid; place-items: center;
  border: 1px solid var(--line);
}
.flip-nav:hover { background: var(--ink); color: #fff; border-color: var(--ink); }
.flip-nav:disabled { opacity: 0; pointer-events: none; }
.flip-nav.prev { left: 18px; }
.flip-nav.next { right: 18px; }

/* ---------- Bottom bar ---------- */
.reader-foot {
  height: 56px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; gap: 14px;
  padding: 0 22px;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: rgba(251,252,251,.92); backdrop-filter: blur(16px);
  border-top: 1px solid var(--line); z-index: 20;
}
.rf-btn {
  width: 38px; height: 38px; border-radius: 10px; font-size: 13px; color: var(--ink-soft);
  background: transparent; transition: all .15s var(--ease); display: grid; place-items: center; flex-shrink: 0;
}
.rf-btn:hover { background: var(--surface-2); color: var(--ink); }
.rf-btn:disabled { opacity: .3; pointer-events: none; }
.rf-progress { display: flex; align-items: center; gap: 14px; flex: 1; max-width: 440px; }
.rf-label { font-size: .82rem; font-weight: 500; color: var(--ink-soft); white-space: nowrap; min-width: 120px; text-align: center; }

#pageSlider {
  flex: 1; height: 6px; -webkit-appearance: none; appearance: none; background: var(--line-strong);
  border-radius: 99px; outline: none; cursor: pointer;
}
#pageSlider::-webkit-slider-thumb {
  -webkit-appearance: none; width: 22px; height: 22px; border-radius: 50%; background: var(--brand);
  cursor: pointer; border: 2px solid #fff; box-shadow: var(--shadow-sm);
}
#pageSlider::-moz-range-thumb {
  width: 22px; height: 22px; border-radius: 50%; background: var(--brand); border: 2px solid #fff; cursor: pointer;
}

/* ---------- TOC drawer ---------- */
.toc-drawer {
  position: absolute; left: 0; top: 0; bottom: 0; width: 320px; background: var(--surface);
  border-right: 1px solid var(--line); z-index: 25; transform: translateX(-100%);
  transition: transform .28s var(--ease); display: flex; flex-direction: column; box-shadow: var(--shadow-lg);
}
.toc-drawer.open { transform: translateX(0); }
.toc-head {
  height: 56px; flex-shrink: 0; display: flex; align-items: center; justify-content: space-between; padding: 0 20px;
  border-bottom: 1px solid var(--line); font-weight: 700; letter-spacing:-0.02em;
}
.toc-close { width: 32px; height: 32px; border-radius: 50%; font-size: 15px; color: var(--ink-mute); transition: background .16s; }
.toc-close:hover { background: var(--surface-2); color: var(--ink); }
.toc-list { flex: 1; overflow-y: auto; padding: 8px; }
.toc-item {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-radius: var(--r-sm);
  cursor: pointer; transition: background .14s; font-weight: 500; color: var(--ink-soft); font-size: .9rem;
}
.toc-item:hover { background: var(--surface-2); }
.toc-item.active { background: var(--brand-50); color: var(--brand-700); font-weight: 700; }
.toc-item .ti-num {
  width: 28px; height: 28px; flex-shrink: 0; border-radius: 8px; display: grid; place-items: center;
  background: var(--surface-2); color: var(--ink-mute); font-weight: 700; font-size: .76rem;
}
.toc-item.active .ti-num { background: var(--brand); color: #fff; }
.toc-backdrop {
  position: absolute; inset: 0; background: rgba(17,21,15,.28); z-index: 24; opacity: 0; pointer-events: none;
  transition: opacity .28s; backdrop-filter: blur(2px);
}
.toc-backdrop.show { opacity: 1; pointer-events: auto; }

/* ---------- Fail state ---------- */
.reader-fail { text-align: center; color: var(--ink-mute); padding: 40px; }
.reader-fail .big { font-size: 1.4rem; margin-bottom: 10px; font-weight: 800; color: var(--ink); }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .sheet-inner { padding: 28px 16px 24px; }
  .sheet-foot { padding: 0 16px; }
  .rb-btn-label { display: inline; font-size: .82rem; }
  .rb-actions { min-width: auto; }
  .rb-title { font-size: .82rem; max-width: 40vw; }
  .rb-back span + span { display: none; } /* Ẩn text "Quay lại", giữ icon */
  .flip-nav { width: 38px; height: 38px; font-size: 18px; opacity: 0.7; }
  .flip-nav.prev { left: 4px; } .flip-nav.next { right: 4px; }
  .toc-drawer { width: 88%; }
  .rf-label { min-width: 80px; font-size: .76rem; }
  /* Tăng touch target cho slider */
  #pageSlider { height: 8px; }
  /* Safe area cho bottom bar */
  .reader-foot {
    height: auto;
    padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px));
  }
  .reader-bar { height: 54px; }
  .rf-btn { width: 42px; height: 42px; }
}

/* ---------- Icons ---------- */
.hk-ico { width:1em; height:1em; display:block; flex-shrink:0; }

/* ---------- Callouts (Ghi nhớ / Mẹo / Lưu ý) ---------- */
.page-md .hk-callout {
  border-left: 3px solid var(--brand);
  margin: 16px 0;
  padding: 14px 16px 12px;
  color: var(--ink);
  background: linear-gradient(180deg, rgba(21,163,90,.06), var(--surface-2));
  border-radius: 0 14px 14px 0;
  box-shadow: 0 1px 0 rgba(17,21,15,.03);
}
.page-md .hk-callout--tip { border-left-color: #e0a100; background: linear-gradient(180deg, rgba(224,161,0,.08), var(--surface-2)); }
.page-md .hk-callout--warn { border-left-color: #d97706; background: linear-gradient(180deg, rgba(217,119,6,.08), var(--surface-2)); }
.hk-callout-head {
  display:flex; align-items:center; gap:8px; margin:0 0 8px;
  font-weight:800; font-size:.86rem; letter-spacing:-0.01em; color:var(--brand-700);
}
.hk-callout--tip .hk-callout-head { color:#a16207; }
.hk-callout--warn .hk-callout-head { color:#b45309; }
.hk-callout-ico {
  width:28px; height:28px; border-radius:9px; display:grid; place-items:center;
  background: rgba(21,163,90,.12); color: var(--brand-700); font-size:15px;
}
.hk-callout--tip .hk-callout-ico { background: rgba(224,161,0,.14); color:#a16207; }
.hk-callout--warn .hk-callout-ico { background: rgba(217,119,6,.14); color:#b45309; }
.page-md .hk-callout p { margin: 6px 0; color: var(--ink-soft); }
.page-md .hk-callout ul, .page-md .hk-callout ol { margin: 6px 0; }
.page-md blockquote:not(.hk-callout) {
  border-left: 3px solid var(--brand); margin: 14px 0; padding: 6px 16px; color: var(--ink-soft);
  background: var(--surface-2); border-radius: 0 10px 10px 0;
}

/* ---------- AI Helper (premium) ---------- */
.ai-helper-wrap {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.ai-helper-wrap.is-open { border-top-color: transparent; padding-top: 8px; }
.ai-helper-btn {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px 10px 12px;
  border-radius: 999px;
  border: 1px solid rgba(21,163,90,.22);
  background: linear-gradient(180deg, #fff, #f4fbf6);
  color: var(--brand-700);
  font-size: .86rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(17,21,15,.04), 0 8px 20px rgba(21,163,90,.08);
  transition: transform .16s var(--ease), box-shadow .16s var(--ease), border-color .16s, background .16s;
}
.ai-helper-btn:hover {
  border-color: rgba(21,163,90,.4);
  background: linear-gradient(180deg, #fff, var(--brand-50));
  box-shadow: 0 2px 8px rgba(21,163,90,.14);
  transform: translateY(-1px);
}
.ai-helper-btn:active { transform: translateY(0); }
.ai-helper-btn-ico {
  width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--brand); color: #fff; font-size: 14px;
  box-shadow: 0 4px 10px rgba(21,163,90,.28);
}
.ai-helper-btn[hidden] { display: none !important; }

.ai-panel {
  margin-top: 4px;
  border: 1px solid rgba(21,163,90,.18);
  border-radius: 18px;
  background: var(--surface);
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(17,21,15,.06), 0 1px 0 rgba(255,255,255,.7) inset;
  animation: aiPanelIn .22s var(--ease);
}
@keyframes aiPanelIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
.ai-panel-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 12px 12px 14px;
  background: linear-gradient(135deg, #12a056 0%, #0f8a4a 55%, #0c7a42 100%);
  color: #fff;
}
.ai-panel-title { display:flex; align-items:center; gap:10px; min-width:0; }
.ai-panel-ico {
  width: 32px; height: 32px; border-radius: 10px;
  display:grid; place-items:center;
  background: rgba(255,255,255,.16);
  font-size: 16px;
  flex-shrink: 0;
}
.ai-panel-title-text { display:flex; flex-direction:column; gap:1px; min-width:0; }
.ai-panel-title-text strong {
  font-size: .88rem; font-weight: 800; letter-spacing: -0.02em; line-height: 1.15;
}
.ai-panel-title-text span {
  font-size: .72rem; font-weight: 500; opacity: .88;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 52vw;
}
.ai-panel-close {
  width: 32px; height: 32px; border-radius: 10px; border: none;
  background: rgba(255,255,255,.12); color: #fff;
  display:grid; place-items:center; cursor:pointer; flex-shrink:0;
  transition: background .15s;
}
.ai-panel-close:hover { background: rgba(255,255,255,.22); }
.ai-panel-close .hk-ico { width: 16px; height: 16px; }

.ai-panel-msgs {
  padding: 14px;
  max-height: 240px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(180deg, #f7faf7, #fff 40%);
}
.ai-msg {
  padding: 10px 13px;
  border-radius: 14px;
  font-size: .86rem;
  line-height: 1.55;
  max-width: 92%;
  box-shadow: 0 1px 2px rgba(17,21,15,.04);
}
.ai-msg.bot {
  background: #fff;
  border: 1px solid var(--line);
  align-self: flex-start;
  color: var(--ink);
  border-bottom-left-radius: 6px;
}
.ai-msg.user {
  background: linear-gradient(135deg, var(--brand), #0f8a4a);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 6px;
}
.ai-msg.typing { font-style: italic; color: var(--ink-mute); box-shadow: none; border-style: dashed; }

.ai-panel-input {
  display: flex; gap: 8px; align-items: center;
  padding: 12px 12px 10px;
  border-top: 1px solid var(--line);
  background: #fff;
}
.ai-input {
  flex: 1;
  padding: 11px 14px;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  font-size: .86rem;
  outline: none;
  background: var(--surface-2);
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.ai-input:focus {
  border-color: rgba(21,163,90,.55);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(21,163,90,.12);
}
.ai-send {
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(135deg, var(--brand), #0f8a4a);
  color: #fff; border: none; cursor: pointer;
  display: grid; place-items: center;
  box-shadow: 0 6px 14px rgba(21,163,90,.28);
  transition: transform .15s, filter .15s;
  flex-shrink: 0;
}
.ai-send:hover { filter: brightness(1.05); transform: scale(1.04); }
.ai-send .hk-ico { width: 16px; height: 16px; margin-left: 1px; }
.ai-panel-note {
  padding: 0 14px 12px;
  font-size: .7rem;
  color: var(--ink-mute);
  text-align: center;
  background: #fff;
}

/* reader-mobile-polish-v41 */
@media (max-width: 720px) {
  .ai-helper-btn { width: 100%; justify-content: center; min-height: 46px; }
  .ai-panel-msgs { max-height: min(40vh, 260px); }
  .ai-input { font-size: 16px !important; }
  .ai-helper-btn-ico, .ai-panel-ico { flex-shrink: 0; }
  .ai-helper-wrap { scroll-margin-bottom: 40px; scroll-margin-top: 80px; }
  .ai-panel { margin-bottom: 20px; }
}