:root {
  --bg: #101116;
  --surface: #191b22;
  --surface-2: #242731;
  --line: #2f3340;
  --text: #eef0f4;
  --muted: #9aa1b1;
  --accent: #ff6b4a;
  --accent-hover: #ff8366;
  --accent-ink: #1a0d08;
  --ok: #4cc38a;
  --warn: #f5b544;
  --danger: #ff6464;
  --radius: 10px;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f4ef;
    --surface: #ffffff;
    --surface-2: #eeebe3;
    --line: #dcd8cd;
    --text: #1c1d22;
    --muted: #62677a;
    --accent: #cf3f22;
    --accent-hover: #b53419;
    --accent-ink: #ffffff;
    --ok: #15803d;
    --warn: #a16207;
    --danger: #c02626;
    --shadow: 0 8px 24px rgba(40, 30, 10, 0.12);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.6;
}
a { color: inherit; text-decoration: none; }
h1, h2, h3 { line-height: 1.3; margin: 0; font-weight: 600; }
h1 { font-size: 26px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
p { margin: 0; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ปุ่มและช่องกรอก */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 6px 14px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 3px 10px; font-size: 13px; }

input[type="text"], input[type="password"], input[type="number"], input[type="search"], input[type="url"], input[type="email"], select, textarea {
  min-height: 38px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit; width: 100%;
}
textarea { resize: vertical; display: block; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; flex: none; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 13px; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }

/* แถบบน */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand .logo { width: 28px; height: 28px; border-radius: 7px; }
.nav { display: flex; gap: 2px; flex: 1; min-width: 0; }
.nav a { padding: 6px 12px; border-radius: 8px; color: var(--muted); white-space: nowrap; }
.nav a:hover { color: var(--text); }
.nav a.on { color: var(--text); background: var(--surface-2); }
.who { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; white-space: nowrap; }

.page { max-width: 1240px; margin: 0 auto; padding: 24px 20px 64px; }
.section { margin-top: 32px; }
.section:first-child { margin-top: 0; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.muted { color: var(--muted); }
.small-text { font-size: 13px; }
.center { text-align: center; }

/* ตัวกรอง */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.filters .grow { flex: 1 1 240px; }
.seg { display: inline-flex; max-width: 100%; overflow-x: auto; background: var(--surface-2); border-radius: 8px; padding: 3px; }
.seg button { border: 0; background: none; color: var(--muted); font: inherit; padding: 5px 14px; border-radius: 6px; cursor: pointer; min-height: 32px; white-space: nowrap; }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 18px; }
.chip { border: 1px solid var(--line); background: none; color: var(--muted); font: inherit; font-size: 13px; padding: 3px 12px; border-radius: 999px; cursor: pointer; min-height: 30px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* การ์ด */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 18px 14px; }
.card { display: block; position: relative; min-width: 0; }
.cover { position: relative; aspect-ratio: 2 / 3; border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.cover img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.2s ease; }
.card:hover .cover img { transform: scale(1.03); }
.cover .none { position: absolute; inset: 0; display: grid; place-items: center; padding: 10px; color: var(--muted); text-align: center; font-size: 13px; }
.badge { position: absolute; top: 6px; left: 6px; padding: 0 7px; border-radius: 5px; font-size: 11px; font-weight: 600; background: rgba(10, 10, 14, 0.78); color: #fff; }
.badge.adult { left: auto; right: 6px; background: var(--danger); color: #fff; }
.badge.new { top: auto; bottom: 10px; left: 6px; background: var(--accent); color: var(--accent-ink); font-size: 12px; padding: 1px 8px; }
.tag.new { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.warn-text { color: var(--warn); }
.alts { margin-top: 2px; font-size: 13px; }
.alts summary { cursor: pointer; color: var(--muted); }
.alts a { display: block; padding: 3px 0; text-decoration: underline; }
.rank-list { display: flex; flex-direction: column; margin-top: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.rank-list .empty { border: 0; }
.rank-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-top: 1px solid var(--line); }
.rank-row:first-child { border-top: 0; }
.rank-no { width: 30px; flex: none; text-align: center; font-size: 18px; font-weight: 700; color: var(--muted); }
.rank-row .thumb { flex: none; }
.rank-body { flex: 1; min-width: 0; }
.rank-name { font-weight: 500; }
.bar { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(0, 0, 0, 0.5); }
.bar i { display: block; height: 100%; background: var(--accent); }
.card-title { margin-top: 7px; font-weight: 500; line-height: 1.35; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; }
.card-th { color: var(--muted); font-size: 13px; line-height: 1.35; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.card > a { display: block; }
.title-th { font-size: 18px; font-weight: 500; margin-top: 2px; }
.mine-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.mine-row select { width: auto; }
.section-tight { margin-top: 10px; }
.audit { margin: 6px 0 0; padding-left: 18px; }
.audit-ok { color: var(--ok); }
.audit-warn { color: var(--warn); }
.audit-fail { color: var(--danger); }
.audit-skip { color: var(--muted); }
.home-actions { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-bottom: 16px; }
.home-actions.spread { justify-content: space-between; flex-wrap: wrap; }
.btn.adult-entry { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.tag-inline { margin-left: 8px; display: inline-block; }
.day-head { padding: 6px 14px; font-size: 13px; color: var(--muted); background: var(--surface-2); }
.day-head:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.day-head + .rank-row { border-top: 0; }

.shelf { display: grid; grid-auto-flow: column; grid-auto-columns: 140px; gap: 14px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: thin; }

.pick { position: absolute; top: 6px; right: 6px; z-index: 2; background: rgba(10, 10, 14, 0.75); border-radius: 6px; padding: 6px; display: flex; }
.card.selected .cover { outline: 3px solid var(--accent); outline-offset: -3px; }
.card.owned .cover img { opacity: 0.45; }

.action-bar { position: sticky; bottom: 12px; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 18px; padding: 10px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }

.empty { padding: 40px 16px; text-align: center; color: var(--muted); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty .btn { margin-top: 12px; }

/* หน้าเรื่อง */
.title-head { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 24px; }
.title-head .cover { max-width: 220px; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.tag { padding: 0 9px; border-radius: 6px; background: var(--surface-2); font-size: 12.5px; color: var(--muted); }
.tag.adult { background: var(--danger); color: #fff; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.panel + .panel { margin-top: 14px; }
.panel h2 { margin-bottom: 10px; }
.progress-now { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.progress-now strong { font-size: 20px; font-weight: 700; }
.progress-now > div { min-width: 0; }
.progress-now .btn { white-space: normal; text-align: center; max-width: 100%; }
.inline { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.inline .field { flex: 1 1 110px; }
.site-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); }
.site-row:first-of-type { border-top: 0; }
.chapters { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.chapters a { padding: 6px 10px; border-radius: 8px; background: var(--surface-2); text-align: center; font-size: 14px; }
.chapters a.read { color: var(--muted); }
.desc { white-space: pre-wrap; color: var(--muted); }

/* admin */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 13px; font-weight: 500; color: var(--muted); padding: 6px 10px; }
.table td { padding: 8px 10px; border-top: 1px solid var(--line); vertical-align: middle; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.table input[type="text"] { min-width: 200px; }
.table select { min-width: 110px; width: auto; }
.thumb { width: 40px; height: 58px; object-fit: cover; border-radius: 4px; background: var(--surface-2); }
.cell-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; align-items: end; }

/* เข้าสู่ระบบ */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth form { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 26px; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; }

.steps { margin: 0; padding-left: 22px; display: flex; flex-direction: column; gap: 8px; }
code { font-family: Consolas, monospace; background: var(--surface-2); padding: 1px 6px; border-radius: 5px; }

/* ตัวอ่าน */
body.reading { background: #0b0b0e; color: #eef0f4; }
.reader-top { position: fixed; inset: 0 0 auto 0; z-index: 30; display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: rgba(16, 17, 22, 0.92); color: #eef0f4; transition: transform 0.2s ease; }
.reader-top.away { transform: translateY(-110%); }
.reader-top .name { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.strip { max-width: 820px; margin: 0 auto; padding-top: 52px; }
.strip img { width: 100%; min-height: 40vh; background: #16171d; }
.strip img.done { min-height: 0; }
.paged { height: 100vh; display: flex; align-items: center; justify-content: center; cursor: pointer; user-select: none; }
.paged img { max-width: 100%; max-height: 100vh; }
.reader-end { display: flex; justify-content: center; gap: 10px; padding: 26px 14px 60px; }
.reader-count { position: fixed; right: 12px; bottom: 12px; z-index: 30; padding: 2px 10px; border-radius: 999px; background: rgba(16, 17, 22, 0.85); color: #eef0f4; font-size: 13px; }

#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; width: max-content; max-width: calc(100vw - 32px); }
.toast { padding: 8px 16px; border-radius: 8px; background: var(--text); color: var(--bg); box-shadow: var(--shadow); }
.toast.bad { background: var(--danger); color: #fff; }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 6px 12px; padding: 8px 16px; }
  .nav { order: 3; flex-basis: 100%; overflow-x: auto; scrollbar-width: none; }
  .nav::-webkit-scrollbar { display: none; }
  .nav a { padding: 6px 8px; }
  .who { margin-left: auto; }
  .page { padding: 16px 16px 56px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 14px 10px; }
  .title-head { grid-template-columns: 110px minmax(0, 1fr); gap: 14px; }
  h1 { font-size: 21px; }
  /* ตารางบนมือถือแสดงเป็นการ์ดเรียงลงมา */
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { padding: 12px 14px; border-top: 1px solid var(--line); }
  .table tr:first-child { border-top: 0; }
  .table td { border: 0; padding: 4px 0; }
  .table input[type="text"], .table select { min-width: 0; width: 100%; }
  .cell-row select { flex: 1 1 140px; width: auto; }
}

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