:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-2: #243044;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-dim: #0ea5e9;
  --success: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --diagram-bg: #f8fafc;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

body {
  display: grid;
  grid-template-columns: minmax(280px, 320px) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "header header"
    "toolbar workspace";
  min-height: 100vh;
}

.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 1rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo {
  font-size: 1.75rem;
  color: var(--accent);
}

.app-header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
}

.tagline {
  margin: 0.15rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.header-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn {
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

.btn-primary {
  background: var(--accent);
  color: #0f172a;
}

.btn-primary:hover {
  background: var(--accent-dim);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  border-color: var(--accent);
}

.btn-secondary.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.toolbar {
  grid-area: toolbar;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem;
  max-height: calc(100vh - 72px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: #475569 #1e293b;
}

.toolbar::-webkit-scrollbar {
  width: 10px;
}

.toolbar::-webkit-scrollbar-track {
  background: #1e293b;
  border-radius: 10px;
}

.toolbar::-webkit-scrollbar-thumb {
  background: #475569;
  border-radius: 10px;
  border: 2px solid #1e293b;
}

.toolbar h2 {
  margin: 0 0 1rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.tool-section {
  margin-bottom: 1.25rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.tool-section:last-child {
  border-bottom: none;
}

.tool-section h3 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
}

.section-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.82rem;
  font-family: inherit;
  cursor: pointer;
}

.section-toggle:hover {
  border-color: var(--accent);
}

.tool-grid {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.tool-grid-compact .tool-btn {
  font-size: 0.72rem;
  padding: 0.4rem 0.5rem;
}

.tool-grid.collapsed,
.tool-grid.section-panel-collapsed,
.section-panel-collapsed {
  display: none !important;
}

.tool-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
}

.tool-btn:hover {
  border-color: var(--accent);
}

.tool-btn.wide {
  justify-content: center;
}

.tool-btn.danger {
  border-color: rgba(248, 113, 113, 0.4);
  color: var(--danger);
}

.tool-btn.danger:hover:not(:disabled) {
  background: rgba(248, 113, 113, 0.12);
}

.tool-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tool-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.tool-select {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.78rem;
  font-family: inherit;
}

.header-select {
  width: 130px;
  margin-top: 0;
  padding: 0.3rem 0.45rem;
}

.orientation-select {
  width: 1px;
  opacity: 0;
  pointer-events: none;
  position: absolute;
}

.header-zoom-label {
  font-size: 0.78rem;
  color: var(--muted);
}

#canvas-zoom-range {
  width: 90px;
  accent-color: var(--accent);
}

.header-zoom-value {
  min-width: 40px;
  font-size: 0.78rem;
  color: var(--muted);
}

.tool-input,
.tool-color {
  width: 100%;
  margin-top: 0.2rem;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.78rem;
  font-family: inherit;
}

.tool-textarea {
  resize: vertical;
  min-height: 70px;
}

.tool-color {
  padding: 0.2rem;
  height: 34px;
}

.icon {
  width: 22px;
  height: 18px;
  flex-shrink: 0;
  border: 2px solid var(--muted);
  background: transparent;
}

.ico-terminal {
  border-radius: 50%;
}

.ico-process {
  border-radius: 2px;
}

.ico-decision {
  border-radius: 2px;
  transform: rotate(45deg) scale(0.72);
  width: 16px;
  height: 16px;
}

.ico-io {
  border-radius: 0;
  transform: skewX(-18deg);
  width: 18px;
}

.ico-variable {
  border-radius: 1px;
  width: 16px;
  height: 16px;
}

.ico-document {
  border-radius: 2px 2px 0 0;
  border-bottom-width: 0;
  height: 14px;
}

.ico-file_store {
  clip-path: polygon(0 0, 100% 0, 100% 70%, 55% 70%, 55% 100%, 45% 100%, 45% 70%, 0 70%);
  border-radius: 0;
}

.ico-database {
  border-radius: 50% 50% 0 0;
  height: 12px;
  border-bottom: none;
}

.ico-connector {
  border-radius: 50%;
  width: 16px;
  height: 16px;
}

.ico-offpage {
  clip-path: polygon(50% 0, 100% 38%, 82% 100%, 18% 100%, 0 38%);
  border-radius: 0;
}

.ico-manual {
  clip-path: polygon(0 0, 100% 0, 85% 100%, 15% 100%);
  border-radius: 0;
}

.ico-predefined {
  border-radius: 2px;
  box-shadow: inset 4px 0 0 var(--muted), inset -4px 0 0 var(--muted);
}

.ico-delay {
  border-radius: 2px 8px 8px 2px;
}

.hint {
  margin: 0.4rem 0 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.35;
}

.tool-divider {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 0.7rem 0;
}

.sim-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.4rem;
}

.sim-actions .tool-btn {
  justify-content: center;
  font-size: 0.74rem;
  padding: 0.42rem 0.5rem;
}

.verify-list {
  margin: 0.5rem 0 0;
  padding-left: 1.1rem;
  font-size: 0.78rem;
  color: var(--muted);
  max-height: 200px;
  overflow-y: auto;
}

.verify-list li.ok {
  color: var(--success);
}

.verify-list li.warn {
  color: var(--warn);
}

.verify-list li.err {
  color: var(--danger);
}

.slider-label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.slider-row input[type="range"] {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
}

.slider-value {
  font-size: 0.75rem;
  color: var(--muted);
  min-width: 2.5rem;
  text-align: right;
}

.workspace {
  grid-area: workspace;
  padding: 1rem;
  overflow: auto;
  background: linear-gradient(160deg, #0c1016 0%, var(--bg) 50%);
}

.workspace-inner {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
}

.workspace-inner.landscape {
  max-width: 920px;
}

.diagram {
  position: relative;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  height: min(88vh, 920px);
  min-height: 620px;
  background: var(--diagram-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.diagram.landscape {
  max-width: 920px;
  height: min(76vh, 620px);
  min-height: 460px;
}

.diagram-content {
  position: absolute;
  inset: 0;
  transform-origin: top left;
}

.diagram-meta {
  position: absolute;
  top: 8px;
  left: 8px;
  right: auto;
  z-index: 2;
  max-width: min(62%, 360px);
  min-width: 180px;
  padding: 0.45rem 0.55rem;
  border-radius: 8px;
  background: rgba(248, 250, 252, 0.9);
  border: 1px solid #cbd5e1;
  color: #0f172a;
  pointer-events: auto;
}

.diagram-meta-toggle {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 20px;
  height: 20px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: #f8fafc;
  color: #334155;
  font-size: 0.75rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.diagram-meta-toggle:hover {
  border-color: #94a3b8;
}

.diagram-meta h2 {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1.2;
  word-break: break-word;
  padding-right: 24px;
}

.diagram-meta p {
  margin: 0.25rem 0 0;
  font-size: 0.74rem;
  line-height: 1.25;
  word-break: break-word;
}

.diagram-meta span {
  display: inline-block;
  margin-top: 0.25rem;
  font-size: 0.72rem;
  color: #334155;
  line-height: 1.2;
  word-break: break-word;
}

.diagram-meta.meta-title-only {
  min-width: 120px;
  max-width: min(45%, 280px);
  padding: 0.32rem 0.48rem;
}

.diagram-meta.meta-title-only .diagram-meta-body {
  display: none;
}

.diagram-meta.meta-title-only h2 {
  font-size: 0.84rem;
}

.svg-lines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: auto;
  z-index: 0;
}

.svg-lines .flow-hit {
  pointer-events: stroke;
  cursor: pointer;
  stroke: transparent;
  stroke-width: 14;
  fill: none;
}

.svg-lines .flow-line {
  fill: none;
  stroke: #334155;
  stroke-width: 2;
  marker-end: url(#arrowhead);
  pointer-events: none;
}

.svg-lines .flow-line.selected {
  stroke: var(--accent-dim);
  stroke-width: 3;
}

.svg-lines .flow-line.sim-path {
  stroke: #22c55e !important;
  stroke-width: 3 !important;
}

.svg-lines .flow-line.flow-orthogonal {
  stroke-linejoin: round;
}

.svg-lines .flow-bend-handle {
  fill: #0ea5e9;
  stroke: #ffffff;
  stroke-width: 2;
  cursor: move;
}

.svg-lines .flow-bend-handle:hover {
  fill: #38bdf8;
}

.svg-lines .sim-runner {
  fill: #16a34a;
  stroke: #ffffff;
  stroke-width: 2;
}

.nodes-layer {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}

.nodes-layer .node,
.nodes-layer .port {
  pointer-events: auto;
}

.node {
  position: absolute;
  --node-fs: 13px;
  min-width: 120px;
  max-width: min(100%, 480px);
  background: #fff;
  border: 2px solid #475569;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
  cursor: grab;
  user-select: none;
  overflow: visible;
}

.node.has-custom-size {
  max-width: none;
  min-width: 0;
  min-height: 0;
}

.resize-handle {
  display: none;
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 13px;
  height: 13px;
  background: #0ea5e9;
  border: 2px solid #fff;
  border-radius: 3px;
  cursor: se-resize;
  z-index: 6;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.25);
}

.node.selected .resize-handle {
  display: block;
}

.resize-handle:hover {
  background: #38bdf8;
}

.node:active {
  cursor: grabbing;
}

.node.selected {
  outline: 3px solid #0ea5e9;
  outline-offset: 2px;
  z-index: 2;
}

.node.sim-current {
  outline: 3px solid #22c55e;
  outline-offset: 2px;
}

/* Terminal — óvalo / elipse (inicio / fin) */
.node-type-terminal {
  border-radius: 999px;
  min-height: 52px;
  min-width: 100px;
}

/* Proceso — rectángulo */
.node-type-process {
  border-radius: 4px;
}

/* Variable declarada/uso — cuadrado */
.node-type-variable {
  min-width: 90px;
  min-height: 90px;
  width: 90px;
  height: 90px;
  border-radius: 2px;
}

.node-type-variable.has-custom-size {
  min-width: 0;
  min-height: 0;
  width: auto;
  height: auto;
}

/* Decisión — rombo (clip-path; el cuadro delimitador sigue siendo rectangular) */
.node-type-decision {
  border-radius: 0;
  min-width: 126px;
  min-height: 126px;
  width: 126px;
  height: 126px;
  transform: rotate(45deg);
  transform-origin: center;
  background: #fff;
}

.node-type-decision .node-label {
  text-align: center;
  transform: rotate(-45deg);
  transform-origin: center;
  margin-top: 20px;
}

.node-type-decision .node-body {
  text-align: center;
  min-height: 1.05rem;
  transform: rotate(-45deg);
  transform-origin: center;
  width: 72%;
  margin: 8px auto 0;
  resize: none;
}

/* Entrada / salida — paralelogramo */
.node-type-io {
  border-radius: 0;
  transform: skewX(-16deg);
  transform-origin: center;
}

.node-type-io .node-label,
.node-type-io .node-body {
  transform: skewX(16deg);
}

.node-type-io .node-body {
  width: 92%;
  margin: 0 auto;
}

/* Documento — rectángulo con base ondulada (onda decorativa) */
.node-type-document {
  border-radius: 4px 4px 0 0;
  border-bottom: none;
  padding-bottom: 18px;
}

.node-type-document::after {
  content: "";
  position: absolute;
  left: -2px;
  right: -2px;
  bottom: 0;
  height: 16px;
  pointer-events: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='48' height='16' viewBox='0 0 48 16'%3E%3Cpath d='M0 2h48M0 2c4 10 8-6 12 2s8-8 12 0 8-8 12 0 8-8 12 0 8-8 12 2' fill='none' stroke='%23475569' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: repeat-x;
  background-size: 48px 16px;
  background-position: bottom;
}

/* Datos almacenados — rectángulo abierto por abajo */
.node-type-file_store {
  border: none;
  background: transparent;
  box-shadow: none;
  clip-path: polygon(
    0% 0%,
    100% 0%,
    100% 78%,
    58% 78%,
    58% 100%,
    42% 100%,
    42% 78%,
    0% 78%
  );
  padding: 0.5rem 0.65rem 1.1rem;
  filter: drop-shadow(0 2px 4px rgba(15, 23, 42, 0.08));
}

.node-type-file_store .node-label,
.node-type-file_store .node-body {
  background: #fff;
  border: 2px solid #475569;
  border-bottom: none;
  margin: 0;
}

.node-type-file_store .node-label {
  border-radius: 2px 2px 0 0;
  border-bottom: 1px solid #e2e8f0;
}

.node-type-file_store .node-body {
  border-top: none;
  border-radius: 0 0 2px 2px;
  min-height: 2.25rem;
}

/* Base de datos — cilindro simplificado */
.node-type-database {
  border: none;
  background: transparent;
  box-shadow: none;
  padding: 18px 0.5rem 0.5rem;
  min-height: 88px;
}

.node-type-database::before {
  content: "";
  position: absolute;
  top: 0;
  left: -2px;
  right: -2px;
  height: 18px;
  border: 2px solid #475569;
  border-radius: 50%;
  background: linear-gradient(180deg, #f1f5f9 0%, #fff 55%);
  z-index: 0;
}

.node-type-database::after {
  content: "";
  position: absolute;
  top: 11px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border: 2px solid #475569;
  border-top: none;
  border-radius: 0 0 14px 14px;
  background: #fff;
  z-index: 0;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.06);
}

.node-type-database .node-label,
.node-type-database .node-body {
  position: relative;
  z-index: 1;
}

/* Conector — círculo pequeño */
.node-type-connector {
  min-width: 44px;
  width: 48px;
  min-height: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.25rem;
}

.node-type-connector .node-label {
  display: none;
}

.node-type-connector .node-body {
  min-height: 1.25rem;
  padding: 0.35rem;
  text-align: center;
  resize: none;
  font-size: var(--node-fs);
  font-weight: 700;
}

/* Conector fuera de página — pentágono */
.node-type-offpage {
  border-radius: 0;
  clip-path: polygon(50% 0%, 100% 36%, 82% 100%, 18% 100%, 0% 36%);
  padding: 1.1rem 0.65rem 0.95rem;
  min-height: 100px;
}

.node-type-offpage .node-label,
.node-type-offpage .node-body {
  text-align: center;
}

/* Entrada manual — trapecio */
.node-type-manual_input {
  border-radius: 0;
  clip-path: polygon(0% 0%, 100% 0%, 88% 100%, 12% 100%);
  padding-left: 0.75rem;
  padding-right: 0.75rem;
}

/* Proceso predefinido — barras verticales internas */
.node-type-predefined {
  border-radius: 4px;
  background-color: #fff;
  background-image: linear-gradient(
      90deg,
      transparent 0,
      transparent 9px,
      #cbd5e1 9px,
      #cbd5e1 11px,
      transparent 11px
    ),
    linear-gradient(
      90deg,
      transparent calc(100% - 11px),
      #cbd5e1 calc(100% - 11px),
      #cbd5e1 calc(100% - 9px),
      transparent calc(100% - 9px)
    );
}

/* Demora — forma en D (lado derecho redondeado) */
.node-type-delay {
  border-radius: 4px 36px 36px 4px;
  min-width: 118px;
}

.node-label {
  font-size: calc(var(--node-fs) * 0.72);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #64748b;
  padding: 0.35rem 0.5rem 0;
  font-weight: 600;
  display: none;
}

.node-body {
  display: block;
  width: 100%;
  min-height: 2.5rem;
  padding: 0.35rem 0.5rem 0.5rem;
  border: none;
  resize: vertical;
  font-size: var(--node-fs);
  font-family: inherit;
  color: #0f172a;
  background: transparent;
  cursor: text;
  user-select: text;
}

.node-body:focus {
  outline: none;
}

.port {
  position: absolute;
  width: 12px;
  height: 12px;
  margin: -6px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #64748b;
  z-index: 3;
  pointer-events: auto;
  cursor: crosshair;
  transition: transform 0.12s, border-color 0.12s;
}

.port:hover {
  transform: scale(1.15);
  border-color: var(--accent-dim);
}

.connect-mode .port {
  box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.35);
}

.port-top {
  top: 0;
  left: 50%;
}

.port-right {
  top: 50%;
  right: 0;
}

.port-bottom {
  bottom: 0;
  left: 50%;
}

.port-left {
  top: 50%;
  left: 0;
}

.flow-label-bg {
  fill: #f8fafc;
  stroke: #cbd5e1;
  stroke-width: 1;
  rx: 4;
  ry: 4;
  pointer-events: none;
}

.flow-label-text {
  fill: #0f172a;
  font-size: 11px;
  font-family: var(--font);
  pointer-events: none;
}

.footer-note {
  margin: 0.75rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.toast {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  padding: 0.65rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  z-index: 100;
  max-width: 320px;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 900px) {
  body {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "toolbar"
      "workspace";
  }

  .toolbar {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .tool-grid {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .tool-btn:not(.wide) {
    flex: 1;
    min-width: 140px;
  }
}
