:root {
  --bg: #14111a;
  --surface: #241f2d;
  --text: #ece8f1;
  --muted: #8f879b;
  --accent: #b9416a;
  --accent-text: #ffffff;
  --danger: #e0707a;
  --line: #ffffff12;
  --radius: 18px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100dvh);
  overflow: hidden;
}

[hidden] { display: none !important; }

section { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: calc(env(safe-area-inset-top) + 8px) 14px 8px;
  border-bottom: 1px solid var(--line);
}

.bar-title { flex: 1; font-size: 20px; font-weight: 700; }
.bar-action { border: none; background: none; color: var(--accent); font: inherit; font-weight: 600; }
.bar-back { border: none; background: none; color: var(--accent); font-size: 34px; line-height: 1; padding: 0 6px 4px 0; }
.bar-who { display: flex; flex-direction: column; min-width: 0; }
#chat-name { font-weight: 600; }
#chat-status { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.avatar { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; background: var(--surface); flex: none; }
.avatar.small { width: 36px; height: 36px; }

#chat-list { flex: 1; overflow-y: auto; }

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

.chat-item:active { background: var(--surface); }
.chat-text { flex: 1; min-width: 0; }
.chat-top { display: flex; justify-content: space-between; gap: 8px; }
.chat-name { font-weight: 600; }
.chat-time { font-size: 12px; color: var(--muted); flex: none; }
.chat-preview { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-preview.typing { color: var(--accent); }

#log {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.day { align-self: center; margin: 14px 0 8px; font-size: 12px; color: var(--muted); }

.row { display: flex; flex-direction: column; max-width: 80%; margin-top: 4px; }
.row.user { align-self: flex-end; align-items: flex-end; }
.row.assistant { align-self: flex-start; align-items: flex-start; }
.row .who { font-size: 12px; font-weight: 600; color: var(--muted); margin: 6px 8px 2px; }

.bubble { padding: 8px 13px; border-radius: var(--radius); white-space: pre-wrap; overflow-wrap: anywhere; }
.user .bubble { background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 6px; }
.assistant .bubble { background: var(--surface); border-bottom-left-radius: 6px; }
.row.failed .bubble { opacity: 0.5; }

.photo {
  display: block;
  width: min(68vw, 300px);
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--surface);
  cursor: zoom-in;
}

.row time { font-size: 11px; color: var(--muted); margin: 2px 6px 0; }

.notice { align-self: center; max-width: 90%; margin: 8px 0; font-size: 13px; color: var(--danger); text-align: center; overflow-wrap: anywhere; }
.notice button { margin-left: 8px; padding: 2px 10px; border: 1px solid var(--danger); border-radius: 999px; background: none; color: var(--danger); font: inherit; }

.typing-row .bubble { display: flex; gap: 4px; padding: 12px 14px; }
.typing-row span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing-row span:nth-child(2) { animation-delay: 0.2s; }
.typing-row span:nth-child(3) { animation-delay: 0.4s; }

@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

#composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}

body.keyboard #composer { padding-bottom: 8px; }

#input {
  flex: 1;
  min-height: 40px;
  max-height: 140px;
  padding: 9px 14px;
  border: 1px solid #ffffff1f;
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  resize: none;
  outline: none;
  overflow-y: hidden;
}

#send { flex: none; width: 40px; height: 40px; border: none; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; }

#login { flex: 1; display: flex; flex-direction: column; justify-content: center; gap: 12px; width: min(100%, 360px); margin: 0 auto; padding: 16px; }
#login input { padding: 11px 14px; border: 1px solid #ffffff1f; border-radius: 12px; background: var(--surface); color: var(--text); font: inherit; outline: none; }
#login button { padding: 11px 14px; border: none; border-radius: 12px; background: var(--accent); color: var(--accent-text); font: inherit; font-weight: 600; }
#login-error { min-height: 1.4em; margin: 0; color: var(--danger); font-size: 14px; text-align: center; }


#album-grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; padding: 2px 2px env(safe-area-inset-bottom); align-content: start; }
#album-grid img { width: 100%; aspect-ratio: 2 / 3; object-fit: cover; display: block; }
.album-empty { grid-column: 1 / -1; padding: 40px 16px; text-align: center; color: var(--muted); }

#lightbox { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; background: #000000e6; }
#lightbox img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }

/* ---- v2: tabs, feed, people, profiles */
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.bar-quiet { border: none; background: none; color: var(--muted); font-size: 18px; letter-spacing: 1px; opacity: 0.6; }

#tabs {
  flex: none;
  display: flex;
  border-top: 1px solid var(--line);
  background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}
#tabs button { flex: 1; padding: 11px 0 9px; border: none; background: none; color: var(--muted); font: 600 13px/1 inherit; font-family: inherit; }
#tabs button.active { color: var(--accent); }
body.in-chat #tabs { display: none; }

.card { margin: 10px 10px 0; padding: 12px; border: 1px solid var(--line); border-radius: 16px; background: #1b1722; }
.composer-card { display: flex; gap: 8px; align-items: flex-end; }
.composer-card textarea, .comment-form input {
  flex: 1; padding: 9px 12px; border: 1px solid #ffffff1f; border-radius: 12px;
  background: var(--surface); color: var(--text); font: inherit; resize: none; outline: none;
}
.composer-card button, .comment-form button { padding: 9px 14px; border: none; border-radius: 12px; background: var(--accent); color: #fff; font: inherit; font-weight: 600; }

.post-head { display: flex; align-items: center; gap: 10px; }
.post-head .avatar { width: 40px; height: 40px; }
.post-head .name { font-weight: 600; }
.post-head .when { font-size: 12px; color: var(--muted); }
.post-text { margin: 10px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.post-photo { display: block; width: 100%; margin-top: 10px; border-radius: 12px; aspect-ratio: 2 / 3; object-fit: cover; background: var(--surface); }
.link-card { display: block; margin-top: 10px; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; color: inherit; text-decoration: none; background: var(--surface); }
.link-card img { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }
.link-card span { display: block; padding: 8px 10px; font-size: 14px; font-weight: 600; }
.post-actions { display: flex; align-items: center; gap: 10px; margin-top: 10px; font-size: 14px; color: var(--muted); }
.post-actions button { border: none; background: none; color: var(--muted); font: inherit; padding: 0; }
.post-actions button.mine { color: var(--accent); }
.comments { margin-top: 8px; border-top: 1px solid var(--line); padding-top: 6px; }
.comment { margin: 6px 0; font-size: 14px; overflow-wrap: anywhere; }
.comment b { margin-right: 6px; }
.comment-form { display: flex; gap: 6px; margin-top: 8px; }
.comment-form input { padding: 7px 10px; font-size: 14px; }
.comment-form button { padding: 7px 12px; font-size: 14px; }
.more { border: none; background: none; color: var(--muted); font: inherit; font-size: 13px; padding: 0; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.person { display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px 14px; border: none; border-bottom: 1px solid var(--line); background: none; color: inherit; font: inherit; text-align: left; }
.person .avatar { width: 56px; height: 56px; }
.person .status { font-size: 14px; color: var(--muted); }

.profile-top { display: flex; flex-direction: column; align-items: center; padding: 18px 16px 8px; text-align: center; }
.profile-top .avatar { width: 120px; height: 120px; }
.profile-top h2 { margin: 10px 0 2px; font-size: 22px; }
.profile-top .status { color: var(--muted); font-size: 14px; }
.profile-section { margin: 12px 16px 0; }
.profile-section h3 { margin: 0 0 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips span { padding: 3px 10px; border-radius: 999px; background: var(--surface); font-size: 13px; }
.bond { margin-top: 4px; height: 8px; border-radius: 999px; background: var(--surface); overflow: hidden; }
.bond div { height: 100%; background: var(--accent); }
.bond-label { display: flex; justify-content: space-between; font-size: 13px; }
.message-button { display: block; width: calc(100% - 32px); margin: 14px 16px 4px; padding: 10px; border: none; border-radius: 12px; background: var(--accent); color: #fff; font: inherit; font-weight: 600; }

.overlay { position: fixed; inset: 0; z-index: 5; display: flex; flex-direction: column; background: var(--bg); }
.stat-row { display: flex; justify-content: space-between; padding: 9px 16px; border-bottom: 1px solid var(--line); }
.stat-row span:last-child { font-variant-numeric: tabular-nums; color: var(--muted); }
.stat-event { padding: 8px 16px; font-size: 14px; border-bottom: 1px solid var(--line); }
.stat-event time { display: block; font-size: 12px; color: var(--muted); }
