:root {
  --bg: #0f1118;
  --bg-card: #1a1d2e;
  --bg-input: #232640;
  --bg-bubble-mine: #2563eb;
  --bg-bubble-partner: #1e293b;
  --bg-header: rgba(15, 17, 24, 0.92);
  --text: #e2e8f0;
  --text-dim: #64748b;
  --text-bubble: #ffffff;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --green: #22c55e;
  --red: #ef4444;
  --yellow: #eab308;
  --border: #2a2d3e;
  --radius: 16px;
  --radius-sm: 10px;
  --radius-xs: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 500px;
  margin: 0 auto;
  min-height: 100dvh;
  position: relative;
}

/* ===== SCREENS ===== */
.screen { display: none; flex-direction: column; min-height: 100dvh; }
.screen.active { display: flex; }

/* ===== HOME ===== */
.home {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 40px 24px;
  gap: 48px;
}

.home-logo {
  text-align: center;
}

.logo-icon {
  width: 72px;
  height: 72px;
  margin: 0 auto 16px;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  font-weight: 800;
  color: white;
  letter-spacing: -1px;
}

.home-logo h1 {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 4px;
  color: white;
}

.home-logo p {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-top: 6px;
}

.home-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.btn-primary, .btn-secondary {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 20px;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
  transition: all 0.2s ease;
  font-family: inherit;
}

.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.98); }

.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: var(--accent); }
.btn-secondary:active { transform: scale(0.98); }

.btn-primary div, .btn-secondary div {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.btn-primary strong, .btn-secondary strong {
  font-size: 0.95rem;
  font-weight: 600;
}

.btn-primary span, .btn-secondary span {
  font-size: 0.75rem;
  opacity: 0.7;
}

.btn-full {
  width: 100%;
  justify-content: center;
  padding: 16px;
  font-size: 1rem;
  font-weight: 600;
  margin-top: 8px;
}

.home-status {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* ===== FORM SCREENS ===== */
.form-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-header);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.form-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.btn-back {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  transition: color 0.2s;
}
.btn-back:hover { color: var(--accent); }

.form-body {
  flex: 1;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-body label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-top: 4px;
}

.form-body input, .form-body select {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  padding: 14px 16px;
  outline: none;
  transition: border-color 0.2s;
  -webkit-appearance: none;
}

.form-body input:focus, .form-body select:focus {
  border-color: var(--accent);
}

.form-body input::placeholder {
  color: var(--text-dim);
  opacity: 0.6;
}

.input-code {
  text-align: center;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 8px;
  text-transform: uppercase;
}

/* ===== WAITING ===== */
.waiting-body {
  flex: 1;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.room-code-box {
  text-align: center;
}

.room-code-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.room-code {
  display: block;
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: 10px;
  color: var(--accent);
  margin-top: 4px;
}

.qr-box { text-align: center; }

.qr-img {
  width: 200px;
  height: 200px;
  border-radius: var(--radius);
  background: white;
  padding: 8px;
}

.waiting-pulse {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.pulse-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--yellow);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.7); }
}

.share-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.share-box input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 0.8rem;
  padding: 12px 14px;
  outline: none;
  width: 100%;
}

.share-actions {
  display: flex;
  gap: 8px;
}

.btn-small {
  flex: 1;
  padding: 10px 16px;
  border: none;
  border-radius: var(--radius-xs);
  background: var(--accent);
  color: white;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-small:active { transform: scale(0.97); }

.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

/* ===== CHAT SCREEN ===== */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-header);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.chat-header-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.chat-header-info strong {
  font-size: 1rem;
  font-weight: 600;
}

.chat-status {
  font-size: 0.72rem;
  color: var(--green);
}

.chat-status.offline { color: var(--red); }
.chat-status.waiting { color: var(--yellow); }

.chat-header-actions {
  display: flex;
  gap: 8px;
}

.btn-icon-small {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
}
.btn-icon-small:hover { background: var(--bg-card); color: var(--text); }
.btn-icon-small.tts-off { color: var(--red); }

/* ===== CHAT MESSAGES ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar { width: 3px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.chat-welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-dim);
  gap: 16px;
  padding: 40px 20px;
}

.welcome-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--bg-card);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
}

.chat-welcome p {
  font-size: 0.9rem;
  line-height: 1.5;
}

/* ===== BUBBLES ===== */
.bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 18px;
  animation: bubbleIn 0.25s ease;
  position: relative;
}

@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(8px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.bubble-mine {
  align-self: flex-end;
  background: var(--bg-bubble-mine);
  color: var(--text-bubble);
  border-bottom-right-radius: 6px;
}

.bubble-partner {
  align-self: flex-start;
  background: var(--bg-bubble-partner);
  color: var(--text-bubble);
  border-bottom-left-radius: 6px;
}

.bubble-original {
  font-size: 0.72rem;
  opacity: 0.65;
  margin-bottom: 4px;
  line-height: 1.3;
}

.bubble-text {
  font-size: 0.95rem;
  line-height: 1.4;
  font-weight: 400;
}

.bubble-time {
  font-size: 0.62rem;
  opacity: 0.5;
  margin-top: 4px;
  text-align: right;
}

/* System messages */
.system-msg {
  align-self: center;
  background: var(--bg-card);
  color: var(--text-dim);
  font-size: 0.75rem;
  padding: 6px 14px;
  border-radius: 12px;
}

/* Typing indicator */
.bubble-typing {
  align-self: flex-end;
  background: rgba(37, 99, 235, 0.3);
  border: 1px solid rgba(37, 99, 235, 0.4);
  color: rgba(255,255,255,0.8);
  border-bottom-right-radius: 6px;
  padding: 10px 14px;
}

.typing-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.typing-dots {
  display: flex;
  gap: 3px;
  align-items: center;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(255,255,255,0.6);
  animation: typingBounce 1.2s ease-in-out infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-4px); }
}

.typing-text {
  font-size: 0.85rem;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== CHAT BOTTOM ===== */
.chat-bottom {
  border-top: 1px solid var(--border);
  background: var(--bg-header);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.mic-status {
  text-align: center;
}

#micStatusText {
  font-size: 0.75rem;
  color: var(--text-dim);
}

#micStatusText.listening {
  color: var(--green);
}

#micStatusText.translating {
  color: var(--accent);
}

.mic-area {
  display: flex;
  align-items: center;
  justify-content: center;
}

.mic-button {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.35);
  position: relative;
}

.mic-button:active { transform: scale(0.93); }

.mic-button.active {
  background: var(--red);
  box-shadow: 0 4px 20px rgba(239, 68, 68, 0.35);
  animation: micPulse 2s ease-in-out infinite;
}

.mic-button.active .mic-icon { display: none; }
.mic-button.active .stop-icon { display: block !important; }

@keyframes micPulse {
  0%, 100% { box-shadow: 0 4px 20px rgba(239, 68, 68, 0.35); }
  50% { box-shadow: 0 4px 30px rgba(239, 68, 68, 0.6), 0 0 0 12px rgba(239, 68, 68, 0.1); }
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 20px;
  font-size: 0.85rem;
  color: var(--text);
  box-shadow: var(--shadow);
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
  z-index: 100;
  white-space: nowrap;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== SAFE AREA ===== */
@supports (padding: env(safe-area-inset-bottom)) {
  .chat-bottom { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  .home { padding-bottom: calc(40px + env(safe-area-inset-bottom)); }
  .form-body { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
}

/* ===== RESPONSIVE ===== */
@media (max-height: 650px) {
  .chat-bottom { padding: 8px 16px 12px; gap: 8px; }
  .mic-button { width: 56px; height: 56px; }
}

::selection {
  background: rgba(59, 130, 246, 0.3);
  color: white;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
