:root {
  --bg: #000;
  --panel: #000;
  --surface: #121212;
  --line: #262626;
  --text: #F5F5F5;
  --muted: #A8A8A8;
  --them: #262626;
  --me: linear-gradient(135deg, #6a5cff 0%, #8f3dff 100%);
  --me-flat: #7a4dff;
  --link: #cfe3ff;
  --accent: #3797F0;
  --radius: 22px;
  --radius-s: 6px;
}
html[data-theme="blue"] {
  --me: linear-gradient(135deg, #3797F0 0%, #3797F0 100%);
  --me-flat: #3797F0;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, Roboto, 'Helvetica Neue', sans-serif;
  font-size: 15px;
  display: flex;
  justify-content: center;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: var(--link); }
[hidden] { display: none !important; }

/* ===================== экраны входа / загрузки ===================== */
.screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  padding: 1rem;
}
.logo {
  font-family: 'Grand Hotel', 'Segoe Script', cursive;
  font-size: 3.2rem;
  line-height: 1;
  text-align: center;
  margin-bottom: 1.6rem;
  color: #fff;
}
.login-card {
  width: 100%; max-width: 350px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 2.4rem 2.2rem 1.6rem;
  display: flex; flex-direction: column; gap: .5rem;
}
.field input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font-size: 16px;
  padding: .7rem .6rem;
  outline: none;
}
.field input:focus { border-color: #555; }
#loginBtn {
  margin-top: .6rem;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: .55rem;
  font-weight: 600;
}
#loginBtn:disabled { opacity: .5; }
.login-err { color: #ED4956; font-size: .85rem; text-align: center; margin-top: .6rem; }
.login-note { color: var(--muted); font-size: .8rem; text-align: center; margin-top: 1.6rem; }

.loading-box { width: 100%; max-width: 320px; text-align: center; }
.bar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.fill { height: 100%; width: 0; background: linear-gradient(90deg, #f9ce34, #ee2a7b, #6228d7); transition: width .2s; }
.fill.indet { width: 30%; animation: indet 1.2s infinite linear; }
@keyframes indet { from { transform: translateX(-100%); } to { transform: translateX(340%); } }
.loading-text { color: var(--muted); font-size: .85rem; margin-top: .8rem; }

/* ===================== каркас телефона ===================== */
.phone {
  position: relative;
  width: 100%; max-width: 640px;
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  overflow: hidden;
}
@media (max-width: 660px) { .phone { border: 0; } }

.topbar {
  display: flex; align-items: center; gap: .6rem;
  padding: .55rem .6rem .55rem .2rem;
  padding-top: calc(.55rem + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
  min-height: 56px;
}
.icon-btn {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%;
  color: var(--text);
  flex: 0 0 auto;
}
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn:active { background: rgba(255,255,255,.08); }
.back { font-size: 2rem; line-height: 1; padding-bottom: 4px; }
.ava {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(45deg, #F58529, #DD2A7B, #8134AF);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1rem; color: #fff;
  flex: 0 0 auto;
}
.ava.him { background: linear-gradient(45deg, #2e6bff, #7a4dff); }
.ava.ai { background: linear-gradient(45deg, #0ea5e9, #8b5cf6); }
.who { line-height: 1.2; min-width: 0; flex: 1 1 auto; }
.who .name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who .sub { color: var(--muted); font-size: .75rem; }
.top-actions { display: flex; gap: 0; flex: 0 0 auto; }

/* ===================== лента ===================== */
.feed-wrap { position: relative; flex: 1 1 auto; min-height: 0; }
.feed {
  position: absolute; inset: 0;
  overflow-y: auto;
  overflow-anchor: none;
  padding: .6rem .7rem 1rem;
  -webkit-overflow-scrolling: touch;
}
.sticky-day {
  position: absolute; top: .5rem; left: 50%; transform: translateX(-50%);
  z-index: 3;
  background: rgba(38,38,38,.92);
  color: var(--text);
  font-size: .72rem;
  padding: .28rem .7rem;
  border-radius: 999px;
  backdrop-filter: blur(6px);
  cursor: pointer;
  pointer-events: auto;
  opacity: 0; transition: opacity .2s;
  white-space: nowrap;
}
.sticky-day.show { opacity: 1; }

.day { display: flow-root; }
.day-sep, .tsep {
  text-align: center;
  color: var(--muted);
  font-size: .72rem;
  margin: 1.2rem 0 .8rem;
}
.tsep { margin: .9rem 0 .5rem; }

.row {
  display: flex;
  align-items: flex-end;
  gap: .45rem;
  margin-top: 2px;
}
.row.g-first { margin-top: 10px; }
.row.has-r { margin-bottom: 4px; }
.row.them { justify-content: flex-start; }
.row.me { justify-content: flex-end; }
.row .av-s {
  width: 24px; height: 24px; border-radius: 50%;
  flex: 0 0 24px;
  font-size: .7rem;
  visibility: hidden;
}
.row.g-last .av-s { visibility: visible; }

.stack {
  position: relative;
  max-width: 74%;
  display: flex; flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}
.row.me .stack { align-items: flex-end; }
.who-label { font-size: .68rem; color: var(--muted); margin: 0 0 1px .5rem; }

.bubble {
  padding: .5rem .8rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.32;
  position: relative;
  max-width: 100%;
}
.them .bubble { background: var(--them); }
.me .bubble { background: var(--me); color: #fff; }
.me .bubble a { color: #fff; }
/* сгруппированные углы, как в Instagram */
.them.g-first:not(.g-last) .bubble { border-bottom-left-radius: var(--radius-s); }
.them.g-mid .bubble { border-top-left-radius: var(--radius-s); border-bottom-left-radius: var(--radius-s); }
.them.g-last:not(.g-first) .bubble { border-top-left-radius: var(--radius-s); }
.me.g-first:not(.g-last) .bubble { border-bottom-right-radius: var(--radius-s); }
.me.g-mid .bubble { border-top-right-radius: var(--radius-s); border-bottom-right-radius: var(--radius-s); }
.me.g-last:not(.g-first) .bubble { border-top-right-radius: var(--radius-s); }

.bubble.emoji { background: none !important; padding: .1rem .2rem; font-size: 2.4rem; line-height: 1.15; }
.bubble.media { padding: 0; background: none !important; }
.bubble.lost { color: var(--muted); font-style: italic; font-size: .85rem; }
.me .bubble.lost { color: rgba(255,255,255,.75); }
.edited { font-size: .68rem; opacity: .7; margin-left: .4rem; }

.bubble img.photo {
  display: block;
  width: auto; height: auto;
  max-width: min(260px, 66vw);
  max-height: 380px;
  border-radius: 18px;
  background: #1a1a1a;
  cursor: zoom-in;
}
.album {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 3px;
  width: min(260px, 66vw);
  border-radius: 18px;
  overflow: hidden;
}
.album img { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; background: #1a1a1a; cursor: zoom-in; }
.album.n3 img:first-child { grid-column: span 2; aspect-ratio: 2; }
.bubble video {
  display: block;
  width: auto; height: auto;
  min-width: 160px;
  max-width: min(260px, 66vw);
  max-height: 420px;
  border-radius: 18px;
  background: #111;
}

/* голосовое */
.voice { display: flex; align-items: center; gap: .5rem; width: 100%; min-width: 180px; max-width: 260px; }
.voice .vp {
  width: 32px; height: 32px; border-radius: 50%;
  background: rgba(255,255,255,.16);
  display: flex; align-items: center; justify-content: center;
  flex: 0 0 32px;
}
.voice .vp svg { width: 14px; height: 14px; fill: currentColor; }
.wave { display: flex; align-items: center; gap: 2px; height: 28px; flex: 1 1 auto; min-width: 0; overflow: hidden; position: relative; cursor: pointer; }
.wave i { display: block; width: 3px; border-radius: 2px; background: rgba(255,255,255,.35); }
.wave i.on { background: #fff; }
.them .wave i.on { background: #e8e8e8; }
.vdur { font-size: .74rem; opacity: .85; min-width: 34px; text-align: right; font-variant-numeric: tabular-nums; }
.vspeed { font-size: .68rem; opacity: .8; border: 1px solid rgba(255,255,255,.35); border-radius: 8px; padding: .15rem .35rem; white-space: nowrap; flex: 0 0 auto; }

/* репост */
.share { max-width: 260px; }
img.gif { display: block; width: min(220px, 60vw); height: auto; border-radius: 18px; background: #1a1a1a; }
.doodle { margin-top: 4px; }
.doodle img { display: block; width: auto; height: auto; max-width: min(220px, 60vw); max-height: 300px; }
.share .kind { font-size: .68rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin-bottom: .15rem; }
.me .share .kind { color: rgba(255,255,255,.75); }
.share .owner { font-weight: 600; margin-bottom: .15rem; }
.share .stext { font-size: .85rem; opacity: .9; margin-bottom: .3rem; white-space: normal; }
.share a.open { display: inline-block; font-size: .8rem; text-decoration: none; border: 1px solid rgba(255,255,255,.3); border-radius: 8px; padding: .2rem .5rem; margin-top: .1rem; }
.share-note { margin-top: 3px; }

/* реакции */
.reacts {
  position: relative;
  margin-top: -10px;
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 12px;
  font-size: .78rem;
  padding: 1px .4rem;
  line-height: 1.3;
  white-space: nowrap;
  z-index: 1;
}
.them .reacts { align-self: flex-end; margin-right: -6px; }
.me .reacts { align-self: flex-start; margin-left: -6px; }
.reacts .cnt { font-size: .7rem; color: var(--muted); margin-left: 2px; }

.time {
  font-size: .66rem;
  color: var(--muted);
  margin: 0 .45rem;
  display: none;
}
.row.showtime .time, body.alltime .row .time { display: block; }
.sys.showtime .time, body.alltime .sys .time { display: inline; }
.row.has-r.showtime .time, body.alltime .row.has-r .time { margin-top: 3px; }

.sys {
  text-align: center;
  color: var(--muted);
  font-size: .76rem;
  margin: .7rem 0;
  padding: 0 1rem;
}
.sys.react { font-size: .7rem; opacity: .8; margin: .3rem 0; }

.flash .bubble, .flash.sys { animation: flash 1.6s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 4px #f9ce34; } 100% { box-shadow: 0 0 0 0 transparent; } }

.endnote { text-align: center; color: var(--muted); font-size: .78rem; margin: 2.5rem 0 1rem; line-height: 1.6; }

/* ===================== нижняя панель ===================== */
.bottombar {
  display: flex; align-items: center; justify-content: space-between; gap: .4rem;
  padding: .45rem .7rem;
  padding-bottom: calc(.45rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  flex: 0 0 auto;
}
.nav-btn {
  color: var(--text);
  font-size: .82rem;
  padding: .6rem .8rem;
  border-radius: 999px;
  background: var(--surface);
  white-space: nowrap;
}
.nav-btn.date { font-weight: 600; flex: 1 1 auto; text-align: center; overflow: hidden; text-overflow: ellipsis; }
.nav-btn:active { background: #222; }

/* ===================== панели ===================== */
.panel {
  position: absolute; inset: 0;
  background: var(--bg);
  z-index: 10;
  display: flex; flex-direction: column;
  animation: slide .18s ease-out;
}
@keyframes slide { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.panel-head {
  display: flex; align-items: center; gap: .4rem;
  padding: .45rem .6rem .45rem .2rem;
  padding-top: calc(.45rem + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  min-height: 56px;
}
.panel-title { font-weight: 600; }
#searchInput {
  flex: 1 1 auto;
  background: var(--surface);
  border: 0; border-radius: 10px;
  color: var(--text);
  font-size: 16px;
  padding: .55rem .8rem;
  outline: none;
}
.chips { display: flex; gap: .4rem; padding: .5rem .7rem 0; overflow-x: auto; flex: 0 0 auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: .45rem .8rem;
  font-size: .8rem;
  color: var(--text);
  white-space: nowrap;
  flex: 0 0 auto;
}
.chip.on { background: #fff; color: #000; border-color: #fff; }
.search-count { color: var(--muted); font-size: .78rem; padding: .6rem .8rem .2rem; }
.results { flex: 1 1 auto; overflow-y: auto; padding: .3rem 0 .5rem; }
.res {
  display: flex; gap: .7rem; align-items: flex-start;
  padding: .55rem .8rem;
  border-bottom: 1px solid #161616;
  cursor: pointer;
}
.res:active { background: var(--surface); }
.res .rav { width: 32px; height: 32px; font-size: .8rem; }
.res .rbody { min-width: 0; flex: 1 1 auto; }
.res .rh { font-size: .72rem; color: var(--muted); margin-bottom: 2px; display: flex; gap: .5rem; }
.res .rh b { color: var(--text); font-weight: 600; }
.res .rt { font-size: .9rem; line-height: 1.3; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-wrap; }
.res mark { background: #f9ce34; color: #000; border-radius: 3px; padding: 0 1px; }
.res .thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; flex: 0 0 44px; background: #1a1a1a; }
.more-btn { margin: .4rem .8rem 1rem; padding: .6rem; border-radius: 10px; background: var(--surface); color: var(--text); flex: 0 0 auto; }

.cal-body { flex: 1 1 auto; overflow-y: auto; padding: .4rem .8rem 2rem; }
.cal-month { margin: 1rem 0 .4rem; }
.cal-month h3 { font-size: .95rem; margin: 0 0 .5rem; display: flex; justify-content: space-between; align-items: baseline; }
.cal-month h3 small { color: var(--muted); font-weight: 400; font-size: .75rem; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-grid .wd { text-align: center; color: var(--muted); font-size: .68rem; padding-bottom: 2px; }
.cal-grid .d {
  aspect-ratio: 1;
  border-radius: 10px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: .9rem;
  color: #444;
  background: #0d0d0d;
}
.cal-grid .d.has { color: var(--text); background: var(--surface); cursor: pointer; }
.cal-grid .d.has:active { background: #2a2a2a; }
.cal-grid .d.hot { background: #2b2140; }
.cal-grid .d.hot2 { background: #3d2a5c; }
.cal-grid .d small { font-size: .58rem; color: var(--muted); line-height: 1; margin-top: 2px; }
.cal-grid .d.cur { outline: 2px solid #f9ce34; }

.menu-body { padding: .8rem; overflow-y: auto; }
.menu-user { color: var(--muted); font-size: .85rem; margin-bottom: 1rem; }
.menu-user b { color: var(--text); }
.switch { display: grid; grid-template-columns: auto 1fr; column-gap: .7rem; align-items: center; padding: .7rem 0; border-bottom: 1px solid #161616; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--me-flat); margin: 0; grid-row: span 2; }
.switch span { font-size: .95rem; }
.switch small { color: var(--muted); font-size: .75rem; grid-column: 2; }
.stats { color: var(--muted); font-size: .82rem; line-height: 1.7; margin: 1.2rem 0; }
.stats b { color: var(--text); font-weight: 600; }
.logout { width: 100%; padding: .7rem; border-radius: 10px; background: var(--surface); color: #ED4956; font-weight: 600; }

/* ===================== просмотр фото ===================== */
.lightbox {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.96);
  display: flex; align-items: center; justify-content: center;
}
.lightbox img { max-width: 100vw; max-height: 100vh; max-height: 100dvh; object-fit: contain; }
.lb-close { position: absolute; top: calc(.6rem + env(safe-area-inset-top)); right: .8rem; font-size: 1.4rem; color: #fff; width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,.12); }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); font-size: 2.4rem; color: #fff; width: 48px; height: 64px; background: rgba(255,255,255,.08); border-radius: 12px; }
.lb-prev { left: .4rem; } .lb-next { right: .4rem; }
.lb-cap { position: absolute; bottom: calc(1rem + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); max-width: 90vw; text-align: center; color: #eee; font-size: .8rem; background: rgba(0,0,0,.6); padding: .3rem .8rem; border-radius: 999px; }

.toast {
  position: fixed; left: 50%; bottom: 5.5rem; transform: translateX(-50%);
  background: #262626; color: #fff;
  padding: .55rem 1rem; border-radius: 999px;
  font-size: .85rem; z-index: 60;
  box-shadow: 0 4px 20px rgba(0,0,0,.5);
}

.story-link {
  display: block; text-align: center; text-decoration: none;
  margin: 1.2rem 0 .2rem; padding: .7rem;
  border: 1px solid var(--line); border-radius: 10px;
  color: var(--text); font-weight: 600;
  background: linear-gradient(135deg, rgba(249,206,52,.12), rgba(238,42,123,.14), rgba(98,40,215,.14));
}
.story-link:active { background: #222; }
