* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0f1117;
  --bg-2: #161922;
  --bg-3: #1f2330;
  --text: #e8eaf0;
  --muted: #8a91a4;
  --brand: #ff4d6d;
  --brand-2: #ff7a90;
  --ok: #1dd1a1;
  --warn: #feca57;
  --line: #262b3a;
}
html, body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Noto Sans Thai", "Segoe UI", Roboto, system-ui, sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand-2); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea {
  font-family: inherit;
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
  font-size: 15px;
}
input:focus { outline: 2px solid var(--brand); border-color: transparent; }

.btn {
  background: var(--brand);
  color: #fff;
  border: 0;
  padding: 11px 16px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 15px;
  transition: transform .08s ease, opacity .15s ease;
}
.btn:hover { opacity: .92; }
.btn:active { transform: scale(.98); }
.btn.secondary { background: var(--bg-3); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: #e74c3c; }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.full { width: 100%; }

/* ---- Auth pages ---- */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(1200px 600px at 10% -10%, rgba(255,77,109,.18), transparent),
              radial-gradient(900px 500px at 110% 110%, rgba(72,219,251,.15), transparent),
              var(--bg);
}
.auth-card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px 24px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.auth-card h1 { font-size: 22px; margin-bottom: 6px; }
.auth-card .sub { color: var(--muted); font-size: 14px; margin-bottom: 20px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.err { color: #ff7a90; font-size: 14px; margin-top: 8px; min-height: 20px; }
.brand-wrap {
  display: flex; flex-direction: column; align-items: center;
  gap: 10px; margin-bottom: 6px;
}
.brand-logo {
  height: 72px; width: auto; display: block;
  object-fit: contain;
}
.brand-mark {
  font-size: 22px; font-weight: 800; text-align: center;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
@media (max-width: 480px) {
  .brand-logo { height: 56px; }
  .brand-mark { font-size: 18px; }
}

/* ---- Topbar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px; border-bottom: 1px solid var(--line);
  background: var(--bg-2); position: sticky; top: 0; z-index: 50;
}
.topbar .logo {
  font-weight: 800; font-size: 18px;
  display: flex; align-items: center; gap: 10px;
}
.topbar .logo span {
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.logo-img {
  height: 32px; width: auto; display: block;
  object-fit: contain;
}
@media (max-width: 640px) {
  .logo-img { height: 26px; }
  .topbar .logo { font-size: 16px; gap: 8px; }
}
.topbar nav { display: flex; gap: 8px; align-items: center; }
.user-chip { color: var(--muted); font-size: 14px; margin-right: 8px; }

/* ---- Dashboard ---- */
.container { max-width: 1200px; margin: 0 auto; padding: 22px; }
h2.section { font-size: 20px; margin-bottom: 14px; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}
.card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: transform .15s ease, border-color .15s ease;
}
.card:hover { transform: translateY(-3px); border-color: var(--brand); }
.card .cover {
  width: 100%; aspect-ratio: 16/9; background: var(--bg-3); display: block;
  object-fit: cover;
}
.card .meta { padding: 10px 12px; }
.card .title { font-weight: 600; font-size: 14px; line-height: 1.3; margin-bottom: 4px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .tag { font-size: 12px; color: var(--muted); }

.empty {
  background: var(--bg-2); border: 1px dashed var(--line);
  border-radius: 12px; padding: 40px 20px; text-align: center; color: var(--muted);
}

/* ---- Admin ---- */
.admin-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
@media (max-width: 900px) { .admin-grid { grid-template-columns: 1fr; } }
.panel {
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 18px;
}
.panel h3 { margin-bottom: 14px; font-size: 16px; }
.row { display: flex; gap: 10px; margin-bottom: 12px; }
.row > * { flex: 1; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); font-size: 14px;
}
.table th { color: var(--muted); font-weight: 600; }
.table tr:hover { background: rgba(255,255,255,.02); }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  background: var(--bg-3); color: var(--muted); font-size: 12px;
}
.checklist {
  max-height: 380px; overflow-y: auto; border: 1px solid var(--line);
  border-radius: 10px; padding: 6px;
}
.checklist .item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
}
.checklist .item:hover { background: var(--bg-3); }
.checklist input[type=checkbox] { width: auto; }

/* ---- Watch page ---- */
.watch-wrap { padding: 20px; max-width: 1200px; margin: 0 auto; }
.watch-title { font-size: 22px; margin: 16px 0 8px; }
.watch-meta { color: var(--muted); margin-bottom: 14px; }

/* ---- Episode list (watch page) ---- */
.ep-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  margin-top: 14px;
}
.ep-card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s ease, transform .12s ease;
  user-select: none;
}
.ep-card:hover:not(.locked) { border-color: var(--brand); transform: translateY(-2px); }
.ep-card.active { border-color: var(--brand); }
.ep-card.active .ep-card-body { background: linear-gradient(180deg, rgba(255,77,109,.18), transparent); }
.ep-card.locked { cursor: not-allowed; }
.ep-card .ep-thumb {
  width: 100%;
  aspect-ratio: 16/9;
  background-color: var(--bg-3);
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex; align-items: center; justify-content: center;
}
.ep-card.locked .ep-thumb { filter: grayscale(.7) brightness(.55); }
.ep-card .ep-overlay {
  background: rgba(0,0,0,.55);
  color: #fff;
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 600;
  backdrop-filter: blur(4px);
}
.ep-card .ep-overlay.play {
  width: 44px; height: 44px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.ep-card .ep-overlay.lock {
  background: rgba(0,0,0,.6);
  width: 44px; height: 44px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.ep-card-body { padding: 10px 12px; }
.ep-card .ep-num { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.ep-card .ep-title { font-size: 13px; color: var(--muted); margin-bottom: 4px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.ep-card .ep-date { font-size: 12px; color: var(--warn); margin-top: 4px; }
.ep-card .ep-cd { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* Admin episode list inside modal */
.ep-admin-list { max-height: 320px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 14px; }
.ep-admin-list .row-ep {
  display: grid; grid-template-columns: 60px 1fr auto auto; gap: 10px; align-items: center;
  padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px;
}
.ep-admin-list .row-ep:last-child { border-bottom: 0; }
.ep-admin-list .row-ep:hover { background: var(--bg-3); }
.ep-admin-list .ep-no { font-weight: 700; font-size: 15px; color: var(--brand-2); }
.ep-admin-list .ep-info small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.ep-status {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: rgba(29,209,161,.15); color: var(--ok);
}
.ep-status.upcoming { background: rgba(254,202,87,.15); color: var(--warn); }
.ep-status.no-source { background: rgba(231,76,60,.15); color: #ff7a90; }

/* Watch progress bar (Netflix-style) */
.ep-progress {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 3px; background: rgba(0,0,0,.6);
}
.ep-progress-fill {
  height: 100%; background: var(--brand);
  transition: width .3s ease;
}
.card-progress { bottom: -1px; height: 4px; } /* sit at the bottom of cover image */

/* ---- Custom video player ---- */
.player {
  position: relative;
  width: 100%;
  background: #000;
  border-radius: 12px;
  aspect-ratio: 16/9;
}
.player.fullscreen { border-radius: 0; }

/* iOS pseudo-fullscreen — CSS-based fullscreen that keeps watermark visible
   (Native iOS fullscreen strips HTML overlays — this is an Apple limitation) */
.player.pseudo-fs {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh; /* dynamic viewport — accounts for Safari bars */
  z-index: 9999;
  border-radius: 0;
  background: #000;
}
.player.pseudo-fs video {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
body.pseudo-fs-active {
  overflow: hidden;
  /* allow landscape rotation for full immersion */
}
.player video {
  width: 100%; height: 100%; display: block; background: #000;
  border-radius: 12px;
}
.player.fullscreen video, .player.pseudo-fs video { border-radius: 0; }
.player .center-play {
  position: absolute; inset: 0; display: grid; place-items: center;
  pointer-events: none;
}
.player .center-play .btn-circle {
  width: 76px; height: 76px; border-radius: 50%;
  background: rgba(0,0,0,.5); border: 2px solid rgba(255,255,255,.8);
  display: grid; place-items: center; pointer-events: auto;
  opacity: 0; transition: opacity .2s ease;
}
.player.paused .center-play .btn-circle { opacity: 1; }
.player .center-play svg { width: 32px; height: 32px; fill: #fff; }

.controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 8px 12px 12px;
  background: linear-gradient(to top, rgba(0,0,0,.85), rgba(0,0,0,0));
  opacity: 0; transition: opacity .2s ease;
  user-select: none;
}
.player.show-controls .controls,
.player.paused .controls { opacity: 1; }

.progress {
  position: relative;
  width: 100%; height: 16px; cursor: pointer;
  display: flex; align-items: center;
}
.progress .bar {
  position: absolute; left: 0; right: 0; top: 7px; height: 4px;
  background: rgba(255,255,255,.25); border-radius: 2px;
}
.progress .buffered {
  position: absolute; left: 0; top: 7px; height: 4px;
  background: rgba(255,255,255,.4); border-radius: 2px;
}
.progress .filled {
  position: absolute; left: 0; top: 7px; height: 4px;
  background: var(--brand); border-radius: 2px;
}
.progress .knob {
  position: absolute; top: 4px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--brand);
  transform: translateX(-50%); pointer-events: none;
  box-shadow: 0 0 0 4px rgba(255,77,109,.25);
  opacity: 0; transition: opacity .15s ease;
}
.progress:hover .knob { opacity: 1; }

.ctrl-row {
  display: flex; align-items: center; gap: 6px;
  margin-top: 4px;
  color: #fff;
}
.ctrl-row .spacer { flex: 1; }
.ctrl-btn {
  background: transparent; border: 0; color: #fff;
  width: 36px; height: 36px; border-radius: 6px;
  display: grid; place-items: center;
}
.ctrl-btn:hover { background: rgba(255,255,255,.12); }
.ctrl-btn svg { width: 22px; height: 22px; fill: #fff; }
.time { font-size: 13px; color: rgba(255,255,255,.85); padding: 0 6px; min-width: 100px; }

.volume {
  display: flex; align-items: center; gap: 6px;
}
.volume input[type=range] {
  width: 90px; accent-color: var(--brand);
  background: transparent; padding: 0; border: 0;
}
@media (max-width: 600px) {
  .volume input[type=range] { display: none; }
  .time { min-width: 80px; font-size: 12px; }
}

.menu-wrap { position: relative; }
.menu {
  position: absolute; bottom: 44px; right: 0;
  background: rgba(20,22,30,.96);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px;
  min-width: 140px;
  max-height: 60vh;
  overflow-y: auto;
  display: none;
  z-index: 60;
}
.menu.open { display: block; }
.menu .item {
  padding: 8px 12px; border-radius: 6px; font-size: 14px; color: #fff;
  cursor: pointer; display: flex; justify-content: space-between; align-items: center;
}
.menu .item:hover { background: rgba(255,255,255,.08); }
.menu .item.active { color: var(--brand); }
.menu .label { font-size: 12px; color: var(--muted); padding: 6px 12px; }

/* Watermark overlay (anti-leak deterrent) — fixed top-center */
.player .wm {
  position: absolute;
  top: 5%;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
  font-family: 'Prompt', -apple-system, "Noto Sans Thai", sans-serif;
  color: rgba(255,255,255,.28);
  font-size: 24px;
  font-weight: 700;
  text-shadow: 0 1px 4px rgba(0,0,0,.85), 0 -1px 4px rgba(0,0,0,.85), 0 0 8px rgba(0,0,0,.5);
  z-index: 5;
  user-select: none;
  white-space: nowrap;
  letter-spacing: .8px;
}
@media (max-width: 640px) {
  .player .wm { font-size: 18px; top: 5%; }
}
/* Pseudo-FS: video is letterboxed via object-fit:contain.
   Anchor watermark to the actual top edge of the visible video frame,
   not to the viewport (otherwise it lands in the black bar). */
.player.pseudo-fs .wm {
  top: calc(max(0px, (100dvh - 100vw * 9 / 16) / 2) + 16px);
}

/* Loading spinner over player */
.player .loader {
  position: absolute; inset: 0; display: none; place-items: center;
  pointer-events: none;
}
.player.loading .loader { display: grid; }
.loader .spin {
  width: 40px; height: 40px;
  border: 3px solid rgba(255,255,255,.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: none; align-items: flex-start; justify-content: center;
  padding: 30px 16px; overflow-y: auto; z-index: 100;
}
.modal-backdrop.open { display: flex; }
.modal-card {
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 14px; padding: 22px; width: 100%; max-width: 560px;
  box-shadow: 0 25px 80px rgba(0,0,0,.5);
}
.modal-card h3 { margin-bottom: 16px; }
.modal-card .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal-card .actions {
  display: flex; gap: 10px; justify-content: flex-end;
  margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line);
}
.cover-preview {
  width: 100%; aspect-ratio: 16/9; max-width: 220px;
  background: var(--bg-3); border-radius: 8px; object-fit: cover; display: block;
  border: 1px solid var(--line);
}
.field-grid {
  display: grid; grid-template-columns: 220px 1fr; gap: 14px; align-items: start;
}
@media (max-width: 600px) {
  .field-grid { grid-template-columns: 1fr; }
  .modal-card .row { grid-template-columns: 1fr; }
  .cover-preview { max-width: 100%; }
}
textarea { resize: vertical; min-height: 70px; }

/* Quality badge — Full HD / 4K UHD */
.q-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .5px;
  vertical-align: middle;
  margin-left: 6px;
  text-shadow: 0 1px 1px rgba(0,0,0,.3);
  white-space: nowrap;
}
.q-badge-fhd {
  background: linear-gradient(135deg, #3b82f6, #60a5fa);
  color: #fff;
}
.q-badge-4k {
  background: linear-gradient(135deg, #f59e0b, #fbbf24);
  color: #1a1a1a;
  text-shadow: none;
}

/* Genre chip picker (multi-select) */
.genre-picker {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}
.genre-chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; min-height: 28px; }
.genre-chips:empty::after {
  content: 'ยังไม่ได้เลือกแนว';
  color: var(--muted); font-size: 12px; padding: 4px;
}
.genre-chip {
  background: var(--brand);
  color: #fff;
  padding: 4px 4px 4px 12px;
  border-radius: 999px;
  font-size: 13px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px;
  line-height: 1;
}
.genre-chip small { opacity: .85; font-weight: 400; }
.genre-chip button {
  background: rgba(0,0,0,.25); border: 0; color: #fff;
  width: 20px; height: 20px; border-radius: 50%;
  font-size: 12px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0;
}
.genre-chip button:hover { background: rgba(0,0,0,.5); }
.genre-picker select { width: 100%; font-size: 13px; }
.genre-picker .hint { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* search bar */
.search-row { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.search-row > * { flex: 1; min-width: 160px; }
.search-row .btn { flex: 0 0 auto; }

/* combobox (searchable dropdown) */
.combobox { position: relative; }
.combobox-input-wrap { position: relative; }
.combobox-input-wrap input { padding-right: 36px; }
.combobox-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: var(--bg-3); border: 1px solid var(--line); color: var(--muted);
  width: 26px; height: 26px; border-radius: 50%;
  display: none; align-items: center; justify-content: center;
  font-size: 16px; line-height: 1;
}
.combobox-clear:hover { color: var(--text); border-color: var(--brand); }
.combobox.has-selection .combobox-clear { display: flex; }
.combobox-menu {
  position: absolute; top: 100%; left: 0; right: 0;
  margin-top: 4px;
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 8px;
  max-height: 280px; overflow-y: auto;
  display: none; z-index: 50;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.combobox.open .combobox-menu { display: block; }
.combobox-menu .opt {
  padding: 10px 12px; cursor: pointer; font-size: 14px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  border-bottom: 1px solid rgba(255,255,255,.04);
}
.combobox-menu .opt:last-child { border-bottom: 0; }
.combobox-menu .opt:hover { background: var(--bg-3); }
.combobox-menu .opt.active { background: rgba(255,77,109,.15); color: var(--brand-2); }
.combobox-menu .opt small { color: var(--muted); font-size: 12px; }
.combobox-empty { padding: 18px; text-align: center; color: var(--muted); font-size: 13px; }

/* pagination */
.pager {
  display: flex; gap: 4px; justify-content: center; align-items: center;
  margin-top: 14px; padding-top: 10px; flex-wrap: wrap;
}
.pager .page-btn {
  background: var(--bg-3); color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 11px; font-size: 13px; min-width: 34px;
}
.pager .page-btn:hover:not(:disabled) { border-color: var(--brand); color: var(--brand); }
.pager .page-btn.active {
  background: var(--brand); color: #fff; border-color: var(--brand); font-weight: 600;
}
.pager .page-btn:disabled { opacity: .35; cursor: not-allowed; }
.pager .page-gap { color: var(--muted); padding: 0 4px; user-select: none; }

/* Mobile tweaks */
@media (max-width: 640px) {
  .container { padding: 14px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .topbar { padding: 12px 14px; }
  .auth-card { padding: 22px 18px; }
}
