:root {
  --ink: #1a1612;
  --muted: #6e6256;
  --paper: #f2ebe1;
  --line: #d2c6b4;
  --gold: #c08a32;
  --gold2: #8f6524;
  --teal: #2a6b5f;
  --unit: #e2b14a;
  --other: #b8975e;
  --common: #3a8a7a;
  --stone: #9fd8e8;
  --shadow: 0 16px 40px rgba(40, 28, 12, 0.12);
  --font-display: "Fraunces", Georgia, serif;
  --font-body: "IBM Plex Sans", system-ui, sans-serif;
  --rail: 340px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
}

.bg {
  position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background:
    radial-gradient(800px 420px at 0% 0%, rgba(192, 138, 50, 0.16), transparent 60%),
    radial-gradient(700px 500px at 100% 10%, rgba(42, 107, 95, 0.10), transparent 55%),
    linear-gradient(160deg, #f7f1e8, #e9e0d2 55%, #e3d9c9);
}

.shell {
  height: 100vh;
  display: grid;
  grid-template-columns: var(--rail) 1fr;
  gap: 0;
}

.rail {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 14px 14px;
  border-right: 1px solid var(--line);
  background: rgba(255, 252, 247, 0.72);
  backdrop-filter: blur(8px);
  overflow: auto;
  min-height: 0;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 4px 4px 8px;
}
.mark {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  background: conic-gradient(from 200deg, #d7b36a, #8f6524, #2a6b5f, #d7b36a);
  box-shadow: inset 0 0 0 5px #f7f1e8, 0 6px 16px rgba(143, 101, 36, 0.25);
}
.brand h1 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  margin: 0;
  letter-spacing: -0.02em;
}
.tag { margin: 2px 0 0; color: var(--muted); font-size: 0.78rem; }

.card {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 253, 249, 0.92);
  padding: 12px;
  box-shadow: var(--shadow);
}
.card-kicker {
  margin: 0 0 8px;
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.input-card { text-align: left; }
.input-card.drag {
  border-color: var(--gold);
  background: #fff8ec;
}
.drop-title {
  font-family: var(--font-display);
  font-size: 1.1rem;
  margin: 0 0 4px;
}
.drop-sub { margin: 0 0 10px; color: var(--muted); font-size: 0.84rem; }
.file-chip {
  margin-top: 10px;
  display: inline-flex;
  padding: 6px 10px;
  border-radius: 999px;
  background: #efe6d7;
  border: 1px solid var(--line);
  font-size: 0.78rem;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.hidden { display: none !important; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #fffaf3;
  color: var(--ink);
  border-radius: 999px;
  padding: 9px 14px;
  font: 500 0.88rem var(--font-body);
  cursor: pointer;
  transition: transform .15s ease, border-color .15s ease;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); border-color: var(--gold); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary {
  width: 100%;
  margin-top: 6px;
  background: linear-gradient(135deg, var(--gold), var(--gold2));
  border-color: transparent;
  color: #fffaf3;
  font-weight: 600;
}
.btn.ghost {
  padding: 6px 10px;
  font-size: 0.78rem;
  text-decoration: none;
  display: inline-flex;
}

.field {
  display: grid;
  gap: 4px;
  margin-bottom: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}
.field input, .field select {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 10px;
  font: 500 0.9rem var(--font-body);
  color: var(--ink);
  background: #fffdf9;
}

.steps {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}
.steps li {
  display: grid;
  grid-template-columns: 14px 1fr;
  gap: 8px;
  align-items: start;
  font-size: 0.84rem;
}
.steps .dot {
  width: 10px; height: 10px; border-radius: 50%;
  margin-top: 4px;
  background: #cfc3b2;
  box-shadow: 0 0 0 3px #efe6d7;
}
.steps li.done .dot { background: var(--teal); }
.steps li.active .dot {
  background: var(--gold);
  box-shadow: 0 0 0 3px rgba(192, 138, 50, 0.25);
  animation: pulse 1.2s ease infinite;
}
.steps li.pending { opacity: 0.55; }
.steps strong { display: block; font-weight: 600; line-height: 1.25; }
.steps em {
  display: block;
  font-style: normal;
  color: var(--muted);
  font-size: 0.76rem;
  margin-top: 2px;
}
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

.dl-row { display: flex; gap: 6px; margin-top: 8px; }

.progress-card { margin-top: auto; }
.bar {
  height: 8px;
  border-radius: 999px;
  background: #e7ddd0;
  overflow: hidden;
  border: 1px solid var(--line);
}
.bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--teal), var(--gold));
  transition: width .3s ease;
}
.stage { margin: 8px 0 2px; font-weight: 600; font-size: 0.9rem; }
.muted { color: var(--muted); margin: 0; font-size: 0.8rem; }
#progressMsg { min-height: 1.1em; }

.stage-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 14px 16px 16px;
}
.stage-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  margin-bottom: 10px;
}
.stage-head h2 {
  font-family: var(--font-display);
  margin: 0;
  font-size: 1.35rem;
}
.mode-row { display: flex; gap: 6px; }
.mode {
  border: 1px solid var(--line);
  background: #fffaf3;
  border-radius: 999px;
  padding: 6px 12px;
  font: 500 0.8rem var(--font-body);
  cursor: pointer;
  color: var(--muted);
}
.mode.on {
  background: var(--ink);
  color: #f7f1e8;
  border-color: var(--ink);
}
.mode:disabled { opacity: 0.35; cursor: not-allowed; }

.viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: linear-gradient(155deg, #f6f1e8, #d9d0c0);
  box-shadow: var(--shadow);
}
.viewport canvas {
  width: 100%;
  height: 100%;
  display: block;
}
.viewer-empty {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--muted);
  pointer-events: none;
  font-size: 0.95rem;
}
.legend {
  position: absolute;
  left: 12px;
  bottom: 12px;
  display: grid;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(26, 22, 18, 0.72);
  color: #f3ebe0;
  font-size: 0.72rem;
  pointer-events: none;
}
.legend i.sw {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: -1px;
}
.sw.unit { background: var(--unit); }
.sw.other { background: var(--other); opacity: 0.55; }
.sw.common { background: var(--common); }
.sw.stone { background: var(--stone); }

@media (max-width: 900px) {
  body { overflow: auto; }
  .shell {
    height: auto;
    min-height: 100vh;
    grid-template-columns: 1fr;
  }
  .rail {
    border-right: none;
    border-bottom: 1px solid var(--line);
    max-height: none;
  }
  .progress-card { margin-top: 0; }
  .viewport { min-height: 55vh; }
}
