:root {
  --bg: #0f1220;
  --panel: #171b2e;
  --panel-2: #1e2338;
  --border: #2b3153;
  --text: #eef0fb;
  --text-dim: #9aa0c3;
  --accent: #7c6cff;
  --accent-2: #5ee6c2;
  --success: #34c98f;
  --danger: #ff6b6b;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: linear-gradient(160deg, #0f1220 0%, #14182b 55%, #10131f 100%);
  color: var(--text);
  min-height: 100vh;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 32px 20px 60px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.home-link {
  align-self: flex-start;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 6px 4px;
}

.home-link:hover { color: var(--text); }

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.site-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  text-decoration: none;
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
}

.brand-logo {
  height: 34px;
  width: auto;
  display: block;
  background: #fff;
  border-radius: 8px;
  padding: 4px 8px;
}

.site-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.site-nav a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 999px;
  white-space: nowrap;
}

.site-nav a:hover { color: var(--text); background: var(--panel-2); }

.site-nav a.active {
  color: white;
  background: linear-gradient(135deg, var(--accent), #9b8cff);
}

.header {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-icon { font-size: 1.6rem; }

.tagline {
  color: var(--text-dim);
  margin: 0;
  font-size: 0.95rem;
  max-width: 520px;
}

.main {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 56px 24px;
  text-align: center;
  transition: border-color 0.2s ease, background 0.2s ease;
  cursor: pointer;
}

.drop-zone.dragover {
  border-color: var(--accent);
  background: var(--panel-2);
}

.drop-inner { display: flex; flex-direction: column; align-items: center; gap: 8px; }

.drop-icon { font-size: 2.6rem; margin-bottom: 4px; }

.drop-title { font-size: 1.1rem; font-weight: 600; margin: 0; }

.drop-sub { color: var(--text-dim); margin: 0; font-size: 0.85rem; }

.drop-hint { color: var(--text-dim); font-size: 0.8rem; margin-top: 10px; }

.btn {
  border: none;
  border-radius: 999px;
  padding: 12px 26px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.12s ease, box-shadow 0.12s ease;
}

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

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #9b8cff);
  color: white;
  box-shadow: 0 8px 24px -8px rgba(124, 108, 255, 0.6);
}

.btn-success {
  background: linear-gradient(135deg, var(--success), var(--accent-2));
  color: #06251a;
  box-shadow: 0 8px 24px -8px rgba(52, 201, 143, 0.5);
}

.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}

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

.hidden { display: none !important; }

.workspace {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.panels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

@media (max-width: 720px) {
  .panels { grid-template-columns: 1fr; }
}

.step-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.step-indicator {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
}

.step-dot {
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-weight: 600;
}

.step-dot.active {
  background: linear-gradient(135deg, var(--accent), #9b8cff);
  border-color: transparent;
  color: white;
}

.step-arrow {
  color: var(--text-dim);
}

#edit-step,
#review-step {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.review-intro {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-dim);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
}

.review-frame {
  min-height: 320px;
  max-height: 70vh;
}

.review-frame canvas {
  max-width: 100%;
  max-height: 70vh;
  display: block;
  object-fit: contain;
}

.review-meta {
  margin: 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  /* Without this, a grid item defaults to min-width/height: auto, so a
     zoomed (large, explicit-pixel-size) canvas inside would grow the whole
     panel — and the page — to fit it instead of being clipped and
     scrolled by .canvas-frame.zoomed below. */
  min-width: 0;
  min-height: 0;
}

.panel h3 {
  margin: 0;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.zoom-controls {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
}

.zoom-btn {
  border: none;
  background: transparent;
  color: var(--text-dim);
  width: 24px;
  height: 24px;
  line-height: 1;
  border-radius: 50%;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }

.zoom-btn.zoom-fit {
  width: auto;
  min-width: 40px;
  border-radius: 999px;
  padding: 0 8px;
  font-size: 0.72rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.canvas-frame {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  min-height: 280px;
  max-height: 420px;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0b0d18;
}

.canvas-frame img,
.canvas-frame canvas {
  max-width: 100%;
  max-height: 420px;
  display: block;
  object-fit: contain;
}

.canvas-frame canvas {
  cursor: none;
  touch-action: none;
}

/* Zoomed: the canvas gets an explicit pixel size (set from JS) instead of
   auto-fitting, and the frame becomes a scrollable viewport onto it. */
.canvas-frame.zoomed {
  overflow: auto;
  align-items: flex-start;
  justify-content: flex-start;
}

.canvas-frame.zoomed canvas {
  max-width: none;
  max-height: none;
}

.brush-cursor {
  position: absolute;
  border: 2px solid white;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6), inset 0 0 0 1px rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%);
  z-index: 5;
}

.brush-cursor.mode-restore { border-color: var(--accent-2); }
.brush-cursor.mode-erase { border-color: #ff8a8a; }

.retouch-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.retouch-hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.retouch-hint strong { color: var(--text); }

.retouch-toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.tool-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-2);
  padding: 4px 4px 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.tool-btn {
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.tool-btn.active {
  background: linear-gradient(135deg, var(--accent), #9b8cff);
  color: white;
}

.brush-size-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.btn-sm {
  padding: 8px 16px;
  font-size: 0.82rem;
}

.retouch-feedback {
  margin: 0;
  font-size: 0.82rem;
  color: #ffd76a;
  background: rgba(255, 215, 106, 0.1);
  border: 1px solid rgba(255, 215, 106, 0.3);
  border-radius: 8px;
  padding: 8px 12px;
}

.export-panel {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  justify-content: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
}

.export-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.export-field select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.85rem;
}

.export-checkbox {
  cursor: pointer;
}

.export-checkbox input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.export-hint {
  display: block;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-dim);
}
.export-hint:empty { display: none; }

.checkerboard {
  background-image:
    linear-gradient(45deg, #2a2f4a 25%, transparent 25%),
    linear-gradient(-45deg, #2a2f4a 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #2a2f4a 75%),
    linear-gradient(-45deg, transparent 75%, #2a2f4a 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  background-color: #1c2036;
}

.result-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-dim);
  font-size: 0.9rem;
  text-align: center;
  padding: 0 20px;
}

.result-placeholder.hidden { display: none; }

.spinner {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  display: none;
}

.spinner.active { display: block; }

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

.progress-wrap {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--panel-2);
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.2s ease;
}

.progress-label {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-dim);
  text-align: center;
}

.actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.78rem;
}

.footer code {
  background: var(--panel-2);
  padding: 2px 6px;
  border-radius: 4px;
}

.legal-links {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 14px;
}

.legal-links a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.76rem;
}

.legal-links a:hover {
  color: var(--text);
  text-decoration: underline;
}

/* --- Theme toggle button --- */
.site-header { flex-wrap: wrap; }
.site-nav { flex: 1 1 auto; justify-content: flex-end; }

.theme-toggle {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
  padding: 0;
  margin-left: 8px;
}
.theme-toggle:hover { border-color: var(--accent); }

/* --- Light mode --- */
:root[data-theme="light"] {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #eef0f8;
  --border: #dbdfee;
  --text: #181b2c;
  --text-dim: #5a5f78;
}

:root[data-theme="light"] body,
:root[data-theme="light"] html {
  background: linear-gradient(160deg, #f6f7fb 0%, #eef0f8 55%, #f6f7fb 100%);
}

:root[data-theme="light"] .canvas-frame {
  background: #e6e9f4;
}

:root[data-theme="light"] .checkerboard {
  background-image:
    linear-gradient(45deg, #dfe2ee 25%, transparent 25%),
    linear-gradient(-45deg, #dfe2ee 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #dfe2ee 75%),
    linear-gradient(-45deg, transparent 75%, #dfe2ee 75%);
  background-color: #eef0f8;
}

:root[data-theme="light"] .crop-wrap {
  background: #e6e9f4;
}

:root[data-theme="light"] .page-thumb img {
  background: #e6e9f4;
}

:root[data-theme="light"] .content-block p {
  color: #3a3f57;
}

/* --- Keep the theme toggle next to the brand on narrow screens, instead
   of stranding it alone below a wrapped nav --- */
@media (max-width: 640px) {
  .site-brand { order: 1; }
  .theme-toggle { order: 2; margin-left: auto; }
  .site-nav {
    order: 3;
    flex-basis: 100%;
    justify-content: flex-start;
  }
}

.content-block h2:not(:first-of-type) {
  margin-top: 28px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.content-block h3 {
  margin: 18px 0 6px;
  font-size: 0.96rem;
  font-weight: 700;
  color: var(--text);
}

.content-block ul, .content-block ol {
  margin: 0 0 12px;
  padding-left: 22px;
  color: #d7dbee;
  font-size: 0.92rem;
  line-height: 1.7;
}
.content-block li { margin-bottom: 6px; }

:root[data-theme="light"] .content-block ul,
:root[data-theme="light"] .content-block ol {
  color: #3a3f57;
}

.content-block {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px clamp(18px, 4vw, 32px);
}

.content-block h2 {
  margin: 0 0 12px;
  font-size: 1.15rem;
  font-weight: 800;
}

.content-block p {
  color: #d7dbee;
  font-size: 0.92rem;
  line-height: 1.7;
  margin: 0 0 12px;
}
.content-block p:last-child { margin-bottom: 0; }
.content-block a { color: var(--accent-2); }

/* --- Animations & micro-interactions --- */
@media (prefers-reduced-motion: no-preference) {
  .site-brand img.brand-logo {
    transition: transform 0.35s cubic-bezier(.34,1.56,.64,1);
  }
  .site-brand:hover img.brand-logo {
    transform: scale(1.08) rotate(-4deg);
  }

  .site-nav a {
    transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
  }
  .site-nav a:hover {
    transform: translateY(-1px);
  }

  .theme-toggle {
    transition: transform 0.2s ease, background 0.2s ease, border-color 0.2s ease;
  }
  .theme-toggle:hover { transform: scale(1.08); }
  .theme-toggle:active { transform: scale(0.92); }
  .theme-toggle.spin { animation: themeSpin 0.5s cubic-bezier(.34,1.56,.64,1); }

  .btn, .tool-cta {
    transition: transform 0.15s ease, box-shadow 0.2s ease, opacity 0.15s ease;
  }
  .btn-primary, .btn-success, .tool-cta {
    position: relative;
    overflow: hidden;
  }
  .btn-primary::after, .btn-success::after, .tool-cta::after {
    content: "";
    position: absolute;
    top: 0;
    left: -75%;
    width: 40%;
    height: 100%;
    background: linear-gradient(120deg, transparent, rgba(255,255,255,0.35), transparent);
    transform: skewX(-20deg);
    transition: left 0.6s ease;
    pointer-events: none;
  }
  .btn-primary:hover::after, .btn-success:hover::after, .tool-cta:hover::after {
    left: 125%;
  }
  .btn:hover:not(:disabled) { transform: translateY(-1px); }

  .drop-zone { transition: border-color 0.25s ease, background 0.25s ease, transform 0.2s ease; }
  .drop-zone.dragover { animation: dropPulse 1.1s ease-in-out infinite; }

  .tool-card-mini, .article-card {
    transition: transform 0.25s ease, border-color 0.2s ease, box-shadow 0.25s ease;
  }
  .tool-card-mini:hover, .article-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 14px 28px -14px rgba(0,0,0,0.4);
  }

  .reveal {
    opacity: 0;
    transform: translateY(18px);
    transition: opacity 0.6s ease, transform 0.6s ease;
  }
  .reveal.is-visible {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes themeSpin {
  0% { transform: rotate(0deg) scale(1); }
  50% { transform: rotate(180deg) scale(1.2); }
  100% { transform: rotate(360deg) scale(1); }
}

@keyframes dropPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.012); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .reveal { opacity: 1; transform: none; }
}

.content-block .updated {
  margin: -6px 0 14px;
  color: var(--text-dim);
  font-size: 0.78rem;
}
