:root {
  color-scheme: dark;
  --bg: #070a12;
  --panel: #0f1524;
  --panel-2: #151c2e;
  --border: rgba(255,255,255,0.08);
  --text: #e8eefc;
  --muted: #93a0b8;
  --accent: #7c6cff;
  --accent-2: #4cc9f0;
  --user: #1f2a44;
  --bot: #121826;
  --danger: #ff6b7a;
  --shadow: 0 12px 40px rgba(0,0,0,0.35);
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(124,108,255,0.18), transparent 50%),
    radial-gradient(900px 500px at 100% 0%, rgba(76,201,240,0.12), transparent 45%),
    var(--bg);
  color: var(--text);
}

#app { min-height: 100%; display: flex; }

.hidden { display: none !important; }

.login-view {
  flex: 1; display: grid; place-items: center; padding: 24px;
}
.login-card {
  width: min(420px, 100%);
  background: linear-gradient(180deg, rgba(255,255,255,0.04), transparent), var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.brand { text-align: center; margin-bottom: 22px; }
.brand-mark {
  width: 56px; height: 56px; margin: 0 auto 12px; border-radius: 16px;
  display: grid; place-items: center; font-weight: 800; font-size: 24px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
}
.brand h1 { margin: 0; font-size: 1.5rem; }
.brand p { margin: 8px 0 0; color: var(--muted); }
.field { display: grid; gap: 8px; margin-bottom: 14px; }
.field span { color: var(--muted); font-size: 0.9rem; }
input, textarea, button {
  font: inherit; color: inherit;
}
input, textarea {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 14px;
  outline: none;
}
input:focus, textarea:focus { border-color: rgba(124,108,255,0.55); box-shadow: 0 0 0 3px rgba(124,108,255,0.15); }
button {
  border: 0; cursor: pointer; border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), #5b8cff);
  color: white; font-weight: 600; padding: 13px 16px;
}
button:disabled { opacity: 0.55; cursor: not-allowed; }
.ghost {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  padding: 8px 12px; border-radius: 12px;
}
.error { color: var(--danger); min-height: 1.2em; margin: 10px 0 0; font-size: 0.92rem; }

.chat-view {
  width: min(920px, 100%);
  margin: 0 auto;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto auto auto;
  background: rgba(10,14,24,0.72);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  backdrop-filter: blur(10px);
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
  background: rgba(15,21,36,0.9); position: sticky; top: 0; z-index: 5;
}
.topbar-left { display: flex; gap: 12px; align-items: center; }
.avatar {
  width: 42px; height: 42px; border-radius: 14px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); font-weight: 800;
}
.title { font-weight: 700; }
.status { color: var(--muted); font-size: 0.85rem; }

.messages {
  overflow-y: auto; padding: 18px 14px 8px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.day-sep {
  align-self: center; color: var(--muted); font-size: 0.78rem;
  background: rgba(255,255,255,0.04); padding: 4px 10px; border-radius: 999px;
}
.bubble {
  max-width: min(78%, 560px); padding: 12px 14px; border-radius: 18px;
  border: 1px solid var(--border); line-height: 1.45; white-space: pre-wrap; word-break: break-word;
  animation: rise .18s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px);} to { opacity:1; transform:none;} }
.bubble.user {
  align-self: flex-end;
  background: linear-gradient(180deg, #243356, var(--user));
  border-bottom-right-radius: 6px;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--bot);
  border-bottom-left-radius: 6px;
}
.meta {
  margin-top: 8px; color: var(--muted); font-size: 0.72rem;
}
.attachments {
  display: grid; gap: 8px; margin-top: 10px;
}
.att {
  display: flex; gap: 10px; align-items: center;
  background: rgba(255,255,255,0.04); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px; text-decoration: none; color: inherit;
}
.att img, .att video {
  max-width: 100%; border-radius: 10px; display: block;
}
.att audio { width: 100%; }
.att .file-icon {
  width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center;
  background: rgba(124,108,255,0.18); color: #cfc8ff; font-weight: 700; font-size: 0.75rem;
}
.att .file-meta { min-width: 0; }
.att .file-meta strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att .file-meta span { color: var(--muted); font-size: 0.78rem; }

.typing {
  display: flex; gap: 5px; padding: 0 18px 8px;
}
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .45; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.preview-bar {
  display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px;
  border-top: 1px solid var(--border); background: rgba(15,21,36,0.85);
}
.preview-item {
  position: relative; min-width: 86px; max-width: 140px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; padding: 6px;
}
.preview-item img { width: 100%; height: 64px; object-fit: cover; border-radius: 8px; }
.preview-item .pname {
  font-size: 0.72rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 4px;
}
.preview-item .rm {
  position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; border-radius: 50%;
  background: rgba(0,0,0,0.65); color: white; border: 0; padding: 0; font-size: 14px;
}

.composer {
  display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: end;
  padding: 12px 12px calc(12px + var(--safe-b));
  border-top: 1px solid var(--border); background: rgba(15,21,36,0.95);
}
.attach {
  width: 44px; height: 44px; display: grid; place-items: center; border-radius: 14px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--muted); cursor: pointer;
}
.attach input { display: none; }
#text-input {
  resize: none; max-height: 160px; min-height: 44px; padding: 12px 14px;
  line-height: 1.35;
}
#send-btn {
  width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border-radius: 14px;
}

.empty {
  margin: auto; text-align: center; color: var(--muted); padding: 40px 20px;
}
.empty strong { display: block; color: var(--text); margin-bottom: 6px; font-size: 1.1rem; }

@media (max-width: 720px) {
  .chat-view { border: 0; width: 100%; }
  .bubble { max-width: 88%; }
}

.topbar-right { display: flex; align-items: center; gap: 8px; }
.install-btn {
  background: linear-gradient(135deg, rgba(124,108,255,0.25), rgba(76,201,240,0.2));
  border: 1px solid rgba(124,108,255,0.45);
  color: var(--text);
  font-weight: 600;
}
.install-btn:hover { border-color: rgba(124,108,255,0.75); }
#install-btn-login {
  width: 100%;
  margin-top: 10px;
  padding: 12px 16px;
  border-radius: 12px;
  cursor: pointer;
  font: inherit;
}
@media (display-mode: standalone) {
  .install-btn { display: none !important; }
}
