:root {
  --bg: #0f0f1a;
  --bg2: #17172a;
  --bg3: #1f1f3a;
  --accent: #e8794a;
  --accent2: #f2a06b;
  --text: #ece7e0;
  --muted: #9a92a5;
  --border: #2c2c4a;
  --user: #3a3a5c;
  --agent: #26264a;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.error { color: #ff8c8c; font-size: 0.85rem; min-height: 1.2em; margin: 4px 0; }

/* ---------- Auth ---------- */
#auth-root {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
  background:
    radial-gradient(60% 50% at 20% 0%, rgba(232,121,74,0.18), transparent 60%),
    radial-gradient(50% 40% at 90% 100%, rgba(90,70,160,0.25), transparent 60%),
    var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.45);
}

.brand { text-align: center; margin-bottom: 24px; }
.brand-ring {
  width: 64px; height: 64px;
  margin: 0 auto 12px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 8px 24px rgba(232,121,74,0.35);
}
.brand-cup { font-size: 30px; }
.brand h1 { margin: 0; font-size: 1.6rem; font-weight: 700; }
.brand p { margin: 6px 0 0; color: var(--muted); font-size: 0.9rem; }

#auth-form { display: flex; flex-direction: column; gap: 12px; }
#auth-form input {
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  outline: none;
}
#auth-form input:focus { border-color: var(--accent); }

button {
  font-family: inherit;
  border: none;
  cursor: pointer;
}
#auth-form button[type="submit"] {
  padding: 13px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #1a1208;
  font-weight: 700;
  font-size: 1rem;
}
.link-btn {
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 6px;
}
.link-btn:hover { color: var(--text); }

.auth-foot { margin-top: 22px; text-align: center; color: var(--muted); font-size: 0.75rem; }

/* ---------- Install (PWA) ---------- */
#install-root {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
  background:
    radial-gradient(60% 50% at 20% 0%, rgba(232,121,74,0.18), transparent 60%),
    radial-gradient(50% 40% at 90% 100%, rgba(90,70,160,0.25), transparent 60%),
    var(--bg);
}
.install-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.45);
  text-align: center;
}
.brand-core {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 0.02em;
}
.brand-sub {
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 2px;
}
.install-lead {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  margin: 0 0 20px;
}
.install-btn {
  width: 100%;
  padding: 13px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #1a1208;
  font-weight: 700;
  font-size: 1rem;
}
.install-ios-guide {
  margin-top: 18px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  text-align: left;
}
.ios-guide-title { font-weight: 700; font-size: 0.9rem; margin-bottom: 8px; color: var(--accent2); }
.install-ios-guide ol { margin: 0; padding-left: 20px; color: var(--muted); font-size: 0.85rem; line-height: 1.7; }
.install-ios-guide strong { color: var(--text); }

/* ---------- Layout ---------- */
#app-root { display: flex; height: 100dvh; }

#sidebar {
  width: 290px;
  min-width: 290px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.sidebar-head { padding: 16px; border-bottom: 1px solid var(--border); }
.sidebar-brand { font-weight: 700; font-size: 1.05rem; margin-bottom: 12px; color: var(--accent2); }
.new-chat-btn {
  width: 100%;
  padding: 11px;
  border-radius: 12px;
  background: var(--bg3);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid var(--border);
}
.new-chat-btn:hover { border-color: var(--accent); }

.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  padding: 11px 12px;
  border-radius: 12px;
  margin-bottom: 4px;
  cursor: pointer;
  border: 1px solid transparent;
}
.conv-item:hover { background: var(--bg3); }
.conv-item.active { background: var(--bg3); border-color: var(--border); }
.conv-item-title {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item-meta {
  font-size: 0.75rem; color: var(--muted);
  margin-top: 3px;
  display: flex; justify-content: space-between;
}
.conv-delete {
  background: transparent; color: var(--muted);
  font-size: 0.8rem; padding: 0 4px;
}
.conv-delete:hover { color: #ff8c8c; }

.sidebar-foot {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  font-size: 0.85rem;
}
.user-info { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Chat ---------- */
#chat { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }

.chat-head { padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--bg2); }
.chat-title { font-size: 0.8rem; color: var(--muted); margin-top: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.agent-picker { display: flex; gap: 8px; flex-wrap: wrap; }
.agent-chip {
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 600;
}
.agent-chip:hover { border-color: var(--accent); }
.agent-chip.active { background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #1a1208; border-color: transparent; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 420px;
}
.empty-bubble {
  width: 72px; height: 72px;
  margin: 0 auto 16px;
  border-radius: 50%;
  background: var(--bg3);
  display: flex; align-items: center; justify-content: center;
  font-size: 34px;
  border: 1px solid var(--border);
}

.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg.agent { justify-content: flex-start; }
.msg-inner {
  max-width: 76%;
  padding: 11px 15px;
  border-radius: var(--radius);
  line-height: 1.5;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user .msg-inner { background: var(--user); border-bottom-right-radius: 4px; }
.msg.agent .msg-inner { background: var(--agent); border-bottom-left-radius: 4px; border: 1px solid var(--border); }
.msg.agent .msg-label { font-size: 0.7rem; color: var(--muted); margin-bottom: 4px; }
.msg.cursor .msg-inner::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s step-end infinite;
}
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

/* ---------- Composer ---------- */
.composer-wrap { padding: 14px 20px 18px; background: var(--bg); }
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
}
.composer:focus-within { border-color: var(--accent); }
#composer-input {
  flex: 1;
  background: transparent;
  border: none;
  resize: none;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  padding: 8px 6px;
  max-height: 160px;
  outline: none;
}
.send-btn {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #1a1208;
  font-size: 1.2rem;
  font-weight: 700;
  flex-shrink: 0;
}
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.composer-hint { margin-top: 8px; font-size: 0.7rem; color: var(--muted); text-align: center; }

/* ---------- Scrollbar ---------- */
::-webkit-scrollbar { width: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Mobile ---------- */
@media (max-width: 700px) {
  #sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(85vw, 300px);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 0 0 40px rgba(0,0,0,0.5);
  }
  #sidebar.open { transform: translateX(0); }
  .chat-head { padding-top: 12px; }
  .msg-inner { max-width: 88%; }
  .composer-wrap { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
}

@media (min-width: 701px) {
  #sidebar { transform: none; }
}