:root {
  --bg: #f6f3ef;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --text: #1f1b17;
  --muted: #6f665d;
  --border: #e6dfd6;
  --accent: #7a2e3a;
  --accent-soft: #f3e6e8;
  --ok: #2f6b45;
  --ok-soft: #e5f1e9;
  --warn: #8a5a12;
  --warn-soft: #f8eedc;
  --danger: #a12d2d;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(31, 27, 23, 0.06), 0 4px 16px rgba(31, 27, 23, 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161412;
    --surface: #201d1a;
    --surface-2: #1b1917;
    --text: #f1ece6;
    --muted: #a69c91;
    --border: #35302b;
    --accent: #d88a96;
    --accent-soft: #3a252a;
    --ok: #7fc59a;
    --ok-soft: #1f3327;
    --warn: #e2b46a;
    --warn-soft: #3a2f1c;
    --danger: #ef8a8a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

body.center {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }

h1, h2, h3 { line-height: 1.25; margin: 0 0 8px; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
p { margin: 0 0 12px; }
small, .muted { color: var(--muted); }
code { font-size: 13px; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card.narrow { max-width: 420px; width: 100%; text-align: center; }

/* ── Layout ───────────────────────────────────────────── */
header.top {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.top-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand { font-weight: 650; letter-spacing: -0.01em; }
.brand span { color: var(--accent); }
.spacer { flex: 1; }

nav.tabs {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 8px;
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}
nav.tabs button {
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 10px 12px;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}
nav.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
nav.tabs button { display: inline-flex; align-items: center; gap: 6px; }
.ico { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.lbl-short { display: none; }

main.content { max-width: 1100px; margin: 0 auto; padding: 20px 16px 48px; }
section[hidden] { display: none !important; }
.section-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.section-head p { margin: 0; }

.grid-2 { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 16px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

/* ── Controles ────────────────────────────────────────── */
button, .btn {
  font: inherit;
  cursor: pointer;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 550; }
button.primary:hover:not(:disabled) { opacity: 0.92; border-color: var(--accent); }
button.ghost { border-color: transparent; background: none; color: var(--muted); }
button.danger { color: var(--danger); }
button.small { padding: 4px 10px; font-size: 13px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

label { display: block; font-weight: 550; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.field small { display: block; font-weight: 400; }

input[type="text"], input[type="number"], select, textarea {
  font: inherit;
  width: 100%;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
}
textarea { resize: vertical; }
textarea.prompt { min-height: 420px; font-size: 14px; line-height: 1.55; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }

.switch { display: flex; align-items: center; gap: 10px; font-weight: 550; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

.notice { padding: 10px 12px; border-radius: 10px; background: var(--warn-soft); color: var(--warn); margin-bottom: 14px; }
.notice.info { background: var(--accent-soft); color: var(--accent); }

/* ── Versiones ────────────────────────────────────────── */
.versions { list-style: none; margin: 0; padding: 0; max-height: 560px; overflow: auto; }
.versions li { padding: 10px 0; border-bottom: 1px solid var(--border); }
.versions li:last-child { border-bottom: 0; }
.versions .meta { font-size: 13px; color: var(--muted); }

/* ── Probador / conversaciones ───────────────────────── */
.chat { display: flex; flex-direction: column; gap: 10px; min-height: 280px; max-height: 60vh; overflow: auto; padding: 4px; }
.bubble { max-width: 85%; padding: 9px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.bubble.usuario { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.bubble.bot { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.propuesta { align-self: flex-start; background: var(--warn-soft); color: var(--text); border: 1px dashed var(--warn); }
.bubble.resultado { align-self: center; background: var(--ok-soft); color: var(--ok); font-size: 13px; }
.bubble .when { display: block; font-size: 11px; opacity: 0.7; margin-top: 4px; }
.bubble img { max-width: 120px; border-radius: 8px; margin: 4px 4px 0 0; }
details.trace { font-size: 12px; color: var(--muted); align-self: flex-start; max-width: 85%; }
details.trace pre { white-space: pre-wrap; word-break: break-all; margin: 6px 0 0; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.thumbs img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
#test-chat:empty::before { content: 'Escribe un mensaje o adjunta fotos para empezar la prueba.'; color: var(--muted); margin: auto; text-align: center; padding: 24px 8px; }
.typing { color: var(--muted); font-style: italic; align-self: flex-start; }

.chat-list { list-style: none; margin: 0; padding: 0; }
.chat-list button { width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border); border-radius: 0; padding: 10px 4px; background: none; }
.chat-list button[aria-current="true"] { background: var(--accent-soft); }

/* ── Consumo ──────────────────────────────────────────── */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat .value { font-size: 26px; font-weight: 650; letter-spacing: -0.02em; }
.stat .label { color: var(--muted); font-size: 13px; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 160px; padding-top: 8px; }
.bar { flex: 1; display: flex; flex-direction: column-reverse; min-width: 0; height: 100%; }
.bar span { display: block; width: 100%; }
.bar .real { background: var(--accent); border-radius: 3px 3px 0 0; }
.bar .prueba { background: var(--warn); opacity: 0.6; }
.bars-axis { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 4px; }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 13px; color: var(--muted); font-weight: 550; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 50;
  max-width: calc(100% - 32px);
}
.toast.error { background: var(--danger); color: #fff; }

#users-body input[type="text"] { min-width: 130px; }

/* ── Composer del probador ───────────────────────────── */
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; }
.composer-actions { display: flex; gap: 8px; }
.composer-actions .btn { margin: 0; font-weight: 400; display: inline-flex; align-items: center; }

/* ── Conversaciones ──────────────────────────────────── */
.grid-history { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 2.2fr); gap: 16px; }
.history-detail-head { margin-bottom: 8px; }
#history-messages { max-height: 70vh; }
.history-back { display: none; }
#history-title { display: none; }

/* ── Móvil ───────────────────────────────────────────── */
@media (max-width: 720px) {
  :root { --nav-h: 60px; }

  body { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }

  .top-inner { padding: 10px 14px; }
  .brand-sub { display: none; }

  /* Pestañas → barra fija inferior, al alcance del pulgar */
  nav.tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    max-width: none;
    padding: 0 0 env(safe-area-inset-bottom);
    gap: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
  }
  nav.tabs button {
    flex: 1 1 0;
    min-width: 0;
    height: var(--nav-h);
    flex-direction: column;
    justify-content: center;
    gap: 3px;
    padding: 6px 2px;
    border-bottom: 0;
    border-top: 2px solid transparent;
    font-size: 11px;
  }
  nav.tabs button[aria-selected="true"] { color: var(--accent); border-top-color: var(--accent); }
  .ico { width: 22px; height: 22px; }
  .lbl-long { display: none; }
  .lbl-short { display: block; }

  main.content { padding: 14px 12px 24px; }
  .section-head { margin-bottom: 12px; }
  .section-head h1 { font-size: 20px; }
  .card { padding: 14px; border-radius: 14px; }

  /* Tamaños táctiles y 16 px en campos (iOS amplía la página con menos) */
  input[type="text"], input[type="number"], select, textarea { font-size: 16px; padding: 10px 12px; }
  button, .btn { min-height: 44px; }
  button.small { min-height: 40px; padding: 6px 12px; }
  .switch input, td input[type="checkbox"] { width: 22px; height: 22px; }
  .switch { min-height: 40px; }

  /* Botones de acción apilados, el principal arriba */
  .actions { flex-direction: column-reverse; align-items: stretch; }
  .actions button { width: 100%; }
  #settings-form .row.end button, #user-form .row.end button { width: 100%; }

  textarea.prompt { min-height: 55vh; }

  /* Probador: campo a lo ancho, botones debajo */
  .composer {
    flex-direction: column;
    align-items: stretch;
    position: sticky;
    bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
    background: var(--surface);
    padding: 8px 0 4px;
    margin: 0 -2px;
    z-index: 5;
  }
  .composer-actions .btn { flex: none; justify-content: center; }
  .composer-actions button { flex: 1; }
  .chat { max-height: none; min-height: 22vh; }
  .bubble { max-width: 92%; }

  /* Consumo: dos columnas de cifras */
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat .value { font-size: 22px; }
  .stats > :last-child:nth-child(odd) { grid-column: span 2; }
  .bars { height: 130px; gap: 2px; }

  /* Conversaciones: lista y, al tocar una, la conversación con "volver" */
  .grid-history { grid-template-columns: 1fr; }
  .grid-history .history-detail { display: none; }
  .grid-history.show-chat .history-list { display: none; }
  .grid-history.show-chat .history-detail { display: block; }
  #history-messages { max-height: none; overflow: visible; }
  .history-back { display: inline-flex; padding-left: 0; }
  #history-title { display: block; width: 100%; order: 3; }
  .chat-list button { padding: 12px 4px; min-height: 56px; }

  /* Usuarios: cada fila de la tabla pasa a ser una tarjeta */
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td { display: block; width: 100%; }
  table.responsive tr { padding: 12px 0; border-bottom: 1px solid var(--border); }
  table.responsive tr:last-child { border-bottom: 0; }
  table.responsive td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 0;
    border: 0;
    text-align: left;
  }
  table.responsive td::before { content: attr(data-label); color: var(--muted); font-size: 14px; flex: none; }
  table.responsive td[data-label="Nombre"] { display: block; }
  table.responsive td[data-label="Nombre"]::before { display: block; margin-bottom: 4px; }
  table.responsive td.actions-cell:empty { display: none; }
  table.responsive td.actions-cell::before { display: none; }
  table.responsive td.actions-cell button { width: 100%; }
  #users-body input[type="text"] { min-width: 0; }

  .toast { bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); }
}
