/*
 * Khung chung theo giao diện template: header (navbar), thanh bên (chọn sách + danh sách bài),
 * vùng nội dung và chân trang. Nội dung bài học có CSS riêng, tự scope trong
 * .book-textbook / .book-workbook (textbook.css, workbook.css), nên không phụ thuộc biến ở đây.
 * Màu và kích thước là token trên :root, giống template/css/styles.css.
 */

:root {
  --color-primary: #3578e5;
  --color-primary-dark: #306cce;
  --color-primary-contrast-bg: #ebf2fc;

  --color-bg: #ffffff;
  --color-bg-alt: #f6f8fa;
  --color-text: #1c1e21;
  --color-text-muted: #606770;
  --color-border: #ebedf0;
  --color-hover: rgba(0, 0, 0, 0.05);

  --font-base: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif;

  --navbar-height: 60px;
  --sidebar-width: 300px;
  --radius: 8px;
  --transition: 150ms ease;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #1b1b1d;
    --color-bg-alt: #242526;
    --color-text: #e3e3e3;
    --color-text-muted: #a0a4ab;
    --color-border: #3a3b3d;
    --color-hover: rgba(255, 255, 255, 0.06);
    --color-primary-contrast-bg: #1f2c44;
  }
}

:root[data-theme="dark"] {
  --color-bg: #1b1b1d;
  --color-bg-alt: #242526;
  --color-text: #e3e3e3;
  --color-text-muted: #a0a4ab;
  --color-border: #3a3b3d;
  --color-hover: rgba(255, 255, 255, 0.06);
  --color-primary-contrast-bg: #1f2c44;
}

/* ---------- Base ---------- */
*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-base);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--color-primary); text-decoration: none; transition: color var(--transition); }
a:hover { color: var(--color-primary-dark); }

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
[hidden] { display: none !important; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 1000;
  padding: 8px 12px;
  background: var(--color-primary);
  color: #fff;
}
.skip-link:focus { left: 12px; top: 12px; }

/* ---------- Header (navbar) ---------- */
.navbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--navbar-height);
  z-index: 100;
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.navbar__inner {
  height: 100%;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.navbar__left,
.navbar__right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.navbar__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--color-text);
  min-width: 0;
}
.navbar__brand:hover { color: var(--color-text); }

.navbar__mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border-radius: var(--radius);
  background: var(--color-primary);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.navbar__logo {
  display: block;
  height: 36px;
  width: auto;
  flex: 0 0 auto;
  object-fit: contain;
}

.navbar__titles {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.navbar__title {
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.navbar__sub {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  white-space: nowrap;
}

.theme-toggle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
}
.theme-toggle:hover { background: var(--color-hover); }

/* Tiêu đề của drawer: chỉ hiện trên điện thoại (xem @media bên dưới) */
.hs-drawer-head { display: none; }

/* Nút hamburger: chỉ hiện trên điện thoại (xem @media bên dưới) */
.navbar__toggle {
  display: none;
  width: 36px;
  height: 36px;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
}
.navbar__toggle:hover { background: var(--color-hover); }

/* Lớp nền mờ khi mở drawer trên điện thoại (giống template) */
.backdrop {
  position: fixed;
  inset: var(--navbar-height) 0 0 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 40;
}

/* ---------- Language dropdown (giống template) ---------- */
.navbar__link {
  padding: 6px 10px;
  border-radius: var(--radius);
  color: var(--color-text);
  font-weight: 500;
  white-space: nowrap;
}
.navbar__link:hover { background: var(--color-hover); color: var(--color-primary); }

.dropdown { position: relative; }
.dropdown__trigger {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: 0;
  font: inherit;
  cursor: pointer;
}
.dropdown__trigger::after {
  content: "";
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
}
.dropdown__menu {
  display: none;
  position: absolute;
  right: 0;
  top: 100%;
  margin: 0;
  padding: 6px;
  min-width: 140px;
  list-style: none;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  z-index: 20;
}
/* Vùng đệm vô hình để con trỏ đi từ nút sang menu không làm menu đóng */
.dropdown__menu::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -12px;
  height: 12px;
}
.dropdown:hover .dropdown__menu,
.dropdown.is-open .dropdown__menu { display: block; }
/* Dùng <button> thay vì <a href="#"> vì app.js điều hướng theo hash (#/bai/...) */
.dropdown__link {
  display: block;
  width: 100%;
  padding: 6px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--color-text);
  font: inherit;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.dropdown__link:hover { background: var(--color-hover); }
.dropdown__link.is-active { color: var(--color-primary); font-weight: 600; }

/* ---------- Layout ---------- */
.layout {
  display: flex;
  padding-top: var(--navbar-height);
  min-height: 100vh;
}

/* ---------- Sidebar (menu) ---------- */
.sidebar {
  position: sticky;
  top: var(--navbar-height);
  align-self: flex-start;
  width: var(--sidebar-width);
  flex: 0 0 var(--sidebar-width);
  height: calc(100vh - var(--navbar-height));
  overflow-y: auto;
  padding: 24px 16px 40px 20px;
  border-right: 1px solid var(--color-border);
  background: var(--color-bg);
}

/* Chọn sách: hai nút dạng segmented control */
.hs-book-switch {
  list-style: none;
  margin: 0 0 16px;
  padding: 4px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  background: var(--color-bg-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}
.hs-book-switch a {
  display: block;
  text-align: center;
  padding: 7px 8px;
  border-radius: 6px;
  color: var(--color-text);
  font-size: 14px;
  font-weight: 600;
}
.hs-book-switch a:hover { background: var(--color-hover); color: var(--color-text); }
.hs-book-switch a.active { background: var(--color-primary); color: #fff; }

.hs-side-label {
  margin: 8px 10px 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
}

/* Danh sách bài */
.hs-lesson-nav { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.hs-lesson-nav a {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px 10px;
  border-radius: var(--radius);
  color: var(--color-text);
}
.hs-lesson-nav a:hover { background: var(--color-hover); color: var(--color-text); }
.hs-lesson-nav a.active,
.hs-lesson-nav a[aria-current="page"] {
  background: var(--color-primary-contrast-bg);
  color: var(--color-primary);
  font-weight: 600;
}
.hs-lesson-nav a.soon { opacity: 0.6; }
.hs-lesson-nav .hs-n {
  flex: 0 0 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: var(--color-bg-alt);
  color: var(--color-text-muted);
  font-size: 13px;
  font-weight: 700;
}
.hs-lesson-nav a.active .hs-n { background: var(--color-primary); color: #fff; }
.hs-names { display: grid; min-width: 0; }
.hs-zh {
  font-size: 14px;
  font-weight: 600;
  font-family: "Times New Roman", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
}
.hs-en {
  font-size: 12px;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hs-lesson-nav a.active .hs-en { color: var(--color-primary); }

/* Nút mở/đóng menu: chỉ hiện trên điện thoại (xem @media bên dưới) */
.hs-menu-toggle { display: none; }
.hs-menu-panel { display: block; }

/* ---------- Content ---------- */
.content {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.hs-app { flex: 1 1 auto; min-width: 0; outline: none; }
.hs-app .book { min-width: 0; }

/* ---------- Trang bài + mục lục bên phải (giống .doc / .toc của template) ---------- */
:root { --toc-width: 220px; }

.doc {
  flex: 1 1 auto;
  display: flex;
  align-items: flex-start;
  gap: 32px;
  padding: 24px 24px 0;
}

.doc__content {
  flex: 1 1 auto;
  min-width: 0;
}

/* Mục lục bên phải: sticky dưới navbar, ẩn khi màn hình hẹp (≤1280px) như template */
.toc {
  position: sticky;
  top: calc(var(--navbar-height) + 24px);
  align-self: flex-start;
  flex: 0 0 var(--toc-width);
  width: var(--toc-width);
  max-height: calc(100vh - var(--navbar-height) - 48px);
  overflow-y: auto;
  padding-top: 12px;
  font-size: 0.875rem;
}
.toc__title { margin: 0 0 8px; font-weight: 600; }
.toc__list {
  list-style: none;
  margin: 0;
  padding: 0 0 0 12px;
  border-left: 1px solid var(--color-border);
}
.toc__link {
  display: block;
  padding: 4px 0;
  color: var(--color-text-muted);
}
.toc__link--sub { padding-left: 12px; }
.toc__link:hover,
.toc__link.is-active { color: var(--color-primary); }

/* Mục lục trên điện thoại: ẩn mặc định, hiện khi màn hình ≤996px (xem @media bên dưới) */
.toc-mobile { display: none; }

/* Tiêu đề mục cuộn tới không bị navbar che */
.hs-app [id] { scroll-margin-top: calc(var(--navbar-height) + 16px); }

@media (max-width: 1280px) {
  .toc { display: none; }
}

@media (max-width: 996px) {
  .doc { padding: 16px; }

  .toc-mobile {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 12px;
    padding: 10px 14px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-weight: 600;
  }
  .toc-mobile::after {
    content: "";
    width: 6px;
    height: 6px;
    border-right: 1.5px solid currentColor;
    border-bottom: 1.5px solid currentColor;
    transform: rotate(45deg);
  }
  .toc-mobile[aria-expanded="true"]::after { transform: rotate(-135deg); }
  .toc-mobile__list {
    list-style: none;
    margin: 0 0 16px;
    padding: 8px 14px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
  }
  .toc-mobile__list a { display: block; padding: 4px 0; color: var(--color-text-muted); }
}

/* ---------- Bài trước / bài tiếp (giống .pagination của template) ---------- */
.pagination {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 2rem;
  padding-bottom: 8px;
}
.pagination__item {
  flex: 1 1 0;
  padding: 14px 16px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  color: var(--color-text);
  transition: border-color var(--transition), color var(--transition);
}
.pagination__item:hover { border-color: var(--color-primary); color: var(--color-primary); }
.pagination__item--next { text-align: right; margin-left: auto; }
.pagination__sub { display: block; font-size: 0.85rem; color: var(--color-text-muted); }
.pagination__label { display: block; font-weight: 600; color: var(--color-primary); }

@media (max-width: 576px) {
  .pagination { flex-direction: column; }
  /* Xếp dọc: cả hai ô rộng bằng nhau, không để ô "Bài tiếp" bị co và đẩy sang phải */
  .pagination__item,
  .pagination__item--next { width: 100%; margin-left: 0; text-align: left; }
}

/* ---------- Footer ---------- */
.site-footer {
  padding: 24px 32px;
  border-top: 1px solid var(--color-border);
  color: var(--color-text-muted);
  font-size: 0.875rem;
  text-align: center;
}
.site-footer p { margin: 0; }

/* Ẩn phiên âm (pinyin): cả hai sách. !important để thắng quy tắc display trong textbook.css / workbook.css */
body.hs-hide-py .hero-pinyin,
body.hs-hide-py .hero-py,
body.hs-hide-py .speaker-py,
body.hs-hide-py .bubble-py,
body.hs-hide-py .w-py,
body.hs-hide-py .tongue-py,
body.hs-hide-py .num-py,
body.hs-hide-py .warm-py,
body.hs-hide-py .ap-py,
body.hs-hide-py .py { display: none !important; }

/* Ẩn bản dịch tiếng Việt: giữ nguyên tiếng Trung. Các class .en / .vi là phần dịch. */
body.hs-hide-vi .en,
body.hs-hide-vi .en-text,
body.hs-hide-vi .vi,
body.hs-hide-vi .hero-en,
body.hs-hide-vi .scene-en,
body.hs-hide-vi .section-en,
body.hs-hide-vi .point-en,
body.hs-hide-vi .check-en,
body.hs-hide-vi .w-en,
body.hs-hide-vi .tongue-en,
body.hs-hide-vi .ap-vi { display: none !important; }

/* Nút phát âm thanh (data-track): đang phát thì viền nổi lên */
[data-track].is-playing { outline: 2px solid currentColor; outline-offset: 2px; }

/* ---------- Responsive ---------- */
@media (max-width: 996px) {
  /* Thanh bên thành ngăn trượt từ trái, giống template: mở bằng nút hamburger trên navbar */
  .navbar__toggle { display: inline-flex; }
  /* Drawer phủ toàn màn hình (đè lên navbar), cao bằng viewport; phần danh sách cuộn bên trong */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 200;
    width: min(var(--sidebar-width), 85vw);
    flex: 0 0 auto;
    height: 100%;
    height: 100dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0 16px 40px;
    border-right: 1px solid var(--color-border);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    transform: translateX(-100%);
    transition: transform 200ms ease;
  }
  .sidebar.is-open { transform: translateX(0); }

  /* Tiêu đề drawer: dính trên cùng khi cuộn */
  .hs-drawer-head {
    position: sticky;
    top: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 0 -16px 16px;
    padding: 12px 16px;
    min-height: 64px;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
  }
  .hs-drawer-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .hs-drawer-title {
    font-size: 1.05rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .hs-drawer-close {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: var(--radius);
    background: none;
    color: var(--color-text);
    cursor: pointer;
  }
  .hs-drawer-close:hover { background: var(--color-hover); }

  /* Trong drawer, danh sách sách và bài luôn hiện (không còn ô accordion) */
  .hs-menu-toggle { display: none; }
  .hs-menu-panel { display: block; }

  .hs-book-switch a { padding: 11px 8px; font-size: 15px; }
  .hs-side-label { margin-top: 0; }

  /* Mỗi bài là một hàng, có mũi tên bên phải như mục menu của template */
  .hs-lesson-nav a { padding: 10px 12px; }
  .hs-lesson-nav a::after {
    content: "";
    flex: 0 0 auto;
    margin-left: auto;
    width: 6px;
    height: 6px;
    border-right: 1.5px solid currentColor;
    border-bottom: 1.5px solid currentColor;
    transform: rotate(-45deg);
    opacity: 0.6;
  }
  .hs-names { flex: 1 1 auto; }

  /* Danh sách bài: mỗi bài một hàng, ô to để dễ bấm bằng ngón tay */
  .hs-lesson-nav { grid-template-columns: minmax(0, 1fr); gap: 6px; }
  .hs-lesson-nav a { min-width: 0; min-height: 52px; padding: 8px 10px; }
  .hs-lesson-nav .hs-zh { font-size: 15px; }

  .navbar__title { font-size: 0.95rem; }
}

@media (max-width: 576px) {
  .navbar__inner { padding: 0 12px; }
  .navbar__sub { display: none; }
  .site-footer { padding: 20px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
