@layer components {
  .video-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1rem;
    margin-bottom: 4rem;

    @media (max-width: 480px) {
      grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
      gap: 0.6rem;
    }
  }

  .video-grid-item {
    position: relative;
    background: transparent;
    border-radius: 12px;
    transition: background-color 0.4s ease;
  }

  .video-grid-item[data-favorite-favorite=true] {
    background-color: #e4ffed;
    box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.5);
  }

  .video-grid-item[data-favorite-favorite=true] .video-grid-favorite .grid-favorite-button {
    width: 1.9rem;
    height: 1.9rem;
    padding: 0;
    color: white;
    background: #15803d;
    border-radius: 50%;
    filter: none;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2), 0 0 0 2px rgba(255, 255, 255, 0.9);
    --icon-size: 0.95rem;
  }

  @media (hover: hover) {
    .video-grid-item[data-favorite-favorite=true] .video-grid-favorite .grid-favorite-button:hover {
      background: #16a34a;
      color: white;
    }
  }

  .video-grid-thumb {
    position: relative;
    display: block;
    aspect-ratio: 16 / 9;
    overflow: hidden;
    background: #111;
    border-radius: 12px;
  }

  .video-grid-media {
    position: relative;
  }

  .video-grid-duration {
    position: absolute;
    right: 0.5rem;
    bottom: 0.5rem;
    padding: 0.1rem 0.35rem;
    font-size: 0.75rem;
    font-weight: 500;
    line-height: 1.2;
    color: white;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 4px;
    pointer-events: none;
  }

  .video-grid-progress {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    pointer-events: none;
  }

  .video-grid-progress::after {
    content: "";
    display: block;
    height: 100%;
    width: var(--progress, 0%);
    background: #ef4444;
  }

  .video-grid-watched-badge {
    position: absolute;
    left: 0.5rem;
    bottom: 0.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.15rem 0.4rem;
    font-size: 0.7rem;
    font-weight: 600;
    line-height: 1;
    color: white;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 4px;
    pointer-events: none;
    opacity: 0;
    --icon-size: 0.85rem;
  }

  .video-grid-watched-action {
    position: absolute;
    left: 0.35rem;
    bottom: 0.35rem;
    color: white;
    --icon-size: 1rem;
  }

  .video-grid-watched-action form {
    margin: 0;
  }

  .video-grid-watched-action .grid-watched-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.9rem;
    height: 1.9rem;
    padding: 0;
    color: white;
    background: rgba(17, 24, 39, 0.55);
    border: none;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2), 0 0 0 2px rgba(255, 255, 255, 0.9);
    filter: none;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  @media (hover: hover) {
    .video-grid-watched-action .grid-watched-button:hover {
      background: rgba(17, 24, 39, 0.75);
    }
  }

  .video-grid-item.is-watched .video-grid-watched-action .grid-watched-button {
    background: #7c3aed;
    color: white;
  }

  @media (hover: hover) {
    .video-grid-item.is-watched .video-grid-watched-action .grid-watched-button:hover {
      background: #6d28d9;
      color: white;
    }
  }

  .video-grid-item.is-watched .video-grid-thumb img {
    opacity: 0.55;
  }

  .video-grid-item.is-watched .video-grid-watched-badge {
    opacity: 1;
  }

  .video-grid-item.is-watched:hover .video-grid-watched-badge,
  .video-grid-item.is-watched:focus-within .video-grid-watched-badge {
    opacity: 0;
  }

  .video-grid-item.is-watched .video-grid-progress {
    display: none;
  }

  .video-grid-item.is-watched .video-grid-title {
    color: #6b7280;
  }

  .video-grid-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.4s ease-out;
  }

  .video-grid-item:hover .video-grid-thumb img {
    transform: scale(1.05);
  }

  .video-grid-thumb-state {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    color: #bbb;
    --icon-size: 1.75rem;
  }

  .video-grid-thumb-spinner {
    animation: spin 1s linear infinite;
  }

  .video-grid-thumb-empty {
    flex-direction: column;
    gap: 0.6rem;
    padding: 1rem;
    background: linear-gradient(135deg, #1f2937 0%, #312e81 50%, #4c1d95 100%);
    color: #c4b5fd;
  }

  .video-grid-thumb-empty .video-grid-placeholder-icon {
    --icon-size: 2.25rem;
  }

  .video-grid-placeholder-domain {
    max-width: 100%;
    padding: 0 0.5rem;
    font-size: 0.8rem;
    font-weight: 500;
    color: #ddd6fe;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .video-grid-body {
    display: grid;
    grid-template-columns: 36px 1fr;
    gap: 0.75rem;
    padding: 0.75rem 0.25rem 0.5rem;
  }

  .video-grid-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    background: #e5e7eb;
    flex-shrink: 0;
  }

  .video-grid-avatar-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #9ca3af;
    --icon-size: 1rem;
  }

  .video-grid-caption {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    min-width: 0;
    color: inherit;
    text-decoration: none;
  }

  .video-grid-title {
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.3;
    color: #111;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-word;
  }

  .video-grid-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.8rem;
    color: #6b7280;
    overflow: hidden;
  }

  .video-grid-channel {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }

  .video-grid-meta-sep {
    color: #9ca3af;
  }

  .video-grid-favorite,
  .video-grid-menu {
    position: absolute;
    top: 0.35rem;
    color: white;
    --icon-size: 1rem;
  }

  .video-grid-favorite {
    left: 0.35rem;
  }

  .video-grid-menu {
    right: 0.35rem;
  }

  .video-grid-favorite form {
    margin: 0;
  }

  .video-grid-menu .dropdown-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.9rem;
    height: 1.9rem;
    padding: 0;
    border-radius: 50%;
    color: white;
    background: #7c3aed;
    border: none;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2), 0 0 0 2px rgba(255, 255, 255, 0.9);
    transition: background-color 0.15s ease;
    --icon-size: 1.25rem;
  }

  @media (hover: hover) {
    .video-grid-menu .dropdown-toggle:hover {
      background: #6d28d9;
    }
  }

  @media (hover: hover) {
    .video-grid-favorite,
    .video-grid-menu,
    .video-grid-watched-action {
      opacity: 0;
      transition: opacity 0.15s ease;
    }

    .video-grid-item:hover .video-grid-favorite,
    .video-grid-item:hover .video-grid-menu,
    .video-grid-item:hover .video-grid-watched-action,
    .video-grid-item:focus-within .video-grid-favorite,
    .video-grid-item:focus-within .video-grid-menu,
    .video-grid-item:focus-within .video-grid-watched-action,
    .video-grid-item[data-favorite-favorite=true] .video-grid-favorite,
    .video-grid-item.is-watched .video-grid-watched-action {
      opacity: 1;
    }
  }

  /* Touch devices never fire the hover reveal above, so the action buttons
     would otherwise sit at full strength on every tile. Keep them one-tap
     reachable but quiet: smaller, glassy, monochrome. The favourite *state*
     is already legible from the tile's green ring/tint, so the buttons can
     recede. (Mirrors the photo grid treatment in images.css.) */
  @media (hover: none) {
    .video-grid-favorite .grid-favorite-button,
    .video-grid-menu .dropdown-toggle,
    .video-grid-watched-action .grid-watched-button {
      width: 1.5rem;
      height: 1.5rem;
      background: rgba(15, 15, 15, 0.4);
      backdrop-filter: blur(8px);
      box-shadow: none;
      opacity: 0.65;
      --icon-size: 0.95rem;
    }

    .video-grid-menu .dropdown-toggle {
      color: #fff;
    }

    /* Active states keep their colour so they still read as a status. */
    .video-grid-item[data-favorite-favorite=true] .video-grid-favorite .grid-favorite-button {
      width: 1.5rem;
      height: 1.5rem;
      background: #15803d;
      box-shadow: none;
      opacity: 1;
    }

    .video-grid-item.is-watched .video-grid-watched-action .grid-watched-button {
      width: 1.5rem;
      height: 1.5rem;
      background: #7c3aed;
      box-shadow: none;
      opacity: 1;
    }
  }

  /* Wider leading column for the video thumbnail. */
  .video-row {
    --lead-col: 78px;
    --lead-gap: 0.55rem;
  }

  .video-row-thumb-wrap {
    display: inline-flex;
  }

  .video-row-thumb {
    width: 56px;
    height: 32px;
    object-fit: cover;
    border-radius: 4px;
    flex-shrink: 0;
    background: #111;
    transition: opacity 0.15s ease, filter 0.15s ease;
  }

  .video-row-thumb-badge {
    position: absolute;
    top: 0.2rem;
    right: 0.2rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1rem;
    height: 1rem;
    color: white;
    background: #7c3aed;
    border-radius: 50%;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.95);
    opacity: 0;
    transition: opacity 0.15s ease;
    --icon-size: 0.65rem;
    pointer-events: none;
  }

  .video-row-icon {
    width: 24px;
    flex-shrink: 0;
    color: #6b7280;
  }

  .video-row-watched-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: 0.4rem;
    width: 1.1rem;
    height: 1.1rem;
    color: white;
    background: #16a34a;
    border-radius: 50%;
    flex-shrink: 0;
    --icon-size: 0.7rem;
  }

  /* Watched state */
  .video-row.is-watched .item-row-title-text,
  .video-row.is-watched .item-row-meta {
    color: #6b7280;
  }

  .video-row.is-watched .video-row-thumb {
    opacity: 0.55;
    filter: saturate(0.85);
  }

  .video-row.is-watched .video-row-thumb-badge {
    opacity: 1;
  }

  .video-row-watched {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #d1d5db;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.25rem;
    transition: color 0.3s ease, background-color 0.15s ease;
  }

  .video-row-watched:hover {
    color: #c4b5fd;
  }

  .video-row.is-watched .video-row-watched {
    color: white;
    background: #7c3aed;
    border-radius: 999px;
  }

  .video-row.is-watched .video-row-watched:hover {
    color: white;
    background: #6d28d9;
  }

  @media (max-width: 640px) {
    .video-row-thumb {
      width: 78px;
      height: 44px;
    }

    .video-row-watched {
      width: 1.55rem;
      height: 1.55rem;
      padding: 0;
      justify-content: center;
    }
  }

  /* ---------- Video edit page ---------- */

  /* Wraps the "show sidebar" button and the back link, like .note-back-row.
     The button only appears while the sidebar is hidden (see sidebar.css). */
  .video-back-row {
    display: flex;
    align-items: center;
    gap: 0.25rem;
  }

  .video-back {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    margin-bottom: 1rem;
    color: #6b7280;
    text-decoration: none;
    font-size: 0.9rem;
  }

  .video-back:hover {
    color: #111;
  }

  .video-edit {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
    margin: 0 0 4rem;
  }

  .video-edit-main {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    min-width: 0;
  }

  .video-edit-aside {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-width: 0;
  }

  @media (min-width: 1100px) {
    .video-edit {
      grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
      align-items: start;
    }

    .video-edit.video-edit-tweet {
      grid-template-columns: minmax(0, 550px) minmax(0, 1fr);
    }

    .video-edit-aside {
      position: sticky;
      top: 5.5rem;
      max-height: calc(100vh - 7rem);
    }
  }

  .video-edit-header {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    padding-right: 2.5rem;
    margin-top: 0.25rem;
  }

  .video-edit-header .video-title-form {
    margin: 0;

    & h2 {
      font-size: 1.15rem;
      font-weight: 600;
      line-height: 1.3;
      margin: 0;
      color: #111827;

      &:focus,
      .ProseMirror:focus {
        outline: none;
      }
    }
  }

  .video-actions {
    position: absolute;
    top: 0;
    right: 0;
    --dropdown-menu-left: auto;
    --dropdown-menu-right: 0;
  }

  .video-edit-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.4rem;
    font-size: 0.85rem;
    color: #6b7280;
    --icon-size: 0.8rem;
  }

  .video-category {
    display: inline-block;
  }

  .video-category-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0;
    background: none;
    border: 1px solid transparent;
    border-radius: 4px;
    color: #4b5563;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;

    &:hover,
    &:focus,
    &:focus-visible {
      outline: none;
    }
  }

  .video-category-current {
    font-weight: 500;
  }

  .video-category .dropdown-menu form.button_to:has(button.selected) {
    background-color: var(--dropdown-menu-selected-color);
    position: relative;

    &::after {
      position: absolute;
      top: 5px;
      right: 5px;
      content: "\2713";
    }
  }

  .video-meta-sep {
    color: #9ca3af;
  }

  .video-channel a {
    color: inherit;
    text-decoration: none;
  }

  .video-channel a:hover {
    color: #111827;
    text-decoration: underline;
  }

  .video-embed {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 9;
    background: #000;
    border-radius: 8px;
    overflow: hidden;
  }

  .video-embed iframe,
  .video-embed video,
  .video-embed [data-youtube-player-target=mount],
  .video-embed [data-vimeo-player-target=mount] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
  }

  .video-embed.video-embed-tweet {
    aspect-ratio: auto;
    background: transparent;
    border-radius: 0;
    overflow: visible;
    max-width: 550px;
  }

  .video-embed.video-embed-tweet iframe {
    position: static;
    inset: auto;
    width: 100% !important;
    height: auto;
    display: block;
  }

  .video-watch-state {
    display: flex;
  }

  .video-watch-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.825rem;
    font-weight: 500;
    color: #4b5563;
    background: white;
    border: 1px solid #d1d5db;
    border-radius: 999px;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
    --icon-size: 0.95rem;
  }

  .video-watch-toggle:hover {
    background: #f3f4f6;
    border-color: #9ca3af;
  }

  .video-watch-toggle.is-watched {
    color: white;
    background: #16a34a;
    border-color: #15803d;
  }

  .video-watch-toggle.is-watched:hover {
    background: #15803d;
  }

  .video-watch-toggle form,
  .video-watch-state form {
    margin: 0;
  }

  .video-link {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.8rem;
    color: #6b7280;
    --icon-size: 0.9rem;
    word-break: break-all;
    max-width: 80ch;
  }

  .video-link a {
    color: inherit;
    text-decoration: underline;
    text-decoration-color: #d1d5db;
    text-underline-offset: 3px;
  }

  .video-link a:hover {
    text-decoration-color: #6b7280;
  }

  .video-provider-description {
    font-size: 0.85rem;
    line-height: 1.45;
    color: #6b7280;
    max-width: 80ch;
  }

  .video-provider-description-text {
    margin: 0;
    max-height: calc(2 * 1.45em);
    overflow: hidden;
  }

  .video-provider-description-text p {
    margin: 0;
    white-space: pre-line;
  }

  .video-provider-description-text p + p {
    margin-top: 0.35rem;
  }

  .video-provider-description-text a {
    color: inherit;
    text-decoration: underline;
  }

  .video-provider-description-more {
    display: inline-block;
    margin-top: 0.25rem;
    font-size: 0.825rem;
    color: #2563eb;
    text-decoration: none;
  }

  .video-provider-description-more:hover {
    text-decoration: underline;
  }

  .video-description-dialog {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    width: min(48rem, 90vw);
    max-height: calc(95vh - 2rem);
  }

  .video-description-dialog-title {
    margin: 0;
    padding-right: 2rem;
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.3;
    color: #111827;
  }

  .video-description-dialog-meta {
    font-size: 0.85rem;
    color: #6b7280;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid rgba(17, 24, 39, 0.08);
  }

  .video-description-dialog-meta a {
    color: inherit;
    text-decoration: none;
  }

  .video-description-dialog-meta a:hover {
    color: #111827;
    text-decoration: underline;
  }

  .video-description-dialog-sep {
    margin: 0 0.35rem;
    color: #9ca3af;
  }

  .video-description-dialog-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding-right: 0.5rem;
    margin-right: -0.25rem;
    font-size: 0.95rem;
    line-height: 1.6;
    color: #1f2937;

    & p {
      margin: 0 0 0.75em;
      white-space: pre-line;
    }

    & p:last-child {
      margin-bottom: 0;
    }

    & a {
      color: #2563eb;
      text-decoration: underline;
    }
  }

  dialog:has(.video-description-dialog) {
    overflow: hidden;
  }

  .video-note-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #6b7280;
  }

  .video-note {
    background-color: white;
    border: 1px solid rgba(17, 24, 39, 0.06);
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06),
                0 4px 12px rgba(15, 23, 42, 0.04);
    padding: 0.75rem 1rem;
    cursor: text;
  }

  .video-note-form {
    margin: 0;
  }

  .video-note-content {
    min-height: 16rem;
    line-height: 1.6;

    &:focus,
    .ProseMirror:focus {
      outline: none;
    }

    & p {
      margin: 0 0 0.75em;
    }

    & p:last-child {
      margin-bottom: 0;
    }
  }

  @media (min-width: 1100px) {
    .video-edit-aside .video-note {
      display: flex;
      flex-direction: column;
      flex: 1;
      overflow: auto;
    }

    .video-edit-aside .video-note-form,
    .video-edit-aside .video-note-content {
      flex: 1;
    }
  }
}
