/**
 * DBForge Studio - Canvas & ERD Diagram Styles
 * Dot grid background, interactive table cards, SVG bezier connectors, and minimap.
 */

#canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background-color: var(--bg-app);
  cursor: default;
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

#canvas-container.show-grid {
  background-image: radial-gradient(var(--border-color) 1.2px, transparent 1.2px);
  background-size: 20px 20px;
}

#canvas-viewport {
  position: absolute;
  top: 0;
  left: 0;
  width: 10000px;
  height: 10000px;
  transform-origin: 0 0;
  will-change: transform;
  touch-action: none;
}

#canvas-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 10;
}

#canvas-tables {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 15;
}

/* Table Cards */
.table-card {
  position: absolute;
  width: 280px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  user-select: none;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
  overflow: hidden;
  cursor: move;
}

.table-card:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--text-tertiary);
}

.table-card.selected {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 2px var(--accent-primary), var(--shadow-md);
}

.table-card-color-bar {
  height: 4px;
  width: 100%;
}

.table-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-muted);
}

.table-card-title-group {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow: hidden;
}

.table-icon {
  color: var(--text-secondary);
  display: flex;
  align-items: center;
}

.table-name {
  font-weight: 700;
  font-size: 13px;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.table-card-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  opacity: 0.7;
  transition: opacity 0.15s ease;
}

.table-card:hover .table-card-actions,
.table-card .table-card-actions {
  opacity: 1;
}

.table-action-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  min-width: 28px;
  min-height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.12s ease;
  touch-action: manipulation;
}

.table-action-btn:hover, .table-action-btn:active {
  color: var(--accent-primary);
  background: var(--bg-surface-hover);
}

.table-action-btn.danger:hover, .table-action-btn.danger:active {
  color: var(--color-danger);
  background: var(--color-danger-bg);
}

/* Columns in Card */
.table-card-columns {
  display: flex;
  flex-direction: column;
}

.table-column-row {
  position: relative;
  display: flex;
  align-items: center;
  padding: 4px 10px;
  font-size: 12px;
  border-bottom: 1px solid var(--bg-app);
  transition: background 0.1s ease;
}

.table-column-row:last-child {
  border-bottom: none;
}

.table-column-row:hover {
  background: var(--bg-surface-hover);
}

.table-column-row.is-pk {
  background: rgba(9, 105, 218, 0.04);
}

/* Connector Port Handles */
.column-port {
  position: absolute;
  top: 50%;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--bg-surface);
  border: 2px solid var(--text-tertiary);
  transform: translateY(-50%);
  cursor: crosshair;
  z-index: 20;
  opacity: 0.6;
  transition: all 0.15s ease;
  touch-action: none;
}

.column-port::after {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
}

.table-card:hover .column-port {
  opacity: 1;
}

.column-port:hover, .column-port:active {
  border-color: var(--accent-primary);
  background: var(--accent-primary);
  transform: translateY(-50%) scale(1.4);
  opacity: 1;
}

.column-port.left-port {
  left: -5px;
}

.column-port.right-port {
  right: -5px;
}

.col-badge-icon {
  width: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-right: 4px;
}

.pk-icon { color: #cf8d00; }
.fk-icon { color: var(--accent-primary); }
.uq-icon { color: #8250df; }
.regular-col-bullet {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--text-tertiary);
}

.col-name {
  flex: 1;
  font-weight: 500;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.table-column-row.is-pk .col-name {
  font-weight: 700;
  color: var(--accent-primary);
}

.col-type {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
  margin: 0 6px;
}

.col-flags {
  display: flex;
  gap: 3px;
}

.flag-badge {
  font-size: 9px;
  font-weight: 700;
  padding: 0 4px;
  border-radius: 3px;
  line-height: 14px;
}

.flag-badge.nn {
  background: var(--border-muted);
  color: var(--text-secondary);
}

.flag-badge.ai {
  background: var(--color-success-bg);
  color: var(--color-success);
}

.table-card-footer {
  display: flex;
  justify-content: space-between;
  padding: 4px 12px;
  font-size: 10px;
  color: var(--text-tertiary);
  background: var(--bg-surface-secondary);
  border-top: 1px solid var(--border-muted);
}

/* --- SVG Connectors & Edges --- */
.relationship-edge {
  cursor: pointer;
  pointer-events: all;
}

.edge-hit-area {
  stroke: transparent;
  stroke-width: 14px;
  fill: none;
}

.edge-line {
  stroke: var(--text-tertiary);
  stroke-width: 1.8px;
  fill: none;
  transition: stroke 0.15s ease, stroke-width 0.15s ease;
}

.relationship-edge:hover .edge-line,
.relationship-edge.hovered .edge-line {
  stroke: var(--accent-primary);
  stroke-width: 2.5px;
}

.relationship-edge.selected .edge-line {
  stroke: var(--accent-primary);
  stroke-width: 3px;
  stroke-dasharray: 4 2;
  animation: edgeDash 1s linear infinite;
}

@keyframes edgeDash {
  to {
    stroke-dashoffset: -12;
  }
}

.draft-edge-line {
  stroke: var(--accent-primary);
  stroke-width: 2px;
  stroke-dasharray: 4 4;
  fill: none;
  pointer-events: none;
}

.edge-endpoint {
  fill: var(--text-tertiary);
  transition: fill 0.15s ease, r 0.15s ease;
}

.relationship-edge:hover .edge-endpoint,
.relationship-edge.selected .edge-endpoint {
  fill: var(--accent-primary);
  r: 5;
}

.edge-badge {
  pointer-events: none;
}

.badge-bg {
  fill: var(--bg-surface);
  stroke: var(--border-color);
  stroke-width: 1px;
}

.badge-text {
  font-size: 10px;
  font-weight: 700;
  fill: var(--text-secondary);
  user-select: none;
}

/* --- Floating Canvas Toolbar --- */
.canvas-toolbar {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  z-index: 30;
  user-select: none;
}

.toolbar-btn {
  height: 30px;
  padding: 0 8px;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 12px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  transition: all 0.12s ease;
}

.toolbar-btn:hover {
  background: var(--bg-surface-hover);
  color: var(--accent-primary);
}

.toolbar-divider {
  width: 1px;
  height: 18px;
  background: var(--border-color);
  margin: 0 2px;
}

/* --- Minimap --- */
.minimap-container {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 180px;
  height: 120px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  z-index: 30;
}

#minimap-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

#minimap-viewport {
  position: absolute;
  border: 1.5px solid var(--accent-primary);
  background: rgba(9, 105, 218, 0.12);
  pointer-events: none;
  border-radius: 2px;
}

/* ========================================================== */
/* CANVAS MOBILE RESPONSIVE                                   */
/* ========================================================== */
@media (max-width: 768px) {
  .table-card {
    width: 220px;
  }

  .table-card-header {
    padding: 6px 10px;
  }

  .table-name {
    font-size: 12px;
  }

  .table-action-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .table-column-row {
    padding: 5px 8px;
    font-size: 11px;
  }

  .col-type {
    font-size: 10px;
  }

  .flag-badge {
    font-size: 8px;
    padding: 0 3px;
  }

  .table-card-footer {
    padding: 3px 10px;
    font-size: 9px;
  }

  .column-port {
    width: 12px;
    height: 12px;
  }

  .column-port::after {
    top: -18px;
    left: -18px;
    right: -18px;
    bottom: -18px;
  }

  .edge-hit-area {
    stroke-width: 32px;
  }
}

@media (max-width: 480px) {
  .table-card {
    width: 190px;
  }

  .table-card-header {
    padding: 5px 8px;
  }

  .table-name {
    font-size: 11px;
  }

  .table-icon svg {
    width: 11px;
    height: 11px;
  }

  .table-column-row {
    padding: 4px 6px;
    font-size: 10px;
  }

  .col-type {
    font-size: 9px;
    margin: 0 4px;
  }

  .col-flags {
    gap: 2px;
  }

  .table-card-footer {
    font-size: 8px;
  }
}

