:root,
html[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-2: #243044;
  --border: #3d4f66;
  --text: #e8eef4;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-dim: #0ea5e9;
  --on-accent: #0f1419;
  --success: #4ade80;
  --danger: #f87171;
  --warning: #fbbf24;
  --manual: #a78bfa;
  --auto: #34d399;
  --canvas-bg: #121820;
  --overlay: rgba(0, 0, 0, 0.55);
  --radius: 10px;
  --font: "Segoe UI", system-ui, sans-serif;
}

html[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #e2e8f0;
  --border: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #0284c7;
  --accent-dim: #0ea5e9;
  --on-accent: #ffffff;
  --success: #15803d;
  --danger: #b91c1c;
  --warning: #b45309;
  --manual: #7c3aed;
  --auto: #059669;
  --canvas-bg: #e2e8f0;
  --overlay: rgba(15, 23, 42, 0.35);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 100;
}

.header-start {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.menu-toggle {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 44px;
  height: 44px;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  cursor: pointer;
}

.menu-toggle span {
  display: block;
  height: 2px;
  width: 100%;
  background: var(--text);
  border-radius: 1px;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius);
  cursor: pointer;
}

.theme-toggle:hover {
  border-color: var(--accent);
}

.theme-toggle svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.theme-toggle .theme-icon-sun {
  display: none;
}

.theme-toggle .theme-icon-moon {
  display: none;
}

html[data-theme="dark"] .theme-toggle .theme-icon-sun {
  display: block;
}

html[data-theme="light"] .theme-toggle .theme-icon-moon {
  display: block;
}

.nav-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 200;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.nav-overlay.is-open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.nav-drawer {
  position: fixed;
  top: 0;
  left: 0;
  width: min(300px, 88vw);
  height: 100vh;
  z-index: 210;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem;
  padding-top: max(1rem, env(safe-area-inset-top));
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  transform: translateX(-105%);
  visibility: hidden;
  pointer-events: none;
  transition: transform 0.22s ease, visibility 0.22s ease;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
}

.nav-drawer.is-open {
  transform: translateX(0);
  visibility: visible;
  pointer-events: auto;
}

.nav-drawer-title {
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.nav-drawer .nav-btn {
  width: 100%;
  text-align: left;
}

.nav-drawer-footer {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.nav-drawer-footer a {
  color: var(--accent);
}

body.nav-open {
  overflow: hidden;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--manual));
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
}

.subtitle {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.main-nav {
  display: none;
}

.nav-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
}

.nav-btn:hover {
  border-color: var(--accent);
}

.nav-btn.is-active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

.app-main {
  flex: 1;
  padding: 1.25rem 1.5rem;
  overflow: auto;
}

.app-footer {
  padding: 0.75rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.85rem;
}

.app-footer a {
  color: var(--accent);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.panel-title {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0.45rem 0.85rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.875rem;
}

.btn:hover {
  border-color: var(--accent);
}

.btn-primary {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

.btn-success {
  background: #166534;
  border-color: var(--success);
  color: var(--success);
}

.btn-danger {
  background: #7f1d1d;
  border-color: var(--danger);
  color: var(--danger);
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th,
td {
  text-align: left;
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

th {
  color: var(--muted);
  font-weight: 500;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-borrador {
  background: #422006;
  color: var(--warning);
}

.badge-listo {
  background: #14532d;
  color: var(--success);
}

.badge-curso {
  background: #1e3a5f;
  color: var(--accent);
}

.badge-completada {
  background: #14532d;
  color: var(--success);
}

.badge-rechazada {
  background: #7f1d1d;
  color: var(--danger);
}

.form-grid {
  display: grid;
  gap: 0.75rem;
}

.form-row label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.form-row input,
.form-row select,
.form-row textarea {
  width: 100%;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.form-row textarea {
  min-height: 72px;
  resize: vertical;
}

.split {
  display: grid;
  gap: 1rem;
}

@media (min-width: 960px) {
  .split-designer {
    grid-template-columns: 220px 1fr 300px;
    min-height: calc(100vh - 180px);
  }
}

.palette {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.palette-item {
  padding: 0.6rem;
  border: 1px dashed var(--border);
  border-radius: 8px;
  cursor: grab;
  font-size: 0.85rem;
  user-select: none;
}

.palette-item:active {
  cursor: grabbing;
}

.palette-inicio { border-color: var(--accent); }
.palette-manual { border-color: var(--manual); }
.palette-auto { border-color: var(--auto); }
.palette-fin { border-color: var(--danger); }

.canvas-wrap {
  position: relative;
  min-height: 480px;
  background:
    linear-gradient(var(--border) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(90deg, var(--border) 1px, transparent 1px) 0 0 / 24px 24px;
  background-color: var(--canvas-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: auto;
}

.canvas {
  position: relative;
  min-width: 1200px;
  min-height: 600px;
}

.canvas svg.connections {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.flow-node {
  position: absolute;
  min-width: 140px;
  max-width: 180px;
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--surface);
  cursor: move;
  font-size: 0.8rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

.flow-node.is-selected {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.flow-node .node-kind {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.flow-node .node-name {
  font-weight: 600;
  margin-top: 0.2rem;
}

.node-inicio { border-color: var(--accent); }
.node-manual { border-color: var(--manual); }
.node-automatica { border-color: var(--auto); }
.node-fin { border-color: var(--danger); }

.props-section {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.props-section h4 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.chip {
  font-size: 0.75rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.chip button {
  margin-left: 0.35rem;
  border: none;
  background: none;
  color: var(--danger);
  cursor: pointer;
  padding: 0;
}

.validation-list {
  margin: 0.5rem 0 0;
  padding-left: 1.1rem;
  font-size: 0.8rem;
  color: var(--danger);
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--muted);
}

.modal-root {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal-root[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 640px;
  width: 100%;
  max-height: 90vh;
  overflow: auto;
  padding: 1.25rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.screen-designer {
  display: grid;
  gap: 1rem;
}

@media (min-width: 720px) {
  .screen-designer {
    grid-template-columns: 1fr 1fr;
  }
}

.screen-blocks {
  min-height: 200px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 0.75rem;
}

.screen-block {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  margin-bottom: 0.35rem;
  background: var(--surface-2);
  border-radius: 6px;
  font-size: 0.85rem;
}

.screen-preview {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  background: var(--bg);
}

.screen-preview h3 {
  margin: 0 0 0.5rem;
}

.screen-preview .preview-field {
  margin-bottom: 0.75rem;
}

.screen-preview .preview-label {
  font-size: 0.75rem;
  color: var(--muted);
}

.screen-preview .preview-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.toast-root {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  padding: 0.65rem 1rem;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.trace-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.trace-item {
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

.trace-item time {
  color: var(--muted);
  font-size: 0.75rem;
}

.progress-bar {
  height: 8px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 0.35rem;
}

.progress-bar span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
  align-items: flex-end;
}

.filters .form-row {
  min-width: 160px;
}

.card-grid {
  display: grid;
  gap: 1rem;
}

@media (min-width: 720px) {
  .card-grid {
    grid-template-columns: 1fr 1fr;
  }
}

.instance-card {
  cursor: pointer;
  transition: border-color 0.15s;
}

.instance-card:hover,
.instance-card.is-selected {
  border-color: var(--accent);
}

.gestion-layout {
  display: grid;
  gap: 1rem;
}

@media (min-width: 900px) {
  .gestion-layout {
    grid-template-columns: 320px 1fr;
  }
}
