:root {
  --bg: #0f1115;
  --bg-panel: #171a21;
  --bg-panel-alt: #1d212b;
  --border: #2a2f3a;
  --text: #e8eaed;
  --text-dim: #9aa1ac;
  --accent: #2563eb;
  --accent-hover: #3b76f0;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --radius: 10px;
  --canvas-checker-a: #23262e;
  --canvas-checker-b: #1a1c22;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

button, input, select { font-family: inherit; }

h1, h2, h3 { margin: 0 0 8px; }

/* ---------- Top bar ---------- */
.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #60a5fa);
  display: inline-block;
}
.brand-tld { color: var(--text-dim); font-weight: 500; }

.topbar-actions { display: flex; align-items: center; gap: 10px; }

.auth-area { display: flex; align-items: center; gap: 8px; }
.auth-area .user-chip { font-size: 13px; color: var(--text-dim); padding: 6px 10px; }

/* ---------- Buttons ---------- */
.btn {
  background: var(--bg-panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: #262b36; border-color: #3a4150; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.small { padding: 6px 10px; font-size: 12px; }
.btn.full { width: 100%; margin-bottom: 8px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }

.btn-group { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.upload-btn {
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}
.upload-btn:hover { background: var(--accent-hover); }
.upload-btn.large { padding: 12px 22px; font-size: 15px; margin-top: 18px; }

/* ---------- Layout ---------- */
.workspace {
  display: grid;
  grid-template-columns: 260px 1fr 280px;
  height: calc(100% - 56px);
}

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; }
}

.panel {
  background: var(--bg-panel);
  padding: 18px 16px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
}
.panel-right { border-right: none; border-left: 1px solid var(--border); }
.panel h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.panel h2:first-child { margin-top: 0; padding-top: 0; border-top: none; }

.field-group { margin-bottom: 14px; }
.field-label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }

.chip-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--bg-panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
}
.chip:hover { border-color: #3a4150; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.select, .num-input {
  width: 100%;
  background: var(--bg-panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

.row-inputs { display: flex; align-items: center; gap: 6px; }
.row-inputs.wrap { flex-wrap: wrap; }
.row-inputs .num-input,
.row-inputs input[type="text"] { width: 0; flex: 1; }
.x-sep { color: var(--text-dim); }

.slider { width: 100%; accent-color: var(--accent); }
.slider-row { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }

.hint { font-size: 12px; color: var(--text-dim); margin: 6px 0; }

/* ---------- Canvas area ---------- */
.canvas-area {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  background:
    linear-gradient(45deg, var(--canvas-checker-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--canvas-checker-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--canvas-checker-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--canvas-checker-a) 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0px;
  background-color: var(--canvas-checker-b);
}

.empty-state {
  text-align: center;
  max-width: 420px;
  padding: 40px;
  color: var(--text-dim);
}
.empty-icon { font-size: 42px; margin-bottom: 12px; }
.empty-state h1 { color: var(--text); font-size: 22px; }

.editor-stage { padding: 30px; }
.stage-inner { position: relative; display: inline-block; line-height: 0; }
#mainCanvas {
  max-width: 78vw;
  max-height: 76vh;
  box-shadow: 0 6px 30px rgba(0,0,0,0.5);
  /* Checkerboard so removed-background (transparent) areas read clearly,
     instead of a solid color that could be mistaken for actual pixels. */
  background:
    linear-gradient(45deg, var(--canvas-checker-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--canvas-checker-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--canvas-checker-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--canvas-checker-a) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
  background-color: var(--canvas-checker-b);
}

/* ---------- Crop overlay ---------- */
.crop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
}
.crop-box {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2000px rgba(0,0,0,0.5);
  cursor: move;
}
.handle {
  position: absolute;
  width: 12px; height: 12px;
  background: #fff;
  border: 1px solid var(--accent);
  border-radius: 2px;
}
.handle-n  { top: -7px; left: calc(50% - 6px); cursor: n-resize; }
.handle-s  { bottom: -7px; left: calc(50% - 6px); cursor: s-resize; }
.handle-e  { right: -7px; top: calc(50% - 6px); cursor: e-resize; }
.handle-w  { left: -7px; top: calc(50% - 6px); cursor: w-resize; }
.handle-nw { top: -7px; left: -7px; cursor: nw-resize; }
.handle-ne { top: -7px; right: -7px; cursor: ne-resize; }
.handle-sw { bottom: -7px; left: -7px; cursor: sw-resize; }
.handle-se { bottom: -7px; right: -7px; cursor: se-resize; }

.crop-dims {
  position: absolute;
  top: -26px; left: 0;
  font-size: 11px;
  background: rgba(0,0,0,0.75);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
}

/* ---------- Perspective overlay ---------- */
/* Visibility is driven by the .is-visible class rather than the `hidden`
   attribute/property: at least one Chromium build we tested against does
   not reliably reflect el.hidden = false back onto an <svg> element's
   `hidden` attribute, which left this overlay invisible-but-still-present
   over the crop box, silently swallowing every click meant for it. */
.perspective-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: none;
  /* Only the drag handles should intercept clicks; otherwise this SVG
     would sit over the crop box and swallow every click meant for it. */
  pointer-events: none;
}
.perspective-overlay.is-visible {
  display: block;
}
.persp-handle {
  pointer-events: auto;
  fill: #fff;
  stroke: var(--accent);
  stroke-width: 2;
  cursor: grab;
}
.persp-line { stroke: #fff; stroke-width: 1.5; fill: none; opacity: 0.85; }

/* ---------- Background removal ---------- */
.bg-brush-cursor {
  position: absolute;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  transform: translate(-50%, -50%);
}
#mainCanvas.bg-wand-active { cursor: crosshair; }
#mainCanvas.bg-brush-active { cursor: none; }

/* ---------- Toasts / modal ---------- */
.toast-root {
  position: fixed;
  bottom: 20px; left: 50%;
  transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px;
  z-index: 1000;
}
.toast {
  background: #20242e;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
}
.toast.error { border-color: var(--danger); color: #ffb4b6; }
.toast.success { border-color: #2f8f5b; color: #8ce6b0; }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 900;
}
.modal {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 340px;
  max-width: 90vw;
}
.modal h3 { margin-bottom: 14px; }
.modal .field { margin-bottom: 12px; }
.modal label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }
.modal input,
.modal textarea {
  width: 100%;
  background: var(--bg-panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
}
.modal .modal-error { color: var(--danger); font-size: 12px; min-height: 16px; margin-bottom: 6px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }
.modal-switch { font-size: 12px; color: var(--text-dim); margin-top: 12px; text-align: center; }
.modal-switch a { color: var(--accent); cursor: pointer; }

.modal.wide { width: 640px; }
.modal.xwide { width: 920px; }
.color-input {
  width: 44px;
  height: 32px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-panel-alt);
  cursor: pointer;
}
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
  max-height: 60vh;
  overflow-y: auto;
  margin-top: 10px;
}
.gallery-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-panel-alt);
}
.gallery-item img { width: 100%; height: 90px; object-fit: cover; display: block; }
.gallery-item .gi-meta { padding: 6px 8px; font-size: 11px; color: var(--text-dim); }
.gallery-item .gi-actions { display: flex; gap: 4px; padding: 0 8px 8px; }
.gallery-item .gi-actions button,
.gallery-item .gi-actions a {
  flex: 1;
  padding: 4px;
  font-size: 11px;
  text-align: center;
  text-decoration: none;
}
.gallery-empty { color: var(--text-dim); font-size: 13px; padding: 20px 0; text-align: center; }

/* ---------- Batch tools ---------- */
.upload-btn.small { padding: 6px 12px; font-size: 12px; }

.batch-dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 16px;
  transition: border-color 0.15s, background 0.15s;
}
.batch-dropzone p { margin: 4px 0; }
.batch-dropzone.drag-over { border-color: var(--accent); background: rgba(37, 99, 235, 0.08); }

.batch-progress { margin: 14px 0; }
.batch-progress-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--bg-panel-alt);
  overflow: hidden;
  border: 1px solid var(--border);
}
.batch-progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.15s;
}
.batch-error { color: var(--danger); font-size: 11px; padding: 8px; }

/* ---------- Image grid tool ---------- */
.grid-tool-body { display: flex; gap: 22px; flex-wrap: wrap; align-items: flex-start; }
.grid-tool-side { flex: 0 0 240px; min-width: 220px; max-height: 62vh; overflow-y: auto; padding-right: 4px; }
.grid-tool-main { flex: 1; min-width: 300px; display: flex; justify-content: center; }
.grid-preview-wrap { width: 100%; max-width: 520px; }

.grid-thumb-strip { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.grid-thumb {
  position: relative;
  width: 56px; height: 56px;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid var(--border);
  cursor: pointer;
  background: var(--bg-panel-alt);
  flex-shrink: 0;
}
.grid-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.grid-thumb.placed { opacity: 0.5; }
.grid-thumb.armed { border-color: var(--accent); opacity: 1; box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.45); }
.grid-thumb-remove {
  position: absolute; top: 2px; right: 2px;
  width: 16px; height: 16px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 10px;
  line-height: 16px;
  text-align: center;
  cursor: pointer;
  padding: 0;
}

.grid-preview {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  user-select: none;
  touch-action: none;
}
.grid-preview-hint {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 30px;
  color: var(--text-dim);
}
.grid-cell {
  position: absolute;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 6px;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--bg-panel);
  box-sizing: border-box;
}
.grid-cell.empty { border: 1px dashed var(--border); cursor: pointer; }
.grid-cell.filled { cursor: pointer; }
.grid-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }

.grid-divider { position: absolute; z-index: 5; }
.grid-divider::after { content: ''; display: block; background: var(--border); }
.grid-divider-row { left: 0; right: 0; height: 12px; margin-top: -6px; cursor: row-resize; }
.grid-divider-row::after { height: 2px; margin-top: 5px; }
.grid-divider-col { top: 0; bottom: 0; width: 12px; margin-left: -6px; cursor: col-resize; }
.grid-divider-col::after { width: 2px; height: 100%; margin-left: 5px; }
.grid-divider:hover::after { background: var(--accent); }
