/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:       #007AFF;
  --primary-dark:  #0062CC;
  --bg:            #000000;
  --card:          #1C1C1E;
  --card-2:        #2C2C2E;
  --text:          #FFFFFF;
  --text-2:        #AEAEB2;
  --text-3:        #636366;
  --border:        #38383A;
  --bubble-out:    #007AFF;
  --bubble-in:     #2C2C2E;
  --txt-out:       #FFFFFF;
  --txt-in:        #FFFFFF;
  --danger:        #FF3B30;
  --success:       #34C759;
  --warn:          #FF9500;
  --radius:        18px;
  --nav-h:         56px;
  --header-h:      56px;
  --input-h:       60px;
  --sai-bottom:    env(safe-area-inset-bottom, 0px);
  --sai-top:       env(safe-area-inset-top, 0px);
}

html {
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
  color-scheme: dark;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

a { color: inherit; text-decoration: none; }

/* ── App shell ── */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 680px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* ── Login ── */
.login-page {
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
}
.login-container {
  width: 100%;
  max-width: 360px;
  padding: 32px 24px;
  text-align: center;
}
.login-logo { font-size: 64px; margin-bottom: 16px; }
.login-title { font-size: 28px; font-weight: 700; margin-bottom: 6px; }
.login-sub { color: var(--text-2); font-size: 15px; margin-bottom: 32px; }
.login-form { display: flex; flex-direction: column; gap: 12px; }
.input-group { position: relative; }
.login-input {
  width: 100%;
  padding: 14px 44px 14px 16px;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  font-size: 17px;
  background: var(--card-2);
  color: var(--text);
  outline: none;
  transition: border-color .2s;
  -webkit-text-fill-color: var(--text);
}
.login-input:focus { border-color: var(--primary); }
.login-input.input-error { border-color: var(--danger); }
.toggle-pass {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 18px;
}
.error-msg { color: var(--danger); font-size: 14px; }
.btn-primary {
  padding: 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
}
.btn-primary:active { opacity: .85; }
.btn-secondary {
  padding: 12px 20px;
  background: var(--border);
  color: var(--text);
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

/* ── Headers ── */
.chat-header, .page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 16px;
  padding-top: var(--sai-top);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}
.header-user { display: flex; align-items: center; gap: 8px; }
.header-info { display: flex; flex-direction: column; }
.header-label { font-size: 10px; color: var(--text-2); text-transform: uppercase; letter-spacing: .4px; margin-bottom: 1px; }
.header-name { font-size: 14px; font-weight: 600; }
.header-status { font-size: 11px; color: var(--text-2); }
.header-status.online { color: var(--success); }
.header-status.typing { color: var(--success); }
.typing-dot {
  display: inline-block;
  width: 4px; height: 4px;
  border-radius: 50%;
  background: currentColor;
  margin-left: 2px;
  vertical-align: middle;
  animation: typing-bounce .9s ease-in-out infinite;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .30s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30%            { transform: translateY(-3px); opacity: 1; }
}
.header-actions { display: flex; gap: 4px; align-items: center; }
.logout-countdown {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-3);
  background: var(--card-2);
  padding: 3px 8px;
  border-radius: 8px;
  letter-spacing: .2px;
}
.logout-countdown.warn   { color: var(--warn); }
.logout-countdown.danger { color: var(--danger); }
.header-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%; font-size: 18px;
  background: none; border: none; cursor: pointer;
  transition: background .15s;
}
.header-btn:hover { background: var(--card-2); }
.logout-btn { color: var(--text-3); }
.logout-btn:hover { color: var(--danger); background: rgba(255,59,48,.12); }
.page-title { font-size: 17px; font-weight: 700; }
.back-btn { color: var(--primary); font-size: 17px; padding: 8px 0; min-width: 60px; background: none; border: none; cursor: pointer; }

/* ── Avatar ── */
.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px; color: #fff;
  flex-shrink: 0; overflow: hidden; position: relative;
}
.avatar.small { width: 28px; height: 28px; font-size: 11px; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar:has(> img) { border-radius: 10px; }

/* ── Messages ── */
.messages-wrap {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  overflow-anchor: none; /* prevent browser from adjusting scrollTop when new messages appear at top */
}
.messages-list {
  display: flex;
  flex-direction: column-reverse; /* newest message last in DOM = visual top; no scroll-to-bottom needed */
  padding: 12px 12px 8px;
  gap: 2px;
}

.msg-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 6px;
}
.msg-group.outgoing { align-items: flex-end; }
.msg-group.incoming { align-items: flex-start; }

.msg-sender {
  font-size: 12px;
  color: var(--text-2);
  padding: 0 12px;
  margin-bottom: 2px;
}

.bubble {
  max-width: 72%;
  padding: 10px 14px;
  border-radius: var(--radius);
  position: relative;
  word-break: break-word;
  font-size: 15px;
  cursor: pointer;
  user-select: text;
  transition: opacity .1s;
}
.bubble:active { opacity: .85; }

.msg-group.outgoing .bubble {
  background: var(--bubble-out);
  color: var(--txt-out);
  border-bottom-right-radius: 4px;
}
.msg-group.incoming .bubble {
  background: var(--bubble-in);
  color: var(--txt-in);
  border-bottom-left-radius: 4px;
}

.bubble.deleted {
  opacity: .5;
  font-style: italic;
  font-size: 14px;
}

.bubble-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 3px;
  font-size: 11px;
  opacity: .7;
  justify-content: flex-end;
}
.msg-group.incoming .bubble-meta { justify-content: flex-start; }
.edited-tag { font-style: italic; }
.read-receipt { font-weight: 500; font-size: 10px; }


/* Reply in bubble */
.bubble-reply {
  background: rgba(0,0,0,.12);
  border-left: 3px solid rgba(255,255,255,.6);
  border-radius: 8px;
  padding: 6px 8px;
  margin-bottom: 6px;
  font-size: 13px;
}
.msg-group.incoming .bubble-reply {
  background: rgba(0,0,0,.06);
  border-left-color: var(--primary);
}
.bubble-reply-name { font-weight: 600; margin-bottom: 2px; }
.bubble-reply-text { opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bubble-reply-thumb {
  width: 44px; height: 44px;
  object-fit: cover;
  border-radius: 6px;
  display: block;
  margin-top: 4px;
}

/* Image & video bubbles — no internal padding, transparent bg */
.bubble.image-bubble,
.bubble.video-bubble-outer { padding: 4px; background: transparent !important; }
.bubble-img {
  max-width: 100%;
  max-height: 280px;
  border-radius: 14px;
  display: block;
  cursor: pointer;
  object-fit: cover;
}

/* Voice bubble */
.voice-player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
}
.voice-play-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: rgba(255,255,255,.25);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}
.msg-group.incoming .voice-play-btn { background: rgba(0,0,0,.1); }
.voice-spin {
  display: block;
  width: 16px; height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.25);
  border-top-color: rgba(255,255,255,.85);
  animation: voice-spin .7s linear infinite;
}
.msg-group.incoming .voice-spin {
  border-color: rgba(0,0,0,.12);
  border-top-color: var(--primary);
}
@keyframes voice-spin { to { transform: rotate(360deg); } }
.voice-scrubber {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: rgba(255,255,255,.35);
  border-radius: 2px;
  cursor: pointer;
  margin: 0;
  padding: 0;
}
.voice-scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: rgba(255,255,255,.95);
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
  cursor: pointer;
}
.voice-scrubber::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: rgba(255,255,255,.95);
  border: none;
  cursor: pointer;
}
.msg-group.incoming .voice-scrubber { background: var(--border); }
.msg-group.incoming .voice-scrubber::-webkit-slider-thumb { background: var(--primary); }
.msg-group.incoming .voice-scrubber::-moz-range-thumb { background: var(--primary); }
.voice-duration { font-size: 12px; opacity: .8; min-width: 32px; }

/* Date separator */
.date-sep {
  text-align: center;
  font-size: 12px;
  color: var(--text-2);
  margin: 8px 0;
  padding: 2px 10px;
}

/* ── Upload indicator ── */
.upload-indicator {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 5px;
  padding: 8px 16px;
  background: var(--card);
  border-top: 1px solid var(--border);
}
.upload-label { font-size: 13px; color: var(--text-2); }
.upload-bar-track {
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.upload-bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 2px;
  width: 0%;
  transition: width .15s;
}

/* ── Video duration badge ── */
.video-duration-badge {
  position: absolute;
  top: 6px;
  right: 8px;
  background: rgba(0,0,0,.55);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 10px;
  pointer-events: none;
  z-index: 2;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.video-duration-badge:empty { display: none; }

/* ── Image load placeholder ── */
.img-load-wrap {
  position: relative;
  min-width: 100px;
  min-height: 60px;
  background: var(--card-2);
  border-radius: 14px;
  overflow: hidden;
}
.img-load-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
}
.img-load-spinner::after {
  content: '';
  width: 24px; height: 24px;
  border-radius: 50%;
  border: 3px solid rgba(128,128,128,.2);
  border-top-color: var(--primary);
  animation: voice-spin .7s linear infinite;
}

/* ── Reply bar ── */
.reply-bar {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: var(--card);
  border-top: 1px solid var(--border);
  gap: 10px;
}
.reply-bar-inner {
  flex: 1;
  border-left: 3px solid var(--primary);
  padding-left: 8px;
  font-size: 13px;
  overflow: hidden;
}
.reply-bar-name { font-weight: 600; color: var(--primary); display: block; }
.reply-bar-text { color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.reply-bar-thumb {
  width: 40px; height: 40px;
  object-fit: cover;
  border-radius: 5px;
  display: block;
  margin-top: 4px;
  flex-shrink: 0;
}
.reply-cancel {
  background: none; border: none;
  color: var(--text-2); font-size: 18px; cursor: pointer; padding: 4px;
}

/* ── Emoji picker ── */
.emoji-picker {
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 8px;
  max-height: 220px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
}
.emoji-item {
  font-size: 26px;
  padding: 4px;
  text-align: center;
  cursor: pointer;
  border-radius: 6px;
  transition: background .1s;
  line-height: 1.3;
}
.emoji-item:hover { background: var(--bg); }

/* ── Input area ── */
.input-area {
  display: flex;
  align-items: flex-end;
  padding: 8px 8px calc(8px + var(--sai-bottom));
  gap: 6px;
  background: var(--card);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.input-wrap {
  flex: 1;
  background: var(--bg);
  border-radius: 20px;
  border: 1px solid var(--border);
  overflow: hidden;
}
.input-wrap [contenteditable] {
  display: block;
  width: 100%;
  padding: 9px 14px;
  background: transparent;
  border: none;
  outline: none;
  font-size: 16px; /* 16px prevents iOS Safari auto-zoom on focus */
  line-height: 1.4;
  color: var(--text);
  overflow-y: auto;
  max-height: 120px;
  font-family: inherit;
  word-break: break-word;
}
.input-wrap [contenteditable]:empty::before {
  content: attr(data-placeholder);
  color: var(--text-2);
  pointer-events: none;
}
.input-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--bg);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-2);
  flex-shrink: 0;
  transition: background .15s;
}
.input-btn:hover { background: var(--border); }
.input-btn.recording {
  background: var(--danger);
  color: white;
  animation: pulse 1s infinite;
}
.send-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--primary);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: white;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}
.send-btn:active { transform: scale(.92); }

/* ── Voice recording overlay ── */
.voice-recording {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 16px 20px calc(16px + var(--sai-bottom));
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 100;
  box-shadow: 0 -4px 20px rgba(0,0,0,.15);
}
.voice-pulse {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--danger);
  animation: pulse 1s infinite;
  flex-shrink: 0;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: .6; transform: scale(.85); }
}
#voiceTimer { font-size: 20px; font-weight: 600; min-width: 48px; }
.voice-cancel { margin-left: auto; }
.voice-send { background: var(--primary); color: white; border-radius: 10px; padding: 10px 18px; border: none; font-size: 15px; font-weight: 600; cursor: pointer; }

/* ── Bottom nav ── */
.bottom-nav {
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: var(--sai-bottom);
  flex-shrink: 0;
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0 6px;
  color: var(--text-2);
  transition: color .15s;
  font-size: 10px;
  gap: 2px;
}
.nav-item.active { color: var(--primary); }
.nav-icon { font-size: 22px; }
.nav-label { font-size: 10px; }

/* ── Image modal ── */
.drop-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,122,255,.12);
  border: 3px dashed var(--primary);
  border-radius: 16px;
  z-index: 999;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  color: var(--primary);
  pointer-events: none;
}
.drop-overlay.active { display: flex; }

.img-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}
.img-modal img {
  max-width: 100%;
  max-height: 80vh;
  object-fit: contain;
  border-radius: 8px;
}
.modal-meta { color: rgba(255,255,255,.6); font-size: 13px; margin-top: 12px; }

/* ── Context menu ── */
.ctx-menu {
  position: fixed;
  background: var(--card);
  border-radius: 14px;
  box-shadow: 0 8px 32px rgba(0,0,0,.25);
  overflow: hidden;
  z-index: 300;
  min-width: 180px;
}
.ctx-menu button {
  display: block;
  width: 100%;
  padding: 13px 16px;
  background: none;
  border: none;
  text-align: left;
  font-size: 15px;
  cursor: pointer;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}
.ctx-menu button:last-child { border-bottom: none; }
.ctx-menu button:hover { background: var(--bg); }
.ctx-menu button.danger { color: var(--danger); }
.ctx-overlay {
  position: fixed; inset: 0; z-index: 299;
}

/* Edit overlay */
.edit-overlay {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 12px 12px calc(12px + var(--sai-bottom));
  z-index: 100;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.edit-overlay textarea {
  flex: 1;
  padding: 10px 14px;
  border: 1.5px solid var(--primary);
  border-radius: 12px;
  font-size: 15px;
  background: var(--card);
  color: var(--text);
  outline: none;
  resize: none;
  font-family: inherit;
  max-height: 120px;
}
.edit-cancel { color: var(--text-2); background: none; border: none; font-size: 15px; cursor: pointer; padding: 10px; }
.edit-save {
  background: var(--primary); color: white; border: none;
  border-radius: 10px; padding: 10px 18px; font-size: 15px; font-weight: 600; cursor: pointer;
}

/* ── Photos ── */
.photos-wrap { flex: 1; overflow-y: auto; padding: 4px; }
.photos-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
}
/* Thumbnail delete button */
.thumb-delete {
  position: absolute;
  top: 4px; right: 4px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: rgba(0,0,0,.6);
  color: #fff;
  border: none;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
  display: flex; align-items: center; justify-content: center;
  opacity: 0;
  transition: opacity .15s;
}
.photo-thumb:hover .thumb-delete { opacity: 1; }
@media (hover: none) { .thumb-delete { opacity: 1; } }

.photo-thumb {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  background: var(--border);
}
.photo-thumb img {
  width: 100%; height: 100%;
  object-fit: cover;
  cursor: pointer;
  transition: opacity .15s;
}
.photo-thumb img:hover { opacity: .9; }
.empty-state {
  grid-column: 1/-1;
  text-align: center;
  padding: 60px 20px;
  color: var(--text-2);
  font-size: 15px;
  line-height: 2;
}
.loading { color: var(--text-2); padding: 40px; text-align: center; grid-column: 1/-1; }

/* ── Admin ── */
.admin-wrap { flex: 1; overflow-y: auto; padding-bottom: calc(var(--nav-h) + 12px); }
.admin-section {
  background: var(--card);
  margin: 12px;
  border-radius: 14px;
  overflow: hidden;
}
.section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: .5px;
  padding: 14px 16px 8px;
}
.admin-form { padding: 0 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.form-row { display: flex; flex-direction: column; gap: 4px; }
.form-row label { font-size: 15px; color: var(--text-2); }
.form-row input[type=text],
.form-row input[type=number],
.form-row input[type=password],
.form-row input[type=email],
.form-row textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  background: var(--card-2);
  color: var(--text);
  outline: none;
  -webkit-text-fill-color: var(--text);
  font-family: inherit;
}
.form-row input:focus,
.form-row textarea:focus { border-color: var(--primary); }
.form-row small { font-size: 12px; color: var(--text-2); }
.checkbox-row { flex-direction: row; align-items: center; }
.form-hint { font-size: 14px; color: var(--text-2); line-height: 1.5; margin-top: 4px; }

/* ── iOS-style toggle switch ── */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--card-2);
  border-radius: 12px;
  margin: 4px 0;
}
.toggle-label { font-size: 15px; color: var(--text); }
.toggle-switch { position: relative; flex-shrink: 0; }
.toggle-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-track {
  display: block;
  width: 51px; height: 31px;
  background: var(--border);
  border-radius: 16px;
  transition: background .2s;
  cursor: pointer;
  position: relative;
}
.toggle-thumb {
  position: absolute;
  width: 27px; height: 27px;
  background: #fff;
  border-radius: 50%;
  top: 2px; left: 2px;
  transition: transform .2s;
  box-shadow: 0 1px 3px rgba(0,0,0,.35);
}
.toggle-switch input:checked ~ .toggle-track { background: var(--success); }
.toggle-switch input:checked ~ .toggle-track .toggle-thumb { transform: translateX(20px); }
.users-list { padding: 0 0 8px; }
.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.user-row:last-child { border-bottom: none; }
.user-row-left { display: flex; align-items: center; gap: 10px; }
.user-row-name { font-size: 15px; font-weight: 500; }
.user-row-meta { font-size: 12px; color: var(--text-2); }
.user-row-actions { display: flex; gap: 6px; align-items: center; }
.badge-admin {
  display: inline-block;
  background: var(--primary);
  color: white;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 4px;
  vertical-align: middle;
}
.btn-sm {
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}
.btn-sm.btn-danger { border-color: var(--danger); color: var(--danger); }
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--border);
  margin-bottom: 8px;
}
.stat-item {
  background: var(--card);
  padding: 14px 8px;
  text-align: center;
}
.stat-num { font-size: 22px; font-weight: 700; }
.stat-label { font-size: 11px; color: var(--text-2); margin-top: 2px; }
.alert {
  margin: 12px 12px 0;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
}
.alert-success { background: rgba(52,199,89,.15); color: #34C759; border: 1px solid rgba(52,199,89,.3); }
.alert-error { background: rgba(255,59,48,.15); color: #FF6B6B; border: 1px solid rgba(255,59,48,.3); }

/* ── Modal ── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 400;
  padding: 20px;
}
.modal-box {
  background: var(--card);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 340px;
}
.modal-box h3 { font-size: 17px; margin-bottom: 16px; }
.modal-input {
  width: 100%;
  padding: 12px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  margin-bottom: 16px;
}
.modal-input:focus { border-color: var(--primary); }
.modal-btns { display: flex; gap: 10px; justify-content: flex-end; }

/* ── Games ── */
.games-wrap { flex: 1; overflow-y: auto; padding-bottom: calc(var(--nav-h) + 12px); }
.games-intro { padding: 16px 16px 8px; color: var(--text-2); font-size: 15px; }
.games-list { padding: 0 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.game-card {
  background: var(--card);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  cursor: pointer;
  transition: opacity .15s;
  border: 1.5px solid transparent;
}
.game-card:hover { border-color: var(--primary); }
.game-card:active { opacity: .85; }
.game-icon { font-size: 22px; }
.game-name { font-size: 14px; font-weight: 600; }
.game-desc { font-size: 11px; color: var(--text-2); margin-top: 1px; }
.active-games { padding: 16px 12px 0; }
.active-games-title { font-size: 13px; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 8px; }
.active-game-row {
  background: var(--card);
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.game-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.game-title { font-size: 16px; font-weight: 700; }
.game-turn { font-size: 13px; color: var(--text-2); }
.game-score {
  text-align: center;
  padding: 8px;
  font-size: 14px;
  color: var(--text-2);
}
.game-board-wrap {
  display: flex;
  justify-content: center;
  padding: 16px;
  overflow-x: auto;
}
.game-status {
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  padding: 8px;
  min-height: 32px;
}
#newGameBtn { display: block; margin: 0 auto 16px; padding: 12px 32px; }

/* Tic-tac-toe */
.ttt-board {
  display: grid;
  gap: 6px;
  /* grid-template-columns and width set by JS based on board size */
}
.ttt-cell {
  aspect-ratio: 1;
  background: var(--card);
  border-radius: 12px;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s;
}
.ttt-cell:hover:not(.filled) { background: var(--bg); }
.ttt-cell.filled { cursor: default; }
.ttt-cell.winning { background: rgba(52,199,89,.2); border-color: var(--success); }

/* Connect Four */
.c4-board {
  background: var(--primary);
  border-radius: 14px;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.c4-col-btn {
  background: rgba(255,255,255,.15);
  border: none;
  padding: 4px 0;
  cursor: pointer;
  border-radius: 6px;
  color: white;
  font-size: 11px;
  grid-column: span 1;
  transition: background .15s;
}
.c4-col-btn:hover { background: rgba(255,255,255,.3); }
.c4-cell {
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--card);
}
.c4-cell.p1 { background: #FF3B30; }
.c4-cell.p2 { background: #FFCC00; }
.c4-cell.winning { box-shadow: 0 0 0 3px white; }

/* Dots and Boxes */
.dab-board {
  position: relative;
  user-select: none;
}
.dab-dot {
  position: absolute;
  width: 10px; height: 10px;
  background: var(--text);
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.dab-line {
  position: absolute;
  cursor: pointer;
  transition: background .15s;
}
.dab-line.h { height: 8px; transform: translateY(-50%); border-radius: 4px; }
.dab-line.v { width: 8px; transform: translateX(-50%); border-radius: 4px; }
.dab-line:not(.drawn):hover { background: var(--primary) !important; opacity: .6; }
.dab-line.drawn { cursor: default; }
.dab-box {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  border-radius: 4px;
}

/* ── Attach sheet ── */
.attach-sheet {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 500;
}
.attach-sheet-inner {
  /* Centering via transform — works regardless of parent display value */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(360px, calc(100% - 32px));
  padding: 16px;
  background: var(--card);
  border-radius: 18px;
}
.attach-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 10px;
}
.attach-choices button {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 10px;
  padding: 28px 16px;
  background: var(--card);
  border: none; border-radius: 16px;
  color: var(--text); font-size: 15px; font-weight: 500;
  cursor: pointer;
  transition: background .15s;
}
.attach-choices button:hover { background: var(--card-2); }
.attach-big-icon { font-size: 40px; line-height: 1; }
.attach-cancel {
  display: flex; align-items: center; justify-content: center;
  width: 100%; padding: 16px;
  background: var(--card-2);
  border: none; border-radius: 14px;
  color: var(--danger); font-size: 16px; font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.attach-cancel:hover { background: var(--card); }

/* ── Video bubble ── */
.video-bubble {
  max-width: 260px;
  border-radius: 14px;
  overflow: hidden;
  background: #1C1C1E;
}
.video-bubble video {
  display: block;
  width: 100%;
  aspect-ratio: 16/9;
  max-height: 240px;
  border-radius: 14px;
  background: #1C1C1E;
  object-fit: contain;
}

/* ── Nav badges ── */
.nav-icon-wrap { position: relative; display: inline-flex; align-items: center; justify-content: center; }
.nav-badge {
  position: absolute;
  top: -1px; right: -3px;
  width: 9px; height: 9px;
  background: var(--success);
  border-radius: 50%;
  border: 1.5px solid var(--card);
}
.nav-badge-count.badge-fading {
  animation: badge-fadeout 1s ease-in-out forwards;
}
@keyframes badge-fadeout {
  from { opacity: 1; }
  to   { opacity: 0; }
}
.bubble.msg-new::before {
  content: '';
  position: absolute;
  top: -4px; right: -4px;
  width: 9px; height: 9px;
  background: #34C759;
  border-radius: 50%;
  border: 1.5px solid var(--bg);
}
.swipe-reply-arrow {
  position: absolute;
  left: -30px; top: 50%;
  transform: translateY(-50%);
  width: 24px; height: 24px;
  background: var(--card-2);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  user-select: none;
}
.bubble.swipe-ready .swipe-reply-arrow { opacity: 1; }
.bubble.msg-new-fade::before {
  animation: badge-fadeout 1s ease-in-out forwards;
}
.nav-badge-count {
  position: absolute;
  top: -5px; right: -8px;
  min-width: 16px; height: 16px;
  background: #FF3B30;
  border-radius: 8px;
  border: 1.5px solid var(--card);
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
  line-height: 1;
}

/* ── Reversi ── */
.rev-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 3px;
  background: #2a6b2a;
  padding: 8px;
  border-radius: 12px;
  width: min(340px, 92vw);
}
.rev-cell {
  aspect-ratio: 1;
  background: #3a8c3a;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
  position: relative;
  transition: background .1s;
}
.rev-cell.rev-hint { cursor: pointer; }
.rev-cell.rev-hint::after {
  content: '';
  width: 24%;
  height: 24%;
  background: rgba(255,255,255,.45);
  border-radius: 50%;
}
.rev-cell.rev-hint:hover { background: rgba(255,255,255,.15); }
.rev-piece {
  width: 78%;
  height: 78%;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0,0,0,.4);
  transition: transform .15s;
}
.rev-piece.rev-black { background: radial-gradient(circle at 35% 35%, #555, #111); }
.rev-piece.rev-white { background: radial-gradient(circle at 35% 35%, #fff, #ccc); }

/* ── Format toolbar ── */
.format-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: var(--card);
  border-top: 1px solid var(--border);
}
.fmt-btn {
  width: 36px; height: 36px;
  border-radius: 8px;
  background: var(--card-2);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.fmt-btn:active { background: var(--border); }

/* ── Jump to message ── */
.bubble-reply { cursor: pointer; }
@keyframes msg-highlight-fade {
  0%   { box-shadow: 0 0 0 3px rgba(0,122,255,.7); }
  100% { box-shadow: 0 0 0 3px rgba(0,122,255,0); }
}
@keyframes msg-highlight-fade-own {
  0%   { box-shadow: 0 0 0 3px rgba(255,255,255,.85); }
  100% { box-shadow: 0 0 0 3px rgba(255,255,255,0); }
}
.bubble.msg-highlight {
  animation: msg-highlight-fade 1.8s ease-out forwards;
}
.msg-group.outgoing .bubble.msg-highlight {
  animation: msg-highlight-fade-own 1.8s ease-out forwards;
}
.jump-toast {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: rgba(30,30,30,.88);
  color: #fff;
  font-size: 13px;
  padding: 8px 18px;
  border-radius: 20px;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  z-index: 9999;
  white-space: nowrap;
}
.jump-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Calendar ── */
.cal-wrap { flex: 1; overflow-y: auto; padding-bottom: 16px; display: flex; flex-direction: column; }
.cal-nav { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px 8px; }
.cal-nav-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: none; background: var(--card-2); color: var(--text);
  font-size: 22px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.cal-month-title { font-size: 17px; font-weight: 600; margin: 0; }
.cal-grid-head {
  display: grid; grid-template-columns: repeat(7, 1fr);
  padding: 0 8px; margin-bottom: 2px;
}
.cal-grid-head span {
  text-align: center; font-size: 12px; color: var(--text-2);
  font-weight: 500; padding: 4px 0;
}
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); padding: 0 8px; gap: 2px; }
.cal-day {
  display: flex; flex-direction: column; align-items: center;
  padding: 4px 0; cursor: pointer; border-radius: 10px;
  min-height: 50px; justify-content: center; gap: 3px;
}
.cal-day-empty { cursor: default; pointer-events: none; }
.cal-day-num {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%; font-size: 15px;
  position: relative; z-index: 0;
}
.cal-today .cal-day-num { background: var(--primary); color: #fff; font-weight: 700; }
.cal-selected:not(.cal-today) .cal-day-num { border: 2px solid var(--primary); font-weight: 600; }

/* Red heart behind the number for days with events */
.cal-has-event .cal-day-num { color: #fff; font-weight: 600; }
.cal-has-event .cal-day-num::before {
  content: '♥';
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -48%);
  font-size: 34px;
  color: #FF3B30;
  z-index: -1;
  line-height: 1;
  pointer-events: none;
}
.cal-today.cal-has-event .cal-day-num { color: #fff; }
.cal-today.cal-has-event .cal-day-num::before { content: none; } /* blue circle wins */

.cal-view-toggle {
  display: flex; margin: 8px 16px 4px;
  background: var(--card-2); border-radius: 10px; padding: 2px;
}
.cal-toggle-btn {
  flex: 1; padding: 7px; border: none; border-radius: 8px;
  background: transparent; color: var(--text-2);
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: background .2s, color .2s;
}
.cal-toggle-btn.active { background: var(--card); color: var(--text); font-weight: 600; }

.cal-list-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 10px;
}
.cal-list-title { font-weight: 600; font-size: 17px; }
.cal-upcoming-date { font-size: 13px; font-weight: 600; color: var(--text-2); padding: 10px 16px 4px; border-bottom: 1px solid var(--border); }

.cal-events { margin: 12px 16px 0; background: var(--card); border-radius: 16px; overflow: hidden; }
.cal-events-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px 10px; border-bottom: 1px solid var(--border);
}
.cal-events-title { font-weight: 600; font-size: 15px; }
.cal-add-btn {
  background: var(--primary); color: #fff; border: none;
  border-radius: 14px; padding: 5px 13px; font-size: 13px;
  cursor: pointer; font-weight: 500;
}
.cal-event-item {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.cal-event-item:last-child { border-bottom: none; }
.cal-event-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; margin-top: 4px; }
.cal-event-body { flex: 1; }
.cal-event-title { font-size: 15px; font-weight: 500; }
.cal-event-time { font-size: 13px; color: var(--text-2); margin-top: 2px; }
.cal-event-note { font-size: 13px; color: var(--text-2); margin-top: 4px; white-space: pre-wrap; }
.cal-event-del {
  background: none; border: none; color: var(--text-2);
  font-size: 16px; cursor: pointer; padding: 0 4px; flex-shrink: 0;
}
.cal-empty { padding: 20px 16px; color: var(--text-2); font-size: 14px; }

.ev-input {
  width: 100%; padding: 10px 12px;
  background: var(--card-2); border: 1px solid var(--border);
  border-radius: 10px; color: var(--text); font-size: 15px;
  box-sizing: border-box;
}
.ev-textarea { resize: none; font-family: inherit; margin-top: 8px; }

/* ── Text formatting ── */
.txt-xl { font-size: 1.6em; line-height: 1.3; font-weight: 500; }
.txt-rainbow {
  background: linear-gradient(90deg, #ff4444, #ff8c00, #ffd700, #44cc44, #4488ff, #aa44ff);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
}
.spoiler {
  background: var(--text-2); color: var(--text-2);
  border-radius: 4px; cursor: pointer;
  user-select: none; padding: 0 3px;
  transition: background .2s, color .2s;
}
.spoiler.revealed { background: rgba(255,255,255,.12); color: inherit; user-select: text; }
/* Always legible while composing */
#msgInput .spoiler { background: rgba(255,200,0,.25); color: inherit; user-select: text; cursor: text; }

/* Format toolbar extra buttons */
.fmt-strike { text-decoration: line-through; }
.fmt-big { font-size: 12px; font-weight: 700; }
.fmt-rainbow-label {
  background: linear-gradient(90deg, #ff4444, #ff8c00, #ffd700, #44cc44, #4488ff, #aa44ff);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: 700;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 0; height: 0; }

/* ── Utilities ── */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
