:root {
  --bg: #fff;
  --text: #000;
  --muted: #737373;
  --line: #dbdbdb;
  --elevated: #fff;
  --hover: #efefef;
  --link: #00376b;
  --blue: #0095f6;
  --red: #ff3040;
  --story-ring: linear-gradient(45deg, #feda75 0%, #fa7e1e 25%, #d62976 50%, #962fbf 75%, #4f5bd5 100%);
  --nav-h: 50px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --max-w: 470px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000;
    --text: #f5f5f5;
    --muted: #a8a8a8;
    --line: #262626;
    --elevated: #262626;
    --hover: #1a1a1a;
    --link: #e0f1ff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 18px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

body.locked { overflow: hidden; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

svg { display: block; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  position: relative;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:active svg { opacity: 0.5; }

main {
  max-width: var(--max-w);
  margin: 0 auto;
  padding-top: var(--safe-top);
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom));
}

/* ───────── Avatars ───────── */
.avatar {
  display: block;
  border-radius: 50%;
  object-fit: cover;
  background: var(--hover);
  flex-shrink: 0;
}
.avatar-fallback {
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 600;
  text-transform: uppercase;
  user-select: none;
}
.ring {
  display: block;
  line-height: 0;
  border-radius: 50%;
  padding: 2px;
  background: var(--story-ring);
  flex-shrink: 0;
}
.ring.seen { background: var(--line); }
.ring > .ring-gap {
  border-radius: 50%;
  padding: 2px;
  background: var(--bg);
}
.ring.none { background: transparent; }

/* ───────── Stories row ───────── */
.stories {
  display: flex;
  gap: 14px;
  overflow-x: auto;
  padding: 12px 12px 10px;
  scrollbar-width: none;
  border-bottom: 1px solid var(--line);
}
.stories::-webkit-scrollbar { display: none; }
.story {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 70px;
  flex-shrink: 0;
}
.story .label {
  font-size: 12px;
  line-height: 16px;
  max-width: 74px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.story.me .label { color: var(--muted); }
.story .avatar-wrap { position: relative; display: block; }
.plus-badge {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--blue);
  border: 2px solid var(--bg);
  display: grid;
  place-items: center;
}
.plus-badge svg { width: 12px; height: 12px; color: #fff; }

/* ───────── Post ───────── */
.post { padding-bottom: 12px; }

.post-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px 10px 12px;
}
.post-head .who { flex: 1; min-width: 0; }
.post-head .line1 {
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}
.username { font-weight: 600; }
.post-head .time { color: var(--muted); }
.post-head .line2 {
  font-size: 12px;
  line-height: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.verified { width: 12px; height: 12px; flex-shrink: 0; }
.post-head .icon-btn svg { width: 20px; height: 20px; }

.media {
  position: relative;
  background: #000;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: pan-x pan-y;
}
.media-track {
  display: flex;
  height: 100%;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
}
.media-track::-webkit-scrollbar { display: none; }
.slide {
  flex: 0 0 100%;
  height: 100%;
  scroll-snap-align: center;
  scroll-snap-stop: always;
  position: relative;
}
.slide img,
.slide video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  -webkit-user-drag: none;
  pointer-events: none;
}
.slide-counter {
  position: absolute;
  top: 14px;
  right: 14px;
  background: rgba(18, 18, 18, 0.7);
  color: #fff;
  font-size: 12px;
  line-height: 16px;
  padding: 3px 8px;
  border-radius: 12px;
  transition: opacity 0.4s;
  pointer-events: none;
}
.slide-counter.faded { opacity: 0; }

.mute-btn {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(38, 38, 38, 0.8);
  color: #fff;
  display: grid;
  place-items: center;
  z-index: 2;
}
.mute-btn svg { width: 14px; height: 14px; }

.big-heart {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 96px;
  height: 96px;
  color: #fff;
  pointer-events: none;
  opacity: 0;
  filter: drop-shadow(0 0 12px rgba(0, 0, 0, 0.25));
}
.big-heart.show { animation: big-heart 1s ease-out forwards; }
@keyframes big-heart {
  0% { opacity: 0; transform: scale(0); }
  15% { opacity: 0.95; transform: scale(1.2); }
  30% { transform: scale(0.95); }
  45%, 80% { opacity: 0.95; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.2) translateY(-60px); }
}

.actions {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 4px 4px 0;
  height: 46px;
}
.actions .left { display: flex; }
.actions .right { display: flex; justify-content: flex-end; }
.actions .icon-btn { width: 40px; height: 40px; }
.like-btn.liked { color: var(--red); }
.like-btn.pop svg { animation: pop 0.45s ease; }
@keyframes pop {
  0% { transform: scale(1); }
  25% { transform: scale(1.25); }
  50% { transform: scale(0.9); }
  100% { transform: scale(1); }
}

.dots { display: flex; gap: 4px; }
.dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--line);
  transition: background 0.2s;
}
.dots span.on { background: var(--blue); }

.post-body { padding: 0 16px; }
.likes { font-weight: 600; margin-bottom: 6px; }
.likes .liked-by-avatar { display: inline-block; vertical-align: -4px; margin-right: 6px; }
.caption { white-space: pre-line; word-wrap: break-word; }
.caption .more,
.view-comments,
.time-foot { color: var(--muted); }
.caption .more { margin-left: 2px; }
.view-comments { display: block; margin-top: 6px; }
.time-foot { font-size: 12px; margin-top: 6px; }

.tag { color: var(--link); }

.feed-end { height: 24px; }

/* ───────── Bottom bar ───────── */
.bottombar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg);
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.bottombar .icon-btn { width: 48px; height: 48px; }
.bottombar .avatar { width: 26px; height: 26px; }
.bottombar .me-avatar { border-radius: 50%; padding: 1px; }
@media (min-width: 520px) {
  .bottombar { max-width: var(--max-w); margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}

/* ───────── Comments sheet ───────── */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.25s;
}
.sheet-backdrop.open { opacity: 1; }
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: var(--max-w);
  height: 80dvh;
  background: var(--elevated);
  border-radius: 14px 14px 0 0;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
  padding-bottom: var(--safe-bottom);
}
.sheet-backdrop.open .sheet { transform: translateY(0); }
.sheet.dragging { transition: none; }
.sheet-grabber {
  padding: 8px 0 0;
  text-align: center;
  border-bottom: 1px solid var(--line);
  touch-action: none;
  cursor: grab;
}
.grabber-bar {
  display: block;
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--muted);
  opacity: 0.6;
  margin: 0 auto 12px;
}
.sheet-grabber h2 { font-size: 16px; font-weight: 700; margin: 0 0 12px; }
.comment-list {
  list-style: none;
  margin: 0;
  padding: 8px 0;
  overflow-y: auto;
  flex: 1;
  overscroll-behavior: contain;
}
.comment {
  display: flex;
  gap: 12px;
  padding: 10px 12px 10px 16px;
}
.comment .c-body { flex: 1; min-width: 0; }
.comment .c-meta { font-size: 12px; line-height: 16px; }
.comment .c-meta .c-time { color: var(--muted); margin-left: 6px; font-weight: 400; }
.comment .c-text { white-space: pre-line; word-wrap: break-word; margin-top: 2px; }
.comment .c-foot { color: var(--muted); font-size: 12px; font-weight: 600; margin-top: 6px; display: flex; gap: 16px; }
.comment .c-like {
  width: 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--muted);
  font-size: 11px;
  padding-top: 8px;
}
.comment .c-like svg { width: 14px; height: 14px; }
.comment .c-like.liked { color: var(--red); }
.replies { list-style: none; margin: 0; padding: 0 0 0 44px; }
.replies .comment { padding-left: 0; }
.replies-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  padding: 2px 0 8px 60px;
}
.replies-toggle::before { content: ""; width: 24px; border-top: 1px solid var(--muted); opacity: 0.6; }
.comment.new { animation: fade-in 0.4s ease; }
@keyframes fade-in { from { opacity: 0; transform: translateY(6px); } }
.no-comments { text-align: center; padding: 48px 24px; color: var(--muted); }
.no-comments strong { display: block; color: var(--text); font-size: 20px; line-height: 26px; margin-bottom: 6px; }

.emoji-row {
  display: flex;
  justify-content: space-around;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  font-size: 24px;
  line-height: 32px;
}
.emoji-row button { padding: 0 4px; }
.comment-form {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px 12px;
}
.comment-form input {
  flex: 1;
  min-width: 0;
  height: 40px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  padding: 0 16px;
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
  outline: none;
}
.comment-form button { color: var(--blue); font-weight: 600; }
.comment-form button:disabled { opacity: 0.4; cursor: default; }

/* ───────── Story viewer ───────── */
.story-viewer {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: story-in 0.25s ease;
}
.story-viewer[hidden] { display: none; }
@keyframes story-in { from { opacity: 0; transform: scale(0.92); } }
.story-frame {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: calc(100dvh * 9 / 16);
  overflow: hidden;
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
}
@media (min-width: 520px) {
  .story-frame { height: 96dvh; border-radius: 10px; }
}
.story-progress {
  position: absolute;
  top: calc(8px + var(--safe-top));
  left: 8px;
  right: 8px;
  z-index: 3;
  display: flex;
  gap: 3px;
}
.story-progress .bar {
  flex: 1;
  height: 2px;
  border-radius: 1px;
  background: rgba(255, 255, 255, 0.35);
  overflow: hidden;
}
.story-progress .fill { height: 100%; width: 0; background: #fff; }
.story-head {
  position: absolute;
  top: calc(18px + var(--safe-top));
  left: 12px;
  right: 4px;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 8px;
  text-shadow: 0 0 4px rgba(0, 0, 0, 0.4);
}
.story-user { font-weight: 600; }
.story-time { opacity: 0.75; flex: 1; }
.story-close { color: #fff; }
.story-media {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #111;
}
.story-media::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 120px;
  background: linear-gradient(rgba(0, 0, 0, 0.35), transparent);
  z-index: 1;
}
.story-media img,
.story-media video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}
.story-tap { position: absolute; top: 70px; bottom: 0; z-index: 2; }
.story-tap-prev { left: 0; width: 30%; }
.story-tap-next { right: 0; width: 70%; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* Carousel arrows — only on devices with a mouse */
.car-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  color: #262626;
  display: grid;
  place-items: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  z-index: 2;
}
.car-nav svg { width: 14px; height: 14px; }
.car-nav.prev { left: 10px; }
.car-nav.next { right: 10px; }
.car-nav[hidden] { display: none; }
@media (hover: none) { .car-nav { display: none; } }
.muted { color: var(--muted); }
