:root {
  --bg-app: #0c0d10;
  --bg-card: #14151a;
  --bg-subtle: #1c1e24;
  --bg-hover: #242730;

  --border: #232630;
  --border-focus: #3b82f6;

  --text-main: #f4f5f8;
  --text-muted: #8c91a0;
  --text-dim: #5a5f6e;

  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-text: #ffffff;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  --font-sans: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'IBM Plex Mono', monospace;

  color-scheme: dark;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body {
  background-color: var(--bg-app);
  color: var(--text-main);
  font-family: var(--font-sans);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  overflow-x: hidden;
  line-height: 1.4;
  padding: env(safe-area-inset-top, 12px) 16px env(safe-area-inset-bottom, 16px) 16px;
}

.app-layout {
  width: 100%;
  max-width: 420px;
  min-height: calc(100vh - env(safe-area-inset-top, 12px) - env(safe-area-inset-bottom, 16px));
  display: flex;
  flex-direction: column;
}

/* Nav & Tool Switcher */
.app-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0 12px 0;
  gap: 8px;
}

.tool-switch {
  display: flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 3px;
  gap: 3px;
  flex: 1;
}

.tool-tab {
  flex: 1;
  background: transparent;
  border: none;
  border-radius: calc(var(--radius-md) - 3px);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  padding: 6px 0;
  cursor: pointer;
  transition: all 0.15s ease;
}

.tool-tab.active {
  background: var(--bg-subtle);
  color: var(--text-main);
  font-weight: 600;
}

.nav-reset-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-reset-btn:active {
  background: var(--bg-hover);
  color: var(--text-main);
}

/* Main */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.section-pane {
  display: none;
  flex: 1;
  flex-direction: column;
  gap: 12px;
}

.section-pane.active {
  display: flex;
}

/* Centered Upload Wrap */
.upload-center-wrap, .loading-center-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 0;
}

.drop-card {
  position: relative;
  width: 100%;
  background: var(--bg-card);
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 48px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.drop-card:hover, .drop-card.dragover {
  border-color: var(--accent);
  background-color: var(--bg-subtle);
}

.drop-card input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.drop-icon-box {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
}

.drop-text h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-main);
  margin-bottom: 4px;
}

.drop-text p {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 22px;
}

.select-btn {
  pointer-events: none;
  padding: 10px 24px;
  font-size: 13px;
}

/* Loading Box */
.loading-card {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 60px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 16px;
}

.loader-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.loading-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
}

/* View Switcher */
.view-switch {
  display: flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 3px;
  gap: 3px;
}

.view-tab {
  flex: 1;
  background: transparent;
  border: none;
  border-radius: calc(var(--radius-md) - 3px);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 500;
  padding: 7px 0;
  cursor: pointer;
  transition: all 0.15s ease;
}

.view-tab.active {
  background: var(--bg-subtle);
  color: var(--text-main);
}

/* Stage Frames */
.stage-frame, .eraser-stage-container, .edit-stage-container {
  position: relative;
  width: 100%;
  height: 300px;
  border-radius: var(--radius-lg);
  background: #09090b;
  border: 1px solid var(--border);
  overflow: hidden;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.eraser-canvas-wrapper, .edit-canvas-wrapper {
  position: relative;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.eraser-canvas-wrapper canvas, .edit-canvas-wrapper canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.brush-cursor {
  position: absolute;
  border: 1.5px solid rgba(255, 255, 255, 0.9);
  background: rgba(239, 68, 68, 0.25);
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%);
  display: none;
  z-index: 20;
}

/* ==========================================
   INTERACTIVE CROP ENGINE OVERLAY
   ========================================== */
.interactive-crop-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  touch-action: none;
}

.crop-backdrop-top, .crop-backdrop-bottom, .crop-backdrop-left, .crop-backdrop-right {
  position: absolute;
  background: rgba(0, 0, 0, 0.6);
  pointer-events: none;
}

.interactive-crop-box {
  position: absolute;
  border: 1.5px solid #ffffff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6), inset 0 0 0 1px rgba(0, 0, 0, 0.4);
  cursor: move;
  touch-action: none;
}

.crop-grid-3x3 {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.grid-line-h {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.3);
}
.grid-line-h.h1 { top: 33.33%; }
.grid-line-h.h2 { top: 66.66%; }

.grid-line-v {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(255, 255, 255, 0.3);
}
.grid-line-v.v1 { left: 33.33%; }
.grid-line-v.v2 { left: 66.66%; }

/* Crop Handles */
.crop-handle {
  position: absolute;
  width: 16px;
  height: 16px;
  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.4);
  border-radius: 2px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.5);
  z-index: 40;
}

.handle-nw { top: -8px; left: -8px; cursor: nwse-resize; }
.handle-ne { top: -8px; right: -8px; cursor: nesw-resize; }
.handle-sw { bottom: -8px; left: -8px; cursor: nesw-resize; }
.handle-se { bottom: -8px; right: -8px; cursor: nwse-resize; }

.handle-n { top: -8px; left: calc(50% - 8px); cursor: ns-resize; width: 16px; height: 8px; border-radius: 4px; }
.handle-s { bottom: -8px; left: calc(50% - 8px); cursor: ns-resize; width: 16px; height: 8px; border-radius: 4px; }
.handle-w { left: -8px; top: calc(50% - 8px); cursor: ew-resize; height: 16px; width: 8px; border-radius: 4px; }
.handle-e { right: -8px; top: calc(50% - 8px); cursor: ew-resize; height: 16px; width: 8px; border-radius: 4px; }

.crop-actions-row {
  display: flex;
  gap: 8px;
}

.matte-layer {
  position: absolute;
  inset: 0;
  transition: background-color 0.15s ease;
}

.checkerboard, .checker-mini {
  background-image:
    linear-gradient(45deg, #1d1f27 25%, transparent 25%),
    linear-gradient(-45deg, #1d1f27 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1d1f27 75%),
    linear-gradient(-45deg, transparent 75%, #1d1f27 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
  background-color: #131419;
}

.checker-mini {
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0px;
}

.stage-render {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  padding: 10px;
}

.split-curtain {
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  overflow: hidden;
  border-right: 1.5px solid rgba(255, 255, 255, 0.9);
}

.split-curtain .source-render {
  width: 420px;
  max-width: none;
}

.split-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 0;
  cursor: ew-resize;
  z-index: 10;
}

.handle-grip {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #ffffff;
  border: 1px solid rgba(0,0,0,0.2);
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

/* Control Card & Panels */
.control-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.control-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
}

.color-label {
  color: var(--text-muted);
}

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

.btn-text {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.btn-text:active {
  opacity: 0.7;
}

.slider-row {
  display: flex;
  align-items: center;
}

.slider-row input[type="range"], .adjust-slider-group input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.palette-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.palette-btn {
  background: transparent;
  border: none;
  padding: 2px;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--border);
  display: inline-block;
  transition: transform 0.12s ease;
}

.palette-btn.active .swatch {
  transform: scale(1.15);
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--bg-app), 0 0 0 4px var(--accent);
}

.custom-color-swatch {
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.custom-color-swatch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.custom-swatch-icon {
  background: var(--bg-subtle);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Edit Studio Specific Styles */
.edit-subtool-nav {
  display: flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 3px;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}
.edit-subtool-nav::-webkit-scrollbar { display: none; }

.subtool-btn {
  flex: 1;
  background: transparent;
  border: none;
  border-radius: calc(var(--radius-md) - 3px);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  padding: 6px 8px;
  white-space: nowrap;
  cursor: pointer;
  transition: all 0.15s ease;
}

.subtool-btn.active {
  background: var(--bg-subtle);
  color: var(--text-main);
  font-weight: 600;
}

.subtool-panel {
  display: none;
  flex-direction: column;
  gap: 10px;
}

.subtool-panel.active {
  display: flex;
}

.adjust-slider-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.slider-row-header {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
}

.mono-val {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
}

/* Filter Pills Grid */
.filter-pills-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.filter-pill {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 11px;
  padding: 7px 4px;
  cursor: pointer;
  text-align: center;
  transition: all 0.12s ease;
}

.filter-pill.active {
  border-color: var(--accent);
  background: var(--bg-hover);
  color: var(--text-main);
  font-weight: 600;
}

/* Transform Grid */
.transform-buttons-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.transform-btn {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  cursor: pointer;
  transition: background-color 0.12s ease;
}

.transform-btn:active {
  background: var(--bg-hover);
}

/* Responsive Resize Panel (No Overflow) */
.resize-inputs-grid {
  display: flex;
  align-items: center;
  gap: 8px;
}

.resize-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field-label {
  font-size: 11px;
  color: var(--text-dim);
}

.resize-field input {
  width: 100%;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  color: var(--text-main);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 13px;
  font-family: var(--font-mono);
  outline: none;
  box-sizing: border-box;
}

.resize-field input:focus {
  border-color: var(--border-focus);
}

.resize-sep {
  color: var(--text-dim);
  font-size: 14px;
  margin-top: 16px;
  flex-shrink: 0;
}

.resize-presets-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
}

.preset-btn {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 11px;
  padding: 6px 2px;
  cursor: pointer;
  text-align: center;
  min-width: 0;
}

.preset-btn:active {
  background: var(--bg-hover);
  color: var(--text-main);
}

/* Crop Panel */
.crop-aspects-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}

.crop-btn {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 11px;
  padding: 7px 2px;
  cursor: pointer;
  text-align: center;
}

.crop-btn.active {
  border-color: var(--accent);
  color: var(--text-main);
  background: var(--bg-hover);
  font-weight: 600;
}

/* Action Grid */
.action-grid {
  display: flex;
  gap: 8px;
}

.flex-1 {
  flex: 1;
}

.btn-primary {
  background-color: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: var(--radius-md);
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.08s ease;
}

.btn-primary:active {
  background-color: var(--accent-hover);
  transform: scale(0.98);
}

.btn-secondary {
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-main);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.08s ease;
}

.btn-secondary:active {
  background-color: var(--bg-hover);
  transform: scale(0.98);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(24px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-main);
  font-size: 12px;
  font-weight: 500;
  padding: 7px 16px;
  border-radius: 9999px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s ease;
  z-index: 99;
}

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