:root {
  --bg: #0f1115;
  --bg-panel: #171a21;
  --bg-elev: #1f232c;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9aa3b5;
  --accent: #4fd1a5;
  --accent-ink: #062a1f;
  --warn: #f6c453;
  --danger: #ff6b6b;
  --ok: #4fd1a5;
  --radius: 10px;
  --panel-w: 400px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--bg-panel);
}
.brand { font-weight: 700; letter-spacing: .2px; }
.brand span { color: var(--accent); font-weight: 500; margin-left: 6px; }
.formats { color: var(--muted); font-size: 13px; }

.layout {
  flex: 1; display: grid; grid-template-columns: 1fr var(--panel-w); min-height: 0;
}

/* ---- Visor ---- */
.viewer { position: relative; min-height: 55vh; background: radial-gradient(ellipse at 50% 40%, #1b1f28 0%, #0f1115 70%); }
#canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.dropzone {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(15,17,21,.75); backdrop-filter: blur(2px);
  transition: background .15s;
}
.dropzone.drag { background: rgba(79,209,165,.15); }
.dropzone.hidden { display: none; }
.dropzone-inner {
  text-align: center; border: 2px dashed var(--line); border-radius: var(--radius);
  padding: 32px 24px; max-width: 460px; background: var(--bg-panel);
}
.dropzone.drag .dropzone-inner { border-color: var(--accent); }
.dz-title { font-size: 22px; font-weight: 700; margin: 0 0 6px; }
.dz-sub { margin: 0 0 14px; color: var(--muted); }
.dz-formats, .dz-privacy, .dz-blend { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.dz-blend { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.link { color: var(--accent); cursor: pointer; text-decoration: underline; }

.viewer.drag { outline: 2px dashed var(--accent); outline-offset: -2px; }

/* Pestañas: una por pieza */
.tabs {
  position: absolute; left: 0; right: 0; top: 0; z-index: 2;
  display: flex; gap: 4px; padding: 6px 8px 0; overflow-x: auto; scrollbar-width: thin;
  background: linear-gradient(var(--bg-panel), transparent); pointer-events: none;
}
.tab {
  display: inline-flex; align-items: center; flex: 0 0 auto; max-width: 220px; pointer-events: auto;
  background: var(--bg-elev); border: 1px solid var(--line); border-bottom: 0; border-radius: 8px 8px 0 0;
}
.tab.active { background: var(--bg); border-color: var(--accent); }
.tab.warn .tab-label::before { content: '⚠ '; color: var(--warn); }
.tab-label {
  background: none; border: 0; color: var(--muted); font: inherit; font-size: 13px; padding: 6px 4px 6px 10px;
  cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px;
}
.tab.active .tab-label { color: var(--text); font-weight: 600; }
.tab-close {
  background: none; border: 0; color: var(--muted); font-size: 16px; line-height: 1; padding: 4px 8px; cursor: pointer; border-radius: 4px;
}
.tab-close:hover { color: var(--danger); background: rgba(255,107,107,.12); }

.viewer-tools {
  position: absolute; left: 12px; top: 12px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
}
.tabs:not([hidden]) ~ .viewer-tools { top: 48px; }
.viewer-tools button, .tool-select select {
  background: var(--bg-elev); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 10px; font: inherit; font-size: 13px; cursor: pointer;
}
.viewer-tools button:hover { border-color: var(--accent); }
.viewer-tools button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tool-select { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

.loading {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: var(--bg-elev); padding: 10px 16px; border-radius: 8px; border: 1px solid var(--line);
}

/* Tarjeta de la pieza activa, superpuesta al visor */
.piece-info {
  position: absolute; left: 12px; bottom: 12px; z-index: 2; width: min(380px, calc(100% - 24px));
  background: rgba(23,26,33,.88); backdrop-filter: blur(4px); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
.piece-info h2 { font-size: 14px; margin: 0 0 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.piece-info .metrics { background: none; padding: 0; }
.piece-info .alerts { margin-top: 8px; }
.piece-info .alert { margin-bottom: 4px; }

/* ---- Panel ---- */
.panel { background: var(--bg-panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.panel h1 { font-size: 22px; margin: 0 0 10px; }
.panel h2 { font-size: 15px; margin: 0 0 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 14px 0 6px; display: flex; justify-content: space-between; align-items: baseline; }
.panel h3 .opt { text-transform: none; letter-spacing: 0; font-size: 11px; }
.panel-empty { padding: 20px; }
.panel-empty p, .panel-empty li { color: var(--muted); }
.panel-empty ul { padding-left: 18px; }

/* Con modelo: zona que hace scroll si hace falta + botón fijo abajo */
.panel-model { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.panel-scroll { padding: 14px 16px 8px; overflow-y: auto; flex: 1; min-height: 0; }
.panel-cta { padding: 10px 16px 14px; border-top: 1px solid var(--line); background: var(--bg-panel); }

.alerts:empty { display: none; }
.alert { padding: 6px 10px; border-radius: 6px; font-size: 12.5px; margin-bottom: 6px; border: 1px solid; line-height: 1.35; }
.alert.warn { color: var(--warn); border-color: rgba(246,196,83,.4); background: rgba(246,196,83,.08); }
.alert.danger { color: var(--danger); border-color: rgba(255,107,107,.4); background: rgba(255,107,107,.08); }
.alert.ok { color: var(--ok); border-color: rgba(79,209,165,.4); background: rgba(79,209,165,.08); }

.metrics { background: var(--bg-elev); border-radius: 8px; padding: 8px 10px; }
.metric-main { font-weight: 600; font-size: 14px; }
.metric-main .sep { color: var(--muted); margin: 0 6px; }
.metric-flags { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 4px; font-size: 12px; }
.flag::before { content: '● '; font-size: 9px; vertical-align: 1px; }
.flag.ok { color: var(--ok); }
.flag.bad { color: var(--danger); }
.flag.muted { color: var(--muted); }
.flag.muted::before { content: ''; }

form label, .dialog label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
form input, form select, form textarea, .dialog input, .dialog textarea {
  display: block; width: 100%; margin-top: 3px; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; font: inherit;
}
form input:focus, form select:focus, form textarea:focus, .dialog input:focus, .dialog textarea:focus { outline: none; border-color: var(--accent); }
.invalid { border-color: var(--danger) !important; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.options { margin-top: 2px; }
.options label { margin-bottom: 0; }
.opt { font-weight: normal; opacity: .7; }
.err { display: block; color: var(--danger); font-size: 12px; min-height: 0; }
.err:empty { display: none; }
.hp { position: absolute; left: -9999px; width: 1px; height: 1px; overflow: hidden; }

/* Lista de piezas del pedido */
.pieces { display: flex; flex-direction: column; gap: 5px; }
.piece-row {
  display: grid; grid-template-columns: 1fr 1fr 56px;
  grid-template-areas: "name name price" "meta meta price" "color colors qty";
  gap: 3px 6px; align-items: center; background: var(--bg-elev); border-radius: 8px; padding: 6px 8px;
}
.piece-name { grid-area: name; font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.piece-meta { grid-area: meta; color: var(--muted); font-size: 11.5px; }
.piece-row .piece-color { grid-area: color; }
.piece-row .piece-colors { grid-area: colors; }
.piece-row .piece-qty { grid-area: qty; text-align: center; }
.piece-row select, .piece-row input { margin: 0; padding: 5px 6px; font-size: 12.5px; }
.piece-price { grid-area: price; font-weight: 600; min-width: 56px; text-align: right; align-self: start; font-size: 14px; }
.piece-price small { display: block; color: var(--warn); font-weight: normal; font-size: 10.5px; }

button.primary {
  width: 100%; padding: 12px; border: 0; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font: inherit; font-weight: 700; cursor: pointer;
}
button.primary:disabled { opacity: .5; cursor: wait; }
button.cta { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; font-size: 15px; transition: opacity .15s; }
button.cta strong { font-size: 20px; }
button.cta.busy { opacity: .6; }
.form-error { color: var(--danger); font-size: 13px; margin: 6px 0; }
.legal { color: var(--muted); font-size: 11.5px; margin: 10px 0 0; }
.panel-done { background: var(--bg-elev); border-radius: 8px; padding: 14px; margin-top: 12px; }
.panel-done h3 { margin-top: 0; color: var(--accent); }

/* Popup con los datos del cliente */
.dialog { background: var(--bg-panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; width: min(440px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0,0,0,.6); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.dialog-head h3 { margin: 0; font-size: 16px; }
.dialog-close { background: none; border: 0; color: var(--muted); font-size: 22px; line-height: 1; cursor: pointer; padding: 2px 6px; }
.dialog-close:hover { color: var(--text); }
.dialog-sum { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.dialog-sum strong { color: var(--accent); font-size: 18px; margin-left: 6px; }

/* ---- Responsive ---- */
@media (max-width: 900px) {
  :root { --panel-w: 100%; }
  .layout { grid-template-columns: 1fr; }
  .viewer { min-height: 50vh; height: 50vh; }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
  .topbar .formats { display: none; }
}
