/* Custom layout and components on top of Pico.css */

/* Scale down Pico's default sizing */
html {
  font-size: 16px;
}

/* Remove Pico's default bottom margin on button types */
[type="button"],
[type="reset"],
[type="submit"] {
  margin-bottom: 0;
}

/* Layout */
body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  padding: 1rem 2rem;
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: var(--pico-card-background-color);
  border-bottom: 1px solid var(--pico-muted-border-color);
  display: flex;
  align-items: center;
  gap: 2rem;
}

header h1 {
  margin: 0;
  flex-shrink: 0;
  font-size: 1.5rem;
}

/* Brand: logo + wordmark, no longer a link */
.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.brand-logo {
  display: block;
  flex-shrink: 0;
}

nav {
  display: flex;
  gap: 1rem;
}

nav a {
  color: var(--pico-muted-color);
  text-decoration: none;
  padding: 0.5rem 1rem;
  border-radius: var(--pico-border-radius);
  transition: all 0.2s;
}

nav a:hover {
  color: var(--pico-primary);
  background-color: var(--pico-card-sectioning-background-color);
}

nav a.active {
  background-color: var(--pico-primary);
  color: var(--pico-primary-inverse);
  font-weight: 600;
}

main {
  flex: 1;
  padding: 2rem 1rem;
}

main > * {
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
}

footer {
  padding: 1rem 2rem;
  text-align: center;
  color: var(--pico-muted-color);
  font-size: 0.9rem;
  background-color: var(--pico-card-background-color);
  border-top: 1px solid var(--pico-muted-border-color);
}

/* Page headers */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.page-header h2 {
  font-size: 1.5rem;
  margin: 0;
}

.actions {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}

/* Forms */
.form-group {
  margin-bottom: 1rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.radio-group label {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
  font-weight: normal;
  margin-bottom: 0;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.file-selector {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.file-selector label {
  margin: 0;
}

.file-selector select {
  flex: 1;
  min-width: 0;
  margin: 0;
}

.file-selector button {
  margin: 0;
  width: auto;
  flex: none;
}

/* Buttons */
button.small,
[role="button"].small {
  padding: 0.3rem 0.6rem;
  font-size: 0.85rem;
}

button.danger,
button.danger:hover,
button.danger:focus,
[role="button"].danger,
[role="button"].danger:hover,
[role="button"].danger:focus {
  --pico-background-color: #c62828;
  --pico-border-color: #c62828;
  --pico-color: white;
}

button.danger:hover,
button.danger:focus,
[role="button"].danger:hover,
[role="button"].danger:focus {
  --pico-background-color: #b71c1c;
  --pico-border-color: #b71c1c;
}

/* One-shot feedback banner shown after a form action (see PageData.Flash). */
.flash {
  padding: 0.75rem 1rem;
  margin-bottom: 1.5rem;
  border-radius: var(--pico-border-radius);
  border: 1px solid transparent;
  border-left-width: 4px;
}

.flash-success {
  background-color: rgba(46, 125, 50, 0.12);
  border-color: #2e7d32;
  color: var(--pico-color);
}

.flash-error {
  background-color: rgba(198, 40, 40, 0.12);
  border-color: #c62828;
  color: var(--pico-color);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Library */
.breadcrumbs {
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.35rem;
  flex-wrap: wrap;
  text-align: left;
  width: 100%;
}

.breadcrumbs a,
.breadcrumbs span {
  flex-shrink: 0;
}

.breadcrumbs .current {
  cursor: default;
  pointer-events: none;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.folder-item,
.media-item {
  background: var(--pico-card-background-color);
  border: 1px solid var(--pico-card-border-color);
  border-radius: var(--pico-border-radius);
  overflow: hidden;
  text-decoration: none;
  color: var(--pico-color);
  box-shadow: var(--pico-card-box-shadow);
}

.folder-item:hover,
.media-item:hover {
  background-color: var(--pico-primary-background);
  border-color: var(--pico-primary-border);
  box-shadow: var(--pico-card-box-shadow), 0 4px 12px rgba(0, 0, 0, 0.15);
}

.folder-item:hover .folder-name,
.media-item:hover .media-info,
.media-item:hover .media-title,
.media-item:hover .media-path {
  color: var(--pico-primary-inverse);
}

.media-info {
  transition: color 0.2s;
}

.media-thumbnail {
  aspect-ratio: 16/9;
  background: var(--pico-card-sectioning-background-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  position: relative;
}

/* Each thumbnail sits in a tile so it can carry its own per-file duration
   badge; the tile fills the container (single file) or a grid cell (multi). */
.thumb-tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

.thumb-tile .media-duration {
  position: absolute;
  bottom: 0.4rem;
  right: 0.4rem;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 0.15rem 0.4rem;
  border-radius: var(--pico-border-radius);
  font-size: 0.7rem;
  font-weight: 500;
  line-height: 1;
}

.folder-icon {
  aspect-ratio: 16/9;
  background: var(--pico-card-sectioning-background-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
}

.thumb-tile img {
  width: 100%;
  height: 100%;
  /* contain: show the whole thumbnail (letterboxed) and never overflow the
     16/9 box, including portrait/vertical sources. */
  object-fit: contain;
}

.thumb-tile img[src$=".svg"] {
  width: 48px;
  height: 48px;
  opacity: 0.5;
  object-fit: contain;
}

/* Multi-file items: tile each media file's thumbnail in a grid. */
.media-thumbnail.count-2,
.media-thumbnail.count-3,
.media-thumbnail.count-many {
  display: grid;
  gap: 2px;
}

.media-thumbnail.count-2 {
  grid-template-columns: 1fr 1fr;
}

.media-thumbnail.count-3,
.media-thumbnail.count-many {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

/* For 3 files, let the first thumbnail span the full top row. */
.media-thumbnail.count-3 .thumb-tile:first-of-type {
  grid-column: 1 / -1;
}

/* "+N" overlay on the last tile when there are more than 4 files. */
.media-thumbnail .thumb-more {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 50%;
  height: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 600;
  pointer-events: none;
}

/* File-count badge in the top-left for multi-file items. */
.media-thumbnail .thumb-count {
  position: absolute;
  top: 0.4rem;
  left: 0.4rem;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 0.15rem 0.4rem;
  border-radius: var(--pico-border-radius);
  font-size: 0.7rem;
  font-weight: 500;
  line-height: 1;
}

.folder-name,
.media-info {
  padding: 0.75rem;
}

.media-title {
  font-weight: 500;
  margin-bottom: 0.3rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.media-path {
  font-size: 0.85rem;
}

.media-info .media-duration {
  display: none;
}

/* Detail view */
.detail-view {
  max-width: 900px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.player-container {
  background: #000;
  border-radius: var(--pico-border-radius);
  overflow: hidden;
  margin-bottom: 1.5rem;
  max-height: 80vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.player-container video,
.player-container audio {
  max-width: 100%;
  max-height: 80vh;
  width: auto;
  height: auto;
  display: block;
}

.detail-meta dl {
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 0.5rem 1rem;
  align-items: baseline;
}

/* Render keys as understated labels so they read as keys — not just another
   line of value text — in both the two-column and the stacked (narrow) layout. */
.detail-meta dt {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--pico-muted-color);
}

.detail-meta dd {
  word-break: break-all;
  margin: 0;
}

/* Description: preserve the source line breaks; collapse long ones to ~2 lines
   with a pure-CSS (no-JS, CSP-safe) Show more/less toggle. */
.detail-meta dd.description {
  margin: 0;
}

.description .desc-text {
  white-space: pre-wrap;
  word-break: normal;
  overflow-wrap: anywhere;
  line-height: 1.5;
}

/* The checkbox is the toggle state; it's driven by the label, never shown. */
.description.collapsible .desc-toggle {
  display: none;
}

/* Collapsed: clamp to ~2 lines (max-height works with pre-wrap; line-clamp
   does not), with a fade so the cut reads as intentional. */
.description.collapsible .desc-text {
  position: relative;
  max-height: 3em;
  overflow: hidden;
}

.description.collapsible .desc-text::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 1.5em;
  background: linear-gradient(to bottom, transparent, var(--pico-background-color));
  pointer-events: none;
}

.description.collapsible .desc-toggle:checked ~ .desc-text {
  max-height: none;
}

.description.collapsible .desc-toggle:checked ~ .desc-text::after {
  display: none;
}

.description.collapsible .desc-toggle-label {
  display: inline-block;
  margin-top: 0.4rem;
  color: var(--pico-primary);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.description.collapsible .desc-toggle-label::after {
  content: "Show more \25be";
}

.description.collapsible .desc-toggle:checked ~ .desc-toggle-label::after {
  content: "Show less \25b4";
}

/* Queue */
.queue-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.queue-item {
  padding: 1rem;
  margin-bottom: 0;
  position: relative;
  transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

/* Stretched link: the whole row navigates to the detail page, matching the
   library card hover. Interactive children (Remove) sit above it via z-index. */
.queue-link {
  position: absolute;
  inset: 0;
  z-index: 1;
}

.queue-item:hover {
  background-color: var(--pico-primary-background);
  border-color: var(--pico-primary-border);
  box-shadow: var(--pico-card-box-shadow), 0 4px 12px rgba(0, 0, 0, 0.15);
  cursor: pointer;
}

/* Keep text legible on the solid primary hover background (status badges and
   the error message keep their own colors). */
.queue-item:hover .queue-url,
.queue-item:hover .queue-meta {
  color: var(--pico-primary-inverse);
}

.queue-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}

.queue-url {
  flex: 1;
  min-width: 0;
  word-break: break-all;
}

.queue-status {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

/* Keep the Remove action clickable above the stretched link overlay. */
.queue-action {
  position: relative;
  z-index: 2;
}

.status-badge {
  padding: 0.2rem 0.6rem;
  border-radius: 12px;
  font-size: 0.85rem;
  font-weight: 500;
}

.status-badge.queued {
  background: var(--pico-card-sectioning-background-color);
  color: var(--pico-muted-color);
}

.status-badge.downloading {
  background: var(--pico-primary);
  color: var(--pico-primary-inverse);
}

.status-badge.completed {
  background: var(--pico-ins-color);
  color: white;
}

.status-badge.error {
  background: var(--pico-del-color);
  color: white;
}

.error-message {
  color: var(--pico-del-color);
  font-size: 0.9rem;
  margin-top: 0.5rem;
}

.error-text {
  color: var(--pico-del-color);
}

.queue-meta {
  margin-top: 0.5rem;
  font-size: 0.85rem;
  display: flex;
  gap: 1rem;
}

/* Logs */
.logs {
  max-height: 500px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

pre {
  padding: 1.5rem;
}

/* Settings */
.settings-layout {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.presets-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.preset-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.preset-header h4 {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
}

.badge {
  background: var(--pico-primary);
  color: var(--pico-primary-inverse);
  padding: 0.1rem 0.4rem;
  border-radius: 10px;
  font-size: 0.75rem;
}

.preset-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 1rem;
}

.preset-description {
  margin-bottom: 0.5rem;
}

.preset-flags {
  margin-top: 0.5rem;
}

.preset-flags pre {
  overflow-x: auto;
  margin-top: 0.3rem;
  margin-bottom: 0;
}

/* Nested articles need visible borders */
.presets-list article {
  border: 1px solid var(--pico-muted-border-color);
}

/* Suppress Pico details marker for button-like summaries */
details:has(summary[role="button"]) summary::after {
  display: none !important;
}

details summary[role="button"] {
  display: inline-flex;
  width: auto;
}

/* Toggle text: Edit vs Cancel */
details .cancel-text {
  display: none;
}

details[open] .edit-text {
  display: none;
}

details[open] .cancel-text {
  display: inline;
}

/* Delete overlay popover */
.delete-overlay {
  position: relative;
}

.delete-overlay summary {
  list-style: none;
  cursor: pointer;
}

.delete-overlay summary::-webkit-details-marker {
  display: none;
}

.delete-overlay summary::after {
  display: none !important;
}

.delete-overlay .overlay-panel {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  z-index: 1000;
  width: 260px;
  background: var(--pico-background-color);
  border: 1px solid var(--pico-muted-border-color);
  border-radius: var(--pico-border-radius);
  padding: 1rem;
  box-shadow: var(--pico-dropdown-box-shadow);
}

/* Popover arrow */
.delete-overlay .overlay-panel::before {
  content: '';
  position: absolute;
  top: -6px;
  right: 1rem;
  width: 10px;
  height: 10px;
  background: var(--pico-background-color);
  border-left: 1px solid var(--pico-muted-border-color);
  border-top: 1px solid var(--pico-muted-border-color);
  transform: rotate(45deg);
}

.delete-overlay .overlay-panel p {
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.delete-overlay .overlay-panel .form-actions {
  margin-top: 0;
  justify-content: flex-end;
}

/* Inline edit form */
.edit-form {
  padding: 1rem;
}

/* Conditional sections (no-JS show/hide) */
.conditional-section {
  margin-bottom: 1rem;
}

.conditional-section .conditional-content {
  display: none;
  margin-top: 1rem;
}

/* Checkbox triggers */
.conditional-section:has(input[type="checkbox"].conditional-trigger:checked) .conditional-content {
  display: block;
}

/* Radio triggers for format mode */
.conditional-section:has(.radio-group input[value="preset"]:checked) .conditional-content.format-preset,
.conditional-section:has(.radio-group input[value="custom"]:checked) .conditional-content.format-custom {
  display: block;
}

/* Subscriptions: lock the cron field unless the schedule is "Custom (cron)".
   Pure CSS — the :has() reacts live as the <select> changes, no JS needed. */
input[name="cron_expr"] {
  opacity: 0.5;
  pointer-events: none;
}
.form-row:has(select[name="schedule_kind"] option[value="cron"]:checked) input[name="cron_expr"] {
  opacity: 1;
  pointer-events: auto;
}

/* Format mode selector - hidden radio inputs with button-like labels */
.radio-group.format-mode {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

.radio-group.format-mode input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.radio-group.format-mode label {
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--pico-muted-border-color);
  border-radius: var(--pico-border-radius);
  cursor: pointer;
  background: var(--pico-background-color);
  transition: all 0.2s;
  font-weight: normal;
}

.radio-group.format-mode label:hover {
  background: var(--pico-card-sectioning-background-color);
}

.radio-group.format-mode label:has(input:checked) {
  background: var(--pico-primary);
  color: var(--pico-primary-inverse);
  border-color: var(--pico-primary);
}

/* Inline informational note paragraph */
.note {
  font-size: 0.85rem;
  color: var(--pico-muted-color);
}

/* Collapsible "managed yt-dlp options" notes component */
.ytdlp-notes {
  margin: 0 0 1rem;
  border: 1px solid var(--pico-muted-border-color);
  border-radius: var(--pico-border-radius);
  background: var(--pico-card-sectioning-background-color);
  overflow: hidden;
}

.ytdlp-notes > summary {
  cursor: pointer;
  padding: 0.6rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--pico-color);
  user-select: none;
  transition: background 0.15s ease-in-out;
}

.ytdlp-notes > summary:hover {
  background: var(--pico-card-background-color);
}

.ytdlp-notes-icon {
  color: var(--pico-primary);
  font-style: normal;
  font-size: 1rem;
  margin-right: 0.4rem;
}

.ytdlp-notes-body {
  padding: 0 0.9rem 0.6rem;
  border-top: 1px solid var(--pico-muted-border-color);
}

.ytdlp-notes-body > p {
  font-size: 0.85rem;
  color: var(--pico-muted-color);
  margin: 0.7rem 0;
}

.ytdlp-notes-table-wrap {
  overflow-x: auto;
}

.ytdlp-notes-body table {
  margin: 0;
  font-size: 0.85rem;
}

.ytdlp-notes-body th,
.ytdlp-notes-body td {
  padding: 0.4rem 0.6rem;
  vertical-align: top;
}

.ytdlp-notes-body td:first-child {
  white-space: nowrap;
}

.ytdlp-notes-body code {
  font-size: 0.8rem;
}

/* Theme selector */
.theme-selector {
  display: flex;
  gap: 0.5rem;
}

.theme-selector button {
  min-width: 80px;
}

.theme-selector button.active {
  background: var(--pico-primary);
  color: var(--pico-primary-inverse);
  border-color: var(--pico-primary);
}

/* Download form */
.download-form {
  max-width: 900px;
}

.download-form form {
  max-width: 800px;
}

/* Filter form inline */
.filter-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.filter-form input,
.filter-form select,
.filter-form button {
  margin-bottom: 0;
  width: auto;
  display: inline-block;
  flex-shrink: 0;
}

.filter-form input[type="text"] {
  min-width: 200px;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 3rem;
}

/* Utility */
.inline {
  display: inline;
}

/* Responsive */
@media (max-width: 768px) {
  main {
    padding: 1rem;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .library-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .detail-meta dl {
    grid-template-columns: 1fr;
    gap: 0;
  }

  /* Stacked layout: tighten key→value and space out each pair so the grouping
     is obvious without the column alignment to lean on. */
  .detail-meta dt {
    margin-top: 0.85rem;
  }

  .detail-meta dt:first-child {
    margin-top: 0;
  }

  .detail-meta dd {
    margin-top: 0.15rem;
  }

  .queue-header {
    flex-direction: column;
  }
  
  header {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }
  
  nav {
    width: 100%;
    justify-content: center;
  }
}
