:root {
  --primary: #0284c7;
  --primary-hover: #0369a1;
  --primary-light: #e0f2fe;
  --success: #16a34a;
  --success-light: #dcfce7;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --purple: #7c3aed;
  --purple-light: #f3e8ff;
  --bg-main: #f1f5f9;
  --bg-card: #ffffff;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --border: #cbd5e1;
  --border-light: #e2e8f0;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  line-height: 1.45;
  font-size: 14px;
}

/* Header & Navbar */
.app-header {
  background: #ffffff;
  border-bottom: 1px solid var(--border-light);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 24px;
  border-bottom: 1px solid #f1f5f9;
}

.brand-area {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  font-size: 1.8rem;
}

.brand-title {
  font-size: 1.25rem;
  font-weight: 800;
  color: #0f172a;
  letter-spacing: -0.02em;
}

.brand-title span {
  color: var(--primary);
}

.brand-subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Sample presets toolbar */
.toolbar-presets {
  background: #f8fafc;
  padding: 8px 24px;
  display: flex;
  align-items: center;
  gap: 10px;
  overflow-x: auto;
  border-bottom: 1px solid var(--border-light);
}

.presets-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  background: #ffffff;
  color: #334155;
  text-decoration: none;
}

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

.btn-sm {
  padding: 4px 8px;
  font-size: 0.8rem;
}

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

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

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

.btn-success:hover {
  background: #15803d;
}

.btn-warning {
  background: #fef3c7;
  color: #92400e;
  border-color: #fde68a;
}

.btn-warning:hover {
  background: #fde68a;
}

.btn-danger {
  background: #fee2e2;
  color: #b91c1c;
  border-color: #fecaca;
}

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

.btn-outline-primary {
  background: transparent;
  color: var(--primary);
  border-color: var(--primary);
}

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

.btn-outline-excel {
  background: #f0fdf4;
  color: #166534;
  border-color: #bbf7d0;
}

.btn-outline-excel:hover {
  background: #dcfce7;
  border-color: #86efac;
}

.btn-outline-clip {
  background: #f8fafc;
  color: #0369a1;
  border-color: #bae6fd;
}

.btn-outline-clip:hover {
  background: #e0f2fe;
}

.btn-run {
  background: linear-gradient(135deg, #16a34a, #15803d);
  color: #ffffff;
  font-size: 1rem;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 8px;
  border: none;
  box-shadow: 0 4px 6px rgba(22, 163, 74, 0.25);
}

.btn-run:hover {
  background: linear-gradient(135deg, #15803d, #166534);
  box-shadow: 0 6px 10px rgba(22, 163, 74, 0.35);
  transform: translateY(-1px);
}

/* App Main Layout */
.app-container {
  display: grid;
  grid-template-columns: 560px 1fr;
  gap: 16px;
  padding: 16px 20px;
  max-width: 1920px;
  margin: 0 auto;
  min-height: calc(100vh - 120px);
}

@media (max-width: 1200px) {
  .app-container {
    grid-template-columns: 1fr;
  }
}

/* Left Panel: Inputs & Settings */
.left-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light);
  background: #fafafa;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: #1e293b;
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 700;
}

.badge-info { background: #e0f2fe; color: #0369a1; }
.badge-success { background: #dcfce7; color: #15803d; }
.badge-warning { background: #fef3c7; color: #b45309; }
.badge-purple { background: #f3e8ff; color: #7e22ce; }

/* Tabs inside Left Card */
.input-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-light);
  background: #f8fafc;
}

.input-tab {
  flex: 1;
  text-align: center;
  padding: 10px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s ease;
  user-select: none;
}

.input-tab:hover {
  color: var(--primary);
  background: #f1f5f9;
}

.input-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: #ffffff;
  font-weight: 700;
}

.tab-pane {
  display: none;
  padding: 14px;
}

.tab-pane.active {
  display: block;
}

/* Info Banners */
.info-banner {
  background: #fffbeb;
  border: 1px solid #fef3c7;
  border-left: 4px solid #f59e0b;
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 0.82rem;
  color: #92400e;
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.banner-content {
  flex: 1;
  line-height: 1.4;
}

/* Action bar above table */
.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* Editable Data Grids */
.table-wrapper {
  overflow-x: auto;
  border: 1px solid var(--border-light);
  border-radius: 6px;
  max-height: 380px;
  position: relative;
  background: #ffffff;
}

.app-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.app-table th {
  background: #f8fafc;
  color: #475569;
  font-weight: 700;
  padding: 8px 8px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  text-align: left;
  white-space: nowrap;
}

.app-table td {
  padding: 6px 6px;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}

.app-table tr:hover {
  background: #f8fafc;
}

.app-table tr.selected {
  background: #e0f2fe !important;
}

.app-table input[type="text"],
.app-table input[type="number"],
.app-table select {
  width: 100%;
  padding: 4px 6px;
  font-size: 0.82rem;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: #ffffff;
}

.app-table input[type="number"] {
  text-align: right;
}

.app-table input:focus,
.app-table select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.2);
}

.app-table .btn-del-row {
  color: #ef4444;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  font-size: 0.9rem;
}

.app-table .btn-del-row:hover {
  background: #fee2e2;
}

/* Parameters Form Grid */
.param-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

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

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

.form-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: #475569;
}

.form-control {
  padding: 7px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.85rem;
  background: #ffffff;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.15);
}

.form-checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  color: #1e293b;
}

/* Run Section */
.run-section {
  padding: 14px;
  background: #ffffff;
  border-top: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.run-btn-wrapper {
  display: flex;
  gap: 8px;
}

/* Right Panel: Results & Canvas */
.right-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* KPI Banner */
.kpi-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
}

.kpi-box {
  background: #ffffff;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
  border-left: 4px solid var(--primary);
}

.kpi-box.highlight {
  background: linear-gradient(135deg, #0284c7, #0369a1);
  color: #ffffff;
  border-left: none;
}

.kpi-box.highlight .kpi-label,
.kpi-box.highlight .kpi-note {
  color: rgba(255, 255, 255, 0.85);
}

.kpi-box.highlight .kpi-number {
  color: #ffffff;
}

.kpi-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.kpi-number {
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1.2;
}

.kpi-note {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Pattern Viewer Card */
.viewer-card {
  background: #ffffff;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.viewer-header {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-light);
  background: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

.pattern-nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pattern-stamp {
  background: #dcfce7;
  color: #15803d;
  font-weight: 800;
  font-size: 0.82rem;
  padding: 4px 12px;
  border-radius: 20px;
  border: 1px solid #86efac;
}

.viewer-tools {
  display: flex;
  align-items: center;
  gap: 6px;
}

.svg-viewport {
  position: relative;
  min-height: 420px;
  max-height: 580px;
  background: #fafafa;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.svg-viewport svg {
  width: 100%;
  height: 100%;
  max-height: 540px;
  user-select: none;
  transition: transform 0.1s ease-out;
  cursor: grab;
}

.svg-viewport svg:active {
  cursor: grabbing;
}

/* SVG interactive styling */
.svg-piece {
  transition: opacity 0.15s ease, filter 0.15s ease;
  cursor: pointer;
}

.svg-piece:hover {
  opacity: 0.9;
  filter: drop-shadow(0 0 6px rgba(2, 132, 199, 0.8)) !important;
}

/* Floating Tooltip */
.svg-tooltip {
  position: fixed;
  z-index: 1000;
  background: rgba(15, 23, 42, 0.92);
  color: #ffffff;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.8rem;
  pointer-events: none;
  box-shadow: var(--shadow-lg);
  display: none;
  backdrop-filter: blur(4px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  line-height: 1.4;
}

/* Result Tables Tab Control */
.result-tabs-card {
  background: #ffffff;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.result-tabs-nav {
  display: flex;
  border-bottom: 1px solid var(--border-light);
  background: #f8fafc;
  overflow-x: auto;
}

.result-tab-btn {
  padding: 10px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  user-select: none;
}

.result-tab-btn:hover {
  color: var(--primary);
  background: #f1f5f9;
}

.result-tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: #ffffff;
  font-weight: 700;
}

.result-tab-content {
  display: none;
  padding: 12px;
}

.result-tab-content.active {
  display: block;
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(3px);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-overlay.open {
  display: flex;
}

.modal-dialog {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 780px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modalIn 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

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

.modal-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: #1e293b;
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal-close {
  background: transparent;
  border: none;
  font-size: 1.3rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}

.modal-close:hover {
  background: #f1f5f9;
  color: #0f172a;
}

.modal-body {
  padding: 18px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border-light);
  background: #fafafa;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

/* Paste Textarea */
.paste-textarea {
  width: 100%;
  height: 160px;
  font-family: monospace;
  font-size: 0.85rem;
  padding: 10px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  resize: vertical;
}

.paste-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.2);
}

/* Dropzone for Excel */
.excel-dropzone {
  border: 2px dashed #94a3b8;
  border-radius: 8px;
  padding: 30px 20px;
  text-align: center;
  background: #f8fafc;
  cursor: pointer;
  transition: all 0.2s ease;
}

.excel-dropzone:hover,
.excel-dropzone.dragover {
  border-color: var(--primary);
  background: #f0f9ff;
}

.dropzone-icon {
  font-size: 2.5rem;
  color: #0284c7;
  margin-bottom: 8px;
}

.dropzone-text {
  font-size: 0.95rem;
  font-weight: 600;
  color: #334155;
  margin-bottom: 4px;
}

.dropzone-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Toast Notification */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: #1e293b;
  color: #ffffff;
  padding: 10px 18px;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  font-size: 0.85rem;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  animation: toastIn 0.25s ease-out;
  pointer-events: auto;
}

.toast.toast-success { background: #16a34a; }
.toast.toast-error { background: #dc2626; }
.toast.toast-info { background: #0284c7; }

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

/* Empty State */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}

.empty-state-icon {
  font-size: 2.5rem;
  margin-bottom: 10px;
  opacity: 0.6;
}

.empty-state-title {
  font-size: 1rem;
  font-weight: 700;
  color: #334155;
  margin-bottom: 6px;
}

.empty-state-desc {
  font-size: 0.85rem;
  max-width: 420px;
  margin: 0 auto;
}

/* Loading Spinner */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #ffffff;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
