/* styles.css — mobile-first, calm/private messenger aesthetic. */

:root {
  --bg: #0b0f14;
  --bg-elev: #131a22;
  --bg-elev-2: #1b232d;
  --line: #24303c;
  --text: #e8edf2;
  --text-dim: #9fb0c0;
  --text-faint: #6b7c8c;
  --accent: #4f8cff;
  --accent-ink: #ffffff;
  --bubble-me: #2a6df4;
  --bubble-me-ink: #ffffff;
  --danger: #ff6b6b;
  --radius: 18px;
  --header-h: 56px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

#app {
  height: 100dvh;
}

.screen {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  position: relative;
}

/* ---- Headers ---- */

.app-header {
  display: flex;
  align-items: center;
  padding: env(safe-area-inset-top) 20px 0;
  min-height: calc(var(--header-h) + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 2;
}

.app-header h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
}

.app-header .menu {
  margin-left: auto;
}

/* ---- Dropdown menu ---- */

.menu {
  position: relative;
  flex: none;
}

.menu-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.menu-btn:active {
  background: var(--bg-elev);
}

.menu-pop {
  position: absolute;
  top: calc(100% + 4px);
  min-width: 220px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px;
  z-index: 20;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.menu-pop.menu-right {
  right: 0;
}

.menu-pop.menu-left {
  left: 0;
}

.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}

.menu-item:active {
  background: var(--bg-elev);
}

/* ---- Toast ---- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 10px 18px;
  font-size: 14px;
  z-index: 60;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  animation: toast-in 0.16s ease-out;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

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

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

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

.chat-row:active {
  background: var(--bg-elev);
}

.chat-row-main {
  flex: 1;
  min-width: 0;
}

.chat-row-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

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

.chat-time {
  color: var(--text-faint);
  font-size: 12px;
  flex: none;
}

.chat-preview {
  color: var(--text-dim);
  font-size: 14px;
  margin-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.avatar {
  flex: none;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--bg-elev-2);
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
  user-select: none;
}

.empty {
  color: var(--text-faint);
  text-align: center;
  padding: 60px 24px;
  line-height: 1.6;
  font-size: 15px;
}

/* ---- FAB ---- */

.fab {
  position: absolute;
  right: 20px;
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  display: grid;
  place-items: center;
}

.fab:active {
  transform: scale(0.94);
}

/* ---- Thread ---- */

.thread-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: env(safe-area-inset-top) 12px 0;
  min-height: calc(var(--header-h) + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 2;
}

.thread-header .avatar {
  width: 36px;
  height: 36px;
  font-size: 18px;
}

.back-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 34px;
  line-height: 1;
  padding: 0 6px 4px;
  cursor: pointer;
}

.thread-title {
  font-weight: 600;
  font-size: 17px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.day-sep {
  text-align: center;
  margin: 14px 0 10px;
}

.day-sep span {
  background: var(--bg-elev);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 12px;
}

.msg-row {
  display: flex;
  justify-content: flex-end;
}

.bubble {
  position: relative;
  max-width: 78%;
  background: var(--bubble-me);
  color: var(--bubble-me-ink);
  padding: 8px 12px 8px;
  border-radius: var(--radius);
  border-bottom-right-radius: 5px;
  font-size: 15px;
  line-height: 1.35;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.bubble.pending {
  opacity: 0.55;
}

.bubble-body {
  margin-right: 46px;
}

.bubble-time {
  position: absolute;
  right: 10px;
  bottom: 7px;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.7);
}

.bubble.failed {
  outline: 1px solid var(--danger);
}

/* ---- Image bubbles ---- */

.bubble-image {
  padding: 4px;
  background: var(--bubble-me);
  overflow: hidden;
}

.photo-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 3px;
  border-radius: 14px;
  overflow: hidden;
}

.photo-grid.multi {
  grid-template-columns: 1fr 1fr;
}

.photo {
  display: block;
  padding: 0;
  border: none;
  background: var(--bg-elev-2);
  cursor: pointer;
  line-height: 0;
  overflow: hidden;
}

.photo img {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 320px;
  object-fit: cover;
  user-select: none;
}

.photo-grid:not(.multi) .photo img {
  max-height: 340px;
  width: auto;
  max-width: 100%;
}

.photo-caption {
  color: var(--bubble-me-ink);
  font-size: 15px;
  line-height: 1.35;
  padding: 6px 8px 4px;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.bubble-time-onphoto {
  right: 10px;
  bottom: 8px;
  padding: 1px 6px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.4);
  color: rgba(255, 255, 255, 0.92);
}

/* ---- Lightbox ---- */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.94);
  display: grid;
  place-items: center;
  padding: calc(env(safe-area-inset-top) + 12px) 12px
    calc(env(safe-area-inset-bottom) + 12px);
  touch-action: pinch-zoom;
}

.lightbox-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 6px;
  touch-action: pinch-zoom;
}

.lightbox-close {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 8px);
  right: 12px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

/* ---- Composer ---- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.composer-input {
  flex: 1;
  resize: none;
  max-height: 140px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 20px;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  padding: 10px 14px;
  line-height: 1.35;
  outline: none;
}

.composer-input:focus {
  border-color: var(--accent);
}

.send-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.send-btn:disabled {
  background: var(--bg-elev-2);
  color: var(--text-faint);
  cursor: default;
}

.icon-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn:active {
  background: var(--line);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---- Message action sheet ---- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  animation: sheet-fade 0.16s ease-out;
}

@keyframes sheet-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.sheet {
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  animation: sheet-up 0.18s ease-out;
}

@keyframes sheet-up {
  from {
    transform: translateY(12px);
  }
  to {
    transform: translateY(0);
  }
}

.sheet-item {
  width: 100%;
  padding: 15px 16px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
}

.sheet-item:active {
  background: var(--bg-elev);
}

.sheet-item.danger {
  color: var(--danger);
}

.sheet-confirm {
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
  padding: 6px 0 2px;
}

/* Small ⋯ affordance on an image bubble to reach the action sheet without
   colliding with the photo's tap-to-open-lightbox. */
.bubble-menu-btn {
  position: absolute;
  top: 6px;
  left: 6px;
  z-index: 1;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.bubble.tappable {
  cursor: pointer;
}

/* ---- Search ---- */

.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.search-icon {
  flex: none;
  font-size: 15px;
  opacity: 0.6;
  line-height: 1;
}

.search-input {
  flex: 1;
  min-width: 0;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 18px;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  padding: 8px 14px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.search-input:focus {
  border-color: var(--accent);
}

/* Hide the native clear affordance; we render our own. */
.search-input::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

.search-clear {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--bg-elev-2);
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.search-clear:active {
  background: var(--line);
}

/* Results list (replaces the chat list while a query is active). */
.results {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Section headers ("Chats" / "Messages") in the combined chat-list search. */
.result-section-head {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  padding: 14px 16px 4px;
}

.result-group-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px 6px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 1;
}

.result-group-head .avatar {
  width: 28px;
  height: 28px;
  font-size: 15px;
}

.result-group-name {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 11px 16px;
  background: none;
  border: none;
  border-bottom: 1px solid var(--line);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.result-row:active {
  background: var(--bg-elev);
}

.result-snippet {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  line-height: 1.4;
  color: var(--text-dim);
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.result-photo {
  margin-right: 5px;
}

.result-snippet mark {
  background: rgba(79, 140, 255, 0.28);
  color: var(--text);
  border-radius: 3px;
  padding: 0 1px;
}

.result-time {
  flex: none;
  color: var(--text-faint);
  font-size: 12px;
}

/* In-thread search overlay: sits under the header, above the message list. */
.thread-search {
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: 55%;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.thread-search .results {
  flex: 1;
}

.thread-search-btn {
  margin-left: auto;
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--text);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.thread-search-btn:active {
  background: var(--bg-elev);
}

/* Flash a deep-linked message briefly when jumped to. */
@keyframes msg-flash {
  0%,
  55% {
    box-shadow: 0 0 0 3px var(--accent);
  }
  100% {
    box-shadow: 0 0 0 3px rgba(79, 140, 255, 0);
  }
}

.msg-row.flash .bubble {
  animation: msg-flash 1.8s ease-out;
}

/* ---- Login screen ---- */

.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top) + 24px) 20px
    calc(env(safe-area-inset-bottom) + 24px);
}

.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 14px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 32px 24px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  text-align: center;
}

.login-mark {
  font-size: 34px;
  line-height: 1;
  margin-bottom: 2px;
}

.login-title {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
}

.login-sub {
  margin: 0 0 4px;
  color: var(--text-dim);
  font-size: 14px;
}

.login-input {
  width: 100%;
  padding: 13px 15px;
  font-size: 16px; /* ≥16px avoids iOS zoom-on-focus */
  color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: none;
}

.login-input:focus {
  border-color: var(--accent);
}

.login-btn {
  width: 100%;
  padding: 13px 15px;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent-ink);
  background: var(--accent);
  border: none;
  border-radius: 12px;
  cursor: pointer;
}

.login-btn:active {
  filter: brightness(0.92);
}

.login-error {
  margin: 0;
  padding: 10px 12px;
  font-size: 14px;
  color: var(--danger);
  background: rgba(255, 107, 107, 0.1);
  border: 1px solid rgba(255, 107, 107, 0.35);
  border-radius: 10px;
}
