:root {
  --bg: #fff; --fg: #000; --muted: #737373; --line: #dbdbdb; --chip: #efefef; --chip-press: #dbdbdb;
  --link: #00376b; --accent: #0095f6; --danger: #ed4956; --sheet: #fff; --page: #f2f2f2;
  --ring: linear-gradient(45deg, #feda75, #fa7e1e 30%, #d62976 60%, #962fbf 85%, #4f5bd5);
  --tile-ratio: 3 / 4;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000; --fg: #f5f5f5; --muted: #a8a8a8; --line: #262626; --chip: #363636; --chip-press: #262626;
    --link: #e0f1ff; --sheet: #262626; --page: #121212;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--page); color: var(--fg); }
body { font: 14px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; overscroll-behavior-y: none; }
body.noscroll { overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.ico { display: inline-flex; }
.ico svg, .icon-btn > svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; display: block; }

#app { max-width: 480px; margin: 0 auto; background: var(--bg); min-height: 100vh; min-height: 100dvh; padding-bottom: calc(env(safe-area-inset-bottom) + 40px); }
@media (min-width: 520px) { #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }

/* Barra superior */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--bg); display: flex; align-items: center; justify-content: space-between; padding: calc(env(safe-area-inset-top) + 6px) 8px 6px 16px; }
.username-btn { display: flex; align-items: center; gap: 2px; font-size: 21px; font-weight: 700; letter-spacing: -.3px; min-width: 0; }
.username-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.username-btn .ico svg { width: 18px; height: 18px; stroke-width: 2.4; }
.topbar-actions { display: flex; }
.icon-btn { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 50%; flex: none; }
.icon-btn:active { opacity: .5; }
.icon-btn.off { opacity: .45; }

/* Cabecera del perfil */
.profile { padding: 4px 16px 0; }
.profile-top { display: flex; align-items: center; gap: 22px; }
.avatar-wrap { flex: none; width: 88px; height: 88px; position: relative; border-radius: 50%; }
.avatar-ring { width: 100%; height: 100%; border-radius: 50%; padding: 3px; }
.avatar-wrap.has-story .avatar-ring { background: var(--ring); }
.avatar-inner { width: 100%; height: 100%; border-radius: 50%; overflow: hidden; background: var(--chip); border: 3px solid var(--bg); display: grid; place-items: center; color: var(--muted); }
.avatar-inner img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-inner .ico svg { width: 46px; height: 46px; stroke-width: 1.3; }
.avatar-plus { position: absolute; right: 2px; bottom: 2px; width: 24px; height: 24px; border-radius: 50%; background: var(--fg); color: var(--bg); border: 3px solid var(--bg); display: grid; place-items: center; font-weight: 700; font-size: 17px; line-height: 1; }
.avatar-wrap.has-story .avatar-plus { display: none; }
.profile-side { flex: 1; min-width: 0; }
.display-name { font-weight: 600; font-size: 15px; margin-bottom: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stats { display: flex; justify-content: space-between; max-width: 250px; gap: 8px; }
.stats div { display: flex; flex-direction: column; min-width: 0; }
.stats b { font-size: 16px; font-weight: 600; }
.stats span { font-size: 13px; }
.bio { margin-top: 10px; overflow-wrap: anywhere; }
.bio-text { white-space: pre-wrap; }
.category { color: var(--muted); }
.bio-link { color: var(--link); font-weight: 600; }
.bio-link:empty, .category:empty, .bio-text:empty { display: none; }
.profile-buttons { display: flex; gap: 6px; margin-top: 12px; }
.btn-grey { flex: 1; background: var(--chip); border-radius: 8px; height: 34px; font-weight: 600; }
.btn-grey:active { background: var(--chip-press); }

/* Destacadas */
.highlights { display: flex; gap: 12px; overflow-x: auto; padding: 14px 0 10px; scrollbar-width: none; }
.highlights::-webkit-scrollbar { display: none; }
.hl { flex: none; width: 68px; display: flex; flex-direction: column; align-items: center; gap: 5px; }
.hl-circle { width: 64px; height: 64px; border-radius: 50%; border: 1px solid var(--line); padding: 3px; flex: none; overflow: hidden; }
.hl-circle.new { display: grid; place-items: center; }
.hl-circle img, .hl-circle video { width: 56px; height: 56px; border-radius: 50%; object-fit: cover; display: block; background: var(--chip); }
.hl-circle.new .ico svg { width: 28px; height: 28px; stroke-width: 1.5; }
.hl-title { font-size: 12px; max-width: 68px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Pestañas y cuadrícula */
.tabs { display: flex; margin-top: 4px; }
.tab { flex: 1; height: 44px; display: grid; place-items: center; color: var(--muted); border-bottom: 1px solid var(--line); }
.tab.active { color: var(--fg); border-bottom: 1.5px solid var(--fg); }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; margin-top: 2px; }
.tile { position: relative; aspect-ratio: var(--tile-ratio); background: var(--chip); overflow: hidden; cursor: pointer; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.tile img, .tile video { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; -webkit-user-drag: none; }
.tile-icons { position: absolute; top: 6px; right: 6px; display: flex; gap: 4px; color: #fff; filter: drop-shadow(0 0 2px rgba(0,0,0,.6)); }
.tile-icons svg { width: 18px; height: 18px; }
.badge { position: absolute; left: 5px; bottom: 5px; background: rgba(0,0,0,.6); color: #fff; font-size: 10px; font-weight: 600; padding: 2px 7px; border-radius: 10px; }
.hide-badges .badge { display: none; }
.tile.placeholder { opacity: .2; }
.ghost { position: fixed; left: 0; top: 0; z-index: 100; pointer-events: none; box-shadow: 0 12px 32px rgba(0,0,0,.4); border-radius: 6px; will-change: transform; }
body.dragging, body.dragging * { cursor: grabbing !important; user-select: none; -webkit-user-select: none; }
.empty { padding: 36px 32px; text-align: center; color: var(--muted); }
.empty p { margin: 6px 0; }
.empty b { color: var(--fg); }
.hint { text-align: center; color: var(--muted); font-size: 12px; padding: 14px 20px; margin: 0; }

/* Capas (pantallas, menús, diálogos) */
#layer > * { position: fixed; inset: 0; z-index: 50; }
.page { background: var(--bg); overflow-y: auto; -webkit-overflow-scrolling: touch; animation: slide .18s ease-out; }
.page-inner { max-width: 480px; margin: 0 auto; padding-bottom: calc(env(safe-area-inset-bottom) + 32px); }
.page-head { position: sticky; top: 0; z-index: 3; background: var(--bg); display: flex; align-items: center; gap: 4px; padding: calc(env(safe-area-inset-top) + 4px) 6px 4px; border-bottom: 1px solid var(--line); min-height: 50px; max-width: 480px; margin: 0 auto; }
.page-head h2 { font-size: 16px; margin: 0; flex: 1; text-align: center; }
.page-head .text-btn { padding: 8px 10px; font-weight: 600; }
.page-head .text-btn.primary { color: var(--accent); }
.page-head .spacer { width: 42px; }
@keyframes slide { from { transform: translateX(24px); opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

.sheet-wrap { background: rgba(0,0,0,.45); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { background: var(--sheet); width: 100%; max-width: 480px; border-radius: 14px 14px 0 0; padding: 6px 0 calc(env(safe-area-inset-bottom) + 6px); max-height: 88vh; overflow: auto; animation: up .2s ease-out; }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--muted); opacity: .4; margin: 4px auto 6px; }
.sheet-title { text-align: center; font-weight: 700; padding: 6px 16px 12px; border-bottom: 1px solid var(--line); }
.sheet-item { display: flex; align-items: center; gap: 14px; width: 100%; padding: 13px 20px; text-align: left; font-size: 15px; }
.sheet-item > span:not(.ico) { flex: 1; }
.sheet-item small { display: block; color: var(--muted); font-size: 12px; margin-top: 1px; }
.sheet-item:active { background: var(--chip); }
.sheet-item.danger { color: var(--danger); }
.sheet-item.checked::after { content: '✓'; color: var(--accent); font-weight: 700; font-size: 17px; }
.sheet-item.cancel { justify-content: center; color: var(--muted); border-top: 1px solid var(--line); margin-top: 4px; }
.sheet-sep { height: 1px; background: var(--line); margin: 4px 0; }

.dialog-wrap { background: rgba(0,0,0,.5); display: grid; place-items: center; padding: 24px; animation: fade .15s; }
.dialog { background: var(--sheet); border-radius: 14px; width: 100%; max-width: 360px; padding: 20px; }
.dialog h3 { margin: 0 0 8px; font-size: 17px; text-align: center; }
.dialog p { color: var(--muted); text-align: center; margin: 0 0 12px; }
.dialog-btns { display: flex; gap: 8px; margin-top: 14px; }
.dialog-btns button { flex: 1; height: 42px; border-radius: 10px; background: var(--chip); font-weight: 600; }
.dialog-btns .primary { background: var(--accent); color: #fff; }
.dialog-btns .danger { background: var(--danger); color: #fff; }
.field-input, .dialog input, .dialog textarea { width: 100%; font-size: 16px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--fg); outline: none; resize: vertical; }
.field-input:focus, .dialog input:focus, .dialog textarea:focus { border-color: var(--muted); }

/* Ver publicación */
.post-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; font-weight: 600; }
.mini-av { width: 32px; height: 32px; border-radius: 50%; overflow: hidden; background: var(--chip); flex: none; }
.mini-av img { width: 100%; height: 100%; object-fit: cover; display: block; }
.carousel-wrap { position: relative; }
.carousel { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none; aspect-ratio: 4 / 5; background: #000; }
.carousel::-webkit-scrollbar { display: none; }
.slide { flex: none; width: 100%; height: 100%; scroll-snap-align: start; }
.slide img, .slide video { width: 100%; height: 100%; object-fit: cover; display: block; }
.counter { position: absolute; top: 12px; right: 12px; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 12px; }
.dots { display: flex; justify-content: center; gap: 4px; height: 10px; align-items: center; margin-top: 10px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--line); }
.dots i.on { background: var(--accent); }
.post-actions { display: flex; gap: 14px; padding: 0 14px; margin-top: -10px; }
.post-actions .grow { flex: 1; }
.caption { display: flex; gap: 6px; padding: 10px 14px 4px; align-items: flex-start; }
.caption b { padding-top: 1px; }
.caption textarea { flex: 1; border: 0; background: transparent; resize: none; outline: none; font-size: 14px; padding: 0; min-height: 40px; color: var(--fg); }
.post-status { margin: 4px 14px 0; font-size: 12px; color: var(--muted); }
.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 14px; }
.tool { background: var(--chip); border-radius: 10px; padding: 11px 12px; font-weight: 600; display: flex; gap: 8px; align-items: center; text-align: left; font-size: 13px; }
.tool .ico svg { width: 20px; height: 20px; }
.tool:active { background: var(--chip-press); }
.tool.danger { color: var(--danger); }
.tool.wide { grid-column: span 2; justify-content: center; }

/* Historias */
.story { background: #000; color: #fff; display: grid; place-items: center; animation: fade .15s; }
.st-frame { position: relative; width: min(100vw, calc(100dvh * 9 / 16)); height: min(100dvh, calc(100vw * 16 / 9)); overflow: hidden; background: #111; }
@media (min-width: 600px) { .st-frame { width: calc(94dvh * 9 / 16); height: 94dvh; border-radius: 10px; } }
.st-stage { position: absolute; inset: 0; }
.st-stage img, .st-stage video { width: 100%; height: 100%; object-fit: cover; display: block; }
.st-top { position: absolute; left: 0; right: 0; top: 0; padding: calc(env(safe-area-inset-top) + 8px) 8px 24px; background: linear-gradient(rgba(0,0,0,.45), transparent); z-index: 3; }
.st-bars { display: flex; gap: 3px; }
.st-bar { flex: 1; height: 2.5px; background: rgba(255,255,255,.35); border-radius: 2px; overflow: hidden; }
.st-bar i { display: block; height: 100%; background: #fff; width: 0; }
.st-head { display: flex; align-items: center; gap: 8px; margin-top: 8px; padding-left: 4px; }
.st-head .mini-av { width: 32px; height: 32px; }
.st-title { font-weight: 600; font-size: 14px; }
.st-time { opacity: .7; font-size: 13px; }
.st-head .grow { flex: 1; }
.st-head .icon-btn { color: #fff; }
.st-tap { position: absolute; inset: 0; z-index: 2; touch-action: none; }
.st-empty { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 40px; opacity: .8; }

/* Elegir historias para destacadas */
.picker-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; padding-bottom: 90px; }
.pick { position: relative; aspect-ratio: 9 / 16; background: var(--chip); overflow: hidden; }
.pick img, .pick video { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.pick-num { position: absolute; top: 6px; right: 6px; width: 22px; height: 22px; border-radius: 50%; border: 2px solid #fff; background: rgba(0,0,0,.25); color: #fff; font-size: 12px; font-weight: 700; display: grid; place-items: center; }
.pick.on .pick-num { background: var(--accent); }
.pick.on img, .pick.on video { opacity: .75; }
.pick.used::after { content: '★'; position: absolute; left: 6px; bottom: 4px; color: #fff; text-shadow: 0 0 3px rgba(0,0,0,.7); font-size: 14px; }
.picker-bar { position: sticky; bottom: 0; max-width: 480px; margin: 0 auto; display: flex; align-items: center; gap: 12px; padding: 10px 14px calc(env(safe-area-inset-bottom) + 10px); background: var(--bg); border-top: 1px solid var(--line); }
.picker-count { flex: 1; color: var(--muted); font-size: 13px; }
.picker-bar .btn-primary { width: auto; padding: 0 18px; margin: 0; height: 40px; }
.btn-primary:disabled { opacity: .4; }

/* Formularios */
.form { padding: 8px 16px; }
.avatar-edit { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 16px 0 8px; }
.avatar-edit .avatar-inner { width: 86px; height: 86px; border: 0; }
.avatar-edit button { color: var(--accent); font-weight: 600; }
.field { display: block; margin: 12px 0; }
.field span { display: block; font-size: 12px; color: var(--muted); margin: 0 0 4px 2px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.note { font-size: 12px; color: var(--muted); padding: 0 2px; }

/* Bienvenida y ayuda */
.welcome { padding: calc(env(safe-area-inset-top) + 40px) 24px 32px; text-align: center; max-width: 440px; margin: 0 auto; }
.welcome img { width: 84px; height: 84px; border-radius: 20px; }
.welcome h1 { font-size: 26px; margin: 14px 0 6px; }
.welcome p { color: var(--muted); margin: 0 0 8px; }
.welcome .lock { background: var(--chip); border-radius: 12px; padding: 12px 14px; margin: 18px 0; text-align: left; color: var(--fg); font-size: 13px; }
.btn-primary, .btn-secondary { display: block; width: 100%; height: 46px; border-radius: 10px; font-weight: 600; font-size: 15px; margin-top: 10px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--chip); }
.btn-link { color: var(--accent); font-weight: 600; margin-top: 16px; display: inline-block; }
.help { padding: 16px 18px; }
.help h3 { font-size: 15px; margin: 20px 0 6px; }
.help p, .help li { color: var(--fg); }
.help ol { padding-left: 20px; margin: 6px 0; }
.help li { margin: 6px 0; }
.help .box { background: var(--chip); border-radius: 12px; padding: 12px 14px; margin: 12px 0; }
.help .muted { color: var(--muted); font-size: 13px; }

/* Espera y avisos */
.busy { position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.5); display: grid; place-items: center; }
.busy-box { background: var(--sheet); border-radius: 14px; padding: 22px 26px; display: flex; flex-direction: column; align-items: center; gap: 12px; min-width: 220px; max-width: 80vw; text-align: center; }
.spinner { width: 30px; height: 30px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); transform: translate(-50%, 20px); background: #262626; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px; opacity: 0; pointer-events: none; transition: .2s; z-index: 300; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
