/* ─── Reset & Base ─── */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-panel: #1e1e3a;
  --bg-input: #252545;
  --bg-hover: #2a2a4a;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b8;
  --text-muted: #6a6a80;
  --accent: #00d4ff;
  --accent-hover: #00b8e0;
  --accent-dim: #0088aa;
  --border: #2a2a4a;
  --danger: #ff4757;
  --danger-hover: #e03e4e;
  --success: #2ed573;
  --warning: #ffa502;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --radius: 6px;
  --transition: 150ms ease;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
}

/* ─── Scrollbar ─── */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ─── Navbar ─── */
#navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  z-index: 100;
}

.nav-left .logo {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

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

.nav-right .status {
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 10px;
  background: var(--bg-input);
  border-radius: 12px;
}

.status.compiling {
  color: var(--warning);
}
.status.success {
  color: var(--success);
}
.status.error {
  color: var(--danger);
}

/* ─── Buttons ─── */
button {
  padding: 6px 14px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  font-family: var(--font-sans);
  transition: background var(--transition), border-color var(--transition);
}
button:hover {
  background: var(--accent-dim);
  border-color: var(--accent);
}
button:active {
  transform: scale(0.97);
}

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

.btn-secondary {
  background: var(--bg-input);
}

/* ─── Select ─── */
select {
  padding: 6px 10px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font-sans);
  cursor: pointer;
}
select:focus {
  outline: none;
  border-color: var(--accent);
}

/* ─── Main Layout ─── */
#panels {
  display: flex;
  height: calc(100vh - 48px - 36px);
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  min-width: 200px;
  overflow: hidden;
}

#editor-panel {
  flex: 1 1 30%;
}
#viewer-panel {
  flex: 1 1 40%;
}
#chat-panel {
  flex: 1 1 30%;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  min-height: 40px;
}

.panel-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
}

.editor-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.editor-controls select {
  font-size: 12px;
  padding: 4px 8px;
}

.editor-controls button {
  font-size: 12px;
  padding: 4px 10px;
}

#btn-compile {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: white;
}
#btn-compile:hover {
  background: var(--accent);
}

/* ─── Resize Handle ─── */
.resize-handle {
  width: 4px;
  background: var(--border);
  cursor: col-resize;
  transition: background var(--transition);
  flex-shrink: 0;
}
.resize-handle:hover,
.resize-handle.active {
  background: var(--accent);
}

/* ─── Editor ─── */
#editor-container {
  flex: 1;
  overflow: hidden;
}

#editor-container .CodeMirror {
  height: 100%;
  font-family: var(--font-mono);
  font-size: 14px;
  background: var(--bg-panel);
}

/* ─── 3D Viewer ─── */
#viewer-container {
  flex: 1;
  background: #0a0a1a;
  position: relative;
  overflow: hidden;
}

#viewer-container canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.viewer-info {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* ─── Chat ─── */
#chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-panel);
}

.chat-msg {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.5;
  max-width: 90%;
  word-wrap: break-word;
}

.chat-msg.user {
  background: var(--bg-tertiary);
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}

.chat-msg.assistant {
  background: var(--bg-input);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
  color: var(--text-secondary);
}

.chat-msg .msg-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ─── Chat Markdown ─── */
.chat-msg .msg-content pre {
  background: #0d0d1a;
  border-radius: 4px;
  padding: 8px 10px;
  overflow-x: auto;
  font-size: 12px;
  margin: 6px 0;
}

.chat-msg .msg-content code {
  font-family: var(--font-mono);
  font-size: 12px;
}

.chat-msg .msg-content p {
  margin: 4px 0;
}

.chat-msg .msg-content strong {
  color: var(--accent);
}

.chat-msg .msg-content ul, .chat-msg .msg-content ol {
  padding-left: 18px;
  margin: 4px 0;
}

/* Streaming indicator */
.typing-indicator {
  animation: pulse 1.2s infinite;
  color: var(--accent);
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

.cursor-blink {
  animation: blink 0.8s infinite;
  color: var(--accent);
  font-size: 14px;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.chat-msg.streaming {
  border-left: 2px solid var(--accent);
}

#chat-model-bar {
  display: flex;
  padding: 4px 12px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

#model-select {
  font-size: 11px;
  padding: 3px 6px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  width: 100%;
}

#chat-input-area {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  align-items: flex-end;
}

#btn-chat-attach {
  padding: 8px;
  font-size: 16px;
  line-height: 1;
  flex-shrink: 0;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#btn-chat-send {
  height: 38px;
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  padding: 8px 12px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-sans);
  font-size: 13px;
  resize: none;
}
#chat-input:focus {
  outline: none;
  border-color: var(--accent);
}

#btn-chat-send {
  padding: 8px 16px;
  background: var(--accent-dim);
  border-color: var(--accent);
  color: white;
}
#btn-chat-send:hover {
  background: var(--accent);
}

/* ─── Bottom Bar ─── */
#bottom-bar {
  height: 36px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

.version-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  overflow-x: auto;
}

.version-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

#version-timeline {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
}

.version-dot {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--bg-input);
  border: 2px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  color: var(--text-muted);
  transition: all var(--transition);
  flex-shrink: 0;
}

.version-dot:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  transform: scale(1.15);
}

.version-dot.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: white;
}

.version-connector {
  width: 16px;
  height: 2px;
  background: var(--border);
  flex-shrink: 0;
}

/* ─── Modal ─── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px;
  width: 380px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal h2 {
  font-size: 18px;
  color: var(--accent);
}

.modal input {
  padding: 10px 14px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: var(--font-sans);
}
.modal input:focus {
  outline: none;
  border-color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}

/* ─── Materials Panel ─── */
.materials-panel {
  position: absolute;
  top: 40px;
  right: 0;
  width: 220px;
  max-height: calc(100% - 40px);
  overflow-y: auto;
  background: rgba(22, 33, 62, 0.95);
  border-left: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 10px;
  z-index: 50;
  backdrop-filter: blur(8px);
  border-radius: 0 0 0 var(--radius);
}

.materials-panel.hidden {
  display: none;
}

.mat-section {
  margin-bottom: 10px;
}

.mat-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.mat-label span {
  color: var(--accent);
  font-weight: 700;
}

.mat-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.mat-preset-btn {
  font-size: 10px;
  padding: 3px 7px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
}

.mat-preset-btn:hover {
  border-color: var(--accent);
  color: var(--text-primary);
}

.mat-preset-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: white;
}

.materials-panel input[type="range"] {
  width: 100%;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

.materials-panel input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.materials-panel input[type="color"] {
  width: 100%;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-input);
  cursor: pointer;
  padding: 2px;
}

.materials-panel input[type="checkbox"] {
  accent-color: var(--accent);
}

/* Make viewer-panel position relative for overlay */
#viewer-panel {
  position: relative;
}

@media (max-width: 768px) {
  .materials-panel {
    width: 180px;
  }
}

/* ─── Empty States ─── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  font-size: 14px;
  gap: 8px;
}

.empty-state .icon {
  font-size: 36px;
  opacity: 0.5;
}

/* ─── Mobile Tab Bar (hidden on desktop) ─── */
#mobile-tabs {
  display: none;
  height: 44px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

#mobile-tabs .tab-btn {
  flex: 1;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0;
  position: relative;
  transition: color var(--transition);
}

#mobile-tabs .tab-btn.active {
  color: var(--accent);
}

#mobile-tabs .tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 20%;
  right: 20%;
  height: 2px;
  background: var(--accent);
  border-radius: 1px;
}

#mobile-tabs .tab-btn:hover {
  background: transparent;
  border-color: transparent;
}

/* ─── Responsive: Tablet (≤1024px) ─── */
@media (max-width: 1024px) {
  #chat-panel {
    flex: 0 0 220px;
    min-width: 180px;
  }

  .nav-left .logo {
    font-size: 14px;
  }

  .modal {
    width: 320px;
    padding: 20px;
  }
}

/* ─── Responsive: Mobile (≤768px) ─── */
@media (max-width: 768px) {
  #mobile-tabs {
    display: flex;
  }

  #navbar {
    height: 42px;
    padding: 0 10px;
  }

  .nav-left .logo {
    font-size: 13px;
  }

  .nav-center {
    gap: 4px;
  }

  .nav-center select {
    max-width: 120px;
    font-size: 12px;
    padding: 4px 6px;
  }

  .nav-center button {
    font-size: 11px;
    padding: 4px 8px;
  }

  #btn-delete-project {
    display: none;
  }

  .nav-right .status {
    font-size: 10px;
    padding: 3px 8px;
  }

  /* Stack panels — only show active one */
  #panels {
    height: calc(100vh - 42px - 44px - 36px);
    flex-direction: column;
  }

  .panel {
    display: none;
    flex: 1 1 100%;
    min-width: 100%;
    width: 100%;
  }

  .panel.mobile-active {
    display: flex;
    height: 100%;
    min-height: 0;
  }

  /* Hide resize handles on mobile */
  .resize-handle {
    display: none;
  }

  /* Editor panel adjustments */
  .panel-header {
    padding: 6px 10px;
    min-height: 36px;
    flex-wrap: wrap;
    gap: 4px;
  }

  .editor-controls {
    gap: 4px;
  }

  .editor-controls select {
    font-size: 11px;
    padding: 3px 6px;
  }

  .editor-controls button {
    font-size: 11px;
    padding: 3px 8px;
  }

  #editor-container .CodeMirror {
    font-size: 13px;
  }

  /* Chat adjustments */
  #chat-messages {
    padding: 8px;
  }

  .chat-msg {
    font-size: 13px;
    padding: 8px 12px;
    max-width: 95%;
  }

  #chat-input-area {
    padding: 8px;
    gap: 6px;
  }

  #chat-input {
    font-size: 14px;
    padding: 8px 10px;
  }

  /* Bottom bar */
  #bottom-bar {
    height: 36px;
    padding: 0 10px;
  }

  .version-label {
    font-size: 10px;
  }

  .version-dot {
    width: 20px;
    height: 20px;
    font-size: 8px;
  }

  .version-connector {
    width: 8px;
  }

  /* Modal */
  .modal {
    width: calc(100vw - 32px);
    max-width: 360px;
    padding: 20px;
  }
}

/* ─── Responsive: Small Mobile (≤480px) ─── */
@media (max-width: 480px) {
  #navbar {
    height: 38px;
    padding: 0 8px;
  }

  .nav-left .logo {
    font-size: 12px;
    letter-spacing: -0.3px;
  }

  .nav-center select {
    max-width: 90px;
  }

  #panels {
    height: calc(100vh - 38px - 44px - 32px);
  }

  #bottom-bar {
    height: 32px;
  }

  .panel-header {
    padding: 4px 8px;
    min-height: 32px;
  }

  .panel-title {
    font-size: 11px;
  }
}
