/* ── Reset & base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #111;
  --surface:  #1e1e1e;
  --border:   #333;
  --text:     #e8e8e8;
  --muted:    #888;
  --accent:   #4a9eff;
  --danger:   #e05;
  --radius:   8px;
  --header-h: 52px;
}

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  min-height: 100dvh;
}

button { cursor: pointer; background: none; border: none; color: inherit; font: inherit; }
.hidden { display: none !important; }

/* ── Header ────────────────────────────────────────────────────────────────── */
#header {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
}

#header-title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-icon {
  font-size: 20px;
  padding: 6px;
  border-radius: var(--radius);
  line-height: 1;
}
.btn-icon:hover { background: var(--border); }

/* ── Main ──────────────────────────────────────────────────────────────────── */
#main { min-height: calc(100dvh - var(--header-h)); }
.view { padding: 20px 16px; }

/* View chapters: hero cố định, chapter list scroll */
#view-chapters {
  padding: 0;
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--header-h));
  overflow: hidden;
}

/* ── Manga detail hero ─────────────────────────────────────────────────────── */
#manga-hero {
  display: flex;
  gap: 20px;
  padding: 16px 16px 12px;
  align-items: flex-start;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
}

.manga-hero-cover {
  width: 140px;
  min-width: 140px;
  aspect-ratio: 2/3;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--border);
}

.manga-hero-info {
  flex: 1;
  min-width: 0;
}

.manga-hero-title {
  font-size: 20px;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 6px;
}

.manga-hero-author {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}

.manga-hero-synopsis {
  font-size: 13px;
  color: #bbb;
  line-height: 1.6;
  white-space: pre-wrap;
}

/* Chapter panel: scroll container */
#chapter-panel {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.chapter-section-header {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 10px 16px 8px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 1;
}

/* ── Chapter list (vertical) ───────────────────────────────────────────────── */
#chapter-list.chapter-list {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0 16px;
}

.chapter-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .12s;
  border-radius: 0;
  background: none;
  border-left: none;
  border-right: none;
  border-top: none;
}
.chapter-row:hover { background: #1a1a1a; }

.chapter-row-name {
  font-size: 14px;
  font-weight: 500;
}

.chapter-row-meta {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  margin-left: 12px;
}

/* ── Card grid (manga & chapter lists) ─────────────────────────────────────── */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 14px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 12px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  user-select: none;
}
.card:hover, .card:active { border-color: var(--accent); background: #252525; }

.card-cover {
  width: 100%;
  aspect-ratio: 2/3;
  object-fit: cover;
  border-radius: calc(var(--radius) - 2px);
  display: block;
  margin-bottom: 10px;
  background: var(--border);
}

.card-cover-placeholder {
  width: 100%;
  aspect-ratio: 2/3;
  background: var(--border);
  border-radius: calc(var(--radius) - 2px);
  margin-bottom: 10px;
}

.card-title {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  word-break: break-word;
}
.card-meta {
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
}

/* ── Fullscreen mode (header hidden) ───────────────────────────────────────── */
#header {
  transition: transform .3s ease;
}

body.reader-fullscreen #header {
  transform: translateY(-100%);
}

body.reader-fullscreen #page-container {
  height: 100dvh;
}

/* ── Reader ────────────────────────────────────────────────────────────────── */
#view-reader { padding: 0; }

#page-container {
  background: #000;
  height: calc(100dvh - var(--header-h));
  transition: height .3s ease;
  overflow: hidden;
  position: relative;
}

.page-wrapper {
  display: none;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  position: relative;
  line-height: 0;
  touch-action: pan-y; /* browser xử lý scroll dọc, JS detect swipe ngang */
}

.page-wrapper.active { display: block; }

.page-img {
  width: 100%;
  height: auto;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

#page-counter {
  position: fixed;
  bottom: 16px;
  right: 16px;
  background: rgba(0,0,0,.65);
  color: #fff;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 12px;
  z-index: 50;
  pointer-events: none;
  letter-spacing: .03em;
}

.page-img.loading { opacity: .3; }

/* tap ripple */
.tap-ripple {
  position: absolute;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(74, 158, 255, .4);
  transform: translate(-50%, -50%) scale(0);
  pointer-events: none;
  animation: ripple .5s ease-out forwards;
}
@keyframes ripple {
  to { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

/* ── Translation overlay ───────────────────────────────────────────────────── */
#translation-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  transition: background .25s;
}

#translation-overlay.overlay-minimized {
  background: transparent;
  pointer-events: none;
}
#translation-overlay.overlay-minimized #translation-card {
  pointer-events: auto;
}

/* Lớp 1 (song ngữ): bảng nhỏ, nền trong suốt, chạm xuyên xuống trang để tra bóng khác. */
#translation-overlay.overlay-peek {
  background: transparent;
  pointer-events: none;
}
#translation-overlay.overlay-peek #translation-card {
  pointer-events: auto;
  height: auto;
  max-height: 34dvh;
  box-shadow: 0 -4px 18px rgba(0,0,0,.25);
}
#translation-overlay.overlay-peek #translation-card-header { height: 32px; }
#translation-overlay.overlay-peek .tl-card-title,
#translation-overlay.overlay-peek #btn-minimize-overlay { visibility: hidden; }
#translation-overlay.overlay-peek #translation-body { overflow-y: auto; }
#translation-card.peek .bl-learn { display: none; }
#translation-overlay.peek-top { align-items: flex-start; }
#translation-overlay.peek-top #translation-card {
  border-radius: 0 0 18px 18px;
  box-shadow: 0 4px 18px rgba(0,0,0,.25);
}
#translation-overlay.peek-top #translation-card.slide-up { animation: slide-in-top .22s ease-out forwards; }
#translation-overlay.peek-top.closing #translation-card { animation: slide-out-top .2s ease-in forwards; }
@keyframes slide-in-top  { from { transform: translateY(-100%); } to { transform: translateY(0); } }
@keyframes slide-out-top { from { transform: translateY(0); } to { transform: translateY(-100%); } }

#translation-overlay.closing #translation-card {
  animation: slide-down .22s ease-in forwards;
}

#translation-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  width: 100%;
  max-width: 680px;
  height: 90dvh;
  display: flex;
  flex-direction: column;
  transition: height .3s cubic-bezier(.22,.8,.36,1);
  overflow: hidden;
}

#translation-card.minimized {
  height: 48px;
}

/* Peek có height:auto và header 32px; khi thu nhỏ vẫn phải thành đúng dải 48px. */
#translation-overlay.overlay-peek.overlay-minimized #translation-card { height: 48px; }
#translation-overlay.overlay-peek.overlay-minimized #translation-card-header { height: 48px; }

#translation-card.slide-up {
  animation: slide-up .28s cubic-bezier(.22,.8,.36,1) forwards;
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

@keyframes slide-down {
  from { transform: translateY(0); }
  to   { transform: translateY(100%); }
}

/* ── Card header ────────────────────────────────────────────────────────────── */
#translation-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: 48px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  cursor: default;
}

#translation-card.minimized #translation-card-header {
  cursor: pointer;
  border-bottom: none;
}

.tl-card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: .04em;
  text-transform: uppercase;
}

#btn-minimize-overlay,
#btn-close-overlay {
  font-size: 18px;
  color: var(--muted);
  padding: 4px 10px;
  border-radius: var(--radius);
  line-height: 1;
}
#btn-minimize-overlay:hover,
#btn-close-overlay:hover { background: var(--border); color: var(--text); }

/* ── Card body (scrollable) ─────────────────────────────────────────────────── */
#translation-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px 0;
  display: flex;
  flex-direction: column;
}

#translation-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  padding: 32px 0;
  font-size: 14px;
}

/* ── Translation content sections ──────────────────────────────────────────── */

.tl-bubble {
  font-size: 13px;
  color: var(--muted);
  font-style: italic;
  line-height: 1.5;
  padding: 10px 14px;
  background: rgba(255,255,255,.04);
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  margin-bottom: 16px;
}

.tl-vi {
  font-size: 22px;
  font-weight: 600;
  line-height: 1.45;
  color: var(--text);
  margin-bottom: 8px;
}

.tl-ipa {
  font-size: 13px;
  color: #7eb8ff;
  font-family: monospace;
  margin-bottom: 4px;
  letter-spacing: .03em;
}

.tl-divider {
  height: 1px;
  background: var(--border);
  margin: 14px 0;
}

.tl-note {
  font-size: 14px;
  color: #bbb;
  line-height: 1.7;
}
.tl-note strong { color: var(--text); }

.tl-body {
  font-size: 14px;
  line-height: 1.7;
  color: #ddd;
}

.tl-body p {
  margin: 0 0 2px;
}

.tl-gap {
  height: 8px;
}

/* Headings */
.tl-h {
  font-weight: 700;
  color: var(--text);
  margin: 14px 0 4px;
}
h3.tl-h { font-size: 14px; color: var(--accent); margin-top: 10px; }
h4.tl-h, h5.tl-h { font-size: 13px; color: var(--muted); margin-top: 8px; }
h2.tl-h { font-size: 15px; }

/* Divider */
.tl-hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

/* Bullet list */
.tl-list {
  margin: 2px 0 2px 18px;
  padding: 0;
}
.tl-list li {
  margin: 3px 0;
  line-height: 1.6;
}

/* Table */
.tl-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
  margin: 8px 0;
}
.tl-table th,
.tl-table td {
  border: 1px solid var(--border);
  padding: 5px 10px;
  text-align: left;
  vertical-align: top;
}
.tl-table th {
  background: rgba(255,255,255,.06);
  color: var(--text);
  font-weight: 600;
}
.tl-table td { color: #bbb; }

/* Inline code */
.tl-code {
  font-family: monospace;
  font-size: 12px;
  background: rgba(255,255,255,.08);
  padding: 1px 5px;
  border-radius: 3px;
  color: #a8d8ff;
}

/* ── Chat follow-up entries ────────────────────────────────────────────────── */
.chat-entry {
  margin: 14px 0;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.55;
}

.chat-entry-user {
  background: var(--accent);
  color: #fff;
  margin-left: 40px;
  border-bottom-right-radius: 2px;
  align-self: flex-end;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-entry-claude {
  background: rgba(255,255,255,.05);
  margin-right: 40px;
  border-left: 3px solid var(--accent);
  border-bottom-left-radius: 2px;
}

.chat-entry-claude p:first-child { margin-top: 0; }
.chat-entry-claude p:last-child  { margin-bottom: 0; }

.chat-loading {
  color: var(--muted);
  font-style: italic;
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  margin-right: 40px;
  padding: 6px 14px;
}

.chat-entry-error {
  background: rgba(255,100,100,.1);
  color: #f88;
  border-left: 3px solid #f66;
}

.tl-error {
  color: #f66;
  font-size: 14px;
  padding: 12px 0;
}

#translation-content { padding-bottom: 8px; }

#btn-tts {
  margin: 12px 0;
  padding: 8px 18px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
  align-self: flex-start;
}
#btn-tts:hover { opacity: .85; }

/* ── Chat input ─────────────────────────────────────────────────────────────── */
#translation-chat {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px 0 16px;
  margin-top: auto;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  bottom: 0;
  background: var(--surface);
}

#chat-input {
  flex: 1;
  background: rgba(255,255,255,.06);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text);
  font-size: 14px;
  padding: 8px 14px;
  outline: none;
  transition: border-color .15s;
}
#chat-input::placeholder { color: var(--muted); }
#chat-input:focus { border-color: var(--accent); }

#btn-chat-send {
  width: 34px;
  height: 34px;
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#btn-chat-send:hover { opacity: .85; }

/* ── Permission overlay ────────────────────────────────────────────────────── */
#permission-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 110;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

#permission-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 480px;
  padding: 20px 18px;
}

#permission-text {
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 16px;
  white-space: pre-wrap;
}

#permission-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.permission-btn {
  padding: 9px 18px;
  border-radius: var(--radius);
  background: var(--border);
  font-size: 14px;
  font-weight: 500;
  transition: background .15s;
}
.permission-btn:hover { background: var(--accent); color: #fff; }

/* ── Toast ─────────────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: var(--text);
  font-size: 14px;
  padding: 10px 20px;
  border-radius: 20px;
  z-index: 200;
  white-space: nowrap;
  animation: fade-in .2s ease;
}
@keyframes fade-in { from { opacity: 0; transform: translateX(-50%) translateY(8px); } }

/* ── Loading state ─────────────────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 20px; height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 60px 0;
  font-size: 15px;
}

/* ── Popup song ngữ (dữ liệu dựng sẵn từ pipeline) ───────────────────────────── */
.tl-bilingual .bl-who  { color: var(--muted); font-size: .85em; margin: 0 0 .4em; }
.tl-bilingual .bl-en   { font-weight: 600; margin: 0 0 .5em; }
.tl-bilingual .bl-vi   { font-size: 1.1em; margin: 0 0 .7em; }
.tl-bilingual .bl-note { color: var(--muted); font-size: .92em; margin: 0 0 .5em; }
.tl-bilingual .bl-hoc  { margin: 0 0 .6em; padding-left: 1.2em; font-size: .9em; }
.tl-bilingual .bl-head { font-size: .8em; font-weight: 600; color: var(--muted); text-transform: uppercase; margin: .6em 0 .2em; }
.tl-bilingual .bl-warn { color: #b26a00; font-size: .9em; margin: 0 0 .6em; }
.tl-bilingual .bl-goc  { color: var(--muted); font-size: .9em; }
.tl-bilingual .bl-more {
  display: block; margin: .2em 0 .6em; padding: .45em .9em;
  border: 1px solid var(--border); border-radius: 999px;
  background: transparent; color: inherit; font-size: .88em; cursor: pointer;
}

/* ── Chế độ e-ink (body.eink) ──────────────────────────────────────────────────
   Màn hình e-ink làm mới chậm: mảng tối lớn gây nhấp nháy và bóng mờ, hiệu ứng chuyển động thành
   mấy lần làm mới giật cục. Nên: nền trắng chữ đen, viền đen rõ thay cho đổ bóng, tắt mọi hiệu ứng. */
body.eink {
  --bg: #fff; --surface: #fff; --border: #000; --text: #000; --muted: #333; --accent: #000;
}
body.eink *, body.eink *::before, body.eink *::after {
  animation: none !important;
  transition: none !important;
  box-shadow: none !important;
}
body.eink #translation-overlay { background: transparent; }
body.eink #translation-card { border: 2px solid #000; }
body.eink #translation-overlay:not(.overlay-peek) #translation-card { height: 100dvh; max-width: none; border-radius: 0; }
body.eink #translation-overlay.overlay-minimized #translation-card { height: 48px; }
body.eink .tl-bilingual .bl-vi   { font-size: 1.2em; }
body.eink .tl-bilingual .bl-warn { color: #000; font-weight: 600; }
body.eink .tl-bilingual .bl-more { border: 2px solid #000; font-weight: 600; }
body.eink #btn-tts  { background: #fff; color: #000; border: 2px solid #000; }
body.eink #page-counter { background: #fff; color: #000; border: 1px solid #000; }
body.eink #toast   { background: #fff; color: #000; border: 2px solid #000; }
body.eink .btn-icon:hover { background: transparent; }
/* Vài khối chữ trong bảng có màu sáng gán cứng cho nền tối → trên e-ink ép đen hết. */
body.eink #translation-card,
body.eink #translation-card * { color: #000 !important; }
body.eink #translation-card .bl-who,
body.eink #translation-card .bl-goc { color: #333 !important; }

/* Loa nhỏ: nghe cả câu (cạnh câu gốc) và nghe từng từ (đầu mỗi mục từ). Vùng chạm ≥ 32px cho ngón tay. */
.tl-bilingual .bl-say {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 32px; min-height: 32px; margin: -6px 2px -6px -4px; padding: 0;
  font-size: 1em; line-height: 1; vertical-align: middle; border-radius: 50%;
}
.tl-bilingual .bl-say:active { background: var(--border); }
.tl-bilingual .bl-hoc li { margin: .15em 0; }

/* ── Máy tính (chuột) ──────────────────────────────────────────────────────── */
/* Nút lật trang chỉ hiện khi dùng chuột; điện thoại vẫn vuốt như cũ. */
.page-nav { display: none; }

@media (hover: hover) and (pointer: fine) {
  /* Cả trang vừa chiều cao màn hình, không phải cuộn; căn giữa */
  .page-wrapper { overflow: hidden; }
  /* Thanh tiêu đề ẩn (trượt lên) vẫn chiếm chỗ → kéo khung trang lên lấp, để trang không mất khúc đáy */
  body.reader-fullscreen #page-container { margin-top: calc(-1 * var(--header-h)); }
  .page-img {
    width: auto;
    max-width: 100%;
    height: 100%;
    margin: 0 auto;
    object-fit: contain;
    cursor: pointer;
  }

  .page-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    width: 56px;
    height: 96px;
    border: none;
    border-radius: 12px;
    background: rgba(255,255,255,.08);
    color: rgba(255,255,255,.7);
    font-size: 48px;
    line-height: 1;
    cursor: pointer;
    z-index: 40;
    transition: background .15s, color .15s;
  }
  .page-nav:hover { background: rgba(255,255,255,.2); color: #fff; }
  .page-nav-prev { left: 16px; }
  .page-nav-next { right: 16px; }
  #view-reader.hidden .page-nav { display: none; }
  body.eink .page-nav { background: #fff; color: #000; border: 1px solid #000; }
}
