:root {
  color-scheme: dark;
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
    sans-serif;
  background-color: #0b0d11;
  color: #f5f6f8;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at top, #162034, #0b0d11 60%);
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: 0.75rem 3vw 0.75rem;
  /*border-bottom: 1px solid rgba(255, 255, 255, 0.08);*/
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.header-text {
  text-align: left;
}

.app-header h1 {
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.01em;
  font-size: 1.45rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.layout-toggle,
.random-button,
.sort-button {
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(13, 19, 32, 0.8);
  color: #f5f6f8;
  border-radius: 50%;
  width: 44px;
  height: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.layout-toggle:hover,
.random-button:hover,
.sort-button:hover {
  border-color: rgba(255, 255, 255, 0.6);
  background: rgba(22, 29, 45, 0.9);
}

.layout-icon,
.random-icon,
.sort-icon {
  width: 20px;
  height: 20px;
  display: block;
  flex-shrink: 0;
}

.layout-dropdown-container {
  position: relative;
}

.layout-dropdown {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  background: rgba(13, 19, 32, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 0.5rem 0;
  min-width: 180px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  z-index: 1000;
  display: none;
  backdrop-filter: blur(20px);
}

.layout-dropdown[aria-hidden="false"] {
  display: block;
}

.layout-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.75rem 1rem;
  background: none;
  border: none;
  color: #d7ddee;
  text-align: left;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.2s ease, color 0.2s ease;
}

.layout-option:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.layout-option .check-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  margin-left: 0.5rem;
}


.toggle-icon {
  width: 18px;
  height: 18px;
  display: block;
  flex-shrink: 0;
}

@media (max-width: 600px) {
  .app-header {
    flex-direction: column;
    align-items: flex-start;
  }

  /* Ensure dropdown aligns to the right of the button on mobile */
  .layout-dropdown {
    left: 0;
    right: auto;
  }
  
  /* Make folders fit 2 per row on mobile */
  .folder-tile {
    flex: 1 1 calc(50% - 0.375rem); /* 50% minus half the gap */
    min-width: calc(50% - 0.375rem);
    max-width: calc(50% - 0.375rem);
  }
  
  .folder-icon {
    width: 80px;
    height: 80px;
  }
  
  .folder-thumbnail {
    width: 120px;
    height: 120px;
  }
}

main {
  flex: 1;
  padding: 0 3vw 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  min-height: 0;
  transition: padding-bottom 0.3s ease;
}

/* When tray is open, add bottom padding to reserve space for tray */
body.tray-open main {
  padding-bottom: calc(25vh + 1.5rem); /* Tray height (25vh) + original bottom padding */
}

.breadcrumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  font-size: 1.425rem; /* 1.5 * 0.95rem for consistency */
  color: #9ea8c1;
}

.breadcrumbs a,
.breadcrumbs span {
  font-size: 1em; /* All items use the same size relative to container */
}

.breadcrumbs a {
  background: none;
  border: none;
  color: inherit;
  text-decoration: none;
  cursor: pointer;
  border-radius: 4px;
  transition: color 0.2s ease, background-color 0.2s ease;
}

.breadcrumbs a:hover {
  color: #fff;
  background-color: rgba(255, 255, 255, 0.08);
}

.folders {
  padding: 1rem 0;
  max-height: 1000px;
  overflow: hidden;
  transition: max-height 0.3s ease-out, padding 0.3s ease-out, opacity 0.3s ease-out;
  opacity: 1;

  /*background: rgba(13, 19, 32, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  backdrop-filter: blur(20px);*/
}

.folders.collapsed {
  max-height: 0;
  padding: 0;
  opacity: 0;
}

.folders h2 {
  margin-top: 0;
  font-size: 1rem;
  font-weight: 500;
  color: #b9c3da;
}

.folder-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.folder-tile {
  border: none;
  border-radius: 12px;
  background: rgba(20, 24, 35, 0.9);
  color: #f5f6f8;
  text-align: center;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  font-size: 0.95rem;
  transition: transform 0.2s ease, background 0.2s ease;
  white-space: nowrap;
  min-width: 120px;
  min-height: 120px;
  position: relative;
  overflow: hidden;
  padding: 0;
}

.folder-tile:hover {
  background: rgba(25, 30, 42, 0.95);
  transform: translateY(-2px);
}

.folder-icon {
  width: 100%;
  height: 100%;
  flex-shrink: 0;
  display: block;
  object-fit: cover;
  border-radius: 12px;
}

.folder-thumbnail {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 12px;
  flex-shrink: 0;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.folder-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 0.5rem;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.6) 50%, transparent 100%);
  color: #f5f6f8;
  font-weight: 500;
  z-index: 1;
  text-align: center;
  border-radius: 0 0 12px 12px;
}

.overlay-close svg {
  width: 24px;
  height: 24px;
  display: block;
}

.media-wrapper {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.media-wall {
  flex: 1;
  min-height: 0;
  padding: 0 0 0.5rem 0;
  scrollbar-width: thin;
  width: 100%;
  box-sizing: border-box;
  transition: opacity 0.3s ease-in-out;
  opacity: 1;
}

.media-wall.loading {
  opacity: 0.3;
  pointer-events: none;
}

/* Masonry layout (default) - Pinterest-style masonry with variable heights */
.media-wall.masonry {
  column-count: auto;
  column-width: 400px;
  column-gap: 1rem;
  column-fill: balance;
  overflow-y: auto;
  overflow-x: hidden;
  width: 100%;
  box-sizing: border-box;
  writing-mode: horizontal-tb; /* Items flow vertically down columns */
}

.media-wall.masonry .media-card {
  break-inside: avoid;
  page-break-inside: avoid;
  -webkit-column-break-inside: avoid;
  margin-bottom: 1rem;
  display: inline-block;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  vertical-align: top;
}

.media-wall.masonry .media-frame {
  aspect-ratio: unset;
  height: auto !important;
  min-height: 0;
  display: block !important;
  position: relative;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

.media-wall.masonry .media-frame img,
.media-wall.masonry .media-frame video {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  max-height: none !important;
  min-width: 0 !important;
  min-height: 0;
  object-fit: contain;
  display: block;
  position: relative;
  box-sizing: border-box;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

.media-wall.masonry .media-frame img.loaded,
.media-wall.masonry .media-frame video.loaded {
  opacity: 1;
}

/* Ensure images are constrained even when loaded - higher specificity */
.media-wall.masonry .media-card .media-frame img[src],
.media-wall.masonry .media-card .media-frame video[src] {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  display: block !important;
}

.media-wall.masonry::-webkit-scrollbar {
  width: 8px;
}

.media-wall.masonry::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.25);
  border-radius: 999px;
}

/* Responsive column width for masonry */
@media (max-width: 600px) {
  .media-wall.masonry {
    column-width: min(90vw, 400px);
  }
}

/* Grid vertical layout - full width, vertical scrolling */
.media-wall.grid-vertical {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 400px), 1fr));
  gap: 1rem;
  overflow-y: auto;
  overflow-x: hidden;
  align-content: start;
}

.media-wall.grid-vertical::-webkit-scrollbar {
  width: 8px;
}

.media-wall.grid-vertical::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.25);
  border-radius: 999px;
}

/* Grid horizontal layout - horizontal scrolling, multiple rows */
.media-wall.grid-horizontal {
  display: flex;
  flex-flow: column wrap;
  gap: 1rem;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x proximity;
  align-content: flex-start;
}

.media-wall.grid-horizontal::-webkit-scrollbar {
  height: 8px;
}

.media-wall.grid-horizontal::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.25);
  border-radius: 999px;
}

/* Legacy support for old class names */
.media-wall.vertical {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 400px), 1fr));
  gap: 1rem;
  overflow-y: auto;
  overflow-x: hidden;
  align-content: start;
}

.media-wall.horizontal {
  display: flex;
  flex-flow: column wrap;
  gap: 1rem;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x proximity;
  align-content: flex-start;
}

.media-card {
  scroll-snap-align: start;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  cursor: pointer;
  width: 100%;
}

.media-wall.masonry .media-card {
  width: 100%;
}

.media-wall.grid-vertical .media-card {
  width: 100%;
}

.media-wall.grid-horizontal .media-card {
  width: clamp(260px, 28vw, 400px);
}

/* Legacy support */
.media-wall.vertical .media-card {
  width: 100%;
}

.media-wall.horizontal .media-card {
  width: clamp(260px, 28vw, 400px);
}

.media-frame {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  aspect-ratio: 16 / 9; /* Default for grid layouts */
}

.media-frame img,
.media-frame video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: opacity 0.3s ease;
  max-width: 100%;
}

.media-caption {
  display: none;
  font-size: 0.85rem;
  color: #9ea8c1;
  margin: 0 0 0.25rem 0.35rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-message {
  margin-top: 0.5rem;
  color: #9ea8c1;
  font-size: 0.95rem;
}

.media-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 11, 0.85);
  backdrop-filter: blur(18px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 0;
  z-index: 1000;
  overflow: visible; /* Allow content to extend beyond overlay bounds when dragging */
}

/* When tray is open, adjust overlay bottom to leave space for tray */
body.tray-open .media-overlay {
  bottom: 25vh; /* Reserve space for tray (25vh) */
}

/* When tray is maximized, overlay can use full height */
body.tray-maximized .media-overlay {
  bottom: 0; /* Full height when tray is maximized */
  z-index: 1001; /* tray (tray is 1002) */
}

.media-overlay[hidden] {
  display: none !important;
}

.overlay-close,
.overlay-tray-button,
.overlay-fit-button,
.overlay-autoplay-button {
  position: absolute;
  top: 1rem;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #f5f6f8;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(15, 18, 26, 0.7);
  color: #fff;
  font-size: 1.4rem;
  cursor: pointer;
  z-index: 1001;
  transition: background 0.2s, transform 0.2s, opacity 0.3s ease, visibility 0.3s ease;
  opacity: 1;
  visibility: visible;
}

.overlay-close {
  right: 1rem;
}

.overlay-tray-button {
  right: 4rem; /* Position to the left of close button */
  display: none; /* Hidden by default, shown only for videos */
}

.overlay-fit-button {
  right: 7rem; /* Position to the left of tray button */
}

.overlay-autoplay-button {
  right: 10rem; /* Position to the left of fit button */
}

.overlay-fit-button svg,
.overlay-autoplay-button svg {
  width: 24px;
  height: 24px;
  display: block;
}

.overlay-close:hover,
.overlay-tray-button:hover,
.overlay-fit-button:hover,
.overlay-autoplay-button:hover {
  background: rgba(25, 30, 40, 0.9);
  transform: scale(1.05);
}

.overlay-autoplay-button.active {
  background: rgba(100, 150, 200, 1);
  border-color: rgba(255, 255, 255, 1);
  box-shadow: 0 0 20px rgba(100, 150, 200, 0.6), 0 0 40px rgba(100, 150, 200, 0.3);
}

.overlay-tray-button:active,
.overlay-tray-button.active {
  background: rgba(100, 150, 200, 1);
  transform: scale(0.85) rotate(5deg);
  border-color: rgba(255, 255, 255, 1);
  box-shadow: 0 0 20px rgba(100, 150, 200, 0.6), 0 0 40px rgba(100, 150, 200, 0.3);
}

.overlay-tray-button.active svg {
  transform: scale(1.1);
  transition: transform 0.2s ease;
}

.overlay-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  opacity: 1;
  visibility: visible;
  color: #fff;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(15, 18, 26, 0.7);
  cursor: pointer;
  z-index: 1001;
  transition: background 0.2s, transform 0.2s, opacity 0.2s;
  opacity: 0.8;
}

.overlay-nav:hover {
  background: rgba(25, 30, 40, 0.9);
  transform: translateY(-50%) scale(1.1);
  opacity: 1;
}

.overlay-nav-left {
  left: 1rem;
}

.overlay-nav-right {
  right: 1rem;
}

/* Mobile: Move navigation buttons to bottom and full height videos */
@media (max-width: 768px) {
  .overlay-nav {
    top: auto;
    bottom: calc(25vh + 1rem); /* Above tray when open */
    transform: none;
  }
  
  body.tray-open .overlay-nav {
    bottom: calc(25vh + 1rem);
  }
  
  body:not(.tray-open) .overlay-nav {
    bottom: 1rem;
  }
  
  .overlay-nav-left {
    left: 1rem;
  }
  
  .overlay-nav-right {
    right: 1rem;
  }
  
  .overlay-nav:hover {
    transform: scale(1.1);
  }
  
  /* Hide navigation buttons on mobile */
  .overlay-nav {
    display: none !important;
  }
  
  /* Full height video on mobile - centered */
  .media-overlay {
    padding: 0;
    padding-top: 3.5rem; /* Space for caption */
  }
  
  .overlay-content {
    max-width: none;
    width: auto;
    flex: 1;
    height: 100%;
    max-height: 100vh;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    min-height: 0;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  
  body.tray-open .overlay-content {
    max-height: none;
    height: 100%;
    flex: 1;
  }
  
  .overlay-content video {
    width: auto;
    height: 100%;
    max-width: none;
    max-height: 100%;
    object-fit: cover;
    border-radius: 0;
    flex-shrink: 0;
    scroll-snap-align: center;
  }
  
  .overlay-content img {
    width: auto;
    height: 100%;
    max-width: none;
    max-height: 100%;
    object-fit: cover;
    border-radius: 0;
    flex-shrink: 0;
    scroll-snap-align: center;
  }
  
  .overlay-caption {
    top: 0.5rem;
    left: 0.5rem;
    font-size: 0.9rem;
    padding: 0.4rem 0.8rem;
  }
}

.overlay-nav svg {
  width: 24px;
  height: 24px;
}

.overlay-content {
  width: 100%;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0; /* Allow flex shrinking */
  overflow: hidden; /* Prevent content from spilling */
  height: 100%; /* Fill available space - takes full remaining height */
}

/* Allow overflow when in cover mode for dragging */
.overlay-content.cover-mode {
  overflow: visible !important;
  /* Ensure container doesn't constrain child width */
  min-width: 0;
  max-width: none;
  /* Allow children to extend beyond container bounds */
  align-items: flex-start;
  justify-content: flex-start;
  /* Use relative positioning context for absolute children if needed */
  position: relative;
}

/* In cover mode, allow image/video to extend beyond container when dragged */
.overlay-content.cover-mode img,
.overlay-content.cover-mode video {
  position: relative;
  will-change: transform;
  /* Remove constraints to allow image to extend beyond container */
  max-width: none !important;
  max-height: none !important;
  /* JavaScript will set explicit width/height, so don't override with auto */
  /* width and height will be set via inline styles from JavaScript */
  min-width: 100%;
  min-height: 100%;
  /* Center the element initially, transform will move it */
  margin: auto;
  /* Ensure transform origin is at center */
  transform-origin: center center;
}

/* When tray is open, content uses available space (top 3/4) */
body.tray-open .overlay-content {
  max-height: none; /* Remove max-height constraint, use flex instead */
  height: 100%; /* Fill all available space */
}

/* When tray is not open, use more space */
body:not(.tray-open) .overlay-content {
  max-height: none; /* Remove max-height constraint, use flex instead */
  height: 100%; /* Fill all available space */
}

.overlay-content img,
.overlay-content video {
  max-width: 100%;
  max-height: 100%;
  width: 100%;
  height: 100%;
  object-fit: contain; /* Default to contain, can be changed via button */
  border-radius: 18px;
  transition: opacity 0.4s ease-in-out;
  opacity: 1;
}

.overlay-content img.fade-out,
.overlay-content video.fade-out {
  opacity: 0;
}

.overlay-content img.fade-in,
.overlay-content video.fade-in {
  opacity: 0;
}

/* Hide video controls when controls-hidden class is present */
.overlay-content video.controls-hidden::-webkit-media-controls-panel,
.overlay-content video.controls-hidden::-webkit-media-controls-play-button,
.overlay-content video.controls-hidden::-webkit-media-controls-start-playback-button {
  display: none !important;
  opacity: 0 !important;
}

.overlay-content video.controls-hidden::--webkit-media-controls {
  display: none !important;
}

/* For Firefox and other browsers */
.overlay-content video.controls-hidden {
  --controls-opacity: 0;
}

/* Alternative approach: hide controls via pointer-events and opacity */
.overlay-content video.controls-hidden {
  pointer-events: none;
}

.overlay-content video.controls-hidden::-webkit-media-controls-enclosure {
  display: none !important;
}

/* When tray is maximized, video can take full height and width */
body.tray-maximized .overlay-content {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
}

body.tray-maximized .overlay-content video,
body.tray-maximized .overlay-content img {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
}

.overlay-content video {
  pointer-events: auto;
  cursor: pointer;
}

.overlay-caption {
  position: absolute;
  top: 1rem;
  left: 1rem;
  color: #d7ddee;
  font-size: 1rem;
  font-weight: 500;
  margin: 0;
  padding: 0.5rem 1rem;
  background: rgba(15, 18, 26, 0.7);
  border: none;
  border-radius: 8px;
  z-index: 1001;
  max-width: calc(100% - 8rem); /* Leave space for buttons */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  opacity: 1;
  visibility: visible;
}

.overlay-open {
  overflow: hidden;
}

/* Video Tray Styles */
.video-tray {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 25vh; /* Bottom 1/4 of screen */
  background: transparent; /* Background moved to individual sections */
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1002;
  display: flex;
  flex-direction: column;
  padding: 0; /* Padding moved to individual sections */
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
  pointer-events: auto;
  overflow: hidden; /* Prevent content from spilling */
  transform: translateY(100%);
  opacity: 0;
  visibility: hidden;
}

/* When tray is shown, slide it up */
body.tray-open .video-tray {
  transform: translateY(0);
  opacity: 1;
  visibility: visible;
}

/* When transitioning from open to minimized, maintain current position */
body.tray-open.tray-minimized .video-tray {
  transform: translateY(0) !important;
  opacity: 1 !important;
  visibility: visible !important;
  height: 25vh; /* Keep current height during transition */
}

/* When tray is hidden (display: none), ensure it's fully hidden */
.video-tray[style*="display: none"] {
  display: none !important;
}

/* Maximized tray - full height, fills entire viewport */
body.tray-maximized .video-tray {
  height: 100vh;
  top: 0 !important;
  bottom: auto !important;
  margin: 0;
  border-radius: 0;
  border-top: none;
  overflow: hidden; /* Prevent tray itself from scrolling, let content scroll */
}

/* When transitioning from maximized to minimized, maintain position with transform */
body.tray-maximized.tray-minimized .video-tray {
  top: auto !important;
  bottom: 0 !important;
  /* Transform will be set inline by JS to maintain visual position */
}

.tray-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(5, 7, 11, 0.6);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 8px 8px 0 0;
  transition: background 0.2s;
  opacity: 0;
  transform: translateY(20px);
  margin: 0;
}

body.tray-minimized .video-tray {
  height: auto !important;
  transform: translateY(0) !important;
  opacity: 1 !important;
  visibility: visible !important;
  position: fixed !important;
  bottom: 0 !important;
  top: auto !important;
  left: 0 !important;
  right: 0 !important;
  max-height: none !important;
  margin: 0 !important;
}

/* Ensure smooth transition from open to minimized - don't reset transform */
body:not(.tray-open).tray-minimized .video-tray {
  transform: translateY(0) !important;
}

body.tray-minimized .tray-header {
  height: 50px;
  opacity: 1;
  transform: translateY(0);
  border-radius: 0;
  border-bottom: none;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  flex-shrink: 0;
}

body.tray-minimized .tray-header:hover {
  background: rgba(15, 20, 30, 0.8);
}

body.tray-minimized .tray-actions {
  display: none;
}

body.tray-maximized .tray-header {
  border-radius: 0;
  margin-top: 0;
  padding-top: 8px;
}

body.tray-open .tray-header {
  opacity: 1;
  transform: translateY(0);
}

.tray-title {
  margin: 0;
  color: #d7ddee;
  font-size: 0.95rem;
  font-weight: 500;
}

.tray-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.tray-minimize,
.tray-maximize,
.tray-clear {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #d7ddee;
  padding: 0.4rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 0.2s, border-color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
}

.tray-minimize:hover,
.tray-maximize:hover,
.tray-clear:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.3);
}

.tray-restore {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 18, 26, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #d7ddee;
  padding: 0.75rem 1.25rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  display: none; /* Hidden by default, shown via JavaScript */
  align-items: center;
  gap: 0.5rem;
  z-index: 1001; /* Lower than tray (1002) so it doesn't appear over it */
  transition: background 0.2s, border-color 0.2s, transform 0.2s;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.tray-restore:hover {
  background: rgba(25, 30, 40, 0.95);
  border-color: rgba(255, 255, 255, 0.4);
  transform: translateX(-50%) translateY(-2px);
}

.tray-restore.active {
  background: rgba(100, 150, 200, 1);
  transform: translateX(-50%) scale(1.1) translateY(-2px);
  box-shadow: 0 0 20px rgba(100, 150, 200, 0.6), 0 0 40px rgba(100, 150, 200, 0.3);
  border-color: rgba(255, 255, 255, 1);
}

.tray-header.active {
  background: rgba(100, 150, 200, 0.3);
  box-shadow: 0 0 20px rgba(100, 150, 200, 0.4), 0 0 40px rgba(100, 150, 200, 0.2);
}

.tray-restore svg {
  width: 20px;
  height: 20px;
}

.scroll-to-top {
  position: fixed;
  bottom: 2rem;
  right: 3vw;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(13, 19, 32, 0.9);
  color: #f5f6f8;
  border-radius: 50%;
  width: 48px;
  height: 48px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease, opacity 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 1000; /* Lower than tray (1002) and tray-restore (1001) */
  opacity: 0;
  pointer-events: none;
}

.scroll-to-top:not([hidden]) {
  opacity: 1;
  pointer-events: auto;
}

.scroll-to-top:hover {
  border-color: rgba(255, 255, 255, 0.6);
  background: rgba(22, 29, 45, 0.95);
  transform: translateY(-2px);
}

.scroll-to-top svg {
  width: 20px;
  height: 20px;
  display: block;
  flex-shrink: 0;
}

.tray-content {
  flex: 1;
  display: flex;
  gap: 0.75rem;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.5rem 1rem;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
  background: rgba(5, 7, 11, 0.6); /* More transparent content area */
  border-radius: 0 0 8px 8px;
  transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1), transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  transform: translateY(20px);
}

body.tray-maximized .tray-content {
  display: block !important;
}

body.tray-open .tray-content {
  opacity: 1;
  transform: translateY(0);
}

.tray-content::-webkit-scrollbar {
  height: 6px;
}

.tray-content::-webkit-scrollbar-track {
  background: transparent;
}

.tray-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.tray-content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

.tray-media-item {
  flex-shrink: 0;
  position: relative;
  width: 200px;
  height: 100%;
  border-radius: 8px;
  overflow: hidden;
  background: rgba(15, 18, 26, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  transition: border-color 0.2s;
}

.tray-media-item:hover {
  transform: scale(1.05);
  border-color: rgba(255, 255, 255, 0.3);
}

.tray-media-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.tray-media-item .tray-media-remove {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  color: #fff;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 10;
}

.tray-media-item:hover .tray-media-remove {
  opacity: 1;
}

.tray-media-item .tray-media-remove:hover {
  background: rgba(255, 0, 0, 0.8);
  border-color: rgba(255, 255, 255, 0.5);
}

.tray-media-item .tray-media-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
  color: #fff;
  padding: 0.5rem;
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Maximized tray - masonry grid layout */
body.tray-maximized .tray-content {
  display: block !important;
  column-count: auto;
  column-width: 200px;
  column-gap: 0.75rem;
  column-fill: balance;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  flex: 1; /* Take remaining space after header */
  min-height: 0; /* Allow shrinking for flex */
  max-height: calc(100vh - 60px); /* Account for header height */
  padding: 0.5rem;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
  writing-mode: horizontal-tb;
  height: auto;
}

body.tray-maximized .tray-content::-webkit-scrollbar {
  width: 8px;
}

body.tray-maximized .tray-content::-webkit-scrollbar-track {
  background: transparent;
}

body.tray-maximized .tray-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 4px;
}

body.tray-maximized .tray-content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

body.tray-maximized .tray-media-item {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  min-height: 0 !important;
  max-height: none !important;
  break-inside: avoid;
  page-break-inside: avoid;
  -webkit-column-break-inside: avoid;
  margin-bottom: 0.75rem;
  display: inline-block;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  background: rgba(20, 24, 35, 0.9);
  flex-shrink: 0;
  flex-grow: 0;
  flex-basis: auto;
}

body.tray-maximized .tray-media-item img,
body.tray-maximized .tray-media-item video {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  max-height: none !important;
  display: block !important;
  object-fit: contain;
  position: relative !important;
  top: auto !important;
  left: auto !important;
}

@media (max-width: 600px) {
  .media-card {
    min-width: 90vw;
  }
  
  .overlay-tray-button {
    right: 4rem;
  }
  
  .tray-media-item {
    width: 150px;
  }
  
  /* Fix tray maximize on mobile - use small viewport height to avoid address bar */
  body.tray-maximized .video-tray {
    height: 100svh; /* Small viewport height - smallest possible viewport */
    height: -webkit-fill-available; /* Safari fallback */
    height: calc(100vh - 1px); /* Fallback for older browsers */
  }
  
  /* Mobile: Vertical stack layout when tray is maximized */
  body.tray-maximized .tray-content {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.75rem;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    padding: 0.5rem;
    align-items: stretch;
  }
  
  body.tray-maximized .tray-media-item {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    display: block !important;
    margin-bottom: 0;
    break-inside: auto !important;
    page-break-inside: auto !important;
    -webkit-column-break-inside: auto !important;
  }
  
  body.tray-maximized .tray-media-item img,
  body.tray-maximized .tray-media-item video {
    width: 100% !important;
    height: auto !important;
    display: block !important;
    object-fit: contain;
  }
}

