:root {
  --bg: #0d0f14;
  --bg-2: #151823;
  --bg-3: #1d2130;
  --bubble-user: #2f6df6;
  --bubble-ai: #1d2130;
  --text: #e8eaf0;
  --muted: #8b91a5;
  --accent: #7c5cff;
  --accent-2: #4dd0c4;
  --danger: #ff6b6b;
  --radius: 18px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", "Noto Kufi Arabic", "Noto Sans Arabic", Tahoma, sans-serif;
  overscroll-behavior: none;
}

.screen { height: 100dvh; display: flex; flex-direction: column; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; margin-top: 10px; }

/* ---------- gate & setup ---------- */
#gate, #setup {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 70% -10%, rgba(124,92,255,.18), transparent),
              radial-gradient(800px 500px at 10% 110%, rgba(77,208,196,.10), transparent),
              var(--bg);
}
.gate-card { width: 100%; max-width: 380px; text-align: center; display: flex; flex-direction: column; gap: 14px; }
.avatar-logo {
  width: 110px; height: 110px; margin: 0 auto;
  border-radius: 28px; object-fit: cover;
  box-shadow: 0 8px 30px rgba(124,92,255,.45);
  border: 2px solid rgba(124,92,255,.5);
}
.topbar-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  object-fit: cover; vertical-align: middle;
  border: 1.5px solid var(--accent);
}
.msg.ai .bubble-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  object-fit: cover; flex-shrink: 0; margin-inline-start: 8px;
  border: 1.5px solid var(--accent); align-self: flex-end;
}
h1 { font-size: 26px; font-weight: 700; }

.btn {
  border: 0; border-radius: 14px; padding: 14px 18px;
  font-size: 16px; font-family: inherit; cursor: pointer;
  transition: transform .05s ease, opacity .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: linear-gradient(135deg, var(--accent), #5f8bff); color: white; font-weight: 600; }
.btn-ghost { background: var(--bg-3); color: var(--text); }
.small-btn { padding: 8px 12px; font-size: 13px; }

.pin-form { display: flex; gap: 10px; }
.pin-form input {
  flex: 1; min-width: 0;
  background: var(--bg-2); border: 1px solid #2a2f42; border-radius: 14px;
  color: var(--text); font-size: 18px; padding: 14px;
  text-align: center; letter-spacing: 4px; outline: none;
  font-family: inherit;
}
.pin-form input:focus { border-color: var(--accent); }

.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: #2a2f42; }

.btn-voice {
  position: relative; background: var(--bg-2); color: var(--text);
  border: 1px solid #2a2f42; display: flex; align-items: center; justify-content: center; gap: 12px;
}
.mic-ring {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent-2);
}
.btn-voice.recording .mic-ring { animation: pulse 1s infinite; background: var(--danger); }
@keyframes pulse { 50% { transform: scale(1.5); opacity: .6; } }

/* ---------- chat ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: rgba(13,15,20,.85); backdrop-filter: blur(12px);
  border-bottom: 1px solid #1c2030;
  position: relative; z-index: 5;
}
.topbar-title { flex: 1; text-align: center; font-weight: 600; font-size: 17px; display: flex; align-items: center; justify-content: center; gap: 8px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-inline-start: 6px; }
.dot.on { background: #4ade80; }
.dot.off { background: var(--danger); }

.icon-btn {
  background: none; border: 0; color: var(--text);
  width: 40px; height: 40px; border-radius: 12px;
  font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: var(--bg-3); }

.drawer {
  position: fixed; top: 0; bottom: 0; right: 0;
  width: min(84vw, 320px);
  background: var(--bg-2); z-index: 20;
  transform: translateX(100%); transition: transform .22s ease;
  display: flex; flex-direction: column;
  border-inline-start: 1px solid #2a2f42;
}
.drawer.open { transform: translateX(0); }
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 15; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; padding-top: calc(14px + env(safe-area-inset-top));
  font-weight: 600; border-bottom: 1px solid #2a2f42;
}
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  display: flex; align-items: center; gap: 8px;
  padding: 12px; border-radius: 12px; cursor: pointer;
  color: var(--text);
}
.conv-item:hover, .conv-item.active { background: var(--bg-3); }
.conv-item .t { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.conv-item .del { color: var(--muted); border: 0; background: none; font-size: 14px; cursor: pointer; padding: 4px; }
.drawer-foot { padding: 12px; border-top: 1px solid #2a2f42; }

.messages {
  flex: 1; overflow-y: auto;
  padding: 16px 12px 8px;
  display: flex; flex-direction: column; gap: 4px;
  -webkit-overflow-scrolling: touch;
}
.msg { display: flex; margin-bottom: 10px; animation: rise .18s ease; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.msg .bubble {
  max-width: 82%;
  padding: 11px 15px;
  border-radius: var(--radius);
  line-height: 1.75;
  font-size: 16px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user { justify-content: flex-start; }
.msg.user .bubble {
  background: var(--bubble-user); color: white;
  border-bottom-left-radius: 6px;
}
.msg.ai { justify-content: flex-end; }
.msg.ai .bubble {
  background: var(--bubble-ai);
  border-bottom-right-radius: 6px;
}
.msg.ai .bubble .cursor {
  display: inline-block; width: 8px; height: 17px;
  background: var(--accent-2); vertical-align: text-bottom;
  animation: blink 0.8s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.msg .time { font-size: 11px; color: var(--muted); align-self: flex-end; padding: 0 6px; }
.msg.system-note .bubble {
  background: transparent; color: var(--muted); font-size: 13px; text-align: center;
  max-width: 100%; margin: 0 auto;
}
.msg.system-note { justify-content: center; }

/* ---------- composer ---------- */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: rgba(13,15,20,.9); backdrop-filter: blur(12px);
  border-top: 1px solid #1c2030;
}
.composer textarea {
  flex: 1; min-width: 0;
  background: var(--bg-2); color: var(--text);
  border: 1px solid #2a2f42; border-radius: 20px;
  padding: 11px 16px; font-size: 16px; font-family: inherit;
  resize: none; outline: none; max-height: 132px; line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); }
.mic-btn { background: var(--bg-3); flex-shrink: 0; }
.mic-btn.live { background: var(--danger); color: white; animation: pulse 1.2s infinite; }
.send-btn { background: var(--accent); color: white; flex-shrink: 0; }
.send-btn:disabled { opacity: .4; }

/* ---------- voice HUD ---------- */
.voice-hud {
  position: absolute; left: 0; right: 0; bottom: 84px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 18px; z-index: 8; pointer-events: none;
}
.voice-hud .btn { pointer-events: auto; }
.pulse-ring {
  width: 64px; height: 64px; border-radius: 50%;
  background: radial-gradient(circle, rgba(124,92,255,.9), rgba(124,92,255,.2));
  animation: hud 1.4s ease-in-out infinite;
}
.pulse-ring.speaking { background: radial-gradient(circle, rgba(77,208,196,.9), rgba(77,208,196,.2)); }
@keyframes hud { 50% { transform: scale(1.25); } }
#voice-hud-text { font-size: 15px; color: var(--text); text-shadow: 0 1px 6px rgba(0,0,0,.6); }
