:root {
  --bg-app: #f1f5f9;
  --bg-panel: #ffffff;
  --border-color: #e2e8f0;
  --text-main: #0f172a;
  --text-muted: #475569;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --green-fill: rgba(34, 197, 94, 0.82);
  --green-stroke: #15803d;
  --green-chair: rgba(34, 197, 94, 0.22);
  --red-fill: rgba(239, 68, 68, 0.85);
  --red-stroke: #b91c1c;
  --red-chair: rgba(239, 68, 68, 0.25);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.08);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.12);
  --radius: 10px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  max-width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg-app);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
}

/* Header */
.topbar {
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  box-shadow: var(--shadow-sm);
  z-index: 20;
  gap: 8px;
  flex-shrink: 0;
  width: 100%;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-text {
  min-width: 0;
}

.brand-icon {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
}

.brand h1 {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  display: block;
}

.stats-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.stat-pill {
  background: #f8fafc;
  border: 1px solid var(--border-color);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.stat-pill.status-ok {
  background: #f0fdf4;
  border-color: #bbf7d0;
  color: #15803d;
}

.stat-pill.status-conflict {
  background: #fef2f2;
  border-color: #fecaca;
  color: #b91c1c;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.dot.green { background: #16a34a; }
.dot.red { background: #dc2626; }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* Buttons & Inputs */
.btn {
  border: 1px solid var(--border-color);
  background: #ffffff;
  color: var(--text-main);
  padding: 6px 10px;
  border-radius: 7px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: all 0.15s ease;
  touch-action: manipulation;
}

.btn:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
}

.btn.active {
  background: #eff6ff;
  border-color: var(--primary);
  color: var(--primary);
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.btn-danger {
  color: #dc2626;
  border-color: #fecaca;
  background: #fef2f2;
}

.btn-danger:hover {
  background: #fee2e2;
}

/* Main Workspace */
.workspace {
  flex: 1;
  min-height: 0;
  min-width: 0;
  width: 100%;
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 290px;
  overflow: hidden;
  position: relative;
}

.mobile-backdrop {
  display: none;
}

.mobile-drawer-header {
  display: none;
}

.sidebar {
  background: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.sidebar-right {
  background: var(--bg-panel);
  border-left: 1px solid var(--border-color);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.panel-section {
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 12px;
  background: #fcfdff;
}

.panel-title {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Table Catalog Grid */
.catalog-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.catalog-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: #ffffff;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: left;
  touch-action: manipulation;
  width: 100%;
}

.catalog-item:hover,
.catalog-item:active {
  border-color: var(--primary);
  background: #f8fafc;
  box-shadow: var(--shadow-sm);
}

.catalog-item-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.shape-preview {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.shape-circle {
  border-radius: 50%;
  background: rgba(34, 197, 94, 0.25);
  border: 2px solid #16a34a;
}

.shape-rect {
  border-radius: 3px;
  background: rgba(34, 197, 94, 0.25);
  border: 2px solid #16a34a;
}

.catalog-info {
  min-width: 0;
}

.catalog-info h3 {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-main);
}

.catalog-info p {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.catalog-add-badge {
  background: #eff6ff;
  color: var(--primary);
  font-size: 11px;
  font-weight: 700;
  padding: 4px 8px;
  border-radius: 6px;
  flex-shrink: 0;
}

/* Form controls */
.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 12.5px;
}

.control-row:last-child {
  margin-bottom: 0;
}

.switch-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}

.switch-label input[type="checkbox"] {
  width: 17px;
  height: 17px;
  accent-color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}

select,
input[type="number"],
input[type="text"] {
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
  color: var(--text-main);
  background: #fff;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
  height: 24px;
}

.hint-box {
  font-size: 11.5px;
  color: var(--text-muted);
  background: #f1f5f9;
  padding: 8px 10px;
  border-radius: 6px;
  line-height: 1.4;
  margin-top: 8px;
}

/* Lienzo 100% adaptable mediante SVG viewBox (sin píxeles fijos que rompan el móvil) */
.canvas-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  background: #e2e8f0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 8px 54px 8px;
  touch-action: none;
}

.plan-svg {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  display: block;
  touch-action: none;
}

/* Floating Zoom & Legend Controls */
.floating-toolbar {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border-color);
  border-radius: 999px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  gap: 5px;
  box-shadow: var(--shadow-md);
  z-index: 10;
}

.floating-legend {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 5px 9px;
  font-size: 11px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  box-shadow: var(--shadow-sm);
  z-index: 10;
  pointer-events: none;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}

.calibration-banner {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #ffffff;
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  z-index: 15;
  display: none;
  align-items: center;
  gap: 8px;
  max-width: 94%;
}

.calibration-banner.visible {
  display: flex;
}

/* Mobile Quick Bar & Bottom Nav (Hidden on Desktop) */
.mobile-quick-bar {
  display: none;
}

.mobile-bottom-nav {
  display: none;
}

/* SVG Interactive Elements */
.table-group {
  cursor: grab;
}

.table-group:active {
  cursor: grabbing;
}

.table-group.selected .selection-outline {
  stroke: #2563eb;
  stroke-width: 2.2px;
  stroke-dasharray: 4 3;
}

.rotate-handle {
  cursor: alias;
}

/* Selected Table Inspector */
.empty-state {
  text-align: center;
  padding: 18px 10px;
  color: var(--text-muted);
  font-size: 12.5px;
  line-height: 1.5;
}

.inspector-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.join-box {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  padding: 8px 10px;
}

.quick-angles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  margin-top: 4px;
}

.quick-angles .btn {
  justify-content: center;
  padding: 6px 4px;
  font-size: 12px;
}

.table-list-mini {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
}

.table-row-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 9px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background: #fff;
  font-size: 12px;
  cursor: pointer;
}

.table-row-item.selected {
  border-color: var(--primary);
  background: #eff6ff;
}

/* ==========================================================================
   DISEÑO RESPONSIVE PARA MÓVILES Y TABLETS (<= 1024px)
   ========================================================================== */

@media (max-width: 1024px) {
  .topbar {
    flex-wrap: wrap;
    padding: 6px 10px;
    gap: 5px;
  }

  .brand-subtitle {
    display: none;
  }

  .brand h1 {
    font-size: 13.5px;
  }

  .brand-icon {
    width: 26px;
    height: 26px;
    font-size: 13px;
  }

  .topbar-actions .btn-text {
    display: none;
  }

  .topbar-actions .btn {
    padding: 5px 8px;
    font-size: 12px;
  }

  .stats-bar {
    width: 100%;
    order: 3;
    justify-content: space-between;
    gap: 4px;
  }

  .stat-pill {
    font-size: 10.5px;
    padding: 2px 7px;
  }

  .workspace {
    display: flex;
    flex-direction: column;
    flex: 1;
    width: 100%;
    min-height: 0;
    position: relative;
    overflow: hidden;
  }

  .canvas-wrapper {
    flex: 1;
    width: 100%;
    height: 100%;
    padding: 4px 4px 48px 4px;
  }

  /* Fondo oscuro modal para cajones inferiores */
  .mobile-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 35;
    display: none;
  }

  .mobile-backdrop.open {
    display: block;
  }

  /* Cajones deslizables desde abajo en móvil */
  .sidebar,
  .sidebar-right {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 56px;
    width: 100%;
    max-width: 100vw;
    max-height: 75dvh;
    border: none;
    border-top: 1px solid var(--border-color);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 30px rgba(15, 23, 42, 0.22);
    z-index: 40;
    display: none;
    padding: 14px;
  }

  .sidebar.drawer-open,
  .sidebar-right.drawer-open {
    display: flex;
  }

  .mobile-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
    font-size: 14px;
    position: sticky;
    top: -14px;
    background: var(--bg-panel);
    padding-top: 4px;
    z-index: 5;
  }

  /* Evitar zoom automático de iOS en inputs */
  select,
  input[type="number"],
  input[type="text"] {
    font-size: 16px;
  }

  /* Barra flotante de acciones rápidas al tocar una mesa */
  .mobile-quick-bar.visible {
    display: flex;
    align-items: center;
    gap: 5px;
    position: absolute;
    bottom: 48px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.94);
    color: #fff;
    padding: 5px 9px;
    border-radius: 999px;
    box-shadow: var(--shadow-md);
    z-index: 15;
    white-space: nowrap;
    max-width: 96%;
  }

  .quick-bar-title {
    font-size: 12px;
    font-weight: 700;
    padding: 0 5px;
    color: #93c5fd;
  }

  .mobile-quick-bar .btn {
    padding: 5px 9px;
    font-size: 11.5px;
    border-radius: 999px;
  }

  .floating-toolbar {
    bottom: 6px;
    padding: 3px 6px;
  }

  .floating-toolbar .btn {
    padding: 4px 8px;
    font-size: 11px;
  }

  .floating-legend {
    top: 6px;
    left: 6px;
    padding: 4px 7px;
    font-size: 10px;
  }

  /* Barra de navegación inferior fija en móvil */
  .mobile-bottom-nav {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    height: 56px;
    width: 100%;
    background: var(--bg-panel);
    border-top: 1px solid var(--border-color);
    z-index: 45;
    flex-shrink: 0;
  }

  .mobile-nav-btn {
    border: none;
    background: transparent;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
  }

  .mobile-nav-btn .nav-icon {
    font-size: 15px;
  }

  .mobile-nav-btn.active {
    color: var(--primary);
    background: #eff6ff;
  }
}

@media print {
  html,
  body {
    height: auto;
    overflow: visible;
  }
  .topbar,
  .sidebar,
  .sidebar-right,
  .floating-toolbar,
  .calibration-banner,
  .mobile-bottom-nav,
  .mobile-quick-bar,
  .mobile-backdrop {
    display: none !important;
  }
  .workspace {
    display: block;
  }
  .canvas-wrapper {
    background: #fff;
    padding: 0;
  }
  .plan-svg {
    width: 100%;
    height: auto;
  }
}
