:root {
  --bg: #0b0f1a;
  --card: #141b2d;
  --card-border: #1e2a45;
  --primary: #6c5ce7;
  --primary-hover: #5a4bd1;
  --accent: #00cec9;
  --text: #e8ecf4;
  --muted: #8b95a8;
  --error: #ff6b6b;
  --success: #51cf66;
  --rank-gold: #ffd43b;
  --radius: 16px;
  --shadow: 0 8px 32px rgba(0,0,0,0.4);
}

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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.bg-blobs {
  position: fixed;
  inset: 0;
  overflow: hidden;
  z-index: 0;
  pointer-events: none;
}
.blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.35;
}
.blob1 {
  width: 400px; height: 400px;
  background: var(--primary);
  top: -100px; left: -100px;
}
.blob2 {
  width: 350px; height: 350px;
  background: var(--accent);
  bottom: -80px; right: -80px;
}

.container {
  position: relative;
  z-index: 1;
  max-width: 480px;
  margin: 0 auto;
  padding: 2rem 1rem;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.container.dashboard {
  max-width: 900px;
  justify-content: flex-start;
  padding-top: 1rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 1.75rem;
  box-shadow: var(--shadow);
}

.auth-card {
  text-align: center;
}

.logo {
  margin-bottom: 1.5rem;
}
.logo-icon {
  font-size: 2.5rem;
  display: block;
  margin-bottom: 0.25rem;
}
.logo h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.tagline {
  color: var(--muted);
  font-size: 0.9rem;
}

.tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  background: rgba(0,0,0,0.25);
  padding: 4px;
  border-radius: 12px;
}
.tab {
  flex: 1;
  padding: 0.6rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s;
}
.tab.active {
  background: var(--primary);
  color: white;
}

.form {
  display: none;
  text-align: left;
}
.form.active {
  display: block;
}

.field {
  margin-bottom: 1rem;
}
.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
  color: var(--muted);
}
.field input,
.field select {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--card-border);
  border-radius: 10px;
  background: rgba(0,0,0,0.3);
  color: var(--text);
  font-size: 1rem;
  transition: border-color 0.2s;
}
.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--primary);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.75rem 1.25rem;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.btn.primary {
  width: 100%;
  background: var(--primary);
  color: white;
  margin-top: 0.5rem;
}
.btn.primary:hover {
  background: var(--primary-hover);
}
.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--card-border);
}
.btn.ghost:hover {
  color: var(--text);
  border-color: var(--muted);
}

.error {
  background: rgba(255,107,107,0.15);
  color: var(--error);
  padding: 0.6rem 0.9rem;
  border-radius: 8px;
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
}

.footer-note {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 1.5rem;
}
.footer-note a {
  color: var(--accent);
  text-decoration: none;
}

/* Navbar */
.navbar {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem;
}
.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 1.15rem;
}

/* Profile */
.profile-header {
  margin-bottom: 1.5rem;
}
.avatar-section {
  display: flex;
  gap: 1.5rem;
  align-items: center;
  flex-wrap: wrap;
}
.avatar-wrap {
  position: relative;
  width: 100px;
  height: 100px;
  flex-shrink: 0;
}
.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--primary);
}
.avatar.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  font-size: 2.5rem;
  font-weight: 700;
  color: white;
}
.avatar-upload {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 32px;
  height: 32px;
  background: var(--card);
  border: 2px solid var(--primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.9rem;
}
.profile-info h2 {
  font-size: 1.25rem;
  word-break: break-all;
}
.rank {
  color: var(--rank-gold);
  font-weight: 600;
  margin: 0.25rem 0 0.75rem;
}
.xp-bar-wrap {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.xp-bar {
  flex: 1;
  height: 10px;
  background: rgba(0,0,0,0.4);
  border-radius: 5px;
  overflow: hidden;
  max-width: 220px;
}
.xp-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 5px;
  transition: width 0.4s ease;
  width: 0%;
}
#xpText {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
}
@media (max-width: 700px) {
  .grid { grid-template-columns: 1fr; }
}
.admin-grid .full-width {
  grid-column: 1 / -1;
}

.card h3 {
  font-size: 1.1rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.badges-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.badge-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  background: rgba(0,0,0,0.25);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  padding: 0.75rem;
  min-width: 90px;
  text-align: center;
}
.badge-item img {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
}
.badge-item .badge-name {
  font-size: 0.8rem;
  font-weight: 600;
}
.badge-item .badge-xp {
  font-size: 0.75rem;
  color: var(--accent);
}

.rank-list {
  list-style: none;
}
.rank-list li {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--card-border);
  font-size: 0.9rem;
}
.rank-list li:last-child {
  border-bottom: none;
}
.rank-list .current {
  color: var(--rank-gold);
  font-weight: 700;
}

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

.table-wrap {
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
th, td {
  text-align: left;
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--card-border);
}
th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--card);
  border: 1px solid var(--card-border);
  padding: 0.85rem 1.5rem;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 100;
  font-weight: 500;
  animation: slideUp 0.3s ease;
}
.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--error); color: var(--error); }
@keyframes slideUp {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* Checkbox */
.checkbox-field {
  margin-top: 0.25rem;
}
.checkbox-label {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text);
  line-height: 1.4;
}
.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  flex-shrink: 0;
  accent-color: var(--primary);
  cursor: pointer;
}
.checkbox-label a {
  color: var(--accent);
  text-decoration: underline;
}
.checkbox-label a:hover {
  color: var(--primary);
}
.newsletter-toggle {
  font-size: 1rem;
  font-weight: 500;
}
.admin-grid .full-width {
  grid-column: 1 / -1;
}

/* CRITICAL: ensure hidden attribute actually hides (overrides .container display:flex) */
[hidden] {
  display: none !important;
}

/* Settings page */
.settings-page {
  padding-bottom: 3rem;
}
.page-title {
  font-size: 1.5rem;
  margin-bottom: 1.25rem;
  position: relative;
  z-index: 1;
}
.settings-section {
  margin-bottom: 1.25rem;
}
.settings-section h3 {
  margin-bottom: 0.5rem;
}
.section-desc {
  margin-bottom: 1rem;
  font-size: 0.9rem;
}
.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem 1rem;
}
@media (max-width: 600px) {
  .settings-grid { grid-template-columns: 1fr; }
}
.toggle-list {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  margin-top: 0.75rem;
}
.redeem-row {
  display: flex;
  gap: 0.75rem;
  align-items: stretch;
}
.redeem-row .field input {
  height: 100%;
}
textarea {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--card-border);
  border-radius: 10px;
  background: rgba(0,0,0,0.3);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  resize: vertical;
}
textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.settings-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 0.5rem;
  position: relative;
  z-index: 1;
}
.success-box {
  background: rgba(81,207,102,0.15);
  color: var(--success);
  padding: 0.6rem 0.9rem;
  border-radius: 8px;
  font-size: 0.9rem;
}
.nav-links {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.nav-links a {
  text-decoration: none;
}
.badge-check-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  max-height: 180px;
  overflow-y: auto;
  padding: 0.5rem;
  background: rgba(0,0,0,0.25);
  border-radius: 10px;
  border: 1px solid var(--card-border);
  margin-bottom: 0.75rem;
}
.code-list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  background: rgba(0,0,0,0.25);
  border: 1px solid var(--card-border);
  border-radius: 10px;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}
.code-list-item .code-name {
  font-weight: 700;
  font-family: ui-monospace, monospace;
  letter-spacing: 0.04em;
}
.btn.danger {
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
}
.btn.danger:hover {
  background: rgba(255,107,107,0.15);
}

/* Friends */
.friends-tabs {
  margin-bottom: 1.25rem;
  position: relative;
  z-index: 1;
}
.tab-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 0.35rem;
  margin-left: 0.25rem;
  border-radius: 999px;
  background: var(--error);
  color: white;
  font-size: 0.7rem;
  font-weight: 700;
}
.friends-panel { position: relative; z-index: 1; }
.people-list { display: flex; flex-direction: column; gap: 0.5rem; }
.person-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.5rem;
  border-radius: 12px;
  border: 1px solid var(--card-border);
  background: rgba(0,0,0,0.2);
}
.person-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}
.person-avatar.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: white;
  font-weight: 700;
}
.person-info { flex: 1; min-width: 0; }
.person-name { font-weight: 600; }
.person-meta { font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-actions { display: flex; gap: 0.35rem; flex-shrink: 0; flex-wrap: wrap; }
.btn-sm {
  padding: 0.4rem 0.7rem;
  font-size: 0.8rem;
  width: auto;
  margin: 0;
}
.dm-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 1rem;
  min-height: 420px;
}
@media (max-width: 700px) {
  .dm-layout { grid-template-columns: 1fr; }
}
.dm-sidebar { max-height: 480px; overflow-y: auto; }
.dm-main {
  display: flex;
  flex-direction: column;
  min-height: 420px;
  max-height: 520px;
}
.dm-friend-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 0.65rem 0.75rem;
  margin-bottom: 0.35rem;
  border: 1px solid var(--card-border);
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.dm-friend-btn:hover, .dm-friend-btn.active {
  border-color: var(--primary);
  background: rgba(108, 92, 231, 0.15);
}
.dm-header {
  display: flex;
  flex-direction: column;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--card-border);
  margin-bottom: 0.75rem;
}
.dm-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.25rem 0;
  min-height: 200px;
}
.dm-bubble {
  max-width: 80%;
  padding: 0.55rem 0.85rem;
  border-radius: 14px;
  font-size: 0.95rem;
}
.dm-bubble.mine {
  align-self: flex-end;
  background: var(--primary);
  color: white;
  border-bottom-right-radius: 4px;
}
.dm-bubble.theirs {
  align-self: flex-start;
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--card-border);
  border-bottom-left-radius: 4px;
}
.dm-time {
  font-size: 0.7rem;
  opacity: 0.75;
  margin-top: 0.25rem;
}
.dm-compose {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--card-border);
}
.dm-compose input {
  flex: 1;
  padding: 0.7rem 1rem;
  border: 1px solid var(--card-border);
  border-radius: 10px;
  background: rgba(0,0,0,0.3);
  color: var(--text);
  font-size: 1rem;
}
.dm-compose input:focus {
  outline: none;
  border-color: var(--primary);
}

.settings-nav-tabs {
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
  position: relative;
  z-index: 1;
}
.settings-sec { position: relative; z-index: 1; }
html[data-font="small"] { font-size: 14px; }
html[data-font="medium"] { font-size: 16px; }
html[data-font="large"] { font-size: 18px; }
html[data-font="xlarge"] { font-size: 20px; }
html.high-contrast {
  --card-border: #8899bb;
  --text: #ffffff;
  --muted: #c5d0e0;
}
html.reduce-motion * {
  animation: none !important;
  transition: none !important;
}
html.underline-links a { text-decoration: underline !important; }
html[data-accent="cyan"] { --primary: #00cec9; --primary-hover: #00b5b0; }
html[data-accent="pink"] { --primary: #e84393; --primary-hover: #d63384; }
html[data-accent="green"] { --primary: #00b894; --primary-hover: #00a383; }
html[data-accent="orange"] { --primary: #e17055; --primary-hover: #d63031; }
html[data-accent="red"] { --primary: #ff6b6b; --primary-hover: #ee5a5a; }
html[data-accent="purple"] { --primary: #6c5ce7; --primary-hover: #5a4bd1; }
html[data-theme="light"] {
  --bg: #f0f2f8;
  --card: #ffffff;
  --card-border: #d8dee9;
  --text: #1a1f2e;
  --muted: #5c677a;
  --shadow: 0 8px 32px rgba(0,0,0,0.08);
}
