:root {
  color-scheme: dark;
  --bg: #0d1117;
  --panel: #151b24;
  --panel-2: #1b2331;
  --line: #26303f;
  --text: #e6edf3;
  --muted: #93a1b1;
  --accent: #f0a44a;
  --accent-2: #4aa8f0;
  --accent-ink: #23180a;
  --ok: #4ade80;
  --err: #f87171;
  --viewport-bg: #10141b;
  --overlay: rgba(13, 17, 23, .72);
  --shadow: none;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f2f5f9;
  --panel: #ffffff;
  --panel-2: #eef2f8;
  --line: #d3dae6;
  --text: #16202b;
  --muted: #5a6675;
  --accent: #e08a2b;
  --accent-2: #1f6feb;
  --accent-ink: #2a1c05;
  --ok: #147f3d;
  --err: #c23b2e;
  --viewport-bg: #e7ecf3;
  --overlay: rgba(240, 244, 249, .84);
  --shadow: 0 1px 2px rgba(16, 32, 48, .08);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}

.topbar h1 { font-size: 16px; margin: 0; letter-spacing: .2px; }
.tagline { color: var(--muted); font-size: 12px; }
.health { margin-left: auto; font-size: 12px; color: var(--muted); }

.ws { display: flex; align-items: center; gap: 6px; }
.ws label { margin: 0; font-size: 12px; color: var(--muted); }
.ws input {
  width: 140px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  padding: 6px 8px;
  font: inherit;
}

.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 370px 1fr;
  gap: 14px;
  padding: 14px;
}

.sidebar { display: flex; flex-direction: column; gap: 14px; min-height: 0; overflow: auto; }

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

.card.grow { flex: 1 1 auto; min-height: 160px; display: flex; flex-direction: column; }
.card.disabled { opacity: .55; pointer-events: none; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card h2 { font-size: 13px; margin: 0 0 4px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.hint { color: var(--muted); font-size: 11px; margin: 0 0 8px; word-break: break-word; }

label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }

textarea {
  width: 100%;
  resize: vertical;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  font: inherit;
}

button {
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 9px 14px;
  font: inherit;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-ink);
}

button:disabled { opacity: .55; cursor: progress; }

#create-submit, #refine-submit { width: 100%; margin-top: 10px; }

button.ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--text);
  font-weight: 500;
  padding: 6px 10px;
}

button.ghost.active { border-color: var(--accent-2); color: var(--accent-2); }

.picker { margin-top: 10px; }
.file-label { display: flex; align-items: baseline; gap: 6px; }
.file-label em { font-style: normal; font-size: 11px; color: var(--muted); opacity: .8; }
.picker input[type="file"] { width: 100%; font-size: 12px; color: var(--muted); }

.image-preview { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.image-preview.hidden { display: none; }
.image-preview img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel-2);
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 10px; }
.chips-hint { color: var(--muted); font-size: 11px; }
.chip {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 500;
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 999px;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

.status { min-height: 18px; margin: 8px 0 0; font-size: 12px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.err { color: var(--err); }

.gallery { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1 1 auto; }

.gallery li {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--panel-2);
  cursor: pointer;
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.gallery li.empty { cursor: default; color: var(--muted); }
.gallery li.selected { border-color: var(--accent); }
.gallery .g-main { flex: 1 1 auto; min-width: 0; }
.gallery .g-label { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gallery .g-meta { display: block; color: var(--muted); font-size: 11px; }
.gallery .del { background: transparent; border: none; color: var(--muted); padding: 2px 4px; font-weight: 700; }
.gallery .del:hover { color: var(--err); }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }

.viewer {
  display: grid;
  grid-template-rows: auto minmax(120px, 1fr) auto;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
}

.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.viewer-toolbar strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toolbar-actions { margin-left: auto; display: flex; gap: 6px; }

#viewport { position: relative; min-height: 200px; min-width: 0; overflow: hidden; background: var(--viewport-bg); }
#viewport canvas {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  color: var(--muted);
  background: var(--overlay);
  pointer-events: none;
}

.overlay.hidden { display: none; }

.details {
  border-top: 1px solid var(--line);
  background: var(--panel-2);
  max-height: 46vh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 2;
}
.details-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.details-head h2 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.downloads { margin-left: auto; display: flex; gap: 10px; font-size: 12px; }
.downloads a { color: var(--accent-2); text-decoration: none; }
.downloads a:hover { text-decoration: underline; }

.detail-panel { padding: 12px; overflow: visible; flex: 0 0 auto; display: flex; gap: 18px; align-items: flex-start; flex-wrap: wrap; }

.kv { display: grid; grid-template-columns: 130px minmax(0, 1fr); gap: 4px 12px; font-size: 12px; margin: 0; flex: 1 1 320px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.kv dd.error { color: var(--err); white-space: pre-wrap; }

.ref-image { display: flex; flex-direction: column; gap: 6px; font-size: 11px; color: var(--muted); }
.ref-image img {
  width: 120px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
}

.mfg { border-top: 1px solid var(--line); padding-bottom: 12px; }
.mfg.disabled { opacity: .5; pointer-events: none; }
.mfg-title { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 12px 12px 6px; }
.mfg-batch { display: inline-flex; align-items: center; gap: 6px; margin: 0; font-size: 11px; color: var(--muted); }
.mfg-batch select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 6px;
  font: inherit;
  font-size: 12px;
}
.mfg-methods { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 10px; padding: 12px; }
.mfg-card { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); padding: 10px 12px; box-shadow: var(--shadow); }
.mfg-card.best { border-color: var(--accent); }
.mfg-card header { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.mfg-card header span { font-size: 11px; color: var(--muted); }
.mfg-summary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; margin-bottom: 8px; }
.mfg-figure { background: var(--panel-2); border-radius: 8px; padding: 6px 8px; }
.mfg-figure b { display: block; font-size: 14px; }
.mfg-figure span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.mfg-table { width: 100%; border-collapse: collapse; font-size: 11.5px; margin: 4px 0 8px; }
.mfg-table caption { text-align: left; color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .08em; padding: 4px 0; }
.mfg-table th, .mfg-table td { text-align: left; padding: 3px 6px 3px 0; border-bottom: 1px solid var(--line); }
.mfg-table th { color: var(--muted); font-weight: 500; }
.mfg-table th:last-child, .mfg-table td:last-child { text-align: right; padding-right: 0; }
.mfg-notes { margin: 4px 0 0; padding-left: 16px; color: var(--muted); font-size: 11px; }
.mfg-bom { padding: 0 12px; overflow: auto; }
.mfg-bom table { width: 100%; border-collapse: collapse; font-size: 11.5px; }
.mfg-bom th, .mfg-bom td { padding: 4px 10px 4px 0; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.mfg-bom th { color: var(--muted); font-weight: 500; }
.mfg-bom tfoot td { font-weight: 600; }
#mfg-note { padding: 8px 12px 0; }

.size-summary { font-size: 11px; color: var(--muted); }

body.modal-open { overflow: hidden; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal.hidden { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(3, 6, 10, .6); }

.modal-card {
  position: relative;
  width: min(1080px, 100%);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .45);
}

.modal-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.modal-head button { margin-left: auto; }
.modal-body { overflow: auto; padding-bottom: 10px; }

.size-step { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.size-grid { display: grid; grid-template-columns: minmax(260px, 1.1fr) minmax(240px, 1fr); gap: 14px; align-items: start; }

.size-viewport {
  position: relative;
  height: 300px;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: var(--viewport-bg);
}

.size-viewport canvas {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

.size-controls { display: flex; flex-direction: column; gap: 8px; }
.size-controls label { margin: 0; }
.size-controls input[type="range"] { width: 100%; accent-color: var(--accent); }

.size-readout { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.size-readout b { display: block; font-size: 18px; }
.size-readout span { font-size: 11px; color: var(--muted); }

.size-fields { display: flex; align-items: center; gap: 6px; }
.size-fields label { min-width: 104px; }
.size-fields input, .size-fields select {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  padding: 6px 8px;
  font: inherit;
}
.size-fields select { flex: 0 0 auto; }
.size-fields button { flex: 0 0 auto; }

.size-presets { display: flex; flex-wrap: wrap; gap: 6px; }
.size-presets button { font-size: 11px; padding: 5px 9px; }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.cost-step.hidden { display: none; }

@media (max-width: 760px) {
  .size-grid { grid-template-columns: 1fr; }
  .size-viewport { height: 240px; }
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .viewer { min-height: 70vh; }
  .sidebar { overflow: visible; }
}
