{{define "title"}}Watch {{.Anime.Title}}{{end}}
{{define "scripts"}}<script type="module" src="{{assetURL "/dist/static/player/main.js"}}" defer></script>{{end}}
{{define "page_container"}}
<div class="-m-4 h-[calc(100dvh-4rem)] overflow-hidden p-4 md:-m-8 md:p-6">
  {{template "content" .}}
</div>
{{end}}
{{define "content"}}
{{if .WatchlistIDs}}<script type="application/json" id="watchlist-ids-json">{{.WatchlistIDs}}</script>{{end}}
{{$anime := .Anime}}
{{$episodes := .Episodes}}
{{$currentEpID := .CurrentEpID}}
{{if or (not $currentEpID) (eq (printf "%v" $currentEpID) "0") (eq (printf "%v" $currentEpID) "")}}{{$currentEpID = "1"}}{{end}}
{{$totalEpisodes := len $episodes}}
{{$currentEpNum := atoi $currentEpID}}
{{$rangeStart := episodeRangeStart $currentEpNum 100}}
{{$rangeEnd := min (add $rangeStart 99) $totalEpisodes}}

<div id="watch-layout" class="flex h-full flex-col gap-6 overflow-y-auto lg:grid lg:grid-cols-[minmax(0,min(calc(100%_-_24rem_-_3rem),calc((100dvh-10.5rem)*16/9)))_24rem] lg:items-start lg:gap-x-12 lg:gap-y-0 lg:overflow-hidden">
  <div class="min-w-0">
    <div id="video-player-container">
      {{template "video_player" dict "WatchData" .WatchData "TotalEpisodes" $totalEpisodes "Error" .Error}}
    </div>

    <div class="mt-3 flex flex-wrap items-center justify-between gap-3">
      <div class="flex gap-2">
        {{$prevEp := sub (int $currentEpID) 1}}
        {{if ge $prevEp 1}}
          <a href="/anime/{{$anime.MalID}}/watch?ep={{$prevEp}}" class=" inline-flex items-center gap-2 bg-background-button px-4 py-2 text-sm font-normal leading-none text-foreground-muted transition-colors hover:bg-background-button-hover hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
            <svg class="block h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" /></svg>
            Prev
          </a>
        {{end}}
        {{$nextEp := add (int $currentEpID) 1}}
        {{if le $nextEp $totalEpisodes}}
          <a href="/anime/{{$anime.MalID}}/watch?ep={{$nextEp}}" class=" inline-flex items-center gap-2 bg-background-button px-4 py-2 text-sm font-normal leading-none text-foreground-muted transition-colors hover:bg-background-button-hover hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
            Next
            <svg class="block h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7" /></svg>
          </a>
        {{end}}
      </div>

      <div class="flex justify-end gap-2" data-segment-editor-root>
        <ui-dropdown class="relative block" data-align="right" data-width="min-w-[160px]">
          <div data-trigger class="cursor-pointer">
            <button type="button" class=" inline-flex items-center justify-between gap-2 bg-background-button px-4 py-2 text-sm font-normal leading-none text-foreground-muted transition-colors hover:bg-background-button-hover hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
              <span id="watchlist-status-display-{{$anime.MalID}}">
                {{if .WatchlistStatus}}{{if eq .WatchlistStatus "watching"}}Watching{{else if eq .WatchlistStatus "completed"}}Completed{{else if eq .WatchlistStatus "plan_to_watch"}}Plan to Watch{{else if eq .WatchlistStatus "dropped"}}Dropped{{end}}{{else}}Add to Watchlist{{end}}
              </span>
              <svg class="h-4 w-4 text-foreground-muted" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="m6 9 6 6 6-6" /></svg>
            </button>
          </div>
          <div data-content class="hidden absolute z-50 right-0 bottom-full mb-2 min-w-40 bg-background-button shadow-card ring-1 ring-black/10">
            <div class="flex flex-col">
              <button
                data-unstyled-button
                data-watchlist-update
                data-mal-id="{{$anime.MalID}}"
                data-watchlist-status="watching"
                data-watchlist-display="Watching"
                data-watchlist-title="{{$anime.Title}}"
                class="flex w-full items-center px-5 py-2.5 text-left transition-colors hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent disabled:opacity-50"
              >
                <span class="text-sm font-normal text-foreground">Watching</span>
              </button>
              <button
                data-unstyled-button
                data-watchlist-update
                data-mal-id="{{$anime.MalID}}"
                data-watchlist-status="completed"
                data-watchlist-display="Completed"
                data-watchlist-title="{{$anime.Title}}"
                class="flex w-full items-center px-5 py-2.5 text-left transition-colors hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent disabled:opacity-50"
              >
                <span class="text-sm font-normal text-foreground">Completed</span>
              </button>
              <button
                data-unstyled-button
                data-watchlist-update
                data-mal-id="{{$anime.MalID}}"
                data-watchlist-status="plan_to_watch"
                data-watchlist-display="Plan to Watch"
                data-watchlist-title="{{$anime.Title}}"
                class="flex w-full items-center px-5 py-2.5 text-left transition-colors hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent disabled:opacity-50"
              >
                <span class="text-sm font-normal text-foreground">Plan to Watch</span>
              </button>
              <button
                data-unstyled-button
                data-watchlist-update
                data-mal-id="{{$anime.MalID}}"
                data-watchlist-status="dropped"
                data-watchlist-display="Dropped"
                data-watchlist-title="{{$anime.Title}}"
                class="flex w-full items-center px-5 py-2.5 text-left transition-colors hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent disabled:opacity-50"
              >
                <span class="text-sm font-normal text-foreground">Dropped</span>
              </button>
              {{template "watchlist_remove_button" dict
                "ID" $anime.MalID
                "Title" $anime.Title
                "ContainerClass" "hidden"
                "DividerClass" "my-1"
                "ButtonClass" "flex w-full items-center px-5 py-2.5 text-left transition-colors hover:bg-red-500/10 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent"
                "SpanClass" "text-sm font-normal text-red-400 whitespace-nowrap"
              }}
            </div>
          </div>
        </ui-dropdown>
        <ui-dropdown class="relative block" data-align="right" data-width="min-w-[220px]">
          <div data-trigger>
            <button type="button" class=" inline-flex items-center gap-2 bg-background-button px-4 py-2 text-sm font-normal leading-none text-foreground-muted transition-colors hover:bg-background-button-hover hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
              <svg class="block h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="1.5"/><circle cx="5" cy="12" r="1.5"/><circle cx="19" cy="12" r="1.5"/></svg>
              More
            </button>
          </div>
          <div data-content class="hidden absolute z-50 right-0 bottom-full mb-2 min-w-55 bg-background-button shadow-card ring-1 ring-black/10">
            <div class="flex flex-col">
              <button type="button" data-segment-editor-toggle class="flex w-full items-center px-4 py-2.5 text-left text-sm font-normal text-foreground-muted transition-colors hover:bg-surface-hover hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
                Add missing segment
              </button>
            </div>
          </div>
        </ui-dropdown>

      <div data-segment-editor class="hidden fixed inset-0 z-50 items-center justify-center bg-black/50 px-4" role="dialog" aria-modal="true" aria-labelledby="segment-editor-title">
        <div class="w-full max-w-130 max-h-[min(420px,60vh)] overflow-auto bg-background-button shadow-card ring-1 ring-black/10">
          <div class="flex items-start justify-between px-4 py-3">
            <span id="segment-editor-title" class="text-base font-medium tracking-[0.02em] text-foreground">Segment capture</span>
            <button type="button" data-segment-editor-close class="inline-flex h-8 w-8 items-center justify-center text-foreground/60 transition-opacity hover:text-foreground/80 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent" aria-label="Close segment capture modal">
              <svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8" aria-hidden="true">
                <path stroke-linecap="round" stroke-linejoin="round" d="M6 6l12 12M18 6 6 18" />
              </svg>
            </button>
          </div>

          <div class="px-4 py-4 flex flex-col gap-4">
            <div class="flex items-center gap-3">
              <label for="segment-type-trigger" class="text-xs text-foreground-muted w-12">Type</label>
              <input type="hidden" data-segment-type-value value="ed" />
              <ui-dropdown class="relative block flex-1" data-align="left" data-width="w-full">
                <div data-trigger>
                  <button id="segment-type-trigger" type="button" data-segment-type-trigger class="w-full flex items-center justify-between px-3 py-2 bg-background-button text-sm font-normal text-foreground transition-colors hover:bg-background-button-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
                    <span data-segment-type-label>Ending (ED)</span>
                    <svg class="h-4 w-4 text-foreground-muted" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="m6 9 6 6 6-6" /></svg>
                  </button>
                </div>
                <div data-content class="hidden absolute z-50 top-full mt-1 left-0 w-full bg-background-button shadow-card ring-1 ring-black/10">
                  <div class="flex flex-col">
                    <button type="button" data-segment-type-option data-value="ed" class="flex w-full items-center justify-between px-4 py-2.5 text-left transition-colors hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
                      <span class="text-sm font-normal text-foreground">Ending (ED)</span>
                    </button>
                    <button type="button" data-segment-type-option data-value="op" class="flex w-full items-center justify-between px-4 py-2.5 text-left transition-colors hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
                      <span class="text-sm font-normal text-foreground">Opening (OP)</span>
                    </button>
                  </div>
                </div>
              </ui-dropdown>
            </div>

            <div class="grid grid-cols-2 gap-3">
              <button type="button" data-segment-mark-start class=" px-3 py-2 bg-background-button text-sm font-normal text-foreground transition-colors hover:bg-background-button-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">Mark start</button>
              <button type="button" data-segment-mark-end class=" px-3 py-2 bg-background-button text-sm font-normal text-foreground transition-colors hover:bg-background-button-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">Mark end</button>
            </div>

            <div class="grid grid-cols-2 gap-3">
              <div class="flex items-center justify-between bg-black/5 px-3 py-2">
                <span class="text-xs text-foreground-muted">Start</span>
                <span data-segment-start class="text-sm tabular-nums text-foreground">—</span>
              </div>
              <div class="flex items-center justify-between bg-black/5 px-3 py-2">
                <span class="text-xs text-foreground-muted">End</span>
                <span data-segment-end class="text-sm tabular-nums text-foreground">—</span>
              </div>
            </div>

            <div class="flex items-center justify-between">
              <button type="button" data-segment-reset class="cursor-pointer px-4 py-2 bg-black/5 text-sm font-normal text-foreground-muted transition-colors hover:bg-black/10 hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">Reset</button>
              <button type="button" data-segment-save class="px-4 py-2 bg-accent text-sm font-normal text-white transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">Save</button>
            </div>

            <div data-segment-error class="hidden text-sm text-red-400"></div>
          </div>
        </div>
      </div>
      </div>
    </div>
  </div>

    <div class="w-full flex flex-col min-h-0{{if le $totalEpisodes 100}} flex-1 lg:h-full lg:overflow-y-auto lg:pb-4{{else}} shrink-0{{end}}">
      {{if .Seasons}}
        {{$currentSeason := dict "Prefix" "" "Title" $anime.Title}}
        {{range .Seasons}}
          {{if .IsCurrent}}{{$currentSeason = .}}{{end}}
        {{end}}
        <ui-dropdown class="relative block mb-4" data-align="left" data-width="w-full">
          <div data-trigger>
            <button class="flex h-10 w-full items-center justify-between bg-background-button px-3 text-sm font-normal text-foreground-muted transition-colors hover:bg-background-button-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
              <span class="truncate">
                {{if $currentSeason.Prefix}}<span class="font-normal text-foreground mr-1">{{$currentSeason.Prefix}}:</span>{{end}}
                {{$currentSeason.Title}}
              </span>
              <svg class="h-4 w-4 text-foreground-muted shrink-0 ml-2" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /></svg>
            </button>
          </div>
          <div data-content class="hidden absolute z-50 top-full mt-1 left-0 w-full bg-background-button shadow-card ring-1 ring-black/10 max-h-64 overflow-y-auto scrollbar-hidden lg:scrollbar-thin lg:[scrollbar-color:var(--scrollbar-thumb)_var(--scrollbar-track)] lg:[&::-webkit-scrollbar]:block lg:[&::-webkit-scrollbar]:h-2 lg:[&::-webkit-scrollbar-track]:bg-(--scrollbar-track) lg:[&::-webkit-scrollbar-track]:rounded-none lg:[&::-webkit-scrollbar-thumb]:bg-(--scrollbar-thumb) lg:[&::-webkit-scrollbar-thumb]:rounded-none lg:[&::-webkit-scrollbar-thumb:hover]:bg-(--scrollbar-thumb-hover)">
            <div class="flex flex-col">
              {{range .Seasons}}
                <a href="/anime/{{.MalID}}/watch" class="px-4 py-2 text-left text-sm {{if .IsCurrent}}text-accent bg-accent/10{{else}}text-foreground-muted hover:bg-surface-hover{{end}} transition-colors">
                  <span class="font-normal text-foreground mr-1">{{.Prefix}}:</span>
                  {{.Title}}
                </a>
              {{end}}
            </div>
          </div>
        </ui-dropdown>
      {{end}}

      {{if eq $totalEpisodes 0}}
      <div class="flex flex-col items-center justify-center gap-2 py-12 text-foreground-muted">
        <svg class="h-10 w-10 opacity-30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5"><path stroke-linecap="round" stroke-linejoin="round" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z" /></svg>
        <p class="text-sm">No episodes found</p>
      </div>
    {{else}}
      <div class="flex flex-col gap-2 shrink-0">
        <div class="flex items-center justify-between">
          <span class="text-sm font-normal text-foreground-muted">Episodes</span>
          {{if gt $totalEpisodes 100}}
            <span class="sr-only">Episode controls</span>
          {{end}}
        </div>

        {{if gt $totalEpisodes 100}}
          <div class="flex items-center gap-2">
            <ui-dropdown class="relative block grow min-w-0" data-align="left" data-width="min-w-[200px]" data-episode-dropdown>
              <div data-trigger>
                <button class="flex h-10 w-full items-center justify-between bg-background-button px-3 text-sm font-normal text-foreground-muted hover:bg-background-button-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent">
                  <span data-dropdown-label>{{printf "%02d" $rangeStart}}-{{printf "%02d" $rangeEnd}}</span>
                  <svg class="h-4 w-4 text-foreground-muted shrink-0 ml-2" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7" /></svg>
                </button>
              </div>
              <div data-content class="hidden absolute z-50 top-full mt-1 left-0 min-w-50 bg-background-button shadow-card ring-1 ring-black/10">
                <div class="flex flex-col">
                  {{$ranges := ceilDiv $totalEpisodes 100}}
                  {{range $i := seq $ranges}}
                    {{$start := imul $i 100}}
                    {{$end := min (add $start 100) $totalEpisodes}}
                    <button class="episode-range-btn px-4 py-2 text-left text-sm font-normal text-foreground-muted hover:bg-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-accent" data-range-index="{{$i}}" data-range-start="{{add $start 1}}" data-range-end="{{$end}}">
                      {{printf "%02d" (add $start 1)}}-{{printf "%02d" $end}}
                    </button>
                  {{end}}
                </div>
              </div>
            </ui-dropdown>

            <input id="episode-search" type="text" name="episode-search" placeholder="Find" data-episode-search class="h-10 w-24 bg-background-button px-3 text-sm text-foreground placeholder-foreground-muted outline-none focus:ring-1 focus:ring-accent" />
          </div>
        {{end}}
      </div>

      {{if gt $totalEpisodes 100}}
        <div class="grid grid-cols-5 gap-1 mt-2" data-episode-grid>
          {{range $episodes}}
            {{$isCurrent := eq (printf "%v" .Number) $currentEpID}}
            {{$isFiller := .Filler}}
            {{$isRecap := .Recap}}
            <a href="/anime/{{$anime.MalID}}/watch?ep={{.Number}}" class="relative flex items-center justify-center py-2 text-xs transition-colors {{if or (lt .Number $rangeStart) (gt .Number $rangeEnd)}}hidden{{end}} {{if $isFiller}}bg-yellow-500/20 text-yellow-400{{else if $isRecap}}bg-blue-500/20 text-blue-400{{else}}text-foreground-muted hover:bg-foreground/5{{end}} {{if $isCurrent}}bg-accent/15 text-accent ring-1 ring-accent{{end}}" data-episode-id="{{.Number}}" data-episode-index="{{.Number}}" data-episode-title="{{.Title}}" data-has-sub="{{.HasSub}}" data-has-dub="{{.HasDub}}" data-sub-only="{{.SubOnly}}">
              {{.Number}}
              {{if .SubOnly}}<span class="absolute right-0.5 top-0.5 text-[8px] font-semibold uppercase text-accent">Sub</span>{{end}}
            </a>
          {{end}}
        </div>
      {{else}}
        <div class="mt-2 flex min-h-0 flex-1 flex-col gap-1 overflow-y-auto overscroll-contain pb-4 scrollbar-hidden" data-episode-list>
          {{range $episodes}}
            {{$isCurrent := eq (printf "%v" .Number) $currentEpID}}
            {{$isFiller := .Filler}}
            {{$isRecap := .Recap}}
            <a href="/anime/{{$anime.MalID}}/watch?ep={{.Number}}" class="relative flex items-center gap-3 px-3 py-2 transition-colors hover:bg-foreground/5 text-left {{if $isCurrent}}bg-accent/20{{end}}" data-episode-id="{{.Number}}" data-episode-title="{{.Title}}" data-has-sub="{{.HasSub}}" data-has-dub="{{.HasDub}}" data-sub-only="{{.SubOnly}}">
              {{if or $isFiller $isRecap}}
                <span aria-hidden="true" class="absolute inset-y-1 left-0 w-0.5 {{if $isFiller}}bg-yellow-400{{else}}bg-blue-400{{end}}"></span>
              {{end}}
              <span class="w-10 shrink-0 text-xs font-normal text-foreground-muted tabular-nums">EP{{.Number}}</span>
              <span class="truncate text-sm {{if $isFiller}}text-yellow-400{{else if $isRecap}}text-blue-400{{else}}text-foreground-muted{{end}}" data-episode-title>{{.Title}}</span>
              {{if .SubOnly}}<span class="ml-auto shrink-0 text-[10px] font-semibold uppercase tracking-wide text-accent">Sub only</span>{{end}}
            </a>
          {{end}}
        </div>
{{end}}
{{end}}

  </div>
</div>
{{end}}
