:root {
  --bg: #09090b;
  --surface: #111114;
  --surface-2: #18181b;
  --input-bg: #0c0c0f;
  --border: #27272a;
  --border-strong: #3f3f46;
  --text: #fafafa;
  --muted: #a1a1aa;
  --primary: #fafafa;
  --primary-text: #09090b;
  --primary-hover: #e4e4e7;
  --emerald: #10b981;
  --emerald-hover: #059669;
  --sky: #38bdf8;
  --warn: #f59e0b;
  --err: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
}

* {
  box-sizing: border-box;
}

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

code, pre, .mono {
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.hidden {
  display: none !important;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }
.full-width { width: 100%; }

/* === Login === */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 26px;
}

.login-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 22px;
}

.login-head h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.02em;
}

.login-head p {
  margin: 2px 0 0;
  font-size: 12px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--emerald);
  color: #09090b;
  flex-shrink: 0;
}

.brand-mark.small {
  width: 30px;
  height: 30px;
  border-radius: 8px;
}

.error-banner {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.35);
  color: #fca5a5;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

/* === Layout Principal === */
.app-layout {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 30;
}

/* === Sidebar === */
.sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 40;
}

.sidebar.collapsed {
  display: none;
}

.sidebar-top {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.02em;
  flex: 1;
}

.search-wrap input {
  padding: 7px 10px;
  font-size: 13px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.conv-item {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 11px;
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 5px;
  transition: background-color 0.12s ease, border-color 0.12s ease;
}

.conv-item:hover {
  background: var(--surface-2);
}

.conv-item.active {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.conv-title {
  font-size: 13px;
  font-weight: 600;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.35;
}

.conv-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
}

.conv-tags {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}

.source-tag {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 9.5px;
  font-weight: 700;
  text-transform: uppercase;
  flex-shrink: 0;
}

.source-tag.cli {
  color: var(--emerald);
  border-color: rgba(16, 185, 129, 0.35);
}

.proj-tag {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--sky);
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 10px;
  font-weight: 600;
  max-width: 145px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.worker-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--warn);
}

.dot.ok { background: var(--emerald); }
.dot.err { background: var(--err); }

.footer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* === Main Chat Pane === */
.main-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg);
}

.chat-header {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.header-meta {
  min-width: 0;
}

.header-meta h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-sub {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.count-pill {
  background: var(--emerald);
  color: #09090b;
  font-size: 11px;
  font-weight: 700;
  padding: 0 6px;
  border-radius: 999px;
}

/* Nueva Conversación */
.new-chat-view {
  flex: 1;
  overflow-y: auto;
  display: grid;
  place-items: center;
  padding: 24px 18px;
}

.new-chat-card {
  width: 100%;
  max-width: 640px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}

.new-chat-card h3 {
  margin: 0 0 4px;
  font-size: 17px;
}

.new-chat-card p {
  margin: 0 0 18px;
  font-size: 13px;
}

/* Mensajes (estilo limpio Antigravity) */
.chat-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 24px 28px;
}

.messages-inner {
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.msg {
  width: 100%;
  word-break: break-word;
}

.msg.user {
  align-self: flex-end;
  width: fit-content;
  max-width: min(680px, 90%);
  padding: 11px 15px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.msg.user.failed {
  border-color: rgba(239, 68, 68, 0.55);
}

.msg.assistant {
  align-self: stretch;
  padding: 4px 2px;
  background: transparent;
  border: none;
}

.msg-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
  font-size: 11px;
  color: var(--muted);
}

.msg-role {
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.msg.user .msg-role { color: var(--sky); }
.msg.assistant .msg-role { color: var(--emerald); }
.msg.failed .msg-time { color: #f87171; font-weight: 600; }

.msg-body {
  font-size: 14px;
  line-height: 1.65;
  color: #e4e4e7;
}

.msg-body h1,
.msg-body h2,
.msg-body h3 {
  color: var(--text);
  margin: 16px 0 8px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.msg-body h1 { font-size: 18px; }
.msg-body h2 { font-size: 16px; }
.msg-body h3 { font-size: 14.5px; }

.msg-body p { margin: 0 0 10px; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { margin: 6px 0 12px; padding-left: 22px; }
.msg-body li { margin: 5px 0; }
.msg-body li > ul, .msg-body li > ol { margin: 4px 0; }

.msg-body a {
  color: var(--sky);
  text-decoration: none;
}

.msg-body a:hover { text-decoration: underline; }

.msg-body code {
  background: #18181b;
  border: 1px solid #27272a;
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 0.87em;
  color: #e4e4e7;
}

.msg-body pre {
  background: #0c0c0f;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  overflow-x: auto;
  margin: 10px 0 12px;
}

.msg-body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 12.5px;
  line-height: 1.5;
}

.file-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 7px 1px 5px;
  border-radius: 6px;
  background: #18181b;
  border: 1px solid #27272a;
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 12px;
  color: #f4f4f5;
  vertical-align: baseline;
}

.file-ext {
  font-size: 9.5px;
  font-weight: 800;
  letter-spacing: 0.03em;
  padding: 1px 4px;
  border-radius: 4px;
  background: #27272a;
  color: #a1a1aa;
}

.file-ext.ext-ts, .file-ext.ext-tsx { color: #38bdf8; background: rgba(56, 189, 248, 0.14); }
.file-ext.ext-js, .file-ext.ext-jsx, .file-ext.ext-mjs { color: #facc15; background: rgba(250, 204, 21, 0.14); }
.file-ext.ext-json, .file-ext.ext-yml, .file-ext.ext-yaml { color: #fb923c; background: rgba(251, 146, 60, 0.14); }
.file-ext.ext-css, .file-ext.ext-html { color: #c084fc; background: rgba(192, 132, 252, 0.14); }
.file-ext.ext-md { color: #34d399; background: rgba(52, 211, 153, 0.14); }

/* Composer */
.composer {
  padding: 12px 18px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.composer-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.mini-select {
  width: auto;
  max-width: 240px;
  padding: 4px 8px;
  font-size: 12px;
}

.composer-row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.composer-row textarea {
  flex: 1;
  resize: vertical;
  min-height: 44px;
  max-height: 160px;
}

/* === Panel Git Derecho === */
.git-panel {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 40;
}

.git-panel.collapsed {
  display: none;
}

.git-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.git-head h3 {
  margin: 0;
  font-size: 15px;
}

.git-head p {
  margin: 2px 0 0;
}

.git-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.git-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}

.pill.ok { color: #4ade80; border-color: rgba(34, 197, 94, 0.35); }
.pill.warn { color: #fbbf24; border-color: rgba(245, 158, 11, 0.35); }
.pill.sky { color: var(--sky); border-color: rgba(56, 189, 248, 0.35); }

.git-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.git-files {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  max-height: 185px;
  overflow-y: auto;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex-shrink: 0;
}

.git-file-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 12px;
  padding: 3px 6px;
  border-radius: 5px;
}

.git-file-row:hover { background: var(--surface-2); }

.git-code {
  font-weight: 700;
  width: 22px;
  text-align: center;
  flex-shrink: 0;
  font-size: 11px;
}

.git-code.M { color: var(--warn); }
.git-code.A, .git-code.untracked { color: var(--emerald); }
.git-code.D { color: var(--err); }

.git-file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
}

.check-row input {
  width: auto;
  accent-color: var(--emerald);
}

.git-btn-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.git-output {
  margin: 0;
  background: #070709;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 12px;
  line-height: 1.5;
  color: #e4e4e7;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 130px;
  max-height: 280px;
  overflow-y: auto;
  flex-shrink: 0;
}

.btn-link {
  background: none;
  border: none;
  color: var(--sky);
  cursor: pointer;
  padding: 0;
  font-size: 11px;
  font-weight: 600;
}

.btn-link:hover { text-decoration: underline; }

.git-commits {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.commit-row {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.commit-subject {
  font-size: 12px;
  font-weight: 600;
}

.commit-meta {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

/* === Controles y formularios === */
.form-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
}

input[type="text"],
input[type="password"],
textarea,
select {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 9px 11px;
  font: inherit;
}

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--border-strong);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-xs { padding: 4px 8px; font-size: 11px; }

.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-emerald { background: var(--emerald); color: #09090b; }
.btn-emerald:hover:not(:disabled) { background: var(--emerald-hover); color: #fafafa; }

.btn-ghost { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.btn-ghost:hover:not(:disabled) { border-color: var(--border-strong); background: #27272a; }

.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 14px;
}

.icon-btn:hover { border-color: var(--border-strong); }

.row-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.inline-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.empty-hint {
  padding: 14px 10px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.toast-stack {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
}

.toast.ok { border-color: rgba(34, 197, 94, 0.45); color: #86efac; }
.toast.err { border-color: rgba(239, 68, 68, 0.45); color: #fca5a5; }

.mobile-only { display: none; }

/* === Responsive Tablet / Mobile === */
@media (max-width: 1080px) {
  .git-panel {
    position: fixed;
    right: 0;
    top: 0;
    bottom: 0;
    width: min(360px, 92vw);
    box-shadow: -12px 0 30px rgba(0, 0, 0, 0.55);
  }
}

@media (max-width: 820px) {
  .mobile-only { display: grid; }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(310px, 86vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .composer-top {
    flex-direction: column;
    align-items: stretch;
  }

  .mini-select {
    max-width: 100%;
  }
}
