:root {
  --navy: #0f2540;
  --navy-2: #173b62;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --blue-soft: #eaf1ff;
  --bg: #eef2f6;
  --surface: #ffffff;
  --line: #dbe3ec;
  --text: #162033;
  --muted: #65758b;
  --success: #0f766e;
  --success-soft: #e7f7f2;
  --danger: #b42318;
  --shadow: 0 18px 50px rgba(15, 37, 64, .10);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select { font: inherit; }
button { cursor: pointer; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 22px clamp(18px, 4vw, 52px);
  color: #fff;
  background: linear-gradient(135deg, var(--navy), var(--navy-2));
}
.topbar h1 { margin: 2px 0 4px; font-size: clamp(1.55rem, 3vw, 2.1rem); }
.eyebrow { margin: 0; font-size: .72rem; font-weight: 850; letter-spacing: .12em; text-transform: uppercase; opacity: .72; }
.eyebrow.dark { color: var(--muted); }
.subtitle { margin: 0; max-width: 720px; opacity: .82; }

.app-shell {
  width: min(1540px, 100%);
  margin: 0 auto;
  padding: 22px;
  display: grid;
  grid-template-columns: minmax(340px, 405px) minmax(0, 1fr);
  align-items: start;
  gap: 22px;
}
.panel, .workspace {
  background: var(--surface);
  border: 1px solid rgba(15, 37, 64, .08);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.controls {
  position: sticky;
  top: 14px;
  max-height: calc(100vh - 28px);
  overflow: auto;
  padding: 16px 18px 18px;
  scrollbar-width: thin;
}
.control-section { padding: 15px 0; border-bottom: 1px solid var(--line); }
.control-section h2 { margin: 0 0 12px; font-size: .96rem; }
.compact-section { border-bottom: 0; padding-bottom: 5px; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.privacy-card {
  display: flex;
  gap: 10px;
  margin-bottom: 2px;
  padding: 11px 12px;
  border: 1px solid #bfe6d9;
  border-radius: 12px;
  background: var(--success-soft);
  color: #155e54;
}
.privacy-card strong { display: block; font-size: .81rem; }
.privacy-card p { margin: 2px 0 0; color: #42756d; font-size: .7rem; line-height: 1.4; }
.privacy-icon { flex: 0 0 23px; display: grid; place-items: center; width: 23px; height: 23px; border-radius: 50%; background: var(--success); color: #fff; font-size: .76rem; font-weight: 900; }

label { display: grid; gap: 5px; font-size: .79rem; font-weight: 750; color: #3b4b61; }
input[type="number"], select {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  color: var(--text);
  background: #fff;
  border: 1px solid #cfd9e5;
  border-radius: 9px;
  outline: none;
}
input[type="number"]:focus, select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37, 99, 235, .11); }
input:disabled, select:disabled { opacity: .58; cursor: not-allowed; }
input[type="range"] { width: 100%; accent-color: var(--blue); }
input[type="color"] { width: 100%; height: 40px; padding: 3px; border: 1px solid #cfd9e5; border-radius: 9px; background: #fff; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.button-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.check-inline { display: flex; grid-template-columns: none; flex-direction: row; align-items: center; gap: 7px; font-weight: 750; white-space: nowrap; }

.upload-box {
  min-height: 94px;
  place-items: center;
  align-content: center;
  text-align: center;
  color: var(--navy);
  border: 1.5px dashed #91a5bd;
  border-radius: 13px;
  background: #f8fafc;
  transition: .18s ease;
}
.upload-box:hover, .upload-box.is-dragover { border-color: var(--blue); background: var(--blue-soft); transform: translateY(-1px); }
.upload-icon { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 50%; background: var(--navy); color: #fff; font-size: 1.15rem; }
.upload-box small { color: var(--muted); font-weight: 500; }
.image-info { margin-top: 8px; padding: 9px 10px; border-radius: 9px; background: #f5f7fa; color: var(--muted); font-size: .74rem; }
.is-hidden { display: none !important; }

.btn {
  min-height: 40px;
  padding: 9px 13px;
  border: 0;
  border-radius: 10px;
  font-weight: 800;
  transition: transform .15s ease, opacity .15s ease, background .15s ease, border-color .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .42; cursor: not-allowed; }
.btn-primary { color: #fff; background: var(--blue); }
.btn-primary:hover:not(:disabled) { background: var(--blue-dark); }
.btn-soft { color: var(--navy); background: #edf2f7; }
.btn-soft:hover:not(:disabled) { background: #e2e8f0; }
.btn-outline { color: var(--navy); background: #fff; border: 1px solid #b8c5d4; }
.btn-outline:hover:not(:disabled) { border-color: var(--navy); background: #f8fafc; }
.btn-ghost { color: #fff; background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.18); }
.btn-small { min-height: 36px; padding: 7px 10px; font-size: .75rem; }
.text-button { padding: 0; border: 0; color: var(--blue); background: transparent; font-size: .73rem; font-weight: 800; }
.text-button:disabled { color: #9aa8b8; cursor: not-allowed; }
.icon-button { width: 36px; height: 36px; border: 0; border-radius: 9px; background: #edf2f7; color: var(--navy); font-size: 1.45rem; line-height: 1; }

.measure-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; }
.calculated-size { min-width: 0; padding: 8px 10px; border-radius: 8px; color: var(--muted); background: #f5f7fa; font-size: .74rem; text-align: right; }
.hint, .server-note { margin: 7px 0 0; color: var(--muted); font-size: .7rem; line-height: 1.42; }
.target-size-row { margin-top: 11px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.target-fields { display: grid; grid-template-columns: 80px 64px; gap: 5px; }
.primary-actions { display: grid; grid-template-columns: 1.25fr .9fr; gap: 9px; margin-top: 14px; }
.status { min-height: 18px; margin: 8px 0 0; text-align: center; color: var(--muted); font-size: .76rem; }
.status.success { color: var(--success); }
.status.error { color: var(--danger); }

.workspace { min-width: 0; overflow: hidden; display: flex; min-height: calc(100vh - 132px); flex-direction: column; }
.workspace-toolbar {
  min-height: 68px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
  border-bottom: 1px solid var(--line);
}
.preset-picker { width: min(390px, 52%); grid-template-columns: auto minmax(180px, 1fr); align-items: center; gap: 9px; }
.preset-picker select { min-width: 0; }
.workspace-meta { display: grid; justify-items: end; gap: 3px; color: var(--muted); font-size: .74rem; white-space: nowrap; }
#canvasSizeBadge { font-weight: 800; color: var(--navy); }
.stage {
  position: relative;
  flex: 1;
  min-height: 590px;
  padding: 32px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background-color: #dfe5eb;
  background-image:
    linear-gradient(45deg, #cfd7df 25%, transparent 25%),
    linear-gradient(-45deg, #cfd7df 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #cfd7df 75%),
    linear-gradient(-45deg, transparent 75%, #cfd7df 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}
#editorCanvas {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 235px);
  background: #fff;
  box-shadow: 0 22px 55px rgba(15, 37, 64, .22);
  touch-action: none;
  cursor: grab;
}
#editorCanvas.dragging { cursor: grabbing; }
.empty-state { position: absolute; display: grid; place-items: center; gap: 6px; color: #526477; text-align: center; pointer-events: none; }
.empty-state span { max-width: 330px; font-size: .8rem; }
.empty-icon { font-size: 2.3rem; opacity: .6; }
.workspace-help { padding: 10px 16px; display: flex; justify-content: center; flex-wrap: wrap; gap: 5px 18px; border-top: 1px solid var(--line); color: var(--muted); font-size: .72rem; text-align: center; }

.print-dialog {
  width: min(480px, calc(100% - 24px));
  padding: 0;
  color: var(--text);
  border: 0;
  border-radius: 16px;
  box-shadow: 0 28px 90px rgba(15, 37, 64, .32);
}
.print-dialog::backdrop { background: rgba(9, 20, 35, .58); backdrop-filter: blur(2px); }
.dialog-head { padding: 17px 18px 13px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.dialog-head h2 { margin: 2px 0 0; font-size: 1.12rem; }
.dialog-body { padding: 17px 18px; }
.dialog-actions { padding: 13px 18px 17px; display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); }
.check-card { margin-top: 13px; padding: 11px; display: flex; grid-template-columns: auto 1fr; align-items: flex-start; gap: 9px; border: 1px solid var(--line); border-radius: 10px; background: #fafbfd; }
.check-card strong, .check-card small { display: block; }
.check-card small { margin-top: 2px; color: var(--muted); font-weight: 500; }
.print-summary { margin-top: 13px; padding: 11px 12px; border-radius: 10px; background: #f2f6fa; color: #45566c; font-size: .78rem; line-height: 1.5; }
.print-summary strong { color: var(--navy); }
.print-summary.warning { color: var(--danger); background: #fff2f0; }

@media (max-width: 980px) {
  .app-shell { grid-template-columns: 1fr; }
  .controls { position: static; max-height: none; overflow: visible; }
  .workspace { min-height: 640px; }
  .stage { min-height: 520px; }
  #editorCanvas { max-height: 480px; }
}
@media (max-width: 640px) {
  .topbar { align-items: flex-start; padding: 17px; }
  .subtitle { font-size: .83rem; }
  .app-shell { padding: 10px; gap: 10px; }
  .panel, .workspace { border-radius: 14px; }
  .controls { padding: 13px; }
  .grid-2 { grid-template-columns: 1fr 1fr; gap: 8px; }
  .measure-actions { align-items: stretch; flex-direction: column; }
  .calculated-size { text-align: left; }
  .workspace { min-height: 570px; }
  .workspace-toolbar { align-items: stretch; flex-direction: column; gap: 9px; }
  .preset-picker { width: 100%; grid-template-columns: 1fr; }
  .workspace-meta { justify-items: start; white-space: normal; }
  .stage { min-height: 420px; padding: 17px; }
  #editorCanvas { max-height: 380px; }
  .workspace-help { justify-content: flex-start; text-align: left; }
  .target-size-row { align-items: flex-start; flex-direction: column; }
  .primary-actions { position: sticky; bottom: 0; margin: 12px -13px -13px; padding: 11px 13px; background: rgba(255,255,255,.96); border-top: 1px solid var(--line); z-index: 3; }
}
@media (max-width: 390px) {
  .grid-2, .button-grid { grid-template-columns: 1fr; }
  .primary-actions { grid-template-columns: 1fr 1fr; }
}

/* Mejoras v3: orientación superior, peso dinámico y redimensión directa */
.weight-estimate-card {
  margin-top: 10px;
  padding: 10px 11px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 2px 10px;
  border: 1px solid #cfe0f5;
  border-radius: 10px;
  background: #f5f9ff;
}
.weight-estimate-card span { color: #526477; font-size: .73rem; font-weight: 750; }
.weight-estimate-card strong { color: var(--navy); font-size: .94rem; }
.weight-estimate-card small { grid-column: 1 / -1; color: var(--muted); font-size: .66rem; line-height: 1.35; }
.measure-actions .calculated-size { width: 100%; text-align: left; }
.workspace-toolbar-main {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 15px;
}
.orientation-picker {
  display: grid;
  gap: 5px;
  flex: 0 0 auto;
  color: #3b4b61;
  font-size: .74rem;
  font-weight: 750;
}
.segmented-control {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border: 1px solid #cfd9e5;
  border-radius: 10px;
  background: #edf2f7;
}
.segment {
  min-height: 34px;
  padding: 6px 11px;
  border: 0;
  border-radius: 7px;
  color: #526477;
  background: transparent;
  font-size: .74rem;
  font-weight: 800;
}
.segment.is-active {
  color: #fff;
  background: var(--navy);
  box-shadow: 0 3px 9px rgba(15, 37, 64, .18);
}
#editorCanvas { cursor: default; }
#editorCanvas.dragging { cursor: grabbing; }

@media (max-width: 1120px) {
  .workspace-toolbar { align-items: flex-start; }
  .workspace-toolbar-main { align-items: stretch; flex-direction: column; gap: 9px; }
  .orientation-picker { grid-template-columns: auto auto; align-items: center; }
  .preset-picker { width: min(460px, 100%); }
}

@media (max-width: 640px) {
  .workspace-toolbar-main { width: 100%; }
  .orientation-picker { grid-template-columns: 1fr; }
  .segmented-control { width: 100%; }
  .segment { min-height: 38px; }
  .weight-estimate-card { grid-template-columns: 1fr auto; }
}
