/* -----------------------------------------------------------------------
   Insta Pages -- design system
   Concept: a darkroom contact sheet. "Instagram" itself is a portmanteau
   of "instant camera" + "telegram", so the vernacular here is film, not
   the Instagram app's own branding: frame counters, safelight red,
   contact-sheet grids. The 5-tile "frame strip" in the header is wired
   to which of your 5 backends actually answered the last request.
   ----------------------------------------------------------------------- */

@media (prefers-color-scheme: light) {
  html:not([data-theme]) { color-scheme: light; }
}

:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101114;
  --surface: #191b20;
  --surface-raised: #21242b;
  --border: #2c2f38;
  --text: #ECEDF0;
  --text-muted: #9297A3;
  --safelight: #E4572E;
  --safelight-strong: #ff6a3d;
  --lens: #2FB8A6;
  --lens-strong: #45d6c3;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  --overlay: rgba(8, 9, 11, 0.86);
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #F6F5F1;
  --surface: #FFFFFF;
  --surface-raised: #FBFAF7;
  --border: #E3E1DA;
  --text: #17181C;
  --text-muted: #6B6E76;
  --safelight: #D14A26;
  --safelight-strong: #b83e1f;
  --lens: #1F8F80;
  --lens-strong: #17766a;
  --shadow: 0 12px 28px rgba(20, 18, 14, 0.10);
  --overlay: rgba(20, 18, 14, 0.55);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: 48px;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

a { color: inherit; }

button {
  font-family: inherit;
  cursor: pointer;
}

img, video { max-width: 100%; display: block; }

:focus-visible {
  outline: 2px solid var(--lens);
  outline-offset: 2px;
}

/* ---------------- topbar ---------------- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-inner {
  max-width: 980px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.brand { display: flex; flex-direction: column; line-height: 1.15; margin-right: auto; }
.brand-mark {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", "Times New Roman", serif;
  font-weight: 700;
  font-size: 1.35rem;
  letter-spacing: -0.01em;
}
.brand-tagline {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 10.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-top: 2px;
}

/* frame strip -- the signature element */
.frame-strip {
  display: flex;
  gap: 4px;
  list-style: none;
  padding: 3px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.frame-strip li {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 9.5px;
  border-radius: 3px;
  color: var(--text-muted);
  background: transparent;
  transition: background-color 0.25s ease, color 0.25s ease;
}
.frame-strip li.is-active { background: var(--lens); color: #06211d; }
.frame-strip li.is-failed { background: var(--safelight); color: #2a0d05; }

.theme-toggle {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text);
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.theme-toggle:hover { border-color: var(--lens); }

/* ---------------- layout ---------------- */
.wrap { max-width: 980px; margin: 0 auto; padding: 36px 20px; }

/* ---------------- hero / search ---------------- */
.hero { display: flex; justify-content: center; margin-bottom: 8px; }
.search-panel {
  width: 100%;
  max-width: 620px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px;
  box-shadow: var(--shadow);
}
.search-modes {
  display: inline-flex;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 14px;
}
.mode-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 6px;
}
.mode-btn.active { background: var(--surface-raised); color: var(--text); box-shadow: 0 1px 0 var(--border); }

.search-form { display: flex; gap: 10px; }
.search-form input {
  flex: 1;
  min-width: 0;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
}
.search-form input:focus { outline: none; border-color: var(--lens); }
.search-form button {
  background: var(--safelight);
  color: #fff;
  border: none;
  padding: 12px 20px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
}
.search-form button:hover { background: var(--safelight-strong); }

.search-example { margin-top: 12px; font-size: 12.5px; color: var(--text-muted); }
.example-chip {
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}
.example-chip:hover { border-color: var(--lens); }

/* ---------------- feedback states ---------------- */
.error {
  max-width: 620px;
  margin: 20px auto 0;
  background: color-mix(in srgb, var(--safelight) 14%, var(--surface));
  color: var(--safelight-strong);
  border-left: 3px solid var(--safelight);
  padding: 14px 18px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
}
.loader { text-align: center; padding: 50px 20px; color: var(--text-muted); }
.spinner {
  width: 34px; height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--lens);
  border-radius: 50%;
  margin: 0 auto 14px;
  animation: spin 0.8s linear infinite;
}
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
@keyframes spin { to { transform: rotate(360deg); } }

.hint { text-align: center; color: var(--text-muted); padding: 48px 20px; font-size: 14px; }

/* ---------------- profile-pic-only card ---------------- */
.pfp-card {
  max-width: 420px;
  margin: 28px auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 26px;
  text-align: center;
  box-shadow: var(--shadow);
}
.pfp-frame {
  width: 168px; height: 168px;
  margin: 0 auto 16px;
  border-radius: 50%;
  padding: 4px;
  background: linear-gradient(135deg, var(--safelight), var(--lens));
}
.pfp-frame img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; border: 3px solid var(--surface); background: var(--surface-raised); }
.pfp-meta h2 { font-size: 15px; font-weight: 600; margin-bottom: 14px; color: var(--text-muted); }

.btn-primary {
  display: inline-block;
  background: var(--safelight);
  color: #fff;
  font-weight: 700;
  font-size: 13.5px;
  padding: 10px 18px;
  border-radius: 8px;
  text-decoration: none;
}
.btn-primary:hover { background: var(--safelight-strong); }

/* ---------------- profile card ---------------- */
.profile-card {
  margin-top: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 8px 0;
  box-shadow: var(--shadow);
}
.profile-header { display: flex; gap: 30px; padding: 26px 20px; align-items: flex-start; flex-wrap: wrap; }
.avatar-frame {
  width: 108px; height: 108px;
  border-radius: 50%;
  padding: 3px;
  flex-shrink: 0;
  background: var(--border);
}
.avatar-frame.has-story { background: linear-gradient(135deg, var(--safelight), var(--lens)); }
.avatar-frame img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; border: 3px solid var(--surface); background: var(--surface-raised); }

.profile-info { flex: 1; min-width: 230px; }
.profile-name-row, .profile-handle-row { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.profile-name-row h1 { font-size: 16px; font-weight: 700; }
.profile-handle-row h2 { font-size: 14px; font-weight: 400; color: var(--text-muted); margin-bottom: 10px; }
.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 999px;
}
.badge-verified { background: color-mix(in srgb, var(--lens) 20%, transparent); color: var(--lens-strong); }
.badge-private { background: var(--surface-raised); color: var(--text-muted); border: 1px solid var(--border); }

.profile-stats { display: flex; gap: 26px; list-style: none; margin-bottom: 12px; flex-wrap: wrap; }
.profile-stats li { display: flex; flex-direction: column; font-size: 13.5px; }
.profile-stats strong { font-size: 15px; }
.profile-stats span { color: var(--text-muted); font-size: 12px; }

.profile-bio { font-size: 13.5px; white-space: pre-wrap; line-height: 1.55; margin-bottom: 6px; max-width: 440px; }
.profile-link { font-size: 13px; font-weight: 600; color: var(--lens-strong); text-decoration: none; display: inline-block; margin-bottom: 6px; }
.profile-link:before { content: "🔗 "; }
.avatar-download { display: inline-block; font-size: 12.5px; color: var(--text-muted); text-decoration: underline; margin-top: 2px; }
.avatar-download:hover { color: var(--lens-strong); }

/* ---------------- highlights row ---------------- */
.highlights-row {
  display: flex; gap: 18px; list-style: none;
  padding: 6px 20px 22px;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
}
.highlight-item { flex-shrink: 0; }
.highlight-item button { background: none; border: none; width: 70px; text-align: center; }
.highlight-circle {
  width: 60px; height: 60px; border-radius: 50%; padding: 2px;
  background: linear-gradient(135deg, var(--safelight), var(--lens));
  margin: 0 auto 6px;
}
.highlight-circle img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; border: 2px solid var(--surface); background: var(--surface-raised); }
.highlight-item span { font-size: 11.5px; color: var(--text); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.highlights-empty { color: var(--text-muted); font-size: 13px; padding: 4px 20px 20px; }

/* ---------------- tabs ---------------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); padding: 0 12px; overflow-x: auto; }
.tab-btn {
  border: none; background: none; color: var(--text-muted);
  font-size: 12.5px; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase;
  padding: 13px 12px; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab-btn.active { color: var(--text); border-bottom-color: var(--safelight); }
.tab-count { font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace; color: var(--text-muted); font-weight: 400; text-transform: none; }

.tab-panel { padding: 18px 12px 22px; min-height: 120px; }
.section-loading, .section-empty { text-align: center; color: var(--text-muted); font-size: 13.5px; padding: 34px 10px; }
.section-error {
  background: color-mix(in srgb, var(--safelight) 12%, var(--surface-raised));
  color: var(--safelight-strong);
  padding: 14px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  border-left: 3px solid var(--safelight);
}

/* ---------------- media grid (posts / reels) ---------------- */
.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  gap: 4px;
}
.grid-cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border: none;
  padding: 0;
  overflow: hidden;
  border-radius: 4px;
  background: var(--surface-raised);
}
.grid-cell img { width: 100%; height: 100%; object-fit: cover; }
.grid-type-icon {
  position: absolute; top: 6px; right: 6px;
  color: #fff; font-size: 13px; text-shadow: 0 1px 3px rgba(0,0,0,0.6);
}
.grid-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.45);
  color: #fff; font-size: 13px; font-weight: 700;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.grid-cell:hover .grid-overlay, .grid-cell:focus-visible .grid-overlay { opacity: 1; }

.load-more-wrap { text-align: center; margin-top: 18px; }
.load-more-btn {
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text);
  padding: 9px 20px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
}
.load-more-btn:hover { border-color: var(--lens); }
.load-more-btn:disabled { opacity: 0.5; cursor: default; }

/* ---------------- stories grid ---------------- */
.stories-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.story-cell {
  position: relative;
  aspect-ratio: 9 / 16;
  border: 2px solid transparent;
  border-radius: 10px;
  overflow: hidden;
  padding: 0;
  background: var(--surface-raised);
}
.story-cell:hover { border-color: var(--safelight); }
.story-cell img, .story-cell video { width: 100%; height: 100%; object-fit: cover; }
.story-badge {
  position: absolute; bottom: 6px; left: 6px;
  background: rgba(0,0,0,0.6); color: #fff;
  font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 999px;
}

/* ---------------- modal / lightbox ---------------- */
.modal { position: fixed; inset: 0; z-index: 50; }
.modal-backdrop { position: absolute; inset: 0; background: var(--overlay); border: none; }
.modal-panel {
  position: relative;
  max-width: 640px;
  margin: 4vh auto;
  background: var(--surface);
  border-radius: 14px;
  overflow: hidden;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
}
.modal-close {
  position: absolute; top: 10px; right: 10px; z-index: 2;
  width: 32px; height: 32px; border-radius: 50%;
  border: none; background: rgba(0,0,0,0.55); color: #fff; font-size: 18px; line-height: 1;
}
.modal-body { overflow-y: auto; }
.modal-media-area { background: #000; display: flex; align-items: center; justify-content: center; min-height: 220px; }
.modal-media-area img, .modal-media-area video { width: 100%; max-height: 74vh; object-fit: contain; }
.modal-loading { padding: 60px 20px; text-align: center; color: var(--text-muted); }
.modal-nav { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--border); font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; }
.modal-nav button { border: 1px solid var(--border); background: var(--surface-raised); color: var(--text); padding: 6px 12px; border-radius: 7px; font-size: 12px; }
.modal-nav button:disabled { opacity: 0.4; }
.modal-footer { padding: 16px 18px; }
.modal-caption { font-size: 13.5px; white-space: pre-wrap; margin-bottom: 12px; color: var(--text-muted); }
.modal-download {
  display: inline-block;
  background: var(--safelight);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 8px;
  text-decoration: none;
}
.modal-download:hover { background: var(--safelight-strong); }

/* ---------------- footer ---------------- */
.site-footer { max-width: 980px; margin: 40px auto 0; padding: 0 20px; text-align: center; color: var(--text-muted); font-size: 12px; }

/* ---------------- responsive ---------------- */
@media (max-width: 640px) {
  .wrap { padding: 24px 14px; }
  .search-form { flex-direction: column; }
  .search-form button { width: 100%; }
  .profile-header { gap: 18px; padding: 20px 14px; }
  .avatar-frame { width: 84px; height: 84px; }
  .brand-tagline { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
