/* ==========================================
   1. BASE RESET & VIEWPORT FIXES
   ========================================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100dvh;
  width: 100vw;
  overflow-x: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: #f8fafc;
  color: #0f172a;
}

/* Visibility Utilities */
.hidden-element {
  display: none !important;
}

.hidden-view {
  display: none !important;
}

.active-view {
  display: block !important;
}

.view-section {
  min-height: 100dvh;
  padding-top: 80px; /* Room for top nav on desktop */
  padding-bottom: 40px;
  box-sizing: border-box;
}

/* ==========================================
   2. BRANDING & NAVIGATION
   ========================================== */
.brand-logo {
  height: 32px;
  width: auto;
  display: block;
  object-fit: contain;
}

.app-nav {
  position: fixed;
  top: 0; /* Pin to top for desktop */
  left: 0;
  right: 0;
  z-index: 1000;
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 24px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.nav-btn {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  color: #64748b;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 8px;
  min-height: 44px; /* Ensure safe touch target size */
  transition: color 0.2s, background-color 0.2s;
}

.nav-btn.active {
  color: #3b82f6;
  font-weight: 600;
  background-color: #eff6ff;
}

/* Align center navigation links horizontally */
.nav-links {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* Constrain all navigation SVG icons */
.nav-icon {
  width: 24px;
  height: 24px;
  display: block;
}

/* Fix Logout Button Flex Alignment */
.logout-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  padding: 6px 12px;
  background-color: white;
  border: 1px solid #d4d4d8;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  color: #3f3f46;
}

/* ==========================================
   3. AUTHENTICATION VIEW
   ========================================== */
.auth-background {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover;
  background-position: center;
  padding: 20px;
}

.auth-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(8px);
  padding: 32px;
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.2);
  text-align: center;
}

.auth-input {
  width: 100%;
  padding: 10px 14px;
  margin-bottom: 12px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 0.95rem;
  outline: none;
}

.auth-input:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.auth-btn {
  width: 100%;
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: opacity 0.2s;
}

.primary-btn {
  background-color: #3b82f6;
  color: white;
}

.secondary-btn {
  background-color: #ffffff;
  border: 1px solid #cbd5e1;
  color: #475569;
}

/* ==========================================
   4. SCAN WORKSPACE & CANVAS TOOLBAR
   ========================================== */
.scan-container {
  display: flex;
  gap: 20px;
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  align-items: flex-start;
}

.canvas-container {
  position: relative;
  flex: 1;
  min-width: 0;
  background: #0f172a;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 450px;
}

#shelfCanvas {
  max-width: 100%;
  height: auto;
  display: block;
}

.save-shelf-btn {
  display: block;
  width: 100%;
}

/* Crop Button Overlay */
.crop-canvas-btn {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 20;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 6px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s, transform 0.1s;
}

.crop-canvas-btn:hover {
  background: #f8fafc;
}

.crop-canvas-btn:active {
  transform: scale(0.95);
}

.crop-icon {
  width: 20px;
  height: 20px;
  display: block;
}

.dropzone-empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: #94a3b8;
  text-align: center;
  width: 100%;
}

/* Sidebar & Detected Spines */
#pendingContainer {
  width: 380px;
  background: #ffffff;
  border-radius: 12px;
  padding: 16px;
  border: 1px solid #e2e8f0;
}

.maximize-spines-btn {
  display: none;
}

/* ==========================================
   5. MAP ENGINE & OVERLAYS
   ========================================== */
#infiniteMap {
  width: 100vw;
  height: calc(100dvh - 80px);
  position: relative;
  overflow: hidden;
  background-color: #f1f5f9;
  cursor: grab;
}

#mapViewport {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

/* Sidebar toggle chevron */
.toggle-icon {
  width: 24px;
  height: 24px;
  display: block;
  transition: transform 0.3s ease;
}

.book-popover {
  position: absolute;
  top: -45px;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: white;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 100;
  pointer-events: auto;
  white-space: nowrap;
}

#libraryView {
  position: relative;
}

/* Floating Desktop Sidebar */
#libraryView .sidebar {
  position: absolute;
  top: 100px; 
  right: 20px;
  width: 320px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(8px);
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  z-index: 50;
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 140px);
}

.sidebar-header {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e2e8f0;
  cursor: default;
}

.sidebar-content {
  padding: 0 16px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
}

/* Sidebar Search & List Styles */
.search-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.9rem;
  outline: none;
}

.search-input:focus {
  border-color: #3b82f6;
}

.library-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
}

/* Sidebar toggle chevron */
.toggle-icon {
  width: 24px;
  height: 24px;
  display: block;
  transition: transform 0.3s ease;
}

/* ==========================================
   6. MODALS & LOADING OVERLAYS
   ========================================== */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100dvh;
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  color: white;
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: #3b82f6;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Shelf Manager Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100dvh;
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 16px;
}

.modal-content {
  background: #ffffff;
  padding: 24px;
  border-radius: 12px;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

/* Modal List Item Styling */
.manager-list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #e2e8f0;
}

.manager-list-item:last-child {
  border-bottom: none;
}

/* ==========================================
   7. MOBILE RESPONSIVE RULES
   ========================================== */
@media (max-width: 768px) {
  /* Shift safe area padding from top to bottom for mobile */
  .view-section {
    padding-top: 20px;
    padding-bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  }

  /* Move Navigation to Bottom */
  .app-nav {
    top: auto; 
    bottom: 0; 
    border-bottom: none;
    border-top: 1px solid #e2e8f0;
    justify-content: space-around;
    padding: 8px 12px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  }
  
  .brand-logo {
    height: 28px;
  }

  .scan-container,
  .scan-workspace {
    flex-direction: column;
    gap: 12px;
    padding: 12px;
  }

  .canvas-container,
  #shelfCanvas {
    max-height: 35vh !important;
    width: 100% !important;
    object-fit: contain;
    margin: 0 auto;
  }

  #pendingContainer {
    width: 100%;
    max-height: 45vh;
    overflow-y: auto;
    padding-bottom: 20px;
  }

  /* Full-screen Mobile Overlay when Maximized */
  #pendingContainer.maximized {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh !important;
    z-index: 500;
    background: #ffffff;
    padding: 16px;
    padding-bottom: calc(100px + env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    box-sizing: border-box;
  }

  .maximize-spines-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  .nav-brand, 
  #userEmailDisplay {
    display: none !important;
  }

  /* Mobile Library Sidebar Drawer */
#libraryView .sidebar {
    position: fixed; /* Locks the drawer to the screen, not the page */
    top: auto;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px)); 
    right: 12px;
    left: 12px;
    width: auto;
    border-radius: 16px 16px 0 0;
    z-index: 900;
    box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.1);
  }
  
  .sidebar-header {
    cursor: pointer;
  }
  
  /* Collapsible State Handled by app.js */
  .sidebar-content.collapsed {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
    overflow: hidden;
    border: none;
  }
  
  .sidebar-header.collapsed .toggle-icon {
    transform: rotate(180deg);
  }
}