:root {
  --pasteboard: #e3e5e9;
  --panel: #ffffff;
  --line: #cbd0d7;
  --line-soft: #e8eaee;
  --ink: #1a1d21;
  --muted: #5f6772;
  --cobalt: #1f4fe0;
  --cobalt-deep: #173fb8;
  --cobalt-soft: #e9eefc;
  --marigold: #f2b01e;
  --marigold-ink: #5c3f00;
  --red: #c93a2b;
  --red-soft: #fbe9e6;
  --radius: 6px;
  --font: "Instrument Sans", system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--pasteboard);
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

h1, h2, p, pre, figure, ol { margin: 0; }
a { color: var(--cobalt); }
a:focus-visible, button:focus-visible, textarea:focus-visible, .file-input:focus-visible + .dropzone-label {
  outline: 2px solid var(--cobalt);
  outline-offset: 2px;
}

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; color: var(--ink); text-decoration: none; white-space: nowrap; }
.filename {
  flex: 1 1 auto;
  color: var(--muted);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-left: 16px;
  border-left: 1px solid var(--line);
}
.topbar-link { flex: none; margin-left: auto; white-space: nowrap; text-decoration: none; }
.topbar-link:hover { text-decoration: underline; }

.main { flex: 1; padding: 24px 20px 48px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 16px;
  font: inherit;
  font-weight: 600;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { color: #fff; background: var(--cobalt); border-color: var(--cobalt); }
.btn.primary:hover { background: var(--cobalt-deep); border-color: var(--cobalt-deep); }
.btn:disabled { opacity: 0.55; cursor: default; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

/* Upload page */
.upload { max-width: 620px; margin: 6vh auto 0; }
.upload h1 {
  font-size: clamp(1.75rem, 4vw, 2.5rem);
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.015em;
}
.lede { margin-top: 14px; max-width: 54ch; color: var(--muted); font-size: 1.05rem; }
.error {
  margin-top: 20px;
  padding: 12px 14px;
  color: var(--red);
  background: var(--red-soft);
  border: 1px solid #efc4bd;
  border-radius: var(--radius);
}

.dropzone {
  position: relative;
  margin-top: 28px;
  background: var(--panel);
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  transition: border-color 120ms ease, background-color 120ms ease;
}
.dropzone.dragging { border-color: var(--cobalt); background: var(--cobalt-soft); }
.dropzone-label {
  display: block;
  padding: 56px 24px;
  text-align: center;
  cursor: pointer;
}
.dropzone-title { display: block; font-size: 1.4rem; font-weight: 600; }
.dropzone-sub { display: block; margin-top: 6px; color: var(--muted); }
.dropzone-sub .link { color: var(--cobalt); text-decoration: underline; }
.dropzone-busy { display: none; margin-top: 12px; color: var(--cobalt); font-weight: 600; }
.dropzone.busy .dropzone-busy { display: block; }
.dropzone.busy .dropzone-label { pointer-events: none; }
.file-input {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.upload-submit { margin: 0 24px 24px; }
.js .upload-submit { display: none; }

/* Working / failed states */
.working, .failed { max-width: 620px; margin: 6vh auto 0; padding: 28px; overflow: hidden; }
.working h2, .failed h2 { font-size: 1.35rem; font-weight: 600; }
.working p, .failed p { margin-top: 8px; }
.working .progress { margin: -28px -28px 24px; }
.error-detail {
  margin: 16px 0 20px;
  padding: 12px 14px;
  font: 0.875rem/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--red);
  background: var(--red-soft);
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
}

.progress {
  position: relative;
  height: 3px;
  background: var(--cobalt-soft);
  overflow: hidden;
}
.progress::after {
  content: "";
  position: absolute;
  inset: 0;
  width: 40%;
  background: var(--cobalt);
  animation: slide 1.2s ease-in-out infinite;
}
@keyframes slide {
  from { transform: translateX(-100%); }
  to { transform: translateX(250%); }
}
@media (prefers-reduced-motion: reduce) {
  .progress::after { animation: none; width: 100%; opacity: 0.5; }
}

/* Workspace: canvas + layers panel */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  align-items: start;
  max-width: 1280px;
  margin: 0 auto;
}
@media (min-width: 900px) {
  .workspace { grid-template-columns: minmax(0, 1fr) 420px; }
  .layers { position: sticky; top: 20px; }
}

.canvas {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background-color: #f4f5f7;
  background-image:
    linear-gradient(45deg, #dde0e5 25%, transparent 25%, transparent 75%, #dde0e5 75%),
    linear-gradient(45deg, #dde0e5 25%, transparent 25%, transparent 75%, #dde0e5 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
}
.canvas img { display: block; max-width: 100%; height: auto; }

.layers { display: flex; flex-direction: column; overflow: hidden; }
.layers-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line-soft);
}
.layers-head h2 { font-size: 1.05rem; font-weight: 600; }
.count { color: var(--muted); font-size: 0.9rem; }
.working-line { padding: 10px 16px 0; color: var(--cobalt); font-weight: 600; }

.layers-body { margin: 0; padding: 0; border: 0; min-width: 0; }
.layers-body:disabled { opacity: 0.6; }
.layer-list { list-style: none; padding: 0; max-height: min(70vh, 720px); overflow-y: auto; }
.layer-row { border-bottom: 1px solid var(--line-soft); }
.layer-row:last-child { border-bottom: 0; }

.layer-label {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  font: inherit;
  text-align: left;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}
.layer-label:hover { background: #f6f7f9; }
.layer-row.open .layer-label { background: var(--cobalt-soft); }

.thumb {
  flex: none;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--muted);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 3px;
}
.layer-row[data-edited="true"] .thumb {
  color: var(--marigold-ink);
  background: var(--marigold);
  border-color: var(--marigold);
}

.layer-text { min-width: 0; display: flex; flex-direction: column; }
.layer-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-excerpt {
  color: var(--muted);
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.editor { padding: 0 16px 14px 58px; }
.editor textarea {
  display: block;
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  line-height: 1.4;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  resize: vertical;
}
.editor textarea:focus { border-color: var(--cobalt); outline: none; box-shadow: 0 0 0 2px var(--cobalt-soft); }

.empty { padding: 20px 16px; color: var(--muted); }

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--line-soft);
  background: #fafbfc;
}
