/* ───────────────────────────────────────────────
   CSS — Variables & Reset
   ─────────────────────────────────────────────── */
:root {
  --bg: #f9fafb;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-soft: #eef2f7;
  --text: #1f2937;
  --text-secondary: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;
  --warning-bg: #fffbeb;
  --warning-border: #fde68a;
  --success: #22c55e;
  --success-bg: #f0fdf4;
  --radius: 10px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
  --font: 'Inter', 'Segoe UI', system-ui, sans-serif;
  --mono: 'JetBrains Mono', 'SF Mono', monospace;
  --left-panel-width: 400px;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button {
  font-family: inherit;
}

/* Warning shown when app is opened via file:// */
.protocol-warning {
  background: #fff7ed;
  border-bottom: 1px solid #fdba74;
  color: #9a3412;
  padding: 10px 20px;
  font-size: 0.85rem;
  line-height: 1.4;
}

.protocol-warning strong {
  display: block;
  font-size: 0.9rem;
  margin-bottom: 2px;
}

.protocol-warning p + p {
  margin-top: 4px;
}

.protocol-warning pre {
  margin-top: 4px;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #fed7aa;
  background: #ffedd5;
  overflow-x: auto;
}

.protocol-warning code {
  font-family: var(--mono);
  font-size: 0.8rem;
}

.protocol-warning.protocol-warning-info {
  background: #eff6ff;
  border-bottom-color: #93c5fd;
  color: #1e3a8a;
}

/* ───────────────────────────────────────────────
   Header
   ─────────────────────────────────────────────── */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  z-index: 10;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
}

.logo svg {
  stroke: var(--accent);
}

.header-actions {
  display: flex;
  gap: 8px;
}

/* ───────────────────────────────────────────────
   Main Layout
   ─────────────────────────────────────────────── */
.main-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* Left Panel (Input) */
.left-panel {
  width: var(--left-panel-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 5;
  transition: width 0.3s ease, margin-left 0.3s ease;
}

.left-panel.collapsed {
  width: 0;
  overflow: hidden;
  border-right: none;
  margin-left: -1px;
}

/* Collapse Toggle */
.panel-toggle {
  position: absolute;
  left: var(--left-panel-width);
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 48px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 6px 6px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 6;
  transition: left 0.3s ease;
  color: var(--text-secondary);
  font-size: 0.7rem;
}

.panel-toggle:hover {
  background: #f3f4f6;
  color: var(--text);
}

.left-panel.collapsed ~ .panel-toggle {
  left: 0;
}

.input-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 18px;
  overflow-y: auto;
}

.input-section.hidden {
  display: none;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 0;
}

.section-title {
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
}

.json-textarea {
  flex: 1;
  width: 100%;
  resize: none;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 14px;
  font-family: var(--mono);
  font-size: 0.86rem;
  line-height: 1.5;
  background: #fbfcfe;
  color: var(--text);
}

.json-textarea:focus {
  outline: 2px solid var(--accent);
  border-color: transparent;
}

.allow-scroll {
  overflow: auto;
}

.table-container {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: auto;
}

.select-sm {
  min-height: 42px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
}

/* Right Panel (Results) */
.right-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px 20px;
  background: #f1f5f9;
  overflow-y: auto;
  gap: 12px;
}

/* ───────────────────────────────────────────────
   Review & Preview Components
   ─────────────────────────────────────────────── */
/* Toolbar */
.results-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  flex-shrink: 0;
}

/* Tabs */
.sheet-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.sheet-tab {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}

.sheet-tab:hover {
  background: #f3f4f6;
}

.sheet-tab.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* Preview Canvas */
.preview-container {
  flex: 1 1 auto;
  min-height: 500px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.08), 0 1px 3px rgb(0 0 0 / 0.06);
  overflow: hidden;
  position: relative;
}

.svg-wrapper {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: radial-gradient(#e5e7eb 1px, transparent 1px);
  background-size: 20px 20px;
}

.svg-wrapper .placeholder {
  color: var(--text-secondary);
  text-align: center;
  padding: 20px;
  font-size: 0.9rem;
  max-width: 300px;
}

/* Zoom Toolbar */
.zoom-toolbar {
  position: absolute;
  bottom: 16px;
  right: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  display: flex;
  gap: 2px;
  box-shadow: var(--shadow);
  z-index: 20;
}

.zoom-toolbar button {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-toolbar button:hover {
  background: #f3f4f6;
}

#zoomLabel {
  font-size: 0.75rem;
  padding: 0 8px;
  display: flex;
  align-items: center;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}

.drag-hint {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.75rem;
  pointer-events: none;
  opacity: 0.8;
}

/* Details Grid */
.details-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
}

.details-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 6px 0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.details-toggle svg {
  transition: transform 0.2s;
}

.details-toggle.expanded svg {
  transform: rotate(180deg);
}

.details-wrapper {
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.details-wrapper.collapsed {
  max-height: 0 !important;
}

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

.panel-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-secondary);
  letter-spacing: 0.05em;
  margin-bottom: 10px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.table-scroll {
  max-height: 200px;
  overflow-y: auto;
}

/* ───────────────────────────────────────────────
   Forms & Controls
   ─────────────────────────────────────────────── */
.mode-toggle {
  display: flex;
  background: #eef2f8;
  border: 1px solid #dbe3ee;
  padding: 4px;
  margin: 16px 18px 0;
  border-radius: 12px;
}

.toggle-btn {
  flex: 1;
  min-height: 48px;
  padding: 10px 12px;
  border: none;
  background: transparent;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.18s ease;
}

.toggle-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 5px rgb(15 23 42 / 0.1);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 2px 0;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group.span-2 {
  grid-column: span 2;
}

.form-group label {
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.2;
  color: var(--text-secondary);
}

.form-group input,
.form-group select {
  min-height: 42px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1.02rem;
  font-weight: 500;
  color: var(--text);
  background: #f8fafc;
}

.form-group input:focus,
.form-group select:focus {
  outline: 2px solid var(--accent);
  border-color: transparent;
}

.checkbox-group {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
}

.checkbox-group input {
  width: auto;
  transform: scale(1.05);
}

.checkbox-group label {
  font-size: 0.92rem;
  font-weight: 500;
  color: #4b5563;
}

/* Pieces Table in Form */
.pieces-section {
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 0 0 auto;
  overflow: visible;
}

.pieces-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.pieces-table th {
  text-align: left;
  padding: 10px 8px;
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 0.82rem;
  letter-spacing: 0.01em;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}

.pieces-table td {
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.pieces-table input {
  width: 100%;
  min-width: 0;
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 0.94rem;
  background: #f8fafc;
}

.pieces-table input[type='number'] {
  -moz-appearance: textfield;
}

.pieces-table input[type='number']::-webkit-outer-spin-button,
.pieces-table input[type='number']::-webkit-inner-spin-button {
  margin: 0;
  -webkit-appearance: none;
}

.pieces-table th:nth-child(1),
.pieces-table td:nth-child(1) {
  width: 34%;
}

.pieces-table th:nth-child(2),
.pieces-table td:nth-child(2),
.pieces-table th:nth-child(3),
.pieces-table td:nth-child(3) {
  width: 20%;
}

.pieces-table th:nth-child(4),
.pieces-table td:nth-child(4) {
  width: 16%;
}

.pieces-table th:nth-child(5),
.pieces-table td:nth-child(5) {
  width: 10%;
  text-align: center;
}

.btn-remove-pieza {
  background: transparent;
  border: none;
  color: var(--danger);
  cursor: pointer;
  padding: 4px 6px;
  font-size: 1.25rem;
  line-height: 1;
  border-radius: 6px;
}

.btn-remove-pieza:hover {
  background: #fee2e2;
}

/* Action Buttons */
.action-buttons {
  padding: 14px 18px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--surface);
}

.secondary-actions {
  display: flex;
  gap: 10px;
}

.secondary-actions button {
  flex: 1;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.96rem;
  cursor: pointer;
  transition: all 0.15s;
  border: 1px solid transparent;
}

.btn-sm {
  min-height: 34px;
  padding: 6px 12px;
  font-size: 0.8rem;
}

.btn-xs {
  min-height: 32px;
  padding: 4px 10px;
  font-size: 0.8rem;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--accent);
  color: white;
  box-shadow: 0 4px 10px rgb(37 99 235 / 0.24);
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

.btn-secondary:hover {
  background: #f3f4f6;
}

.btn-danger {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger);
}

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

.btn-outline {
  background: transparent;
}

.btn:focus-visible,
.toggle-btn:focus-visible,
.btn-remove-pieza:focus-visible {
  outline: 2px solid #93c5fd;
  outline-offset: 1px;
}

/* ───────────────────────────────────────────────
   Summaries & Tables
   ─────────────────────────────────────────────── */
.summary-cards {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.summary-card {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  text-align: center;
  flex: 1;
  min-width: 0;
}

.card-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.summary-card.success .card-value {
  color: var(--success);
}

.summary-card .card-label {
  font-size: 0.7rem;
  color: var(--text-secondary);
}

.coords-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.coords-table th {
  text-align: left;
  padding: 8px;
  background: #f8fafc;
  color: var(--text-secondary);
  font-weight: 600;
  position: sticky;
  top: 0;
}

.coords-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}

.color-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

/* ───────────────────────────────────────────────
   Modals
   ─────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  backdrop-filter: blur(2px);
}

.modal-content {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
}

.modal-content.modal-lg {
  max-width: 900px;
  height: 80vh;
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.btn-close {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--text-secondary);
  cursor: pointer;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  font-size: 0.9rem;
}

.modal-body.p-0 {
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* 3D Viewer */
.canvas-3d {
  flex: 1;
  background: #111;
  width: 100%;
  height: 100%;
}

.view-controls {
  display: flex;
  background: #f1f5f9;
  border-radius: 6px;
  padding: 2px;
  gap: 2px;
}

.btn-icon {
  background: transparent;
  border: none;
  padding: 4px 8px;
  font-size: 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text-secondary);
}

.btn-icon.active {
  background: white;
  color: var(--accent);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

/* Banners */
.banner {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  display: none;
}

.banner.visible {
  display: block;
}

.banner-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger-border);
}

.banner-success {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid #bbf7d0;
}

.banner-warning {
  background: var(--warning-bg);
  color: #b45309;
  border: 1px solid var(--warning-border);
}

/* Summary info table compact */
#panelResumen table {
  width: 100%;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

#panelResumen table td {
  padding: 2px 0;
}

#panelResumen table td:first-child {
  font-weight: 500;
  padding-right: 12px;
  white-space: nowrap;
}

/* Animate in */
.animate-in {
  animation: fadeSlideIn 0.25s ease-out;
}

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

/* Utils */
.hidden {
  display: none !important;
}

.mt-4 {
  margin-top: 16px;
}

.mt-2 {
  margin-top: 8px;
}

/* Responsive */
@media (max-width: 1024px) {
  .main-layout {
    flex-direction: column;
    overflow: auto;
  }

  .left-panel {
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .left-panel.collapsed {
    width: 100%;
    height: 0;
  }

  .panel-toggle {
    display: none !important;
  }

  .right-panel {
    overflow: visible;
    padding: 14px;
  }

  .preview-container {
    min-height: 360px;
  }
}

@media (max-width: 768px) {
  body {
    height: auto;
    min-height: 100vh;
    overflow: auto;
  }

  .app-header {
    position: sticky;
    top: 0;
  }

  .input-section {
    gap: 12px;
    padding: 14px;
  }

  .mode-toggle {
    margin: 12px 14px 0;
  }

  .pieces-section {
    flex: 0 0 auto;
    min-height: 0;
    overflow: visible;
  }

  .table-container {
    max-height: 260px;
  }

  .pieces-table {
    min-width: 0;
    font-size: 0.88rem;
  }

  .action-buttons {
    position: sticky;
    bottom: 0;
    z-index: 7;
    background: var(--surface);
    box-shadow: 0 -8px 18px rgb(15 23 42 / 0.1);
  }
}

@media (max-width: 560px) {
  :root {
    --left-panel-width: 100%;
  }

  .app-header {
    height: 64px;
    padding: 0 14px;
  }

  .logo {
    font-size: 1.22rem;
    gap: 8px;
  }

  .logo svg {
    width: 26px;
    height: 26px;
  }

  .header-actions {
    display: none;
  }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .form-group.span-2 {
    grid-column: span 1;
  }

  .secondary-actions {
    gap: 8px;
  }

  .section-title {
    font-size: 0.98rem;
  }

  .form-group label {
    font-size: 0.82rem;
  }

  .form-group input,
  .form-group select {
    min-height: 40px;
    font-size: 0.98rem;
  }

  .action-buttons {
    padding: 12px 14px 14px;
  }

  .btn {
    min-height: 40px;
    font-size: 0.95rem;
  }
}
