:root {
  /* Light mode colors */
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-tertiary: #e9ecef;
  --text-primary: #212529;
  --text-secondary: #6c757d;
  --text-tertiary: #adb5bd;
  --border-color: #dee2e6;
  --accent-primary: #0066ff;
  --accent-hover: #0052cc;
  --accent-light: #e6f0ff;
  --success: #28a745;
  --danger: #dc3545;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.15);
  --crop-overlay: rgba(0, 0, 0, 0.5);
}

[data-theme="dark"] {
  /* Dark mode colors */
  --bg-primary: #1a1a1a;
  --bg-secondary: #2d2d2d;
  --bg-tertiary: #3d3d3d;
  --text-primary: #ffffff;
  --text-secondary: #b0b0b0;
  --text-tertiary: #808080;
  --border-color: #404040;
  --accent-primary: #3d8eff;
  --accent-hover: #5c9fff;
  --accent-light: #1a3a5c;
  --success: #4ade80;
  --danger: #f87171;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.5);
  --crop-overlay: rgba(0, 0, 0, 0.7);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  transition: background-color 0.3s ease, color 0.3s ease;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 0;
  border-bottom: 1px solid var(--border-color);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--accent-primary);
}

.logo h1 {
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
}

.theme-toggle {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
  transition: all 0.3s ease;
}

.theme-toggle:hover {
  background: var(--bg-tertiary);
  transform: scale(1.05);
}

.theme-toggle .sun-icon {
  display: none;
}

.theme-toggle .moon-icon {
  display: block;
}

[data-theme="dark"] .theme-toggle .sun-icon {
  display: block;
}

[data-theme="dark"] .theme-toggle .moon-icon {
  display: none;
}

/* Main Content */
.main-content {
  flex: 1;
  padding: 40px 0;
}

/* Upload Section */
.upload-section {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 500px;
}

.upload-area {
  width: 100%;
  max-width: 600px;
  padding: 60px 40px;
  border: 3px dashed var(--border-color);
  border-radius: 16px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: var(--bg-secondary);
}

.upload-area:hover {
  border-color: var(--accent-primary);
  background: var(--accent-light);
  transform: translateY(-2px);
}

.upload-area.dragover {
  border-color: var(--accent-primary);
  background: var(--accent-light);
  transform: scale(1.02);
}

.upload-area svg {
  color: var(--accent-primary);
  margin-bottom: 20px;
}

.upload-area h2 {
  font-size: 24px;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.upload-area p {
  color: var(--text-secondary);
  font-size: 16px;
  margin-bottom: 20px;
}

.supported-formats {
  display: inline-block;
  padding: 8px 16px;
  background: var(--bg-primary);
  border-radius: 20px;
  font-size: 14px;
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

/* Editor Section */
.editor-section {
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.editor-layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 30px;
  align-items: start;
}

/* Canvas Container */
.canvas-container {
  background: var(--bg-secondary);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow-md);
  position: relative;
}

.canvas-wrapper {
  position: relative;
  width: 100%;
  min-height: 400px;
  background: repeating-conic-gradient(var(--bg-tertiary) 0% 25%, var(--bg-secondary) 0% 50%) 50% / 20px 20px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#imageCanvas {
  max-width: 100%;
  max-height: 600px;
  display: block;
  border-radius: 4px;
}

.canvas-info {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
  font-size: 14px;
  color: var(--text-secondary);
  gap: 20px;
}

/* Crop Overlay */
.crop-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: transparent;
  cursor: move;
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.crop-box {
  position: absolute;
  border: 2px solid var(--accent-primary);
  background: transparent;
  box-shadow: 0 0 0 9999px var(--crop-overlay);
  cursor: move;
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.crop-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: var(--accent-primary);
  border: 2px solid white;
  border-radius: 50%;
  z-index: 10;
  pointer-events: auto;
  touch-action: none;
}

/* Larger touch targets for mobile */
@media (max-width: 768px) {
  .crop-handle {
    width: 20px;
    height: 20px;
  }

  .crop-handle.tl {
    top: -10px;
    left: -10px;
  }

  .crop-handle.tr {
    top: -10px;
    right: -10px;
  }

  .crop-handle.bl {
    bottom: -10px;
    left: -10px;
  }

  .crop-handle.br {
    bottom: -10px;
    right: -10px;
  }

  .crop-handle.t {
    top: -10px;
  }

  .crop-handle.b {
    bottom: -10px;
  }

  .crop-handle.l {
    left: -10px;
  }

  .crop-handle.r {
    right: -10px;
  }
}

.crop-handle.tl {
  top: -6px;
  left: -6px;
  cursor: nwse-resize;
}

.crop-handle.tr {
  top: -6px;
  right: -6px;
  cursor: nesw-resize;
}

.crop-handle.bl {
  bottom: -6px;
  left: -6px;
  cursor: nesw-resize;
}

.crop-handle.br {
  bottom: -6px;
  right: -6px;
  cursor: nwse-resize;
}

.crop-handle.t {
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  cursor: ns-resize;
}

.crop-handle.b {
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  cursor: ns-resize;
}

.crop-handle.l {
  left: -6px;
  top: 50%;
  transform: translateY(-50%);
  cursor: ew-resize;
}

.crop-handle.r {
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  cursor: ew-resize;
}

/* Control Panel */
.control-panel {
  background: var(--bg-secondary);
  border-radius: 16px;
  padding: 24px;
  box-shadow: var(--shadow-md);
  position: sticky;
  top: 20px;
}

.control-group {
  margin-bottom: 28px;
  padding-bottom: 28px;
  border-bottom: 1px solid var(--border-color);
}

.control-group:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.control-group h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-primary);
}

/* Buttons */
.btn {
  width: 100%;
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 10px;
}

.btn:last-child {
  margin-bottom: 0;
}

.btn-primary {
  background: var(--accent-primary);
  color: white;
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-secondary {
  background: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

.btn-secondary:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent-primary);
}

.btn-toggle {
  background: var(--accent-light);
  color: var(--accent-primary);
  border: 1px solid var(--accent-primary);
}

.btn-toggle.active {
  background: var(--accent-primary);
  color: white;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Crop Presets */
.crop-presets {
  margin-top: 16px;
}

.crop-presets label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.preset-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.preset-btn {
  flex: 1;
  min-width: 50px;
  padding: 8px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: var(--text-primary);
}

.preset-btn:hover {
  border-color: var(--accent-primary);
  background: var(--accent-light);
}

.preset-btn.active {
  background: var(--accent-primary);
  color: white;
  border-color: var(--accent-primary);
}

/* Settings */
.setting-item {
  margin-bottom: 20px;
}

.setting-item:last-child {
  margin-bottom: 0;
}

.setting-item>label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 500;
}

.setting-item>label:has(input[type="range"]) {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.setting-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--accent-primary);
}

.setting-description {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
  margin-left: 26px;
}

.quality-value {
  font-weight: 600;
  color: var(--accent-primary);
}

/* Slider */
.slider {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--bg-tertiary);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent-primary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all 0.2s ease;
}

.slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
  box-shadow: var(--shadow-md);
}

.slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent-primary);
  cursor: pointer;
  border: none;
  box-shadow: var(--shadow-sm);
  transition: all 0.2s ease;
}

.slider::-moz-range-thumb:hover {
  transform: scale(1.2);
  box-shadow: var(--shadow-md);
}

.slider-labels {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 4px;
}

/* File Info */
.file-info {
  background: var(--bg-primary);
  border-radius: 8px;
  padding: 16px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  font-size: 14px;
}

.info-row:not(:last-child) {
  border-bottom: 1px solid var(--border-color);
}

.info-row span:first-child {
  color: var(--text-secondary);
}

.info-row span:last-child {
  font-weight: 600;
  color: var(--text-primary);
}

/* Footer */
.footer {
  text-align: center;
  padding: 40px 0 20px;
  border-top: 1px solid var(--border-color);
  color: var(--text-secondary);
  font-size: 14px;
}

.footer p {
  margin-bottom: 8px;
}

.copyright {
  font-size: 13px;
  opacity: 0.8;
}

/* Loading Overlay */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  z-index: 9999;
  backdrop-filter: blur(4px);
}

.loading-overlay.active {
  display: flex;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading-overlay p {
  color: white;
  margin-top: 20px;
  font-size: 16px;
  font-weight: 500;
}

/* Responsive Design */
@media (max-width: 1024px) {
  .editor-layout {
    grid-template-columns: 1fr;
  }

  .control-panel {
    position: static;
  }
}

@media (max-width: 768px) {
  .container {
    padding: 0 16px;
  }

  .header {
    padding: 16px 0;
  }

  .logo h1 {
    font-size: 20px;
  }

  .main-content {
    padding: 24px 0;
  }

  .upload-area {
    padding: 40px 24px;
  }

  .upload-area h2 {
    font-size: 20px;
  }

  .canvas-container {
    padding: 12px;
  }

  .control-panel {
    padding: 20px;
  }

  .preset-buttons {
    gap: 6px;
  }

  .preset-btn {
    font-size: 12px;
    padding: 6px 10px;
  }
}

@media (max-width: 480px) {
  .logo svg {
    width: 24px;
    height: 24px;
  }

  .logo h1 {
    font-size: 18px;
  }

  .upload-area {
    padding: 30px 20px;
  }

  .upload-area svg {
    width: 48px;
    height: 48px;
  }

  .upload-area h2 {
    font-size: 18px;
  }

  .canvas-info {
    flex-direction: column;
    gap: 8px;
  }
}