:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --ink: #26211b;
  --muted: #6b6258;
  --line: #e4ddd2;
  --accent: #8a4f14;
  --accent-ink: #ffffff;
  --accent-soft: #f6e9d8;
  --ok: #1f7a4d;
  --ok-soft: #dff3e8;
  --warn: #8a5a00;
  --warn-soft: #fdf0cf;
  --err: #b3261e;
  --err-soft: #fbe3e1;
  --info: #1d5fa8;
  --info-soft: #e1ecf9;
  --bubble-user: #8a4f14;
  --bubble-user-ink: #ffffff;
  --bubble-bot: #f0ebe3;
  --shadow: 0 1px 2px rgba(38, 33, 27, 0.06), 0 4px 16px rgba(38, 33, 27, 0.05);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17140f;
    --surface: #211d17;
    --surface-2: #2a251d;
    --ink: #f1ebe1;
    --muted: #b1a696;
    --line: #3a3328;
    --accent: #e0a45f;
    --accent-ink: #1c1409;
    --accent-soft: #3a2c18;
    --ok: #5fcf97;
    --ok-soft: #1b3326;
    --warn: #f0c35c;
    --warn-soft: #3a2f12;
    --err: #ff8a80;
    --err-soft: #3d1d1b;
    --info: #86b8f2;
    --info-soft: #172a40;
    --bubble-user: #e0a45f;
    --bubble-user-ink: #1c1409;
    --bubble-bot: #2f291f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--ink);
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.2;
}
h2 {
  font-size: 1.15rem;
}
h3 {
  font-size: 1rem;
}
p {
  margin: 0;
}
a {
  color: var(--accent);
}
.muted {
  color: var(--muted);
}
.small {
  font-size: 0.85rem;
}
.hidden,
[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Layout de la app ---------- */
.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 14px;
  font-weight: 700;
  font-size: 1.05rem;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 800;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
}
.nav-item:hover {
  background: var(--surface-2);
  color: var(--ink);
}
.nav-item[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 650;
}
.nav-item svg {
  flex: none;
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 8px;
  padding: 12px 8px 0;
  border-top: 1px solid var(--line);
}
.who {
  display: grid;
  gap: 2px;
  word-break: break-all;
}

.content {
  min-width: 0;
  padding: 28px clamp(16px, 3vw, 40px) 48px;
}
.view {
  max-width: 1100px;
  margin: 0 auto;
  display: grid;
  gap: 18px;
}
.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}
.page-head h1 {
  font-size: 1.5rem;
}
.page-head p {
  margin-top: 4px;
  color: var(--muted);
}
.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

/* ---------- Componentes ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
  align-content: start;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 18px;
  align-items: start;
}
.fields {
  display: grid;
  gap: 12px;
}
.fields.two {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

label {
  display: grid;
  gap: 5px;
  font-weight: 550;
  font-size: 0.9rem;
}
label > .hint,
.hint {
  font-weight: 400;
  font-size: 0.82rem;
  color: var(--muted);
}
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

input,
select,
textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
}
input[type="checkbox"] {
  width: auto;
}
input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea {
  resize: vertical;
  min-height: 70px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border: 1px solid var(--accent);
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover {
  filter: brightness(1.06);
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn.secondary {
  background: transparent;
  color: var(--accent);
}
.btn.ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--ink);
}
.btn.danger {
  background: transparent;
  border-color: var(--err);
  color: var(--err);
}
.btn.sm {
  padding: 5px 10px;
  font-size: 0.85rem;
}

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge.ok {
  background: var(--ok-soft);
  border-color: transparent;
  color: var(--ok);
}
.badge.warn {
  background: var(--warn-soft);
  border-color: transparent;
  color: var(--warn);
}
.badge.err {
  background: var(--err-soft);
  border-color: transparent;
  color: var(--err);
}
.badge.info {
  background: var(--info-soft);
  border-color: transparent;
  color: var(--info);
}

.notice {
  padding: 10px 12px;
  border-radius: 9px;
  background: var(--info-soft);
  color: var(--info);
  font-size: 0.9rem;
}
.notice.err {
  background: var(--err-soft);
  color: var(--err);
}
.notice.ok {
  background: var(--ok-soft);
  color: var(--ok);
}

.empty {
  padding: 28px 12px;
  text-align: center;
  color: var(--muted);
}

/* ---------- Tablas ---------- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
th,
td {
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--line);
}
th {
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
tbody tr:last-child td {
  border-bottom: 0;
}
tbody tr:hover {
  background: var(--surface-2);
}
td.nowrap {
  white-space: nowrap;
}
td .row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}
details > summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 0.85rem;
}
pre {
  margin: 6px 0 0;
  padding: 10px;
  max-height: 260px;
  overflow: auto;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-size: 0.8rem;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---------- KPIs ---------- */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
}
.kpi {
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.kpi .value {
  font-size: 2rem;
  font-weight: 750;
  line-height: 1.1;
  color: var(--accent);
}
.kpi .label {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.85rem;
}

/* ---------- Subida de archivos ---------- */
.dropzone {
  display: grid;
  place-items: center;
  gap: 4px;
  padding: 28px 16px;
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover,
.dropzone.drag {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone strong {
  color: var(--accent);
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* ---------- Chat de prueba ---------- */
.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: min(72vh, 720px);
  min-height: 480px;
  padding: 0;
  gap: 0;
  overflow: hidden;
}
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.chat-title {
  display: flex;
  align-items: center;
  gap: 10px;
}
.avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 800;
}
.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 5px;
  border-radius: 50%;
  background: var(--ok);
}
.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px;
  overflow-y: auto;
  scroll-behavior: smooth;
  background: var(--surface-2);
}
.msg {
  display: flex;
  flex-direction: column;
  max-width: min(82%, 640px);
  gap: 4px;
  animation: pop 0.18s ease-out;
}
.msg.user {
  align-self: flex-end;
  align-items: flex-end;
}
.msg.bot {
  align-self: flex-start;
}
.bubble {
  padding: 10px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .bubble {
  background: var(--bubble-user);
  color: var(--bubble-user-ink);
  border-bottom-right-radius: 4px;
}
.msg.bot .bubble {
  background: var(--bubble-bot);
  border-bottom-left-radius: 4px;
}
.msg.error .bubble {
  background: var(--err-soft);
  color: var(--err);
}
.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--muted);
}
.trace {
  font-size: 0.82rem;
}
.trace-call {
  margin-top: 6px;
}
.typing {
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
}
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) {
  animation-delay: 0.15s;
}
.typing span:nth-child(3) {
  animation-delay: 0.3s;
}
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: auto 0;
  justify-content: center;
}
.suggestions .intro {
  width: 100%;
  text-align: center;
  color: var(--muted);
  margin-bottom: 4px;
}
.chip {
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 0.88rem;
  cursor: pointer;
}
.chip:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}
.composer textarea {
  min-height: 44px;
  max-height: 160px;
  resize: none;
  border-radius: 22px;
  padding: 10px 16px;
}

@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0.25;
  }
  40% {
    opacity: 1;
  }
}
@media (prefers-reduced-motion: reduce) {
  .msg,
  .typing span {
    animation: none;
  }
  .messages {
    scroll-behavior: auto;
  }
}

/* ---------- Toasts ---------- */
.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 50;
  display: grid;
  gap: 8px;
  max-width: min(92vw, 380px);
}
.toast {
  padding: 11px 14px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--bg);
  box-shadow: var(--shadow);
  animation: pop 0.18s ease-out;
}
.toast.ok {
  background: var(--ok);
  color: #fff;
}
.toast.err {
  background: var(--err);
  color: #fff;
}

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  width: min(100%, 400px);
}
.login-card .brand {
  padding: 0 0 6px;
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: static;
    height: auto;
    padding: 12px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .brand {
    padding-bottom: 6px;
  }
  .nav {
    flex-direction: row;
    overflow-x: auto;
    padding-bottom: 4px;
  }
  .nav-item {
    width: auto;
    white-space: nowrap;
    padding: 8px 12px;
  }
  .sidebar-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 4px;
    padding: 8px 4px 0;
  }
  .content {
    padding-top: 18px;
  }
  .msg {
    max-width: 92%;
  }
  .chat {
    height: 78vh;
  }
}
