:root {
  --bg: #111214;
  --panel: #1a1c20;
  --panel-2: #24272d;
  --line: #2c3038;
  --text: #f0f1f4;
  --muted: #9aa0a8;
  --accent: #ff8a3d;
  --danger: #d7263d;
  --ok: #1f7a45;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Noto Sans Thai", "Sarabun", sans-serif;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); font-size: 13px; }

/* ---------------- top header */
header#top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-top)) 12px 8px;
  background: rgba(17, 18, 20, .92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
header h1 {
  font-size: 18px; font-weight: 700; margin: 0; flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon {
  width: 44px; height: 44px; font-size: 26px; line-height: 1;
  border-radius: 10px; flex: none;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon:active { background: var(--panel); }
#admin-btn, #logout { min-height: 44px; padding: 6px 12px; font-size: 13px; text-decoration: none; color: var(--text, #f0f1f4); }
.admin-badge {
  background: var(--accent); color: #111; font-size: 11px; font-weight: 700;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 99px;
  display: inline-flex; align-items: center; justify-content: center; margin-left: 6px;
}

main {
  padding: 14px 14px calc(32px + var(--safe-bottom));
  max-width: 1100px; margin: 0 auto;
}
h2 { font-size: 15px; margin: 20px 2px 10px; color: var(--muted); font-weight: 600; letter-spacing: .2px; }
h2:first-child { margin-top: 2px; }

/* ---------------- login */
.login-wrap {
  min-height: calc(100dvh - 48px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px 4px;
}
.login {
  width: 100%; max-width: 360px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 24px 20px; display: flex; flex-direction: column; gap: 14px;
}
.login-brand { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-bottom: 4px; }
.login-brand img { width: 52px; height: 52px; border-radius: 14px; }
.login-brand h2 { margin: 0; color: var(--text); font-size: 21px; font-weight: 700; }
.login label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 13px; }
.login input {
  width: 100%; font: inherit; font-size: 16px; color: var(--text);
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 12px; min-height: 48px;
}
.login input:focus { border-color: var(--accent); outline: none; }
.pw-wrap { position: relative; display: flex; align-items: center; }
.pw-wrap input { padding-right: 64px; }
.pw-toggle {
  position: absolute; right: 4px; min-width: 52px; min-height: 40px;
  padding: 0 8px; border-radius: 8px; font-size: 12.5px; color: var(--muted);
}
.pw-toggle:active { background: var(--panel-2); }
.err-inline { color: #ff6b6b; min-height: 1.2em; font-size: 13.5px; }
.field-hint { font-size: 12px; color: var(--muted); }
.auth-switch {
  min-height: 44px; width: 100%; display: flex; align-items: center; justify-content: center; gap: 4px;
  color: var(--muted); font-size: 13.5px; border-radius: 10px;
}
.auth-switch b { color: var(--accent); font-weight: 600; }
.auth-switch:active { background: var(--panel-2); }
.ok-box {
  background: rgba(31, 122, 69, .18); border: 1px solid rgba(31, 122, 69, .6);
  color: #b8f2cc; padding: 14px; border-radius: 12px;
  font-size: 14px; line-height: 1.5; text-align: center;
}

/* ---------------- home & cards */
.cats { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.cat {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 16px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; text-decoration: none; color: inherit; min-height: 68px;
}
.cat:active { background: var(--panel-2); }
.cat b { display: block; font-size: 17px; margin-bottom: 2px; }
.cat-arrow { font-size: 22px; color: var(--muted); }

.recent {
  display: flex; gap: 10px; overflow-x: auto; padding-bottom: 6px;
  scroll-snap-type: x mandatory; scrollbar-width: none;
}
.recent::-webkit-scrollbar { display: none; }
.recent .card { flex: 0 0 118px; scroll-snap-align: start; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 14px 10px; }
.card { text-decoration: none; color: inherit; display: block; min-width: 0; }
.card:active .cover { opacity: .82; }
.cover {
  aspect-ratio: 2 / 3; border-radius: 10px; overflow: hidden;
  background: var(--panel); border: 1px solid var(--line); position: relative;
}
.cover::after {
  content: ""; position: absolute; inset: auto 0 0 0; height: 42%;
  background: linear-gradient(transparent, rgba(0, 0, 0, .65));
  pointer-events: none;
}
.cover img {
  width: 100%; height: 100%; object-fit: cover; display: block;
  opacity: 0; transition: opacity .22s;
}
.cover img.ok { opacity: 1; }
.cover img.bad { display: none; }
.card .name {
  margin-top: 6px; font-size: 13.5px; line-height: 1.3; font-weight: 500;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card .sub {
  margin-top: 2px; font-size: 12px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bar-progress {
  position: absolute; left: 0; right: 0; bottom: 0; height: 4px; z-index: 2;
  background: rgba(0, 0, 0, .55);
}
.bar-progress i { display: block; height: 100%; background: var(--accent); }
.badge {
  position: absolute; top: 6px; right: 6px; z-index: 2;
  background: rgba(0, 0, 0, .74); backdrop-filter: blur(4px);
  font-size: 11px; padding: 2px 7px; border-radius: 99px; font-weight: 600;
}

/* ---------------- category search & filters */
.search-box { position: relative; margin-bottom: 10px; }
.search-box input {
  width: 100%; min-height: 44px; padding: 10px 42px 10px 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 11px;
  color: var(--text); font: inherit; font-size: 15px;
}
.search-box input:focus { border-color: var(--accent); outline: none; }
.search-clear {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 36px; height: 36px; border-radius: 8px; color: var(--muted); font-size: 18px;
}
.filters { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.filters .seg button { min-height: 40px; padding: 6px 4px; font-size: 13px; }
.count-row { display: flex; align-items: center; justify-content: space-between; margin: 4px 2px 12px; }
.link-btn { color: var(--accent); font-size: 13px; padding: 4px 6px; }

/* ---------------- tags */
.tags { position: absolute; left: 5px; bottom: 7px; z-index: 2; display: flex; flex-wrap: wrap; gap: 3px; }
.tags-inline { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { font-size: 10.5px; font-weight: 700; padding: 2px 6px; border-radius: 6px; color: #fff; }
.tags-inline .tag { font-size: 12px; padding: 3px 8px; }
.tag.adult { background: var(--danger); }
.tag.done { background: var(--ok); }
.tag.ongoing { background: rgba(38, 42, 50, .94); color: #ffcf8a; border: 1px solid rgba(255, 207, 138, .25); }

/* ---------------- series page */
.series-head {
  display: flex; gap: 14px; align-items: flex-start;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px; margin-bottom: 12px;
}
.series-cover { width: 84px; flex: none; }
.series-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.series-meta h2 { margin: 0; color: var(--text); font-size: 17px; line-height: 1.3; }
.tagedit { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.tagedit .seg { flex: 1; }
.tagedit .seg button { min-height: 40px; padding: 6px 4px; font-size: 13px; }
.tagedit .adulttoggle { min-height: 44px; padding: 8px 14px; font-weight: 700; border: 1px solid var(--line); }
.tagedit .adulttoggle.on { background: var(--danger); border-color: var(--danger); color: #fff; }

.continue {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 48px; margin: 0 0 12px; padding: 12px 16px;
  border-radius: 12px; background: var(--accent); color: #111;
  font-weight: 700; text-decoration: none; text-align: center;
}
.continue:active { filter: brightness(.92); }

.list { display: flex; flex-direction: column; gap: 4px; }
.row {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 10px; border-radius: 12px; background: var(--panel);
  border: 1px solid transparent; text-decoration: none; color: inherit;
}
.row:active { background: var(--panel-2); border-color: var(--line); }
.row .thumb { width: 46px; flex: none; }
.row .txt { flex: 1; min-width: 0; }
.row .txt div:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.row .done { color: var(--accent); font-size: 12.5px; font-weight: 600; }

/* ---------------- states */
.empty, .err { padding: 42px 16px; text-align: center; color: var(--muted); }
.err { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.err-actions { display: flex; gap: 10px; }

/* ---------------- reader */
#reader { position: fixed; inset: 0; z-index: 20; background: #000; }
#stage { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; }
#stage.page { display: flex; touch-action: pan-x pan-y pinch-zoom; }
#stage.page img {
  margin: auto; max-width: 100%; max-height: 100%; object-fit: contain;
  user-select: none; -webkit-user-drag: none;
}
#stage.page.fit-width img { margin: 0 auto; max-height: none; width: 100%; }
#stage.scroll img {
  display: block; width: 100%; max-width: 900px; margin: 0 auto;
  aspect-ratio: 0.7; background: #0c0c0c;
}
#stage.scroll img.ok { aspect-ratio: auto; background: none; }
#stage.scroll img.bad { min-height: 220px; background: #18181b; }
#stage .loading { margin: auto; color: var(--muted); }

#stage.page.busy img { opacity: .45; transition: opacity .15s .15s; }
#stage.page.busy::after {
  content: "กำลังโหลด…"; position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, .78); padding: 8px 14px; border-radius: 10px;
  font-size: 13px; pointer-events: none; opacity: 0; animation: show 0s .35s forwards;
}
#stage.page.failed::after {
  content: "โหลดหน้านี้ไม่สำเร็จ — แตะเพื่อลองใหม่"; position: fixed; left: 50%; top: 50%;
  transform: translate(-50%, -50%); background: rgba(0, 0, 0, .88);
  padding: 12px 16px; border-radius: 12px; font-size: 14px; text-align: center;
}
@keyframes show { to { opacity: 1; } }

.bar {
  position: absolute; left: 0; right: 0;
  background: rgba(18, 19, 22, .94); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  transition: transform .2s, opacity .2s;
}
#rtop { top: 0; padding-top: calc(8px + var(--safe-top)); border-bottom: 1px solid rgba(255,255,255,.06); }
#rbottom { bottom: 0; padding-bottom: calc(10px + var(--safe-bottom)); border-top: 1px solid rgba(255,255,255,.06); }
#reader.hide-ui #rtop { transform: translateY(-100%); opacity: 0; pointer-events: none; }
#reader.hide-ui #rbottom { transform: translateY(100%); opacity: 0; pointer-events: none; }
.rtitle { flex: 1; min-width: 0; }
.rtitle div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#rslider { flex: 1; min-width: 60px; height: 44px; accent-color: var(--accent); }
#rslider.rtl { direction: rtl; }
#rpage { min-width: 54px; text-align: center; font-variant-numeric: tabular-nums; }

.chip {
  min-height: 44px; padding: 8px 12px; border-radius: 10px;
  background: var(--panel-2); font-size: 13px; white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center;
}
.chip:active:not(:disabled) { filter: brightness(1.12); }
.chip.wide {
  width: 100%; padding: 12px; margin-top: 8px;
  background: var(--accent); color: #111; font-weight: 700; font-size: 15px;
}
.chip.danger { background: var(--danger); color: #fff; font-weight: 600; }
.chip.ok { background: var(--ok); color: #fff; font-weight: 600; }

/* ---------------- admin */
.row.admin-row { justify-content: space-between; gap: 10px; padding: 10px 12px; border-color: var(--line); }
.row.admin-row:active { background: var(--panel); }
.admin-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.admin-name {
  font-weight: 600; font-size: 15px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  overflow-wrap: anywhere;
}
.role-tag {
  font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 6px;
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
}
.role-tag.admin {
  background: rgba(255, 138, 61, .16); color: var(--accent); border-color: rgba(255, 138, 61, .35);
}
.admin-actions { display: flex; gap: 6px; flex: none; }
.empty-inline {
  padding: 18px 12px; text-align: center; color: var(--muted);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; font-size: 13.5px;
}

/* ---------------- sheets & modals */
#settings, #modal { position: fixed; inset: 0; z-index: 30; }
#backdrop, .modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 1;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  padding: 10px 16px calc(18px + var(--safe-bottom));
  max-width: 560px; margin: 0 auto;
}
.sheet-handle { width: 38px; height: 4px; border-radius: 99px; background: var(--line); margin: 0 auto 12px; }
.sheet h3 { margin: 0 0 10px; font-size: 17px; }
.opt { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; transition: opacity .15s; }
.opt.dim { opacity: .42; pointer-events: none; }
.seg { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; border: 1px solid var(--line); }
.seg button { flex: 1; min-height: 44px; padding: 8px 6px; border-radius: 8px; font-size: 13.5px; }
.seg button.on { background: var(--accent); color: #111; font-weight: 700; }

.modal-card {
  position: relative; z-index: 1; width: min(320px, calc(100% - 32px));
  margin: 30vh auto 0; background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 18px;
}
.modal-card h3 { margin: 0 0 6px; font-size: 17px; }
.modal-card p { margin: 0 0 16px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.modal-actions .chip { flex: 1; }

.toast {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 50;
  background: rgba(15, 16, 18, .9); border: 1px solid var(--line);
  padding: 12px 18px; border-radius: 12px; pointer-events: none; font-size: 14px;
}

input[type=search]::-webkit-search-cancel-button { -webkit-appearance: none; display: none; }
