/*
 * Copyright (C) 2025 Jema Technology
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/* JemaOS Gallery - Fullscreen Viewer Styles */

.fullscreen-viewer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #000000;
  z-index: 2000; /* Ensure it covers everything including header */
  display: flex;
  flex-direction: column;
  opacity: 0;
  visibility: hidden;
  transition: all var(--transition-normal);
}

.fullscreen-viewer.active {
  opacity: 1;
  visibility: visible;
}

/* Viewer Toolbar */
.viewer-toolbar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 56px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 var(--space-lg);
  background-color: var(--surface-color);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
  color: var(--on-surface);
  z-index: var(--z-modal);
  flex-shrink: 0;
  pointer-events: none;
  box-sizing: border-box;
  overflow: visible;
}

.viewer-toolbar > * {
  pointer-events: auto;
}

.viewer-toolbar.edit-mode-toolbar {
  position: relative;
  background: var(--surface-color);
  pointer-events: auto;
}

#viewer-edit-toolbar {
  pointer-events: auto;
}

.viewer-top-left {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  justify-self: start;
  height: 100%;
  min-width: 0;
}

.viewer-top-center {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  justify-content: center;
  height: 100%;
  min-width: 0;
}

.viewer-top-right {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  justify-self: end;
  height: 100%;
  min-width: 0;
}

.viewer-filename {
  font-size: 14px;
  font-weight: 500;
  color: var(--on-surface-variant);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-left: var(--space-sm);
  max-width: 200px;
}

.viewer-divider {
  width: 1px;
  height: 24px;
  background-color: var(--divider-color);
  margin: 0 var(--space-xs);
}

.viewer-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--on-surface-variant);
  cursor: pointer;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
  pointer-events: auto;
}

.viewer-btn:hover {
  background-color: var(--surface-variant);
  color: var(--on-surface);
}

.viewer-btn:active {
  transform: scale(0.95);
}

.viewer-btn .material-icons {
  font-size: 20px;
}

/* Viewer Content */
.viewer-content {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background-color: #000000;
  box-sizing: border-box;
}


.viewer-media {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Image Viewer */
.viewer-media img {
  /* Default: shrink-to-fit (large images). JS may override for 1:1 display. */
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  transition: transform var(--transition-normal);
  cursor: default;
  /* GPU acceleration for smooth zoom/pan */
  will-change: transform;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
}

.viewer-media img.zoomed {
  cursor: grab;
}

.viewer-media img.zoomed:active {
  cursor: grabbing;
}

/* Micro-smooth transition during zoom gestures for Windows 11 Photos-like fluidity */
.viewer-media img.zooming {
  transition: transform 0.08s ease-out !important;
}

/* No transition during pinch-to-zoom for 1:1 tracking */
.viewer-media img.pinching {
  transition: none !important;
}

/* Video Viewer */
.viewer-media video {
  max-width: 100%;
  max-height: 100%;
  background-color: #000000;
  outline: none;
}

.video-controls-container {
  position: absolute;
  bottom: var(--space-xl);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(32, 33, 36, 0.9);
  border-radius: 24px;
  padding: var(--space-md) var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  backdrop-filter: blur(10px);
  opacity: 0;
  transition: opacity var(--transition-fast);
  min-width: 400px;
  max-width: 90vw;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

.video-controls-container.visible {
  opacity: 1;
  pointer-events: auto;
}

.video-controls-row {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  width: 100%;
}

.video-controls-row.primary {
  justify-content: center;
}

.video-controls-row.secondary {
  justify-content: space-between;
  font-size: var(--font-size-xs);
}

.video-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: #e8eaed;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  pointer-events: auto;
}

.video-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.play-pause-btn {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.1);
}

.play-pause-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.volume-slider-container {
  width: 80px;
  display: flex;
  align-items: center;
}

.volume-slider {
  width: 100%;
  height: 4px;
  -webkit-appearance: none;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  outline: none;
}

.volume-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
  cursor: pointer;
}

.video-progress {
  flex: 1;
  height: 4px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  position: relative;
  display: flex;
  align-items: center;
  margin: 0 var(--space-md);
}

.video-progress-bar {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: var(--primary-color);
  border-radius: 2px;
  width: 0%;
  pointer-events: none;
}

.video-seek-slider {
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-appearance: none;
  background: transparent;
  margin: 0;
  outline: none;
  cursor: pointer;
  z-index: 2;
}

.video-seek-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 0;
  height: 0;
  background: transparent;
}

.video-progress:hover .video-seek-slider::-webkit-slider-thumb {
  width: 12px;
  height: 12px;
  background: var(--primary-color);
  border-radius: 50%;
}

.video-time {
  color: #bdc1c6;
  font-family: Roboto, sans-serif;
  min-width: 40px;
  text-align: center;
}

/* Audio Viewer */
.viewer-media audio {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-md);
  outline: none;
}

.audio-visualizer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: 60px;
  margin-bottom: var(--space-md);
}

.audio-bar {
  width: 4px;
  background: var(--accent-color);
  border-radius: 2px;
  animation: audioVisualize 1s ease-in-out infinite;
}

.audio-bar:nth-child(1) { height: 20%; animation-delay: 0s; }
.audio-bar:nth-child(2) { height: 40%; animation-delay: 0.1s; }
.audio-bar:nth-child(3) { height: 80%; animation-delay: 0.2s; }
.audio-bar:nth-child(4) { height: 60%; animation-delay: 0.3s; }
.audio-bar:nth-child(5) { height: 90%; animation-delay: 0.4s; }
.audio-bar:nth-child(6) { height: 70%; animation-delay: 0.5s; }
.audio-bar:nth-child(7) { height: 50%; animation-delay: 0.6s; }
.audio-bar:nth-child(8) { height: 85%; animation-delay: 0.7s; }
.audio-bar:nth-child(9) { height: 30%; animation-delay: 0.8s; }
.audio-bar:nth-child(10) { height: 75%; animation-delay: 0.9s; }
.audio-bar:nth-child(11) { height: 55%; animation-delay: 1s; }
.audio-bar:nth-child(12) { height: 95%; animation-delay: 1.1s; }
.audio-bar:nth-child(13) { height: 35%; animation-delay: 1.2s; }
.audio-bar:nth-child(14) { height: 65%; animation-delay: 1.3s; }
.audio-bar:nth-child(15) { height: 45%; animation-delay: 1.4s; }

@keyframes audioVisualize {
  0%, 100% { transform: scaleY(0.3); }
  50% { transform: scaleY(1); }
}



/* Loading States */
.viewer-loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-md);
  color: white;
}

.viewer-spinner {
  width: 48px;
  height: 48px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: var(--radius-full);
  animation: spin 1s linear infinite;
}


/* Keyboard Shortcuts Hint */
.keyboard-hint {
  position: absolute;
  top: var(--space-lg);
  left: var(--space-lg);
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: var(--space-sm);
  border-radius: var(--radius-md);
  font-size: var(--font-size-xs);
  opacity: 0;
  transition: opacity var(--transition-fast);
  backdrop-filter: blur(10px);
}

.keyboard-hint.visible {
  opacity: 1;
}

.keyboard-hint ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.keyboard-hint li {
  display: flex;
  justify-content: space-between;
  gap: var(--space-md);
}

/* Error States */
.viewer-error {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: white;
}

.viewer-error .material-icons {
  font-size: 64px;
  margin-bottom: var(--space-md);
  opacity: 0.5;
}

.viewer-error h3 {
  margin-bottom: var(--space-sm);
  font-size: var(--font-size-xl);
}

.viewer-error p {
  opacity: 0.7;
  margin-bottom: var(--space-lg);
}

/* Touch Gestures */
.touch-gestures {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.touch-gesture-area {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 20%;
  pointer-events: all;
}

.touch-gesture-area.left {
  left: 0;
}

.touch-gesture-area.right {
  right: 0;
}


.text-icon-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    background: none;
    border: none;
    color: var(--on-surface-variant);
    font-size: 14px;
    cursor: pointer;
    padding: 8px 16px;
    border-radius: 20px;
    transition: background 0.2s;
}

.text-icon-btn:hover {
    background-color: var(--surface-variant);
    color: var(--on-surface);
}

.icon-btn {
  background: none;
  border: none;
  color: var(--on-surface-variant);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  transition: background-color 0.2s;
}

.icon-btn:hover {
  background-color: var(--surface-variant);
  color: var(--on-surface);
}


/* Responsive Design */
@media (max-width: 768px) {
  .viewer-toolbar {
    padding: 0 var(--space-md);
  }
  
  .viewer-info {
    max-width: 100%;
    margin: 0;
    order: 3;
    flex-basis: 100%;
  }
  
  .viewer-actions {
    order: 2;
  }
  
  
  .zoom-controls {
    top: var(--space-md);
    right: var(--space-md);
  }
  
  .zoom-level {
    top: 70px;
    right: var(--space-md);
  }
}

@media (max-width: 480px) {
  .viewer-toolbar {
    padding: 0 var(--space-sm);
  }
  
  .viewer-btn {
    width: 36px;
    height: 36px;
  }
  
  
  .video-controls-container {
    padding: var(--space-sm);
    gap: var(--space-sm);
    bottom: var(--space-md);
    min-width: auto;
    width: 95%;
  }
}

/* Animation Classes */
.slide-in-left {
  animation: slideInLeft 0.3s ease-out;
}

.slide-in-right {
  animation: slideInRight 0.3s ease-out;
}

.zoom-in {
  animation: zoomIn 0.3s ease-out;
}

@keyframes slideInLeft {
  from {
    transform: translateX(-100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes slideInRight {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes zoomIn {
  from {
    transform: scale(0.8);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* Window Controls Overlay Support */
@media (display-mode: window-controls-overlay) {
  .viewer-toolbar {
    padding-left: calc(var(--space-lg) + env(titlebar-area-x, 0));
    padding-right: calc(var(--space-lg) + (100vw - (env(titlebar-area-x, 0) + env(titlebar-area-width, 100vw))));
    -webkit-app-region: drag;
    app-region: drag;
    pointer-events: auto;
  }

  .viewer-toolbar > * {
    -webkit-app-region: no-drag;
    app-region: no-drag;
  }
}

/* Light Mode Support */
@media (prefers-color-scheme: light) {
  .fullscreen-viewer {
    background-color: #f8f9fa;
  }

  .viewer-content {
    background-color: #f8f9fa;
  }
  
  /* Keep video background dark for better viewing experience */
  .viewer-media video {
    background-color: #000000;
  }

  .keyboard-hint {
    background: rgba(255, 255, 255, 0.9);
    color: #202124;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }
  
  .viewer-error {
    color: #5f6368;
  }
  
  .viewer-spinner {
    border: 3px solid rgba(0, 0, 0, 0.1);
    border-top: 3px solid var(--primary-color);
  }
  
  .crop-controls {
    background: rgba(255, 255, 255, 0.9);
    color: #202124;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
  }
}

/* Adjustment Controls */
.adjustment-group {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
}

.adjustment-group:last-child {
  border-bottom: none;
}

.adjustment-header {
  font-size: 14px;
  font-weight: 500;
  color: var(--on-surface);
  margin-bottom: 8px;
}

.adjustment-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.adjustment-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--on-surface-variant);
}

.adjustment-label {
  display: flex;
  align-items: center;
  gap: 8px;
}

.adjustment-slider {
  width: 100%;
  -webkit-appearance: none;
  height: 2px;
  background: var(--border-color);
  outline: none;
}

.adjustment-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  background: var(--primary-color);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.1s;
}

.adjustment-slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

/* Sidebar Styles */
.sidebar-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 0;
    border-bottom: 1px solid var(--border-color);
}

.group-label {
    font-size: 11px;
    font-weight: 500;
    color: var(--on-surface-variant);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Size Options */
.size-options {
    display: flex;
    gap: 8px;
    background: var(--surface-variant);
    padding: 4px;
    border-radius: 20px;
    width: fit-content;
}

.size-btn {
    width: 40px;
    height: 32px;
    border: none;
    background: transparent;
    border-radius: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--on-surface-variant);
    transition: all 0.2s;
}

.size-btn:hover {
    background: var(--border-color);
}

.size-btn.active {
    background: var(--primary-color);
    color: #ffffff;
}

.size-btn.active .size-shape {
    background-color: var(--surface-color);
}

.size-shape {
    background-color: var(--on-surface-variant);
    border-radius: 2px;
}

/* Color Grid */
.color-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    justify-items: center;
}

.color-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform 0.2s, border-color 0.2s;
    box-sizing: border-box;
}

.color-btn:hover {
    transform: scale(1.1);
}

.color-btn.active {
    border-color: var(--on-surface);
    transform: scale(1.1);
    box-shadow: 0 0 0 2px var(--surface-color);
}

/* Tool Buttons in Edit Toolbar */
.tool-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--surface-variant);
    color: var(--on-surface-variant);
    border: none;
    margin: 0 2px;
    pointer-events: auto;
    transition: background-color 0.2s, color 0.2s;
}

.tool-btn:hover {
    background: var(--border-color);
    color: var(--on-surface);
}

.tool-btn.active {
    background: var(--primary-color);
    color: #ffffff;
}

/* Save Button in Sidebar */
#viewer-save-split {
    background-color: var(--primary-color);
    color: #ffffff;
    width: 100%;
    justify-content: center;
    font-weight: 500;
}

#viewer-save-split:hover {
    background-color: var(--primary-variant);
}

.split-btn-container {
    width: 100%;
    display: flex;
    margin-bottom: 16px;
}

/* Split button inside toolbar: no margin, proper alignment */
.viewer-toolbar .split-btn-container {
    width: auto;
    margin-bottom: 0;
    align-items: center;
}

.split-btn-main {
    flex: 1;
    background-color: var(--primary-color);
    color: #ffffff;
    border: none;
    height: 36px;
    padding: 0 16px;
    border-radius: 4px 0 0 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 500;
    font-size: 14px;
    pointer-events: auto;
    transition: background-color 0.2s;
}

.split-btn-arrow {
    background-color: var(--primary-color);
    color: #ffffff;
    border: none;
    height: 36px;
    padding: 0 8px;
    border-radius: 0 4px 4px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-left: 1px solid rgba(255,255,255,0.2);
    pointer-events: auto;
    transition: background-color 0.2s;
}

.split-btn-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    background-color: var(--surface-variant);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    box-shadow: var(--shadow-md);
    display: none;
    flex-direction: column;
    min-width: 180px;
    z-index: 100;
    margin-top: 4px;
    pointer-events: auto;
}

.split-btn-item {
    background: none;
    border: none;
    color: var(--on-surface);
    cursor: pointer;
    padding: 8px 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    text-align: left;
    pointer-events: auto;
    font-size: 14px;
    transition: background-color 0.2s;
}

.split-btn-item:hover {
    background-color: var(--surface-variant);
}

/* ============================================================
   Fullscreen Mode Overrides (Windows 11 Photos-like behavior)
   ============================================================
   Uses the :fullscreen pseudo-class to target true browser
   fullscreen state. The image fills the entire viewport with
   a black background, and the toolbar becomes a transparent
   overlay that auto-hides.
   ============================================================ */

/* Force black background in fullscreen regardless of color scheme */
:fullscreen .fullscreen-viewer,
:fullscreen .viewer-content {
    background-color: #000000 !important;
}

:-webkit-full-screen .fullscreen-viewer,
:-webkit-full-screen .viewer-content {
    background-color: #000000 !important;
}

/* Toolbar becomes a transparent overlay in fullscreen */
:fullscreen .viewer-toolbar {
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: none;
    box-shadow: none;
    transition: opacity 0.3s ease;
}

:-webkit-full-screen .viewer-toolbar {
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: none;
    box-shadow: none;
    transition: opacity 0.3s ease;
}

/* Ensure .viewer-media fills the entire viewport in fullscreen */
:fullscreen .viewer-media {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

:-webkit-full-screen .viewer-media {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

/* Image fills the container using object-fit: contain for proper aspect ratio */
:fullscreen .viewer-media img {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    object-fit: contain;
}

:-webkit-full-screen .viewer-media img {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    object-fit: contain;
}

/* Ensure viewer-content has no padding in fullscreen */
:fullscreen .viewer-content {
    padding: 0 !important;
}

:-webkit-full-screen .viewer-content {
    padding: 0 !important;
}
