:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --border: #e2dfd8;
  --text: #1d1d1b;
  --muted: #6f6c64;
  --accent: #c2603f;
  --accent-text: #ffffff;
  --ok: #2f7d4f;
  --warn: #a96a00;
  --err: #b3392f;
  --user-bubble: #1d1d1b;
  --user-text: #f6f5f2;
  --code-bg: #f0eee9;
  --radius: 14px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121314;
    --surface: #1b1c1e;
    --surface-2: #232427;
    --border: #2e3033;
    --text: #e8e6df;
    --muted: #9a978f;
    --accent: #d97757;
    --accent-text: #121314;
    --ok: #5fbf86;
    --warn: #e0a640;
    --err: #ef7a6e;
    --user-bubble: #2c2d31;
    --user-text: #e8e6df;
    --code-bg: #17181a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }

.app { display: flex; flex-direction: column; height: 100dvh; max-width: 760px; margin: 0 auto; }
.center { margin: auto; text-align: center; padding: 24px 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* Barra superior */
.top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky; top: 0; z-index: 2;
}
.top h1 { font-size: 17px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top .sub { display: block; font-size: 12px; font-weight: 400; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }

.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 9px 14px; cursor: pointer; min-height: 40px;
}
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; color: var(--text); }
a.btn.block { display: flex; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--err); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 10px;
  display: inline-grid; place-items: center; cursor: pointer; font-size: 18px; flex: none;
}
.icon-btn:active, .btn:active { transform: scale(.97); }

/* Tarjetas y formularios */
.page { flex: 1; overflow-y: auto; padding: 16px; padding-bottom: calc(env(safe-area-inset-bottom) + 24px); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card h2 { font-size: 16px; margin: 0 0 8px; }
.field { display: block; margin: 12px 0; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.input, textarea.input, select.input {
  width: 100%; border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 10px 12px; font-size: 16px;
}
textarea.input { resize: vertical; min-height: 120px; }
.code-input { font-family: var(--mono); font-size: 26px; letter-spacing: 6px; text-align: center; }
.row { display: flex; gap: 10px; align-items: center; }
.row > * { flex: 1; }
.check { display: flex; gap: 10px; align-items: flex-start; margin: 12px 0; }
.check input { width: 20px; height: 20px; margin-top: 2px; flex: none; }
.error { color: var(--err); font-size: 14px; min-height: 20px; margin: 8px 0 0; }
.qr { display: block; width: 220px; height: 220px; margin: 8px auto; background: #fff; border-radius: 8px; }
.secret { font-family: var(--mono); font-size: 13px; word-break: break-all; text-align: center; color: var(--muted); }
.divider { text-align: center; color: var(--muted); font-size: 13px; margin: 16px 0; }

/* Lista de chats */
.chat-item {
  display: block; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 10px;
}
.chat-item .title { font-weight: 600; display: flex; gap: 8px; align-items: center; }
.chat-item .title span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .meta { font-size: 13px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--border); flex: none; }
.dot.running, .dot.starting { background: var(--accent); animation: pulse 1.2s infinite; }
.dot.waiting { background: var(--warn); }
@keyframes pulse { 50% { opacity: .35; } }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

/* Conversación */
.msgs { flex: 1; overflow-y: auto; padding: 14px 12px 8px; -webkit-overflow-scrolling: touch; }
.msg { margin: 10px 0; overflow-wrap: anywhere; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); color: var(--user-text); border-radius: 16px 16px 4px 16px;
  padding: 9px 13px; max-width: 88%; white-space: pre-wrap;
}
.msg.assistant { padding: 0 2px; }
.msg.assistant.sub { border-left: 2px solid var(--border); padding-left: 10px; color: var(--muted); font-size: 14px; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-left: 22px; }
.md h1, .md h2, .md h3, .md h4 { font-size: 16px; margin: 12px 0 4px; }
.md code { font-family: var(--mono); font-size: 13.5px; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; overflow-x: auto; margin: 8px 0; }
.md pre code { background: none; padding: 0; font-size: 13px; white-space: pre; }
.md blockquote { margin: 6px 0; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }
.md .table-wrap { overflow-x: auto; margin: 8px 0; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; vertical-align: top; }

.tool { margin: 6px 0; font-size: 14px; }
.tool > summary {
  list-style: none; cursor: pointer; display: flex; gap: 6px; align-items: baseline;
  color: var(--muted); padding: 4px 2px; border-radius: 8px;
}
.tool > summary::-webkit-details-marker { display: none; }
.tool > summary::before { content: "▸"; font-size: 11px; }
.tool[open] > summary::before { content: "▾"; }
.tool .name { font-weight: 600; color: var(--text); flex: none; }
.tool .sum { font-family: var(--mono); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.tool .state { flex: none; margin-left: auto; font-size: 12px; }
.tool .state.err { color: var(--err); }
.tool pre {
  font-family: var(--mono); font-size: 12px; background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; margin: 4px 0; max-height: 320px; overflow: auto; white-space: pre-wrap;
}

.approval { background: var(--surface); border: 1px solid var(--warn); border-radius: var(--radius); padding: 12px; margin: 10px 0; }
.approval.done { border-color: var(--border); opacity: .8; }
.approval .head { font-weight: 600; margin-bottom: 4px; }
.approval .sum { font-family: var(--mono); font-size: 13px; background: var(--code-bg); border-radius: 8px; padding: 8px 10px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 240px; overflow: auto; }
.approval .actions { display: flex; gap: 8px; margin-top: 10px; }
.approval .actions .btn { flex: 1; }
.approval .verdict { font-size: 14px; margin-top: 6px; }
.approval .verdict.allow { color: var(--ok); }
.approval .verdict.deny { color: var(--err); }

.note { text-align: center; font-size: 12.5px; color: var(--muted); margin: 10px 0; }
.note.err { color: var(--err); }
.typing { color: var(--muted); font-size: 14px; padding: 4px 2px; }
.typing::after { content: "…"; animation: pulse 1.2s infinite; }

/* Barra para escribir */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 8px 10px calc(env(safe-area-inset-bottom) + 8px);
  border-top: 1px solid var(--border); background: var(--bg);
}
.composer textarea {
  flex: 1; resize: none; max-height: 160px; min-height: 44px; border-radius: 22px;
  border: 1px solid var(--border); background: var(--surface); padding: 10px 14px; font-size: 16px; line-height: 1.4;
}
.send, .stop {
  width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-size: 18px; display: grid; place-items: center;
}
.stop { background: var(--err); color: #fff; }

/* Hoja inferior (menús) */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--surface); width: 100%; max-width: 760px; border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 16px); max-height: 85dvh; overflow-y: auto;
}
.sheet h3 { margin: 0 0 10px; font-size: 16px; }
.sheet .btn { margin-bottom: 8px; }
.sheet pre { font-family: var(--mono); font-size: 12.5px; background: var(--code-bg); border-radius: 8px; padding: 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.seg .btn.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 80px); z-index: 20;
  max-width: 560px; margin: 0 auto; background: var(--text); color: var(--bg);
  border-radius: 12px; padding: 10px 14px; font-size: 14px; text-align: center;
}

.list-row { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: 0; }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .grow div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
