/* ============================================================================
   msg — clean iOS / iMessage-inspired UI
   Light + dark via prefers-color-scheme. Safe-area aware for standalone PWA.
   ========================================================================== */
:root {
  --bg: #ffffff;
  --bg-elev: #f2f2f7;
  --header: rgba(249, 249, 249, 0.82);
  --text: #000000;
  --text-2: #8e8e93;
  --line: rgba(60, 60, 67, 0.12);
  --accent: #0a84ff;
  --sent-bg: linear-gradient(180deg, #0a90ff 0%, #0a7cff 100%);
  --sent-text: #ffffff;
  --recv-bg: #e9e9eb;
  --recv-text: #000000;
  --field: #ffffff;
  --field-border: rgba(60, 60, 67, 0.2);
  --danger: #ff3b30;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elev: #1c1c1e;
    --header: rgba(22, 22, 24, 0.8);
    --text: #ffffff;
    --text-2: #98989f;
    --line: rgba(84, 84, 88, 0.4);
    --sent-bg: linear-gradient(180deg, #0a90ff 0%, #0a7cff 100%);
    --recv-bg: #262629;
    --recv-text: #ffffff;
    --field: #1c1c1e;
    --field-border: rgba(84, 84, 88, 0.5);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  font-size: 16px;
}
.hidden { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12.5px; }
.error { color: var(--danger); font-size: 13.5px; min-height: 16px; margin-top: 8px; text-align: center; }

/* ---------- inputs / buttons ---------- */
input, textarea, button { font-family: inherit; font-size: 16px; color: var(--text); }
input, textarea {
  width: 100%; background: var(--field); border: 1px solid var(--field-border);
  padding: 13px 15px; border-radius: 12px; outline: none; transition: border-color 0.15s;
}
input:focus, textarea:focus { border-color: var(--accent); }
button { cursor: pointer; border: none; border-radius: 12px; background: none; }
button.primary {
  background: var(--accent); color: #fff; padding: 14px 16px; font-weight: 600; font-size: 16px;
  border-radius: 12px; transition: transform 0.08s, opacity 0.15s;
}
button.primary:active { transform: scale(0.98); }
button.primary:disabled { opacity: 0.5; }
button.tiny { padding: 9px 14px; font-size: 14px; border-radius: 10px; }
button.ghost { background: transparent; color: var(--accent); padding: 8px; }
button.ghost.tiny { padding: 4px 8px; }

/* ---------- views ---------- */
.view { height: 100dvh; width: 100%; }
.center { display: flex; align-items: center; justify-content: center; padding: 24px; }
.card { width: 100%; max-width: 380px; padding: 8px; animation: fadeUp 0.35s ease; }
.card.narrow { max-width: 360px; }
.brand {
  font-size: 34px; font-weight: 700; letter-spacing: -0.5px; text-align: center;
  margin-bottom: 4px; display: flex; align-items: center; justify-content: center; gap: 10px;
}
form { display: flex; flex-direction: column; gap: 11px; margin-top: 18px; }
.spinner { color: var(--text-2); font-size: 13px; margin-top: 12px; text-align: center; }
p.muted { text-align: center; margin: 6px 0; }

.tabs {
  display: flex; gap: 2px; margin: 22px 0 4px; background: var(--bg-elev);
  padding: 3px; border-radius: 11px;
}
.tab {
  flex: 1; background: transparent; color: var(--text); padding: 9px; font-weight: 600;
  font-size: 15px; border-radius: 9px; transition: background 0.2s;
}
.tab.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }
@media (prefers-color-scheme: dark) { .tab.active { background: #48484a; } }

/* ---------- app layout ---------- */
.app { display: flex; height: 100dvh; }
.sidebar {
  width: 340px; flex-shrink: 0; background: var(--bg); border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.side-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(14px + var(--safe-top)) 16px 12px; border-bottom: 1px solid var(--line);
}
.me-name { font-weight: 700; font-size: 18px; letter-spacing: -0.3px; }
.me-id { font-size: 12.5px; color: var(--text-2); margin-top: 1px; }
.me-id span { color: var(--accent); font-weight: 600; letter-spacing: 0.5px; }
#logout-btn { font-size: 20px; }

.start { flex-direction: row; gap: 8px; margin: 12px; }
.start input { text-transform: uppercase; letter-spacing: 1px; border-radius: 12px; }
.start input::placeholder { letter-spacing: normal; text-transform: none; }
.start-error { margin: -4px 14px 0; text-align: left; }

.convos { flex: 1; overflow-y: auto; padding: 6px 8px; -webkit-overflow-scrolling: touch; }
.convo {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 14px;
  cursor: pointer; transition: background 0.15s;
}
.convo:active { background: var(--bg-elev); }
.convo.active { background: var(--bg-elev); }
.avatar-wrap { position: relative; flex-shrink: 0; }
.avatar {
  width: 46px; height: 46px; border-radius: 50%; display: flex; align-items: center;
  justify-content: center; font-weight: 600; color: #fff; font-size: 18px;
}
.avatar-wrap .dot {
  position: absolute; bottom: 0; right: 0; width: 13px; height: 13px;
  border: 2.5px solid var(--bg); border-radius: 50%;
}
.avatar-wrap .dot:not(.online) { display: none; }
.convo-meta { overflow: hidden; }
.convo-name { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-id { font-size: 13px; color: var(--text-2); margin-top: 1px; }

/* ---------- chat pane ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; background: var(--bg); }
.chat-head {
  display: flex; align-items: center; gap: 8px; padding: calc(10px + var(--safe-top)) 16px 10px;
  border-bottom: 1px solid var(--line); background: var(--header);
  backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  position: sticky; top: 0; z-index: 5;
}
.back-btn { display: none; font-size: 26px; color: var(--accent); padding: 0 4px; line-height: 1; }
.peer { text-align: left; }
.peer-name { display: flex; align-items: center; gap: 7px; font-weight: 600; font-size: 17px; letter-spacing: -0.2px; }
.peer-id { font-size: 12.5px; }

.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-2); display: inline-block; flex-shrink: 0; }
.dot.online { background: #34c759; }

.messages {
  flex: 1; overflow-y: auto; padding: 16px 14px 6px; display: flex; flex-direction: column;
  gap: 3px; -webkit-overflow-scrolling: touch; scroll-behavior: smooth;
}
.empty { margin: auto; text-align: center; color: var(--text-2); padding: 24px; }
.empty-emoji { font-size: 52px; margin-bottom: 10px; }

/* message rows */
.msg-row { max-width: 76%; display: flex; flex-direction: column; position: relative; margin-top: 2px; }
.msg-row.me { align-self: flex-end; align-items: flex-end; }
.msg-row.them { align-self: flex-start; align-items: flex-start; }
.msg-row.appear { animation: msgIn 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); }

.msg {
  max-width: 100%; padding: 8px 14px; border-radius: 20px; font-size: 16px; line-height: 1.32;
  word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg-row.me .msg { background: var(--sent-bg); color: var(--sent-text); border-bottom-right-radius: 7px; }
.msg-row.them .msg { background: var(--recv-bg); color: var(--recv-text); border-bottom-left-radius: 7px; }
.msg.gif { padding: 0; background: transparent !important; }
.msg.gif img { max-width: 220px; width: 100%; border-radius: 18px; display: block; }
.msg-time { display: none; }

.reply-quote {
  font-size: 13px; color: var(--text-2); padding: 3px 12px 6px; margin-bottom: -4px;
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer; opacity: 0.85;
}
.msg-row.me .reply-quote { text-align: right; }
.reply-quote b { color: var(--text-2); font-weight: 600; margin-right: 5px; }
.reply-quote::before { content: "↩ "; opacity: 0.6; }

/* action toolbar */
.msg-actions {
  position: absolute; top: 50%; transform: translateY(-50%); display: flex; gap: 4px;
  opacity: 0; pointer-events: none; transition: opacity 0.14s; z-index: 2;
}
.msg-row.me .msg-actions { right: 100%; margin-right: 8px; }
.msg-row.them .msg-actions { left: 100%; margin-left: 8px; }
.msg-row:hover .msg-actions { opacity: 1; pointer-events: auto; }
.msg-actions button {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 50%;
  width: 30px; height: 30px; font-size: 14px; display: flex; align-items: center;
  justify-content: center; color: var(--text); box-shadow: var(--shadow);
}
.msg-actions button:active { transform: scale(0.9); }

/* reactions */
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction {
  display: inline-flex; align-items: center; gap: 3px; background: var(--bg-elev);
  border: 1px solid var(--line); border-radius: 14px; padding: 2px 9px; font-size: 13px;
  cursor: pointer; user-select: none; animation: pop 0.2s ease;
}
.reaction.mine { background: var(--accent); border-color: var(--accent); color: #fff; }
.reaction.mine .count { color: rgba(255, 255, 255, 0.85); }
.reaction .count { color: var(--text-2); font-size: 12px; }

.seen { font-size: 12px; color: var(--text-2); margin-top: 3px; padding-right: 4px; }

/* typing bubble */
.typing-bubble {
  display: inline-flex; gap: 4px; align-items: center; background: var(--recv-bg);
  padding: 12px 15px; border-radius: 20px; border-bottom-left-radius: 7px;
}
.typing-bubble span {
  width: 8px; height: 8px; border-radius: 50%; background: var(--text-2);
  animation: typing 1.3s infinite ease-in-out;
}
.typing-bubble span:nth-child(2) { animation-delay: 0.18s; }
.typing-bubble span:nth-child(3) { animation-delay: 0.36s; }

/* react picker */
.react-picker {
  position: fixed; display: flex; gap: 2px; background: var(--bg);
  border: 1px solid var(--line); border-radius: 24px; padding: 6px 8px;
  box-shadow: var(--shadow); z-index: 50; animation: pop 0.16s ease;
}
.react-picker button { font-size: 24px; padding: 3px 5px; border-radius: 50%; transition: transform 0.1s; }
.react-picker button:hover { transform: scale(1.25); }

/* reply bar */
.reply-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 16px; border-top: 1px solid var(--line); background: var(--bg-elev); font-size: 14px;
}
.reply-bar-label { color: var(--accent); font-weight: 600; margin-right: 6px; }
.reply-bar-text { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--text-2); }

/* composer */
.composer {
  display: flex; align-items: flex-end; gap: 8px; flex-direction: row;
  padding: 8px 12px calc(8px + var(--safe-bottom)); border-top: 1px solid var(--line); background: var(--bg); margin: 0;
}
.gif-btn {
  flex-shrink: 0; font-weight: 700; font-size: 12px; letter-spacing: 0.5px; width: 38px; height: 38px;
  border-radius: 50%; background: var(--bg-elev); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
}
.composer textarea {
  resize: none; max-height: 130px; line-height: 1.35; padding: 9px 15px; border-radius: 20px;
  background: var(--field);
}
.send-btn {
  flex-shrink: 0; width: 38px; height: 38px; border-radius: 50%; background: var(--accent);
  color: #fff; font-size: 17px; display: flex; align-items: center; justify-content: center;
  transition: transform 0.1s, opacity 0.15s;
}
.send-btn:active { transform: scale(0.9); }

/* gif panel */
.gif-panel {
  position: absolute; bottom: calc(66px + var(--safe-bottom)); left: 12px; right: 12px; max-width: 460px; height: 320px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 18px;
  display: flex; flex-direction: column; z-index: 20; overflow: hidden; box-shadow: var(--shadow);
  animation: fadeUp 0.2s ease;
}
.gif-search { display: flex; gap: 8px; padding: 10px; border-bottom: 1px solid var(--line); }
.gif-grid { flex: 1; overflow-y: auto; padding: 8px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.gif-grid img { width: 100%; border-radius: 10px; cursor: pointer; display: block; }
.gif-grid img:active { opacity: 0.7; }
.gif-empty { grid-column: 1/-1; text-align: center; color: var(--text-2); padding: 24px; font-size: 14px; }

/* toast */
.toast {
  position: fixed; bottom: calc(28px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: rgba(30, 30, 32, 0.95); color: #fff; padding: 11px 20px; border-radius: 22px;
  z-index: 100; font-size: 14px; box-shadow: var(--shadow); animation: fadeUp 0.2s ease;
}

/* ---------- keyframes ---------- */
@keyframes msgIn { from { opacity: 0; transform: translateY(10px) scale(0.96); } to { opacity: 1; transform: none; } }
@keyframes fadeUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(0.6); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; scroll-behavior: auto !important; }
}

/* ---------- responsive (phone) ---------- */
@media (max-width: 760px) {
  .sidebar { width: 100%; }
  .chat { display: none; position: absolute; inset: 0; z-index: 10; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat { display: flex; }
  .back-btn { display: inline-flex; }
  .msg-row { max-width: 82%; }
  .gif-panel { left: 8px; right: 8px; max-width: none; }
}
@media (hover: none) {
  .msg-actions { position: static; transform: none; opacity: 0.5; pointer-events: auto; margin: 4px 2px 0; }
  .msg-actions button { width: 26px; height: 26px; box-shadow: none; }
}
