/* 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;
}

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;
}

/* 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;
}

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;
}

.media-thumbnail .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.75rem;
  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;
}

.media-thumbnail 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;
}

.media-thumbnail 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 img:first-of-type {
  grid-column: 1 / -1;
}

.media-thumbnail.count-2 img,
.media-thumbnail.count-3 img,
.media-thumbnail.count-many img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  min-width: 0;
  min-height: 0;
}

/* "+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;
}

.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;
}

.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;
}

.queue-url a {
  word-break: break-all;
}

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

.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;
}

/* 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);
}

/* 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;
  }

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