/* ===== TerraForge CSS ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #7C3AED;
  --primary-light: #EDE9FE;
  --primary-dark: #5B21B6;
  --bg: #F8F7FF;
  --surface: #FFFFFF;
  --border: #E5E7EB;
  --border-strong: #D1D5DB;
  --text: #111827;
  --text-muted: #6B7280;
  --text-light: #9CA3AF;
  --success: #059669;
  --warning: #D97706;
  --danger: #DC2626;
  --info: #2563EB;
  --aws: #FF9900;
  --azure: #0078D4;
  --gcp: #4285F4;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.05);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ===== HEADER ===== */
.app-header {
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 16px;
  flex-shrink: 0;
  box-shadow: 0 1px 0 var(--border);
  z-index: 100;
}

.header-left { display: flex; align-items: center; min-width: 200px; }
.header-center { flex: 1; display: flex; justify-content: center; }
.header-right { display: flex; align-items: center; gap: 8px; min-width: 200px; justify-content: flex-end; }

.logo { display: flex; align-items: center; gap: 8px; }
.logo-text { font-weight: 700; font-size: 16px; color: var(--primary); letter-spacing: -0.3px; }
.logo-sub { font-size: 11px; color: var(--text-muted); background: var(--primary-light); padding: 2px 6px; border-radius: 4px; }

.cloud-tabs { display: flex; gap: 2px; background: var(--bg); border-radius: var(--radius); padding: 3px; }
.cloud-tab {
  display: flex; align-items: center; gap: 5px;
  padding: 5px 12px; border: none; background: transparent;
  border-radius: 6px; font-size: 13px; font-weight: 500; cursor: pointer;
  color: var(--text-muted); transition: all 0.15s;
}
.cloud-tab:hover { background: var(--surface); color: var(--text); }
.cloud-tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.btn-header {
  display: flex; align-items: center; gap: 5px;
  padding: 6px 10px; border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius); font-size: 12px; font-weight: 500; cursor: pointer;
  color: var(--text-muted); transition: all 0.15s; white-space: nowrap;
}
.btn-header:hover { background: var(--bg); color: var(--text); border-color: var(--border-strong); }

.btn-generate {
  background: var(--primary) !important; color: white !important;
  border-color: var(--primary) !important;
  padding: 6px 14px !important;
}
.btn-generate:hover { background: var(--primary-dark) !important; }

/* ===== LAYOUT ===== */
.app-body {
  flex: 1;
  display: grid;
  grid-template-columns: 240px 1fr 320px;
  overflow: hidden;
}

/* ===== LEFT SIDEBAR ===== */
.sidebar-left {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.palette-search {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.palette-search i { color: var(--text-light); font-size: 14px; }
.palette-search input {
  flex: 1; border: none; background: transparent;
  font-size: 13px; color: var(--text); outline: none;
}
.palette-search input::placeholder { color: var(--text-light); }

.palette-content { overflow-y: auto; flex: 1; padding: 8px 0; }

.palette-section { margin-bottom: 4px; }
.palette-section-header {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px 4px;
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.8px; color: var(--text-light);
  cursor: pointer;
}
.palette-section-header .section-toggle { margin-left: auto; transition: transform 0.2s; }
.palette-section-header.collapsed .section-toggle { transform: rotate(-90deg); }
.palette-section-header.collapsed + .palette-items { display: none; }

.cloud-badge {
  font-size: 9px; font-weight: 700; padding: 1px 5px; border-radius: 3px;
  letter-spacing: 0.5px;
}
.badge-aws { background: #FFF3E0; color: #E65100; }
.badge-azure { background: #E3F2FD; color: #0D47A1; }
.badge-gcp { background: #E8F5E9; color: #1B5E20; }
.badge-generic { background: #F3E8FF; color: #4C1D95; }

.palette-item {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 12px; cursor: grab;
  border-radius: 0; transition: background 0.1s;
  user-select: none;
}
.palette-item:hover { background: var(--bg); }
.palette-item:active { cursor: grabbing; }

.palette-item-icon {
  width: 28px; height: 28px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; flex-shrink: 0;
}
.palette-item-info { flex: 1; min-width: 0; }
.palette-item-name { font-size: 12px; font-weight: 500; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.palette-item-type { font-size: 10px; color: var(--text-light); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ===== CANVAS ===== */
.canvas-area {
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--bg); position: relative;
}

.canvas-toolbar {
  height: 40px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  padding: 0 12px; gap: 4px;
  justify-content: space-between;
  flex-shrink: 0;
}
.toolbar-left { display: flex; align-items: center; gap: 2px; }
.toolbar-right { display: flex; align-items: center; }

.tool-btn {
  width: 30px; height: 30px; display: flex; align-items: center; justify-content: center;
  border: 1px solid transparent; background: transparent;
  border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--text-muted);
  transition: all 0.15s;
}
.tool-btn:hover { background: var(--bg); color: var(--text); border-color: var(--border); }
.tool-btn.active { background: var(--primary-light); color: var(--primary); border-color: var(--primary); }
#zoomLabel { width: auto; padding: 0 8px; font-size: 11px; font-weight: 600; color: var(--text-muted); }

.tool-divider { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.canvas-info { font-size: 11px; color: var(--text-light); }

.canvas-viewport {
  flex: 1; overflow: hidden; position: relative;
  cursor: default;
}
.canvas-viewport.drag-over { background: #F5F3FF; }

.canvas-svg {
  width: 100%; height: 100%;
  position: absolute; top: 0; left: 0;
}

.canvas-drop-hint {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; color: var(--text-muted); pointer-events: none;
}
.canvas-drop-hint.hidden { display: none; }

/* ===== NODES ===== */
.node-group { cursor: pointer; }
.node-group:hover .node-rect { filter: drop-shadow(0 4px 8px rgba(0,0,0,0.15)); }
.node-group.selected .node-rect { stroke-width: 2.5 !important; }

/* Connection lines */
.connection-line {
  stroke: #7C3AED; stroke-width: 1.5; fill: none;
  opacity: 0.6; stroke-dasharray: none;
  transition: opacity 0.2s; cursor: pointer;
  marker-end: url(#arrowhead);
}
.connection-line:hover { opacity: 1; stroke-width: 2.5; }
.connection-line.selected { opacity: 1; stroke-width: 2.5; stroke: #DB2777; }
.connection-label { font-size: 10px; fill: #7C3AED; font-weight: 600; }

/* ===== RIGHT SIDEBAR ===== */
.sidebar-right {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}

.right-tabs {
  display: flex; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.rtab {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 4px;
  padding: 10px 4px; border: none; background: transparent;
  font-size: 11px; font-weight: 600; cursor: pointer;
  color: var(--text-muted); border-bottom: 2px solid transparent;
  transition: all 0.15s; text-transform: uppercase; letter-spacing: 0.3px;
}
.rtab:hover { color: var(--text); background: var(--bg); }
.rtab.active { color: var(--primary); border-bottom-color: var(--primary); }

.rtab-content { flex: 1; overflow-y: auto; }

/* Properties Panel */
.no-selection {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 200px; gap: 8px; color: var(--text-muted); padding: 20px; text-align: center;
  font-size: 13px;
}

.props-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; border-bottom: 1px solid var(--border);
}
.props-icon {
  width: 36px; height: 36px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; flex-shrink: 0;
}
.props-type { font-size: 10px; color: var(--text-light); text-transform: uppercase; letter-spacing: 0.5px; }
.props-resource { font-size: 13px; font-weight: 600; color: var(--text); }
.delete-btn {
  margin-left: auto; width: 28px; height: 28px;
  border: 1px solid var(--border); background: transparent;
  border-radius: 6px; cursor: pointer; color: var(--danger);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
}
.delete-btn:hover { background: #FEF2F2; }

.props-section { padding: 8px 12px; }
.props-section-title {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.5px; color: var(--text-light); margin-bottom: 8px;
}
.props-field { margin-bottom: 10px; }
.props-field label { display: block; font-size: 11px; color: var(--text-muted); margin-bottom: 4px; font-weight: 500; }
.props-field input,
.props-field select,
.props-field textarea {
  width: 100%; padding: 6px 8px;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 12px; color: var(--text); background: var(--surface);
  outline: none; transition: border-color 0.15s;
  font-family: inherit;
}
.props-field input:focus,
.props-field select:focus,
.props-field textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); }
.props-field textarea { resize: vertical; min-height: 60px; }
.props-field.inline { display: flex; gap: 6px; }
.props-field.inline input { flex: 1; }

.props-tags { display: flex; flex-direction: column; gap: 4px; }
.tag-row { display: flex; gap: 4px; }
.tag-row input { flex: 1; }
.tag-remove { padding: 5px 7px; border: 1px solid var(--border); border-radius: 5px; background: transparent; cursor: pointer; color: var(--danger); font-size: 11px; }
.btn-add-tag {
  font-size: 11px; color: var(--primary); background: transparent;
  border: 1px dashed var(--primary); border-radius: 5px;
  padding: 4px 8px; cursor: pointer; margin-top: 4px;
}
.btn-add-tag:hover { background: var(--primary-light); }

.props-separator { height: 1px; background: var(--border); margin: 8px 0; }

/* Output panel */
.output-toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.output-label { font-size: 11px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; flex: 1; }
.output-actions { display: flex; gap: 4px; align-items: center; }
.output-actions select {
  font-size: 11px; padding: 3px 6px; border: 1px solid var(--border);
  border-radius: 5px; color: var(--text); background: var(--surface);
  cursor: pointer; outline: none;
}
.btn-copy {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 8px; border: 1px solid var(--border);
  background: var(--surface); border-radius: 5px;
  font-size: 11px; cursor: pointer; color: var(--text-muted);
}
.btn-copy:hover { background: var(--bg); color: var(--text); }

.code-output {
  flex: 1; overflow-y: auto; overflow-x: auto;
  padding: 10px 12px; margin: 0;
  font-family: 'Monaco', 'Menlo', 'Consolas', monospace;
  font-size: 11px; line-height: 1.6;
  background: #0F0F1A; color: #CDD6F4;
  white-space: pre;
  height: calc(100vh - 160px);
}

/* Syntax highlighting classes */
.tf-comment { color: #6C7086; }
.tf-keyword { color: #CBA6F7; font-weight: 600; }
.tf-string { color: #A6E3A1; }
.tf-number { color: #FAB387; }
.tf-attr { color: #89DCEB; }
.tf-block { color: #89B4FA; }
.tf-resource { color: #F5C2E7; font-weight: 600; }
.tf-variable { color: #FAB387; }
.tf-ref { color: #94E2D5; }

/* Variables panel */
.vars-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 12px; font-weight: 600; color: var(--text-muted);
}
.btn-add-var {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 8px; border: 1px solid var(--primary);
  background: var(--primary-light); border-radius: 5px;
  font-size: 11px; cursor: pointer; color: var(--primary); font-weight: 600;
}
.var-item {
  padding: 8px 12px; border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 4px;
}
.var-row { display: flex; gap: 4px; align-items: center; }
.var-row input, .var-row select {
  flex: 1; padding: 5px 7px;
  border: 1px solid var(--border); border-radius: 5px;
  font-size: 11px; color: var(--text); background: var(--surface); outline: none;
}
.var-row input:focus { border-color: var(--primary); }
.var-del { padding: 4px 6px; border: 1px solid var(--border); border-radius: 5px; background: transparent; cursor: pointer; color: var(--danger); font-size: 11px; }

/* ===== TOAST ===== */
.toast {
  position: fixed; bottom: 20px; right: 20px;
  background: #1F2937; color: white;
  padding: 10px 16px; border-radius: var(--radius);
  font-size: 13px; font-weight: 500;
  transform: translateY(100px); opacity: 0;
  transition: all 0.3s;
  z-index: 9999; box-shadow: var(--shadow-md);
  display: flex; align-items: center; gap: 8px;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== GROUP / VPC OVERLAY ===== */
.canvas-group-rect { fill: rgba(124,58,237,0.04); stroke: rgba(124,58,237,0.2); stroke-width: 1.5; stroke-dasharray: 6,4; rx: 12; }
.canvas-group-label { font-size: 11px; fill: rgba(124,58,237,0.5); font-weight: 700; letter-spacing: 0.5px; text-transform: uppercase; }

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-light); }

/* ===== NODE COLOR THEMES ===== */
.node-aws .node-rect { stroke: #FF9900; }
.node-azure .node-rect { stroke: #0078D4; }
.node-gcp .node-rect { stroke: #4285F4; }

/* Cloud colored accents */
.ic-aws { color: #FF9900; }
.ic-azure { color: #0078D4; }
.ic-gcp { color: #4285F4; }
.ic-generic { color: #7C3AED; }

/* Context menu */
.ctx-menu {
  position: fixed; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-md); z-index: 9000; min-width: 160px;
  overflow: hidden; animation: fadeIn 0.1s;
}
.ctx-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; font-size: 12px; cursor: pointer; color: var(--text);
}
.ctx-item:hover { background: var(--bg); }
.ctx-item.danger { color: var(--danger); }
.ctx-divider { height: 1px; background: var(--border); }

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

/* Resize/responsive */
@media (max-width: 1100px) {
  .app-body { grid-template-columns: 200px 1fr 280px; }
}
