/* ══════════════════════════════════════════════════════════════
   Msocial — Design System
   Modern, clean, Instagram-inspired with glassmorphism accents.
   Mobile-first. Light & dark themes via [data-theme] on <html>.
   ══════════════════════════════════════════════════════════════ */

/* ── Tokens ─────────────────────────────────────────────────── */
:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --bg: #f4f4f8;
  --bg-accent-1: rgba(124, 58, 237, 0.10);
  --bg-accent-2: rgba(236, 72, 153, 0.08);
  --surface: #ffffff;
  --surface-2: #f7f7fa;
  --glass: rgba(255, 255, 255, 0.72);
  --glass-border: rgba(255, 255, 255, 0.55);

  --text: #17171c;
  --text-muted: #6b6b76;
  --text-faint: #9a9aa4;

  --accent: #7c3aed;
  --accent-2: #ec4899;
  --gradient: linear-gradient(135deg, #7c3aed 0%, #ec4899 100%);
  --danger: #ef4444;

  --ring: rgba(124, 58, 237, 0.35);
  --shadow-sm: 0 1px 3px rgba(23, 23, 28, 0.07);
  --shadow-md: 0 8px 30px rgba(23, 23, 28, 0.10);
  --shadow-lg: 0 24px 70px rgba(23, 23, 28, 0.22);

  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 22px;

  --topbar-h: 60px;
  --bottomnav-h: 62px;
}

[data-theme="dark"] {
  --bg: #0e0e13;
  --bg-accent-1: rgba(124, 58, 237, 0.16);
  --bg-accent-2: rgba(236, 72, 153, 0.12);
  --surface: #181820;
  --surface-2: #20202a;
  --glass: rgba(24, 24, 32, 0.72);
  --glass-border: rgba(255, 255, 255, 0.09);

  --text: #f2f2f6;
  --text-muted: #a2a2ae;
  --text-faint: #6f6f7b;

  --ring: rgba(167, 139, 250, 0.45);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 8px 30px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 24px 70px rgba(0, 0, 0, 0.6);
}

/* ── Base ───────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  color: var(--text);
  background:
    radial-gradient(900px 480px at 85% -10%, var(--bg-accent-1), transparent 60%),
    radial-gradient(700px 420px at -10% 110%, var(--bg-accent-2), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  min-height: 100dvh;
}

h1, h2, h3 { line-height: 1.2; margin: 0 0 0.5em; font-weight: 700; letter-spacing: -0.02em; }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.1rem; }
p { margin: 0.4em 0; }
a { color: var(--accent); text-decoration: none; font-weight: 600; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }

.muted { color: var(--text-muted); font-size: 0.92rem; }

/* ── App shell ──────────────────────────────────────────────── */
.app-main {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(var(--topbar-h) + 18px) 16px calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px) + 26px);
}

body.no-nav .app-main {
  padding-top: calc(var(--topbar-h) + 18px);
  padding-bottom: 40px;
}

/* Top bar — glass */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 100;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(14px, env(safe-area-inset-left, 0px)) 0 max(14px, env(safe-area-inset-right, 0px));
  background: var(--glass);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  backdrop-filter: saturate(180%) blur(18px);
  border-bottom: 1px solid var(--glass-border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}
.brand:hover { text-decoration: none; }
.brand img { width: 30px; height: 30px; border-radius: 9px; box-shadow: var(--shadow-sm); }

.topbar-actions { display: flex; align-items: center; gap: 6px; }

/* Bottom nav — glass, icon tabs */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 100;
  height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex;
  background: var(--glass);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  backdrop-filter: saturate(180%) blur(18px);
  border-top: 1px solid var(--glass-border);
}

.bottom-nav a {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  transition: color 0.18s ease, transform 0.18s ease;
}
.bottom-nav a svg { width: 24px; height: 24px; }
.bottom-nav a:active { transform: scale(0.92); }
.bottom-nav a:hover { text-decoration: none; }
.bottom-nav a.active { color: var(--accent); }
.bottom-nav a.active svg { filter: drop-shadow(0 0 8px var(--ring)); }

/* ── Buttons & inputs ───────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: var(--radius-sm);
  padding: 12px 20px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--gradient);
  color: #fff;
  box-shadow: 0 6px 20px var(--ring);
}
.btn-primary:hover { opacity: 0.92; }

.btn-ghost {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--glass-border);
}

.btn-danger { background: var(--danger); color: #fff; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background 0.18s ease, transform 0.15s ease;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(0.9); }
.icon-btn svg { width: 21px; height: 21px; }

input[type="text"],
input[type="password"],
input:not([type]),
textarea {
  width: 100%;
  border: 1.5px solid var(--glass-border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  padding: 12px 14px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  -webkit-appearance: none;
  appearance: none;
}
textarea { resize: vertical; min-height: 88px; }

input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--ring);
}

input::placeholder, textarea::placeholder { color: var(--text-faint); }

input[type="file"] { font-size: 0.9rem; color: var(--text-muted); }
input[type="file"]::file-selector-button {
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  margin-right: 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--glass-border);
}

/* ── Cards & forms ──────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 24px 20px;
}

.auth-main { display: flex; align-items: flex-start; justify-content: center; }
.auth-card { width: 100%; max-width: 400px; margin-top: 4vh; }
.auth-card h2 {
  background: var(--gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  font-size: 1.7rem;
  margin-bottom: 14px;
}

.stack { display: flex; flex-direction: column; gap: 12px; }
.stack .btn { margin-top: 4px; }

.field-label { font-size: 0.88rem; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 6px; }

/* JS-filled message boxes */
[id$="-message"] {
  display: none;
  border-radius: var(--radius-sm);
  padding: 11px 14px;
  margin: 12px 0;
  font-size: 0.92rem;
  font-weight: 600;
  background: rgba(239, 68, 68, 0.1);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.25);
}
[id$="-message"]:not(:empty) { display: block; }

/* ── Section headings ───────────────────────────────────────── */
.section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 26px 0 14px;
  font-size: 1.25rem;
}
.section-title::after {
  content: "";
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, var(--glass-border), transparent);
}

/* ── Feed grid (JS-rendered) ────────────────────────────────── */
.feed-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  border-radius: var(--radius);
  overflow: hidden;
}

.grid-cell {
  position: relative;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  cursor: pointer;
  background: var(--surface-2);
}

.grid-cell img,
.grid-cell video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.25s ease;
}
.grid-cell:hover img,
.grid-cell:hover video { transform: scale(1.04); }

.grid-cell .text-cell {
  width: 100%;
  height: 100%;
  overflow: hidden;
  padding: 12px;
  box-sizing: border-box;
  font-size: 0.85rem;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  word-break: break-word;
}

.grid-cell .like-badge {
  position: absolute;
  bottom: 6px;
  left: 6px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(0, 0, 0, 0.45);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: #fff;
  font-size: 11.5px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
}
.grid-cell .like-badge::before { content: "♥"; font-size: 11px; }

/* ── Post popup (JS-rendered) ───────────────────────────────── */
.popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(10, 10, 16, 0.55);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.18s ease;
}

.popup-box {
  width: 100%;
  max-width: 600px;
  max-height: 92dvh;
  overflow-y: auto;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.22s ease;
}

.popup-box video,
.popup-box > img {
  width: 100%;
  border-radius: var(--radius);
  margin: 12px 0;
  display: block;
  background: var(--surface-2);
}

.popup-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

#popup-close-btn {
  border: 0;
  background: var(--surface-2);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
}
#popup-close-btn:hover { background: var(--glass-border); }

.popup-author {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0 8px;
  font-weight: 700;
}
.popup-author img {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid transparent;
  background: var(--gradient) border-box;
}

.popup-content { word-break: break-word; margin: 6px 0 12px; }

.popup-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.like-count { font-weight: 800; }

#popup-comments { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }

.comment {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 0.92rem;
}
.comment img { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; flex-shrink: 0; }
.comment strong { margin-right: 4px; }
.comment div { flex: 1; word-break: break-word; }

.delete-comment-btn {
  border: 0;
  background: transparent;
  color: var(--text-faint);
  font-weight: 800;
  cursor: pointer;
  padding: 0 4px;
  border-radius: 6px;
  font-size: 14px;
}
.delete-comment-btn:hover { color: var(--danger); background: rgba(239, 68, 68, 0.08); }

#popup-comment-form {
  display: flex;
  gap: 8px;
}
#popup-comment-form input { flex: 1; }
#popup-comment-form .btn { padding: 10px 18px; }

/* ── Profile ────────────────────────────────────────────────── */
#profile-info { text-align: center; }
#profile-info img {
  width: 92px;
  height: 92px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid transparent;
  background: var(--gradient) border-box;
  margin-bottom: 8px;
}
#profile-info p { margin: 4px 0; }
#profile-info .stat { color: var(--text-muted); font-size: 0.93rem; }
#profile-info .stat span { color: var(--text); font-weight: 800; }

#follow-btn {
  margin-top: 12px;
  border: 0;
  border-radius: var(--radius-sm);
  padding: 11px 30px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  background: var(--gradient);
  color: #fff;
  box-shadow: 0 6px 20px var(--ring);
}

#profile-edit-section { margin-top: 18px; }

/* ── Search results ─────────────────────────────────────────── */
#users-result ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
#users-result li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  padding: 12px 16px;
  box-shadow: var(--shadow-sm);
}
#users-result li a { font-weight: 700; }
#users-result li span, #users-result li { color: var(--text-muted); font-size: 0.93rem; }

/* ── Top profiles ranking ───────────────────────────────────── */
#rank-list ol { list-style: none; margin: 0; padding: 0; counter-reset: rank; display: flex; flex-direction: column; gap: 10px; }
#rank-list li {
  counter-increment: rank;
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  padding: 13px 16px;
  box-shadow: var(--shadow-sm);
}
#rank-list li::before {
  content: counter(rank);
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--gradient);
  color: #fff;
  font-weight: 800;
  font-size: 0.9rem;
}
#rank-list li:nth-child(1)::before { box-shadow: 0 0 16px var(--ring); }
#rank-list li a { font-weight: 700; flex: 1; }
#rank-list li .rank-likes { color: var(--text-muted); font-size: 0.9rem; font-weight: 600; }

/* ── Empty / loading states ─────────────────────────────────── */
#posts-list, #user-posts, #users-result, #posts-result, #rank-list, #profile-info {
  min-height: 40px;
  color: var(--text-muted);
}

/* ── Welcome line ───────────────────────────────────────────── */
.welcome { margin-top: 2px; }
.welcome .grad-text {
  background: var(--gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* ── Animations ─────────────────────────────────────────────── */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(28px); opacity: 0.6; } to { transform: translateY(0); opacity: 1; } }

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

/* ── Desktop refinements ────────────────────────────────────── */
@media (min-width: 720px) {
  .app-main { max-width: 620px; }
  .popup-overlay { align-items: center; padding: 24px; }
  .popup-box { border-radius: var(--radius-lg); }
  .bottom-nav {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    width: min(480px, calc(100% - 32px));
    bottom: 14px;
    border: 1px solid var(--glass-border);
    border-radius: 999px;
    height: 62px;
    padding-bottom: 0;
    box-shadow: var(--shadow-md);
  }
  body:not(.no-nav) .app-main {
    padding-bottom: 110px;
  }
}
