:root {
  --blue: #1f4e9c;
  --blue-dark: #163a75;
  --gold: #c9a227;
  --bg: #f5f7fc;
  --surface: #ffffff;
  --text: #1b2233;
  --muted: #5d6680;
  --border: #dde3f0;
  --user-bubble: var(--blue);
  --bot-bubble: #eef2fb;
  --link: var(--blue);
  --radius: 14px;
  --nav-h: 62px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
  -webkit-text-size-adjust: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1422;
    --surface: #171e31;
    --text: #e7ebf5;
    --muted: #9aa4bf;
    --border: #2a3450;
    --bot-bubble: #1f2840;
    --user-bubble: #2c5fb8;
    --link: #9cbcff;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

a { color: var(--link); }

/* ---------- Barra superior ---------- */
.topbar {
  padding: calc(0.55rem + env(safe-area-inset-top)) 1rem 0.55rem;
  background: linear-gradient(135deg, var(--blue), var(--blue-dark));
  color: #fff;
  border-bottom: 3px solid var(--gold);
}
.brand { display: flex; align-items: center; gap: 0.65rem; max-width: 820px; margin: 0 auto; }
.logo { border-radius: 50%; background: #fff; }
h1 { font-size: 1.15rem; margin: 0; letter-spacing: 0.05em; }
.subtitle { margin: 0; font-size: 0.75rem; opacity: 0.85; }

/* ---------- Contenido ---------- */
main {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.card h2 { margin: 0; font-size: 1.05rem; color: var(--link); }
.card p { margin: 0; line-height: 1.5; }

.banner { border: 2px solid var(--gold); }

blockquote {
  margin: 0;
  padding: 0.6rem 0.9rem;
  border-left: 3px solid var(--gold);
  background: var(--bot-bubble);
  border-radius: 8px;
  line-height: 1.55;
  font-style: italic;
}
.frase { font-size: 1.1rem; }
.fecha { margin: 0; text-transform: capitalize; }

.row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.row input { flex: 1; min-width: 0; }

label { font-weight: 600; font-size: 0.9rem; }

input[type="text"], select, textarea {
  font: inherit;
  font-size: max(16px, 1rem);
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  width: 100%;
}

button, .button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 0.65rem 1.1rem;
  min-height: 44px;
  background: var(--blue);
  color: #fff;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  touch-action: manipulation;
}
button:hover:not(:disabled), .button:hover { background: var(--blue-dark); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
:is(button, .button, select, input, textarea, a):focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}
button.ghost {
  background: transparent;
  color: var(--link);
  border: 1px solid var(--border);
}
button.ghost:hover:not(:disabled) { background: var(--bot-bubble); }
button.small { min-height: 36px; padding: 0.35rem 0.75rem; font-size: 0.85rem; }
button.danger { background: #b3261e; }
button.danger:hover { background: #8c1d18; }

.muted { color: var(--muted); font-size: 0.9rem; }
.warning {
  background: #fff4e0; color: #7a4b00;
  border: 1px solid #f0c674;
  border-radius: 10px; padding: 0.7rem;
}

/* ---------- Chat ---------- */
.chat { gap: 0.6rem; padding-bottom: 0.5rem; overflow: hidden; }

.chat-tools { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.badge {
  font-size: 0.75rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.6rem;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  padding: 0.2rem;
}

.msg {
  max-width: 88%;
  padding: 0.7rem 0.95rem;
  border-radius: var(--radius);
  line-height: 1.55;
  overflow-wrap: anywhere;
}
.msg p { margin: 0 0 0.5em; }
.msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: 0.3em 0 0.5em; padding-left: 1.3em; }
.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.msg.bot {
  align-self: flex-start;
  background: var(--bot-bubble);
  border-bottom-left-radius: 4px;
  border-left: 3px solid var(--gold);
}
.msg.bot.typing .body::after {
  content: "▍";
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  .msg.bot.typing .body::after { animation: none; }
}
.msg .sources { margin-top: 0.5rem; font-size: 0.8rem; color: var(--muted); }
.msg .sources summary { cursor: pointer; }
.msg .sources ul { margin: 0.3rem 0 0; }
.msg .actions { margin-top: 0.5rem; }

.suggestions {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  scrollbar-width: none;
  flex-shrink: 0;
}
.chip {
  flex-shrink: 0;
  background: var(--surface);
  color: var(--link);
  border: 1px solid var(--border);
  font-weight: 500;
  font-size: 0.85rem;
  min-height: 38px;
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
}
.chip:hover:not(:disabled) { background: var(--bot-bubble); }

.local-status { display: flex; flex-direction: column; gap: 0.3rem; }
.local-status p { margin: 0; font-size: 0.8rem; }
progress { width: 100%; height: 8px; accent-color: var(--blue); }

.composer { display: flex; gap: 0.5rem; align-items: flex-end; flex-shrink: 0; }
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  field-sizing: content;
}
#send-btn { width: 48px; padding: 0; font-size: 1.1rem; }

.disclaimer { margin: 0; text-align: center; font-size: 0.7rem; color: var(--muted); flex-shrink: 0; }

/* ---------- Horario ---------- */
.horario-lista { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.horario-lista li {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.45rem 0.2rem;
  border-bottom: 1px solid var(--border);
}
.horario-lista label { flex: 1; display: flex; align-items: center; gap: 0.6rem; font-weight: 500; cursor: pointer; }
.horario-lista input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--blue); }
.dots { display: flex; gap: 3px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border); }
.dot.on { background: var(--gold); }
.examen-preguntas { margin: 0; padding-left: 1.2rem; line-height: 1.6; }

/* ---------- Ajustes ---------- */
.engine-options { border: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.6rem; }
.option {
  display: flex; gap: 0.7rem; align-items: flex-start;
  border: 1px solid var(--border); border-radius: 10px; padding: 0.7rem;
  font-weight: 400; cursor: pointer;
}
.option:has(input:checked) { border-color: var(--blue); background: var(--bot-bubble); }
.option input { margin-top: 0.25rem; accent-color: var(--blue); }

/* ---------- Navegación inferior ---------- */
.bottom-nav {
  display: flex;
  justify-content: space-around;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav button {
  flex: 1;
  flex-direction: column;
  gap: 0.1rem;
  background: transparent;
  color: var(--muted);
  border-radius: 0;
  height: var(--nav-h);
  font-size: 0.75rem;
  font-weight: 500;
}
.bottom-nav button span { font-size: 1.25rem; }
.bottom-nav button:hover { background: var(--bot-bubble); }
.bottom-nav button[aria-current="page"] { color: var(--link); font-weight: 700; }

/* ---------- Diálogo de historial ---------- */
dialog {
  width: min(520px, 92vw);
  max-height: 80dvh;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1rem;
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { margin: 0; font-size: 1.1rem; }
.history-list { list-style: none; padding: 0; margin: 0.8rem 0 0; display: flex; flex-direction: column; gap: 0.4rem; }
.history-list li { display: flex; gap: 0.4rem; }
.history-list .open {
  flex: 1;
  justify-content: flex-start;
  text-align: left;
  background: var(--bot-bubble);
  color: var(--text);
  font-weight: 500;
  overflow: hidden;
}
.history-list .open small { color: var(--muted); font-weight: 400; margin-left: auto; white-space: nowrap; }
.history-list .open.current { outline: 2px solid var(--blue); }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}

[hidden] { display: none !important; }
