:root {
  --bg: #f4f4f2;
  --panel: #ffffff;
  --ink: #1d1d1b;
  --muted: #6b6b66;
  --line: #e2e1dc;
  --red: #c41212;
  --red-ink: #ffffff;
  --carga: #e0a100;
  --fila: #2f6fd6;
  --saida: #1f9d62;
  --otro: #8a8a85;
  --exc: #3f8f2a;
  --wall: #5e5e5a;
  --path: #c41212;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161615; --panel: #20201e; --ink: #ecebe6; --muted: #a09f98; --line: #34332f;
    --red: #e0413b; --otro: #a5a59f; --wall: #b3b2ab;
  }
}
:root[data-theme="dark"] {
  --bg: #161615; --panel: #20201e; --ink: #ecebe6; --muted: #a09f98; --line: #34332f;
  --red: #e0413b; --otro: #a5a59f; --wall: #b3b2ab;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
}
h1 { font-size: 18px; margin: 0; letter-spacing: -0.01em; }
h1 span { color: var(--muted); font-weight: 400; }
.server { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.server i { width: 9px; height: 9px; border-radius: 50%; background: var(--otro); }
.server.ok i { background: var(--saida); }
.server.down i { background: var(--red); }

main {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); gap: 16px;
  padding: 16px; max-width: 1500px; margin: 0 auto;
}
@media (max-width: 900px) { main { grid-template-columns: 1fr; padding: 12px; gap: 12px; } }
@media (max-width: 520px) {
  .top { padding: 12px 16px; }
  h1 span { display: none; }
  .panel { padding: 12px; }
  .panel-head .actions { width: 100%; }
  .panel-head .actions .btn { flex: 1; text-align: center; padding: 11px 8px; font-size: 15px; }
  .drop img { max-width: 240px; }
  .busy { white-space: normal; width: max-content; max-width: 85%; }
}

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; min-width: 0; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
h2 { font-size: 16px; margin: 0; display: flex; align-items: center; gap: 8px; }
h2 b {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--red); color: var(--red-ink); font-size: 12px;
}
.actions, .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row { margin-top: 10px; }

.btn {
  appearance: none; border: 1px solid var(--red); background: var(--red); color: var(--red-ink);
  font: inherit; font-weight: 600; font-size: 14px; padding: 7px 14px; border-radius: 8px; cursor: pointer;
  text-decoration: none; display: inline-block;
}
.btn.ghost { background: transparent; color: var(--red); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn:not(:disabled):hover { filter: brightness(1.08); }

.stage { position: relative; }
.drop {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 22px; text-align: center; color: var(--muted);
}
.drop.over { border-color: var(--red); background: color-mix(in srgb, var(--red) 6%, transparent); }
.drop strong { color: var(--ink); }
.drop img { max-width: 320px; width: 100%; margin-top: 8px; border-radius: 6px; background: #fff; }

.sketch { position: relative; line-height: 0; }
.sketch img { width: 100%; height: auto; border-radius: 8px; display: block; }
.sketch svg { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.sketch svg .h { cursor: grab; }
.sketch svg .h:active { cursor: grabbing; }

.busy {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); line-height: 1.4;
  background: color-mix(in srgb, var(--panel) 92%, transparent); border: 1px solid var(--line);
  padding: 10px 14px; border-radius: 8px; display: flex; align-items: center; gap: 10px; white-space: nowrap;
}
.spin { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--red); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.hint { color: var(--muted); font-size: 13px; margin: 10px 0 6px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: 12px; color: var(--muted); }
.k::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; background: currentColor; }
.k.carga { color: var(--carga); } .k.fila { color: var(--fila); } .k.saida { color: var(--saida); } .k.otro { color: var(--otro); }
.k.exc { color: var(--exc); } .k.wall { color: var(--wall); } .k.path { color: var(--path); }
.k { font-weight: 600; }

.trucks { list-style: none; padding: 0; margin: 10px 0; display: flex; flex-wrap: wrap; gap: 8px; }
.trucks li { display: flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px 4px 10px; font-size: 13px; }
.trucks select { font: inherit; background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 2px 4px; }
.trucks button { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; padding: 0 4px; }

details { margin-top: 8px; }
summary { cursor: pointer; color: var(--muted); font-size: 13px; }
textarea {
  width: 100%; min-height: 220px; margin-top: 8px; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 8px;
}
.err { color: var(--red); font-size: 13px; }
.meta { color: var(--muted); font-size: 13px; }

.result {
  position: relative; aspect-ratio: 4 / 3; border-radius: 8px; overflow: hidden;
  background: #fafafa; border: 1px solid var(--line); display: grid; place-items: center;
}
.result img { width: 100%; height: 100%; object-fit: contain; display: block; }
.result .empty { color: #6b6b66; max-width: 36ch; text-align: center; padding: 16px; }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--ink); max-width: 360px; width: calc(100% - 32px); }
dialog::backdrop { background: rgb(0 0 0 / .35); }
dialog h3 { margin: 0 0 6px; }
dialog p { color: var(--muted); margin: 0 0 10px; font-size: 14px; }
dialog input { width: 100%; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); text-transform: uppercase; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); max-width: calc(100% - 32px);
  background: var(--ink); color: var(--panel); padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 10;
}

/* --- editor: herramientas --- */
.toolbar { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.tool {
  appearance: none; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px;
}
.tool.small { padding: 4px 10px; }
.tool:disabled { opacity: .4; cursor: default; }
.tool:not(:disabled):hover { border-color: var(--red); }
.sketch svg .hit { cursor: pointer; }

/* --- visor 3D --- */
.tabs { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.tab {
  appearance: none; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  background: transparent; color: var(--muted); border: 1px solid transparent; border-radius: 8px; padding: 5px 10px;
}
.tab.on { color: var(--ink); border-color: var(--line); background: var(--bg); }
.tab:disabled { opacity: .4; cursor: default; }
.persp { margin-left: auto; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.result canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.stale {
  position: absolute; right: 10px; top: 10px; font-size: 12px; line-height: 1.3; color: #444;
  background: rgb(255 255 255 / .9); border: 1px solid #ddd; border-radius: 6px; padding: 4px 8px;
}
@media (max-width: 520px) {
  .toolbar .tool { flex: 1; padding: 10px 6px; }
}

/* --- historial --- */
.panel.wide { grid-column: 1 / -1; }
.sub { font-size: 13px; color: var(--muted); margin: 14px 0 6px; font-weight: 600; }
.thumbs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.thumbs img { height: 72px; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 6px; border: 2px solid transparent; cursor: pointer; background: #fafafa; }
.thumbs img.on { border-color: var(--red); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.card {
  appearance: none; text-align: left; font: inherit; color: var(--ink); cursor: pointer; padding: 0; overflow: hidden;
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
.card.on { border-color: var(--red); }
.card img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: #fafafa; }
.card .noimg { aspect-ratio: 4 / 3; display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.card span { display: block; padding: 6px 8px; font-size: 12px; color: var(--muted); }
.card b { color: var(--ink); font-weight: 600; }
@media (max-width: 520px) { .cards { grid-template-columns: repeat(2, 1fr); } }
.top-right { display: flex; align-items: center; gap: 12px; }
