:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #14202e;
  --muted: #5d6b7c;
  --line: #d9e0e8;
  --brand: #0b5cab;
  --brand-ink: #ffffff;
  --user: #dcebfb;
  --good: #1a7f37;
  --bad: #b3261e;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151c;
    --panel: #18212b;
    --text: #e8eef5;
    --muted: #93a1b2;
    --line: #2a3644;
    --brand: #4d9be6;
    --brand-ink: #06121f;
    --user: #1f3550;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }

button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button.primary {
  background: var(--brand); color: var(--brand-ink); border: 0; border-radius: var(--radius);
  padding: 0 18px; min-height: 48px; font-weight: 600;
}
button.ghost { background: transparent; border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
button.icon {
  background: transparent; border: 0; font-size: 22px; min-width: 44px; min-height: 44px; border-radius: 10px;
}
button:disabled { opacity: .5; cursor: default; }

/* Barra superior */
.topbar {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.filters { flex: 1; display: flex; gap: 6px; min-width: 0; }
.filters select, .filters input {
  min-height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 0 10px; background: var(--bg);
}
.filters select { flex: 1 1 45%; min-width: 0; font-weight: 600; }
.filters input { flex: 1 1 55%; min-width: 0; }

/* Historial */
.drawer {
  position: fixed; inset: 0 auto 0 0; width: min(380px, 92vw); background: var(--panel);
  border-right: 1px solid var(--line); z-index: 20; display: flex; flex-direction: column;
  box-shadow: 0 0 40px rgba(0,0,0,.25);
}
.drawer[hidden], .settings[hidden] { display: none; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 12px; border-bottom: 1px solid var(--line); }
.drawer-head .check { margin-left: auto; font-size: 14px; }
.history { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.history li { border-bottom: 1px solid var(--line); }
.history button {
  width: 100%; text-align: left; background: none; border: 0; padding: 12px 14px; display: block;
}
.history .t { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history .s { color: var(--muted); font-size: 13px; }

.settings {
  position: absolute; right: 10px; top: 60px; z-index: 15; width: min(320px, 92vw);
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; display: grid; gap: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.2);
}
.settings label { display: grid; gap: 4px; }
.settings select { min-height: 44px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 0 10px; }
.check { display: flex !important; align-items: center; gap: 8px; }
.check input { width: 20px; height: 20px; }

/* Chat */
.chat { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 12px; }
.empty { margin: auto; text-align: center; max-width: 420px; padding: 20px; }
.empty h2 { margin: 0 0 8px; }
.msg { max-width: min(720px, 92%); padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--user); }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); }
.msg.error { align-self: center; color: var(--bad); border: 1px solid var(--bad); background: transparent; }
.msg.typing { color: var(--muted); }
.msg .tools { display: flex; gap: 6px; margin-top: 8px; align-items: center; flex-wrap: wrap; }
.msg .tools button { background: none; border: 1px solid var(--line); border-radius: 8px; min-height: 36px; min-width: 40px; }
.msg .tools button.on { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 15%, transparent); }
.msg details { margin-top: 6px; font-size: 13px; color: var(--muted); }

/* Compositor */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px; min-height: 48px; padding: 12px; line-height: 1.3;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg);
}
.mic {
  width: 56px; height: 48px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--bg); font-size: 22px;
}
.mic.rec { background: var(--bad); border-color: var(--bad); animation: pulse 1.1s infinite; }
@keyframes pulse { 50% { opacity: .6; } }

/* Login */
body.login { align-items: center; justify-content: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card form { display: grid; gap: 14px; margin-top: 16px; }
.login-card label { display: grid; gap: 4px; font-weight: 600; }
.login-card input { min-height: 48px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; background: var(--bg); font-weight: 400; }
.error { color: var(--bad); }
