* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-dark: #0a0514;
  --bg-card: #1a0a2e;
  --purple: #7b2ff7;
  --gold: #d4af37;
  --gold-light: #f5e6a3;
  --text: #f0e6ff;
  --text-dim: #8b7fa8;
  --danger: #ff4757;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg-dark);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100%;
  position: relative;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.screen.active {
  opacity: 1;
  pointer-events: all;
}

/* Login Screen */
#screen-login {
  align-items: center;
  justify-content: center;
}

.login-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.login-input {
  width: 240px;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid rgba(212, 175, 55, 0.3);
  background: var(--bg-card);
  color: var(--text);
  font-size: 16px;
  text-align: center;
  outline: none;
  transition: border-color 0.2s;
}

.login-code {
  -webkit-text-security: disc;
  text-security: disc;
}

.login-input:focus {
  border-color: var(--gold);
}

.login-btn {
  flex: none;
  width: 240px;
  padding: 12px 32px;
  font-size: 14px;
  margin-top: 24px;
}

/* Camera Screen */
.camera-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}

#camera-feed {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.wand-corners {
  position: absolute;
  inset: 20px;
  border: 2px solid rgba(212, 175, 55, 0.3);
  border-radius: 12px;
}

.wand-corners::before,
.wand-corners::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border-color: var(--gold);
  border-style: solid;
}

.wand-corners::before {
  top: -2px;
  left: -2px;
  border-width: 3px 0 0 3px;
  border-radius: 8px 0 0 0;
}

.wand-corners::after {
  bottom: -2px;
  right: -2px;
  border-width: 0 3px 3px 0;
  border-radius: 0 0 8px 0;
}

/* Theme selector */
.theme-selector {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  background: var(--bg-dark);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.theme-selector::-webkit-scrollbar {
  display: none;
}

.theme-btn {
  flex-shrink: 0;
  padding: 8px 14px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.theme-btn.active {
  border-color: var(--gold);
  background: rgba(212, 175, 55, 0.15);
  color: var(--gold);
}

.camera-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 30px;
  padding-bottom: calc(20px + var(--safe-bottom));
  background: var(--bg-dark);
}

.btn-icon {
  width: 44px;
  height: 44px;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-icon svg {
  width: 22px;
  height: 22px;
}

.btn-capture {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 4px solid var(--gold);
  background: transparent;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.btn-capture:active {
  transform: scale(0.92);
}

.capture-ring {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--gold);
  display: block;
  transition: transform 0.1s ease;
}

.btn-capture:active .capture-ring {
  transform: scale(0.9);
  background: var(--gold-light);
}

.spacer {
  width: 44px;
}

/* Capture preview + magic effect */
.capture-preview {
  position: absolute;
  inset: 0;
  z-index: 100;
  pointer-events: none;
  animation: preview-lifecycle 1.8s ease-out forwards;
}

.capture-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  animation: preview-freeze 1.8s ease-out forwards;
}

.capture-magic {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.capture-magic::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 200%;
  height: 200%;
  transform: translate(-50%, -50%);
  background: conic-gradient(
    from 0deg,
    transparent 0deg,
    rgba(212, 175, 55, 0.6) 40deg,
    transparent 80deg,
    rgba(123, 47, 247, 0.4) 160deg,
    transparent 200deg,
    rgba(212, 175, 55, 0.5) 280deg,
    transparent 320deg
  );
  animation: magic-spin 1.8s ease-out forwards;
  opacity: 0;
}

.capture-magic::after {
  content: '';
  position: absolute;
  inset: 0;
  background: 
    radial-gradient(circle at 20% 30%, rgba(212, 175, 55, 0.8) 0%, transparent 8%),
    radial-gradient(circle at 75% 20%, rgba(245, 230, 163, 0.7) 0%, transparent 6%),
    radial-gradient(circle at 50% 60%, rgba(212, 175, 55, 0.9) 0%, transparent 10%),
    radial-gradient(circle at 30% 80%, rgba(245, 230, 163, 0.6) 0%, transparent 5%),
    radial-gradient(circle at 80% 70%, rgba(212, 175, 55, 0.7) 0%, transparent 7%),
    radial-gradient(circle at 60% 15%, rgba(123, 47, 247, 0.6) 0%, transparent 6%),
    radial-gradient(circle at 15% 55%, rgba(245, 230, 163, 0.8) 0%, transparent 4%),
    radial-gradient(circle at 85% 45%, rgba(212, 175, 55, 0.7) 0%, transparent 5%);
  animation: magic-particles 1.8s ease-out forwards;
  opacity: 0;
}

@keyframes preview-lifecycle {
  0% { opacity: 1; }
  75% { opacity: 1; }
  100% { opacity: 0; }
}

@keyframes preview-freeze {
  0% { filter: brightness(2) saturate(0.5); }
  10% { filter: brightness(1) saturate(1); }
  60% { filter: brightness(0.7) saturate(0.6) sepia(0.3); transform: scale(1); }
  100% { filter: brightness(0.3) saturate(0) sepia(0.5); transform: scale(0.9); }
}

@keyframes magic-spin {
  0% { opacity: 0; transform: translate(-50%, -50%) rotate(0deg) scale(0.3); }
  15% { opacity: 0.8; }
  50% { opacity: 0.6; transform: translate(-50%, -50%) rotate(180deg) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) rotate(360deg) scale(1.5); }
}

@keyframes magic-particles {
  0% { opacity: 0; transform: scale(0.5); }
  20% { opacity: 1; transform: scale(1); }
  60% { opacity: 0.8; transform: scale(1.1); }
  100% { opacity: 0; transform: scale(1.3); }
}

/* Toast notification */
.toast {
  position: fixed;
  bottom: calc(100px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-card);
  color: var(--gold);
  padding: 12px 24px;
  border-radius: 16px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  z-index: 200;
  border: 1px solid rgba(212, 175, 55, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  white-space: pre-line;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Loading Screen */
.loading-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.wand-animation {
  position: relative;
  width: 120px;
  height: 120px;
  margin-bottom: 40px;
}

.wand {
  position: absolute;
  bottom: 10px;
  left: 50%;
  width: 4px;
  height: 80px;
  background: linear-gradient(to top, #5c3a1e, #8b6914, var(--gold));
  border-radius: 2px;
  transform: translateX(-50%) rotate(-20deg);
  transform-origin: bottom center;
  animation: wand-wave 2s ease-in-out infinite;
}

.sparkles {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}

.sparkles span {
  position: absolute;
  width: 6px;
  height: 6px;
  background: var(--gold-light);
  border-radius: 50%;
  animation: sparkle 1.5s ease-in-out infinite;
  box-shadow: 0 0 10px var(--gold), 0 0 20px var(--gold);
}

.sparkles span:nth-child(1) { left: -20px; top: 10px; animation-delay: 0s; }
.sparkles span:nth-child(2) { left: 10px; top: -5px; animation-delay: 0.2s; }
.sparkles span:nth-child(3) { left: 25px; top: 15px; animation-delay: 0.4s; }
.sparkles span:nth-child(4) { left: -10px; top: -15px; animation-delay: 0.6s; }
.sparkles span:nth-child(5) { left: 0px; top: 20px; animation-delay: 0.8s; }
.sparkles span:nth-child(6) { left: 15px; top: -10px; animation-delay: 1.0s; }

@keyframes wand-wave {
  0%, 100% { transform: translateX(-50%) rotate(-20deg); }
  50% { transform: translateX(-50%) rotate(10deg); }
}

@keyframes sparkle {
  0%, 100% { opacity: 0; transform: scale(0); }
  50% { opacity: 1; transform: scale(1.5); }
}

.loading-text {
  font-size: 22px;
  font-weight: 600;
  color: var(--gold);
  margin-bottom: 8px;
  letter-spacing: 0.5px;
}

.loading-subtext {
  font-size: 14px;
  color: var(--text-dim);
}

/* Result Screen */
.result-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
  overflow: hidden;
}

#result-image {
  max-width: 100%;
  max-height: 100%;
  border-radius: 12px;
  box-shadow: 0 8px 40px rgba(123, 47, 247, 0.3), 0 0 80px rgba(212, 175, 55, 0.1);
  object-fit: contain;
}

.result-controls {
  display: flex;
  gap: 16px;
  padding: 20px 24px;
  padding-bottom: calc(20px + var(--safe-bottom));
  background: var(--bg-dark);
}

.btn {
  flex: 1;
  padding: 16px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: linear-gradient(135deg, var(--purple), #9b59b6);
  color: white;
  box-shadow: 0 4px 15px rgba(123, 47, 247, 0.4);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

/* Error Screen */
.error-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  text-align: center;
}

.error-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

#error-message {
  font-size: 16px;
  color: var(--text-dim);
  margin-bottom: 30px;
  line-height: 1.5;
}

#screen-error .btn {
  flex: none;
  width: auto;
  padding: 16px 48px;
}

/* Gallery Screen */
.gallery-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  padding-top: calc(16px + var(--safe-top));
  gap: 12px;
}

.gallery-header h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--gold);
}

.gallery-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: min-content;
  gap: 3px;
  padding: 3px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  align-content: start;
}

.gallery-item {
  position: relative;
  border-radius: 4px;
  cursor: pointer;
  overflow: hidden;
}

.gallery-item::before {
  content: '';
  display: block;
  padding-top: 150%;
}

.gallery-item img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.2s ease;
}

.gallery-item:active img {
  transform: scale(0.95);
}

.gallery-item-error {
  background: var(--bg-card);
  border: 1px solid rgba(255, 71, 87, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
}

.gallery-item-error .error-badge {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 28px;
}

.gallery-item-pending {
  background: var(--bg-card);
  border: 1px solid rgba(212, 175, 55, 0.2);
}

.gallery-item-pending .pending-animation {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 50px;
  height: 50px;
  margin-top: -25px;
  margin-left: -25px;
}

.gallery-item-pending .wand {
  position: absolute;
  bottom: 5px;
  left: 50%;
  width: 3px;
  height: 35px;
  background: linear-gradient(to top, #5c3a1e, var(--gold));
  border-radius: 2px;
  transform: translateX(-50%) rotate(-20deg);
  transform-origin: bottom center;
  animation: wand-wave 2s ease-in-out infinite;
}

.gallery-item-pending .sparkles {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}

.gallery-item-pending .sparkles span {
  position: absolute;
  width: 4px;
  height: 4px;
  background: var(--gold-light);
  border-radius: 50%;
  animation: sparkle 1.5s ease-in-out infinite;
  box-shadow: 0 0 6px var(--gold);
}

.gallery-item-pending .sparkles span:nth-child(1) { left: -8px; top: 5px; animation-delay: 0s; }
.gallery-item-pending .sparkles span:nth-child(2) { left: 5px; top: -3px; animation-delay: 0.3s; }
.gallery-item-pending .sparkles span:nth-child(3) { left: 12px; top: 8px; animation-delay: 0.6s; }

.gallery-item-name {
  position: absolute;
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 90%;
}

.gallery-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 16px;
}

/* Gallery Detail Screen */
.detail-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
  overflow: hidden;
  position: relative;
}

#detail-image {
  max-width: 100%;
  max-height: 100%;
  border-radius: 12px;
  object-fit: contain;
  box-shadow: 0 8px 40px rgba(123, 47, 247, 0.3);
}

.detail-taken-by {
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: var(--text-dim);
  background: rgba(0, 0, 0, 0.6);
  padding: 4px 12px;
  border-radius: 10px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* Reprocess Modal */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-content {
  background: var(--bg-card);
  border-radius: 16px;
  padding: 24px;
  width: 85%;
  max-width: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  border: 1px solid rgba(212, 175, 55, 0.2);
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--gold);
}

.modal-themes {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

.modal-theme-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid rgba(212, 175, 55, 0.15);
  background: rgba(212, 175, 55, 0.05);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, border-color 0.2s;
  width: 100%;
  text-align: left;
}

.modal-theme-btn:active {
  background: rgba(212, 175, 55, 0.15);
  border-color: var(--gold);
}

.modal-theme-btn .theme-icon {
  font-size: 24px;
}

.modal-cancel {
  margin-top: 4px;
  flex: none;
  width: 100%;
}

.detail-controls {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  background: var(--bg-dark);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  flex-shrink: 0;
}

.detail-controls::-webkit-scrollbar {
  display: none;
}

.detail-controls .btn {
  flex: none;
  white-space: nowrap;
  padding: 12px 18px;
  font-size: 14px;
}

.btn-reprocess {
  background: rgba(212, 175, 55, 0.1);
  color: var(--gold);
  border: 1px solid rgba(212, 175, 55, 0.3);
}

.btn-danger {
  background: rgba(255, 71, 87, 0.15);
  color: var(--danger);
  border: 1px solid rgba(255, 71, 87, 0.3);
}
