:root {
  --ig-bg: #000;
  --ig-elev: #121212;
  --ig-fg: #f5f5f5;
  --ig-muted: #a8a8a8;
  --ig-line: #262626;
  --ig-blue: #0095f6;
  --ig-red: #ed4956;
  --wa-header: #1f2c34;
  --wa-bg: #0b141a;
  --wa-in: #1f2c34;
  --wa-out: #005c4b;
  --wa-green: #00a884;
  --wa-tick: #53bdeb;
  --wa-tick-wait: #8696a0;
  --wa-unread: #25d366;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --bar: 52px;
  --nav: 56px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; background: var(--ig-bg); color: var(--ig-fg); min-height: 100%; }
html { height: 100%; }
body { min-height: 100dvh; }
img { max-width: 100%; display: block; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: var(--ig-blue); text-decoration: none; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--ig-blue); outline-offset: 2px; }

#app { min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(8px + var(--safe-t)) 12px 8px;
  background: var(--ig-bg);
  border-bottom: 1px solid var(--ig-line);
  display: flex; align-items: center; gap: 10px;
}
.topbar h1 { font-size: 18px; font-weight: 650; margin: 0; letter-spacing: -0.02em; flex: 1; }
.topbar .ahora { font-size: 11px; color: var(--ig-muted); text-align: right; line-height: 1.3; max-width: 52%; }
.switch {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 4px; border-radius: 999px;
  border: 1px solid var(--ig-line); background: var(--ig-elev);
  max-width: 58%;
}
.switch img { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
.switch .who { display: flex; flex-direction: column; align-items: flex-start; min-width: 0; }
.switch .who b { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 42vw; }
.switch .who span { font-size: 11px; color: var(--ig-muted); }

.nav {
  position: sticky; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: 1fr 1fr;
  height: calc(var(--nav) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--ig-bg);
  border-top: 1px solid var(--ig-line);
}
.nav button {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: 11px; color: var(--ig-muted); min-height: var(--nav);
}
.nav button.active { color: var(--ig-fg); font-weight: 600; }
.nav svg { width: 24px; height: 24px; }
.badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 99px;
  background: var(--wa-unread); color: #042; font-size: 11px; font-weight: 700;
  margin-left: 4px;
}
.nav .badge { position: absolute; margin: 0; transform: translate(12px, -16px); }

.main { flex: 1; width: 100%; max-width: 560px; margin: 0 auto; padding-bottom: 8px; }

/* Perfil */
.perfil { padding: 16px 16px 8px; }
.perfil-head { display: grid; grid-template-columns: 92px 1fr; gap: 18px; align-items: center; }
.perfil-head .ava {
  width: 92px; height: 92px; border-radius: 50%; object-fit: cover;
  outline: 1.5px solid var(--ig-line); outline-offset: 3px;
}
.counts { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; }
.counts b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; }
.counts span { font-size: 12px; color: var(--ig-muted); }
.nombre { margin: 14px 0 0; font-size: 14px; font-weight: 650; display: flex; align-items: center; gap: 6px; }
.nombre .lock { font-size: 12px; color: var(--ig-muted); }
.bio { margin: 4px 0 0; font-size: 14px; line-height: 1.4; white-space: pre-wrap; }
.handle { color: var(--ig-muted); font-size: 13px; margin: 0; }

.grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px;
  margin-top: 16px; border-top: 1px solid var(--ig-line); padding-top: 2px;
}
.grid button {
  aspect-ratio: 1; overflow: hidden; background: #1a1a1a; position: relative;
}
.grid button img { width: 100%; height: 100%; object-fit: cover; }
.grid button:active { opacity: .85; }
.vacio { padding: 32px 16px; text-align: center; color: var(--ig-muted); font-size: 14px; }

/* Lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 40; background: #000;
  display: flex; flex-direction: column;
  padding-top: var(--safe-t); padding-bottom: var(--safe-b);
}
.lightbox header {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-bottom: 1px solid var(--ig-line);
}
.lightbox header img { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; }
.lightbox header .meta { flex: 1; min-width: 0; }
.lightbox header .meta b { display: block; font-size: 14px; }
.lightbox header .meta span { font-size: 12px; color: var(--ig-muted); }
.lightbox .shot {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: #000; min-height: 220px;
}
.lightbox .shot img { max-height: min(70dvh, 720px); width: auto; max-width: 100%; height: auto; object-fit: contain; }
.lightbox .body { padding: 12px 16px 20px; overflow: auto; max-height: 36dvh; }
.likes { font-weight: 650; font-size: 14px; margin: 0 0 8px; }
.pie { font-size: 14px; line-height: 1.45; margin: 0 0 12px; }
.pie b { margin-right: 6px; }
.comentario { font-size: 14px; line-height: 1.4; margin: 0 0 8px; display: flex; gap: 8px; align-items: flex-start; }
.comentario img { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; flex: none; }
.comentario p { margin: 0; }
.comentario .u { font-weight: 650; margin-right: 6px; }
.cerrar { width: 40px; height: 40px; font-size: 24px; line-height: 1; }

/* Chats */
.chat-list { display: flex; flex-direction: column; }
.chat-row {
  display: grid; grid-template-columns: 56px 1fr auto; gap: 10px;
  align-items: center; padding: 10px 14px; text-align: left; width: 100%;
  border-bottom: 1px solid var(--ig-line);
}
.chat-row img { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; }
.chat-row .t { font-size: 15px; font-weight: 600; }
.chat-row .prev { font-size: 13px; color: var(--ig-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 2px 0 0; }
.chat-row.unread .prev { color: var(--ig-fg); font-weight: 600; }
.chat-row .hora { font-size: 11px; color: var(--ig-muted); text-align: right; }
.chat-row.unread .hora { color: var(--wa-unread); }
.chat-row .right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }

.thread {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column;
  background: var(--wa-bg);
  padding-top: var(--safe-t);
}
.thread header {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; background: var(--wa-header); color: #e9edef;
}
.thread header img { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; }
.thread header .meta { flex: 1; min-width: 0; }
.thread header .meta b { display: block; font-size: 16px; }
.thread header .meta span { font-size: 12px; color: #8696a0; }
.msgs {
  flex: 1; overflow: auto; padding: 12px 10px calc(16px + var(--safe-b));
  display: flex; flex-direction: column; gap: 4px;
  background: var(--wa-bg);
}
.dia {
  align-self: center; background: #182229; color: #8696a0;
  font-size: 12px; padding: 4px 10px; border-radius: 8px; margin: 8px 0;
}
.unread-bar {
  align-self: stretch; text-align: center;
  background: #1d3b2a; color: #d1f4d9; font-size: 12px; font-weight: 600;
  padding: 5px 8px; margin: 8px 0; border-radius: 6px;
}
.burbuja {
  max-width: 82%; padding: 6px 8px 4px 9px; border-radius: 8px;
  font-size: 14.5px; line-height: 1.35; position: relative;
  word-wrap: break-word; white-space: pre-wrap;
}
.burbuja.in { background: var(--wa-in); color: #e9edef; align-self: flex-start; border-top-left-radius: 0; }
.burbuja.out { background: var(--wa-out); color: #e9edef; align-self: flex-end; border-top-right-radius: 0; }
.burbuja .hora {
  display: inline-flex; align-items: center; gap: 3px;
  float: right; margin: 4px 0 0 10px;
  font-size: 11px; color: #8696a0;
}
.ticks { letter-spacing: -1px; font-size: 14px; color: var(--wa-tick-wait); }
.ticks.read { color: var(--wa-tick); }

/* Selector de personaje */
.picker {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.72);
  display: flex; align-items: flex-end; justify-content: center;
}
.picker .sheet {
  background: #1a1a1a; width: 100%; max-width: 560px;
  max-height: min(86dvh, 720px);
  border-radius: 16px 16px 0 0;
  padding: 8px 0 calc(12px + var(--safe-b));
  overflow: auto;
}
.picker .handle { width: 40px; height: 4px; background: #444; border-radius: 4px; margin: 8px auto 12px; }
.picker h2 { font-size: 16px; margin: 0 16px 8px; }
.picker button.p {
  display: grid; grid-template-columns: 48px 1fr auto; gap: 12px; align-items: center;
  width: 100%; padding: 8px 16px; text-align: left;
}
.picker button.p img { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; }
.picker button.p b { display: block; font-size: 15px; }
.picker button.p span { font-size: 13px; color: var(--ig-muted); }
.picker button.p.active { background: #262626; }
.check { color: var(--ig-blue); font-size: 18px; }

.disclaimer {
  font-size: 11px; color: var(--ig-muted); text-align: center;
  padding: 16px 16px 8px; line-height: 1.4;
}

@media (min-width: 700px) {
  #app { max-width: 560px; margin: 0 auto; border-left: 1px solid var(--ig-line); border-right: 1px solid var(--ig-line); }
  .lightbox, .thread, .picker { left: 50%; right: auto; width: 560px; transform: translateX(-50%); }
  .picker { transform: none; left: 50%; margin-left: -280px; }
}
