/*
  Design tokens follow Threads' own web CSS (the --barcelona-* variables), except where a
  WCAG contrast rule needs a darker grey. Sources and rules: DESIGN.md.
*/
:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --page: #fafafa;
  --text: #000000;
  --muted: #6b6b6b;      /* Threads uses #999999 (2.85:1); darkened to pass WCAG 4.5:1 */
  --charcoal: #424242;   /* Threads action icons */
  --line: rgba(0, 0, 0, 0.15);
  --outline: #d5d5d5;
  --soft: #f5f5f5;
  --skeleton: #efefef;
  --seg-on: #ffffff;
  --bar-bg: rgba(255, 255, 255, 0.85);
  --link: #0095f6;
  --verified: #0095f6;
  --avatar-l: 46%;
  --gutter: 16px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101010;
    --page: #0a0a0a;
    --text: #f3f5f7;
    --muted: #8a8a8a;     /* Threads uses #777777 (4.25:1); lightened to pass 4.5:1 */
    --charcoal: #cccccc;
    --line: rgba(243, 245, 247, 0.15);
    --outline: #2d2d2d;
    --soft: #1e1e1e;
    --skeleton: #1e1e1e;
    --seg-on: #2d2d2d;
    --bar-bg: rgba(16, 16, 16, 0.85);
    --link: #18a3fe;
    --avatar-l: 40%;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
a { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.app {
  max-width: 640px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 641px) {
  .app { border-left: 0.5px solid var(--outline); border-right: 0.5px solid var(--outline); }
}

/* ---------- Top bar: brand row + service tabs. Slides away on scroll down, back on scroll up. ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--line);
  transition: transform 0.25s ease;
}
.topbar.is-hidden { transform: translateY(-100%); }

.topbar-main {
  display: grid; grid-template-columns: 44px 1fr 44px; align-items: center;
  min-height: 56px; padding: 4px 4px 0;
}
.title-block { grid-column: 2; min-width: 0; text-align: center; }
.brand { margin: 0; font-size: 17px; line-height: 22px; font-weight: 700; letter-spacing: -0.2px; }
.subtitle { margin: 0; font-size: 12px; line-height: 16px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%; color: var(--charcoal);
}
.icon-btn .icon { width: 22px; height: 22px; }
.back-btn { grid-column: 1; grid-row: 1; }
.info-btn { grid-column: 3; grid-row: 1; }
.icon-btn:active { background: var(--soft); }

/* Service tabs, styled like Threads' feed tabs: text tabs with an underline on the active one. */
.chips-wrap { position: relative; }
.chips {
  display: flex; gap: 4px; overflow-x: auto; overscroll-behavior-x: contain;
  padding: 0 calc(var(--gutter) - 8px);
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
/* Fade at the edge so a cut-off tab shows there is more to scroll. */
.chips-wrap::after {
  content: ""; position: absolute; top: 0; right: 0; bottom: 0; width: 32px; pointer-events: none;
  background: linear-gradient(to right, transparent, var(--bg));
  opacity: 0; transition: opacity 0.15s;
}
.chips-wrap.has-more::after { opacity: 1; }

.chip {
  flex: none; position: relative;
  min-height: 44px; padding: 0 8px;
  display: inline-flex; align-items: center;
  color: var(--muted);
}
.chip > span { display: inline-flex; align-items: baseline; gap: 5px; font-size: 15px; font-weight: 600; white-space: nowrap; }
.chip small { font-size: 13px; font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.chip::after {
  content: ""; position: absolute; left: 8px; right: 8px; bottom: 0; height: 2px; border-radius: 2px 2px 0 0;
  background: transparent;
}
.chip[aria-pressed="true"] { color: var(--text); }
.chip[aria-pressed="true"]::after { background: var(--text); }

/* ---------- Search and sort ---------- */
.controls { padding: 12px var(--gutter) 0; border-bottom: 0.5px solid var(--line); }

.search {
  display: flex; align-items: center; gap: 8px;
  padding: 0 14px; min-height: 44px;
  background: var(--soft); border: 0.5px solid var(--line); border-radius: 16px;
  color: var(--muted);
}
.search input {
  flex: 1; min-width: 0; height: 44px; border: 0; outline: 0; background: transparent;
  color: var(--text); font-size: 16px; /* 16px stops iOS from zooming on focus */
}
.search input::placeholder { color: var(--muted); }
.search:focus-within { border-color: var(--muted); }

.sortbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 52px; }
.result-count { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Two-option segmented control (Apple HIG: equal-width segments for one closely related choice). */
.sort {
  display: grid; grid-template-columns: 1fr 1fr; padding: 2px;
  background: var(--soft); border-radius: 10px;
}
.sort-btn {
  min-height: 32px; padding: 0 12px; border-radius: 8px;
  font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap;
  position: relative;
}
/* Keeps the tap area at least 40px tall while the visible segment stays slim. */
.sort-btn::before { content: ""; position: absolute; inset: -4px 0; }
.sort-btn[aria-pressed="true"] {
  background: var(--seg-on); color: var(--text);
  box-shadow: 0 0 0 0.5px var(--line), 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* ---------- Feed post (Threads post layout: 36px avatar in a 48px column, 15/21 text) ---------- */
.post {
  position: relative;
  display: grid; grid-template-columns: 36px 1fr; column-gap: 12px;
  padding: 12px var(--gutter) 4px;
  border-bottom: 0.5px solid var(--line);
}
.post:active { background: var(--soft); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: hsl(var(--h, 0) 45% var(--avatar-l));
  color: #fff; font-weight: 700; font-size: 15px; text-transform: uppercase;
  margin-top: 2px;
}
.post-body { min-width: 0; }
.post-head { display: flex; align-items: baseline; gap: 6px; min-width: 0; line-height: 21px; }
.username { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.verified { flex: none; width: 14px; height: 14px; align-self: center; margin-left: -2px; }
.verified[hidden] { display: none; }
.verified circle { fill: var(--verified); }
.verified path { fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.time { flex: none; color: var(--muted); font-variant-numeric: tabular-nums; }

.post-text {
  margin: 3px 0 0; line-height: 21px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.post-text.is-summary { color: var(--muted); font-style: italic; }

.actions { display: flex; align-items: center; gap: 8px; margin: 6px 0 0 -8px; min-height: 40px; }
.action {
  display: inline-flex; align-items: center; gap: 4px; min-height: 36px; min-width: 44px; padding: 0 8px;
  font-size: 13px; color: var(--charcoal); font-variant-numeric: tabular-nums;
}
.action .num:empty { display: none; }
.open-link {
  margin-left: auto; margin-right: -4px; color: var(--charcoal); font-size: 13px; font-weight: 500; text-decoration: none;
  min-height: 40px; padding: 0 4px; display: inline-flex; align-items: center; gap: 2px;
}
.open-link .icon { width: 16px; height: 16px; stroke-width: 2; }
/* The link covers the whole post, so tapping anywhere opens the original post. */
.open-link::after { content: ""; position: absolute; inset: 0; }
.open-link:focus-visible { outline: none; }
.open-link:focus-visible::after { outline: 2px solid var(--link); outline-offset: -2px; }

/* ---------- Loading skeleton (NN/g: skeleton screens for full-page loads) ---------- */
.skeleton {
  display: grid; grid-template-columns: 36px 1fr; column-gap: 12px;
  padding: 14px var(--gutter) 18px; border-bottom: 0.5px solid var(--line);
}
.sk-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--skeleton); }
.sk-lines { display: grid; gap: 8px; padding-top: 4px; }
.sk-lines span { height: 12px; border-radius: 6px; background: var(--skeleton); }
.sk-lines span:nth-child(1) { width: 35%; }
.sk-lines span:nth-child(2) { width: 92%; }
.sk-lines span:nth-child(3) { width: 64%; }
@media (prefers-reduced-motion: no-preference) {
  .sk-avatar, .sk-lines span { animation: pulse 1.2s ease-in-out infinite; }
}
@keyframes pulse { 50% { opacity: 0.55; } }

/* ---------- Empty, error and end-of-list states ---------- */
.state { padding: 56px 32px; text-align: center; }
.state-title { margin: 0 0 4px; font-size: 16px; font-weight: 600; }
.state-text { margin: 0; color: var(--muted); overflow-wrap: anywhere; }
.state-btn {
  margin-top: 16px; min-height: 40px; padding: 0 18px; border-radius: 10px;
  border: 0.5px solid var(--outline); font-size: 14px; font-weight: 600;
}
.state-btn:active { background: var(--soft); }
.end { margin: 0; padding: 24px var(--gutter) 40px; text-align: center; font-size: 13px; color: var(--muted); }
.sentinel { height: 1px; }

/* ---------- "New posts" pill (shown when fresh data arrives while scrolled down) ---------- */
.new-pill {
  position: fixed; z-index: 11; left: 50%; transform: translateX(-50%);
  top: calc(12px + env(safe-area-inset-top));
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; padding: 0 16px; border-radius: 999px;
  background: var(--text); color: var(--bg); font-size: 14px; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
}
.new-pill .icon { width: 16px; height: 16px; stroke-width: 2.2; }

/* ---------- About ---------- */
.about { padding: 8px 20px 40px; }
.about h2 { font-size: 22px; line-height: 28px; margin: 16px 0 8px; }
.about h3 { font-size: 15px; margin: 24px 0 6px; }
.about p { margin: 0 0 10px; line-height: 21px; }
.muted { color: var(--muted); font-size: 13px; }

[hidden] { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  .topbar { transition: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .chip, .sort-btn { transition: color 0.15s, background-color 0.15s; }
}
