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

:root {
  --bg-primary: #1a1a1a;
  --bg-secondary: #232323;
  --bg-tertiary: #2d2d2d;
  --bg-hover: #353535;
  --text-primary: #e8e0d5;
  --text-secondary: #9a9082;
  --text-muted: #6b6259;
  --accent: #d4845a;
  --accent-soft: rgba(212, 132, 90, 0.15);
  --border: #333;
  --green: #5ab87a;
  --red: #d45a5a;
  --radius: 12px;
  --radius-sm: 8px;
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: none;
  font-family: 'Inter', -apple-system, sans-serif;
  font-weight: 400;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

.screen {
  height: 100%;
}

.hidden {
  display: none !important;
}

/* ---- Auth Screen ---- */

#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg-primary);
}

.auth-container {
  width: 420px;
  padding: 56px 40px;
  background: var(--bg-secondary);
  border-radius: 20px;
  border: 1px solid var(--border);
  text-align: center;
  animation: fadeIn 0.5s ease;
}

.logo {
  font-family: 'Source Serif 4', Georgia, serif;
  font-weight: 300;
  font-size: 2.6rem;
  margin-bottom: 12px;
  background: linear-gradient(135deg, var(--text-primary) 0%, var(--accent) 50%, var(--text-secondary) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
}

.auth-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 40px;
  letter-spacing: 1px;
  text-transform: lowercase;
}

/* 6-digit code inputs */

.code-inputs {
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: center;
  margin-bottom: 24px;
}

.code-digit {
  width: 52px;
  height: 64px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Source Serif 4', serif;
  font-size: 1.6rem;
  font-weight: 400;
  text-align: center;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-text-security: disc;
  caret-color: var(--accent);
}

.code-digit:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.code-digit.filled {
  border-color: var(--text-muted);
}

.code-digit.error {
  border-color: var(--red);
  animation: shake 0.4s ease;
}

.code-separator {
  width: 12px;
  height: 2px;
  background: var(--text-muted);
  border-radius: 1px;
  flex-shrink: 0;
}

.auth-error {
  color: var(--red);
  font-size: 0.8rem;
  min-height: 1.2em;
}

.no-code-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-family: 'Inter', sans-serif;
  font-size: 0.78rem;
  cursor: pointer;
  margin-top: 20px;
  letter-spacing: 0.3px;
  transition: color 0.2s;
  display: block;
  width: 100%;
}

.no-code-btn:hover {
  color: var(--text-secondary);
}

/* ---- No Code Modal ---- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn 0.2s ease;
}

.modal-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 44px 40px;
  max-width: 360px;
  width: 90%;
  text-align: center;
  animation: fadeIn 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-title {
  font-family: 'Source Serif 4', serif;
  font-weight: 400;
  font-size: 1.4rem;
  color: var(--text-primary);
  margin-bottom: 16px;
  letter-spacing: -0.01em;
}

.modal-text {
  font-size: 0.86rem;
  color: var(--text-secondary);
  line-height: 1.65;
  margin-bottom: 32px;
}

.modal-close-btn {
  padding: 10px 32px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 50px;
  color: var(--text-secondary);
  font-family: 'Inter', sans-serif;
  font-size: 0.83rem;
  cursor: pointer;
  transition: all 0.2s;
}

.modal-close-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- Greeting Screen ---- */

#greeting-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg-primary);
}

.greeting-container {
  text-align: center;
}

.greeting-text {
  font-family: 'Source Serif 4', Georgia, serif;
  font-weight: 400;
  font-size: 2.8rem;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  opacity: 0;
  line-height: 1.2;
}

.greeting-text.animate {
  animation: greetFadeIn 1.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes greetFadeIn {
  0% {
    opacity: 0;
    transform: translateY(16px);
    filter: blur(12px);
  }
  60% {
    filter: blur(0);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
    filter: blur(0);
  }
}

/* ---- App Layout ---- */

#app-screen {
  display: flex;
  height: 100vh;
}

#sidebar {
  width: 260px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo-small {
  font-family: 'Source Serif 4', Georgia, serif;
  font-weight: 300;
  font-size: 1.15rem;
  background: linear-gradient(135deg, var(--text-primary) 0%, var(--accent) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#current-user {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

#contacts-list {
  flex: 1;
  overflow-y: auto;
  touch-action: pan-y;
  padding: 8px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
}

.contact-item:hover {
  background: var(--bg-hover);
}

.contact-item.active {
  background: var(--bg-tertiary);
}

.contact-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Source Serif 4', serif;
  font-size: 0.95rem;
  color: var(--text-secondary);
  flex-shrink: 0;
  border: 1px solid var(--border);
}

.contact-name {
  font-size: 0.9rem;
  color: var(--text-primary);
}

.contact-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  margin-left: auto;
  flex-shrink: 0;
}

.contact-status.online {
  background: var(--green);
}

#logout-btn {
  margin: 12px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-family: 'Inter', sans-serif;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s;
}

#logout-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* ---- Chat ---- */

#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#chat-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.placeholder-text {
  font-family: 'Source Serif 4', serif;
  font-size: 1.1rem;
  color: var(--text-muted);
  font-style: italic;
}

#chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
}

#chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-secondary);
}

#chat-peer-name {
  font-family: 'Source Serif 4', serif;
  font-size: 1.1rem;
  font-weight: 400;
}

.chat-actions button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
}

.chat-actions button:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

#messages-container {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  touch-action: pan-y;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg {
  max-width: 65%;
  padding: 10px 16px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.5;
  word-wrap: break-word;
  animation: msgIn 0.2s ease;
}

.msg.mine {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.theirs {
  align-self: flex-start;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.msg-time {
  font-size: 0.7rem;
  color: rgba(255,255,255,0.5);
  margin-top: 4px;
}

.msg.theirs .msg-time {
  color: var(--text-muted);
}

#message-form {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  background: var(--bg-secondary);
}

#message-input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Inter', sans-serif;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
}

#message-input::placeholder {
  color: var(--text-muted);
}

#message-input:focus {
  border-color: var(--accent);
}

#message-form button {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

#message-form button:hover {
  opacity: 0.85;
}

#voice-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

#voice-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  opacity: 1;
}

#voice-btn.recording {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  animation: pulse-rec 1s ease infinite;
}

@keyframes pulse-rec {
  0%, 100% { box-shadow: 0 0 0 0 rgba(212, 90, 90, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(212, 90, 90, 0); }
}

/* Voice recording bar */
#voice-recording-bar {
  padding: 12px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  gap: 12px;
}

.voice-rec-indicator {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
}

.rec-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--red);
  animation: blink-rec 1s ease infinite;
}

@keyframes blink-rec {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

#voice-rec-timer {
  font-family: 'Inter', sans-serif;
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

#voice-cancel-btn, #voice-send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s;
}

#voice-cancel-btn {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

#voice-cancel-btn:hover {
  background: var(--red);
  color: #fff;
}

#voice-send-btn {
  background: var(--accent);
  color: #fff;
}

#voice-send-btn:hover {
  opacity: 0.85;
}

/* Voice message bubble */
.voice-msg {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 180px;
}

.voice-play-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.2s;
}

.msg.mine .voice-play-btn {
  background: rgba(255,255,255,0.2);
  color: #fff;
}

.msg.theirs .voice-play-btn {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.voice-play-btn:hover {
  opacity: 0.8;
}

.voice-waveform {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 2px;
  height: 28px;
}

.voice-waveform-bar {
  width: 3px;
  border-radius: 2px;
  transition: height 0.1s ease;
}

.msg.mine .voice-waveform-bar {
  background: rgba(255,255,255,0.5);
}

.msg.theirs .voice-waveform-bar {
  background: var(--text-muted);
}

.voice-waveform-bar.active {
  background: var(--accent) !important;
}

.voice-duration {
  font-size: 0.7rem;
  flex-shrink: 0;
  min-width: 30px;
  text-align: right;
}

.msg.mine .voice-duration {
  color: rgba(255,255,255,0.6);
}

.msg.theirs .voice-duration {
  color: var(--text-muted);
}

/* ---- Call Overlay ---- */

#call-overlay, #incoming-call {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(8px);
}

.call-card {
  text-align: center;
  padding: 48px;
}

.call-card p {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 2px;
}

.call-card h3 {
  font-family: 'Source Serif 4', serif;
  font-size: 2rem;
  font-weight: 400;
  color: var(--text-primary);
  margin-bottom: 24px;
}

#call-timer {
  font-family: 'Inter', sans-serif;
  font-size: 1.2rem;
  color: var(--text-secondary);
  margin-bottom: 24px;
  font-variant-numeric: tabular-nums;
}

.call-buttons {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 16px;
}

.call-action {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  position: relative;
}

.call-action:hover {
  background: var(--bg-hover);
}

.call-action.end {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}

.call-action.end:hover {
  opacity: 0.85;
}

.call-action.accept {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}

.call-action.accept:hover {
  opacity: 0.85;
}

.call-action.muted {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}

/* Noise suppression tumbler */
.noise-toggle-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.noise-toggle-label {
  font-size: 0.6rem;
  font-weight: 500;
  letter-spacing: 1.5px;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.tumbler {
  cursor: pointer;
  display: inline-block;
  position: relative;
}

.tumbler input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.tumbler-track {
  display: block;
  width: 44px;
  height: 24px;
  border-radius: 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  transition: background 0.25s, border-color 0.25s;
  position: relative;
}

.tumbler-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), background 0.25s;
}

.tumbler input:checked + .tumbler-track {
  background: var(--green);
  border-color: var(--green);
}

.tumbler input:checked + .tumbler-track .tumbler-thumb {
  transform: translateX(20px);
  background: #fff;
}

.tumbler:hover .tumbler-track {
  opacity: 0.85;
}

/* ---- Back Button (desktop: hidden) ---- */

#back-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.2s;
  margin-right: 4px;
}

#back-btn:hover {
  color: var(--text-primary);
}

/* ---- Scrollbar ---- */

::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ---- Animations ---- */

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

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

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

/* ---- Mobile ---- */

@media (max-width: 768px) {

  /* Auth: no card, full screen */
  #auth-screen {
    align-items: stretch;
    padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0);
  }

  .auth-container {
    width: 100%;
    flex: 1;
    border-radius: 0;
    border: none;
    padding: 0 28px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background: var(--bg-primary);
  }

  .logo {
    font-size: 2rem;
  }

  /* Prevent iOS zoom on focused inputs */
  #message-input {
    font-size: 16px;
  }

  /* Greeting */
  .greeting-text {
    font-size: 2rem;
    padding: 0 28px;
    text-align: center;
  }

  /* App: slide navigation */
  #app-screen {
    position: relative;
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
  }

  #sidebar {
    width: 100%;
    position: absolute;
    inset: 0;
    z-index: 2;
    transform: translateX(0);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  #app-screen.chat-open #sidebar {
    transform: translateX(-100%);
    pointer-events: none;
  }

  #chat-area {
    position: absolute;
    inset: 0;
    z-index: 1;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    height: 100%;
    overflow: hidden;
  }

  #app-screen.chat-open #chat-area {
    transform: translateX(0);
  }

  /* Chat view fills screen */
  #chat-view {
    height: 100%;
    overflow: hidden;
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  /* Back button visible on mobile */
  #back-btn {
    display: flex;
  }

  /* Chat header */
  #chat-header {
    padding: 10px 16px;
  }

  /* Messages */
  #messages-container {
    padding: 12px 16px;
  }

  .msg {
    max-width: 82%;
  }

  /* Message form */
  #message-form {
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
  }

  #voice-recording-bar {
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
  }

  /* Contacts: bigger touch targets */
  .contact-item {
    padding: 14px 12px;
  }

  .contact-avatar {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }

  .contact-name {
    font-size: 0.95rem;
  }

  /* Sidebar header safe area */
  .sidebar-header {
    padding-top: max(20px, env(safe-area-inset-top, 20px));
  }

  /* Call overlay */
  .call-card {
    padding: 60px 32px;
    width: 100%;
  }

  .call-card h3 {
    font-size: 2.4rem;
  }

  .call-action {
    width: 64px;
    height: 64px;
  }

  /* Modal */
  .modal-card {
    padding: 36px 28px;
  }
}

/* ---- Screen Share ---- */

#screen-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 300;
}

#remote-screen {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

.screen-overlay-bar {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 301;
}

#screen-overlay-label {
  background: rgba(0, 0, 0, 0.65);
  color: var(--text-secondary);
  font-size: 0.78rem;
  padding: 6px 12px;
  border-radius: 20px;
  backdrop-filter: blur(6px);
  letter-spacing: 0.4px;
}

#close-screen-btn {
  background: rgba(30, 30, 30, 0.85);
  border: 1px solid rgba(255,255,255,0.12);
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(6px);
  transition: background 0.2s, transform 0.15s;
}

#close-screen-btn:hover {
  background: rgba(212, 90, 90, 0.75);
  transform: scale(1.08);
}

#screen-btn.sharing {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* ---- Channel ---- */

.channel-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
}

.channel-item:hover {
  background: var(--bg-hover);
}

.channel-item.active {
  background: var(--bg-tertiary);
}

.channel-avatar {
  background: var(--accent-soft) !important;
  border-color: rgba(212, 132, 90, 0.3) !important;
  color: var(--accent) !important;
  font-family: 'Inter', sans-serif !important;
  font-size: 1rem !important;
  font-weight: 500 !important;
}

.channel-pin {
  margin-left: auto;
  font-size: 0.62rem;
  color: var(--text-muted);
  letter-spacing: 0.8px;
  text-transform: uppercase;
}

.contacts-separator {
  height: 1px;
  background: var(--border);
  margin: 6px 8px;
}

#channel-readonly-bar {
  padding: 14px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  letter-spacing: 0.5px;
}
