:root {
  --bg: #0F0E17;
  --surface: #1A1825;
  --surface2: #232136;
  --border: #2D2A3E;
  --text: #E0DEF4;
  --text-muted: #8B8BA7;
  --accent: #7C3AED;
  --accent2: #F59E0B;
  --accent-soft: rgba(124,58,237,0.12);
  --green: #10B981;
  --red: #EF4444;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.3);
}

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

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

/* Topbar */
.topbar {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 100;
}
.topbar-inner {
  max-width: 100%;
  height: 100%;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--text);
  font-weight: 700;
  font-size: 1.1rem;
}
.brand-icon { font-size: 1.3rem; }
.brand-accent {
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.topbar-nav { display: flex; gap: 8px; align-items: center; }

/* Language switcher */
.lang-switcher {
  display: flex;
  gap: 2px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  padding: 2px;
}
.lang-btn {
  padding: 4px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}
.lang-btn:hover {
  color: var(--text);
}
.lang-btn.active {
  background: var(--accent);
  color: #fff;
}

/* Buttons */
.btn-primary, .btn-ghost, .btn-sm, .btn-close {
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  transition: all 0.15s;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
  padding: 8px 16px;
  font-weight: 600;
  font-size: 0.88rem;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn-primary:hover { background: #6928D6; transform: translateY(-1px); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  padding: 6px 12px;
  font-size: 0.85rem;
  text-decoration: none;
}
.btn-ghost:hover { background: var(--surface2); color: var(--text); }
.btn-sm {
  background: var(--surface2);
  color: var(--text-muted);
  padding: 6px 12px;
  font-size: 0.8rem;
  border: 1px solid var(--border);
}
.btn-sm:hover { background: var(--border); color: var(--text); }
.btn-sm.btn-export { color: var(--accent2); border-color: rgba(245,158,11,0.3); }
.btn-sm.btn-export:hover { background: rgba(245,158,11,0.1); }
.btn-close {
  background: transparent;
  color: var(--text-muted);
  font-size: 1.4rem;
  padding: 4px 10px;
}
.btn-close:hover { color: var(--text); }

/* Layout */
.layout {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* Chat Panel */
.chat-panel {
  width: 380px;
  min-width: 320px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--surface);
}
.chat-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.chat-header h2 { font-size: 1rem; font-weight: 700; }
.chat-status {
  font-size: 0.75rem;
  color: var(--green);
  background: rgba(16,185,129,0.12);
  padding: 3px 10px;
  border-radius: 100px;
}
.chat-status.loading { color: var(--accent2); background: rgba(245,158,11,0.12); }
.chat-status.error { color: var(--red); background: rgba(239,68,68,0.12); }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.msg {
  display: flex;
  gap: 10px;
  max-width: 92%;
}
.msg.user { flex-direction: row-reverse; margin-left: auto; }
.msg-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.msg.user .msg-avatar { background: var(--accent2); }
.msg-bubble {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 0.88rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .msg-bubble {
  background: var(--accent);
  color: #fff;
  border: none;
  border-bottom-right-radius: 4px;
}
.msg.ai .msg-bubble { border-bottom-left-radius: 4px; }
.msg-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.chip {
  background: var(--accent-soft);
  border: 1px solid rgba(124,58,237,0.25);
  color: var(--accent);
  padding: 6px 12px;
  border-radius: 100px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}
.chip:hover { background: var(--accent); color: #fff; }

/* Loading dots */
.msg.loading .msg-bubble {
  display: flex;
  gap: 4px;
  align-items: center;
}
.msg.loading .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}
.msg.loading .dot:nth-child(2) { animation-delay: 0.2s; }
.msg.loading .dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* Chat input */
.chat-input-area {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}
#chatInput {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 10px 14px;
  font-size: 0.88rem;
  font-family: inherit;
  resize: none;
  transition: border-color 0.15s;
}
#chatInput:focus { outline: none; border-color: var(--accent); }
#chatInput::placeholder { color: var(--text-muted); }
#chatInput:disabled { opacity: 0.5; }

/* Preview Panel */
.preview-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}
.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.tab-group { display: flex; gap: 2px; }
.tab {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 16px;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: var(--radius-sm);
  font-family: inherit;
  transition: all 0.15s;
}
.tab:hover { color: var(--text); background: var(--surface2); }
.tab.active { color: var(--accent); background: var(--accent-soft); }
.toolbar-actions { display: flex; gap: 6px; }

/* Tab content */
.tab-content {
  flex: 1;
  display: none;
  overflow: hidden;
  position: relative;
}
.tab-content.active { display: flex; flex-direction: column; }

/* Canvas */
.canvas-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: auto;
  padding: 20px;
}
#turtleCanvas {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-width: 100%;
  max-height: 100%;
}
.canvas-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.canvas-overlay.hidden { display: none; }
.canvas-placeholder {
  text-align: center;
  color: var(--text-muted);
}
.placeholder-icon { font-size: 3rem; display: block; margin-bottom: 8px; }

.canvas-info {
  display: flex;
  gap: 20px;
  padding: 10px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.82rem;
  color: var(--text-muted);
  flex-wrap: wrap;
}
.info-item strong { color: var(--text); }

/* Code view */
.code-view {
  flex: 1;
  overflow: auto;
  padding: 20px;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.82rem;
  line-height: 1.6;
  background: var(--surface);
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}
.code-view .placeholder { color: var(--text-muted); font-style: italic; }
.code-view::-webkit-scrollbar { width: 6px; height: 6px; }
.code-view::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-overlay.active { display: flex; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 440px;
  max-width: 90vw;
  box-shadow: var(--shadow);
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h3 { font-size: 1.1rem; }
.modal-body { padding: 20px; }
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-muted);
}
.form-group select, .form-group input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 8px 12px;
  font-size: 0.9rem;
  font-family: inherit;
}
.form-group select:focus, .form-group input:focus { outline: none; border-color: var(--accent); }
.callout-info {
  background: var(--accent-soft);
  border: 1px solid rgba(124,58,237,0.2);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.5;
}

/* Responsive */
@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .chat-panel { width: 100%; min-width: 0; height: 45vh; border-right: none; border-bottom: 1px solid var(--border); }
  .preview-panel { height: 55vh; }
  #turtleCanvas { max-width: 90vw; max-height: 40vh; }
}
