*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Elimina flash de tap en iOS y retraso de 300ms */
* { -webkit-tap-highlight-color: transparent; }
button, a, [role="button"] { touch-action: manipulation; user-select: none; }

:root {
  --bg:        #111113;
  --surface:   #1a1a1f;
  --border:    #2a2a32;
  --text:      #e8e8f0;
  --muted:     #6060a0;
  --accent:    #7c3aed;
  --accent-h:  #6025c9;
  --user-bg:   #1e1b3a;
  --user-bd:   #2d2960;
  --alex-bg:   #1a1a1f;
  --err:       #ef4444;
  --green:     #22c55e;
  --tool-run:  rgba(124,58,237,.15);
  --tool-done: rgba(22,163,74,.15);
  --font:      -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --mono:      'Fira Code', 'Cascadia Code', 'Consolas', monospace;
}

html, body {
  height: 100%; height: 100dvh; /* dvh se ajusta cuando aparece el teclado en iPhone */
  font-family: var(--font); background: var(--bg); color: var(--text); overflow: hidden;
}

.hidden { display: none !important; }

/* ── Auth ── */
#auth-screen {
  height: 100dvh; display: flex; align-items: center; justify-content: center;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 340px;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,.6);
}

.brand { display: flex; align-items: center; justify-content: center; gap: 8px; margin-bottom: 24px; }
.brand-icon { font-size: 24px; }
.brand-name { font-size: 17px; font-weight: 700; color: var(--muted); }

.alex-name {
  font-size: 38px; font-weight: 800; letter-spacing: -1.5px;
  background: linear-gradient(135deg, #a78bfa 0%, #7c3aed 100%);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
  margin-bottom: 6px;
}

.alex-sub { color: var(--muted); font-size: 12px; margin-bottom: 32px; text-transform: uppercase; letter-spacing: 1.5px; }

#username-input, #password-input {
  width: 100%; padding: 12px 14px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); font-size: 14px; outline: none;
  transition: border-color .15s; margin-bottom: 10px; font-family: var(--font);
}
#username-input:focus, #password-input:focus { border-color: var(--accent); }

#login-btn {
  width: 100%; padding: 12px;
  background: var(--accent); border: none; border-radius: 10px;
  color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
  transition: background .15s; font-family: var(--font);
}
#login-btn:hover:not(:disabled) { background: var(--accent-h); }
#login-btn:disabled { opacity: .5; cursor: not-allowed; }

.auth-err { color: var(--err); font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ── App layout ── */
#app { height: 100dvh; display: flex; flex-direction: column; }

/* ── Header ── */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.hdr-left { display: flex; align-items: center; gap: 10px; }
.hdr-icon { font-size: 20px; }
.hdr-title { font-size: 15px; font-weight: 700; letter-spacing: -.2px; line-height: 1.2; }
.hdr-sub { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

.hdr-right { display: flex; align-items: center; gap: 8px; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); transition: background .3s; }
.dot.loading { background: var(--accent); animation: blink 1s ease-in-out infinite; }

@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: .15; } }

.btn-icon {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  padding: 5px 9px; border-radius: 7px; font-size: 15px; cursor: pointer;
  transition: all .15s; font-family: var(--font);
}
.btn-icon:hover { color: var(--text); border-color: var(--muted); }

/* ── Chat ── */
#chat { flex: 1; overflow-y: auto; padding: 20px; scroll-behavior: smooth; }
#chat::-webkit-scrollbar { width: 4px; }
#chat::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#messages { display: flex; flex-direction: column; gap: 14px; max-width: 760px; margin: 0 auto; min-height: 100%; justify-content: flex-end; }

/* ── Welcome ── */
.welcome { text-align: center; padding: 60px 20px 20px; animation: fadeUp .3s ease; }
.welcome-icon { font-size: 38px; margin-bottom: 16px; }
.welcome h2 { font-size: 26px; font-weight: 700; margin-bottom: 8px; }
.welcome p { color: var(--muted); font-size: 15px; margin-bottom: 28px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: 7px 14px; border-radius: 20px; font-size: 13px;
  cursor: pointer; transition: all .15s; font-family: var(--font);
}
.chip:hover { border-color: var(--accent); color: #a78bfa; }

/* ── Messages ── */
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.msg { display: flex; gap: 10px; animation: fadeUp .2s ease; }
.msg.user { flex-direction: row-reverse; }

.avatar {
  width: 30px; height: 30px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; flex-shrink: 0; margin-top: 2px;
}
.user-av { background: var(--accent); color: #fff; }
.alex-av { background: var(--surface); border: 1px solid var(--border); }

.msg-body { display: flex; flex-direction: column; gap: 5px; min-width: 0; max-width: calc(100% - 40px); }

.bubble {
  padding: 10px 14px; border-radius: 14px;
  font-size: 14px; line-height: 1.65; word-break: break-word;
}

.user-bubble {
  background: var(--user-bg); border: 1px solid var(--user-bd);
  border-radius: 14px 4px 14px 14px; color: #ddd4ff; white-space: pre-wrap;
}

.alex-bubble {
  background: var(--alex-bg); border: 1px solid var(--border);
  border-radius: 4px 14px 14px 14px;
}

/* Typing cursor */
.cursor::after { content: '▌'; color: var(--accent); animation: blink .9s ease-in-out infinite; }

/* ── Markdown in messages ── */
.alex-bubble code {
  background: #0d0d14; border: 1px solid var(--border);
  border-radius: 4px; padding: 1px 6px;
  font-family: var(--mono); font-size: 12px; color: #a78bfa;
}

.alex-bubble pre {
  background: #0d0d14; border: 1px solid var(--border);
  border-radius: 8px; padding: 12px; overflow-x: auto; margin: 6px 0;
}

.alex-bubble pre code {
  background: transparent; border: none; padding: 0; color: #c8c8d8; font-size: 13px;
}

.alex-bubble strong { font-weight: 600; color: #f0f0ff; }
.alex-bubble em { font-style: italic; color: #c0c0e0; }

/* ── Tool indicators ── */
.tools { display: flex; flex-direction: column; gap: 4px; }

.tool {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 6px; font-size: 12px; font-weight: 500;
  max-width: fit-content;
}

.tool.running {
  background: var(--tool-run); border: 1px solid rgba(124,58,237,.35);
  color: #a78bfa; animation: blink 1s ease-in-out infinite;
}

.tool.done {
  background: var(--tool-done); border: 1px solid rgba(22,163,74,.35); color: #86efac;
}

.tool-icon { font-size: 11px; }

/* ── Error ── */
.err { color: var(--err); }

/* ── Footer / input ── */
footer {
  padding: 14px 20px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

header {
  padding-top: calc(12px + env(safe-area-inset-top));
}

.input-wrap {
  display: flex; align-items: flex-end; gap: 10px;
  max-width: 760px; margin: 0 auto;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 8px 8px 8px 14px;
  transition: border-color .15s;
}
.input-wrap:focus-within { border-color: var(--accent); }

textarea {
  flex: 1; background: transparent; border: none; outline: none;
  color: var(--text); font-family: var(--font); font-size: 14px;
  line-height: 1.5; resize: none; max-height: 200px; overflow-y: auto;
}
textarea::placeholder { color: var(--muted); }
textarea::-webkit-scrollbar { width: 3px; }
textarea::-webkit-scrollbar-thumb { background: var(--border); }

#send-btn {
  background: var(--accent); border: none; color: #fff;
  width: 34px; height: 34px; border-radius: 9px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: background .15s;
}
#send-btn:hover:not(:disabled) { background: var(--accent-h); }
#send-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ── Mobile / iPhone ── */
@media (max-width: 600px) {

  /* Auth */
  .auth-card {
    width: calc(100% - 32px);
    padding: 36px 20px;
    border-radius: 16px;
  }
  .alex-name { font-size: 32px; }

  /* 16px mínimo en inputs para evitar zoom automático en iPhone */
  #username-input, #password-input {
    font-size: 16px;
    padding: 14px;
  }
  #login-btn {
    font-size: 16px;
    padding: 14px;
    min-height: 50px;
  }

  /* Header */
  header {
    padding: 10px 14px;
    padding-top: calc(10px + env(safe-area-inset-top));
  }
  .hdr-sub { display: none; }
  .hdr-icon { font-size: 18px; }
  .hdr-title { font-size: 14px; }

  /* Botones de cabecera: mínimo 44×44px (HIG de Apple) */
  .btn-icon {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 8px;
    font-size: 17px;
    border-radius: 10px;
  }

  /* Chat */
  #chat { padding: 10px 12px; }
  #messages { gap: 10px; }

  /* Welcome */
  .welcome { padding: 28px 8px 16px; }
  .welcome-icon { font-size: 32px; margin-bottom: 10px; }
  .welcome h2 { font-size: 22px; }
  .welcome p { font-size: 14px; margin-bottom: 20px; }

  /* Chips: scroll horizontal en móvil */
  .chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    justify-content: flex-start;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex-shrink: 0; font-size: 13px; }

  /* Mensajes */
  .avatar { width: 26px; height: 26px; font-size: 11px; border-radius: 7px; }
  .msg { gap: 8px; }
  .msg-body { max-width: calc(100% - 34px); }

  .bubble {
    font-size: 15px;
    padding: 9px 12px;
    line-height: 1.6;
  }

  .alex-bubble pre { padding: 10px; }
  .alex-bubble pre code { font-size: 12px; }

  /* Footer */
  footer {
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  .input-wrap {
    padding: 5px 5px 5px 14px;
    border-radius: 22px;
    gap: 8px;
  }

  textarea {
    font-size: 16px; /* evita zoom en iPhone */
    padding: 5px 0;
  }

  /* Send: mínimo 44×44px */
  #send-btn {
    width: 40px;
    height: 40px;
    border-radius: 14px;
  }
}

/* ── WhatsApp modal ── */
.modal {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,.6); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; width: 100%; max-width: 340px;
  padding: 20px; box-shadow: 0 16px 60px rgba(0,0,0,.7);
}
.modal-hdr {
  display: flex; justify-content: space-between; align-items: center;
  font-weight: 700; font-size: 16px; margin-bottom: 14px;
}
.wa-status {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; padding: 8px 12px; border-radius: 10px;
  margin-bottom: 12px;
}
.wa-status.ok  { background: rgba(34,197,94,.12); color: #4ade80; }
.wa-status.err { background: rgba(239,68,68,.12);  color: #f87171; }
.wa-status.mid { background: rgba(124,58,237,.12); color: #a78bfa; }
#wa-qr-area img { border-radius: 12px; max-width: 240px; width: 100%; }
.text-muted { color: var(--text-muted); }
