/* ============================================
   StudioX v1.1 — Premium AI Builder
   ============================================ */

:root {
  /* Colores base */
  --bg-primary: #0a0a0f;
  --bg-secondary: #12121a;
  --bg-tertiary: #1a1a24;
  --bg-hover: #22222e;
  --bg-elevated: #1e1e2a;

  /* Bordes */
  --border: #26262f;
  --border-light: #33333f;
  --border-focus: #4f46e5;

  /* Texto */
  --text-primary: #f4f4f7;
  --text-secondary: #a8a8b3;
  --text-muted: #6b6b78;

  /* Acento — Índigo/Violeta premium */
  --accent: #6366f1;
  --accent-hover: #7c7ff5;
  --accent-active: #4f46e5;
  --accent-glow: rgba(99, 102, 241, 0.12);
  --accent-glow-strong: rgba(99, 102, 241, 0.25);

  /* Gradientes */
  --gradient-brand: linear-gradient(135deg, #6366f1 0%, #a855f7 50%, #ec4899 100%);
  --gradient-bg: radial-gradient(ellipse 80% 50% at 50% -20%, rgba(99, 102, 241, 0.15), transparent);

  /* Semánticos */
  --green: #10b981;
  --orange: #f59e0b;
  --red: #ef4444;

  /* Sistema */
  --radius: 12px;
  --radius-sm: 8px;
  --radius-xs: 6px;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', monospace;
  --sidebar-w: 280px;
  --topbar-h: 60px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.35);
  --shadow-glow: 0 0 24px var(--accent-glow-strong);
  --transition: 180ms cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="midnight"] {
  --bg-primary: #050814;
  --bg-secondary: #0a1020;
  --bg-tertiary: #0f172d;
  --bg-hover: #172140;
  --bg-elevated: #131c33;
  --border: #1e2a48;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-active: #2563eb;
  --accent-glow: rgba(59, 130, 246, 0.15);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  background-image: var(--gradient-bg);
  color: var(--text-primary);
  display: flex;
  font-size: 14px;
  line-height: 1.5;
}

button { font-family: inherit; }

/* ---- SCROLLBAR ---- */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 4px;
  border: 2px solid var(--bg-primary);
}
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ============================================
   SIDEBAR
   ============================================ */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: min-width var(--transition), width var(--transition);
  overflow: hidden;
  z-index: 20;
  position: relative;
}

.sidebar.collapsed {
  width: 0;
  min-width: 0;
  border-right: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  min-height: var(--topbar-h);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  letter-spacing: -0.01em;
}

.logo strong {
  color: var(--text-primary);
  font-weight: 700;
}

.logo-icon {
  width: 30px;
  height: 30px;
  background: var(--gradient-brand);
  color: #fff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 14px;
  box-shadow: var(--shadow-glow);
  letter-spacing: -0.02em;
}

/* ---- NAV TABS ---- */
.sidebar-nav {
  display: flex;
  gap: 4px;
  padding: 10px;
  border-bottom: 1px solid var(--border);
}

.nav-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  padding: 10px 4px;
  border-radius: var(--radius-xs);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  transition: all var(--transition);
  letter-spacing: 0.01em;
}

.nav-btn:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.nav-btn.active {
  background: var(--accent-glow);
  color: var(--accent-hover);
}

.nav-icon { font-size: 15px; }

/* ---- PANELS ---- */
.sidebar-panel {
  display: none;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  flex: 1;
  overflow-y: auto;
}

.sidebar-panel.active { display: flex; }

.sidebar-panel label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: -4px;
}

.sidebar-panel select,
.sidebar-panel input,
.sidebar-panel textarea {
  width: 100%;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 9px 11px;
  border-radius: var(--radius-xs);
  font-size: 13px;
  font-family: var(--font-sans);
  outline: none;
  transition: all var(--transition);
}

.sidebar-panel select:hover,
.sidebar-panel input:hover,
.sidebar-panel textarea:hover {
  border-color: var(--border-light);
}

.sidebar-panel select:focus,
.sidebar-panel input:focus,
.sidebar-panel textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.sidebar-panel textarea {
  resize: vertical;
  font-size: 12.5px;
  line-height: 1.55;
  font-family: var(--font-mono);
}

.range-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.range-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
}

.range-val {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-hover);
  min-width: 32px;
  text-align: center;
  background: var(--accent-glow);
  padding: 2px 6px;
  border-radius: 4px;
}

.hint {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.45;
}

.empty-msg {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  padding: 40px 0;
  font-style: italic;
}

.sidebar-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  text-align: center;
}

.version {
  font-size: 10px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  letter-spacing: 0.05em;
}

/* ============================================
   MAIN & TOPBAR
   ============================================ */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  backdrop-filter: blur(10px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.topbar-left h2 {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.token-count {
  font-size: 11px;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 20px;
  font-weight: 600;
  font-family: var(--font-mono);
  letter-spacing: -0.02em;
}

/* ---- BOTONES ---- */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: var(--radius-xs);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all var(--transition);
  box-shadow: 0 1px 2px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.15);
  letter-spacing: -0.01em;
}

.btn-primary:hover {
  background: var(--accent-hover);
  box-shadow: 0 4px 12px var(--accent-glow-strong), inset 0 1px 0 rgba(255,255,255,0.2);
  transform: translateY(-1px);
}

.btn-primary:active {
  transform: translateY(0);
  background: var(--accent-active);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn-icon {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-xs);
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--border-light);
}

.btn-icon-sm { font-size: 13px; }

.mobile-menu { display: none; }

/* ============================================
   WORKSPACE & CHAT
   ============================================ */
.workspace {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.chat-panel {
  width: 400px;
  min-width: 340px;
  background: var(--bg-primary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ---- WELCOME ---- */
.welcome {
  text-align: center;
  padding: 30px 10px;
  animation: fadeIn 0.4s ease;
}

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

.welcome-icon {
  width: 64px;
  height: 64px;
  background: var(--gradient-brand);
  color: #fff;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 26px;
  margin: 0 auto 20px;
  box-shadow: var(--shadow-glow);
  letter-spacing: -0.02em;
}

.welcome h3 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
  letter-spacing: -0.02em;
  background: linear-gradient(180deg, var(--text-primary), var(--text-secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome p {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 24px;
  line-height: 1.55;
}

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

.suggestion {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 12px 10px;
  border-radius: var(--radius-xs);
  font-size: 11.5px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  text-align: left;
  line-height: 1.4;
}

.suggestion:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: var(--accent-glow);
  transform: translateY(-1px);
}

/* ---- MENSAJES ---- */
.msg {
  max-width: 92%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.55;
  word-break: break-word;
  animation: fadeIn 0.25s ease;
}

.msg-user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.msg-ai {
  align-self: flex-start;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-bottom-left-radius: 4px;
}

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

.chat-input-area textarea {
  flex: 1;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13.5px;
  font-family: var(--font-sans);
  resize: none;
  outline: none;
  max-height: 120px;
  transition: all var(--transition);
  line-height: 1.5;
}

.chat-input-area textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: var(--radius);
  font-size: 16px;
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
}

.btn-send:hover {
  background: var(--accent-hover);
  box-shadow: 0 4px 12px var(--accent-glow-strong);
  transform: translateY(-1px);
}

.btn-send:active { transform: translateY(0); }

/* ============================================
   CODE + PREVIEW
   ============================================ */
.code-preview {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  min-width: 0;
}

.tabs {
  display: flex;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
  gap: 2px;
}

.tab {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 12.5px;
  font-weight: 600;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all var(--transition);
  letter-spacing: -0.01em;
}

.tab:hover { color: var(--text-secondary); }

.tab.active {
  color: var(--accent-hover);
  border-bottom-color: var(--accent);
}

.view-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.view {
  display: none;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.view.active {
  display: flex;
  flex-direction: column;
}

.view iframe {
  flex: 1;
  width: 100%;
  border: none;
  background: #fff;
}

.code-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.code-toolbar span {
  flex: 1;
  letter-spacing: 0.02em;
}

.btn-sm {
  background: var(--bg-hover);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font-sans);
}

.btn-sm:hover {
  background: var(--accent-glow);
  color: var(--accent-hover);
  border-color: var(--accent);
}

.code-editor {
  flex: 1;
  width: 100%;
  background: #08080c;
  color: #a5d6ff;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.65;
  padding: 18px;
  border: none;
  resize: none;
  outline: none;
  tab-size: 2;
}

/* ---- SPLIT VIEW ---- */
.split-view {
  flex-direction: row !important;
}

.split-view .code-editor {
  width: 50%;
}

.split-divider {
  width: 3px;
  background: var(--border);
  cursor: col-resize;
  transition: background var(--transition);
}

.split-divider:hover { background: var(--accent); }

.split-view iframe {
  width: 50%;
  flex: none;
  height: 100%;
}

/* ============================================
   PREVIEW LOADER EMBEBIDO (NUEVO)
   ============================================ */
.preview-loader {
  display: none;
  position: absolute;
  inset: 0;
  background: var(--bg-primary);
  z-index: 10;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  animation: fadeIn 0.2s ease;
}

.preview-loader.active {
  display: flex;
}

.loader-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-light);
  border-top-color: var(--accent);
  border-right-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.preview-loader p {
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: -0.01em;
  font-family: var(--font-sans);
}

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

/* ============================================
   HISTORY ITEMS
   ============================================ */
.history-item {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  padding: 11px 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: all var(--transition);
}

.history-item:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
  transform: translateX(2px);
}

.history-item h4 {
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-primary);
}

.history-item span {
  font-size: 10.5px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
  .chat-panel {
    width: 340px;
    min-width: 300px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 50;
    box-shadow: var(--shadow-md);
  }
  .sidebar.collapsed {
    width: 0;
    min-width: 0;
  }
  .mobile-menu { display: flex; }
  .chat-panel {
    width: 100%;
    min-width: 0;
    max-height: 45vh;
  }
  .workspace { flex-direction: column; }
  .code-preview {
    min-height: 55vh;
    border-top: 1px solid var(--border);
  }
  .split-view {
    flex-direction: column !important;
  }
  .split-view .code-editor,
  .split-view iframe {
    width: 100%;
    height: 50%;
  }
  .split-divider {
    width: 100%;
    height: 3px;
    cursor: row-resize;
  }
  .token-count { display: none; }
}

/* ---- Reducir movimiento accesible ---- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
