/* ===================== Easy Chat — Apple-style UI ===================== */
:root {
  --blue: #0a84ff;
  --blue-2: #0066ff;
  --bg: #f2f2f7;
  --bg-elevated: #ffffff;
  --sidebar: #ffffff;
  --header-blur: rgba(255, 255, 255, 0.72);
  --text: #1c1c1e;
  --text-2: #8a8a8e;
  --sep: rgba(60, 60, 67, 0.12);
  --bubble-in: #e9e9eb;
  --bubble-in-text: #1c1c1e;
  --field: #ffffff;
  --field-border: rgba(60, 60, 67, 0.16);
  --hover: rgba(0, 0, 0, 0.04);
  --active: rgba(10, 132, 255, 0.1);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --app-h: 100dvh; /* overridden live by JS to visualViewport height (keyboard-safe) */
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    system-ui, "Segoe UI", Roboto, Helvetica, Arial, "Apple Color Emoji",
    "Segoe UI Emoji", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elevated: #1c1c1e;
    --sidebar: #0a0a0b;
    --header-blur: rgba(20, 20, 22, 0.72);
    --text: #ffffff;
    --text-2: #98989f;
    --sep: rgba(84, 84, 88, 0.4);
    --bubble-in: #262628;
    --bubble-in-text: #ffffff;
    --field: #1c1c1e;
    --field-border: rgba(120, 120, 128, 0.3);
    --hover: rgba(255, 255, 255, 0.06);
    --active: rgba(10, 132, 255, 0.18);
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  font-family: var(--font);
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}
/* lock the document so the virtual keyboard can't push the whole page up */
body { position: fixed; inset: 0; width: 100%; overflow: hidden; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input { font-family: inherit; }
.hidden { display: none !important; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--sep); border-radius: 8px; }

/* ===================== AUTH ===================== */
.auth {
  height: var(--app-h); overflow-y: auto; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 18px;
  padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
  background: radial-gradient(130% 100% at 50% 0%, rgba(10, 132, 255, 0.14), transparent 60%), var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--bg-elevated); border-radius: 26px;
  padding: 30px 26px 26px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center;
  animation: pop 0.45s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes pop { from { opacity: 0; transform: translateY(14px) scale(0.98); } to { opacity: 1; transform: none; } }
.brand-logo { border-radius: 20px; box-shadow: 0 8px 22px rgba(10, 132, 255, 0.35); }
.brand-title { margin: 14px 0 2px; font-size: 27px; font-weight: 700; letter-spacing: -0.4px; }
.brand-by { margin: 0 0 20px; font-size: 14px; color: var(--text-2); font-weight: 500; }
.segment {
  display: flex; width: 100%; background: var(--bg); border-radius: 12px;
  padding: 3px; margin-bottom: 18px; gap: 3px;
}
.seg {
  flex: 1; padding: 9px 0; border-radius: 9px; font-size: 15px; font-weight: 600;
  color: var(--text); transition: background 0.2s, box-shadow 0.2s;
}
.seg.active { background: var(--bg-elevated); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }
.form { width: 100%; display: flex; flex-direction: column; gap: 11px; }
.row2 { display: flex; gap: 11px; }
.row2 input { flex: 1; min-width: 0; }
.form input {
  width: 100%; padding: 14px 16px; font-size: 16px;
  background: var(--field); color: var(--text);
  border: 1px solid var(--field-border); border-radius: 13px; outline: none;
  transition: border-color 0.18s, box-shadow 0.18s;
}
.form input:focus { border-color: var(--blue); box-shadow: 0 0 0 3.5px rgba(10, 132, 255, 0.18); }
.primary {
  padding: 14px; font-size: 16px; font-weight: 600; color: #fff; border-radius: 13px;
  background: linear-gradient(180deg, var(--blue), var(--blue-2));
  box-shadow: 0 6px 16px rgba(10, 132, 255, 0.34); transition: transform 0.12s, filter 0.15s;
}
.primary:active { transform: scale(0.98); filter: brightness(0.95); }
.err { color: #ff453a; font-size: 14px; margin: 12px 0 0; text-align: center; min-height: 1px; }
.auth-foot { color: var(--text-2); font-size: 13px; margin: 0; }

/* ===================== APP LAYOUT ===================== */
.app { display: flex; height: var(--app-h); position: relative; overflow: hidden; }
.sidebar {
  width: 340px; flex-shrink: 0; height: 100%;
  background: var(--sidebar); border-right: 1px solid var(--sep);
  display: flex; flex-direction: column;
}
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 12px;
  position: sticky; top: 0; z-index: 2;
  background: var(--header-blur); backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px); border-bottom: 1px solid var(--sep);
}
.side-brand { display: flex; align-items: center; gap: 9px; font-size: 19px; font-weight: 700; letter-spacing: -0.3px; }
.side-brand img { border-radius: 7px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  color: var(--blue); transition: background 0.15s;
}
.icon-btn:active { background: var(--hover); }
.me-chip { display: flex; align-items: center; gap: 12px; padding: 12px 18px; }
.me-meta { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.me-meta b { font-size: 16px; font-weight: 600; }
.me-meta small { font-size: 13px; color: var(--text-2); }
.me-meta .crown { color: #ffb300; font-weight: 700; }
.avatar {
  width: 46px; height: 46px; flex-shrink: 0; border-radius: 50%;
  display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 17px;
  background: linear-gradient(135deg, #5ac8fa, #0a84ff); user-select: none;
}
.avatar.sm { width: 36px; height: 36px; font-size: 14px; }
.avatar.group { background: linear-gradient(135deg, #30d158, #34c759); }

.push-banner {
  margin: 2px 14px 8px; padding: 10px 14px; border-radius: 13px;
  background: var(--active); color: var(--blue);
  display: flex; align-items: center; justify-content: space-between; font-size: 14px; font-weight: 500;
}
.push-banner button { color: #fff; background: var(--blue); padding: 6px 14px; border-radius: 9px; font-weight: 600; font-size: 13px; }

.actions { display: flex; gap: 7px; padding: 4px 14px 12px; flex-wrap: wrap; }
.act {
  position: relative; flex: 1 1 auto; display: inline-flex; align-items: center; gap: 6px;
  justify-content: center; padding: 9px 10px; border-radius: 11px; font-size: 13.5px; font-weight: 600;
  background: var(--bg); color: var(--text); transition: background 0.15s, transform 0.1s;
}
.act svg { color: var(--blue); }
.act:active { transform: scale(0.96); background: var(--hover); }
.act.owner { color: #c77700; background: rgba(255, 179, 0, 0.14); flex-basis: 100%; }
.act.owner svg { color: #ffb300; }
.badge {
  position: absolute; top: -5px; right: -4px; min-width: 19px; height: 19px; padding: 0 5px;
  border-radius: 10px; background: #ff3b30; color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center;
}

.conv-list { list-style: none; margin: 0; padding: 4px 8px 20px; overflow-y: auto; flex: 1; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.conv {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 14px;
  cursor: pointer; transition: background 0.14s;
}
.conv:hover { background: var(--hover); }
.conv.active { background: var(--active); }
.conv .meta { min-width: 0; flex: 1; }
.conv .meta b { font-size: 15.5px; font-weight: 600; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .meta small { font-size: 13.5px; color: var(--text-2); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .unread {
  flex-shrink: 0; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: #34c759; color: #fff; font-size: 12px; font-weight: 700;
  display: grid; place-items: center; align-self: center;
}
.empty-note { padding: 28px 18px; color: var(--text-2); font-size: 14.5px; text-align: center; }

/* ===================== CHAT ===================== */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; background: var(--bg); position: relative; }
.chat-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; color: var(--text-2); font-size: 16px; text-align: center; line-height: 1.5;
}
.chat-empty img { opacity: 0.85; border-radius: 18px; }
.chat-view { display: flex; flex-direction: column; height: 100%; }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--header-blur); backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--sep); z-index: 2;
}
.chat-head-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.chat-head-meta b { font-size: 16.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head-meta small { font-size: 12.5px; color: var(--text-2); }
.back { display: none; width: 34px; height: 34px; color: var(--blue); align-items: center; justify-content: center; margin-left: -6px; }

.messages {
  flex: 1; overflow-y: auto; padding: 16px 14px 10px;
  display: flex; flex-direction: column; gap: 3px; scroll-behavior: smooth;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: 76%; padding: 8px 14px; border-radius: 20px; font-size: 16px; line-height: 1.34;
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; position: relative;
  align-self: flex-start; background: var(--bubble-in); color: var(--bubble-in-text);
  border-bottom-left-radius: 7px; animation: bubble 0.22s ease; margin-top: 2px;
}
.msg.out {
  align-self: flex-end; color: #fff; border-bottom-left-radius: 20px; border-bottom-right-radius: 7px;
  background: linear-gradient(180deg, var(--blue), var(--blue-2));
}
.msg.cont { border-top-left-radius: 7px; }
.msg.out.cont { border-top-right-radius: 7px; }
@keyframes bubble { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
.msg .who { font-size: 12.5px; font-weight: 700; color: var(--blue); margin-bottom: 2px; }
.msg .time { font-size: 10.5px; opacity: 0.6; margin-left: 8px; float: right; margin-top: 4px; }
.msg.out .time { color: rgba(255, 255, 255, 0.85); opacity: 0.9; }
.day-sep { align-self: center; font-size: 12px; color: var(--text-2); font-weight: 600; margin: 14px 0 6px; }

/* composer */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 9px 12px calc(9px + var(--safe-bottom));
  background: var(--header-blur); backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px); border-top: 1px solid var(--sep); position: relative;
}
.emoji-btn { width: 36px; height: 36px; flex-shrink: 0; color: var(--text-2); display: grid; place-items: center; padding-bottom: 3px; }
.emoji-btn:active { color: var(--blue); }
#msg-input {
  flex: 1; min-width: 0; padding: 10px 16px; font-size: 16px; line-height: 1.3;
  background: var(--field); color: var(--text); border: 1px solid var(--field-border);
  border-radius: 20px; outline: none; max-height: 120px;
}
#msg-input:focus { border-color: var(--blue); }
.send {
  width: 38px; height: 38px; flex-shrink: 0; border-radius: 50%; color: #fff; display: grid; place-items: center;
  background: linear-gradient(180deg, var(--blue), var(--blue-2)); transition: transform 0.12s, opacity 0.15s;
}
.send:active { transform: scale(0.9); }
.send svg { margin-left: 1px; }

/* emoji picker — Apple style */
.emoji-pop {
  position: absolute; left: 10px; right: 10px; bottom: calc(100% + 8px);
  background: var(--bg-elevated); border: 1px solid var(--sep); border-radius: 18px;
  box-shadow: var(--shadow); overflow: hidden; max-width: 420px;
  animation: pop 0.2s ease;
}
.emoji-grid {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 10px;
  max-height: 240px; overflow-y: auto;
}
.emoji-grid button { font-size: 26px; line-height: 1; padding: 6px 0; border-radius: 10px; transition: background 0.1s; }
.emoji-grid button:active { background: var(--hover); }
.emoji-cats {
  display: flex; border-top: 1px solid var(--sep); padding: 6px 8px; gap: 2px;
  background: var(--bg);
}
.emoji-cats button { flex: 1; font-size: 21px; padding: 6px 0; border-radius: 9px; opacity: 0.55; }
.emoji-cats button.active { opacity: 1; background: var(--hover); }

/* ===================== MODAL ===================== */
.modal {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: rgba(0, 0, 0, 0.4); backdrop-filter: blur(3px);
  animation: fade 0.2s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal-card {
  width: 100%; max-width: 400px; max-height: 80vh; overflow-y: auto;
  background: var(--bg-elevated); border-radius: 22px; padding: 10px 22px 20px;
  box-shadow: var(--shadow); animation: pop 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.modal-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--sep); margin: 4px auto 10px; }
.modal-card h3 { margin: 0 0 16px; font-size: 20px; font-weight: 700; text-align: center; }
.modal-card input {
  width: 100%; padding: 13px 15px; font-size: 16px; background: var(--field); color: var(--text);
  border: 1px solid var(--field-border); border-radius: 12px; outline: none; margin-bottom: 8px;
}
.modal-card input:focus { border-color: var(--blue); }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions button { flex: 1; padding: 13px; border-radius: 12px; font-size: 16px; font-weight: 600; }
.ghost { background: var(--bg); color: var(--text); }

.list-scroll { max-height: 48vh; overflow-y: auto; margin: 0 -6px; }
.search-item, .inv-item, .user-row {
  display: flex; align-items: center; gap: 11px; padding: 9px 8px; border-radius: 13px;
}
.search-item:hover, .user-row:hover { background: var(--hover); }
.search-item .grow, .inv-item .grow, .user-row .grow { flex: 1; min-width: 0; }
.search-item b, .inv-item b, .user-row b { font-size: 15px; font-weight: 600; }
.search-item small, .inv-item small, .user-row small { font-size: 13px; color: var(--text-2); }
.row-btn { background: var(--blue); color: #fff; padding: 8px 15px; border-radius: 10px; font-size: 14px; font-weight: 600; flex-shrink: 0; }
.row-btn:active { filter: brightness(0.93); }
.row-btn.rej { background: var(--bg); color: #ff453a; }
.row-btn.acc { background: #34c759; }

/* ===================== TOAST ===================== */
.toast {
  position: fixed; left: 50%; bottom: calc(26px + var(--safe-bottom)); transform: translateX(-50%);
  background: rgba(30, 30, 32, 0.95); color: #fff; padding: 12px 20px; border-radius: 14px;
  font-size: 14.5px; z-index: 100; box-shadow: var(--shadow); max-width: 86%; text-align: center;
  animation: toastin 0.25s ease;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translateX(-50%); } }

/* ===================== MOBILE ===================== */
@media (max-width: 760px) {
  .sidebar { width: 100%; border-right: none; transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1); }
  .chat { position: absolute; inset: 0; transform: translateX(100%); transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1); z-index: 5; }
  .app.in-chat .chat { transform: none; }
  .app.in-chat .sidebar { transform: translateX(-16%); }
  .back { display: flex; }
  .msg { max-width: 82%; }
  .emoji-pop { max-width: none; }
  /* keep modal + its input above the on-screen keyboard */
  .modal { align-items: flex-start; }
  .modal-card { margin-top: calc(7vh + var(--safe-top)); max-height: calc(var(--app-h) - 14vh); }
}
