:root {
  color-scheme: light dark;
  --primary: #5b4fe0;
  --primary-dark: #3a2c9e;
  --primary-light: #efebff;
  --bg: #f7f6fc;
  --card: #ffffff;
  --text: #1a1b3a;
  --muted: #8b8a9e;
  --border: #ecebf5;
  --gold: #f5b942;
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary: #8b7bff;
    --primary-dark: #4a3ac0;
    --primary-light: #2a2454;
    --bg: #15132b;
    --card: #201d42;
    --text: #f1effa;
    --muted: #a6a3c2;
    --border: #322d5c;
    --gold: #f5c360;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  overflow: hidden; /* the outer shell never scrolls/bounces — only #scroll-area does */
}

body {
  margin: 0;
  font-family: 'Poppins', 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

#scroll-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
}

.topbar {
  flex: none;
  padding: 24px 20px 8px;
  text-align: center;
}
.topbar h1 { margin: 0; font-size: 1.7rem; font-weight: 700; display: flex; align-items: center; justify-content: center; gap: 8px; }
.subtitle { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; }
.app-logo { width: 34px; height: 34px; border-radius: 9px; vertical-align: middle; }
.app-logo-lg { width: 84px; height: 84px; border-radius: 20px; margin-bottom: 8px; }

/* --- Bottom tab bar --- */
.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 8px 4px calc(8px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 24px rgba(91, 79, 224, 0.08);
  z-index: 10;
}
.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.68rem;
  font-family: inherit;
  cursor: pointer;
  padding: 6px 2px;
  border-radius: 12px;
  white-space: nowrap;
}
.tab-btn .tab-icon { font-size: 1.15rem; }
.tab-btn.active { color: var(--primary); font-weight: 600; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(64px + env(safe-area-inset-bottom) + 24px);
}

.tab-content { display: none; }
.tab-content.active { display: block; }

h2 { font-weight: 700; }
.checkin-recent-title { margin-top: 28px; }

/* --- Hero book card --- */
.book-card {
  background: linear-gradient(160deg, var(--primary-dark), var(--primary));
  color: #fff;
  border-radius: 20px;
  padding: 22px 20px;
  margin-bottom: 18px;
  box-shadow: 0 12px 30px rgba(91, 79, 224, 0.25);
}
.book-title { margin: 0 0 4px; font-size: 1.3rem; font-weight: 700; text-align: center; }
.book-info-group { color: rgba(255,255,255,0.78); font-size: 0.85rem; text-align: center; margin-bottom: 16px; }

.book-score-panel {
  background: rgba(255,255,255,0.12);
  border-radius: 14px;
  padding: 4px 14px;
}
.score-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  font-size: 0.82rem;
  color: rgba(255,255,255,0.85);
}
.score-row + .score-row { border-top: 1px solid rgba(255,255,255,0.14); }
.score-row strong { color: #fff; font-weight: 700; }

.book-card-mockup-wrap { display: flex; justify-content: center; margin-bottom: 16px; }

/* --- 3D book mockup --- */
.book-mockup { width: 112px; height: 164px; flex: none; perspective: 500px; }
.book-mockup-lg { width: 148px; height: 216px; }
.book-mockup-cover {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 3px 7px 7px 3px;
  background: linear-gradient(135deg, rgba(255,255,255,0.25), rgba(255,255,255,0.05));
  background-size: cover;
  background-position: center;
  transform: rotateY(-22deg);
  transform-style: preserve-3d;
  box-shadow:
    inset 3px 0 0 rgba(0,0,0,0.2),
    6px 14px 20px rgba(0,0,0,0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.4rem;
}
.book-mockup-cover::after {
  content: '';
  position: absolute;
  top: 2px;
  bottom: 2px;
  right: -5px;
  width: 5px;
  background: repeating-linear-gradient(to bottom, #f1efe5 0 2px, #d8d5c8 2px 4px);
  transform: rotateY(90deg) translateZ(2.5px);
  border-radius: 0 2px 2px 0;
}

/* --- Floating action button --- */
.fab {
  position: fixed;
  right: 18px;
  bottom: 86px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  font-size: 1.8rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 24px rgba(91, 79, 224, 0.4);
  cursor: pointer;
  z-index: 15;
}
.fab:active { transform: scale(0.94); }

/* --- Feed day headers --- */
.feed-day-header {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  margin: 4px 2px;
}

/* --- Rank / leaderboard cards --- */
.rank-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: 0 6px 18px rgba(91, 79, 224, 0.06);
}
.rank-card-top { display: flex; align-items: center; gap: 12px; }
.rank-score-num { font-size: 1.2rem; font-weight: 700; color: var(--primary); flex: none; }
.rank-meta-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 0.72rem;
  color: var(--muted);
}
.rank-avatar, .avatar-sm, .profile-avatar {
  border-radius: 50%;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #fff;
  position: relative;
  overflow: hidden;
}
.rank-avatar img, .avatar-sm img, .profile-avatar img {
  width: 100%; height: 100%; object-fit: cover;
}
.rank-avatar { width: 46px; height: 46px; font-size: 0.95rem; }
.avatar-sm { width: 36px; height: 36px; font-size: 0.8rem; }
.rank-medal {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--card);
  border-radius: 50%;
  font-size: 0.85rem;
  line-height: 1;
  padding: 1px;
}
.rank-name { font-weight: 600; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; flex: 1; min-width: 0; }
.badge {
  font-size: 0.65rem;
  background: var(--primary-light);
  color: var(--primary);
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
}
.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--primary-dark)); border-radius: 999px; }

form { display: flex; flex-direction: column; gap: 14px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 0.85rem; color: var(--muted); font-weight: 500; }
input, select, textarea {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  resize: vertical;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.inline-row { display: flex; gap: 8px; }
.inline-row input { flex: 1; }
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper input { text-align: center; flex: 1; }
.stepper button {
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--primary-light); color: var(--primary); font-size: 1.2rem; cursor: pointer; font-weight: 700;
}
button.primary {
  padding: 14px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  border: none;
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(91, 79, 224, 0.3);
}
button.primary:active { opacity: 0.88; }
button:disabled { opacity: 0.6; cursor: not-allowed; }
#add-participant-btn {
  border: 1px solid var(--primary);
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 999px;
  padding: 0 16px;
  cursor: pointer;
  font-weight: 600;
  font-family: inherit;
}

.hint { color: var(--muted); font-size: 0.82rem; }

#participants-list { list-style: none; padding: 0; margin: 0; }
#participants-list li {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
  margin-bottom: 8px;
  font-size: 0.88rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
#participants-list .when { color: var(--muted); font-size: 0.72rem; }
.remove-btn {
  background: none; border: none; color: var(--muted); cursor: pointer; font-size: 0.95rem;
}
.edit-btn {
  background: none; border: none; color: var(--primary); cursor: pointer; font-size: 0.95rem;
}

/* --- Photo picker --- */
.hidden { display: none !important; }
.hidden-input { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; }
.photo-picker { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.photo-upload-btn {
  display: inline-block;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.photo-preview {
  width: 100%;
  max-height: 220px;
  object-fit: cover;
  border-radius: 16px;
}

/* --- Feed cards (Gym Rats style) --- */
#feed-list { display: flex; flex-direction: column; gap: 14px; }
.feed-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 6px 18px rgba(91, 79, 224, 0.06);
}
.feed-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
}
.feed-card-header .who { flex: 1; min-width: 0; }
.feed-card-header .who .name { font-weight: 600; font-size: 0.9rem; }
.feed-card-header .who .when { color: var(--muted); font-size: 0.72rem; }
.feed-card-photo { width: 100%; max-height: 360px; object-fit: cover; display: block; background: var(--primary-light); }
.feed-card-body { padding: 12px 14px 14px; }
.feed-card-title { font-weight: 700; margin-bottom: 4px; }
.feed-card-desc { font-size: 0.88rem; color: var(--text); margin-bottom: 8px; white-space: pre-wrap; }
.feed-card-meta { display: flex; gap: 12px; font-size: 0.78rem; color: var(--muted); flex-wrap: wrap; }
.feed-card-actions { display: flex; gap: 14px; padding: 0 14px 12px; }

/* --- Edit form (inline, reused in feed + profile) --- */
.edit-form {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.edit-form-actions { display: flex; gap: 8px; }
.edit-form-actions button {
  flex: 1;
  border-radius: 999px;
  padding: 10px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: none;
}
.edit-form-actions .save-btn { background: var(--primary); color: #fff; }
.edit-form-actions .cancel-btn { background: var(--primary-light); color: var(--primary); }

/* --- Profile --- */
.profile-header-card {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 18px;
  margin: 14px 0;
}
.profile-avatar-wrap { position: relative; flex: none; }
.profile-avatar {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  object-fit: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 1.5rem;
}
.profile-avatar-edit {
  position: absolute;
  bottom: -2px;
  right: -2px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  cursor: pointer;
  border: 2px solid var(--card);
}
.profile-name-row { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.profile-name-row input { font-weight: 600; }
.profile-name-actions { display: flex; gap: 8px; }
.profile-name-actions button {
  border: none; border-radius: 999px; padding: 6px 12px; font-size: 0.78rem; font-weight: 600;
  cursor: pointer; font-family: inherit;
}
.profile-name-actions .save-btn { background: var(--primary); color: #fff; }

.stats-row { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.stat-chip {
  flex: 1;
  min-width: 90px;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 16px;
  padding: 12px;
  text-align: center;
}
.stat-chip .num { font-size: 1.25rem; font-weight: 700; display: block; }
.stat-chip .label { font-size: 0.7rem; color: var(--muted); }

#checkin-feedback, #settings-feedback, #quiz-question-feedback, #quiz-feedback, #invite-feedback, #invite-admin-feedback {
  color: var(--primary); font-size: 0.88rem; min-height: 1.2em; font-weight: 500;
}

/* --- Admin lock panel --- */
.admin-lock {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 16px;
}
.admin-lock .admin-code-input { text-transform: uppercase; letter-spacing: 1px; }
.admin-unlock-btn {
  border: none;
  background: var(--primary);
  color: #fff;
  border-radius: 999px;
  padding: 0 18px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

/* --- Logout --- */
.logout-btn {
  display: block;
  width: 100%;
  margin-top: 24px;
  border: 1px solid var(--border);
  background: none;
  color: var(--muted);
  border-radius: 999px;
  padding: 12px;
  font-size: 0.85rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
}

/* --- Splash screen --- */
.splash-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, var(--primary-dark), var(--primary));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 200;
  transition: opacity 0.4s ease, visibility 0.4s ease;
}
.splash-screen.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.splash-logo {
  width: 96px;
  height: 96px;
  border-radius: 24px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.3);
  animation: splash-pop 0.5s ease;
}
.splash-title {
  color: #fff;
  font-weight: 700;
  font-size: 1.3rem;
  letter-spacing: 0.5px;
  animation: splash-fade-up 0.5s ease 0.1s both;
}
@keyframes splash-pop {
  0% { transform: scale(0.6); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes splash-fade-up {
  0% { transform: translateY(8px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}

/* --- Invite gate --- */
.invite-gate {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, var(--primary-dark), var(--primary));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 100;
}
.invite-gate-card {
  background: var(--card);
  border-radius: 24px;
  padding: 28px 24px;
  max-width: 320px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
}
.invite-gate-emoji { font-size: 2.2rem; margin-bottom: 8px; }
.invite-gate-card h2 { margin: 0 0 6px; }
.invite-gate-card .hint { margin-bottom: 18px; }
.invite-gate-card form { text-align: left; }
#invite-code-input { text-align: center; letter-spacing: 2px; text-transform: uppercase; font-weight: 600; }

/* --- Invite code (Config) --- */
#invite-code-display { font-weight: 700; font-size: 1.1rem; letter-spacing: 2px; color: var(--primary); }
.invite-actions { display: flex; flex-direction: column; gap: 6px; }
.invite-actions button {
  border: none;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}

.subtabs { display: flex; gap: 6px; margin-bottom: 16px; }
.subtab-btn {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.82rem;
  font-family: inherit;
  font-weight: 500;
}
.subtab-btn.active { background: var(--primary-light); color: var(--primary); border-color: var(--primary); }
.subtab-content { display: none; }
.subtab-content.active { display: block; }

.unlock-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px 16px;
  margin-bottom: 16px;
}
#unlock-status { font-weight: 600; font-size: 0.88rem; }
#unlock-status .sub { display: block; font-weight: 400; color: var(--muted); font-size: 0.75rem; margin-top: 2px; }
#unlock-toggle-btn {
  border: none;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 999px;
  padding: 9px 16px;
  cursor: pointer;
  font-weight: 600;
  font-family: inherit;
  white-space: nowrap;
}

.quiz-question-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 14px 16px;
  margin-bottom: 10px;
}
.quiz-question-card .q-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  font-weight: 600;
  margin-bottom: 8px;
}
.quiz-options { display: flex; flex-direction: column; gap: 6px; }
.quiz-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  cursor: pointer;
  font-size: 0.88rem;
}
.quiz-option input { margin: 0; }
.quiz-option.correct { border-color: var(--gold); background: rgba(245, 185, 66, 0.14); }
.quiz-option.selected-wrong { border-color: var(--primary); }
.quiz-result-banner {
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 16px;
  padding: 14px 16px;
  font-weight: 600;
  margin-bottom: 12px;
}
.manage-question-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
