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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #232342;
  --primary: #4f6ef7;
  --primary-dark: #3a56d4;
  --text: #e8e8f0;
  --text-dim: #8888aa;
  --msg-own: #4f6ef7;
  --msg-other: #2a2a4a;
  --danger: #e74c5f;
  --success: #4caf7d;
  --radius: 16px;
  --sat: env(safe-area-inset-top);
  --sar: env(safe-area-inset-right);
  --sab: env(safe-area-inset-bottom);
  --sal: env(safe-area-inset-left);
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  font-size: 16px;
}

/* --- Auth Screen --- */
.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}
.screen.active {
  display: flex;
}

#auth-screen {
  justify-content: center;
  align-items: center;
  padding: 24px;
  padding-top: max(24px, var(--sat));
  padding-left: max(24px, var(--sal));
  padding-right: max(24px, var(--sar));
  padding-bottom: max(24px, var(--sab));
}

.auth-box {
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.auth-box h1 {
  font-size: 28px;
  margin-bottom: 8px;
  font-weight: 700;
}

.auth-box p {
  color: var(--text-dim);
  margin-bottom: 32px;
  font-size: 14px;
}

.input-group {
  margin-bottom: 16px;
}

.input-group input {
  width: 100%;
  padding: 14px 16px;
  background: var(--surface);
  border: 2px solid var(--surface2);
  border-radius: 12px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

.input-group input:focus {
  border-color: var(--primary);
}

.input-group input::placeholder {
  color: var(--text-dim);
}

.btn {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:active {
  background: var(--primary-dark);
  transform: scale(0.98);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
}

.error-text {
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
  min-height: 20px;
}

/* --- Chat Screen --- */
#chat-screen {
  flex-direction: column;
  height: 100%;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: max(12px, var(--sat));
  padding-left: max(16px, var(--sal));
  padding-right: max(16px, var(--sar));
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  min-height: 56px;
  flex-shrink: 0;
}

.chat-header .title {
  font-size: 18px;
  font-weight: 700;
}

.chat-header .user-info {
  font-size: 12px;
  color: var(--text-dim);
}

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

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--surface2);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
.icon-btn:active {
  background: var(--primary);
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  padding-left: max(16px, var(--sal));
  padding-right: max(16px, var(--sar));
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 4px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.load-more-wrapper {
  text-align: center;
  padding: 8px 0;
}

.load-more-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 20px;
  background: var(--surface2);
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
}
.load-more-btn:active {
  background: var(--primary);
  color: #fff;
}

.message {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--radius);
  word-wrap: break-word;
  position: relative;
  animation: fadeIn 0.2s ease;
}

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

.message.own {
  align-self: flex-end;
  background: var(--msg-own);
  border-bottom-right-radius: 4px;
}

.message.other {
  align-self: flex-start;
  background: var(--msg-other);
  border-bottom-left-radius: 4px;
}

.message .msg-username {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
  opacity: 0.8;
}

.message.own .msg-username {
  display: none;
}

.message .msg-text {
  font-size: 15px;
  line-height: 1.4;
}

.message .msg-time {
  font-size: 11px;
  opacity: 0.5;
  text-align: right;
  margin-top: 4px;
}

/* --- Voice Message Player --- */
.voice-player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
}

.voice-play-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s;
}
.voice-play-btn:active {
  background: rgba(255, 255, 255, 0.3);
}

.voice-waveform {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.voice-bars {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 28px;
  cursor: pointer;
  position: relative;
}

.voice-bar {
  flex: 1;
  background: rgba(255, 255, 255, 0.25);
  border-radius: 2px;
  min-width: 3px;
  transition: background 0.15s;
}

.voice-bar.played {
  background: rgba(255, 255, 255, 0.8);
}

.message.other .voice-bar {
  background: rgba(255, 255, 255, 0.2);
}
.message.other .voice-bar.played {
  background: var(--primary);
}

.voice-time {
  font-size: 11px;
  opacity: 0.6;
  font-variant-numeric: tabular-nums;
}

/* Hide native audio element (not display:none — iOS won't load it) */
.message audio {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  pointer-events: none;
}

/* --- Input Bar --- */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 4px 8px;
  padding-left: max(8px, var(--sal));
  padding-right: max(8px, var(--sar));
  background: transparent;
  flex-shrink: 0;
}

.input-bar textarea {
  flex: 1;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--surface2);
  border-radius: 20px;
  color: var(--text);
  font-size: 16px;
  resize: none;
  outline: none;
  max-height: 120px;
  min-height: 42px;
  line-height: 1.4;
  font-family: inherit;
}

.input-bar textarea:focus {
  border-color: var(--primary);
}

.input-bar textarea::placeholder {
  color: var(--text-dim);
}

.send-btn, .voice-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  transition: all 0.2s;
}

.send-btn {
  background: var(--primary);
  color: #fff;
  display: none;
}
.send-btn.visible {
  display: flex;
}
.send-btn:active {
  transform: scale(0.9);
}

.voice-btn {
  background: var(--surface2);
  color: var(--text);
  display: flex;
}
.voice-btn.hidden {
  display: none;
}
.voice-btn.recording {
  background: var(--danger);
  color: #fff;
  animation: pulse 1s infinite;
}

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

/* --- Settings Screen --- */
#settings-screen {
  flex-direction: column;
  height: 100%;
}

.settings-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, var(--sat));
  padding-left: max(16px, var(--sal));
  padding-right: max(16px, var(--sar));
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  min-height: 56px;
  flex-shrink: 0;
}

.settings-header h2 {
  font-size: 18px;
  font-weight: 700;
}

.settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  padding-left: max(16px, var(--sal));
  padding-right: max(16px, var(--sar));
  padding-bottom: max(20px, var(--sab));
}

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

.settings-section h3 {
  font-size: 14px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 12px;
}

.settings-section .input-group {
  margin-bottom: 12px;
}

.settings-section label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.settings-msg {
  font-size: 13px;
  margin-top: 8px;
  min-height: 20px;
}
.settings-msg.success { color: var(--success); }
.settings-msg.error { color: var(--danger); }

/* --- Username Screen --- */
#username-screen {
  justify-content: center;
  align-items: center;
  padding: 24px;
  padding-top: max(24px, var(--sat));
  padding-left: max(24px, var(--sal));
  padding-right: max(24px, var(--sar));
  padding-bottom: max(24px, var(--sab));
}

/* --- Scrollbar --- */
::-webkit-scrollbar {
  width: 4px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--surface2);
  border-radius: 2px;
}

/* Date separator */
.date-separator {
  text-align: center;
  padding: 8px 0;
  font-size: 12px;
  color: var(--text-dim);
}

.date-separator span {
  background: var(--surface2);
  padding: 4px 12px;
  border-radius: 10px;
}

/* Recording overlay */
.recording-indicator {
  display: none;
  position: fixed;
  bottom: calc(70px + var(--sab));
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  z-index: 100;
  animation: fadeIn 0.2s ease;
}
.recording-indicator.active {
  display: block;
}
