/* ---------- Tokens ---------- */
:root {
  --ink: #111B21;
  --ink-soft: #2A3942;
  --bg: #EFEAE2;
  --panel: #FFFFFF;
  --border: #E9EDEF;
  --teal: #008069;
  --teal-dark: #005C4B;
  --teal-header: #00A884;
  --sent-bubble: #D9FDD3;
  --error: #B3261E;
  --success: #25D366;
  --text: #111B21;
  --text-muted: #667781;

  --font-display: 'Fraunces', Georgia, serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
.hidden { display: none !important; }

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden; /* the page itself never scrolls/bounces — only #message-list and #chat-list do */
  position: fixed;
  inset: 0;
  font-family: var(--font-body);
  color: var(--text);
  background: var(--bg);
  overscroll-behavior: none;
}

button { font-family: inherit; }
input { font-family: inherit; }

/* ---------- Auth screens (login / register / verify) ---------- */
.auth-screen {
  height: 100vh;
  height: 100dvh; /* accounts for mobile browser toolbars; falls back to 100vh where dvh isn't supported */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow-y: auto;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 36px 32px;
}

.wordmark {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 28px;
  color: var(--ink);
}

.wordmark.small { font-size: 20px; }

.auth-sub {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 6px;
  margin-bottom: 24px;
}

.auth-card label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
  margin-bottom: 6px;
  margin-top: 14px;
}

.auth-card input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}

.auth-card input:focus {
  border-color: var(--teal);
}

.btn-primary {
  width: 100%;
  margin-top: 22px;
  padding: 12px;
  background: var(--teal);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-primary:hover { background: var(--teal-dark); }

.btn-ghost {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
}

.btn-ghost:hover { border-color: var(--teal); color: var(--teal-dark); }

.auth-error {
  color: var(--error);
  font-size: 13px;
  margin-top: 12px;
  min-height: 16px;
}

.auth-success {
  color: var(--success);
  font-size: 13px;
  margin-top: 6px;
}

.auth-switch {
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 22px;
}

.auth-switch a {
  color: var(--teal-dark);
  font-weight: 600;
  text-decoration: none;
}

.auth-switch a:hover { text-decoration: underline; }

/* ---------- Main app shell ---------- */
#app-screen {
  height: 100vh;
  height: 100dvh;
}

#app-shell {
  height: 100%;
  display: flex;
  max-width: 1100px;
  margin: 0 auto;
  background: var(--panel);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

/* ---------- Sidebar ---------- */
#inbox-sidebar {
  width: 320px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  background: var(--teal-header);
}

.sidebar-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0; /* lets the wordmark ellipsize instead of forcing the row wider */
}

.sidebar-header .wordmark {
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Avatars (shared: sidebar, inbox rows, chat header) ---------- */
.avatar {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--teal-header);
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-initial {
  color: #fff;
  font-weight: 600;
  font-size: 16px;
}

.own-avatar {
  width: 38px;
  height: 38px;
  border: none;
  cursor: pointer;
  padding: 0;
  box-shadow: 0 0 0 2px rgba(255,255,255,0.6);
  transition: box-shadow 0.15s;
}

.own-avatar:hover { box-shadow: 0 0 0 2px #fff; }

.sidebar-header-actions {
  display: flex;
  flex-shrink: 0;
  gap: 6px;
}

.sidebar-header .btn-ghost {
  white-space: nowrap;
  padding: 6px 10px;
  font-size: 12.5px;
  border-color: rgba(255,255,255,0.55);
  color: #fff;
}

.sidebar-header .btn-ghost:hover {
  border-color: #fff;
  color: #fff;
  background: rgba(255,255,255,0.12);
}

.new-chat-panel {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.new-chat-panel input {
  width: 100%;
  padding: 9px 10px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  outline: none;
}

.new-chat-panel input:focus {
  border-color: var(--teal);
}

.new-chat-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn-small {
  width: auto;
  margin-top: 0;
  padding: 8px 16px;
  font-size: 13px;
}

#chat-list {
  flex-grow: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.inbox-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 12px 16px;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
}

.inbox-item-text {
  flex-grow: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.inbox-item-toprow {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.inbox-item-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.inbox-item-time {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--text-muted);
}

.inbox-item-bottomrow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.inbox-item-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 3px;
}

.inbox-msg-ticks {
  flex-shrink: 0;
  font-size: 12px;
  line-height: 1;
  letter-spacing: -2px;
  padding-right: 2px;
}

.inbox-msg-ticks.sent,
.inbox-msg-ticks.delivered {
  color: var(--text-muted);
}

.inbox-msg-ticks.read {
  color: #2F8FE0;
}

.unread-badge {
  flex-shrink: 0;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: #E0433E;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 20px;
  text-align: center;
}

.inbox-item:hover { background: var(--bg); }
.inbox-item.active { background: var(--sent-bubble); }
.inbox-item.active .inbox-item-name { font-weight: 700; }

.inbox-empty {
  padding: 24px 20px;
  color: var(--text-muted);
  font-size: 13px;
}

.inbox-error { color: var(--error); }

/* ---------- Chat panel ---------- */
#chat-panel {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-empty-state {
  flex-grow: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 14px;
}

#chat-container {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.back-btn {
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  width: 40px;
  height: 40px;
  margin-right: 4px;
  margin-left: -8px;
  padding: 8px;
  border-radius: 50%;
  cursor: pointer;
  color: var(--ink);
  transition: background 0.15s;
}

.back-btn:hover { background: var(--bg); }
.back-btn svg { display: block; }

.chat-header .avatar {
  width: 40px;
  height: 40px;
  margin-right: 12px;
}

.chat-header-info {
  display: flex;
  align-items: center;
  background: none;
  border: none;
  padding: 4px 8px;
  margin: -4px 0 -4px -4px;
  border-radius: 8px;
  cursor: pointer;
  min-width: 0;
  text-align: left;
  transition: background 0.15s;
}

.chat-header-info:hover, .chat-header-info:focus-visible {
  background: var(--bg);
}

.chat-header-info .avatar {
  margin-right: 12px;
}

.chat-header-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
}

.chat-contact-name {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-contact-sub {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-height: 15px;
}

.chat-contact-sub.online {
  color: var(--success);
}

#message-list {
  flex-grow: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 20px;
  display: flex;
  flex-direction: column;
  background-color: var(--bg);
  /* Subtle repeating doodle pattern, WhatsApp-style — a faint tiled SVG,
     not an image file, so there's nothing extra to upload/host. */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140' viewBox='0 0 140 140'%3E%3Cg fill='none' stroke='%23000000' stroke-opacity='0.045' stroke-width='1.4'%3E%3Ccircle cx='20' cy='20' r='9'/%3E%3Cpath d='M55 15 L65 25 M65 15 L55 25'/%3E%3Cpath d='M100 10 q10 10 0 20 q-10 10 0 20'/%3E%3Crect x='15' y='60' width='16' height='16' rx='3' transform='rotate(20 23 68)'/%3E%3Cpath d='M60 65 l8 8 l-8 8 l-8 -8 z'/%3E%3Ccircle cx='105' cy='70' r='6'/%3E%3Cpath d='M20 105 q8 -10 16 0 q8 10 16 0'/%3E%3Cpath d='M70 100 L78 116 L62 116 Z'/%3E%3Ccircle cx='115' cy='115' r='9'/%3E%3C/g%3E%3C/svg%3E");
  background-repeat: repeat;
  background-size: 140px 140px;
}

.messages-loading {
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  margin-top: 12px;
}

/* Bubble is a column: text on top, time below it — never overlapping,
   regardless of how short or long the message is. */
.message-bubble {
  display: flex;
  flex-direction: column;
  max-width: 70%;
  padding: 8px 12px;
  margin-bottom: 10px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.45;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 1px 1px rgba(0, 0, 0, 0.05);
  animation: bubble-in 0.18s ease-out;
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.message-bubble.received {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.msg-text {
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  align-self: flex-end;
  margin-top: 3px;
  white-space: nowrap; /* keeps time + ticks on one line regardless of 12h/24h format length */
}

.msg-time {
  font-size: 10px;
  color: var(--text-muted);
}

.msg-ticks {
  font-size: 12px;
  line-height: 1;
  letter-spacing: -2px; /* pulls the double-check glyph pair tight, like a single icon */
  padding-right: 2px; /* compensate for the negative letter-spacing eating the last glyph's space */
}

.msg-ticks.sent,
.msg-ticks.delivered {
  color: var(--text-muted);
}

.msg-ticks.read {
  color: #2F8FE0;
}

/* Date separators between groups of messages from different days */
.date-divider {
  display: flex;
  justify-content: center;
  margin: 16px 0 12px;
}

.date-divider span {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 3px 12px;
}

#message-form {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#message-input {
  flex-grow: 1;
  padding: 11px 16px;
  border: 1px solid var(--border);
  border-radius: 22px;
  font-size: 14px;
  outline: none;
}

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

#send-btn {
  padding: 0 20px;
  background: var(--teal);
  color: #fff;
  border: none;
  border-radius: 22px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent; /* stops the browser's default rectangular tap flash, which ignores border-radius and is what made a round button look square on tap */
  transition: transform 0.08s ease, background 0.15s;
}

#send-btn:hover { background: var(--teal-dark); }

#send-btn:active {
  background: var(--teal-dark);
  transform: scale(0.94); /* shrinks in place, so the pill shape is preserved — this is the actual "pressed" feedback that was missing */
}

#send-btn:focus-visible {
  outline: 2px solid var(--teal-dark);
  outline-offset: 2px;
}

#send-btn:disabled { opacity: 0.6; cursor: default; }
#send-btn:disabled:active { transform: none; }

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 3px; }

/* ---------- Mobile ---------- */
/* #app-shell toggles a `.chat-open` class in app.js when a chat is opened
   or closed, so navigation works the same on every mobile browser
   regardless of :has() support. */
@media (max-width: 720px) {
  #app-shell { border: none; }

  #inbox-sidebar { width: 100%; border-right: none; }
  #chat-panel { display: none; }

  #app-shell.chat-open #inbox-sidebar { display: none; }
  #app-shell.chat-open #chat-panel { display: flex; }

  .back-btn { display: flex; }
  .message-bubble { max-width: 85%; }
}

/* ---------- Profile page ---------- */
/* Occupies the same slot as #chat-container/#chat-empty-state inside
   #chat-panel (only one of the three is ever un-hidden at a time), so it
   behaves like a real screen — pushed in over the open chat, with its own
   back button returning to it — consistently on both desktop and the
   mobile single-column layout below, rather than a popup floating on top. */
#profile-page {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.profile-page-header {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

.profile-page-content {
  flex-grow: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 48px 32px;
}

.profile-page-avatar {
  width: 160px;
  height: 160px;
  margin-bottom: 20px;
}

.profile-page-avatar .avatar-initial {
  font-size: 56px;
}

.profile-page-name {
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 600;
  color: var(--text);
  overflow-wrap: anywhere;
}

.profile-page-status {
  margin-top: 8px;
  font-size: 14px;
  color: var(--text-muted);
}

.profile-page-status.online { color: var(--success); }
