/*
 * VPlayer — ธีม
 *
 * เลือกธีม:   new VPlayer(el, { theme: 'dark' | 'light' | 'auto' | 'cream', accent: '#22c55e' })
 *             หรือ player.setTheme('light') / player.setAccent('#22c55e')
 * สร้างธีมเอง: กำหนด token ด้านล่างใน .vp[data-vp-theme="ชื่อธีม"] { ... } แล้ว setTheme('ชื่อธีม')
 *
 * สีอื่น ๆ ทั้งหมด (hover, แถบเวลา, เงา ฯลฯ) คำนวณจาก token เหล่านี้ด้วย color-mix()
 */
.vp {
  --vp-accent: #F472B6;     /* สีหลัก: แถบเวลา, ปุ่มที่เลือก, spinner */
  --vp-on-accent: #2a0f1f;  /* สีไอคอน/ตัวอักษรบนพื้น accent (setAccent() คำนวณให้อัตโนมัติ) */
  --vp-fg: #fff;            /* ไอคอนและตัวอักษรบนแถบควบคุม */
  --vp-shade: #000;         /* สีไล่เฉดใต้แถบควบคุม/ชื่อเรื่อง และพื้นหลัง overlay */
  --vp-bg: #000;            /* พื้นหลังรอบวิดีโอ (letterbox) */
  --vp-panel: rgba(18, 18, 22, .94);   /* พื้นเมนูตั้งค่าและ tooltip */
  --vp-panel-fg: #fff;
  --vp-cue-bg: rgba(0, 0, 0, .7);      /* พื้นคำบรรยาย */
  --vp-cue-fg: #fff;
  --vp-radius: 12px;
  --vp-font: system-ui, -apple-system, "Sarabun", "Noto Sans Thai", "Segoe UI", sans-serif;

  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: var(--vp-bg);
  color: var(--vp-fg);
  overflow: hidden;
  border-radius: var(--vp-radius);
  font: 14px/1.4 var(--vp-font);
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
  container-type: inline-size;
}

/* ---------- ธีมสำเร็จรูป ---------- */
.vp[data-vp-theme="light"] {
  --vp-fg: #18181d;
  --vp-shade: #fff;
  --vp-panel: rgba(255, 255, 255, .96);
  --vp-panel-fg: #18181d;
}
.vp[data-vp-theme="cream"] {   /* โทนแบรนด์: ชมพู + ครีม + ตัวอักษรม่วงเข้ม */
  --vp-accent: #F472B6;
  --vp-on-accent: #3D1A2E;
  --vp-fg: #3D1A2E;
  --vp-shade: #FFF8E7;
  --vp-panel: rgba(255, 248, 231, .97);
  --vp-panel-fg: #3D1A2E;
  --vp-cue-bg: rgba(61, 26, 46, .78);
}
@media (prefers-color-scheme: light) {
  .vp[data-vp-theme="auto"] {
    --vp-fg: #18181d;
    --vp-shade: #fff;
    --vp-panel: rgba(255, 255, 255, .96);
    --vp-panel-fg: #18181d;
  }
}

.vp:focus-visible { box-shadow: 0 0 0 3px color-mix(in srgb, var(--vp-accent) 70%, transparent); }
.vp.vp-fs, .vp:fullscreen { border-radius: 0; aspect-ratio: auto; width: 100%; height: 100%; }
.vp.vp-idle { cursor: none; }
.vp *, .vp *::before, .vp *::after { box-sizing: border-box; }

.vp-video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: contain;
  background: var(--vp-bg);
  display: block;
}
/* เต็มจอสำรอง เมื่อเบราว์เซอร์ไม่มี Fullscreen API */
.vp.vp-pseudo-fs {
  position: fixed; inset: 0; z-index: 2147483000;
  width: 100vw; height: 100vh; height: 100dvh;
  aspect-ratio: auto; border-radius: 0;
}
html.vp-noscroll, html.vp-noscroll body { overflow: hidden; }

/* ---------- overlays ---------- */
.vp-title, .vp-spinner, .vp-ripple, .vp-toast, .vp-audio-art, .vp-cues { pointer-events: none; }

/* คำบรรยาย: ยกขึ้นหลบแถบควบคุมเมื่อแถบแสดงอยู่, ขนาดตัวอักษรตามความกว้าง player */
.vp-cues {
  position: absolute; left: 5%; right: 5%; bottom: 4%;
  text-align: center;
  font-family: var(--vp-font);
  font-size: clamp(14px, 3.4cqw, 40px);
  line-height: 1.35;
  white-space: pre-line;
  transition: bottom .25s;
  z-index: 1;
}
.vp:not(.vp-idle) .vp-cues { bottom: max(4%, 76px); }
.vp-cues span {
  display: inline;
  padding: .08em .35em;
  background: var(--vp-cue-bg);
  color: var(--vp-cue-fg);
  border-radius: 4px;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

.vp-title {
  position: absolute; top: 0; left: 0; right: 0;
  padding: 14px 18px 32px;
  font-size: 16px; font-weight: 600;
  background: linear-gradient(color-mix(in srgb, var(--vp-shade) 70%, transparent), transparent);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: opacity .25s;
}
.vp-title:empty, .vp-empty .vp-title { display: none; }

.vp-audio-art {
  position: absolute; inset: 0;
  display: none; place-items: center;
  background: radial-gradient(circle at center, color-mix(in srgb, var(--vp-accent) 35%, var(--vp-shade)) 0%, var(--vp-shade) 70%);
}
.vp-audio-art svg { width: 22%; max-width: 140px; fill: var(--vp-accent); opacity: .9; }
.vp-audio .vp-audio-art { display: grid; }
.vp-audio.vp-paused .vp-audio-art svg { opacity: .5; }

.vp-spinner {
  position: absolute; top: 50%; left: 50%;
  width: 52px; height: 52px; margin: -26px 0 0 -26px;
  border: 4px solid color-mix(in srgb, var(--vp-fg) 25%, transparent);
  border-top-color: var(--vp-accent);
  border-radius: 50%;
  display: none;
  animation: vp-spin .8s linear infinite;
}
.vp-loading .vp-spinner { display: block; }
@keyframes vp-spin { to { transform: rotate(360deg); } }

.vp-big-play {
  position: absolute; top: 50%; left: 50%;
  width: 76px; height: 76px; margin: -38px 0 0 -38px;
  border: 0; border-radius: 50%;
  background: color-mix(in srgb, var(--vp-shade) 60%, transparent);
  color: var(--vp-fg);
  display: none; place-items: center;
  cursor: pointer;
  transition: background .2s, transform .2s;
}
.vp-big-play svg { width: 44px; height: 44px; fill: currentColor; margin-left: 4px; }
.vp-big-play:hover { background: var(--vp-accent); color: var(--vp-on-accent); transform: scale(1.06); }
.vp-paused:not(.vp-loading):not(.vp-has-error):not(.vp-empty) .vp-big-play { display: grid; }

.vp-ripple {
  position: absolute; top: 0; bottom: 0; width: 38%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px;
  opacity: 0;
}
.vp-ripple-l { left: 0; border-radius: 0 50% 50% 0; background: radial-gradient(circle at 30% 50%, color-mix(in srgb, var(--vp-fg) 18%, transparent), transparent 70%); }
.vp-ripple-r { right: 0; border-radius: 50% 0 0 50%; background: radial-gradient(circle at 70% 50%, color-mix(in srgb, var(--vp-fg) 18%, transparent), transparent 70%); }
.vp-ripple.show { animation: vp-ripple .6s ease-out; }
@keyframes vp-ripple { 0% { opacity: 0; } 25% { opacity: 1; } 100% { opacity: 0; } }

.vp-toast {
  position: absolute; top: 14%; left: 50%;
  transform: translateX(-50%);
  padding: 7px 16px;
  border-radius: 999px;
  background: var(--vp-panel);
  color: var(--vp-panel-fg);
  font-weight: 600;
  white-space: nowrap;
  opacity: 0;
  transition: opacity .2s;
}
.vp-toast.show { opacity: 1; }

.vp-error {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: color-mix(in srgb, var(--vp-shade) 90%, transparent);
  text-align: center;
  z-index: 3;
}
.vp-error[hidden] { display: none; }
.vp-error-box { max-width: 560px; }
.vp-error-title { font-size: 16px; font-weight: 600; margin-bottom: 10px; }
.vp-error-hint { font-size: 13px; opacity: .8; }
.vp-error-skip { margin-top: 12px; font-size: 13px; color: var(--vp-accent); }
.vp-error-hint code {
  display: block; margin-top: 8px; padding: 8px 10px;
  background: color-mix(in srgb, var(--vp-fg) 10%, transparent); border-radius: 6px;
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px;
  user-select: all; -webkit-user-select: all;
  word-break: break-all;
}

/* ---------- controls ---------- */
.vp-controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 28px 10px 6px;
  background: linear-gradient(transparent, color-mix(in srgb, var(--vp-shade) 80%, transparent));
  transition: opacity .25s, transform .25s;
  z-index: 2;
}
.vp-empty .vp-controls { display: none; }
/* พื้นที่โปร่งใสของแถบควบคุมให้แตะทะลุถึงวิดีโอ (สำคัญบนจอเล็ก) — รับคลิกเฉพาะปุ่มและแถบเวลา */
.vp-controls, .vp-bar { pointer-events: none; }
.vp-progress, .vp-bar > * { pointer-events: auto; }
.vp-idle .vp-controls, .vp-idle .vp-title { opacity: 0; }
.vp-idle .vp-controls * { pointer-events: none; }
.vp-idle .vp-controls { transform: translateY(6px); }

.vp-progress {
  position: relative;
  height: 18px;
  display: flex; align-items: center;
  cursor: pointer;
  touch-action: none;
  margin: 0 6px;
}
.vp-track {
  position: relative; width: 100%; height: 4px;
  background: color-mix(in srgb, var(--vp-fg) 25%, transparent);
  border-radius: 4px;
  transition: height .15s;
}
.vp-progress:hover .vp-track, .vp-scrubbing .vp-track { height: 6px; }
.vp-buffered, .vp-played {
  position: absolute; inset: 0;
  transform-origin: left center;
  transform: scaleX(0);
  border-radius: inherit;
}
.vp-buffered { background: color-mix(in srgb, var(--vp-fg) 40%, transparent); }
.vp-played { background: var(--vp-accent); }
.vp-knob {
  position: absolute; top: 50%; left: 0;
  width: 14px; height: 14px; margin: -7px 0 0 -7px;
  border-radius: 50%;
  background: var(--vp-accent);
  transform: scale(0);
  transition: transform .15s;
}
.vp-progress:hover .vp-knob, .vp-scrubbing .vp-knob { transform: scale(1); }
.vp-tip {
  position: absolute; bottom: 22px; left: 0;
  transform: translateX(-50%);
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--vp-panel);
  color: var(--vp-panel-fg);
  font-size: 12px; font-variant-numeric: tabular-nums;
  pointer-events: none;
  opacity: 0;
  transition: opacity .12s;
}
.vp-tip.show { opacity: 1; }
.vp-live .vp-progress { visibility: hidden; }

.vp-bar { display: flex; align-items: center; gap: 2px; height: 44px; }
.vp-spacer { flex: 1; }

.vp-btn {
  position: relative;
  width: 40px; height: 40px; flex: none;
  padding: 0; border: 0; border-radius: 8px;
  background: none; color: inherit;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background .15s;
}
.vp-btn[hidden] { display: none; }
.vp-btn:disabled { opacity: .35; cursor: default; background: none; }
.vp-pl-only { display: none; }
.vp-has-list .vp-pl-only { display: grid; }
.vp-btn svg { width: 24px; height: 24px; fill: currentColor; }
.vp-btn svg text { fill: currentColor; font-family: system-ui, sans-serif; }
.vp-btn:hover { background: color-mix(in srgb, var(--vp-fg) 14%, transparent); }
.vp-btn:focus-visible { outline: 2px solid var(--vp-accent); outline-offset: -2px; }
.vp-btn[data-act="cc"].vp-on::after {
  content: ""; position: absolute; left: 10px; right: 10px; bottom: 5px;
  height: 3px; border-radius: 2px; background: var(--vp-accent);
}
.vp-btn[data-act="settings"] svg { transition: transform .3s; }
.vp-btn[data-act="settings"].vp-on svg { transform: rotate(45deg); }
.vp-btn[data-act="pip"].vp-on { color: var(--vp-accent); }

.vp-vol { display: flex; align-items: center; }
.vp-vol-slider {
  --vp-fill: 100%;
  -webkit-appearance: none; appearance: none;
  width: 0; height: 4px; margin: 0;
  border-radius: 4px;
  background: linear-gradient(to right, var(--vp-fg) var(--vp-fill), color-mix(in srgb, var(--vp-fg) 30%, transparent) var(--vp-fill));
  opacity: 0;
  cursor: pointer;
  transition: width .2s, opacity .2s, margin .2s;
}
.vp-vol:hover .vp-vol-slider, .vp-vol:focus-within .vp-vol-slider { width: 76px; opacity: 1; margin: 0 8px 0 4px; }
.vp-vol-slider::-webkit-slider-thumb { -webkit-appearance: none; width: 12px; height: 12px; border-radius: 50%; background: var(--vp-fg); border: 0; }
.vp-vol-slider::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: var(--vp-fg); border: 0; }

.vp-time {
  margin-left: 8px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.vp-live .vp-dur-wrap { display: none; }
.vp-live-badge {
  display: none; align-items: center; gap: 6px;
  margin-left: 10px; padding: 2px 8px;
  border: 0; border-radius: 4px;
  background: none; color: inherit;
  font: inherit; font-size: 12px; font-weight: 700;
  cursor: pointer;
}
.vp-live-badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #ef4444; }
.vp-live .vp-live-badge { display: inline-flex; }

/* ---------- settings menu ---------- */
.vp-menu {
  position: absolute; right: 12px; bottom: 64px;
  min-width: 240px; max-width: calc(100% - 24px);
  max-height: calc(100% - 80px);
  overflow-y: auto;
  padding: 6px;
  border-radius: 12px;
  background: var(--vp-panel);
  color: var(--vp-panel-fg);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  z-index: 4;
}
.vp-menu[hidden] { display: none; }
.vp-menu-item {
  display: flex; align-items: center; gap: 16px;
  width: 100%;
  padding: 10px 12px;
  border: 0; border-radius: 8px;
  background: none; color: inherit;
  font: inherit; text-align: left;
  cursor: pointer;
}
.vp-menu-item:hover, .vp-menu-item:focus-visible { background: color-mix(in srgb, var(--vp-panel-fg) 10%, transparent); outline: none; }
.vp-menu-item > span:first-child { flex: 1; }
.vp-menu-right { opacity: .7; white-space: nowrap; }
.vp-menu-item.is-more::after { content: "›"; font-size: 18px; line-height: 1; opacity: .7; }
.vp-menu-item.is-back { font-weight: 600; border-bottom: 1px solid color-mix(in srgb, var(--vp-panel-fg) 14%, transparent); border-radius: 8px 8px 0 0; margin-bottom: 4px; }
.vp-menu-item.is-back::before { content: "‹"; font-size: 18px; line-height: 1; margin-right: -6px; }
.vp-menu-item.is-checked { color: var(--vp-accent); }
.vp-menu-item.is-checked::after { content: "✓"; font-weight: 700; }

/* ---------- เพลย์ลิสต์ ---------- */
.vp-list {
  position: absolute; top: 12px; right: 12px; bottom: 64px;
  width: min(340px, calc(100% - 24px));
  display: flex; flex-direction: column;
  border-radius: 12px;
  background: var(--vp-panel);
  color: var(--vp-panel-fg);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  overflow: hidden;
  z-index: 4;
}
.vp-list[hidden] { display: none; }
.vp-list-head {
  display: flex; align-items: center; gap: 2px;
  padding: 6px 6px 6px 14px;
  border-bottom: 1px solid color-mix(in srgb, var(--vp-panel-fg) 14%, transparent);
}
.vp-list-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.vp-list-head .vp-btn { width: 34px; height: 34px; }
.vp-list-head .vp-btn svg { width: 20px; height: 20px; }
.vp-list-head .vp-btn:hover { background: color-mix(in srgb, var(--vp-panel-fg) 10%, transparent); }
.vp-list-head .vp-btn.vp-on { color: var(--vp-accent); }
.vp-list-items {
  flex: 1; margin: 0; padding: 6px; list-style: none;
  overflow-y: auto; overscroll-behavior: contain;
}
.vp-list-items li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 6px 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.vp-list-items li:hover { background: color-mix(in srgb, var(--vp-panel-fg) 10%, transparent); }
.vp-list-items li.is-active { background: color-mix(in srgb, var(--vp-accent) 18%, transparent); color: var(--vp-accent); font-weight: 600; }
.vp-list-num {
  flex: none; width: 22px;
  text-align: center; font-size: 12px; font-variant-numeric: tabular-nums; opacity: .7;
}
.vp-list-num svg { width: 16px; height: 16px; fill: currentColor; vertical-align: middle; }
.is-active .vp-list-num { opacity: 1; }
.vp-list-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.vp-list-del {
  flex: none; width: 28px; height: 28px; padding: 0;
  display: grid; place-items: center;
  border: 0; border-radius: 6px; background: none; color: inherit;
  opacity: 0; cursor: pointer;
}
.vp-list-del svg { width: 16px; height: 16px; fill: currentColor; }
.vp-list-items li:hover .vp-list-del, .vp-list-del:focus-visible { opacity: .7; }
.vp-list-del:hover { opacity: 1; background: color-mix(in srgb, var(--vp-panel-fg) 12%, transparent); }

/* ---------- จอเล็ก / มือถือ ---------- */
@container (max-width: 520px) {
  .vp-hide-sm { display: none; }
  .vp-btn { width: 36px; height: 36px; }
  .vp-btn svg { width: 22px; height: 22px; }
  .vp-time { font-size: 12px; margin-left: 4px; }
  .vp-menu { bottom: 56px; min-width: 200px; }
  .vp-list { top: 0; right: 0; bottom: 0; width: 100%; border-radius: 0; }
  .vp-big-play { width: 60px; height: 60px; margin: -30px 0 0 -30px; }
  .vp-big-play svg { width: 34px; height: 34px; }
}
@media (hover: none) {
  .vp-vol-slider { display: none; }
  .vp-list-del { opacity: .6; }
  .vp-knob { transform: scale(1); width: 12px; height: 12px; margin: -6px 0 0 -6px; }
  .vp-progress { height: 24px; }
}
@media (prefers-reduced-motion: reduce) {
  .vp *, .vp *::before, .vp *::after { transition: none !important; animation-duration: 0s !important; }
  .vp-spinner { animation: vp-spin 1.6s linear infinite !important; }
}
