:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1b1e24;
  --muted: #646b78;
  --line: #e1e4e9;
  --accent: #c8783a;
  --accent-ink: #ffffff;
  --field: #f7f8fa;
  --error: #b3261e;
  --warn-bg: #fff4e0;
  --warn-ink: #7a4b00;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --panel: #1c1f26;
    --ink: #e8eaee;
    --muted: #9aa1ad;
    --line: #2c303a;
    --accent: #e0914f;
    --accent-ink: #1b1209;
    --field: #232731;
    --error: #ff8a80;
    --warn-bg: #3a2c12;
    --warn-ink: #ffd590;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
header h1 { margin: 0; font-size: 18px; letter-spacing: -0.01em; }
.tag { color: var(--muted); }
.spacer { flex: 1; }

main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 300px 1fr 290px;
  gap: 12px;
  padding: 12px;
}
.col { display: flex; flex-direction: column; gap: 12px; overflow-y: auto; min-height: 0; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.panel h2 {
  margin: 0;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
}
.panel .body { padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }

button, select, input[type="text"], input[type="number"], input[type="password"] {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  background: var(--field);
  border-radius: 7px;
  padding: 7px 10px;
}
button { cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
button.ghost { background: transparent; }
input[type="number"] { width: 84px; }
input[type="text"], input[type="password"] { width: 100%; }

.field { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; }
.field > select, .field > input, .field > .inline, .field > .seg { color: var(--ink); font-size: 14px; }
.field output { float: right; color: var(--ink); }
.inline { display: flex; align-items: center; gap: 6px; }
.unit { color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { flex: 1; padding: 6px 4px; }
.hint { margin: 0; color: var(--muted); font-size: 12px; }
.error { margin: 0; color: var(--error); }
.warn { margin: 0; padding: 8px 10px; border-radius: 7px; background: var(--warn-bg); color: var(--warn-ink); font-size: 12px; }
a { color: var(--accent); }

#drop {
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
  min-height: 170px;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 10px;
  position: relative;
  outline: none;
}
#drop.over, #drop:focus-visible { border-color: var(--accent); }
#drop p { margin: 0 0 8px; }
#drop .actions { justify-content: center; margin-bottom: 8px; }
#preview { max-width: 100%; max-height: 220px; border-radius: 6px; cursor: pointer; }

#status p { margin: 6px 0 0; color: var(--muted); font-size: 12px; }
.bar { height: 6px; background: var(--field); border-radius: 3px; overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
.bar.indeterminate span { width: 30%; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }

#stage {
  position: relative;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: radial-gradient(ellipse at 50% 30%, var(--panel), var(--bg));
  min-height: 0;
  overflow: hidden;
}
#viewport, #viewport canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#empty-stage { position: absolute; inset: auto 0 18px; text-align: center; color: var(--muted); pointer-events: none; padding: 0 16px; }
#empty-stage p { margin: 0; }
#dims {
  position: absolute; left: 12px; top: 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 7px;
  padding: 5px 9px; font-variant-numeric: tabular-nums; font-size: 13px;
}

#stats { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; font-size: 13px; }
#stats dt { color: var(--muted); }
#stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

#history { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
#history li { position: relative; }
#history button.thumb { padding: 0; width: 100%; aspect-ratio: 1; overflow: hidden; display: block; background: var(--field); }
#history button.thumb.active { border-color: var(--accent); border-width: 2px; }
#history img { width: 100%; height: 100%; object-fit: cover; display: block; }
#history .del {
  position: absolute; top: 2px; right: 2px; padding: 0 5px; font-size: 11px; line-height: 16px;
  opacity: 0; background: var(--panel);
}
#history li:hover .del { opacity: 1; }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--panel); color: var(--ink);
  width: min(440px, calc(100vw - 32px)); padding: 0;
}
dialog form { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; font-size: 16px; }
dialog .actions { justify-content: flex-end; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }

@media (max-width: 1000px) {
  body { height: auto; }
  main { grid-template-columns: 1fr; padding: 12px 16px; }
  .col { overflow: visible; }
  #stage { height: 60vh; order: -1; }
  #left { order: -2; }
}
