:root {
  --bg: #0e0e10;
  --panel: #18181b;
  --panel-2: #26262b;
  --text: #f2f2f2;
  --muted: #9b9ba3;
  --accent: #ff4b4b;
  --accent-soft: rgba(255, 75, 75, 0.16);
  --track: rgba(255, 255, 255, 0.24);
  --buffered: rgba(255, 255, 255, 0.45);
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", Pretendard, "Noto Sans KR", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; }

/* ---------- 레이아웃: 데스크톱은 스크롤 없는 한 화면, 영상 70% / 챕터 30% ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 7fr) minmax(300px, 3fr);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "player top"
    "player chapters";
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #000;
}
.layout.no-chapters {
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "player" "player";
}
.layout.no-chapters .topbar, .layout.no-chapters .chapters { display: none; }

.topbar {
  grid-area: top;
  display: flex; flex-direction: column; gap: 4px;
  padding: 18px 20px 12px;
  background: var(--panel);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; letter-spacing: -0.01em; }
.topbar .meta { color: var(--muted); font-size: 13px; }

/* 왼쪽 칸: 검은 배경, 영상을 세로 중앙에, 너비는 칸의 100% */
.player-col {
  grid-area: player;
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: #000;
}
.now { display: none; }

/* ---------- 플레이어 ---------- */
.player {
  position: relative;
  background: #000;
  aspect-ratio: var(--aspect, 16 / 9);
  width: 100%;
  max-height: 100vh;
  max-height: 100dvh; /* 화면이 낮으면 좌우 레터박스로 맞춤 */
  margin: 0;
  overflow: hidden;
  outline: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.player:fullscreen {
  width: 100%; height: 100%; max-height: none; aspect-ratio: auto; border-radius: 0;
}
.player video {
  display: block; width: 100%; height: 100%; object-fit: contain; background: #000;
}
.player.idle { cursor: none; }
.player.idle .controls, .player.idle .chapter-toast { opacity: 0; pointer-events: none; }

.controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 24px 12px 6px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.88), rgba(0, 0, 0, 0.45) 55%, transparent);
  transition: opacity 0.25s;
}

/* 진행바 */
.progress {
  position: relative; height: 20px;
  display: flex; align-items: center;
  cursor: pointer; touch-action: none;
}
.track { position: relative; width: 100%; height: 4px; transition: height 0.12s; }
.progress.hovering .track, .player.scrubbing .track { height: 6px; }
.seg { position: absolute; top: 0; bottom: 0; padding: 0 1px; }
.seg:first-child { padding-left: 0; }
.seg:last-child { padding-right: 0; }
.seg-inner {
  position: relative; height: 100%; background: var(--track);
  border-radius: 2px; overflow: hidden;
  transition: transform 0.12s;
}
.seg.hover .seg-inner { transform: scaleY(1.7); }
.seg-buf, .seg-played {
  position: absolute; inset: 0; transform-origin: left center; transform: scaleX(0);
}
.seg-buf { background: var(--buffered); }
.seg-played { background: var(--accent); }
.knob {
  position: absolute; top: 50%; left: 0; width: 14px; height: 14px;
  margin: -7px 0 0 -7px; border-radius: 50%; background: var(--accent);
  transform: scale(0); transition: transform 0.12s; pointer-events: none;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.35);
}
.progress.hovering .knob, .player.scrubbing .knob { transform: scale(1); }
.hover-line {
  position: absolute; top: 4px; bottom: 4px; width: 2px; margin-left: -1px;
  background: rgba(255, 255, 255, 0.7); pointer-events: none; display: none;
}
.progress.hovering .hover-line { display: block; }

.preview {
  position: absolute; bottom: 24px; left: 0;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  pointer-events: none;
}
.preview[hidden] { display: none; }
.preview-img {
  width: 200px; height: 112px; border-radius: 8px; border: 2px solid #fff;
  background-color: #000; background-repeat: no-repeat;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.6);
}
.preview-img[hidden] { display: none; }
.preview-chapter {
  font-size: 12px; color: #e6e6e6; max-width: 240px; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
}
.preview-time { font-size: 13px; font-weight: 700; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9); }
@media (max-width: 600px) { .preview-img { width: 140px; height: 79px; } }

/* 버튼 줄 */
.buttons { display: flex; align-items: center; gap: 2px; }
.buttons .spacer { flex: 1; }
.btn {
  appearance: none; border: 0; background: transparent; color: #fff;
  width: 40px; height: 40px; padding: 0; border-radius: 8px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  position: relative; flex: 0 0 auto;
}
.btn:hover { background: rgba(255, 255, 255, 0.12); }
.btn:focus-visible { outline: 2px solid #fff; outline-offset: -2px; }
.btn svg { width: 24px; height: 24px; fill: currentColor; flex: 0 0 auto; }
.btn[hidden] { display: none; }
.btn.text { width: auto; padding: 0 10px 0 8px; font-size: 13px; font-weight: 600; }
.btn.text .label { white-space: nowrap; }
.time { font-size: 13px; font-variant-numeric: tabular-nums; margin: 0 8px; white-space: nowrap; color: #eee; }

.volume-wrap { display: flex; align-items: center; }
.volume-wrap[hidden] { display: none; }
.volume-wrap input {
  width: 0; opacity: 0; margin: 0; transition: width 0.2s, opacity 0.2s;
  accent-color: #fff; cursor: pointer;
}
.volume-wrap:hover input, .volume-wrap:focus-within input { width: 80px; opacity: 1; margin-right: 8px; }
@media (pointer: coarse) {
  .volume-wrap input { display: none; }
  .btn.text .label { font-size: 12px; }
  #btnBack, #btnFwd, #btnHelp, #btnPip { display: none; }
}
@media (max-width: 480px) {
  .time { font-size: 12px; margin: 0 4px; }
  #btnShare { display: none; }
}

.i-pause, .i-replay, .i-full-exit, .i-vol-low, .i-vol-off { display: none; }
.player.playing .i-play { display: none; }
.player.playing .i-pause { display: block; }
.player.ended .i-play, .player.ended .i-pause { display: none; }
.player.ended .i-replay { display: block; }
.player.fullscreen .i-full { display: none; }
.player.fullscreen .i-full-exit { display: block; }
.player.vol-low .i-vol-high, .player.vol-off .i-vol-high { display: none; }
.player.vol-low .i-vol-low, .player.vol-off .i-vol-off { display: block; }

/* 메뉴 */
.menu {
  position: absolute; bottom: 56px; right: 12px;
  background: rgba(24, 24, 27, 0.97); border-radius: 12px; padding: 6px; min-width: 150px;
  display: none; flex-direction: column; box-shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.06);
}
.menu.open { display: flex; }
.menu button {
  appearance: none; border: 0; background: transparent; color: #eee; text-align: left;
  padding: 9px 12px 9px 30px; border-radius: 8px; font-size: 14px; cursor: pointer; position: relative;
}
.menu button:hover { background: rgba(255, 255, 255, 0.1); }
.menu button.active { color: #fff; font-weight: 700; }
.menu button.active::before {
  content: ""; position: absolute; left: 12px; top: 50%; width: 8px; height: 8px; margin-top: -4px;
  border-radius: 50%; background: var(--accent);
}

/* 중앙 버튼, 토스트, 로딩, 에러 */
.big-play {
  position: absolute; inset: 0; margin: auto; width: 78px; height: 78px; border-radius: 50%;
  background: rgba(0, 0, 0, 0.62); border: 0; color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: opacity 0.2s, transform 0.2s;
}
.big-play:hover { transform: scale(1.06); background: rgba(0, 0, 0, 0.75); }
.big-play svg { width: 42px; height: 42px; fill: currentColor; }
.big-play .i-play { margin-left: 4px; }
.player.playing .big-play { opacity: 0; pointer-events: none; }
@media (pointer: coarse) {
  .player.playing:not(.idle) .big-play { opacity: 1; pointer-events: auto; }
}

.chapter-toast {
  position: absolute; top: 14px; left: 14px; max-width: 70%;
  background: rgba(0, 0, 0, 0.66); padding: 8px 12px; border-radius: 10px;
  font-size: 14px; font-weight: 600; opacity: 0; transition: opacity 0.25s; pointer-events: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chapter-toast.show { opacity: 1; }
.toast {
  position: absolute; bottom: 84px; left: 50%; transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.78); color: #fff; padding: 8px 14px; border-radius: 999px;
  font-size: 13px; opacity: 0; transition: opacity 0.2s; pointer-events: none; white-space: nowrap;
}
.toast.show { opacity: 1; }

.loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.loading[hidden] { display: none; }
.spinner {
  width: 56px; height: 56px; border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.22); border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: rgba(0, 0, 0, 0.82); text-align: center; padding: 20px; z-index: 5;
}
.error[hidden] { display: none; }
.error p { margin: 0; font-size: 15px; }
.error button, .help-close button {
  appearance: none; border: 0; background: #fff; color: #111; padding: 9px 16px;
  border-radius: 999px; font-weight: 700; cursor: pointer;
}

/* ---------- 챕터 패널 (오른쪽, 화면 높이만큼, 내부 스크롤) ---------- */
.chapters {
  grid-area: chapters;
  position: relative; min-height: 0;
  background: var(--panel);
}
.chapters-inner {
  position: absolute; inset: 0; overflow: auto; padding: 8px 12px 16px; scroll-behavior: smooth;
}
.chapters h2 {
  font-size: 13px; font-weight: 600; color: var(--muted); margin: 8px 0 8px; padding: 0 10px;
  display: flex; justify-content: space-between; align-items: baseline;
}
.chapters .count { color: var(--muted); font-size: 12px; font-weight: 500; }
.chapters ol { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.ch {
  width: 100%; display: flex; gap: 12px; align-items: center; text-align: left;
  background: transparent; border: 0; color: var(--text); padding: 11px 12px; border-radius: 10px; cursor: pointer;
}
.ch:hover { background: rgba(255, 255, 255, 0.06); }
.ch:focus-visible { outline: 2px solid #fff; }
li.active .ch { background: var(--accent-soft); }
li.active .ch-idx { background: var(--accent); border-color: var(--accent); color: #fff; }
li.active .ch-start { color: #fff; font-weight: 700; }
.ch-idx {
  flex: 0 0 30px; height: 30px; border-radius: 50%; border: 1.5px solid #3a3a40; background: transparent;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: var(--muted);
}
.ch-body { flex: 1; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.ch-title { font-size: 14px; font-weight: 600; line-height: 1.35; overflow-wrap: anywhere; }
.ch-dur { font-size: 12px; color: var(--muted); }
.ch-start { flex: 0 0 auto; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- 단축키 안내 ---------- */
.help {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.72); z-index: 50;
  display: flex; align-items: center; justify-content: center;
}
.help[hidden] { display: none; }
.help-card {
  background: var(--panel); border-radius: 16px; padding: 22px 26px;
  max-width: 520px; width: calc(100% - 32px); max-height: 85vh; overflow: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
.help-card h3 { margin: 0 0 12px; font-size: 17px; }
.help-card table { width: 100%; border-collapse: collapse; font-size: 14px; }
.help-card td { padding: 7px 4px; border-bottom: 1px solid rgba(255, 255, 255, 0.06); vertical-align: middle; }
.help-card td:first-child { width: 46%; color: #ddd; }
.help-card kbd {
  background: #2a2a2e; border: 1px solid #47474d; border-bottom-width: 2px; border-radius: 6px;
  padding: 1px 7px; font-family: inherit; font-size: 12px;
}
.help-close { margin: 16px 0 0; text-align: right; }

/* ---------- 플레이어 폭에 따라 버튼 줄이기 (컨테이너 쿼리) ---------- */
.player { container-type: inline-size; }
@container (max-width: 640px) {
  #btnHelp, #btnPip { display: none; }
  .btn.text .label { font-size: 12px; }
}
@container (max-width: 540px) {
  #btnShare, #btnBack, #btnFwd { display: none; }
  .btn { width: 36px; height: 36px; }
  .time { margin: 0 4px; font-size: 12px; }
}
@container (max-width: 420px) {
  #btnQuality .label { display: none; }
  #btnChapters { display: none; }
}

/* ---------- 좁은 화면(1000px 이하): 제목 → 영상 → 챕터 목록 세로 배치, 페이지 스크롤 ---------- */
@media (max-width: 1000px) {
  html, body { height: auto; background: var(--bg); }
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    grid-template-areas: "top" "player" "chapters";
    height: auto; overflow: visible; background: var(--bg);
    gap: 12px; padding: 0 12px 20px;
  }
  .layout.no-chapters { grid-template-rows: auto auto; grid-template-areas: "top" "player"; }
  .layout.no-chapters .topbar { display: flex; }
  .topbar {
    flex-direction: row; align-items: baseline; justify-content: space-between; gap: 12px;
    padding: 12px 0 0; background: transparent; border: 0;
  }
  .player-col { display: block; background: transparent; }
  .player { border-radius: var(--radius); max-height: calc(100vh - 150px); }
  .now { display: flex; margin-top: 10px; color: var(--muted); font-size: 14px; gap: 8px; align-items: center; }
  .now strong { color: var(--text); font-weight: 600; }
  .chapters { border-radius: var(--radius); overflow: auto; max-height: 50vh; padding: 14px; }
  .chapters-inner { position: static; padding: 0; }
  .chapters h2 { margin: 0 0 10px; padding: 0 4px; font-size: 15px; color: var(--text); }
}
