:root {
  --border: #dbdbdb;
  --text: #262626;
  --muted: #737373;
  --blue: #3b82f6;
}
* { box-sizing: border-box; }
html, body {
  max-width: 100%;
  overflow-x: hidden; /* safety net: nothing should ever push the page wider than the viewport */
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: #fafafa;
  color: var(--text);
}
button { font-family: inherit; cursor: pointer; border: none; background: none; padding: 0; }
img, svg { display: block; }
a { color: inherit; text-decoration: none; }

/* ---------- layout shells ---------- */
.app { display: flex; min-height: 100vh; width: 100%; max-width: 100%; }
.sidebar {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  position: fixed;
  left: 0; top: 0; bottom: 0;
  width: 72px;
  border-right: 1px solid var(--border);
  background: #fff;
  padding: 24px 8px;
  z-index: 30;
}
.sidebar .logo { padding: 0 12px 32px; display: flex; justify-content: center; font-size: 22px; font-weight: 700; font-style: italic; }
.sidebar nav { display: flex; flex-direction: column; gap: 4px; }
.navitem {
  display: flex; align-items: center; gap: 16px;
  width: 100%; padding: 12px; border-radius: 12px;
}
.navitem:hover { background: #f5f5f5; }
.navitem.active { font-weight: 700; }
.navitem .label { display: none; font-size: 15px; }
.sidebar .footnote { display: none; padding: 0 12px; font-size: 11px; color: #a3a3a3; }

@media (min-width: 768px) {
  .sidebar { display: flex; }
  main { margin-left: 72px; }
  header.mobile-header, nav.bottom-nav { display: none !important; }
}
@media (min-width: 1280px) {
  .sidebar { width: 245px; }
  .sidebar .logo { justify-content: flex-start; }
  .navitem .label { display: inline; }
  .sidebar .footnote { display: block; }
  main { margin-left: 245px; }
}

header.mobile-header {
  position: sticky; top: 0; z-index: 30;
  background: #fff; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  height: 56px; padding: 0 16px;
  width: 100%;
}
header.mobile-header .brand { font-size: 20px; font-weight: 700; font-style: italic; }
header.mobile-header .icons { display: flex; align-items: center; gap: 16px; }
header.mobile-header .profile-title { font-weight: 600; font-size: 16px; }

main {
  flex: 1;
  min-width: 0;      /* critical: lets the flex item shrink below its content's natural width */
  max-width: 100%;
  padding-top: 16px;
  padding-bottom: 80px;
}
@media (min-width: 768px) { main { padding-bottom: 40px; } }

nav.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: #fff; border-top: 1px solid var(--border);
  height: 56px; display: flex; align-items: center; justify-content: space-around;
  z-index: 30;
}

.avatar-ring {
  background: linear-gradient(45deg, #fbbf24, #ec4899, #9333ea);
  border-radius: 999px; padding: 2px;
  flex-shrink: 0;
}
.avatar-ring-inner { background: #fff; border-radius: 999px; padding: 2px; width: 100%; height: 100%; }
.avatar-fill { background: #f5f5f5; border-radius: 999px; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; font-weight: 700; }

/* profile avatar: sized purely by CSS breakpoints, never by inline JS styles */
.avatar-ring.profile-avatar { width: 64px; height: 64px; }
.avatar-ring.profile-avatar .avatar-fill { font-size: 20px; }
@media (min-width: 480px) {
  .avatar-ring.profile-avatar { width: 88px; height: 88px; }
  .avatar-ring.profile-avatar .avatar-fill { font-size: 26px; }
}
@media (min-width: 768px) {
  .avatar-ring.profile-avatar { width: 120px; height: 120px; }
  .avatar-ring.profile-avatar .avatar-fill { font-size: 32px; }
}

/* ---------- stories ---------- */
.stories { display: flex; gap: 16px; overflow-x: auto; padding: 0 16px 16px; max-width: 100%; }
@media (min-width: 768px) { .stories { padding: 0 0 16px; } }
.story { display: flex; flex-direction: column; align-items: center; gap: 4px; flex-shrink: 0; width: 64px; background: none; }
.story .bubble { width: 64px; height: 64px; font-size: 24px; }
.story .label { font-size: 11px; color: #404040; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100%; text-align: center; }

/* ---------- feed ---------- */
.feed { width: 100%; max-width: 470px; margin: 0 auto; }
.post { background: #fff; border: 1px solid var(--border); margin-bottom: 16px; max-width: 100%; }
@media (min-width: 640px) { .post { border-radius: 8px; } }
.post-head { display: flex; align-items: center; justify-content: space-between; padding: 12px; gap: 8px; }
.post-head-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.post-head .avatar-ring { width: 32px; height: 32px; }
.post-user { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.post-tag { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.post-media {
  width: 100%; aspect-ratio: 1/1; display: flex; align-items: center; justify-content: center;
  position: relative; font-size: 64px; color: #fff;
}
.post-media .badge {
  position: absolute; bottom: 12px; left: 12px; right: 12px; font-size: 12px; font-weight: 500;
  background: rgba(0,0,0,0.35); backdrop-filter: blur(4px);
  padding: 4px 8px; border-radius: 6px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.post-actions { display: flex; align-items: center; gap: 16px; padding: 8px 12px 0; }
.post-actions .spacer { margin-left: auto; }
.post-body { padding: 8px 12px 12px; }
.post-likes { font-size: 14px; font-weight: 600; }
.post-caption { font-size: 14px; margin-top: 4px; word-break: break-word; }
.post-caption b { font-weight: 600; }
.post-tags { font-size: 14px; color: #1e3a8a; margin-top: 4px; word-break: break-word; }
.post-comments { font-size: 13px; color: var(--muted); margin-top: 4px; }
.post-time { font-size: 11px; color: #a3a3a3; margin-top: 4px; text-transform: uppercase; letter-spacing: 0.02em; }

/* icon buttons */
.icon-btn svg { width: 24px; height: 24px; }

/* ---------- reels grid ---------- */
.reels-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; width: 100%; max-width: 100%; }
.reel-thumb {
  position: relative; aspect-ratio: 9/16; display: flex; align-items: flex-end;
  padding: 8px; overflow: hidden; color: #fff; min-width: 0;
}
.reel-thumb .play-icon { position: absolute; top: 8px; right: 8px; width: 16px; height: 16px; }
.reel-thumb p { font-size: 11px; font-weight: 500; text-align: left; line-height: 1.2; margin: 0; overflow-wrap: break-word; }

/* ---------- reels player ---------- */
.reels-player { position: fixed; inset: 0; background: #000; z-index: 50; display: none; }
.reels-player.open { display: block; }
.reels-player .close-btn {
  position: absolute; top: 16px; left: 16px; z-index: 30;
  width: 36px; height: 36px; border-radius: 999px; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
}
.reels-scroll { width: 100%; height: 100%; overflow-y: scroll; overflow-x: hidden; scroll-snap-type: y mandatory; scroll-behavior: smooth; }
.reels-scroll::-webkit-scrollbar { display: none; }
.reel-slide {
  width: 100%; height: 100%; scroll-snap-align: start; position: relative;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.reel-slide .center { position: relative; z-index: 10; text-align: center; padding: 0 32px; max-width: 100%; }
.reel-slide .center .emoji { font-size: 56px; margin-bottom: 12px; }
.reel-slide .center .hint { color: rgba(255,255,255,0.5); font-size: 12px; margin-top: 8px; }
.reel-rail { position: absolute; right: 12px; bottom: 110px; z-index: 20; display: flex; flex-direction: column; align-items: center; gap: 20px; }
.reel-rail button { display: flex; flex-direction: column; align-items: center; gap: 4px; color: #fff; }
.reel-rail svg { width: 28px; height: 28px; }
.reel-rail span { font-size: 11px; }
.reel-info { position: absolute; left: 0; right: 0; bottom: 16px; z-index: 20; padding: 0 60px 0 16px; color: #fff; }
.reel-info .user-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.reel-info .avatar-ring { width: 32px; height: 32px; }
.reel-info .username { font-size: 14px; font-weight: 600; }
.reel-info .title { font-size: 14px; word-break: break-word; }
.reel-info .plays { font-size: 12px; color: rgba(255,255,255,0.6); margin-top: 4px; }

/* ---------- profile ---------- */
.profile { width: 100%; max-width: 935px; margin: 0 auto; padding: 0 16px 40px; box-sizing: border-box; }
@media (min-width: 768px) { .profile { padding: 0 0 40px; } }

.profile-top {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px 0;
  flex-wrap: wrap;         /* stacks gracefully instead of overflowing on very narrow screens */
  max-width: 100%;
}
@media (min-width: 420px) { .profile-top { gap: 24px; flex-wrap: nowrap; } }

.profile-info { flex: 1 1 200px; min-width: 0; max-width: 100%; }
.profile-row1 { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; row-gap: 8px; }
.profile-username { font-size: 16px; font-weight: 400; overflow-wrap: anywhere; }
.btn { padding: 6px 14px; border-radius: 8px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-secondary { background: #f5f5f5; color: var(--text); }
.profile-stats { display: flex; flex-wrap: wrap; gap: 12px 20px; font-size: 13px; }
@media (min-width: 640px) { .profile-stats { font-size: 14px; } }

.profile-bio { padding-bottom: 16px; font-size: 14px; max-width: 100%; }
.profile-bio p {
  margin: 4px 0;
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 6px;
  color: #404040;
  overflow-wrap: anywhere;
}
.profile-bio p svg { margin-top: 2px; }
.profile-bio p.name { font-weight: 600; color: var(--text); }
.profile-bio p.link { color: #1e3a8a; }
.profile-bio svg { width: 14px; height: 14px; flex-shrink: 0; }

.profile-highlights { padding-bottom: 16px; max-width: 100%; }

.profile-tabs {
  border-top: 1px solid var(--border); display: flex; justify-content: center; gap: 40px;
  font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: 0.03em;
}
.profile-tab {
  display: flex; align-items: center; gap: 6px; padding: 12px 0; margin-top: -1px;
  border-top: 2px solid transparent;
}
.profile-tab.active { border-top-color: var(--text); color: var(--text); }
.profile-tab svg { width: 16px; height: 16px; }
.profile-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; margin-top: 1px; max-width: 100%; }
.profile-grid .cell { aspect-ratio: 1/1; display: flex; align-items: center; justify-content: center; font-size: 28px; }
@media (min-width: 640px) { .profile-grid .cell { font-size: 32px; } }

/* ---------- search overlay ---------- */
.search-overlay {
  position: fixed; inset: 0; z-index: 50; background: #fff; display: none;
}
.search-overlay.open { display: flex; }
@media (min-width: 768px) { .search-overlay.open { background: rgba(0,0,0,0.4); } }
.search-panel { width: 100%; height: 100%; background: #fff; display: flex; flex-direction: column; max-width: 100%; }
@media (min-width: 768px) { .search-panel { width: 400px; } }
.search-header { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.search-header .back-btn { display: block; }
@media (min-width: 768px) { .search-header .back-btn { display: none; } }
.search-input-wrap { flex: 1; min-width: 0; display: flex; align-items: center; background: #f5f5f5; border-radius: 8px; padding: 8px 12px; }
.search-input-wrap svg { width: 16px; height: 16px; color: var(--muted); margin-right: 8px; flex-shrink: 0; }
.search-input-wrap input { border: none; background: none; outline: none; font-size: 14px; width: 100%; min-width: 0; }
.search-cancel { display: none; font-size: 14px; color: var(--blue); font-weight: 500; flex-shrink: 0; }
@media (min-width: 768px) { .search-cancel { display: block; } }
.search-results { flex: 1; overflow-y: auto; }
.search-hint { text-align: center; color: #a3a3a3; font-size: 14px; padding: 40px 24px 0; }
.search-group { padding: 16px 16px 0; }
.search-group-title { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 8px; }
.search-result { display: flex; align-items: center; gap: 12px; padding: 8px 0; width: 100%; text-align: left; }
.search-result .thumb { width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.search-result .thumb.round { border-radius: 999px; background: #f5f5f5; }
.search-result .rtext { min-width: 0; }
.search-result .rtitle { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-result .rsub { font-size: 12px; color: var(--muted); }
