* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: #f4f6f8;
  color: #1a1a1a;
  -webkit-tap-highlight-color: transparent;
}
button, input, label { font-family: inherit; }
.view { display: none; min-height: 100vh; min-height: 100dvh; }
.view.active { display: block; }

/* ---------- Login / Register (mobile-first) ---------- */
#login-view, #register-view {
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
#login-view.active, #register-view.active { display: flex; }
.login-card {
  background: #fff;
  padding: 28px 24px;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.08);
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.login-card h1 { margin-top: 0; font-size: 22px; }
.subtitle { color: #666; font-size: 14px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 20px; }
.login-card input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid #ddd;
  font-size: 16px; /* prevents iOS zoom-on-focus */
  width: 100%;
}
.login-card button {
  padding: 14px;
  border: none;
  border-radius: 10px;
  background: #4f46e5;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  cursor: pointer;
}
.login-card button:active { background: #4338ca; }
.switch-link { font-size: 13px; margin-top: 16px; }
.switch-link a { color: #4f46e5; text-decoration: none; }
.file-label { text-align: left; font-size: 13px; color: #444; display: flex; flex-direction: column; gap: 4px; }
.avatar-preview { width: 72px; height: 72px; border-radius: 50%; object-fit: cover; margin: 0 auto; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 16px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 18px; line-height: 1.2; margin: 0; font-weight: 700; }
.topbar-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; font-size: 13px; }
.topbar-actions #welcome-msg { color: #666; font-size: 13px; }
.topbar button {
  padding: 6px 14px;
  border: none;
  border-radius: 999px;
  background: #eef0ff;
  color: #4f46e5;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

/* ---------- Dashboard ---------- */
.dashboard-main { max-width: 900px; margin: 0 auto; padding: 16px; }

.service-message-banner {
  background: #f4d03f;
  color: #3b2f00;
  border-radius: 12px;
  padding: 12px 14px;
  margin: 12px 0 14px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
.service-message-banner[hidden] { display: none; }

.stat-cards-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}
.stat-card {
  background: #fff;
  border-radius: 16px;
  padding: 16px 14px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
  text-align: center;
}
.stat-card-icon { font-size: 20px; margin-bottom: 4px; }
.stat-card-label { font-size: 12px; color: #777; margin-bottom: 6px; }
.stat-card-value { font-size: 26px; font-weight: 700; color: #1a1a1a; }
.stat-card-sub { font-size: 11px; color: #999; margin-top: 4px; }

.last-week-winner-card { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.lww-avatars { display: flex; gap: 4px; justify-content: center; flex-wrap: wrap; margin-bottom: 4px; }
.lww-avatars img { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; border: 2px solid #e0c040; }

.leaderboard-card {
  background: #fff;
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.leaderboard-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.leaderboard-header .section-title { margin: 0; font-size: 17px; font-weight: 700; color: #1a1a1a; }
.leaderboard-period { font-size: 12px; color: #4f46e5; background: #eef0ff; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }

.leaderboard-list { display: flex; flex-direction: column; }
.leaderboard-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  border-bottom: 1px solid #f0f1f3;
  cursor: pointer;
}
.leaderboard-row:last-child { border-bottom: none; }
.leaderboard-row-me { background: #f5f6ff; border-radius: 10px; }

.rank-number {
  width: 20px;
  flex-shrink: 0;
  text-align: center;
  font-weight: 700;
  font-size: 14px;
  color: #999;
}
.rank-number.rank-1 { color: #f59e0b; }
.rank-number.rank-2 { color: #4f46e5; }
.rank-number.rank-3 { color: #d97706; }

.leaderboard-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.leaderboard-main-col { flex: 1; min-width: 0; }
.leaderboard-name {
  font-size: 14px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.leaderboard-bar-outer {
  background: #eef0f2;
  border-radius: 999px;
  height: 6px;
  overflow: hidden;
}
.leaderboard-bar-inner {
  background: #f59e0b;
  height: 100%;
  border-radius: 999px;
  transition: width .3s;
}
.leaderboard-row-me .leaderboard-bar-inner { background: #4f46e5; }

.leaderboard-count { flex-shrink: 0; text-align: right; min-width: 46px; }
.leaderboard-count-num { font-size: 15px; font-weight: 700; color: #1a1a1a; }
.leaderboard-count-unit { font-size: 10px; color: #999; }

.rank-medal { flex-shrink: 0; width: 22px; text-align: center; font-size: 16px; margin-left: 2px; }

.fab-log-btn {
  display: block;
  width: 100%;
  margin: 18px auto 0;
  padding: 15px 24px;
  border: none;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(239,68,68,0.4);
}
.fab-log-btn:active { background: #dc2626; }

.section-title { margin: 28px 0 10px; font-size: 15px; color: #333; }

.carousel-wrapper { display: flex; align-items: center; gap: 6px; }
.carousel {
  display: flex;
  gap: 16px;
  overflow-x: auto;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x proximity;
  padding: 6px 4px 14px;
  flex: 1;
}
.carousel::-webkit-scrollbar { height: 4px; }
.carousel::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }

.carousel-arrow {
  border: none;
  background: #fff;
  width: 32px; height: 32px;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  cursor: pointer;
  flex-shrink: 0;
  font-size: 13px;
}

.carousel-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  flex-shrink: 0;
  width: 76px;
  scroll-snap-align: start;
}
.carousel-item img {
  width: 64px; height: 64px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid transparent;
  transition: transform .15s, border-color .15s;
}
.carousel-item:active img { transform: scale(1.06); border-color: #4f46e5; }
.carousel-item span {
  font-size: 11px;
  color: #444;
  text-align: center;
  max-width: 76px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- User detail ---------- */
.detail-main { max-width: 600px; margin: 0 auto; padding: 16px; text-align: center; }
.detail-avatar-wrapper { position: relative; display: inline-block; margin-bottom: 10px; }
.detail-avatar { width: 88px; height: 88px; border-radius: 50%; object-fit: cover; display: block; cursor: zoom-in; }
.change-avatar-btn {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #4f46e5;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.change-avatar-btn[hidden] { display: none; }
.change-avatar-btn:active { background: #4338ca; }
.progress-bar-outer { background: #e5e7eb; border-radius: 999px; height: 10px; overflow: hidden; margin: 8px 0 20px; }
.progress-bar-inner { background: #22c55e; height: 100%; width: 0%; transition: width .3s; }

.activity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 10px;
}
.activity-card { position: relative; font-size: 11px; color: #666; }
.activity-card img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 10px; display: block; cursor: zoom-in; }
.activity-date { margin-top: 4px; }
.activity-note {
  margin-top: 2px;
  font-size: 11px;
  color: #444;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.activity-exercise-type {
  margin-top: 2px;
  font-size: 11px;
  font-weight: 700;
  color: #4f46e5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.empty-msg { grid-column: 1/-1; color: #888; font-size: 14px; }

.activity-delete-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: rgba(239,68,68,0.92);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  z-index: 2;
}
.activity-delete-btn:active { background: #dc2626; }

.delete-account-btn {
  display: block;
  width: 100%;
  max-width: 320px;
  margin: 0 auto;
  padding: 14px;
  border: 1px solid #ef4444;
  border-radius: 10px;
  background: #fff;
  color: #ef4444;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}
.delete-account-btn:active { background: #ef4444; color: #fff; }

.account-settings-card {
  background: #fff;
  border-radius: 16px;
  padding: 18px 16px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
  max-width: 480px;
  margin: 32px auto 8px;
  text-align: left;
}
.account-settings-card[hidden] { display: none; }
.account-settings-title {
  margin: 0 0 16px;
  font-size: 16px;
  font-weight: 700;
  color: #1a1a1a;
}
.account-settings-section {
  padding: 14px 0;
  border-top: 1px solid #f0f1f3;
}
.account-settings-section:first-of-type { border-top: none; padding-top: 0; }
.account-settings-label {
  font-size: 13px;
  font-weight: 600;
  color: #555;
  margin-bottom: 8px;
}
.account-settings-row {
  display: flex;
  gap: 8px;
}
.account-settings-input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #ddd;
  font-size: 15px;
  min-width: 0;
}
.account-settings-btn {
  padding: 10px 16px;
  border: none;
  border-radius: 10px;
  background: #4f46e5;
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.account-settings-btn:active { background: #4338ca; }
.account-settings-feedback {
  margin-top: 6px;
  font-size: 13px;
  border-radius: 8px;
  padding: 6px 10px;
}
.account-settings-feedback[hidden] { display: none; }
.account-settings-feedback-success { background: #f0fdf4; color: #16a34a; }
.account-settings-feedback-error { background: #fef2f2; color: #dc2626; }

/* ---------- Photo lightbox ---------- */
.lightbox-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  z-index: 200;
  cursor: zoom-out;
}
.lightbox-overlay[hidden] { display: none; }
.lightbox-image {
  max-width: 100%;
  max-height: 75vh;
  border-radius: 12px;
  object-fit: contain;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  cursor: default;
}
.lightbox-note {
  color: #fff;
  font-size: 15px;
  text-align: center;
  max-width: 480px;
  cursor: default;
}
.lightbox-note[hidden] { display: none; }
.lightbox-exercise-type {
  color: #a5b4fc;
  font-size: 16px;
  font-weight: 700;
  text-align: center;
  max-width: 480px;
  cursor: default;
  margin: 0;
}
.lightbox-exercise-type[hidden] { display: none; }
.lightbox-timestamp {
  color: #ccc;
  font-size: 13px;
  text-align: center;
  max-width: 480px;
  cursor: default;
  margin: 0;
}
.lightbox-timestamp[hidden] { display: none; }

/* ---------- Log activity modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
}
.modal-overlay[hidden] { display: none; }
.modal-card {
  background: #fff;
  padding: 24px 20px calc(24px + env(safe-area-inset-bottom, 0px));
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 420px;
  text-align: center;
}
.capture-options { display: flex; flex-direction: column; gap: 10px; margin: 16px 0; }
.capture-btn {
  padding: 14px 8px;
  border: 2px dashed #ccc;
  border-radius: 12px;
  font-size: 14px;
  cursor: pointer;
}
.capture-btn:active { border-color: #4f46e5; color: #4f46e5; }
.capture-preview { width: 100%; max-height: 200px; object-fit: cover; border-radius: 10px; margin-bottom: 12px; }
#capture-exercise-type, #capture-note {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid #ddd;
  margin-bottom: 16px;
  font-size: 16px;
}
.modal-actions { display: flex; gap: 10px; }
.modal-actions button {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: 15px;
}
.modal-actions button:not(.secondary) { background: #4f46e5; color: #fff; }
.modal-actions button:not(.secondary):disabled { background: #c7c7f0; cursor: not-allowed; }
.modal-actions button.secondary { background: #eee; }

/* ---------- Desktop enhancements (min-width up) ---------- */
@media (min-width: 640px) {
  .dashboard-main { padding: 24px; }
  .stat-cards-row { grid-template-columns: 1fr 1fr 1fr; max-width: 720px; }
  .fab-log-btn { width: auto; padding: 14px 28px; }
  .carousel-item { width: 84px; }
  .carousel-item img { width: 72px; height: 72px; }
  .detail-avatar { width: 100px; height: 100px; }
  .modal-overlay { align-items: center; }
  .modal-card { border-radius: 16px; width: 360px; }
}
