/* ============================================================
   Семейный чат — дизайн в стиле Apple (iOS / iMessage)
   ============================================================ */

:root {
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Helvetica Neue", "Segoe UI", Roboto, Arial, sans-serif;

  --bg: #ffffff;
  --chat-bg: #ffffff;
  --bubble-in: #e9e9eb;
  --bubble-in-text: #000000;
  --bubble-out: #007aff;
  --bubble-out-text: #ffffff;
  --text: #000000;
  --muted: #8e8e93;
  --separator: rgba(60, 60, 67, 0.14);
  --material: rgba(255, 255, 255, 0.72);
  --field: #f2f2f7;
  --accent: #007aff;
  --accent-2: #0a84ff;
  --danger: #ff3b30;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.10);
  --shadow-strong: 0 12px 40px rgba(0, 122, 255, 0.28);
  --dot-online: #34c759;
  --dot-offline: #ff3b30;
}

html[data-theme="dark"] {
  --bg: #000000;
  --chat-bg: #000000;
  --bubble-in: #26262a;
  --bubble-in-text: #ffffff;
  --bubble-out: #0a84ff;
  --bubble-out-text: #ffffff;
  --text: #ffffff;
  --muted: #98989f;
  --separator: rgba(84, 84, 88, 0.65);
  --material: rgba(28, 28, 30, 0.72);
  --field: #1c1c1e;
  --accent: #0a84ff;
  --accent-2: #409cff;
  --danger: #ff453a;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.55);
  --shadow-strong: 0 12px 40px rgba(10, 132, 255, 0.35);
  --dot-online: #30d158;
  --dot-offline: #ff453a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  background: var(--chat-bg);
  color: var(--text);
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--chat-bg);
  position: relative;
}

/* --- Заголовок: матовое стекло --- */
.header {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: var(--material);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
}
.header__info { display: flex; align-items: center; gap: 11px; flex: 1; min-width: 0; }
.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 18px;
  color: #fff;
  flex-shrink: 0;
  background: linear-gradient(135deg, #ff9f0a 0%, #ff375f 55%, #bf5af2 100%);
  box-shadow: inset 0 0 0 0.5px rgba(255, 255, 255, 0.3), 0 2px 8px rgba(0, 0, 0, 0.12);
}
.header__title {
  font-weight: 600;
  font-size: 17px;
  letter-spacing: -0.02em;
  line-height: 1.15;
}
.header__status {
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 1px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.header__status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.header__status.online::before {
  background: var(--dot-online);
  animation: pulse 2.2s ease-out infinite;
}
.header__status.offline::before { background: var(--dot-offline); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0.45); }
  70% { box-shadow: 0 0 0 7px rgba(52, 199, 89, 0); }
  100% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0); }
}

.icon-btn {
  border: none;
  background: rgba(120, 120, 128, 0.12);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.15s ease, background 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn:hover { background: rgba(120, 120, 128, 0.2); }
.icon-btn:active { transform: scale(0.9); }
.icon-btn svg { width: 18px; height: 18px; display: block; }

/* --- Баннер соединения --- */
.banner {
  background: linear-gradient(180deg, #ff9f0a, #ff9500);
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  text-align: center;
  letter-spacing: -0.01em;
}

/* --- Лента сообщений --- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.msg {
  max-width: 78%;
  padding: 8px 13px;
  border-radius: 20px;
  line-height: 1.35;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  position: relative;
  font-size: 16.5px;
  letter-spacing: -0.01em;
  animation: bubbleIn 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.2);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
}
@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(10px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.msg.in {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  border-bottom-left-radius: 6px;
}
.msg.out {
  align-self: flex-end;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-2) 100%);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 6px;
}

.msg__author {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 3px;
}
.msg.out .msg__author { color: rgba(255, 255, 255, 0.85); }

.msg__text { display: inline; }

.msg__time {
  font-size: 11px;
  color: var(--muted);
  float: right;
  margin: 6px 0 -2px 10px;
  user-select: none;
}
.msg.out .msg__time { color: rgba(255, 255, 255, 0.7); }

.msg__media { margin: 2px 0 3px; }
.msg__media img,
.msg__media video {
  max-width: 100%;
  border-radius: 14px;
  display: block;
}
.msg__media .sticker { width: 132px; }

.msg.pending { opacity: 0.55; }
.msg.failed { opacity: 0.85; }
.msg.failed .msg__time { color: #ffd60a; font-weight: 600; }

/* --- Кнопка «вниз» --- */
.scroll-down {
  position: absolute;
  right: 16px;
  bottom: 88px;
  z-index: 4;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0.5px solid var(--separator);
  background: var(--material);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  color: var(--accent);
  cursor: pointer;
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  transition: transform 0.15s ease;
}
.scroll-down:active { transform: scale(0.9); }
.scroll-down svg { width: 20px; height: 20px; }

/* --- Панель ввода --- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--material);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--separator);
}
.attach {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(120, 120, 128, 0.12);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.attach:hover { background: rgba(120, 120, 128, 0.2); }
.attach:active { transform: scale(0.9); }
.attach svg { width: 20px; height: 20px; }

.composer__input {
  flex: 1;
  resize: none;
  border: 1px solid var(--separator);
  outline: none;
  background: var(--field);
  color: var(--text);
  font-size: 16.5px;
  font-family: inherit;
  line-height: 1.35;
  max-height: 120px;
  padding: 8px 14px;
  border-radius: 20px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.composer__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.15);
}
.composer__input::placeholder { color: var(--muted); }

.send {
  flex-shrink: 0;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-2) 100%);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: var(--shadow-strong);
  transition: transform 0.15s ease, opacity 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
.send:active { transform: scale(0.88); }
.send:disabled { opacity: 0.4; cursor: default; box-shadow: none; }
.send svg { width: 20px; height: 20px; }

/* --- Служебные сообщения --- */
.sys {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  padding: 3px 12px;
  border-radius: 12px;
}

/* --- Страница доступа --- */
.access-page {
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(120% 120% at 50% 0%, var(--field) 0%, var(--bg) 60%);
}
.access-card {
  width: 100%;
  max-width: 420px;
  background: var(--material);
  backdrop-filter: saturate(180%) blur(24px);
  -webkit-backdrop-filter: saturate(180%) blur(24px);
  border: 0.5px solid var(--separator);
  padding: 28px;
  border-radius: 24px;
  box-shadow: var(--shadow);
}
.access-card h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0 0 8px;
}
.access-card p { color: var(--muted); font-size: 14.5px; line-height: 1.45; margin: 0 0 16px; }
.access-link {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  word-break: break-all;
  background: var(--field);
  border: 0.5px solid var(--separator);
  padding: 12px;
  border-radius: 14px;
  margin: 0 0 16px;
  color: var(--text);
}
.access-actions { display: flex; gap: 10px; }
.access-actions button {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-2) 100%);
  color: #fff;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  transition: transform 0.15s ease, opacity 0.2s ease;
}
.access-actions button:active { transform: scale(0.97); }
.access-actions .secondary { background: rgba(120, 120, 128, 0.16); color: var(--text); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
