:root {
  --bg: #0f1220;
  --panel: #171b2e;
  --panel-2: #1e2338;
  --border: #2b3153;
  --text: #eef0fb;
  --text-dim: #9aa0c3;
  --accent: #6366f1;
  --accent-2: #a5b4fc;
  --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: #161033;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}

.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), var(--accent-2));
  color: #161033;
  box-shadow: 0 8px 24px -8px rgba(99,102,241,0.55);
}

.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; }

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

.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; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  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);
}

.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;
}

.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); } }

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

.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; }

.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;
}

.panels-single {
  grid-template-columns: 1fr;
  max-width: 520px;
  margin: 0 auto;
  width: 100%;
}

.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); }

.field-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
}

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

.field-row input[type="range"] {
  accent-color: var(--accent);
  width: 140px;
}

.field-row input[type="number"] {
  width: 80px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.85rem;
}

.field-row input[type="color"] {
  width: 40px;
  height: 32px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-2);
  cursor: pointer;
}

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

.field-row .lock-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 0.9rem;
}
.field-row .lock-btn.active { color: var(--accent); border-color: var(--accent); }

.preset-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.crop-stage {
  position: relative;
  display: inline-block;
  max-width: 100%;
  touch-action: none;
}

.crop-stage img, .crop-stage canvas {
  display: block;
  max-width: 100%;
  max-height: 480px;
  user-select: none;
  -webkit-user-drag: none;
}

.crop-box {
  position: absolute;
  border: 2px solid var(--accent);
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.55);
  cursor: move;
}

.crop-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 50%;
}
.crop-handle.nw { top: -8px; left: -8px; cursor: nwse-resize; }
.crop-handle.ne { top: -8px; right: -8px; cursor: nesw-resize; }
.crop-handle.sw { bottom: -8px; left: -8px; cursor: nesw-resize; }
.crop-handle.se { bottom: -8px; right: -8px; cursor: nwse-resize; }

/* --- 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;
  }
}
