:root {
  --bg: #000;
  --bg-elev: #0c0c0d;
  --bg-elev-2: #161618;
  --border: #1f1f22;
  --border-strong: #2a2a2e;
  --text: #ececf1;
  --text-dim: #9b9ba4;
  --text-faint: #6b6b75;
  --user-bubble: #1f1f22;
  --radius: 18px;
  --shadow-soft: 0 8px 32px rgba(0, 0, 0, .55);
  --ease: cubic-bezier(.22, 1, .36, 1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter",
    "Helvetica Neue", Roboto, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
  overscroll-behavior: none;
  /* Impede zoom no iOS ao focar input */
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(255,255,255,.04), transparent 60%),
    var(--bg);
}

button { font: inherit; color: inherit; cursor: pointer; }
textarea { font: inherit; color: inherit; }

button,
textarea,
input,
select {
  -webkit-appearance: none;
  appearance: none;
}

h1, h2, h3, h4, h5, h6, p {
  font: inherit;
}

/* ===== Layout ===== */
.app {
  display: flex;
  flex-direction: column;
  /* --app-h é setado via JS para lidar com teclado virtual no mobile */
  height: var(--app-h, 100dvh);
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  position: relative;
  will-change: transform;
}

.topbar {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-wrap: nowrap;
  gap: 10px;
  padding: calc(var(--safe-top) + 10px) 12px 10px;
  background: rgba(0, 0, 0, .72);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand {
  display: flex;
  flex: 1;
  min-width: 0;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  letter-spacing: .2px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.logo-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fff 0%, #8e8ea0 100%);
  box-shadow: 0 0 12px rgba(255,255,255,.5);
}
.brand-text { font-size: 15px; }

.icon-btn {
  width: 40px; height: 40px;
  border: 0;
  background: transparent;
  border-radius: 12px;
  color: var(--text);
  display: grid; place-items: center;
  flex-shrink: 0;
  transition: background .18s var(--ease), transform .18s var(--ease);
}
.icon-btn:hover { background: var(--bg-elev); }
.icon-btn:active { transform: scale(.94); }

/* DB debug panel in header */
.db-debug {
  margin-left: 12px;
  padding: 8px 12px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.03);
  border-radius: 12px;
  font-size: 13px;
  color: var(--text-dim);
}
.db-table-view { margin-top:6px; border-collapse: collapse; font-size: 13px; }
.db-table-view td { padding: 2px 8px; color: var(--text-faint); }
.db-table { text-transform: lowercase; color: var(--text-faint); }
.db-count { text-align: right; padding-left: 12px; color: var(--text); }
.db-last { margin-top:6px; font-size:12px; color:var(--text-faint); }

/* ===== Chat ===== */
.chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 14px 24px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}
.chat::-webkit-scrollbar { width: 6px; }
.chat::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }

.welcome {
  text-align: center;
  padding: 40px 12px 24px;
  animation: fade-up .5s var(--ease) both;
}
.welcome h1 {
  font-size: clamp(22px, 5.2vw, 28px);
  margin: 0 0 8px;
  font-weight: 600;
  letter-spacing: -.2px;
}
.welcome p {
  margin: 0 auto 22px;
  color: var(--text-dim);
  max-width: 38ch;
  line-height: 1.5;
}
.suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 440px;
  margin: 0 auto;
}
.chip {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 14px;
  border-radius: 14px;
  text-align: left;
  font-size: 14px;
  line-height: 1.4;
  transition: border-color .18s var(--ease), transform .18s var(--ease), background .18s var(--ease);
}
.chip:hover { border-color: var(--border-strong); background: var(--bg-elev-2); }
.chip:active { transform: scale(.985); }

/* ===== Messages ===== */
.msg {
  display: flex;
  gap: 10px;
  margin: 14px 0;
  animation: fade-up .35s var(--ease) both;
}
.msg .avatar {
  width: 28px; height: 28px;
  flex: 0 0 28px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .3px;
}
.msg.user { justify-content: flex-end; }
.msg.user .avatar { display: none; }

.bubble {
  max-width: 86%;
  padding: 12px 14px;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--user-bubble);
  border: 1px solid var(--border);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
}
.msg.ai .bubble {
  background: transparent;
  border: 0;
  padding-left: 0;
}
.msg.ai .avatar {
  background: linear-gradient(135deg, #ffffff 0%, #c7c7d1 100%);
  color: #111;
}

/* Markdown */
.bubble h1, .bubble h2, .bubble h3 { margin: .8em 0 .35em; line-height: 1.25; }
.bubble h1 { font-size: 1.25rem; }
.bubble h2 { font-size: 1.12rem; }
.bubble h3 { font-size: 1rem; }
.bubble p { margin: .35em 0; }
.bubble ul, .bubble ol { padding-left: 1.25em; margin: .35em 0; }
.bubble li { margin: .2em 0; }
.bubble code {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: .88em;
  font-family: ui-monospace, Menlo, Consolas, monospace;
}
.bubble pre {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  overflow-x: auto;
  font-size: .85em;
}
.bubble pre code { background: transparent; border: 0; padding: 0; }
.bubble blockquote {
  margin: .5em 0;
  padding: .2em 0 .2em .9em;
  border-left: 2px solid var(--border-strong);
  color: var(--text-dim);
}

/* ===== Login gate ===== */
body.is-locked .app {
  filter: blur(10px);
  transform: scale(.985);
  pointer-events: none;
  user-select: none;
}

.login-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 50% 0%, rgba(255,255,255,.08), transparent 56%),
    rgba(0, 0, 0, .78);
  backdrop-filter: blur(18px) saturate(120%);
  -webkit-backdrop-filter: blur(18px) saturate(120%);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s var(--ease);
  z-index: 50;
}

.login-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.login-card {
  width: min(92vw, 440px);
  border: 1px solid var(--border-strong);
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(18,18,20,.98), rgba(10,10,12,.98));
  box-shadow: 0 26px 80px rgba(0, 0, 0, .6);
  padding: 28px 24px 22px;
}

.login-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.12);
  color: var(--text-dim);
  font-size: 12px;
  letter-spacing: .4px;
  text-transform: uppercase;
}

.login-card h1 {
  margin: 0 0 10px;
  font-size: clamp(24px, 5vw, 30px);
  letter-spacing: -.3px;
  line-height: 1.1;
}

.login-card p {
  margin: 0 0 20px;
  color: var(--text-dim);
  line-height: 1.5;
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-form label {
  display: grid;
  gap: 8px;
}

.login-form span {
  font-size: 13px;
  color: var(--text-dim);
}

.login-form input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-elev);
  padding: 14px 15px;
  color: var(--text);
  outline: none;
  transition: border-color .18s var(--ease), box-shadow .18s var(--ease), transform .18s var(--ease);
}

.login-form input:focus {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px rgba(255,255,255,.06);
}

.login-btn {
  margin-top: 4px;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  background: linear-gradient(180deg, #f4f4f7, #d6d6dd);
  color: #0f0f12;
  padding: 13px 16px;
  font-weight: 700;
  letter-spacing: .2px;
  transition: transform .18s var(--ease), filter .18s var(--ease);
}

.login-btn:hover {
  filter: brightness(1.03);
}

.login-btn:active {
  transform: scale(.99);
}

.login-status {
  min-height: 1.2em;
  margin: 0;
  color: var(--text-dim);
  font-size: 13px;
}

.login-status.error {
  color: #ff8d8d;
}

.login-footer {
  margin-top: 18px;
  font-size: 12px;
  color: var(--text-faint);
  line-height: 1.45;
}

@media (max-width: 520px) {
  .login-card {
    width: min(100%, 440px);
    padding: 24px 18px 18px;
    border-radius: 20px;
  }
}
.bubble a { color: #9ec5ff; text-decoration: underline; text-underline-offset: 2px; }
.bubble strong { color: #fff; }
.bubble hr { border: 0; border-top: 1px solid var(--border); margin: .8em 0; }

/* Loading dots */
.typing {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  padding: 6px 2px;
}
.typing span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }

/* ===== Composer ===== */
.composer {
  padding: 8px 12px calc(var(--safe-bottom) + 10px);
  background:
    linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,.85) 30%, #000 100%);
  /* sticky funciona dentro do flex column */
  position: sticky;
  bottom: 0;
  z-index: 5;
  /* garante que não some atrás do teclado virtual */
  flex-shrink: 0;
}
.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 8px 8px 8px 14px;
  transition: border-color .18s var(--ease), box-shadow .18s var(--ease);
  box-shadow: var(--shadow-soft);
}
.composer-inner:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px rgba(255,255,255,.04), var(--shadow-soft);
}
#input {
  flex: 1;
  background: transparent;
  border: 0;
  outline: 0;
  resize: none;
  max-height: 180px;
  padding: 8px 4px;
  line-height: 1.45;
  font-size: 16px;
}
#input::placeholder { color: var(--text-faint); }

.send-btn {
  flex: 0 0 38px;
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 0;
  display: grid; place-items: center;
  background: #fff;
  color: #000;
  transition: transform .15s var(--ease), opacity .18s var(--ease), background .18s var(--ease);
}
.send-btn:disabled {
  background: var(--bg-elev-2);
  color: var(--text-faint);
  cursor: not-allowed;
}
.send-btn:not(:disabled):active { transform: scale(.92); }

.composer-hint {
  margin: 6px 0 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-faint);
}

/* ===== Drawer ===== */
.drawer {
  position: fixed;
  inset: 0 auto 0 0;
  width: min(86vw, 360px);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform .32s var(--ease);
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer-inner {
  display: flex; flex-direction: column;
  height: 100%;
  padding: calc(var(--safe-top) + 12px) 6px var(--safe-bottom);
}
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 10px 10px;
  border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.history-list {
  list-style: none;
  margin: 0; padding: 6px;
  overflow-y: auto;
  flex: 1;
}
.history-item {
  padding: 12px;
  border-radius: 12px;
  cursor: pointer;
  transition: background .18s var(--ease);
  border: 1px solid transparent;
}
.history-item:hover { background: var(--bg-elev-2); border-color: var(--border); }
.history-item .q {
  font-size: 14px;
  line-height: 1.4;
  display: -webkit-box;
  line-clamp: 2;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.history-item .t {
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-faint);
}

.drawer-empty {
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  padding: 24px 12px;
  display: none;
}
.drawer-empty.show { display: block; }

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s var(--ease);
  z-index: 15;
}
.scrim.show { opacity: 1; pointer-events: auto; }

/* ===== Animações ===== */
@keyframes fade-up {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes blink {
  0%, 80%, 100% { opacity: .25; transform: translateY(0); }
  40%           { opacity: 1;   transform: translateY(-2px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}