:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --border: #e2e1dc;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --code-bg: #f0efea;
  --user-bg: #e9eefb;
  --ok: #1f7a4a;
  --ok-bg: #e5f4ec;
  --warn: #8a5a00;
  --warn-bg: #fdf3dc;
  --danger: #b3261e;
  --danger-bg: #fbe9e7;
  --radius: 10px;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --text: #ecebe6;
    --muted: #9d9c95;
    --border: #34332f;
    --accent: #7b9cff;
    --accent-text: #0f1830;
    --code-bg: #2a2926;
    --user-bg: #232a3d;
    --ok: #6fcf97;
    --ok-bg: #18301f;
    --warn: #f2c26b;
    --warn-bg: #352a12;
    --danger: #ff8a80;
    --danger-bg: #3a1c19;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1.5rem 0 .5rem; }
code { font-family: var(--mono); font-size: .9em; }
:not(pre) > code { background: var(--code-bg); padding: .1em .35em; border-radius: 5px; }
pre {
  background: var(--code-bg);
  padding: .75rem 1rem;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
  margin: .5rem 0;
}
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* Controles */
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .55rem .7rem;
  width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label { display: grid; gap: .3rem; font-weight: 500; }
label.inline { display: inline-flex; align-items: center; gap: .5rem; }
label.inline select { width: auto; }
button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .5rem .9rem;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { color: var(--danger); }
button.link { background: none; border: 0; padding: 0; color: var(--muted); }
button.link:hover { color: var(--text); }
button:disabled { opacity: .5; cursor: not-allowed; }

/* Estructura */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .6rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); }
.brand span, h1 .muted { font-weight: 400; color: var(--muted); }
.top nav { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.top nav a { color: var(--muted); text-decoration: none; }
.top nav a[aria-current="page"], .top nav a:hover { color: var(--text); }
.top nav form { margin: 0; }

.page { max-width: 760px; margin: 0 auto; padding: 1.5rem 16px 3rem; }
.page.wide { max-width: 1100px; }
.center { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-top: 1.25rem;
}
.card.narrow { width: 100%; max-width: 380px; display: grid; gap: 1rem; margin: 0; }
.card.narrow p { margin: 0; }
.stack { display: grid; gap: 1rem; max-width: 420px; }
.row { display: flex; gap: .5rem; flex-wrap: wrap; }
.row form { margin: 0; }
.alert { padding: .6rem .8rem; border-radius: 8px; margin: 1rem 0 0; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.error { background: var(--danger-bg); color: var(--danger); }
.token code { word-break: break-all; white-space: pre-wrap; font-size: 14px; }

/* Tabla admin */
.table-wrap { overflow-x: auto; margin-top: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; }
tr:last-child td { border-bottom: 0; }
td.actions { display: flex; gap: .4rem; }
td.actions form { margin: 0; }
td.actions button { padding: .3rem .6rem; font-size: 13px; }
.tag { font-size: 12px; padding: .1rem .5rem; border-radius: 99px; background: var(--ok-bg); color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }

/* Chat */
body.chat-body { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; max-width: 900px; width: 100%; margin: 0 auto; }
.toolbar { display: flex; align-items: center; gap: .75rem; padding: .75rem 16px; flex-wrap: wrap; }
#estado { font-size: .85rem; margin-left: auto; }
#mensajes { flex: 1; min-height: 0; overflow-y: auto; padding: 0 16px 1rem; }
.empty { text-align: center; margin-top: 15vh; }
.msg { margin: 1rem 0; }
.msg .who { font-size: .8rem; color: var(--muted); margin-bottom: .2rem; }
.msg.user .body {
  background: var(--user-bg);
  padding: .6rem .85rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.assistant .body p { margin: .4rem 0; }
.msg.pending .body:empty::after { content: "pensando…"; color: var(--muted); }
.msg .error { color: var(--danger); }
.code { margin: .6rem 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.code-bar {
  display: flex; justify-content: space-between; align-items: center;
  padding: .25rem .5rem .25rem .8rem; font-size: 12px; color: var(--muted);
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.code-bar button { padding: .15rem .55rem; font-size: 12px; }
.code pre { margin: 0; border-radius: 0; }
#composer {
  display: flex; gap: .5rem; align-items: flex-end;
  padding: .75rem 16px 1rem; border-top: 1px solid var(--border);
}
#composer textarea { resize: none; max-height: 40vh; }

@media (max-width: 600px) {
  .top { flex-direction: column; align-items: flex-start; gap: .4rem; }
  #estado { margin-left: 0; width: 100%; }
}
