/**
 * @file public/css/components.css
 * @description Standard reusable UI components (Buttons, forms, cards, modals, tables, and spinners).
 * @dependencies public/css/base.css
 */

/* Buttons components */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3) var(--space-4); /* Adjusted padding for better tap target */
  font-family: inherit;
  font-size: var(--font-size-sm);
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  border: 1px solid transparent;
  border-radius: var(--radius-md); /* Using design token */
  cursor: pointer;
  transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, color 0.15s ease-in-out;
  min-height: 44px; /* Ensured mobile tap target size match */
}

.btn-primary {
  background-color: var(--color-accent); /* Using design token */
  /* navy-on-saffron = 6.84:1 (AA). White was only 2.75:1 in light mode (fail). */
  color: var(--color-on-accent);
  border-color: var(--color-accent); /* Match background for solid look */
}
.btn-primary:hover {
  filter: brightness(1.08); /* Subtle brightness change on hover */
  text-decoration: none;
}

.btn-secondary {
  background-color: transparent;
  border: 1.5px solid var(--color-border); /* Using design token */
  color: var(--color-text); /* Using design token */
}
.btn-secondary:hover {
  background-color: var(--color-primary-light); /* Light hover background */
  color: var(--color-primary); /* Darker text on hover */
  text-decoration: none;
}

.btn-danger {
  background-color: var(--color-danger); /* Using design token */
  color: var(--color-on-danger); /* white in both themes — 4.83:1 (AA) */
  border-color: var(--color-danger);
}
.btn-danger:hover {
  filter: brightness(1.08);
  text-decoration: none;
}

.btn-block {
  display: flex;
  width: 100%;
}

.btn-sm {
  padding: var(--space-1) var(--space-2);
  font-size: var(--text-sm); /* Using design token */
  min-height: 32px;
}

/* Row action buttons (edit / deactivate / erase) — a shared min-width so the
   actions column lines up across table rows regardless of label length (P3-4). */
.btn-action {
  min-width: 96px;
  justify-content: center;
  text-align: center;
  white-space: nowrap;
}
/* When several row actions stack/inline, keep consistent spacing. */
.row-actions {
  display: inline-flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}

.btn-ghost {
  background: transparent;
  color: var(--color-text-muted); /* Using design token */
  border-color: transparent;
}

.btn-google {
  background-color: #ffffff;
  color: #757575;
  border-color: #cccccc;
}
.btn-google:hover {
  background-color: #f5f5f5;
  color: #333333;
}

/* Form Layouts */
.form-group {
  margin-bottom: var(--space-4); /* Using new spacing variable */
}

.form-group label {
  display: block;
  font-size: var(--text-sm); /* Using design token */
  font-weight: 600;
  margin-bottom: var(--space-1); /* Using new spacing variable */
  color: var(--color-text); /* Using design token */
}

.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"],
.form-group input[type="number"],
.form-group select,
.form-group textarea {
  width: 100%;
  padding: var(--space-2); /* Using new spacing variable */
  font-family: inherit;
  font-size: var(--text-base); /* Using design token */
  border: 1px solid var(--color-border); /* Using design token */
  border-radius: var(--radius-sm); /* Using design token */
  background-color: var(--color-surface); /* Using design token */
  transition: border-color 0.15s ease-in-out;
  min-height: 44px;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--color-accent); /* Using design token */
}

/* Keyboard focus still gets a visible ring (the :focus rule above strips the
   outline for mouse focus; this restores it for keyboard/AT users). Declared
   after so it wins at equal specificity. */
.form-group input:focus-visible,
.form-group select:focus-visible,
.form-group textarea:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.form-group textarea {
  min-height: 100px;
  resize: vertical;
}

.checkbox-consent-container {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2); /* Using new spacing variable */
}

.checkbox-consent-container input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin-top: 2px;
  flex-shrink: 0;
  cursor: pointer;
}

.checkbox-consent-container label {
  font-weight: normal;
  font-size: var(--text-sm); /* Using design token */
  line-height: 1.4;
  cursor: pointer;
}

/* Data Tables */
.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: var(--space-4); /* Using new spacing variable */
  background-color: var(--color-surface); /* Using design token */
}

.data-table th,
.data-table td {
  padding: var(--space-2) var(--space-4); /* Using new spacing variables */
  border: 1px solid var(--color-border); /* Using design token */
  text-align: left;
}

.data-table th {
  background-color: var(--color-primary-light); /* Using design token */
  color: var(--color-primary); /* Using design token */
  font-weight: 600;
}

/* Modals Backdrops & Overlays*/
.modal-backdrop,
.modal-overlay{
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.65);
  z-index: 3000;
  align-items: center;
  justify-content: center; /* Keep as is */
  padding: var(--space-4); /* Using new spacing variable */
  backdrop-filter: blur(4px);
}

.modal-backdrop.active,
.modal-overlay.active {
  display: flex;
}

.modal-card {
  background-color: var(--color-surface); /* Using design token */
  border-radius: var(--radius-lg); /* Using design token */
  width: 100%;
  max-width: 550px;
  padding: var(--space-8);
  box-shadow: var(--shadow-lg);
  position: relative;
  animation: modal-enter 0.25s ease-out;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-6);
  border-bottom: 1px solid var(--color-border);
  padding-bottom: var(--space-4);
}

.modal-header h3 {
  margin: 0;
  font-family: var(--font-display);
  color: var(--color-primary);
}

.modal-close-btn {
  background: none;
  border: none;
  font-size: 1.75rem;
  color: var(--color-text-subtle);
  cursor: pointer;
  line-height: 1;
}

.modal-body {
  margin-bottom: var(--space-8);
  color: var(--color-text);
}

.modal-actions-row,
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-4);
  margin-top: var(--space-6);
}

@keyframes modal-enter {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

/* Pure CSS Spinners */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(0, 0, 0, 0.1);
  border-top-color: var(--color-accent); /* Using design token */
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Static Ad Placement Blocks */
.ad-container-wrapper {
  margin: var(--space-4) auto; /* Using new spacing variable */
  text-align: center;
}

.ad-slot-container {
  display: inline-block;
  background-color: #eeeeee;
  border: 1px solid var(--color-border); /* Using design token */
  padding: var(--space-1); /* Using new spacing variable */
  position: relative;
}

.ad-slot-container::before {
  content: "ADVERTISEMENT";
  display: block;
  font-size: 10px;
  letter-spacing: 1px;
  color: var(--color-text-muted); /* Using design token */
  margin-bottom: var(--space-1); /* Using new spacing variable */
}

/* ── Leaderboard & Gamification ─────────────────────────────────────────────── */

/* Highlight for current user row.
   No !important needed: in light mode no higher-specificity rule sets a row
   background (there is no light-mode `tbody tr:hover`), and when a row is both
   highlighted and a medal, the medal rule wins by source order — same outcome
   as before. */
.highlight-row {
  background-color: var(--color-accent-light);
}

/* ── Rank medal rows ──────────────────────────────────────────────────────────
   WHY: StackExchange and SoloLearn both use visual rank badges for top users.
   Gold/silver/bronze backgrounds with a subtle glow animation for position 1
   create a rewarding, premium feel. */

/* Gold — Position 1 */
.rank-gold {
  background: linear-gradient(135deg, var(--medal-gold-bg) 0%, #FEF9C3 100%);
  border-left: 4px solid var(--medal-gold-border);
  animation: medal-glow-gold 2s ease-in-out infinite;
}

.rank-gold .rank-badge {
  background: var(--medal-gold-border);
  color: #FEF3C7;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--text-sm);
  box-shadow: 0 0 8px var(--medal-gold-glow);
}

/* Silver — Position 2 */
.rank-silver {
  background: linear-gradient(135deg, var(--medal-silver-bg) 0%, #E2E8F0 100%);
  border-left: 4px solid var(--medal-silver-border);
  animation: medal-glow-silver 2.5s ease-in-out infinite;
}

.rank-silver .rank-badge {
  background: var(--medal-silver-border);
  color: #F1F5F9;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--text-sm);
  box-shadow: 0 0 8px var(--medal-silver-glow);
}

/* Bronze — Position 3 */
.rank-bronze {
  background: linear-gradient(135deg, var(--medal-bronze-bg) 0%, #FED7AA 100%);
  border-left: 4px solid var(--medal-bronze-border);
  animation: medal-glow-bronze 3s ease-in-out infinite;
}

.rank-bronze .rank-badge {
  background: var(--medal-bronze-border);
  color: #FFF7ED;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--text-sm);
  box-shadow: 0 0 8px var(--medal-bronze-glow);
}

/* ── Glow animations ──────────────────────────────────────────────────────────
   WHY: Subtle pulsing glow draws attention to top performers without being
   distracting.  Each medal tier has a slightly different timing for variety. */

@keyframes medal-glow-gold {
  0%, 100% { box-shadow: 0 0 0 rgba(217, 119, 6, 0); }
  50% { box-shadow: 0 4px 20px rgba(217, 119, 6, 0.2); }
}

@keyframes medal-glow-silver {
  0%, 100% { box-shadow: 0 0 0 rgba(148, 163, 184, 0); }
  50% { box-shadow: 0 4px 16px rgba(148, 163, 184, 0.2); }
}

@keyframes medal-glow-bronze {
  0%, 100% { box-shadow: 0 0 0 rgba(194, 65, 12, 0); }
  50% { box-shadow: 0 4px 16px rgba(194, 65, 12, 0.15); }
}

/* ── Reduced motion ───────────────────────────────────────────────────────────
   WHY: The pulsing medal glow is decorative and can trigger discomfort for
   users with vestibular sensitivities. Honour prefers-reduced-motion by
   stopping the glow loop (the medal colour/border still conveys rank). */
@media (prefers-reduced-motion: reduce) {
  .rank-gold,
  .rank-silver,
  .rank-bronze {
    animation: none;
  }
}

/* ── League badges (compact pill) ───────────────────────────────────────────── */
.badge-league-gold {
  background-color: var(--medal-gold-border);
  color: #FEF3C7;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 600;
}

.badge-league-silver {
  background-color: var(--medal-silver-border);
  color: #F1F5F9;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 600;
}

.badge-league-bronze {
  background-color: var(--medal-bronze-border);
  color: #FFF7ED;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 600;
}

/* ── Dark mode medal overrides ────────────────────────────────────────────────
   These keep !important deliberately: dark mode DOES define
   `html.dark-mode .data-table tbody tr:hover` (base.css, specificity 0,3,2),
   which would otherwise override the medal background on hover. !important is
   the least-invasive way to win here without scoping the medal classes to
   `.data-table` (which would strip the medal look off profile `.rank-card`s
   that reuse these same classes). Do not remove. */
html.dark-mode .rank-gold {
  background: linear-gradient(135deg, rgba(217, 119, 6, 0.15) 0%, rgba(254, 243, 199, 0.08) 100%) !important;
  border-left-color: var(--medal-gold-border) !important;
}

html.dark-mode .rank-silver {
  background: linear-gradient(135deg, rgba(148, 163, 184, 0.12) 0%, rgba(226, 232, 240, 0.06) 100%) !important;
  border-left-color: var(--medal-silver-border) !important;
}

html.dark-mode .rank-bronze {
  background: linear-gradient(135deg, rgba(194, 65, 12, 0.12) 0%, rgba(254, 215, 170, 0.06) 100%) !important;
  border-left-color: var(--medal-bronze-border) !important;
}

.bookmarked {
  color: var(--color-accent) !important;
  font-size: 1.2em;
}

/* CDC Cognitive-Level Badges */
.badge-level-1 { background: #EFF6FF; color: #1D4ED8; border: 1px solid #BFDBFE; padding: 2px 8px; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }
.badge-level-2 { background: #F0FDF4; color: #16A34A; border: 1px solid #BBF7D0; padding: 2px 8px; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }
.badge-level-3 { background: #FFF7ED; color: #C4671A; border: 1px solid #FED7AA; padding: 2px 8px; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }
.badge-level-4 { background: #FEF2F2; color: #DC2626; border: 1px solid #FECACA; padding: 2px 8px; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }

/* ── Type / status badges (authoritative source) ──────────────────────────────
   WHY: Previously duplicated in base.css and layout.css with conflicting
   values. Consolidated here so every consumer renders identically. All values
   reference design tokens; dark-mode overrides live in base.css. */
.badge-type    { background-color: var(--color-primary-light); color: var(--color-primary-mid); }
.badge-free    { background-color: var(--color-success-light); color: var(--color-success); }
.badge-premium { background-color: var(--color-accent-light);  color: var(--color-accent-dark); }
.badge-live    { background-color: var(--color-danger-light);  color: var(--color-danger); }
.badge-muted   { background-color: var(--color-surface-alt);   color: var(--color-muted); }

/* ── Status badges (pill) — admin/teacher tables & dashboards ──────────────────
   Moved out of inline <style> blocks in admin/ads.ejs (.ad-badge),
   admin/seasons.ejs (.sn-badge), and teacher/questions.ejs (.tq-badge).
   Token-based with dark-mode overrides so they theme correctly everywhere. */
.ad-badge,
.sn-badge,
.tq-badge {
  display: inline-block;
  font-size: 10px;
  padding: 3px 10px;
  border-radius: var(--radius-full);
  text-transform: uppercase;
  font-weight: 700;
}
.tq-badge { padding: 2px 8px; }

/* Positive / active */
.ad-badge.on,
.sn-badge.active,
.tq-badge.published { background: var(--color-success-light); color: var(--color-success-strong); }

/* Informational / upcoming / in-review */
.sn-badge.upcoming,
.tq-badge.review { background: var(--color-info-light); color: var(--color-info-strong); }

/* Neutral / completed / draft */
.sn-badge.completed,
.tq-badge.draft { background: var(--color-surface-alt); color: var(--color-text-muted); }

/* Negative / off / rejected */
.ad-badge.off,
.tq-badge.rejected,
.tq-badge.flagged { background: var(--color-danger-light); color: var(--color-danger-strong); }

/* ── Landing Page ─────────────────────────────────────────────────────────── */
.landing-section {
  padding: var(--space-16) 0;
}

.landing-section--surface {
  background: var(--color-surface);
}

.landing-section--alt {
  background: var(--color-bg);
}

.landing-btn-group {
  display: flex;
  gap: var(--space-4);
  justify-content: center;
}

.landing-grid-gap {
  margin-top: var(--space-12);
}

.landing-step-card {
  padding: var(--space-6);
}

/* ═══════════════════════════════════════════════════════════════════════════
   BREADCRUMBS — shared "you are here" trail (partials/breadcrumbs.ejs)
   Sits at the top of every app page, aligned to the 1200px content container.
   ═══════════════════════════════════════════════════════════════════════════ */
.breadcrumbs {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-3) var(--space-4) 0;
}

.breadcrumbs-list {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1);
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: var(--text-sm);
}

.breadcrumbs-item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  min-width: 0; /* allow long labels to truncate rather than overflow */
}

.breadcrumbs-link {
  display: inline-block;
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px var(--space-1);
  border-radius: var(--radius-xs);
  color: var(--color-text-muted);
  text-decoration: none;
  transition: color 0.15s ease, background-color 0.15s ease;
}
.breadcrumbs-link:hover {
  color: var(--color-accent);
  background-color: var(--color-accent-light);
  text-decoration: none;
}
.breadcrumbs-link:active {
  color: var(--color-accent-dark);
}
.breadcrumbs-link:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.breadcrumbs-current {
  display: inline-block;
  max-width: 16rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px var(--space-1);
  color: var(--color-text);
  font-weight: 600;
}

/* Grouping level with no standalone page (e.g. "Admin") — looks like a muted
   label, not a link, so users don't click into a 404. */
.breadcrumbs-text {
  display: inline-block;
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px var(--space-1);
  color: var(--color-text-muted);
}
html.dark-mode .breadcrumbs-text { color: var(--color-dark-text-muted); }

.breadcrumbs-sep {
  flex-shrink: 0;
  color: var(--color-text-subtle);
}

html.dark-mode .breadcrumbs-link { color: var(--color-dark-text-muted); }
html.dark-mode .breadcrumbs-current { color: var(--color-dark-text); }

/* Responsive collapse: on narrow screens a long trail wastes the whole row, so
   show only the immediate parent + current page. The parent gets a leading "‹"
   so it still reads as "back one level". Achieved with CSS only — no JS. */
@media (max-width: 640px) {
  .breadcrumbs-item { display: none; }
  /* current page */
  .breadcrumbs-item:last-child { display: inline-flex; }
  /* immediate parent */
  .breadcrumbs-item:nth-last-child(2) { display: inline-flex; }
  .breadcrumbs-item:nth-last-child(2) .breadcrumbs-link::before,
  .breadcrumbs-item:nth-last-child(2) .breadcrumbs-text::before {
    content: "‹ ";
    color: var(--color-text-subtle);
  }
  .breadcrumbs-item:nth-last-child(2) .breadcrumbs-sep { display: none; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   PAGE HEADER — canonical cross-app page title block. Gives non-admin views the
   same structured header the admin shell already has, instead of bespoke inline
   <style> blocks, so every page opens the same way.
   ═══════════════════════════════════════════════════════════════════════════ */
.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}
.page-header__eyebrow {
  display: block;
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-accent-dark);
  margin-bottom: var(--space-1);
}
.page-header__heading {
  font-family: var(--font-display);
  font-size: var(--text-3xl);
  line-height: 1.2;
  color: var(--color-primary);
  margin: 0;
}
.page-header__subtitle {
  margin: var(--space-2) 0 0;
  color: var(--color-text-muted);
  max-width: 60ch;
}
.page-header__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* ═══════════════════════════════════════════════════════════════════════════
   BUTTON INTERACTION STATES — the base .btn had hover only. Add the full set so
   every button gives consistent press / keyboard-focus / disabled feedback.
   ═══════════════════════════════════════════════════════════════════════════ */
.btn:active {
  transform: translateY(1px);
}
.btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}
.btn:disabled,
.btn[disabled],
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
  filter: none;
  transform: none;
  pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
  .btn:active { transform: none; }
}

/* Inline spinner for the busy/loading state (MCQplexUI.setLoading). */
.btn-spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  margin-right: var(--space-2);
  vertical-align: -0.125em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: btn-spin 0.6s linear infinite;
}
@keyframes btn-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .btn-spinner { animation-duration: 1.5s; }
}