/* ========== Base Reset & Variables ========== */
:root {
  --bg: #0a0a0a;
  --surface: #1a1a1a;
  --surface-2: #2a2a2a;
  --text: #f0f0f0;
  --text-dim: #999;
  --accent: #4f8cff;
  --accent-hover: #6ea1ff;
  --danger: #ff4f6e;
  --success: #34d399;
  --radius: 12px;
  --radius-sm: 8px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ========== App Layout ========== */
#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.view {
  display: none;
  flex-direction: column;
  height: 100%;
}

.view.active {
  display: flex;
}

/* ========== Camera View ========== */
.camera-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(10px);
  z-index: 10;
}

.camera-header h1 {
  font-size: 18px;
  font-weight: 600;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  transition: background 0.2s;
}

.icon-btn:active {
  background: var(--surface-2);
}

.text-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 15px;
  cursor: pointer;
  padding: 4px 8px;
}

/* ========== Camera Container ========== */
.camera-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}

#camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.focus-ring {
  position: absolute;
  width: 80px;
  height: 80px;
  border: 2px solid var(--accent);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  animation: focus-pulse 0.4s ease-out;
}

@keyframes focus-pulse {
  0% { transform: translate(-50%, -50%) scale(1.5); opacity: 0.5; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

/* ========== Camera Controls ========== */
.camera-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 32px;
  padding: 24px 16px calc(24px + var(--safe-bottom));
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(10px);
}

.control-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
}

.control-btn.secondary {
  width: 52px;
  height: 52px;
  border-radius: var(--radius);
  background: var(--surface-2);
  overflow: hidden;
  transition: background 0.2s;
}

.control-btn.secondary:active {
  background: var(--surface);
}

/* Capture button */
.capture-btn {
  position: relative;
  width: 72px;
  height: 72px;
}

.capture-ring {
  width: 100%;
  height: 100%;
  border: 4px solid #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s;
}

.capture-inner {
  width: 56px;
  height: 56px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.1s, background 0.1s;
}

.capture-btn:active .capture-ring {
  transform: scale(0.95);
}

.capture-btn:active .capture-inner {
  transform: scale(0.85);
  background: #ddd;
}

.thumbnail {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--radius);
}

.fallback-upload {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-size: 16px;
  cursor: pointer;
}

/* ========== Preview View ========== */
.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--surface-2);
}

.preview-header h2 {
  font-size: 17px;
  font-weight: 600;
}

.preview-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  overflow: hidden;
  padding: 8px;
}

#preview-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--radius-sm);
}

/* ========== Preview Controls ========== */
.preview-controls {
  padding: 16px;
  background: var(--surface);
  border-top: 1px solid var(--surface-2);
  overflow-y: auto;
  max-height: 45vh;
}

.upload-config,
.upload-section {
  margin-bottom: 16px;
}

.config-item {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--text-dim);
}

.config-item span:first-child {
  min-width: 72px;
  flex-shrink: 0;
  color: var(--text);
  font-size: 13px;
}

.config-item input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.config-item select,
.config-item input[type="text"],
.config-item input[type="url"],
.config-item textarea {
  flex: 1;
  padding: 8px 12px;
  background: var(--surface-2);
  border: 1px solid #333;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  outline: none;
  transition: border-color 0.2s;
}

.config-item select:focus,
.config-item input:focus,
.config-item textarea:focus {
  border-color: var(--accent);
}

.config-item textarea {
  font-family: monospace;
  resize: vertical;
}

.url-input {
  min-width: 0;
}

.field-input {
  max-width: 120px;
}

/* Advanced config */
.advanced-config {
  margin-top: 4px;
}

.advanced-config summary {
  font-size: 13px;
  color: var(--accent);
  cursor: pointer;
  padding: 4px 0;
}

.advanced-body {
  padding-top: 8px;
}

/* Upload button */
.upload-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.upload-btn:hover {
  background: var(--accent-hover);
}

.upload-btn:active {
  transform: scale(0.98);
}

.upload-btn:disabled {
  background: #555;
  cursor: not-allowed;
  transform: none;
}

/* ========== Action Buttons ========== */
.action-buttons {
  display: flex;
  gap: 10px;
  width: 100%;
}

/* Save to desktop button */
.save-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px;
  background: var(--success);
  color: #000;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.save-btn:hover {
  background: #5ee8b7;
}

.save-btn:active {
  transform: scale(0.98);
}

.save-btn:disabled {
  background: #555;
  color: #999;
  cursor: not-allowed;
  transform: none;
}

/* ========== Connection Status Bar ========== */
.connection-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  margin-bottom: 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  font-size: 13px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #555;
}

.status-dot.online {
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
}

.status-dot.local {
  background: #f0b429;
  box-shadow: 0 0 6px #f0b429;
}

.save-dir {
  color: var(--text-dim);
  font-size: 11px;
  margin-left: auto;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Cloud mode status dot */
.status-dot.cloud {
  background: var(--accent);
  box-shadow: 0 0 6px var(--accent);
}

/* Cloud mode save button variant */
.save-btn.cloud-save {
  background: var(--accent);
  color: #fff;
}

.save-btn.cloud-save:hover {
  background: var(--accent-hover);
}

/* ========== Toast ========== */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  background: var(--surface-2);
  color: var(--text);
  border-radius: 20px;
  font-size: 14px;
  z-index: 100;
  animation: toast-in 0.3s ease-out;
  white-space: nowrap;
}

.toast.success {
  background: var(--success);
  color: #000;
}

.toast.error {
  background: var(--danger);
  color: #fff;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ========== Overlay ========== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.overlay-content {
  text-align: center;
  padding: 32px;
  background: var(--surface);
  border-radius: var(--radius);
  min-width: 240px;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid var(--surface-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#upload-progress {
  width: 100%;
  height: 6px;
  border: none;
  border-radius: 3px;
  margin-top: 12px;
  appearance: none;
  -webkit-appearance: none;
}

#upload-progress::-webkit-progress-bar {
  background: var(--surface-2);
  border-radius: 3px;
}

#upload-progress::-webkit-progress-value {
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.2s;
}

/* ========== Desktop adjustments ========== */
@media (min-width: 768px) {
  #app {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid #222;
    border-right: 1px solid #222;
  }

  .camera-controls {
    gap: 40px;
    padding: 28px 16px;
  }
}

/* ========== Landscape on mobile ========== */
@media (max-height: 500px) and (orientation: landscape) {
  .camera-controls {
    flex-direction: row;
    padding: 8px 16px;
    gap: 20px;
  }

  .capture-btn {
    width: 56px;
    height: 56px;
  }

  .capture-inner {
    width: 42px;
    height: 42px;
  }

  .control-btn.secondary {
    width: 40px;
    height: 40px;
  }
}