/* Reset and Base Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-gradient: linear-gradient(135deg, #0d1b2a 0%, #1b263b 50%, #415a77 100%);
  --text-primary: #f8f9fa;
  --text-secondary: #e0e1dd;
  --glass-bg: rgba(15, 23, 42, 0.55);
  --glass-border: rgba(255, 255, 255, 0.08);
  --glass-highlight: rgba(255, 255, 255, 0.12);
  --accent-blue: #00b4d8;
  --accent-green: #06d6a0;
  --accent-purple: #a2d2ff;
  --accent-coral: #ff7096;
  --shadow-main: 0 8px 32px 0 rgba(0, 0, 0, 0.4);
}

body {
  font-family: 'Quicksand', 'Noto Sans TC', sans-serif;
  background: var(--bg-gradient);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  user-select: none;
}

/* Immersive 3D Canvas Background */
#canvas-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1;
}

/* Header UI */
#main-header {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 40px;
  z-index: 10;
  pointer-events: none; /* Let clicks pass through to Three.js unless over button */
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 12px;
  text-shadow: 0 4px 10px rgba(0,0,0,0.5);
}

.logo-icon {
  font-size: 2rem;
  animation: float-logo 3s ease-in-out infinite;
}

#site-title {
  font-size: 1.8rem;
  font-weight: 800;
  background: linear-gradient(90deg, #90e0ef, #a2d2ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 1px;
}

.nav-controls {
  display: flex;
  gap: 15px;
  pointer-events: auto; /* Enable clicks */
}

/* Buttons */
.btn-glass {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: 50px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  backdrop-filter: blur(12px) saturate(180%);
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  box-shadow: 0 4px 15px rgba(0,0,0,0.25);
}

.btn-glass:hover {
  background: var(--glass-highlight);
  border-color: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(144, 224, 239, 0.25);
}

.btn-glass:active {
  transform: translateY(1px);
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent-blue) 0%, #0077b6 100%);
  color: #fff;
  border: none;
  padding: 12px 24px;
  border-radius: 30px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 15px rgba(0, 180, 216, 0.35);
}

.btn-primary:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(0, 180, 216, 0.5);
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--glass-border);
  color: var(--text-primary);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.2);
}

.btn-danger {
  background: linear-gradient(135deg, var(--accent-coral) 0%, #d90429 100%);
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 20px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 12px rgba(217, 4, 41, 0.3);
}

.btn-danger:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(217, 4, 41, 0.45);
}

/* Modals */
.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(8, 14, 30, 0.7);
  backdrop-filter: blur(8px);
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: opacity 0.4s ease;
}

.modal-card {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: 24px;
  padding: 35px;
  width: 400px;
  max-width: 90%;
  box-shadow: var(--shadow-main);
  backdrop-filter: blur(16px) saturate(180%);
  position: relative;
  text-align: center;
  transform: scale(1);
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal-close {
  position: absolute;
  top: 15px;
  right: 20px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.8rem;
  cursor: pointer;
  transition: color 0.2s;
}

.modal-close:hover {
  color: #fff;
}

.modal-card h3 {
  font-size: 1.4rem;
  margin-bottom: 10px;
  color: var(--accent-purple);
}

.modal-card p {
  font-size: 0.95rem;
  color: var(--text-secondary);
  margin-bottom: 25px;
}

.input-group {
  margin-bottom: 20px;
  text-align: left;
}

.input-group label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--accent-blue);
}

.input-group input[type="password"],
.input-group input[type="text"],
.input-group textarea,
select {
  width: 100%;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  color: #fff;
  font-family: inherit;
  font-size: 0.95rem;
  outline: none;
  transition: all 0.3s;
}

.input-group input:focus,
.input-group textarea:focus,
select:focus {
  border-color: var(--accent-blue);
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 3px rgba(0, 180, 216, 0.15);
}

.hint-text {
  display: block;
  font-size: 0.8rem;
  color: rgba(255,255,255,0.4);
  margin-top: 5px;
}

/* Modal animation state */
.modal-hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-hidden .modal-card,
.modal-hidden .storybook-card {
  transform: scale(0.9);
}

/* Storybook Styles */
.storybook-card {
  background: radial-gradient(circle at center, #1b263b 0%, #0d1b2a 100%);
  border: 4px solid var(--accent-purple);
  border-radius: 30px;
  width: 900px;
  height: 600px;
  max-width: 95%;
  max-height: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  position: relative;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.15);
}

.book-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.book-cover {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 40px;
  gap: 20px;
  z-index: 10;
  background: rgba(13, 27, 42, 0.8);
  width: 100%;
  height: 100%;
  transition: opacity 0.5s ease;
}

.book-cover h2 {
  font-size: 2.8rem;
  font-weight: 800;
  background: linear-gradient(135deg, #ffc8dd, #a2d2ff, #bde0fe);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 4px 15px rgba(0,0,0,0.4);
}

.book-cover p {
  color: var(--text-secondary);
  font-size: 1.2rem;
}

.book-page {
  width: 100%;
  height: 100%;
  display: flex;
  opacity: 1;
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.page-hidden {
  opacity: 0;
  pointer-events: none;
  position: absolute;
}

.page-layout {
  display: flex;
  width: 100%;
  height: 100%;
}

.page-visual {
  flex: 1;
  background: rgba(0,0,0,0.25);
  border-right: 1px solid rgba(255,255,255,0.05);
  position: relative;
}

#storybook-3d-renderer {
  width: 100%;
  height: 100%;
}

.page-text {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
}

.page-text h4 {
  font-size: 1.8rem;
  color: var(--accent-blue);
  margin-bottom: 20px;
  border-bottom: 2px dashed rgba(255,255,255,0.1);
  padding-bottom: 10px;
}

.page-text p {
  font-size: 1.1rem;
  line-height: 1.8;
  color: var(--text-secondary);
  flex-grow: 1;
  overflow-y: auto;
}

.page-navigation {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 30px;
}

#page-indicator {
  font-weight: 700;
  color: var(--accent-purple);
}

/* Admin Workshop Panel (Left Sidebar) */
#workshop-panel {
  position: absolute;
  top: 0;
  left: 0;
  height: 100vh;
  width: 380px;
  background: rgba(10, 18, 36, 0.85);
  border-right: 1px solid var(--glass-border);
  box-shadow: 8px 0 32px 0 rgba(0,0,0,0.5);
  backdrop-filter: blur(20px) saturate(180%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.workshop-collapsed {
  transform: translateX(-340px);
}

.workshop-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.workshop-header h2 {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--accent-blue);
}

#btn-workshop-toggle {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  color: var(--text-primary);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  transition: transform 0.4s, background-color 0.2s;
  pointer-events: auto;
}

#btn-workshop-toggle:hover {
  background: var(--glass-highlight);
}

.workshop-collapsed #btn-workshop-toggle {
  transform: rotate(180deg);
}

.workshop-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.panel-section {
  background: rgba(255,255,255,0.02);
  border: 1px solid rgba(255,255,255,0.03);
  padding: 18px;
  border-radius: 16px;
}

.panel-section h3 {
  font-size: 0.95rem;
  color: var(--accent-purple);
  margin-bottom: 12px;
  font-weight: 700;
}

/* Upload zone */
.upload-dropzone {
  border: 2px dashed rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: 30px 15px;
  text-align: center;
  cursor: pointer;
  background: rgba(255,255,255,0.01);
  transition: all 0.2s;
}

.upload-dropzone:hover, .upload-dropzone.dragover {
  border-color: var(--accent-blue);
  background: rgba(0, 180, 216, 0.05);
}

.icon-upload {
  font-size: 2.2rem;
  display: block;
  margin-bottom: 8px;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.3));
}

.upload-dropzone p {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.file-hidden {
  display: none;
}

/* Canvas Wrapper for crop/remove-bg */
.canvas-wrapper {
  width: 100%;
  max-height: 200px;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 15px;
  border: 1px solid rgba(255,255,255,0.1);
}

#crop-canvas {
  max-width: 100%;
  max-height: 198px;
  object-fit: contain;
}

/* Live 3D Preview inside Workshop */
.live-preview-container {
  width: 100%;
  height: 150px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.08);
  background: radial-gradient(circle at center, #1b263b 0%, #0d1b2a 100%);
  margin-top: 12px;
}

#live-3d-preview {
  width: 100%;
  height: 100%;
}

/* Control Group (Sliders) */
.control-group {
  margin-bottom: 14px;
}

.control-group label {
  display: flex;
  justify-content: space-between;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.control-group input[type="range"] {
  width: 100%;
  -webkit-appearance: none;
  background: rgba(255,255,255,0.1);
  height: 6px;
  border-radius: 3px;
  outline: none;
}

.control-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-blue);
  cursor: pointer;
  box-shadow: 0 0 10px rgba(0, 180, 216, 0.5);
  transition: transform 0.1s;
}

.control-group input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.value-display {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent-blue);
  float: right;
  margin-top: -24px;
}

/* Inspector Panel (Right/Bottom card for detail viewing) */
#inspector-panel {
  position: absolute;
  bottom: 30px;
  right: 30px;
  z-index: 40;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.inspector-hidden {
  transform: translateY(120%) scale(0.9);
  opacity: 0;
  pointer-events: none;
}

.inspector-card {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  box-shadow: var(--shadow-main);
  backdrop-filter: blur(20px) saturate(180%);
  border-radius: 24px;
  width: 480px;
  display: flex;
  overflow: hidden;
  height: 250px;
  position: relative;
}

.inspector-close {
  position: absolute;
  top: 10px;
  right: 15px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  cursor: pointer;
  z-index: 5;
}

#inspector-3d-view {
  width: 180px;
  height: 100%;
  background: rgba(0,0,0,0.3);
  border-right: 1px solid rgba(255,255,255,0.05);
}

.inspector-details {
  flex: 1;
  padding: 25px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  justify-content: space-between;
}

.inspector-details h2 {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--accent-purple);
  padding-right: 15px;
}

.story-scroll {
  flex: 1;
  overflow-y: auto;
}

#inspect-story {
  font-size: 0.95rem;
  line-height: 1.6;
  color: var(--text-secondary);
}

.inspect-actions {
  display: flex;
  gap: 10px;
}

/* Toast */
#toast {
  position: absolute;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(10, 18, 36, 0.9);
  border: 1px solid var(--accent-blue);
  color: #fff;
  padding: 12px 25px;
  border-radius: 50px;
  font-size: 0.95rem;
  z-index: 200;
  box-shadow: var(--shadow-main);
  backdrop-filter: blur(8px);
  transition: opacity 0.3s, bottom 0.3s;
}

.toast-hidden {
  opacity: 0;
  bottom: 10px;
  pointer-events: none;
}

/* Instruction Hint */
#instruction-hint {
  position: absolute;
  bottom: 25px;
  left: 30px;
  z-index: 10;
  pointer-events: none;
  animation: pulse-hint 2s infinite ease-in-out;
}

#instruction-hint p {
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.45);
  text-shadow: 0 2px 4px rgba(0,0,0,0.6);
}

/* Utility admin styles */
.hidden {
  display: none !important;
}

/* Animations */
@keyframes float-logo {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-6px) rotate(8deg); }
}

@keyframes pulse-hint {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 0.9; }
}

/* Custom Scrollbars */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Responsive adjustments */
@media (max-width: 768px) {
  #main-header {
    padding: 15px;
  }
  #site-title {
    font-size: 1.4rem;
  }
  .logo-icon {
    font-size: 1.6rem;
  }
  .nav-controls {
    gap: 8px;
  }
  .btn-glass {
    padding: 8px 14px;
    font-size: 0.85rem;
  }
  #workshop-panel {
    width: 100vw;
  }
  .workshop-collapsed {
    transform: translateX(-100vw);
  }
  #workshop-panel.workshop-collapsed #btn-workshop-toggle {
    left: auto;
    right: -40px;
  }
  #inspector-panel {
    left: 15px;
    right: 15px;
    bottom: 15px;
    width: auto;
  }
  .inspector-card {
    width: 100%;
    height: auto;
    flex-direction: column;
  }
  #inspector-3d-view {
    width: 100%;
    height: 140px;
    border-right: none;
    border-bottom: 1px solid rgba(255,255,255,0.05);
  }
  .inspector-details {
    padding: 15px;
  }
  .storybook-card {
    height: 95%;
  }
  .page-layout {
    flex-direction: column;
  }
  .page-visual {
    height: 40%;
    border-right: none;
    border-bottom: 1px solid rgba(255,255,255,0.05);
  }
  .page-text {
    padding: 20px;
}

/* --- Zen Mode (Picture Frame) --- */
body.zen-mode header,
body.zen-mode .nav-controls,
body.zen-mode .admin-only,
body.zen-mode #workshop-panel,
body.zen-mode #instruction-hint {
  opacity: 0 !important;
  pointer-events: none !important;
  transition: opacity 1s ease-in-out;
}

body header, body .nav-controls, body #workshop-panel, body #instruction-hint {
  transition: opacity 1s ease-in-out;
}

/* --- Gallery Grid for Historical Drawings --- */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  max-height: 180px;
  overflow-y: auto;
  padding: 4px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.2);
}

.gallery-item {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  aspect-ratio: 1;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gallery-item:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: var(--accent-blue);
  transform: scale(1.03);
}

.gallery-item img {
  width: 90%;
  height: 90%;
  object-fit: contain;
  border-radius: 4px;
}

.gallery-item-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(10, 18, 36, 0.8);
  color: var(--text-primary);
  font-size: 0.6rem;
  text-align: center;
  padding: 2px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

.gallery-item-delete {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(230, 57, 70, 0.9);
  color: white;
  border: none;
  border-radius: 4px;
  width: 18px;
  height: 18px;
  font-size: 0.65rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s ease;
  z-index: 10;
}

.gallery-item:hover .gallery-item-delete {
  opacity: 1;
}

.gallery-item-delete:hover {
  background: #ff4d6d;
  transform: scale(1.1);
}

