:root {
  --bg: #0b141a;
  --sidebar: #111b21;
  --chat-bg: #0b141a;
  --header: #202c33;
  --bubble-out: #005c4b;
  --bubble-in: #202c33;
  --text: #e9edef;
  --text-muted: #8696a0;
  --accent: #00a884;
  --danger: #ea4335;
  --border: #2a3942;
}

:root[data-mode="light"] {
  --bg: #f4f7f8;
  --sidebar: #ffffff;
  --chat-bg: #eef3f5;
  --header: #e5ecef;
  --bubble-in: #ffffff;
  --text: #18252b;
  --text-muted: #5f717a;
  --border: #d3dee3;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

.screen {
  height: 100vh;
  width: 100%;
}

/* ========== Login ========== */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0b141a 0%, #1a2a32 100%);
}

:root[data-mode="light"] #login-screen {
  background: linear-gradient(135deg, #f4f7f8 0%, #dce9ed 100%);
}

.login-card {
  background: var(--sidebar);
  padding: 2.5rem;
  border-radius: 16px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.logo {
  font-size: 3.5rem;
  margin-bottom: 0.5rem;
}

.login-card h1 {
  font-size: 1.8rem;
  margin-bottom: 0.25rem;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1.8rem;
}

.primary-btn {
  width: 100%;
  padding: 0.9rem;
  background: var(--accent);
  color: #111;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.primary-btn:hover { background: #00c49a; }
.primary-btn.small { width: auto; padding: 0.55rem 1rem; font-size: 0.9rem; }

.hint {
  margin-top: 1.2rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ========== App Layout ========== */
#app {
  display: flex;
}

.sidebar {
  width: 340px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 1rem;
  background: var(--header);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #111;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
}

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

.my-name { font-weight: 600; font-size: 0.95rem; }
.my-id {
  font-size: 0.75rem;
  color: var(--text-muted);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#copy-id-btn {
  background: transparent;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.4rem;
  border-radius: 6px;
}
#copy-id-btn:hover { background: rgba(255,255,255,0.08); }

.sidebar-header #show-qr-btn {
  min-width: 40px;
  padding: 0.4rem;
}

.connect-box {
  padding: 0.9rem;
  display: flex;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border);
}

#remote-id-input {
  flex: 1;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}

.section-title {
  padding: 0.75rem 1rem 0.4rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.03);
  transition: background 0.15s;
}

.chat-item:hover, .chat-item.active {
  background: rgba(255,255,255,0.06);
}

.chat-item .name { font-weight: 500; font-size: 0.95rem; }
.chat-item .preview {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.sidebar-footer {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
}

.text-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
}
.text-btn:hover { color: var(--danger); }

/* ========== Chat Area ========== */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%231a2a32' fill-opacity='0.4'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.empty-icon { font-size: 4rem; margin-bottom: 1rem; opacity: 0.5; }
.empty-state h2 { color: var(--text); margin-bottom: 0.5rem; }

:root[data-mode="light"] .chat-area {
  background-image: none;
}

.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
}

.chat-header {
  padding: 0.75rem 1.25rem;
  background: var(--header);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.chat-peer-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.peer-name { font-weight: 600; }
.peer-status { font-size: 0.8rem; color: var(--text-muted); }

.chat-actions { display: flex; gap: 0.4rem; }

.icon-btn {
  background: transparent;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0.4rem 0.5rem;
  border-radius: 50%;
  transition: background 0.15s;
}
.icon-btn:hover { background: rgba(255,255,255,0.1); }
.icon-btn.send {
  background: var(--accent);
  color: #111;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.message {
  max-width: 65%;
  padding: 0.55rem 0.85rem;
  border-radius: 8px;
  position: relative;
  word-wrap: break-word;
  line-height: 1.4;
}

.message.out {
  align-self: flex-end;
  background: var(--bubble-out);
  color: #fff;
  border-bottom-right-radius: 2px;
}

.message.in {
  align-self: flex-start;
  background: var(--bubble-in);
  border-bottom-left-radius: 2px;
}

.message .time {
  font-size: 0.7rem;
  color: rgba(255,255,255,0.55);
  margin-top: 0.2rem;
  text-align: right;
}

.message.out .time {
  color: rgba(255,255,255,0.75);
}

.message.file {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.message.file a {
  color: #8ab4f8;
  text-decoration: none;
  font-weight: 500;
}

.message.file .file-icon {
  font-size: 1.5rem;
}

.message img.preview {
  max-width: 240px;
  border-radius: 6px;
  margin-top: 0.3rem;
  cursor: pointer;
}

/* Footer */
.chat-footer {
  padding: 0.7rem 1rem;
  background: var(--header);
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border-top: 1px solid var(--border);
}

.file-btn {
  font-size: 1.4rem;
  cursor: pointer;
  padding: 0.3rem;
  border-radius: 50%;
}
.file-btn:hover { background: rgba(255,255,255,0.08); }

#message-input {
  flex: 1;
  padding: 0.7rem 1rem;
  border: none;
  border-radius: 24px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
}

/* Call Overlay */
.call-overlay {
  position: absolute;
  inset: 0;
  background: rgba(11, 20, 26, 0.95);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.call-info { text-align: center; margin-bottom: 2.5rem; }

.call-avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: var(--accent);
  color: #111;
  font-size: 2.5rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
}

.call-name { font-size: 1.5rem; font-weight: 600; }
.call-status { color: var(--text-muted); margin-top: 0.3rem; }
.call-timer { font-size: 1.1rem; margin-top: 0.8rem; font-variant-numeric: tabular-nums; }

.call-controls {
  display: flex;
  gap: 1.5rem;
}

.call-btn {
  padding: 0.9rem 1.6rem;
  border: none;
  border-radius: 50px;
  font-size: 1rem;
  cursor: pointer;
  background: #3b4a54;
  color: var(--text);
}
.call-btn.end {
  background: var(--danger);
  color: white;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-content {
  background: var(--sidebar);
  padding: 2rem;
  border-radius: 16px;
  text-align: center;
  min-width: 280px;
}

.incoming-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--accent);
  color: #111;
  font-size: 2rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
}

.modal-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
  justify-content: center;
}

.danger-btn {
  padding: 0.75rem 1.4rem;
  background: var(--danger);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}

/* Responsive */
@media (max-width: 700px) {
  .sidebar { width: 100%; position: absolute; z-index: 5; height: 100%; }
  .sidebar.hidden-mobile { display: none; }
  .chat-area { width: 100%; }
}

/* ========== Phone App Improvements ========== */
@media (max-width: 700px) {
  .sidebar {
    width: 100%;
    position: absolute;
    z-index: 5;
    height: 100%;
    transition: transform 0.25s ease;
  }
  .sidebar.hidden-mobile {
    transform: translateX(-100%);
  }
  .chat-area {
    width: 100%;
  }
  .chat-header {
    padding-top: max(0.75rem, env(safe-area-inset-top));
  }
  .chat-footer {
    padding-bottom: max(0.7rem, env(safe-area-inset-bottom));
  }
  .message {
    max-width: 85%;
  }
  .login-card {
    margin: 1rem;
    padding: 2rem 1.5rem;
  }
}

/* Prevent pull-to-refresh & overscroll on mobile */
html, body {
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
}

/* Better touch targets for phones */
.icon-btn, .file-btn, .primary-btn, .call-btn, .danger-btn {
  min-height: 44px;
  min-width: 44px;
}

/* Mobile back button */
.back-btn {
  display: none;
  margin-right: 0.4rem;
  font-size: 1.4rem;
}
@media (max-width: 700px) {
  .back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .chat-header {
    gap: 0.3rem;
  }
}

/* ========== Video Call Styles ========== */
.remote-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
  z-index: 1;
}

.local-video {
  position: absolute;
  bottom: 100px;
  right: 16px;
  width: 120px;
  height: 160px;
  object-fit: cover;
  border-radius: 12px;
  border: 2px solid var(--accent);
  z-index: 5;
  background: #111;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.call-overlay {
  background: #000;
  color: #fff;
}

.call-info {
  position: relative;
  z-index: 4;
  text-shadow: 0 2px 8px rgba(0,0,0,0.8);
}

.call-status { color: rgba(255,255,255,0.75); }

.call-controls {
  position: relative;
  z-index: 6;
}

.call-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  padding: 0;
  color: #fff;
}

.call-btn.end {
  background: var(--danger);
}

/* Typing indicator */
.typing-indicator {
  padding: 0.4rem 1.25rem 0.6rem;
  display: flex;
  gap: 4px;
  align-items: center;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typing 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* Read receipts */
.message .ticks {
  font-size: 0.75rem;
  margin-left: 4px;
  color: rgba(255,255,255,0.5);
}
.message .ticks.read {
  color: #53bdeb;
}

/* Mobile video adjustments */
@media (max-width: 700px) {
  .local-video {
    width: 90px;
    height: 120px;
    bottom: 90px;
    right: 12px;
  }
}

/* ========== Status / Stories ========== */
.status-bar {
  display: flex;
  gap: 12px;
  padding: 0.6rem 1rem 1rem;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
}

.status-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  min-width: 64px;
}

.status-ring {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  padding: 2px;
  background: var(--border);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-ring.has-status {
  background: linear-gradient(45deg, #f09433, #e6683c, #dc2743, #cc2366, #bc1888);
}

.status-ring .avatar {
  width: 48px;
  height: 48px;
  font-size: 1.1rem;
  border: 2px solid var(--sidebar);
}

.status-ring .add-icon {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 18px;
  height: 18px;
  background: var(--accent);
  color: #111;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--sidebar);
}

.status-name {
  font-size: 0.7rem;
  color: var(--text-muted);
  max-width: 64px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.status-modal-content {
  width: 90%;
  max-width: 360px;
}

.status-modal-content textarea {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  resize: none;
  margin: 1rem 0;
  outline: none;
}

.status-preview {
  margin-bottom: 0.75rem;
  text-align: center;
}

.status-preview img {
  max-width: 100%;
  max-height: 200px;
  border-radius: 8px;
}

.status-file-btn {
  display: inline-block;
  margin-bottom: 1rem;
  padding: 0.5rem 1rem;
  background: var(--header);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
}

/* Status Viewer */
#status-viewer {
  background: rgba(0,0,0,0.92);
}

.status-viewer-inner {
  width: 100%;
  max-width: 420px;
  height: 80vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 1rem;
}

.status-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
  z-index: 10;
}

.status-full-img {
  max-width: 100%;
  max-height: 60vh;
  border-radius: 12px;
  object-fit: contain;
}

.status-full-text {
  color: white;
  font-size: 1.3rem;
  text-align: center;
  margin-top: 1rem;
  padding: 0 1rem;
  line-height: 1.4;
}

.status-meta {
  color: rgba(255,255,255,0.6);
  font-size: 0.85rem;
  margin-top: 1rem;
}

/* Friends list */
.friends-list .chat-item {
  position: relative;
}

.friends-list .online-dot {
  position: absolute;
  bottom: 10px;
  left: 42px;
  width: 10px;
  height: 10px;
  background: #00a884;
  border-radius: 50%;
  border: 2px solid var(--sidebar);
}

.friends-list .offline-dot {
  position: absolute;
  bottom: 10px;
  left: 42px;
  width: 10px;
  height: 10px;
  background: #8696a0;
  border-radius: 50%;
  border: 2px solid var(--sidebar);
}

.connect-box {
  flex-wrap: wrap;
}

.connect-box input {
  width: 100%;
  margin-bottom: 0.4rem;
}

#friend-name-input {
  flex: 1;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}

.friend-actions {
  display: flex;
  gap: 0.3rem;
  margin-left: auto;
}

.friend-actions button {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.2rem;
}

.friend-actions button:hover {
  color: var(--accent);
}

.remove-friend:hover {
  color: var(--danger) !important;
}

.sidebar-header #settings-btn {
  min-width: 40px;
  padding: 0.4rem;
}

.secondary-btn {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--header);
  color: var(--text);
  padding: 0.55rem 0.75rem;
  cursor: pointer;
}

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

.form-modal {
  width: min(92vw, 420px);
  max-height: 90vh;
  overflow-y: auto;
  text-align: left;
}

.form-modal h3 {
  text-align: center;
  margin-bottom: 1.2rem;
}

.form-modal > label:not(.file-btn):not(.setting-toggle) {
  display: block;
  margin: 0.8rem 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.form-modal input[type="text"],
.form-modal select {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}

.form-modal input[type="color"] {
  display: block;
  width: 100%;
  height: 44px;
  margin-top: 0.35rem;
  padding: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
}

.form-modal .status-file-btn {
  display: inline-block;
  margin: 0.7rem 0;
}

.form-modal .modal-actions > button {
  width: auto;
  padding: 0.7rem 1rem;
}

.avatar-preview {
  min-height: 0;
  text-align: center;
}

.avatar-preview img {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--accent);
}

.setting-toggle {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 1rem 0;
}

.setting-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.form-modal > .secondary-btn {
  width: 100%;
  margin: 0.25rem 0 0.5rem;
}

.qr-modal {
  width: min(92vw, 360px);
}

.qr-modal h3 { margin-bottom: 0.5rem; }

.qr-modal p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.qr-code {
  display: flex;
  justify-content: center;
  min-height: 240px;
  margin: 0 auto 1rem;
}

.qr-code img, .qr-code canvas {
  max-width: 100%;
  height: auto;
}

#my-qr-peer-id {
  display: block;
  overflow-wrap: anywhere;
  margin-bottom: 1rem;
  font-size: 0.8rem;
}

.qr-modal > .secondary-btn, .qr-modal > .danger-btn {
  width: 100%;
}

#qr-reader {
  width: 100%;
  margin: 0 auto 1rem;
}

#qr-reader video {
  border-radius: 8px;
}

.avatar.has-picture {
  background-size: cover;
  background-position: center;
}

.incoming-avatar.has-picture,
.call-avatar.has-picture {
  background-size: cover;
  background-position: center;
}

.message-sender {
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
  margin-bottom: 0.2rem;
}

.chat-actions .hidden {
  display: none !important;
}
