:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-faint: #94a3b8;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --accent-grad: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  --success: #10b981;
  --success-soft: #d1fae5;
  --warning: #f59e0b;
  --warning-soft: #fef3c7;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow:    0 4px 12px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 16px 40px rgba(15, 23, 42, 0.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Inter', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-feature-settings: 'cv02', 'cv03', 'cv04', 'cv11';
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 1.25rem;
  padding: 0.85rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  min-height: 56px;
}
.topbar .spacer { flex: 1; }

/* Brand */
.brand {
  display: inline-flex; align-items: center; gap: 0.6rem;
  font-weight: 600;
  color: var(--text);
  font-size: 0.98rem;
  letter-spacing: -0.01em;
}
.brand:hover { text-decoration: none; }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px;
  background: var(--accent-grad);
  color: #fff;
  border-radius: 8px;
  font-weight: 700; font-size: 0.85rem;
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.3);
  letter-spacing: -0.02em;
}

/* Breadcrumb */
.breadcrumb {
  display: flex; align-items: center; gap: 0.5rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}
.breadcrumb a { color: var(--text-muted); }
.breadcrumb a:hover { color: var(--text); text-decoration: none; }
.breadcrumb .current { color: var(--text); font-weight: 500; }
.breadcrumb .sep { color: var(--text-faint); }

/* Helpers */
.muted { color: var(--text-muted); }
.small { font-size: 0.85em; }
.error { color: var(--danger); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 0.4rem;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--text);
  text-decoration: none;
  font-family: inherit;
  transition: all 0.15s ease;
  line-height: 1.2;
}
.btn:hover { background: var(--surface-2); border-color: var(--text-faint); text-decoration: none; }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--primary-soft); border-color: var(--primary); }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.primary:focus-visible { box-shadow: 0 0 0 3px var(--primary-soft); }
.btn.danger { color: var(--danger); border-color: var(--danger-soft); background: var(--surface); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { padding: 0.32rem 0.7rem; font-size: 0.8rem; }
.btn.big { padding: 0.75rem 1.5rem; font-size: 1rem; font-weight: 500; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }

/* Forms */
input, select, textarea {
  font-family: inherit; font-size: 0.9rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
input[type=color] { padding: 2px; height: 36px; cursor: pointer; width: 100%; }
input[type=file] { padding: 0.35rem; }

label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.875rem; color: var(--text); }
label > span { font-weight: 500; color: var(--text); }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; }
.radio-row { display: flex; gap: 1rem; }
.radio-row label { flex-direction: row; align-items: center; gap: 0.4rem; font-weight: 400; }

/* Layout */
.container { max-width: 1200px; margin: 2rem auto; padding: 0 1.5rem; }
.container.narrow { max-width: 720px; }

.section-title {
  margin: 0 0 1rem;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  display: flex; align-items: baseline; gap: 0.6rem;
}
.section-title .count {
  color: var(--text-faint);
  font-weight: 400;
  font-size: 0.85rem;
}

/* Card grids */
.template-grid, .job-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}
.template-card, .job-card-mini {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: box-shadow 0.2s, border-color 0.2s, transform 0.2s;
}
.template-card:hover, .job-card-mini:hover {
  box-shadow: var(--shadow);
  border-color: var(--border-strong);
}

.template-card .thumb,
.job-thumb {
  display: block;
  aspect-ratio: 4/3;
  background: var(--surface-2);
  position: relative;
  overflow: hidden;
}
.template-card .thumb img,
.job-thumb img {
  width: 100%; height: 100%; object-fit: contain;
}
.job-placeholder {
  display: flex; align-items: center; justify-content: center;
  height: 100%;
  font-size: 0.85rem;
}

.template-card .meta, .job-info { padding: 1rem 1.1rem 1.1rem; }
.template-card h3, .job-info h4 {
  margin: 0;
  font-size: 0.95rem; font-weight: 600;
  line-height: 1.3;
  letter-spacing: -0.01em;
}
.template-card .meta p.muted, .job-info p.muted {
  margin: 0.25rem 0 0;
  font-size: 0.82rem;
}
.template-card .actions, .job-info .actions {
  margin-top: 0.9rem;
  display: flex; gap: 0.4rem;
  flex-wrap: wrap; align-items: center;
}
.template-card .actions form.inline, .job-info form.inline { display: inline; margin: 0; }

/* Empty state */
.empty {
  text-align: center;
  padding: 3.5rem 1.5rem;
  background: var(--surface);
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  color: var(--text-muted);
}
.empty h3 { color: var(--text); margin: 0 0 0.4rem; font-size: 1.05rem; font-weight: 600; }
.empty p  { margin: 0 0 1.25rem; }

/* Status badges */
.badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  line-height: 1.3;
}
.badge-done    { background: var(--success-soft); color: #047857; }
.badge-running { background: var(--warning-soft); color: #92400e; }
.badge-queued  { background: var(--primary-soft); color: var(--primary-hover); }
.badge-error   { background: var(--danger-soft);  color: #b91c1c; }

/* Dialog */
dialog {
  border: none;
  border-radius: var(--radius-lg);
  padding: 0;
  box-shadow: var(--shadow-lg);
  max-width: 92vw;
}
dialog::backdrop { background: rgba(15, 23, 42, 0.5); backdrop-filter: blur(2px); }
.dialog-form { padding: 1.5rem 1.75rem; min-width: 380px; display: flex; flex-direction: column; gap: 1rem; }
.dialog-form h2 { margin: 0 0 0.25rem; font-size: 1.15rem; letter-spacing: -0.01em; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.5rem; }
.modal-close { position: absolute; top: 0.6rem; right: 0.75rem; background: transparent; border: 0; font-size: 1.5rem; cursor: pointer; color: var(--text-muted); }
.modal-img-wrap { padding: 1rem; max-width: 80vw; max-height: 80vh; overflow: auto; }
.modal-img-wrap img { display: block; max-width: 100%; max-height: 75vh; }

/* Public job page */
.job-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.75rem 2rem;
  max-width: 920px;
}
.job-title { margin: 0 0 1rem; font-size: 1.45rem; letter-spacing: -0.01em; font-weight: 700; }
.job-meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.4rem 1.5rem;
  margin: 0 0 1.25rem;
  padding: 1rem 1.25rem;
  background: var(--surface-2);
  border-radius: var(--radius);
  font-size: 0.9rem;
}
.job-meta dt { color: var(--text-muted); font-weight: 500; }
.job-meta dd { margin: 0; color: var(--text); }
.job-meta code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.85rem; background: var(--surface); padding: 0.1rem 0.4rem; border-radius: 4px; border: 1px solid var(--border); }

.share-box {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin: 0 0 1rem;
  padding: 0.55rem;
  background: var(--surface-2);
  border-radius: var(--radius);
}
.share-box input { flex: 1; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.85rem; background: var(--surface); }

.sample-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.85rem;
  margin-top: 0.75rem;
}
.sample-grid a { display: block; }
.sample-grid img {
  width: 100%;
  aspect-ratio: 4/3;
  object-fit: contain;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: transform 0.2s, box-shadow 0.2s;
}
.sample-grid a:hover img {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

/* Login */
.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background:
    radial-gradient(at 15% 20%, #eef2ff 0%, transparent 50%),
    radial-gradient(at 85% 10%, #faf5ff 0%, transparent 50%),
    radial-gradient(at 70% 90%, #ecfdf5 0%, transparent 50%),
    #f8fafc;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2.25rem 2rem 1.75rem;
  box-shadow: var(--shadow);
}
.login-brand {
  display: flex; flex-direction: column; align-items: center;
  gap: 0.85rem;
  margin-bottom: 1.75rem;
  text-align: center;
}
.login-brand .brand-mark {
  width: 52px; height: 52px;
  border-radius: 12px;
  font-size: 1.5rem;
  box-shadow: 0 8px 24px rgba(79, 70, 229, 0.25);
}
.login-brand h1 { margin: 0; font-size: 1.15rem; font-weight: 600; letter-spacing: -0.01em; }
.login-brand .tagline { margin: 0; color: var(--text-muted); font-size: 0.875rem; }

/* Editor */
.editor-body { background: var(--bg); }
.editor-shell {
  display: grid;
  grid-template-columns: 1fr 420px;
  height: calc(100vh - 56px);
}
.canvas-pane {
  padding: 1.5rem;
  display: flex; flex-direction: column; align-items: center;
  overflow: auto;
}
.canvas-wrap {
  position: relative;
  background: #fff;
  background-image:
    linear-gradient(45deg, #f0f1f3 25%, transparent 25%),
    linear-gradient(-45deg, #f0f1f3 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #f0f1f3 75%),
    linear-gradient(-45deg, transparent 75%, #f0f1f3 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  box-shadow: var(--shadow-lg);
  border-radius: 4px;
}
#base-img { display: block; user-select: none; -webkit-user-drag: none; max-width: 100%; }
.editor-hint { margin-top: 1rem; max-width: 600px; color: var(--text-muted); font-size: 0.85rem; }

.overlay {
  position: absolute;
  outline-offset: -2px;
  box-sizing: border-box;
  cursor: move;
  user-select: none;
  display: flex; align-items: center; justify-content: center;
}
.text-overlay {
  outline: 2px dashed var(--danger);
  background: rgba(239, 68, 68, 0.06);
}
.qr-overlay {
  outline: 2px solid var(--primary);
  background: rgba(79, 70, 229, 0.07);
}
.overlay .label {
  position: absolute; top: -11px; left: 6px;
  background: #fff; padding: 0 6px;
  font-size: 0.7rem; letter-spacing: 0.08em;
  color: var(--text-muted); font-weight: 700;
  pointer-events: none;
  border-radius: 4px;
}
.qr-overlay .label { color: var(--primary); }
.text-overlay .label { color: var(--danger); }

.handle { position: absolute; background: #fff; border: 2px solid var(--text-muted); border-radius: 50%; }
.handle.move { width: 14px; height: 14px; top: 50%; left: 50%; transform: translate(-50%, -50%); cursor: grab; }
.handle.resize { width: 14px; height: 14px; bottom: -7px; right: -7px; cursor: nwse-resize; border-radius: 4px; }
.qr-overlay .handle { border-color: var(--primary); }
.text-overlay .handle { border-color: var(--danger); }

.text-content {
  width: 100%; height: 100%;
  display: flex;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  pointer-events: none;
}

.sidebar {
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 1.25rem 1.5rem;
  overflow-y: auto;
}
.sidebar .section { padding: 0.85rem 0; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 0.7rem; }
.sidebar .section:first-child { padding-top: 0; }
.sidebar .section:last-child { border-bottom: 0; }
.sidebar h2 {
  margin: 0;
  font-size: 0.72rem; text-transform: uppercase;
  letter-spacing: 0.1em; color: var(--text-muted);
  font-weight: 700;
}

/* Generate page */
.generate-grid { display: grid; grid-template-columns: 260px 1fr; gap: 2rem; align-items: start; }
.thumb-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 1rem;
  text-align: center;
}
.thumb-card img { max-width: 100%; max-height: 240px; border-radius: 6px; }

.progress { background: var(--surface-2); border-radius: 999px; height: 8px; overflow: hidden; margin: 1rem 0 0.5rem; }
.progress .bar { background: var(--primary); height: 100%; width: 0; transition: width .3s; }
#progress-card {
  margin-top: 1.5rem; padding: 1.25rem 1.5rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
#download-btn { margin-top: 0.7rem; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff;
  padding: 0.7rem 1.1rem; border-radius: 10px;
  box-shadow: var(--shadow-lg);
  z-index: 99;
  font-size: 0.9rem; font-weight: 500;
}
.toast.error { background: var(--danger); }

.app-footer {
  text-align: center;
  padding: 2.5rem 1rem 1.5rem;
  color: var(--text-faint);
  font-size: 0.8rem;
}

@media (max-width: 980px) {
  .editor-shell { grid-template-columns: 1fr; height: auto; }
  .sidebar { border-left: 0; border-top: 1px solid var(--border); }
  .generate-grid { grid-template-columns: 1fr; }
  .topbar { padding: 0.7rem 1rem; gap: 0.75rem; flex-wrap: wrap; }
  .breadcrumb { font-size: 0.82rem; }
}

/* Editor: shrink-to-fit warning */
.size-warn {
  background: #fef3c7;
  color: #92400e;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  font-size: 0.82rem;
  line-height: 1.4;
  margin: 0;
  border: 1px solid #fde68a;
}
.size-warn strong { font-weight: 600; }
