/* Đọc Dài — kiểu «trang sách biên tập»: nền giấy ấm, chữ đọc serif, câu đang đọc tô vàng hổ phách. */

:root {
  color-scheme: light;
  --giay: #f5efe3;
  --giay-dam: #ebe2cf;
  --be-mat: #fbf8f1;
  --muc: #221d16;
  --muc-phu: #6b6152;
  --muc-da-doc: #7d7262;
  --vien: #d9cdb7;
  --nhan: #a5481c;
  --nhan-dam: #8a3a14;
  --nhan-chu: #fff8ef;
  --sang: #f6d886;
  --sang-chu: #2a1f0c;
  --gach: #a5481c;
  --nen-bao: #f4dfca;
  --bong-thanh: 0 -14px 34px -18px rgb(70 45 15 / 0.3);
  --bong-nut: 0 10px 22px -10px rgb(165 72 28 / 0.6);
  --bong-o: inset 0 1px 2px rgb(60 40 10 / 0.06);

  --chu-doc: 'Literata', 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  --chu-ui: 'Be Vietnam Pro', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --co-doc: clamp(1.08rem, 1rem + 0.45vw, 1.28rem);
  --co-tieu-de: clamp(2.3rem, 1.3rem + 4vw, 3.9rem);

  --le: 1rem;
  --cot-doc: 68ch;
  --cot-ui: 46rem;
  --cao-thanh-phat: 10rem;
  --bo-goc: 14px;

  --nhanh: 150ms;
  --vua: 300ms;
  --em: cubic-bezier(0.16, 1, 0.3, 1);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --giay: #15120e;
    --giay-dam: #262017;
    --be-mat: #1c1813;
    --muc: #ece4d4;
    --muc-phu: #a89e8c;
    --muc-da-doc: #8a8170;
    --vien: #3a3229;
    --nhan: #e07a3f;
    --nhan-dam: #eb8d55;
    --nhan-chu: #1a0f06;
    --sang: #5d4613;
    --sang-chu: #fff2d2;
    --gach: #f0a465;
    --nen-bao: #33241a;
    --bong-thanh: 0 -14px 34px -18px rgb(0 0 0 / 0.7);
    --bong-nut: 0 10px 24px -10px rgb(224 122 63 / 0.45);
    --bong-o: inset 0 1px 2px rgb(0 0 0 / 0.3);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --giay: #15120e;
  --giay-dam: #262017;
  --be-mat: #1c1813;
  --muc: #ece4d4;
  --muc-phu: #a89e8c;
  --muc-da-doc: #8a8170;
  --vien: #3a3229;
  --nhan: #e07a3f;
  --nhan-dam: #eb8d55;
  --nhan-chu: #1a0f06;
  --sang: #5d4613;
  --sang-chu: #fff2d2;
  --gach: #f0a465;
  --nen-bao: #33241a;
  --bong-thanh: 0 -14px 34px -18px rgb(0 0 0 / 0.7);
  --bong-nut: 0 10px 24px -10px rgb(224 122 63 / 0.45);
  --bong-o: inset 0 1px 2px rgb(0 0 0 / 0.3);
}

/* ── Nền tảng ─────────────────────────────────────────────────────────────── */

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-padding-top: 1rem;
  scroll-padding-bottom: calc(var(--cao-thanh-phat) + 1rem);
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(120% 55% at 50% -12%, var(--be-mat), transparent 70%),
    var(--giay);
  color: var(--muc);
  font: 400 1rem/1.55 var(--chu-ui);
  overflow-x: hidden;
}

::selection { background: var(--sang); color: var(--sang-chu); }

button, select, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: 2px solid var(--nhan); outline-offset: 3px; }

svg { width: 22px; height: 22px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
svg .to { fill: currentColor; stroke: none; }
svg text { fill: currentColor; stroke: none; font: 700 7.2px var(--chu-ui); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ── Nút ──────────────────────────────────────────────────────────────────── */

.nut-chinh, .nut-phu, .nut-chu {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem;
  border-radius: 999px; font-weight: 600; white-space: nowrap;
  transition: transform var(--nhanh) var(--em), background-color var(--nhanh), border-color var(--nhanh), color var(--nhanh), opacity var(--nhanh);
}

.nut-chinh {
  min-height: 50px; padding: 0.75rem 1.5rem;
  background: var(--nhan); color: var(--nhan-chu); box-shadow: var(--bong-nut);
}
.nut-chinh svg { width: 18px; height: 18px; fill: currentColor; stroke: none; }
.nut-chinh:hover:not(:disabled) { background: var(--nhan-dam); transform: translateY(-1px); }
.nut-chinh:active:not(:disabled) { transform: translateY(0) scale(0.98); }
.nut-chinh:disabled { opacity: 0.4; box-shadow: none; }

.nut-phu {
  min-height: 50px; padding: 0.7rem 1.3rem;
  border: 1.5px solid var(--muc); font-variant-numeric: tabular-nums;
}
.nut-phu:hover { background: var(--muc); color: var(--giay); }
.nut-phu:active { transform: scale(0.98); }

.nut-chu {
  min-height: 44px; padding: 0.5rem 1rem; font-weight: 500; font-size: 0.93rem;
  background: var(--be-mat); border: 1px solid var(--vien);
}
.nut-chu svg { width: 18px; height: 18px; }
.nut-chu:hover { border-color: var(--muc-phu); }
.nut-chu:active { transform: scale(0.97); }

.nut-icon {
  display: inline-grid; place-items: center; width: 46px; height: 46px; border-radius: 50%;
  color: var(--muc);
  transition: background-color var(--nhanh), transform var(--nhanh) var(--em);
}
.nut-icon:hover { background: var(--giay-dam); }
.nut-icon:active { transform: scale(0.9); }
.nut-icon svg { width: 24px; height: 24px; }

/* ── Đầu trang ────────────────────────────────────────────────────────────── */

.dau-trang {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem;
  padding: 0.7rem max(var(--le), env(safe-area-inset-left));
  border-bottom: 1px solid var(--vien);
  background: color-mix(in srgb, var(--giay) 90%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 20;
}

.thuong-hieu { display: flex; align-items: center; gap: 0.6rem; margin-right: auto; }
.dau-an {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px;
  background: var(--muc); color: var(--sang);
}
.dau-an svg { width: 22px; height: 22px; fill: currentColor; stroke: none; }
.ten-app { font: italic 600 1.2rem/1 var(--chu-doc); letter-spacing: -0.01em; }

.o-chon select {
  appearance: none; -webkit-appearance: none;
  min-height: 44px; max-width: 16rem; padding: 0.45rem 2.1rem 0.45rem 0.95rem;
  border: 1px solid var(--vien); border-radius: 999px;
  background: var(--be-mat) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5l5-5' fill='none' stroke='%238a7f6d' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 0.9rem center / 11px 8px;
  font: 500 0.9rem var(--chu-ui); text-overflow: ellipsis; cursor: pointer;
  transition: border-color var(--nhanh);
}
.o-chon select:hover { border-color: var(--muc-phu); }
.o-chon select:disabled { opacity: 0.55; cursor: not-allowed; }
.o-toc-do select { font-variant-numeric: tabular-nums; }

#nut-giao-dien .bieu-toi { display: none; }
#nut-giao-dien.dang-toi .bieu-toi { display: block; }
#nut-giao-dien.dang-toi .bieu-trang { display: none; }

/* ── Thông báo ────────────────────────────────────────────────────────────── */

.thong-bao {
  display: flex; align-items: flex-start; gap: 0.5rem;
  max-width: var(--cot-ui);
  margin: 1rem max(var(--le), calc((100% - var(--cot-ui)) / 2)) 0;
  padding: 0.75rem 0.5rem 0.75rem 1.1rem;
  background: var(--nen-bao); border-left: 3px solid var(--nhan); border-radius: 10px;
  font-size: 0.93rem; line-height: 1.5;
}
.thong-bao p { margin: 0.2rem 0 0; flex: 1; }
.nut-tat { width: 40px; height: 40px; margin: -0.3rem 0; }
.nut-tat svg { width: 18px; height: 18px; }

/* ── Màn nhập bài ─────────────────────────────────────────────────────────── */

.man-nhap {
  max-width: var(--cot-ui); margin: 0 auto;
  padding: clamp(1.75rem, 1rem + 4vw, 4.5rem) var(--le) 3rem;
}

.kicker {
  display: flex; align-items: center; gap: 0.65rem; margin: 0 0 0.9rem;
  font: 600 0.76rem/1 var(--chu-ui); letter-spacing: 0.16em; text-transform: uppercase; color: var(--nhan);
}
.kicker::before { content: ''; width: 1.75rem; height: 2px; background: currentColor; }

.man-nhap h1 {
  margin: 0 0 0.7rem;
  font: 600 var(--co-tieu-de)/1.02 var(--chu-doc); letter-spacing: -0.03em; text-wrap: balance;
}
.man-nhap h1 em { font-style: italic; color: var(--nhan); }
.mo-ta { margin: 0 0 1.8rem; max-width: 34rem; color: var(--muc-phu); font-size: 1.06rem; text-wrap: pretty; }

.o-van-ban {
  display: block; width: 100%; min-height: 44vh; resize: vertical;
  padding: 1.25rem 1.4rem;
  font: 400 var(--co-doc)/1.7 var(--chu-doc);
  background: var(--be-mat); border: 1px solid var(--vien); border-radius: var(--bo-goc);
  box-shadow: var(--bong-o);
  transition: border-color var(--nhanh), box-shadow var(--nhanh), background-color var(--nhanh);
}
.o-van-ban::placeholder { color: var(--muc-phu); font-style: italic; }
.o-van-ban:focus-visible {
  outline: none; border-color: var(--nhan);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--nhan) 22%, transparent);
}
.man-nhap.dang-tha .o-van-ban {
  border-style: dashed; border-color: var(--nhan);
  background: color-mix(in srgb, var(--sang) 22%, var(--be-mat));
}

.hang-nhap { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; margin-top: 1rem; }
.thong-ke { flex: 1 1 10rem; color: var(--muc-phu); font-size: 0.92rem; font-variant-numeric: tabular-nums; }

/* ── Khung đọc ────────────────────────────────────────────────────────────── */

.man-doc {
  padding: clamp(1.75rem, 1rem + 3vw, 3.5rem) var(--le) calc(var(--cao-thanh-phat) + 3rem + env(safe-area-inset-bottom));
}

.khung-doc {
  max-width: var(--cot-doc); margin: 0 auto;
  font: 400 var(--co-doc)/1.85 var(--chu-doc);
  overflow-wrap: break-word;
}
.khung-doc p { margin: 0 0 1.15em; }
.khung-doc > p:first-child::first-letter {
  float: left; margin: 0.06em 0.12em 0 0;
  font-size: 3.5em; line-height: 0.85; font-weight: 600; color: var(--nhan);
}

.cau {
  cursor: pointer; border-radius: 0.3em;
  -webkit-box-decoration-break: clone; box-decoration-break: clone;
  transition: background-color var(--nhanh), color var(--nhanh), box-shadow var(--nhanh);
}
.cau:hover { background: color-mix(in srgb, var(--sang) 42%, transparent); }
.cau.da-doc { color: var(--muc-da-doc); }
.cau.dang-doc {
  background: var(--sang); color: var(--sang-chu);
  box-shadow: 0 0 0 0.14em var(--sang);
}

::highlight(tu-dang-doc) {
  text-decoration-line: underline;
  text-decoration-thickness: 2px;
  text-decoration-color: var(--gach);
  text-underline-offset: 0.24em;
}

/* ── Thanh phát ───────────────────────────────────────────────────────────── */

.thanh-phat {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  padding: 0.65rem var(--le) calc(0.7rem + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--be-mat) 93%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--vien);
  box-shadow: var(--bong-thanh);
}
.thanh-phat-trong { max-width: var(--cot-ui); margin: 0 auto; }

.hang-thoi-gian { display: flex; align-items: center; gap: 0.75rem; }
.gio {
  min-width: 3.6em; color: var(--muc-phu);
  font: 500 0.82rem var(--chu-ui); font-variant-numeric: tabular-nums;
}
.gio-tong { text-align: right; }

#thanh-thoi-gian {
  --phan-tram: 0%;
  --da-dung: 0%; /* phần tiếng VieNeu đã dựng sẵn */
  --mau-da-dung: color-mix(in srgb, var(--nhan) 32%, var(--vien));
  --nen-thanh: linear-gradient(to right,
    var(--nhan) var(--phan-tram),
    var(--mau-da-dung) var(--phan-tram), var(--mau-da-dung) var(--da-dung),
    var(--vien) var(--da-dung));
  flex: 1; min-width: 0; height: 30px; margin: 0;
  appearance: none; -webkit-appearance: none; background: transparent; cursor: pointer;
}
#thanh-thoi-gian::-webkit-slider-runnable-track {
  height: 6px; border-radius: 999px;
  background: var(--nen-thanh);
}
#thanh-thoi-gian::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 20px; height: 20px; margin-top: -7px; border-radius: 50%;
  background: var(--nhan); border: 3px solid var(--be-mat);
  box-shadow: 0 1px 5px rgb(0 0 0 / 0.3);
  transition: transform var(--nhanh) var(--em);
}
#thanh-thoi-gian:active::-webkit-slider-thumb { transform: scale(1.2); }
#thanh-thoi-gian::-moz-range-track { height: 6px; border-radius: 999px; background: var(--nen-thanh); }
#thanh-thoi-gian::-moz-range-progress { height: 6px; border-radius: 999px; background: var(--nhan); }
#thanh-thoi-gian::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--nhan); border: 3px solid var(--be-mat); box-shadow: 0 1px 5px rgb(0 0 0 / 0.3);
}

.xem-truoc {
  height: 1.5em; margin: 0.1rem 0 0.4rem;
  color: var(--muc-phu); font-size: 0.82rem; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.xem-truoc.dang-keo { color: var(--muc); font: italic 400 0.92rem/1.5 var(--chu-doc); }
.xem-truoc.dang-cho { color: var(--nhan); font-weight: 500; }

.hang-nut { display: flex; align-items: center; justify-content: center; gap: clamp(0.3rem, 2.5vw, 1.1rem); }

.nut-phat {
  position: relative;
  display: grid; place-items: center; width: 62px; height: 62px; border-radius: 50%;
  background: var(--nhan); color: var(--nhan-chu); box-shadow: var(--bong-nut);
  transition: transform var(--nhanh) var(--em), background-color var(--nhanh);
}
.nut-phat:hover:not(:disabled) { background: var(--nhan-dam); transform: scale(1.05); }
.nut-phat:active:not(:disabled) { transform: scale(0.93); }
.nut-phat:disabled { opacity: 0.4; box-shadow: none; }
.nut-phat svg { width: 28px; height: 28px; fill: currentColor; stroke: none; }
.nut-phat .bieu-dung, .nut-phat.dang-doc .bieu-phat { display: none; }
.nut-phat.dang-doc .bieu-dung { display: block; }
/* Đang chờ máy Mac dựng câu sắp đọc: vòng quay quanh nút phát. */
.nut-phat.dang-cho::after {
  content: ''; position: absolute; inset: -6px; border-radius: 50%;
  border: 2.5px solid transparent; border-top-color: var(--nhan); border-right-color: var(--nhan);
  animation: quay 0.9s linear infinite;
}
@keyframes quay { to { transform: rotate(360deg); } }

.ghi-chu { margin: 1rem 0 0; color: var(--muc-phu); font-size: 0.82rem; line-height: 1.5; max-width: 40rem; }

/* ── Chuyển động ──────────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: no-preference) {
  .man-nhap:not([hidden]), .man-doc:not([hidden]) { animation: hien-ra var(--vua) var(--em); }
  .thanh-phat:not([hidden]) { animation: truot-len var(--vua) var(--em); }
}
@keyframes hien-ra { from { opacity: 0; transform: translateY(10px); } }
@keyframes truot-len { from { transform: translateY(100%); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation-duration: 0ms !important; }
}

/* ── Màn rộng / màn hẹp ───────────────────────────────────────────────────── */

@media (min-width: 641px) {
  .dau-trang { position: sticky; top: 0; padding-inline: max(1.5rem, env(safe-area-inset-left)); }
  html { scroll-padding-top: 5rem; }
}

@media (max-width: 640px) {
  .o-giong { order: 5; flex: 1 1 100%; }
  .o-giong select { width: 100%; max-width: none; }
  .o-toc-do select { padding-left: 0.8rem; padding-right: 1.9rem; }
  .an-khi-hep { display: none; }
  .hang-nhap .nut-chinh, .hang-nhap .nut-phu { flex: 1 1 100%; }
  .thong-ke { order: -1; flex-basis: 100%; }
}

@media (max-width: 400px) {
  .ten-app { display: none; }
  .hang-nut .nut-icon { width: 42px; height: 42px; }
  .nut-phat { width: 56px; height: 56px; }
  .hang-thoi-gian { gap: 0.5rem; }
  .gio { min-width: 3.2em; font-size: 0.78rem; }
}
