/* JukeBox - dark, glassy, mobile-first video gallery.
   Tuned for phone browsers first; widens to a two-column layout on desktop. */

:root {
  --bg: #07070d;
  --bg-2: #0d0d18;
  --surface: #14141f;
  --surface-2: #1c1c2b;
  --line: #262636;
  --text: #f3f3fb;
  --muted: #9a9ab2;
  --accent: #7c5cff;
  --accent-2: #18d3c8;
  --accent-grad: linear-gradient(135deg, #7c5cff 0%, #18d3c8 100%);
  --danger: #ff5c7c;

  --radius: 16px;
  --radius-sm: 11px;
  --gap: 14px;

  --dur-fast: 140ms;
  --dur: 240ms;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);

  /* Player sizing. The stage adopts each video's aspect ratio (--video-ar is
     set from JS on loadedmetadata): landscape clips fill the width up to
     --stage-max-h; portrait clips stand up tall (bound by --stage-portrait-h)
     so burned-in text stays legible instead of shrinking into a letterbox. */
  --stage-max-h: min(46vh, 420px);
  --stage-portrait-h: min(58vh, 520px);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win, even over class rules that set
   display (e.g. .eq { display: flex }). The front-end toggles visibility via
   element.hidden, so this guard keeps those toggles authoritative. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Noto Sans JP", Meiryo, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(120% 60% at 50% -10%, #1a1330 0%, transparent 60%),
    var(--bg);
}

/* ------------------------------------------------------------------ Player */
.player {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: calc(var(--safe-top) + 8px) 14px 12px;
  background: linear-gradient(180deg, rgba(10, 10, 20, 0.96) 0%, rgba(10, 10, 20, 0.82) 100%);
  backdrop-filter: blur(18px) saturate(140%);
  -webkit-backdrop-filter: blur(18px) saturate(140%);
  border-bottom: 1px solid var(--line);
}

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: var(--video-ar, 16 / 9);
  max-height: var(--stage-max-h);
  margin-inline: auto;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  box-shadow: 0 18px 50px -20px rgba(0, 0, 0, 0.9),
    inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  display: grid;
  place-items: center;
}

/* Portrait clip: stand the stage up and let its width follow the video, so the
   frame (and any burned-in text) shows large and centered instead of being
   letterboxed small inside a wide box. */
.stage.is-portrait {
  width: auto;
  max-width: 100%;
  height: var(--stage-portrait-h);
  max-height: none;
}

#video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  display: block;
}

.stage-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
  text-align: center;
  padding: 20px;
  background:
    radial-gradient(80% 80% at 50% 30%, #1a1530 0%, #07070d 75%);
}
.stage-empty__logo { font-size: 52px; filter: drop-shadow(0 8px 18px rgba(124, 92, 255, 0.5)); }
.stage-empty__title {
  margin: 4px 0 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 0.5px;
  background: var(--accent-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.stage-empty__hint { margin: 0; color: var(--muted); font-size: 13px; }
.stage--playing .stage-empty { display: none; }

.tap-play {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 76px;
  height: 76px;
  border: none;
  border-radius: 50%;
  background: rgba(12, 12, 22, 0.55);
  backdrop-filter: blur(6px);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.18), 0 10px 30px rgba(0, 0, 0, 0.5);
  transition: transform var(--dur) var(--ease), opacity var(--dur) var(--ease);
}
.tap-play svg { width: 38px; height: 38px; fill: #fff; margin-left: 4px; }
.tap-play:active { transform: scale(0.92); }

/* Fullscreen toggle (overlay, top-right of the stage). Hidden until a clip is
   loaded; in fullscreen it grows and shows the "exit" glyph so it's easy to
   tap to come back. */
.fs-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  display: none;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 12px;
  background: rgba(12, 12, 22, 0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  color: #fff;
  place-items: center;
  cursor: pointer;
  opacity: 0.85;
  z-index: 3;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.16), 0 6px 16px rgba(0, 0, 0, 0.4);
  transition: transform var(--dur-fast) var(--ease), background var(--dur) var(--ease),
    opacity var(--dur) var(--ease);
}
.stage--playing .fs-btn { display: grid; }
.fs-btn svg { width: 22px; height: 22px; }
.fs-btn:hover { background: rgba(22, 22, 38, 0.8); opacity: 1; }
.fs-btn:active { transform: scale(0.9); }

/* Make the stage fill the screen in fullscreen (its normal aspect-ratio sizing
   would otherwise shrink the video into a small centered box). */
.stage:fullscreen,
.stage:-webkit-full-screen {
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  aspect-ratio: auto;
  border-radius: 0;
}
.stage:fullscreen #video,
.stage:-webkit-full-screen #video { width: 100%; height: 100%; }
.stage:fullscreen .fs-btn,
.stage:-webkit-full-screen .fs-btn {
  top: max(16px, var(--safe-top));
  right: 16px;
  width: 48px;
  height: 48px;
  opacity: 1;
}

.nowbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 2px 10px;
}
.nowtitle {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  font-weight: 650;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 20px;
  color: var(--text);
}
.nowtitle:empty::before { content: "—"; color: var(--muted); }

/* Collapse the player to a compact control strip so the library can take the
   screen for browsing while a clip keeps playing. */
.collapse-btn {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform var(--dur-fast) var(--ease), background var(--dur) var(--ease),
    color var(--dur) var(--ease);
}
.collapse-btn:hover { background: var(--surface-2); color: var(--text); }
.collapse-btn:active { transform: scale(0.9); }
.collapse-btn svg { width: 20px; height: 20px; transition: transform var(--dur) var(--ease); }
.player-collapsed #collapseBtn svg { transform: rotate(180deg); }

/* Blackout / sleep: opaque black layer over everything that swallows taps so a
   pocketed phone can't hit controls; audio keeps playing. Pure #000 also lets
   OLED screens save power. Double-tap wakes (see app.js). */
.blackout {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #000;
  display: grid;
  place-items: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.blackout__hint {
  margin: 0;
  color: rgba(255, 255, 255, 0.5);
  font-size: 14px;
  letter-spacing: 0.04em;
  opacity: 0;
  transition: opacity 600ms var(--ease);
  pointer-events: none;
}
.blackout.show-hint .blackout__hint { opacity: 1; }
body.is-blackout { overflow: hidden; }

/* Always-visible (but dim) "back" button so there's a clear way out even after
   the hint fades — the dark stays restful but you can always find the exit. */
.blackout__exit {
  position: absolute;
  top: max(16px, var(--safe-top));
  right: 16px;
  width: 46px;
  height: 46px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.7);
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 0.5;
  -webkit-tap-highlight-color: transparent;
  transition: opacity var(--dur) var(--ease);
}
.blackout__exit svg { width: 22px; height: 22px; }
.blackout__exit:active { opacity: 1; }

/* Seek */
.seek {
  display: flex;
  align-items: center;
  gap: 10px;
}
.seek__time {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--muted);
  min-width: 38px;
  text-align: center;
}

/* Range inputs (seek + volume share the look) */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 30px;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 999px;
  background: var(--track, linear-gradient(90deg, var(--accent) var(--fill, 0%), var(--surface-2) var(--fill, 0%)));
}
input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
}
input[type="range"]::-moz-range-progress {
  height: 6px;
  border-radius: 999px;
  background: var(--accent);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -5px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  transition: transform var(--dur-fast) var(--ease);
}
input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: none;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}
input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.25); }

/* Controls */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin: 6px 0 4px;
}
.ctl {
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  position: relative;
  transition: transform var(--dur-fast) var(--ease), background var(--dur) var(--ease),
    color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.ctl svg { width: 24px; height: 24px; fill: currentColor; }
.ctl:active { transform: scale(0.9); }
.ctl:hover { background: var(--surface-2); }
.ctl--play {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  background: var(--accent-grad);
  color: #fff;
  box-shadow: 0 10px 26px -8px rgba(124, 92, 255, 0.7);
}
.ctl--play svg { width: 30px; height: 30px; }
.ctl--play:hover { background: var(--accent-grad); filter: brightness(1.06); }
.ctl--ghost { background: transparent; }
.ctl--ghost:hover { background: var(--surface); }

/* active toggle state for shuffle / repeat */
.ctl[data-on="true"],
.ctl[data-mode="all"],
.ctl[data-mode="one"] {
  color: var(--accent-2);
  background: color-mix(in srgb, var(--accent-2) 16%, var(--surface));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent-2) 45%, transparent);
}
.repeat-one {
  position: absolute;
  right: 6px;
  bottom: 5px;
  font-size: 10px;
  font-weight: 800;
  color: var(--accent-2);
}

/* Volume */
.volume {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}
.volume .ctl { width: 40px; height: 40px; }
.volume .ctl svg { width: 22px; height: 22px; }
.volume__bar { flex: 1; }
.muted #volWaves { display: none; }
.muted #volIcon { color: var(--danger); }

/* ----------------------------------------------------------------- Library */
.library {
  padding: 16px 14px calc(40px + var(--safe-bottom));
  max-width: 1100px;
  margin: 0 auto;
}
.library__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.library__title {
  margin: 0;
  font-size: 17px;
  font-weight: 750;
  display: flex;
  align-items: center;
  gap: 9px;
}
.badge {
  font-size: 12px;
  font-weight: 700;
  color: var(--bg);
  background: var(--accent-grad);
  padding: 2px 9px;
  border-radius: 999px;
}
.library__actions { display: flex; gap: 8px; }
.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 999px;
  cursor: pointer;
  transition: background var(--dur) var(--ease), transform var(--dur-fast) var(--ease);
}
.chip:hover { background: var(--surface-2); }
.chip:active { transform: scale(0.94); }

.search { margin-bottom: 14px; }
.search input {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0 14px;
  font-size: 15px;
  outline: none;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(124, 92, 255, 0.18);
}
.search input::placeholder { color: var(--muted); }

/* Video grid */
.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--gap);
}

.card {
  position: relative;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  transition: transform var(--dur) var(--ease), border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease);
  will-change: transform;
}
.card:hover { transform: translateY(-3px); border-color: #38384e; }
.card:active { transform: translateY(0) scale(0.98); }

.card__thumb {
  position: relative;
  aspect-ratio: 16 / 10;
  background: linear-gradient(135deg, #16162400 0%, #1d1d2e 100%), #101019;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.card__thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity var(--dur) var(--ease);
}
.card__thumb img.loaded { opacity: 1; }
.card__thumb .placeholder {
  position: absolute;
  font-size: 26px;
  opacity: 0.5;
}
.card__dur {
  position: absolute;
  right: 6px;
  bottom: 6px;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #fff;
  background: rgba(0, 0, 0, 0.66);
  padding: 2px 6px;
  border-radius: 6px;
}
.card__name {
  padding: 9px 10px 11px;
  font-size: 13px;
  line-height: 1.35;
  font-weight: 550;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* now-playing card */
.card.is-playing {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 14px 34px -16px rgba(124, 92, 255, 0.8);
}
.card.is-playing .card__name { color: var(--accent-2); font-weight: 700; }

/* animated equalizer badge on the playing card */
.eq {
  position: absolute;
  left: 6px;
  top: 6px;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 16px;
  padding: 4px 5px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.55);
}
.eq i {
  width: 3px;
  height: 100%;
  background: var(--accent-2);
  border-radius: 2px;
  transform-origin: bottom;
  animation: eq 0.9s var(--ease) infinite;
}
.eq i:nth-child(2) { animation-delay: 0.2s; }
.eq i:nth-child(3) { animation-delay: 0.4s; }
.card.is-paused .eq i { animation-play-state: paused; }
@keyframes eq {
  0%, 100% { transform: scaleY(0.35); }
  50% { transform: scaleY(1); }
}

.empty-list {
  text-align: center;
  color: var(--muted);
  padding: 40px 0;
  font-size: 14px;
}

/* Collapsed player (mobile only): tuck the video away, keep the control strip,
   and let the library fill the screen. The clip keeps playing — the stage stays
   in the render tree at 0 height rather than display:none, so audio continues. */
@media (max-width: 899px) {
  body.player-collapsed .stage {
    height: 0;
    min-height: 0;
    max-height: 0;
    aspect-ratio: auto;
    margin: 0;
    border: none;
    box-shadow: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* Desktop: side-by-side player + library */
@media (min-width: 900px) {
  :root {
    --stage-max-h: min(68vh, 620px);
    --stage-portrait-h: min(86vh, 880px);
  }
  body { display: grid; grid-template-columns: minmax(420px, 1fr) minmax(360px, 460px); }
  .player {
    position: sticky;
    top: 0;
    align-self: start;
    height: 100dvh;
    border-bottom: none;
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .library { height: 100dvh; overflow-y: auto; max-width: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
  /* Both columns are already visible side by side, so collapsing is moot. */
  .collapse-btn { display: none; }
}
