:root {
  --bg: #0f1115;
  --surface: #161a22;
  --surface-2: #1d222c;
  --border: #262c38;
  --text: #e8ecf3;
  --text-dim: #9aa3b2;
  --text-muted: #6b7280;
  --accent: #f92b89;
  --accent-2: #ffd21f;
  --ok: #2dd4bf;
  --warn: #fbbf24;
  --err: #ef4444;
  --radius: 8px;
  --radius-sm: 6px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px; line-height: 1.5;
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* ===== Top bar ===== */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: 0.2px; }
.page-tag {
  background: var(--surface-2); color: var(--text-dim);
  padding: 2px 8px; border-radius: 999px; font-size: 11px;
  border: 1px solid var(--border);
}
.logo-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 8px var(--accent);
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.divider { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

/* ===== Buttons & inputs ===== */
.btn {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 12px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; cursor: pointer;
  transition: all 0.15s ease;
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: #d61b73; border-color: #d61b73; }
.btn-secondary { background: transparent; }
.btn-ghost { background: transparent; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Botón Compare destacado (magenta con glow) */
.btn-compare {
  background: linear-gradient(135deg, var(--accent), #ff5da3) !important;
  color: #fff !important;
  border-color: transparent !important;
  font-weight: 700 !important;
  box-shadow: 0 0 0 0 rgba(249, 43, 137, 0.6);
  animation: compare-pulse 2.4s infinite;
}
.btn-compare:hover {
  background: linear-gradient(135deg, #d61b73, var(--accent)) !important;
  transform: translateY(-1px);
}
.btn-compare.active {
  background: var(--accent-2) !important;
  color: #111 !important;
  animation: none;
  box-shadow: 0 0 0 2px rgba(255, 210, 31, 0.3);
}
@keyframes compare-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(249, 43, 137, 0.55); }
  50% { box-shadow: 0 0 0 8px rgba(249, 43, 137, 0); }
}

/* Botón Mark cambios */
.btn-mark {
  background: transparent !important;
  color: #ff3b3b !important;
  border-color: #ff3b3b !important;
  font-weight: 600 !important;
}
.btn-mark:hover {
  background: rgba(255, 59, 59, 0.1) !important;
}
.btn-mark.active {
  background: #ff3b3b !important;
  color: #fff !important;
  border-color: #ff3b3b !important;
}

select, textarea, input[type="text"] {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 10px; border-radius: var(--radius-sm);
  font-family: inherit; font-size: 13px;
  outline: none;
}
select:focus, textarea:focus, input:focus { border-color: var(--accent); }

.viewport-toggle {
  display: flex; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); overflow: hidden;
}
.viewport-toggle .vp {
  background: transparent; color: var(--text-dim);
  border: 0; padding: 6px 10px; font-size: 12px; cursor: pointer;
  border-right: 1px solid var(--border);
}
.viewport-toggle .vp:last-child { border-right: 0; }
.viewport-toggle .vp.active { background: var(--accent); color: #fff; }

.link {
  background: transparent; border: 0; color: var(--accent);
  cursor: pointer; font-size: 12px; padding: 0;
}
.link:hover { text-decoration: underline; }

/* ===== Layout ===== */
.layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  flex: 1; min-height: 0;
  transition: grid-template-columns 0.25s ease;
}
.layout.sidebar-collapsed {
  grid-template-columns: 0 1fr;
}
.sidebar {
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: transform 0.25s ease;
}
.layout.sidebar-collapsed .sidebar {
  transform: translateX(-100%);
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-right: 4px;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.icon-btn.danger { color: #fca5a5; border-color: rgba(239, 68, 68, 0.3); }
.icon-btn.danger:hover { background: rgba(239, 68, 68, 0.15); color: #ef4444; border-color: #ef4444; }
.sidebar-section { display: flex; flex-direction: column; min-height: 0; }
.sidebar-changes { flex: 1 1 60%; border-bottom: 1px solid var(--border); }
.sidebar-info { flex: 0 0 auto; max-height: 40%; overflow-y: auto; }

.live-dot {
  color: var(--ok); font-size: 14px; opacity: 0.5;
  margin-left: 4px;
  transition: all 0.2s ease;
}
.live-dot.pulse { opacity: 1; transform: scale(1.4); text-shadow: 0 0 8px var(--ok); }

.head-actions { display: flex; gap: 8px; }

.info-block {
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.info-title {
  margin: 0 0 8px; font-size: 11px; font-weight: 600; letter-spacing: 0.5px;
  text-transform: uppercase; color: var(--text-dim);
}
.info-steps {
  margin: 0; padding-left: 18px; color: var(--text-dim); font-size: 12px;
}
.info-steps li { margin-bottom: 4px; }
.info-steps em { color: var(--text); font-style: normal; font-weight: 500; }

.manual-add > summary {
  cursor: pointer; font-size: 12px; color: var(--text-dim);
  list-style: none; user-select: none; padding: 8px 16px;
}
.manual-add > summary::before { content: '▸ '; }
.manual-add[open] > summary::before { content: '▾ '; }
.manual-add[open] > summary { background: var(--surface-2); }
.manual-form {
  display: flex; flex-direction: column; gap: 6px;
  padding: 10px 16px 16px;
}
.manual-form input, .manual-form textarea, .manual-form select {
  width: 100%;
}
.manual-form textarea { resize: vertical; font-family: ui-monospace, 'SF Mono', monospace; font-size: 12px; }
.manual-form button { align-self: flex-start; }

.section-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.section-head h2 {
  margin: 0; font-size: 12px; font-weight: 600; letter-spacing: 0.6px;
  text-transform: uppercase; color: var(--text-dim);
}
.counter {
  display: inline-block; background: var(--border); color: var(--text);
  font-size: 11px; padding: 1px 7px; border-radius: 999px; margin-left: 6px;
}

/* ===== Changes list ===== */
.changes-list {
  list-style: none; margin: 0; padding: 8px;
  overflow-y: auto; flex: 1; min-height: 0;
}
.changes-list .empty {
  color: var(--text-muted); padding: 16px; text-align: center; font-size: 13px;
}
.change-item {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 12px; margin-bottom: 8px;
  font-size: 13px;
}
.change-item.applied {
  border-color: var(--accent);
  background: rgba(249, 43, 137, 0.08);
}
.change-item.just-arrived {
  animation: arrived 1.6s ease;
  box-shadow: 0 0 0 2px var(--ok), 0 0 12px rgba(45, 212, 191, 0.4);
}
@keyframes arrived {
  0% { transform: scale(0.96); opacity: 0; }
  60% { transform: scale(1.02); }
  100% { transform: scale(1); opacity: 1; }
}
.change-head {
  display: flex; align-items: center; gap: 8px; margin-bottom: 6px;
}
.change-id {
  font-family: ui-monospace, 'SF Mono', monospace;
  font-size: 11px; color: var(--text-dim);
  background: var(--bg); padding: 1px 6px; border-radius: 4px;
}
.change-title {
  flex: 1; font-weight: 600; font-size: 13px; line-height: 1.3;
}
.change-prio {
  font-size: 10px; padding: 1px 6px; border-radius: 999px; text-transform: uppercase;
  font-weight: 600; letter-spacing: 0.4px;
}
.change-kind {
  font-size: 9px; padding: 1px 5px; border-radius: 3px; letter-spacing: 0.5px;
  font-weight: 700; font-family: ui-monospace, 'SF Mono', monospace;
}
.kind-css { background: rgba(124, 58, 237, 0.15); color: #c4b5fd; }
.kind-dom_move,
.kind-dom_insert,
.kind-dom_replace,
.kind-dom_remove { background: rgba(45, 212, 191, 0.18); color: #5eead4; }
.prio-alta { background: rgba(239, 68, 68, 0.15); color: #fca5a5; }
.prio-media { background: rgba(251, 191, 36, 0.15); color: #fcd34d; }
.prio-baja { background: rgba(45, 212, 191, 0.15); color: #5eead4; }
.change-body {
  color: var(--text-dim); font-size: 12px; margin: 4px 0 8px;
}
.change-meta {
  display: flex; gap: 6px; flex-wrap: wrap; align-items: center;
  margin-top: 6px;
}
.change-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  cursor: pointer; user-select: none;
  font-size: 12px; color: var(--text-dim);
}
.change-toggle input { accent-color: var(--accent); }
.change-actions {
  margin-left: auto; display: flex; gap: 4px;
}
.change-actions button {
  background: transparent; border: 0; color: var(--text-muted);
  font-size: 11px; cursor: pointer; padding: 2px 4px;
}
.change-actions button:hover { color: var(--text); }
details.change-details { margin-top: 6px; }
details.change-details > summary {
  cursor: pointer; color: var(--text-muted); font-size: 11px;
  list-style: none; user-select: none;
}
details.change-details > summary::before { content: '▸ '; }
details.change-details[open] > summary::before { content: '▾ '; }
details.change-details pre {
  margin: 6px 0 0; padding: 8px; background: var(--bg);
  border-radius: 4px; font-size: 11px;
  overflow-x: auto; max-height: 200px;
  border: 1px solid var(--border);
  font-family: ui-monospace, 'SF Mono', monospace;
  color: #c8d3e0;
}
details.change-details .field { font-size: 11px; color: var(--text-dim); margin: 4px 0; }
details.change-details .field strong { color: var(--text); }

/* ===== Chat ===== */
.chat-log {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 12px; display: flex; flex-direction: column; gap: 10px;
}
.msg { padding: 10px 12px; border-radius: var(--radius-sm); font-size: 13px; }
.msg p { margin: 0; }
.msg.system { background: var(--surface-2); color: var(--text-dim); border: 1px dashed var(--border); }
.msg.user { background: var(--accent); color: #fff; align-self: flex-end; max-width: 85%; }
.msg.assistant { background: var(--surface-2); border: 1px solid var(--border); max-width: 95%; }
.msg.error { background: rgba(239, 68, 68, 0.12); border: 1px solid rgba(239,68,68,0.4); color: #fca5a5; }
.msg.loading { color: var(--text-muted); font-style: italic; }

.chat-form {
  display: flex; gap: 8px; padding: 10px;
  border-top: 1px solid var(--border); background: var(--surface);
}
.chat-form textarea {
  flex: 1; resize: none; min-height: 40px; max-height: 120px;
}
.chat-form button { align-self: stretch; }
.hint {
  padding: 0 12px 8px; color: var(--text-muted); font-size: 11px; min-height: 14px;
}

/* ===== Preview ===== */
.preview {
  background: #2a2f3a;
  display: flex; align-items: flex-start; justify-content: center;
  padding: 16px; overflow: auto;
  gap: 16px;
}
.preview-frame {
  background: #fff; border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
  transition: width 0.2s ease;
  width: 100%; height: 100%;
  position: relative;
}
.preview-frame[data-vp="mobile"] { width: 390px; height: 844px; }
.preview-frame[data-vp="tablet"] { width: 820px; height: 1180px; }
.preview-frame[data-vp="desktop"] { width: 100%; height: 100%; min-height: calc(100vh - 65px - 32px); }
.preview-frame iframe {
  width: 100%; height: 100%; border: 0; display: block;
  background: #fff;
}

/* Compare mode: 2 frames side by side */
.preview.compare-mode {
  align-items: stretch;
}
.preview.compare-mode .preview-frame {
  flex: 1 1 0;
  width: auto !important;
  max-width: calc(50% - 8px);
  min-width: 0;
}
.preview.compare-mode .preview-frame[data-vp="mobile"] { max-width: 390px; }
.preview.compare-mode .preview-frame[data-vp="tablet"] { max-width: 820px; }

.preview-frame.compare-frame {
  display: flex;
  flex-direction: column;
}
/* Override del [hidden] para que funcione sobre flex */
.preview-frame[hidden] { display: none !important; }
/* En pantallas estrechas, compare-mode oculta original automaticamente */
@media (max-width: 900px) {
  .preview.compare-mode .preview-frame.compare-frame.original {
    display: none !important;
  }
  .preview.compare-mode .preview-frame.compare-frame.current {
    max-width: 100% !important;
    flex: 1 1 100% !important;
  }
}
.preview-frame.compare-frame iframe {
  flex: 1;
  min-height: 0;
}
.frame-label {
  position: relative;
  display: none;
  width: 100%;
  text-align: center;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 2.5px;
  padding: 10px 12px;
  flex-shrink: 0;
  text-transform: uppercase;
  background: var(--surface-2);
  color: var(--text);
  border-radius: 0;
  z-index: 1;
}
.preview-frame.compare-frame.original .frame-label {
  background: #1a1f2a;
  color: #9aa3b2;
  border-bottom: 3px solid #4a5060;
}
.preview-frame.compare-frame.original .frame-label::before {
  content: '○ ';
  margin-right: 4px;
}
.preview-frame.compare-frame.current .frame-label {
  background: linear-gradient(90deg, #ffd21f, #ffe066);
  color: #111;
  border-bottom: 3px solid var(--accent);
  box-shadow: 0 2px 12px rgba(255, 210, 31, 0.4);
}
.preview-frame.compare-frame.current .frame-label::before {
  content: '● ';
  color: var(--accent);
  margin-right: 4px;
}
.preview.compare-mode .frame-label { display: block; }

/* ===== Toast ===== */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: var(--radius-sm); box-shadow: var(--shadow);
  font-size: 13px; z-index: 9999;
}
.toast.ok { border-color: var(--ok); }
.toast.err { border-color: var(--err); color: #fca5a5; }

/* ===== Scrollbars ===== */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #2f3644; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { max-height: 50vh; }
}
