/* ─── design tokens ────────────────────────────────────────────────────────── */
:root {
  --primary:         #2563EB;
  --primary-dark:    #1D4ED8;
  --primary-light:   #EFF6FF;
  --primary-border:  #BFDBFE;

  --text:            #111827;
  --text-secondary:  #6B7280;
  --text-inverse:    #FFFFFF;

  --bg:              #F3F4F6;
  --surface:         #FFFFFF;
  --border:          #E5E7EB;

  --rilo-avatar-bg:  #2563EB;
  --rilo-bubble:     #EFF6FF;
  --rilo-bubble-border: #BFDBFE;

  --user-bubble:     #2563EB;

  --error-bg:        #FEF2F2;
  --error-border:    #FECACA;
  --error-text:      #B91C1C;

  --radius:          12px;
  --radius-sm:       8px;
  --shadow:          0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md:       0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.05);
}

/* ─── reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html { font-size: 16px; -webkit-text-size-adjust: 100%; overflow-x: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  max-width: 100vw;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
}
button:disabled { opacity: .5; cursor: default; }
textarea { font: inherit; resize: none; }

/* ─── header ───────────────────────────────────────────────────────────────── */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  box-shadow: var(--shadow);
  z-index: 10;
}

.header-logo {
  display: flex;
  align-items: center;
  gap: 8px;
}
.logo-icon { font-size: 22px; }
.logo-text {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -.3px;
}

.header-topic {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  min-width: 0;
  overflow: hidden;
}

.btn-ghost {
  font-size: 13px;
  color: var(--primary);
  padding: 4px 10px;
  border-radius: 6px;
  transition: background .15s;
}
.btn-ghost:hover { background: var(--primary-light); }

/* ─── main ─────────────────────────────────────────────────────────────────── */
.app-main {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ─── topic picker ─────────────────────────────────────────────────────────── */
.topic-picker {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 32px;
}

.topic-intro {
  text-align: center;
  color: var(--text-secondary);
  font-size: 15px;
  margin-bottom: 20px;
}

.topic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
  gap: 12px;
  max-width: 680px;
  margin: 0 auto;
}

.topic-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 14px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
  transition: transform .12s, box-shadow .12s, border-color .12s;
  box-shadow: var(--shadow);
}
.topic-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--primary-border);
}
.topic-card:active { transform: translateY(0); }

.topic-card-emoji { font-size: 28px; line-height: 1; }
.topic-card-label { font-size: 13px; font-weight: 600; color: var(--text); }
.topic-card-hint  { font-size: 11px; color: var(--text-secondary); line-height: 1.3; }

.topic-section-label {
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin: 24px auto 12px;
  max-width: 680px;
}
.topic-section-label--situations {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
  color: var(--primary);
}
.topic-card--situations {
  border-color: color-mix(in srgb, var(--primary) 30%, var(--border));
}
.topic-card--situations:hover {
  border-color: var(--primary);
}

/* ─── conversation ─────────────────────────────────────────────────────────── */
.conversation {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

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

/* ─── message rows ─────────────────────────────────────────────────────────── */
.msg {
  display: flex;
  gap: 8px;
  max-width: 88%;
  animation: fadeUp .2s ease;
}

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

/* Rilo (left) */
.msg-rilo { align-self: flex-start; align-items: flex-end; }

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--rilo-avatar-bg);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.msg-rilo .msg-bubble {
  background: var(--rilo-bubble);
  border: 1px solid var(--rilo-bubble-border);
  border-radius: 4px 14px 14px 14px;
}

/* User (right) */
.msg-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-user .msg-bubble {
  background: var(--user-bubble);
  color: #fff;
  border-radius: 14px 4px 14px 14px;
}

/* Bubble common */
.msg-bubble {
  padding: 10px 13px;
  font-size: 14.5px;
  line-height: 1.6;
  max-width: 100%;
  box-shadow: var(--shadow);
}

.msg-content {
  white-space: pre-wrap;
  word-break: break-word;
}

/* language badge on user messages */
.msg-lang-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  margin-top: 6px;
  opacity: .75;
}
.msg-lang-ar { background: rgba(255,255,255,.25); color: #fff; }
.msg-lang-en { background: rgba(255,255,255,.25); color: #fff; }

/* play button on Rilo messages */
.msg-actions { margin-top: 6px; display: flex; gap: 4px; }

.msg-play-btn {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--primary-light);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
}
.msg-play-btn:hover { background: var(--primary-border); }
.msg-play-btn svg { width: 14px; height: 14px; }
.msg-play-btn.playing { background: var(--primary); color: #fff; }
.msg-play-btn.playing svg path { d: path('M6 19h4V5H6v14zm8-14v14h4V5h-4z'); }

/* error inline */
.msg-error {
  align-self: center;
  font-size: 13px;
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  max-width: 90%;
  text-align: center;
}

/* ─── typing indicator ─────────────────────────────────────────────────────── */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px 12px;
}

.typing-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--rilo-avatar-bg);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.typing-dots {
  background: var(--rilo-bubble);
  border: 1px solid var(--rilo-bubble-border);
  border-radius: 4px 14px 14px 14px;
  padding: 10px 14px;
  display: flex;
  gap: 5px;
  align-items: center;
  box-shadow: var(--shadow);
}

.typing-dots span {
  width: 7px;
  height: 7px;
  background: var(--primary);
  border-radius: 50%;
  opacity: .4;
  animation: blink 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }

@keyframes blink {
  0%, 80%, 100% { opacity: .4; transform: scale(1); }
  40%           { opacity: 1;  transform: scale(1.15); }
}

/* ─── input bar ────────────────────────────────────────────────────────────── */
.input-bar {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
  box-shadow: 0 -1px 4px rgba(0,0,0,.06);
}

/* language toggle */
.lang-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.lang-btn {
  font-size: 11px;
  font-weight: 700;
  padding: 6px 9px;
  color: var(--text-secondary);
  background: var(--surface);
  transition: background .15s, color .15s;
}
.lang-btn.active {
  background: var(--primary);
  color: #fff;
}

/* mic button */
.mic-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary-light);
  color: var(--primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 2px solid transparent;
  transition: background .15s, border-color .15s, transform .1s;
  position: relative;
  gap: 2px;
}
.mic-btn:hover { background: var(--primary-border); }

.mic-icon { width: 18px; height: 18px; }
.mic-label { font-size: 9px; font-weight: 600; line-height: 1; }

.mic-btn.recording {
  background: #FEF2F2;
  border-color: #EF4444;
  color: #EF4444;
  animation: pulse-ring 1s infinite;
}

@keyframes pulse-ring {
  0%   { box-shadow: 0 0 0 0 rgba(239,68,68,.3); }
  70%  { box-shadow: 0 0 0 8px rgba(239,68,68,0); }
  100% { box-shadow: 0 0 0 0 rgba(239,68,68,0); }
}

/* text input */
.text-input-wrap {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  transition: border-color .15s;
}
.text-input-wrap:focus-within { border-color: var(--primary); }

.text-input-wrap textarea {
  background: transparent;
  border: none;
  outline: none;
  width: 100%;
  font-size: 14px;
  color: var(--text);
  line-height: 1.5;
  max-height: 120px;
  overflow-y: auto;
}
.text-input-wrap textarea::placeholder { color: var(--text-secondary); }

/* send button */
.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
  box-shadow: var(--shadow);
}
.send-btn:hover { background: var(--primary-dark); }
.send-btn:active { transform: scale(.94); }
.send-btn svg { width: 18px; height: 18px; }

/* audio toggle */
.audio-toggle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color .15s;
}
.audio-toggle:hover { color: var(--primary); }
.audio-toggle svg { width: 20px; height: 20px; }

/* ─── login screen ─────────────────────────────────────────────────────────── */
.login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 32px;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.login-logo-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 28px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

.login-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -.3px;
}

.login-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 8px;
}

.google-btn-wrap {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-error {
  font-size: 13px;
  color: var(--error-text);
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  width: 100%;
}

/* ─── header user info ──────────────────────────────────────────────────────── */
.header-user {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.user-avatar-img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.user-avatar-initial {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.user-name {
  font-size: 13px;
  color: var(--text-secondary);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 480px) { .user-name { display: none; } }

/* ─── no speech notice ──────────────────────────────────────────────────────── */
.no-speech-notice {
  background: #FFFBEB;
  border-top: 1px solid #FDE68A;
  padding: 10px 16px;
  font-size: 13px;
  color: #92400E;
  text-align: center;
  flex-shrink: 0;
}

/* ─── scrollbar (Webkit) ────────────────────────────────────────────────────── */
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ─── responsive ────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  /* header */
  .app-header { padding: 0 10px; gap: 6px; }
  .logo-text { font-size: 15px; }
  .header-topic { gap: 4px; font-size: 13px; }
  /* hide the long topic label text; keep emoji + button */
  .header-topic > span:not(.btn-ghost):last-of-type { display: none; }
  #headerTopicLabel { display: none; }
  .btn-ghost { font-size: 12px; padding: 4px 7px; }

  /* input bar */
  .input-bar { padding: 8px 8px; gap: 6px; }
  .lang-toggle { display: none; }
  .mic-btn { width: 40px; height: 40px; }
  .send-btn { width: 40px; height: 40px; }

  /* messages & topics */
  .msg { max-width: 95%; }
  .topic-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .topic-card { padding: 14px 10px 12px; }
  .topic-card-emoji { font-size: 24px; }

  /* login card */
  .login-card { padding: 28px 20px; }
}

@media (max-width: 360px) {
  .logo-text { display: none; }
}

@media (min-width: 768px) {
  .messages { padding: 20px 60px; }
  .topic-picker { padding: 40px 24px 40px; }
  .topic-grid { max-width: 720px; }
  .input-bar { padding: 12px 60px; }
}
