:root {
  color-scheme: dark;
  --bg-main: #0b0c10;
  --bg-toolbar: #0f1016;
  --bg-menu: #171821;
  --bg-sidebar: #14151c;
  --text-main: #e2e8f0;
  --text-muted: #8190a5;
  --accent-color: #6366f1;
  --accent-hover: #7c7ff5;
  --hover-bg: rgba(99, 102, 241, 0.16);
  --border-glow: rgba(255, 255, 255, 0.08);
  --danger: #f87171;
  --ok: #4ade80;
  --sat: env(safe-area-inset-top, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  padding: var(--sat) var(--sar) 0 var(--sal);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Barra de herramientas ---------- */
.toolbar {
  display: flex;
  align-items: center;
  background-color: var(--bg-toolbar);
  height: 48px;
  flex: none;
  padding: 0 12px;
  border-bottom: 1px solid var(--border-glow);
  position: relative;
  z-index: 100;
}

.toolbar-brand {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: -0.02em;
  margin: 0 16px 0 4px;
}

.toolbar-nav { display: flex; gap: 4px; }
.menu-container { position: relative; }

.menu-trigger {
  background: none;
  border: none;
  padding: 0 14px;
  min-height: 40px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 6px;
}

.menu-trigger:hover,
.menu-trigger.active { background-color: rgba(255, 255, 255, 0.06); }

/* Menús: solo transform + opacity (compositor/GPU) */
.dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  background-color: var(--bg-menu);
  list-style: none;
  margin: 0;
  padding: 6px;
  min-width: 220px;
  border-radius: 8px;
  border: 1px solid var(--border-glow);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.5);
  opacity: 0;
  transform: scaleY(0.9) translateY(-8px);
  transform-origin: top left;
  visibility: hidden;
  pointer-events: none;
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.15s ease-out, visibility 0s linear 0.2s;
}

.dropdown-menu.show {
  opacity: 1;
  transform: scaleY(1) translateY(0);
  visibility: visible;
  pointer-events: auto;
  transition-delay: 0s;
}

.dropdown-menu li button {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  background: none;
  border: none;
  border-radius: 4px;
  text-align: left;
  font-size: 13px;
  cursor: pointer;
}

.dropdown-menu li button:hover,
.dropdown-menu li button:focus-visible { background-color: var(--hover-bg); color: #fff; outline: none; }
.dropdown-menu li button.danger { color: var(--danger); }
.dropdown-menu .sep { height: 1px; margin: 4px 6px; background: var(--border-glow); }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-main);
  font-size: 20px;
  line-height: 1;
  min-width: 40px;
  min-height: 40px;
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn:hover { background-color: rgba(255, 255, 255, 0.06); }

/* ---------- Contenedor y editor ---------- */
.app-container {
  display: flex;
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.editor-area {
  flex: 1;
  min-width: 0;
  display: flex;
  background-color: var(--bg-main);
}

#code-editor {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: #f8fafc;
  font-family: 'Fira Code', ui-monospace, 'Cascadia Code', Menlo, Consolas, 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.6;
  padding: 16px 20px;
  resize: none;
  outline: none;
  white-space: pre;
  overflow: auto;
  tab-size: 2;
  caret-color: var(--accent-hover);
}

/* ---------- Paneles laterales ---------- */
.side-panel {
  position: absolute;
  top: 0;
  bottom: 0;
  background-color: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  z-index: 90;
  visibility: hidden;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), visibility 0s linear 0.25s;
}
.side-panel.open { transform: translateX(0); visibility: visible; transition-delay: 0s; }

.ai-sidebar {
  right: 0;
  width: min(380px, 100vw);
  border-left: 1px solid var(--border-glow);
  transform: translateX(100%);
}

.files-drawer {
  left: 0;
  width: min(320px, 86vw);
  border-right: 1px solid var(--border-glow);
  transform: translateX(-100%);
}

@media (min-width: 900px) {
  .app-container.ai-open .editor-area { padding-right: 380px; }
}

.scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 80;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease-out, visibility 0s linear 0.2s;
}
.scrim.show { opacity: 1; visibility: visible; transition-delay: 0s; }

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 8px 8px 16px;
  border-bottom: 1px solid var(--border-glow);
}
.sidebar-header h3 { margin: 0; font-size: 14px; font-weight: 600; }
.header-actions { display: flex; align-items: center; gap: 4px; }

/* ---------- Botones genéricos ---------- */
.btn {
  background: var(--accent-color);
  color: #fff;
  border: none;
  border-radius: 6px;
  min-height: 40px;
  padding: 0 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text-main); }
.btn-ghost:hover { background: rgba(255, 255, 255, 0.07); }

/* ---------- Archivos ---------- */
.drawer-actions { padding: 12px 16px; border-bottom: 1px solid var(--border-glow); }
.drawer-actions .btn { width: 100%; }

.files-list { list-style: none; margin: 0; padding: 8px; overflow-y: auto; flex: 1; }
.file-item { display: flex; align-items: center; border-radius: 6px; }
.file-item.current { background: var(--hover-bg); }
.file-open {
  flex: 1;
  min-width: 0;
  min-height: 48px;
  padding: 6px 10px;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  border-radius: 6px;
}
.file-open:hover { background: rgba(255, 255, 255, 0.05); }
.file-name { display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta { display: block; font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.file-del { font-size: 15px; color: var(--text-muted); }
.file-del:hover { color: var(--danger); }
.files-empty { padding: 16px; color: var(--text-muted); font-size: 13px; text-align: center; }

/* ---------- Chat ---------- */
.chat-history {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overscroll-behavior: contain;
}

.ai-message {
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.45;
  max-width: 92%;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.ai-message.user { background: var(--hover-bg); align-self: flex-end; }
.ai-message.response { background: rgba(255, 255, 255, 0.04); align-self: flex-start; border: 1px solid var(--border-glow); max-width: 100%; width: 100%; }
.ai-message.system { color: var(--text-muted); text-align: center; max-width: 100%; align-self: center; }
.ai-message.error { color: var(--danger); border: 1px solid rgba(248, 113, 113, 0.35); align-self: stretch; max-width: 100%; }
.ai-message p { margin: 0 0 8px; }
.ai-message p:last-child { margin-bottom: 0; }

.code-block { margin: 8px 0; border: 1px solid var(--border-glow); border-radius: 6px; background: var(--bg-main); overflow: hidden; }
.code-actions { display: flex; justify-content: flex-end; gap: 2px; padding: 2px; border-bottom: 1px solid var(--border-glow); }
.code-actions button {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 12px;
  min-height: 32px;
  padding: 0 10px;
  border-radius: 4px;
  cursor: pointer;
}
.code-actions button:hover { color: #fff; background: rgba(255, 255, 255, 0.07); }
.code-block pre {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  font-family: ui-monospace, Menlo, Consolas, 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre;
}

.chat-input-container {
  padding: 10px 16px calc(12px + var(--sab));
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-top: 1px solid var(--border-glow);
}
.include-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-muted); min-height: 28px; }
.include-row input { width: 18px; height: 18px; accent-color: var(--accent-color); }
.input-row { display: flex; gap: 8px; }

#ai-input,
.dlg-body input[type="text"],
.dlg-body input[type="password"] {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border-glow);
  border-radius: 6px;
  padding: 0 12px;
  color: #fff;
  font-size: 14px;
  outline: none;
}
#ai-input:focus,
.dlg-body input:focus { border-color: var(--accent-color); }

#send-ai-btn {
  background: var(--accent-color);
  color: #fff;
  border: none;
  border-radius: 6px;
  min-height: 40px;
  padding: 0 16px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 500;
}
#send-ai-btn.stop { background: #b4424a; }

/* ---------- Barra de estado ---------- */
.statusbar {
  flex: none;
  display: flex;
  gap: 16px;
  align-items: center;
  padding: 4px 12px calc(4px + var(--sab));
  background: var(--bg-toolbar);
  border-top: 1px solid var(--border-glow);
  font-size: 11px;
  color: var(--text-muted);
}
.statusbar #st-file { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-main); }

/* ---------- Diálogos ---------- */
dialog {
  background: var(--bg-menu);
  color: var(--text-main);
  border: 1px solid var(--border-glow);
  border-radius: 12px;
  padding: 0;
  width: min(440px, 92vw);
  max-height: 90dvh;
  overflow: auto;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.65); }
.dlg-body { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.dlg-body h3 { margin: 0; font-size: 15px; }
.dlg-note { margin: 0; font-size: 12px; color: var(--text-muted); line-height: 1.5; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--text-muted); }
.field-row { display: flex; gap: 8px; align-items: center; }
.dlg-msg { margin: 0; min-height: 16px; font-size: 12px; color: var(--ok); }
.dlg-msg.error { color: var(--danger); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (prefers-reduced-motion: reduce) {
  .dropdown-menu, .side-panel, .scrim { transition-duration: 0.01s; }
}

/* ---------- Pestaña y punto de "sin guardar" ---------- */
.tabbar { flex: none; display: flex; height: 36px; background: var(--bg-toolbar); border-bottom: 1px solid var(--border-glow); }
.tab {
  display: flex;
  align-items: center;
  gap: 2px;
  max-width: 80%;
  padding: 0 4px 0 14px;
  background: var(--bg-main);
  border-top: 2px solid var(--accent-color);
  font-size: 13px;
}
#tab-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-dot {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.tab-dot:hover { background: rgba(255, 255, 255, 0.08); }
.tab-dot span { width: 9px; height: 9px; border-radius: 50%; background: #fff; }
.file-name .dot { display: inline-block; width: 7px; height: 7px; margin-left: 6px; border-radius: 50%; background: #fff; vertical-align: middle; }

/* ---------- Selector de modelo ---------- */
.model-row { display: flex; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border-glow); align-items: center; }
select {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  border: 1px solid var(--border-glow);
  border-radius: 6px;
  padding: 0 8px;
  font-size: 13px;
  outline: none;
}
select:focus { border-color: var(--accent-color); }
.field .hint { font-size: 11px; line-height: 1.4; }
