@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600&family=Inter:wght@400;500;600&display=swap');

/* ============ Tokens (same palette as the desktop app) ============ */
:root {
  --bg: #08090d;
  --surface: #101219;
  --surface-raised: #171a24;
  --line: rgba(255, 255, 255, 0.07);
  --line-strong: rgba(255, 255, 255, 0.12);

  --ink: #eef0f6;
  --ink-dim: #8a8fa3;
  --ink-faint: #4d5163;

  --core: #9b8cff;
  --core-deep: #4b3fc7;
  --core-soft: #9b8cff22;
  --core-glow: #9b8cff4d;

  --online: #6bd9b8;
  --danger: #e2685f;
  --danger-glow: #e2685f4d;

  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 24px;

  --font-display: 'Space Grotesk', sans-serif;
  --font-body: 'Inter', sans-serif;

  /* iOS safe-area insets (notch / Dynamic Island / home indicator). Falls
     back to 0 on platforms without env(safe-area-inset-*). */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
:focus-visible { outline: 2px solid var(--core); outline-offset: 2px; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  overflow: hidden;
  /* Prevent iOS Safari's rubber-band bounce from revealing white edges. */
  overscroll-behavior: none;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

.app {
  display: flex;
  flex-direction: column;
  /* 100dvh accounts for mobile browser chrome + the iOS keyboard resizing
     the visual viewport; 100vh is the fallback for browsers without
     dvh support. */
  height: 100vh;
  height: 100dvh;
  background:
    radial-gradient(ellipse 700px 420px at 50% 8%, #9b8cff10, transparent 65%),
    radial-gradient(ellipse 500px 300px at 50% 100%, #4b3fc70d, transparent 70%),
    var(--bg);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.app.setup-active { filter: blur(4px) saturate(0.7); pointer-events: none; }

/* ============ Top bar ============ */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(18px + var(--safe-top)) 22px 14px;
  flex-shrink: 0;
}

.identity { display: flex; align-items: center; gap: 11px; }
.identity-text { display: flex; flex-direction: column; line-height: 1.3; }

.name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.03em;
  color: var(--ink);
}

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: lowercase;
  color: var(--ink-faint);
}

.status::before {
  content: '';
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 6px currentColor;
  flex-shrink: 0;
  transition: background 0.2s ease, box-shadow 0.2s ease;
}

.status.online { color: var(--online); }
.status.error { color: var(--danger); }

.topbar-actions { display: flex; gap: 2px; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--ink-faint);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn:hover { background: var(--surface-raised); color: var(--ink); }

.icon {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ============ Orb ============ */

.orb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  flex-shrink: 0;
  position: relative;
  background: radial-gradient(circle at 34% 28%, #cbc2ff, var(--core) 52%, var(--core-deep) 100%);
  box-shadow: 0 0 10px var(--core-glow);
  animation: breathe 4.2s ease-in-out infinite;
}

.orb.listening {
  animation: breathe 1.8s ease-in-out infinite !important;
  box-shadow: 0 0 18px #9b8cff4d, 0 0 35px #9b8cff22 !important;
}

.orb.thinking {
  animation: think-pulse 1.6s ease-in-out infinite;
  box-shadow: 0 0 16px var(--core-glow), 0 0 34px var(--core-glow);
}

.orb.speaking {
  animation: speak-pulse 0.9s ease-in-out infinite;
  box-shadow: 0 0 20px var(--core-glow);
}

.orb.error {
  background: radial-gradient(circle at 34% 28%, #ffc9c4, var(--danger) 58%, #8f342c 100%);
  box-shadow: 0 0 12px var(--danger-glow);
  animation: error-throb 2.6s ease-in-out infinite;
}

.orb-lg { width: 88px; height: 88px; margin: 0 auto 20px; box-shadow: 0 0 46px var(--core-glow); }

.orb-lg::after {
  content: '';
  position: absolute;
  inset: -14px;
  border-radius: 50%;
  border: 1px solid transparent;
  opacity: 0;
  transition: opacity 0.4s ease;
}

.orb-lg.thinking::after {
  opacity: 1;
  border-top-color: var(--core-glow);
  border-right-color: transparent;
  animation: orbit 2.4s linear infinite;
}

.orb-lg.listening::after {
  opacity: 1;
  border-color: var(--core-glow);
  animation: ring-breathe 1.8s ease-in-out infinite;
}

@keyframes breathe { 0%, 100% { transform: scale(1); opacity: 0.9; } 50% { transform: scale(1.08); opacity: 1; } }
@keyframes think-pulse { 0%, 100% { transform: scale(1); opacity: 0.95; } 50% { transform: scale(1.14); opacity: 1; } }
@keyframes speak-pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.2); } }
@keyframes error-throb { 0%, 100% { transform: scale(1); opacity: 0.9; } 50% { transform: scale(1.05); opacity: 1; } }
@keyframes orbit { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
@keyframes ring-breathe { 0%, 100% { transform: scale(1); opacity: 0.5; } 50% { transform: scale(1.06); opacity: 0.9; } }

/* ============ Conversation ============ */

.conversation {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 0 28px;
  display: flex;
  flex-direction: column;
  mask-image: linear-gradient(to bottom, transparent 0, black 56px, black calc(100% - 8px), transparent 100%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, black 56px, black calc(100% - 8px), transparent 100%);
}

.empty-state { margin: auto; text-align: center; padding: 40px 32px; }

.empty-tagline {
  font-family: var(--font-display);
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 22px;
}

.empty-greeting {
  font-size: 16.5px;
  color: var(--ink-dim);
  line-height: 1.5;
  max-width: 320px;
  margin: 0 auto;
}

.msg-row {
  padding: 9px 26px;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  animation: rise-in 0.35s ease both;
}

@keyframes rise-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.bubble {
  font-size: 14.5px;
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg-row.assistant { text-align: left; }

.msg-row.assistant .bubble {
  color: var(--ink);
  position: relative;
  padding-left: 15px;
}

.msg-row.assistant .bubble::before {
  content: '';
  position: absolute;
  left: 0;
  top: 8px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--core);
  box-shadow: 0 0 5px var(--core-glow);
}

.msg-row.user { text-align: right; }

.msg-row.user .bubble {
  color: var(--ink-dim);
  font-size: 13.5px;
  display: inline-block;
  white-space: pre-wrap;
}

.bubble.error-bubble { color: var(--danger); }
.msg-row.assistant .bubble.error-bubble::before { background: var(--danger); box-shadow: 0 0 5px var(--danger-glow); }

/* Markdown content inside assistant bubbles */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h3, .bubble h4, .bubble h5, .bubble h6 {
  font-family: var(--font-display);
  font-weight: 600;
  margin: 16px 0 8px;
  color: var(--ink);
}
.bubble h3:first-child, .bubble h4:first-child { margin-top: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble strong { color: var(--ink); font-weight: 600; }
.bubble em { font-style: italic; }
.bubble code {
  background: var(--surface-raised);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: 'SF Mono', ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.88em;
}
.bubble pre {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0 0 10px;
  -webkit-overflow-scrolling: touch;
}
.bubble pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.85em;
  line-height: 1.55;
  white-space: pre;
}
.bubble a { color: var(--core); text-decoration: underline; text-decoration-color: var(--core-soft); text-underline-offset: 2px; }
.bubble a:hover { text-decoration-color: var(--core); }

/* Streaming cursor flourish on the currently-growing assistant message */
.msg-row.assistant.streaming .bubble::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 14px;
  margin-left: 2px;
  background: var(--core);
  vertical-align: text-bottom;
  animation: cursor-blink 0.9s steps(2) infinite;
  border-radius: 1px;
}
@keyframes cursor-blink { 50% { opacity: 0; } }

/* ============ Composer ============ */

.composer {
  flex-shrink: 0;
  padding: 10px 22px calc(24px + var(--safe-bottom));
}

#composerForm {
  max-width: 620px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 12px 12px 12px 20px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#composerForm:focus-within { border-color: var(--core-soft); box-shadow: 0 0 0 3px var(--core-soft), 0 0 22px var(--core-glow); }

#messageInput {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px; /* >= 16px prevents iOS Safari auto-zoom on focus */
  line-height: 1.5;
  max-height: 160px;
  padding: 5px 0;
}

#messageInput::placeholder { color: var(--ink-faint); }

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  border: none;
  background: var(--core);
  color: #100d1c;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s ease, transform 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.send-btn:disabled { opacity: 0; transform: scale(0.7); pointer-events: none; }
.send-btn:not(:disabled):hover { transform: translateY(-1px); }
.send-btn .icon { stroke: #100d1c; }

/* ============ Settings panel ============ */

.settings-panel {
  position: fixed;
  top: 0;
  right: -340px;
  width: min(320px, 88vw);
  height: 100%;
  height: 100dvh;
  background: rgba(16, 18, 25, 0.92);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-left: 1px solid var(--line);
  padding: calc(22px + var(--safe-top)) 22px calc(22px + var(--safe-bottom));
  transition: right 0.3s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 20;
  overflow-y: auto;
}

.settings-panel.open { right: 0; }

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 13.5px;
  color: var(--ink);
  margin-bottom: 26px;
}

.settings-section { margin-bottom: 24px; }

.settings-section label { display: block; font-size: 11px; color: var(--ink-faint); letter-spacing: 0.05em; margin-bottom: 9px; }
.settings-section p.hint { font-size: 12px; color: var(--ink-faint); line-height: 1.5; margin: 6px 0 0; }

#modelSelect {
  width: 100%;
  background: var(--surface-raised);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  font-size: 13px;
  font-family: var(--font-body);
}

.connection-detail { font-size: 12px; color: var(--ink-faint); margin-bottom: 10px; word-break: break-word; line-height: 1.5; }

.secondary-btn {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--line);
  color: var(--ink-dim);
  padding: 10px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease;
  margin-top: 8px;
}
.secondary-btn:hover { border-color: var(--core-soft); color: var(--ink); }
.secondary-btn.danger:hover { border-color: var(--danger-glow); color: var(--danger); }

.app-info { font-size: 12px; color: var(--ink-faint); line-height: 1.6; }

.scrim {
  position: fixed;
  inset: 0;
  background: #00000066;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 10;
}
.scrim.visible { opacity: 1; pointer-events: auto; }

/* ============ Setup overlay (first run) ============ */

.setup-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
  padding-bottom: calc(24px + var(--safe-bottom));
  background: rgba(8, 9, 13, 0.88);
  backdrop-filter: blur(6px);
}
.setup-overlay.visible { display: flex; }

.setup-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 28px 26px;
}

.setup-card .orb { margin: 0 auto 16px; width: 44px; height: 44px; }

.setup-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 17px;
  text-align: center;
  margin: 0 0 4px;
}

.setup-subtitle {
  font-size: 13px;
  color: var(--ink-dim);
  text-align: center;
  line-height: 1.5;
  margin: 0 0 22px;
}

.setup-field { margin-bottom: 14px; }
.setup-field label { display: block; font-size: 11.5px; color: var(--ink-faint); letter-spacing: 0.04em; margin-bottom: 7px; }

.setup-field input {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  color: var(--ink);
  font-size: 16px; /* prevent iOS zoom-on-focus */
  font-family: var(--font-body);
}
.setup-field input:focus { outline: none; border-color: var(--core-soft); box-shadow: 0 0 0 3px var(--core-soft); }

.setup-error {
  display: none;
  color: var(--danger);
  font-size: 12.5px;
  line-height: 1.5;
  margin: 4px 0 14px;
}

.setup-submit {
  width: 100%;
  background: var(--core);
  color: #100d1c;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
  transition: opacity 0.15s ease;
}
.setup-submit:disabled { opacity: 0.6; cursor: default; }

.setup-hint {
  font-size: 11.5px;
  color: var(--ink-faint);
  line-height: 1.55;
  margin-top: 16px;
  text-align: center;
}

input[type="checkbox"] { accent-color: var(--core); width: 16px; height: 16px; }

/* ============ Scrollbar ============ */

.conversation::-webkit-scrollbar { width: 6px; }
.conversation::-webkit-scrollbar-thumb { background: var(--surface-raised); border-radius: 8px; }

/* ============ Responsive ============ */

@media (max-width: 420px) {
  .topbar { padding: calc(16px + var(--safe-top)) 16px 12px; }
  .msg-row { padding: 8px 16px; }
  .composer { padding: 8px 16px calc(18px + var(--safe-bottom)); }
  .orb-lg { width: 72px; height: 72px; }
}

/* ============ Startup animation ============ */

.startup-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  opacity: 1;
  visibility: visible;
  transition: opacity 0.55s ease, visibility 0.55s ease;
}
.startup-overlay.finished { opacity: 0; visibility: hidden; pointer-events: none; }

.startup-orb {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 28%, #cbc2ff, var(--core) 52%, var(--core-deep) 100%);
  box-shadow: 0 0 24px var(--core-glow), 0 0 70px var(--core-glow);
  animation: startup-orb 1.5s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.startup-name {
  margin-top: 24px;
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 0.12em;
  color: var(--ink);
  animation: startup-name 1.2s ease both;
}

@keyframes startup-orb {
  0% { opacity: 0; transform: scale(0.45); box-shadow: 0 0 0 transparent, 0 0 0 transparent; }
  45% { opacity: 1; transform: scale(1.08); box-shadow: 0 0 30px var(--core-glow), 0 0 90px var(--core-glow); }
  75% { transform: scale(0.98); }
  100% { opacity: 1; transform: scale(1); box-shadow: 0 0 18px var(--core-glow), 0 0 55px var(--core-glow); }
}

@keyframes startup-name {
  0% { opacity: 0; transform: translateY(8px); }
  45% { opacity: 0; transform: translateY(8px); }
  100% { opacity: 1; transform: translateY(0); }
}
