:root {
  --bg: #0f1216;
  --bg2: #171c23;
  --bg3: #1f2731;
  --panel: #12161c;
  --text: #e8edf2;
  --muted: #93a1b0;
  --accent: #2dd4a7;
  --accent-2: #23a57f;
  --line: #2a333e;
  --active: rgba(45, 212, 167, 0.16);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  font-size: 15px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* ---------- Header ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 6px; white-space: nowrap; }
.logo { color: var(--accent); font-size: 17px; }
.brand-name { font-weight: 700; font-size: 17px; }
.brand-sub { color: var(--muted); font-size: 11px; }

.url-form { display: flex; flex: 1; gap: 7px; }
.url-form input {
  flex: 1;
  padding: 8px 11px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg3);
  color: var(--text);
  outline: none;
  font-size: 13px;
  min-width: 0;
}
.url-form input:focus { border-color: var(--accent); }
.url-form button {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #06281f;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.url-form button:hover { background: var(--accent-2); }
.url-form button:disabled { opacity: .5; cursor: default; }

.btn-lib {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg3);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  flex-shrink: 0;
}
.btn-lib:hover { background: var(--line); border-color: var(--accent); }

.status {
  padding: 8px 14px;
  background: #1a2533;
  color: #bfe3ff;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

/* ---------- Layout ---------- */
.layout {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 50px);  /* topbar ~50px */
  overflow: hidden;
}

/* ---------- Player ---------- */
.player {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 16px;
  background: linear-gradient(180deg, var(--bg2), var(--bg));
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.player-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.now-playing { flex: 1; min-width: 0; text-align: center; }
.now-title { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.now-sub { margin-top: 2px; }

/* Nút mở drawer chương */
.btn-chapters {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg3);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
}
.btn-chapters:hover { background: var(--line); border-color: var(--accent); }

.controls { display: flex; justify-content: center; align-items: center; gap: 14px; }
.ctl {
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ctl:hover { background: var(--bg3); }
.ctl-main {
  background: var(--accent);
  color: #06281f;
  font-size: 20px;
  width: 56px;
  height: 56px;
}
.ctl-main:hover { background: var(--accent-2); }

.options {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  font-size: 13px;
  color: var(--muted);
}
.options label { display: flex; align-items: center; gap: 6px; }
.options select {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 5px 7px;
  max-width: 180px;
}
.chk input { accent-color: var(--accent); }

.mode-toggle { display: flex; gap: 6px; }
.mode-btn {
  border: 1px solid var(--line);
  background: var(--bg3);
  color: var(--muted);
  padding: 5px 11px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 12.5px;
}
.mode-btn.active {
  background: var(--accent);
  color: #06281f;
  border-color: var(--accent);
  font-weight: 600;
}

/* ---------- Reader ---------- */
.reader {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.chapter-title {
  padding: 14px 20px 4px;
  font-weight: 700;
  font-size: 17px;
  flex-shrink: 0;
}

.chapter-text {
  flex: 1;
  overflow-y: auto;
  padding: 8px 22px 60px;
  line-height: 1.85;
  font-size: 16px;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}
.chapter-text p { margin: 0 0 1em 0; text-indent: 2em; }
.chapter-text .chunk { display: inline; border-radius: 4px; transition: background .2s; }
.chapter-text .chunk.active {
  background: var(--active);
  outline: 1px solid rgba(45, 212, 167, .4);
}

/* ---------- Chapter Drawer ---------- */
.chapter-drawer {
  position: fixed;
  inset: 0;
  z-index: 200;
}

.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.55);
}

.drawer-panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: min(380px, 92vw);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 24px rgba(0,0,0,0.5);
  animation: slideIn .2s ease;
}
@keyframes slideIn {
  from { transform: translateX(100%); }
  to   { transform: translateX(0); }
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.story-mini { display: flex; align-items: center; gap: 10px; min-width: 0; }
.cover-mini {
  width: 42px;
  height: 58px;
  object-fit: cover;
  border-radius: 5px;
  background: var(--bg3);
  flex-shrink: 0;
}
.story-title-mini {
  font-weight: 700;
  font-size: 14px;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.btn-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
  line-height: 1;
  padding: 4px 6px;
}
.btn-close:hover { color: var(--text); }

.drawer-search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.drawer-search-bar .muted { white-space: nowrap; }
#chapter-search {
  flex: 1;
  padding: 6px 9px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--bg3);
  color: var(--text);
  outline: none;
  font-size: 13px;
}
#chapter-search:focus { border-color: var(--accent); }

.chapter-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  flex: 1;
}
.chapter-list li {
  padding: 9px 10px;
  border-radius: 7px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  border: 1px solid transparent;
}
.chapter-list li:hover { background: var(--bg2); }
.chapter-list li.playing {
  background: var(--active);
  border-color: var(--accent);
  font-weight: 600;
}
.chapter-list li .idx {
  color: var(--muted);
  font-size: 12px;
  min-width: 36px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.chapter-list li .eq {
  display: none;
  width: 14px;
  height: 14px;
}
.chapter-list li.playing .eq { display: inline-block; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.chapter-list li .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.badge-saved { font-size: 12px; color: var(--accent); flex-shrink: 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #2a1f1f;
  color: #ffd7d7;
  border: 1px solid #5a3232;
  padding: 10px 16px;
  border-radius: 8px;
  max-width: 80vw;
  z-index: 500;
  font-size: 13.5px;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.modal-box {
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: 100%;
  max-width: 650px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 36px rgba(0,0,0,0.5);
}
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.modal-head h2 { margin: 0; font-size: 17px; }

.library-items {
  padding: 14px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.lib-item {
  display: flex;
  gap: 14px;
  padding: 12px;
  border-radius: 8px;
  background: var(--bg3);
  border: 1px solid var(--line);
  align-items: center;
}
.lib-cover {
  width: 50px;
  height: 70px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--bg);
  flex-shrink: 0;
}
.lib-info { flex: 1; min-width: 0; }
.lib-title { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.lib-meta { color: var(--muted); font-size: 12.5px; }
.lib-btn {
  padding: 8px 14px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #06281f;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}
.lib-btn:hover { background: var(--accent-2); }

/* ---------- Mobile ---------- */
@media (max-width: 600px) {
  .brand-sub { display: none; }
  .url-form input { font-size: 12px; }
  .options { gap: 10px; }
  .now-title { font-size: 14px; }
}
