:root {
  --accent: #00a884;
  --accent-dark: #008f6f;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --sidebar: #ffffff;
  --border: #e9edef;
  --text: #111b21;
  --text-muted: #667781;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --danger: #e63946;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
button { cursor: pointer; font-family: inherit; }
input { font-family: inherit; }

/* Auth screen */
.auth-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #00a884, #128c7e);
}
.auth-card {
  background: var(--panel);
  padding: 2.5rem 2rem;
  border-radius: 12px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  text-align: center;
}
.auth-card h1 { margin: 0 0 0.5rem; color: var(--accent); }
.auth-sub { color: var(--text-muted); font-size: 0.92rem; margin-bottom: 1.5rem; }
.auth-card input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  margin-bottom: 1rem;
}
.btn-primary {
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-dark); }
.auth-error { color: var(--danger); font-size: 0.85rem; min-height: 1.2em; margin-top: 0.75rem; }

/* App layout */
.app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
}
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--border);
}
.me { display: flex; align-items: center; gap: 0.7rem; min-width: 0; }
.me-info { min-width: 0; }
.me-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-code { font-size: 0.8rem; color: var(--text-muted); }
.avatar {
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 1rem; flex-shrink: 0;
  background: var(--accent);
}
.icon-btn {
  background: none; border: none; font-size: 1.1rem;
  padding: 0.35rem 0.5rem; border-radius: 6px; color: var(--text-muted);
}
.icon-btn:hover { background: var(--bg); }

.add-contact-row {
  display: flex; gap: 0.5rem; padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.add-contact-row input {
  flex: 1; padding: 0.5rem 0.75rem; border: 1px solid var(--border);
  border-radius: 8px; font-size: 0.95rem;
}
.btn-secondary {
  padding: 0.5rem 0.9rem; background: var(--accent); color: white;
  border: none; border-radius: 8px; font-weight: 600; font-size: 0.9rem;
}
.btn-secondary:hover { background: var(--accent-dark); }
.add-error { color: var(--danger); font-size: 0.8rem; padding: 0 1rem; min-height: 1em; margin: 0.25rem 0 0; }

.contacts-list { flex: 1; overflow-y: auto; }
.contact-item {
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.7rem 1rem; border-bottom: 1px solid var(--border);
  position: relative;
}
.contact-item:hover { background: var(--bg); }
.contact-item.active { background: #e9f7f2; }
.contact-main { flex: 1; min-width: 0; }
.contact-top { display: flex; justify-content: space-between; align-items: baseline; }
.contact-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-time { font-size: 0.72rem; color: var(--text-muted); flex-shrink: 0; }
.contact-sub { font-size: 0.82rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.online-dot {
  width: 10px; height: 10px; border-radius: 50%; background: #ccc;
  position: absolute; left: 40px; top: 44px; border: 2px solid white;
}
.online-dot.online { background: #31d158; }

/* Chat pane */
.chat-pane { display: flex; flex-direction: column; min-width: 0; background: #efeae2; }
.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--text-muted); padding: 2rem; text-align: center; }
.chat-active { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-header {
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.6rem 1rem; background: var(--panel); border-bottom: 1px solid var(--border);
}
.chat-header-info { flex: 1; min-width: 0; }
.chat-name { font-weight: 600; }
.chat-status { font-size: 0.8rem; color: var(--text-muted); }
.chat-header-actions { display: flex; gap: 0.25rem; }
.chat-header-actions .icon-btn { font-size: 1.3rem; }

.messages { flex: 1; overflow-y: auto; padding: 1rem 1.5rem; display: flex; flex-direction: column; gap: 0.4rem; }
.bubble-row { display: flex; align-items: center; gap: 0.35rem; }
.bubble-row.out { justify-content: flex-end; }
.bubble {
  max-width: 60%; padding: 0.5rem 0.7rem; border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08); font-size: 0.93rem; line-height: 1.35;
  word-wrap: break-word;
}
.bubble-row.in .bubble { background: var(--bubble-in); border-top-left-radius: 2px; }
.bubble-row.out .bubble { background: var(--bubble-out); border-top-right-radius: 2px; }
.bubble .meta { display: block; font-size: 0.68rem; color: var(--text-muted); margin-top: 0.2rem; text-align: right; }
.bubble img { max-width: 240px; border-radius: 6px; display: block; }
.expire-countdown { font-size: 0.7rem; color: var(--text-muted); margin-top: 0.3rem; }
.bubble .location-link { color: var(--accent-dark); font-weight: 600; text-decoration: none; display: flex; align-items: center; gap: 0.3rem; }
.bubble.system { background: #fff3cd; color: #7a5c00; text-align: center; font-size: 0.8rem; }
.bubble.deleted { font-style: italic; color: var(--text-muted); background: var(--bubble-in); }
.day-divider { text-align: center; color: var(--text-muted); font-size: 0.75rem; margin: 0.6rem 0; }

.delete-msg-btn {
  border: none; background: none; cursor: pointer; padding: 0.25rem;
  font-size: 0.85rem; opacity: 0; transition: opacity 0.15s; flex-shrink: 0;
}
.bubble-row.out:hover .delete-msg-btn { opacity: 0.6; }
.delete-msg-btn:hover { opacity: 1 !important; }
.delete-msg-btn.confirming {
  opacity: 1 !important; color: var(--danger); font-weight: 700; font-size: 0.7rem;
  border: 1px solid var(--danger); border-radius: 10px; padding: 0.15rem 0.5rem;
}

.composer {
  display: flex; align-items: center; gap: 0.4rem;
  padding: 0.6rem 1rem; background: var(--panel); border-top: 1px solid var(--border);
}
.composer input[type="text"] {
  flex: 1; padding: 0.6rem 1rem; border: 1px solid var(--border);
  border-radius: 20px; font-size: 0.95rem;
}
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; padding: 0; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 1.1rem;
}

/* Call overlay */
.call-overlay {
  position: fixed; inset: 0; background: rgba(10, 15, 20, 0.92);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.call-card { text-align: center; color: white; width: 100%; max-width: 480px; padding: 1rem; }
.call-avatar {
  width: 96px; height: 96px; border-radius: 50%; margin: 0 auto 1rem;
  display: flex; align-items: center; justify-content: center;
  font-size: 2.2rem; font-weight: 700; background: var(--accent);
}
.call-peer-name { font-size: 1.4rem; font-weight: 600; }
.call-status { color: #b7c2c7; margin-bottom: 1rem; }
.call-videos { position: relative; margin: 1rem 0; }
.call-videos video { display: none; }
#remote-video { width: 100%; max-height: 55vh; border-radius: 10px; background: #000; }
#local-video {
  position: absolute; width: 110px; bottom: 10px; right: 10px;
  border-radius: 8px; background: #000; border: 2px solid rgba(255,255,255,0.3);
}
.call-actions { display: flex; gap: 0.8rem; justify-content: center; flex-wrap: wrap; margin-top: 1rem; }
.call-btn {
  padding: 0.7rem 1.2rem; border-radius: 30px; border: none; font-size: 0.95rem;
  font-weight: 600; background: #3a4a50; color: white;
}
.call-btn.accept { background: var(--accent); }
.call-btn.end { background: var(--danger); }

.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%);
  background: #323232; color: white; padding: 0.6rem 1.2rem; border-radius: 8px;
  font-size: 0.9rem; z-index: 100; box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.back-btn { display: none; font-size: 1.3rem; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar, .chat-pane { position: fixed; inset: 0; }
  .sidebar { z-index: 20; }
  .chat-pane { z-index: 10; }

  /* Chat pane is only reachable on mobile once a chat is opened (body.chat-open),
     at which point the sidebar must get out of the way — otherwise it just sits
     on top of the chat at a higher z-index and the messages are unreachable. */
  body:not(.chat-open) .chat-pane { display: none; }
  body.chat-open .sidebar { display: none; }

  .back-btn { display: inline-flex; align-items: center; justify-content: center; }

  /* No real :hover on touch devices, so the delete button must stay reachable. */
  .delete-msg-btn { opacity: 0.5; }
}
