:root {
  --bg-primary: #f8fafc;
  --bg-secondary: #ffffff;
  --bg-tertiary: #f1f5f9;
  --bg-canvas: #e2e8f0;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --border-color: #e2e8f0;
  --border-strong: #cbd5e1;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-light: #eef2ff;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --grid-line: rgba(99, 102, 241, 0.15);
  --grid-line-strong: rgba(99, 102, 241, 0.3);
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
}

.dark {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-canvas: #1a2332;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border-color: #334155;
  --border-strong: #475569;
  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-light: rgba(129, 140, 248, 0.1);
  --grid-line: rgba(129, 140, 248, 0.12);
  --grid-line-strong: rgba(129, 140, 248, 0.25);
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.5);
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  overflow: hidden;
}

/* Layout */
.app-container {
  display: grid;
  grid-template-rows: 56px 1fr auto;
  grid-template-columns: 260px 1fr 280px;
  grid-template-areas:
  "header header header"
  "left canvas right"
  "code code code";
  height: 100vh;
}

/* ===== FULLSCREEN CODE MODE ===== */
.app-container.fullscreen-code {
  grid-template-rows: 56px 1fr;
  grid-template-columns: 1fr;
  grid-template-areas:
  "header"
  "code";
}

.app-container.fullscreen-code .code-panel {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app-container.fullscreen-code .code-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-panel, #111);
}

/* Fullscreen toggle icon */
.app-container.fullscreen-code .fullscreen-toggle i {
  transform: rotate(180deg);
}

.app-container.fullscreen-code .left-panel,
.app-container.fullscreen-code .right-panel,
.app-container.fullscreen-code .canvas-container {
  display: none;
}

.app-container.fullscreen-code .code-panel {
  max-height: 100vh;
  height: 100%;
}

.app-container.fullscreen-code .code-content {
  height: calc(100vh - 56px - 44px);
}

/* Header */
.header {
  grid-area: header;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--accent);
}

.logo i {
  font-size: 1.5rem;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
  font-family: inherit;
}

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

.btn-primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

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

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-sm);
}

/* Panels */
.panel {
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  padding: 16px;
}

.panel::-webkit-scrollbar {
  width: 6px;
}

.panel::-webkit-scrollbar-track {
  background: transparent;
}

.panel::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 3px;
}

.left-panel {
  grid-area: left;
}

.right-panel {
  grid-area: right;
  border-right: none;
  border-left: 1px solid var(--border-color);
}

.panel-section {
  margin-bottom: 24px;
}

.panel-title {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.panel-title i {
  font-size: 0.75rem;
}

/* Form Controls */
.form-group {
  margin-bottom: 12px;
}

.form-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.875rem;
  font-family: inherit;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.form-row-3 {
  grid-template-columns: 1fr 1fr 1fr;
}

.input-with-unit {
  position: relative;
}

.input-with-unit .form-input {
  padding-right: 36px;
}

.input-unit {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.75rem;
  color: var(--text-muted);
}

select.form-input {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2394a3b8'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 16px;
  padding-right: 32px;
}

/* Canvas */
.canvas-container {
  grid-area: canvas;
  background: var(--bg-canvas);
  overflow: auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.canvas-wrapper {
  position: relative;
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.grid-canvas {
  position: relative;
  display: grid;
  min-width: 600px;
  min-height: 400px;
  background-size: var(--cell-width) var(--cell-height);
  background-image:
  linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
  linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  transition: background-size 0.3s ease;
}

.grid-canvas.show-strong-grid {
  background-image:
  linear-gradient(to right, var(--grid-line-strong) 1px, transparent 1px),
  linear-gradient(to bottom, var(--grid-line-strong) 1px, transparent 1px);
}

/* Grid Items */
.grid-item {
  position: relative;
  background: linear-gradient(135deg, var(--accent-light), transparent);
  border: 2px solid var(--accent);
  border-radius: var(--radius-md);
  cursor: move;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--accent);
  transition: box-shadow 0.2s, transform 0.1s;
  user-select: none;
  min-height: 60px;
}

.grid-item:hover {
  box-shadow: 0 0 0 2px var(--accent), var(--shadow-md);
}

.grid-item.selected {
  box-shadow: 0 0 0 3px var(--accent), var(--shadow-lg);
  z-index: 10;
}

.grid-item.dragging {
  opacity: 0.9;
  z-index: 100;
  cursor: grabbing;
}

.item-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.item-delete {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  opacity: 0;
  transition: opacity 0.2s;
}

.grid-item:hover .item-delete,
.grid-item.selected .item-delete {
  opacity: 1;
}

.item-delete:hover {
  background: #dc2626;
  transform: scale(1.1);
}

/* Resize Handles */
.resize-handle {
  position: absolute;
  background: var(--accent);
  border-radius: 2px;
  opacity: 0;
  transition: opacity 0.2s;
}

.grid-item:hover .resize-handle,
.grid-item.selected .resize-handle {
  opacity: 1;
}

.resize-handle.corner {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--bg-secondary);
}

.resize-handle.nw {
  top: -5px;
  left: -5px;
  cursor: nwse-resize;
}
.resize-handle.ne {
  top: -5px;
  right: -5px;
  cursor: nesw-resize;
}
.resize-handle.sw {
  bottom: -5px;
  left: -5px;
  cursor: nesw-resize;
}
.resize-handle.se {
  bottom: -5px;
  right: -5px;
  cursor: nwse-resize;
}

.resize-handle.edge {
  background: var(--accent);
}
.resize-handle.n {
  top: -3px;
  left: 20%;
  right: 20%;
  height: 6px;
  cursor: ns-resize;
}
.resize-handle.s {
  bottom: -3px;
  left: 20%;
  right: 20%;
  height: 6px;
  cursor: ns-resize;
}
.resize-handle.w {
  left: -3px;
  top: 20%;
  bottom: 20%;
  width: 6px;
  cursor: ew-resize;
}
.resize-handle.e {
  right: -3px;
  top: 20%;
  bottom: 20%;
  width: 6px;
  cursor: ew-resize;
}

/* Drop Zone Highlight */
.drop-highlight {
  position: absolute;
  background: var(--accent);
  opacity: 0.15;
  border-radius: var(--radius-sm);
  pointer-events: none;
  transition: all 0.15s ease;
}

/* Code Panel */
.code-panel {
  grid-area: code;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  max-height: 280px;
  transition: max-height 0.3s ease;
}

.code-panel.collapsed {
  max-height: 44px;
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-tertiary);
  cursor: pointer;
}

.code-tabs {
  display: flex;
  gap: 4px;
}

.code-tab {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  background: transparent;
  color: var(--text-secondary);
  border: none;
}

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

.code-tab:hover:not(.active) {
  background: var(--border-color);
}

.code-actions {
  display: flex;
  gap: 6px;
}

.code-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.code-block {
  display: none;
  height: 100%;
}

.code-block.active {
  display: block;
}

.code-block pre {
  height: 100%;
  margin: 0;
  padding: 16px;
  overflow: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
}

.code-block code {
  display: block;
}

/* Color coding */
.tag {
  color: #e879f9;
}
.attr {
  color: #38bdf8;
}
.string {
  color: #4ade80;
}
.property {
  color: #fb923c;
}
.value {
  color: #a78bfa;
}
.comment {
  color: var(--text-muted);
  font-style: italic;
}
.selector {
  color: #fbbf24;
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  text-align: center;
  padding: 40px;
}

.empty-state i {
  font-size: 3rem;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.empty-state p {
  font-size: 0.875rem;
}

/* Color Palette */
.color-palette {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.color-swatch {
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.2s;
}

.color-swatch:hover {
  transform: scale(1.1);
}

.color-swatch.active {
  border-color: var(--text-primary);
  box-shadow: 0 0 0 2px var(--bg-secondary);
}

/* Toggle Switch */
.toggle-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.toggle {
  position: relative;
  width: 40px;
  height: 22px;
  background: var(--border-strong);
  border-radius: 11px;
  cursor: pointer;
  transition: background 0.2s;
}

.toggle.active {
  background: var(--accent);
}

.toggle::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
}

.toggle.active::after {
  transform: translateX(18px);
}

/* Tooltip */
.tooltip {
  position: relative;
}

.tooltip::before {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(-4px);
  padding: 6px 10px;
  background: var(--text-primary);
  color: var(--bg-primary);
  font-size: 0.75rem;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}

.tooltip:hover::before {
  opacity: 1;
  transform: translateX(-50%) translateY(-8px);
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 320px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  padding: 12px 24px;
  background: var(--text-primary);
  color: var(--bg-primary);
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s ease;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 10px;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast i {
  color: var(--success);
}

/* Info Box */
.info-box {
  background: var(--accent-light);
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  padding: 12px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.info-box h4 {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Divider */
.divider {
  height: 1px;
  background: var(--border-color);
  margin: 16px 0;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.modal-overlay.show {
  opacity: 1;
  pointer-events: all;
}

.modal {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: 24px;
  max-width: 400px;
  width: 90%;
  transform: scale(0.9);
  transition: transform 0.2s;
}

.modal-overlay.show .modal {
  transform: scale(1);
}

.modal-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.modal-text {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* Animations */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.grid-item {
  animation: fadeIn 0.3s ease;
}

/* Responsive */
@media (max-width: 1024px) {
  .app-container {
    grid-template-columns: 1fr;
    grid-template-rows: 56px auto 1fr auto;
    grid-template-areas:
    "header"
    "left"
    "canvas"
    "code";
  }

  .left-panel, .right-panel {
    border: none;
    border-bottom: 1px solid var(--border-color);
    max-height: 200px;
    overflow-y: auto;
  }

  .right-panel {
    display: none;
  }

  .canvas-container {
    padding: 20px;
  }

  .grid-canvas {
    min-width: 100%;
    min-height: 300px;
  }
}

@media (max-width: 640px) {
  .header {
    padding: 0 12px;
  }

  .logo span {
    display: none;
  }

  .btn span {
    display: none;
  }

  .btn {
    padding: 8px 10px;
  }

  .panel {
    padding: 12px;
  }
}