/* منظّمي — صفحة الإنجازات والرتب والاحتفالات (js/views-achievements.js). مأخوذ من النموذج المعتمد design-reference/achievements.
   الحركة transform/opacity/stroke-dashoffset فقط، كل حركة ≤ 480ms (الانتقال بين الرتب تسلسل حركات قصيرة بتأخيرات)،
   لا حركة لا نهائية، وprefers-reduced-motion يلغيها. الألوان والحواف والظلال من متغيرات base.css:
   الدرجات الثلاث تعيد استخدام ألوان قائمة فتتبع الوضع الليلي ولون التمييز الذي يختاره الطالب. */

:root {
  --tier-1: var(--text-muted);
  --tier-2: var(--accent-primary);
  --tier-3: var(--semantic-warning);
  --ach-burst-dur: var(--dur-3);
}

/* ---------- ترويسة الرتبة ---------- */
.ach-page { display: flex; flex-direction: column; gap: 14px; }
.ach-hero {
  position: relative;
  overflow: hidden;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-4);
  border-radius: var(--radius-lg);
  background-color: var(--accent-primary);
  color: var(--on-accent);
}
.ach-hero-arcs { position: absolute; inset-block: 0; inset-inline-start: -40px; width: 220px; height: 100%; opacity: 0.9; pointer-events: none; }
.ach-hero-arcs circle { fill: none; stroke: var(--on-color-soft); stroke-width: 1.5; }
.ach-spark { position: absolute; width: 10px; height: 10px; color: var(--on-color); animation: ach-twinkle var(--dur-3) var(--ease-out) both; animation-delay: var(--d, 0ms); }
.ach-medal-wrap { position: relative; width: 104px; height: 104px; z-index: 1; }
.ach-medal-wrap > svg.ach-xp-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg) scaleY(-1); }
.ach-xp-track { fill: none; stroke: var(--on-color-track); stroke-width: 4; }
.ach-xp-fill { fill: none; stroke: var(--on-color); stroke-width: 4; stroke-linecap: round; animation: ach-draw var(--dur-3) var(--ease-out) 120ms both; }
.ach-medal-wrap .ach-medal { position: absolute; inset: 14px; width: calc(100% - 28px); height: calc(100% - 28px); animation: ach-pop var(--dur-3) var(--ease-spring) both; }
.ach-hero-text { position: relative; z-index: 1; min-width: 0; }
.ach-kicker { margin: 0; font-size: var(--fs-small); opacity: 0.85; }
.ach-rank-name { margin: 2px 0 var(--space-2); font-size: var(--fs-hero); font-weight: 700; line-height: 1.1; animation: ach-rise var(--dur-3) var(--ease-out) 80ms both; }
.ach-hero-sub { margin: 0 0 var(--space-3); font-size: var(--fs-small); opacity: 0.92; }
.ach-hero-bar { height: 8px; border-radius: var(--radius-pill); background-color: var(--on-color-track); overflow: hidden; }
.ach-hero-bar > i { display: block; height: 100%; width: 100%; border-radius: var(--radius-pill); background-color: var(--on-color); transform-origin: right center; transform: scaleX(var(--p, 0)); animation: ach-fill var(--dur-3) var(--ease-out) 160ms both; }

.ach-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.ach-stat { display: flex; flex-direction: column; gap: 2px; padding: var(--space-3); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); background-color: var(--bg-surface-1); animation: ach-rise var(--dur-2) var(--ease-out) both; animation-delay: var(--d, 0ms); }
.ach-stat b { font-size: var(--fs-stat-lg); font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.ach-stat span { font-size: var(--fs-caption); color: var(--text-muted); }

/* ---------- الشارة (دائرة بحلقة تقدّم وأيقونة) ---------- */
.ach-badge { position: relative; flex: none; width: 48px; height: 48px; color: var(--tier-color, var(--text-muted)); display: flex; align-items: center; justify-content: center; overflow: hidden; border-radius: var(--radius-pill); }
.ach-badge > svg.ach-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg) scaleY(-1); }
.ach-ring-track { fill: none; stroke: var(--border-subtle); stroke-width: 3; }
.ach-ring-fill { fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; animation: ach-draw var(--dur-3) var(--ease-out) both; animation-delay: var(--d, 0ms); }
.ach-badge > svg.ach-glyph { position: relative; width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.ach-row.is-unlocked .ach-badge { background-color: color-mix(in srgb, var(--tier-color) 14%, var(--bg-surface-1)); }
.ach-row.is-progress .ach-badge { color: var(--text-secondary); }
.ach-row.is-progress .ach-ring-fill { stroke: var(--tier-color, var(--accent-primary)); }
.ach-row.is-sealed .ach-badge { color: var(--text-muted); background-color: var(--muted-bg); }
.ach-row.is-sealed .ach-ring-track { stroke: var(--border-strong); stroke-dasharray: 3 5; }
.ach-seal-mark { position: relative; font-size: var(--fs-title); font-weight: 700; }
.t1 { --tier-color: var(--tier-1); } .t2 { --tier-color: var(--tier-2); } .t3 { --tier-color: var(--tier-3); }
.ach-pips { display: inline-flex; gap: 3px; margin-inline-start: var(--space-2); vertical-align: middle; }
.ach-pips i { width: 6px; height: 6px; border-radius: var(--radius-pill); background-color: var(--border-strong); }
.ach-pips i.on { background-color: var(--tier-color); }
.ach-new-tag { display: inline-block; margin-inline-start: var(--space-2); padding: 1px var(--space-2); border-radius: var(--radius-xs); background-color: var(--accent-primary); color: var(--on-accent); font-size: var(--fs-micro); font-weight: 600; vertical-align: middle; }
.ach-row { animation: ach-rise var(--dur-2) var(--ease-out) both; animation-delay: var(--d, 0ms); }
.ach-row:active { transform: scale(var(--press-scale)); }
.ach-side { flex: none; min-width: 52px; text-align: end; font-size: var(--fs-small); color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.ach-side .ach-check { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; color: var(--semantic-success); }
.ach-bar { margin-top: var(--space-2); height: 5px; border-radius: var(--radius-pill); background-color: var(--muted-bg); overflow: hidden; }
.ach-bar > i { display: block; height: 100%; width: 100%; border-radius: var(--radius-pill); background-color: var(--tier-color, var(--accent-primary)); transform-origin: right center; transform: scaleX(var(--p, 0)); animation: ach-fill var(--dur-3) var(--ease-out) both; animation-delay: var(--d, 0ms); }

/* لمعة تمرّ مرة واحدة على الشارات المفتوحة حديثاً */
.ach-shine { position: relative; overflow: hidden; }
.ach-shine::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(105deg, transparent 35%, var(--shine, var(--on-color-soft)) 50%, transparent 65%);
  transform: translateX(-130%); animation: ach-shine var(--dur-3) var(--ease-out) both; animation-delay: var(--shine-delay, 420ms);
}
.ach-row.is-new .ach-badge { --shine: color-mix(in srgb, var(--bg-surface-1) 75%, transparent); }

/* ---------- العادات ---------- */
.ach-map { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.ach-cell { display: flex; flex-direction: column; align-items: center; gap: var(--space-1); padding: var(--space-3) var(--space-2); border-radius: var(--radius-md); border: 1px solid var(--border-subtle); color: var(--text-muted); font-size: var(--fs-caption); text-align: center; animation: ach-rise var(--dur-2) var(--ease-out) both; animation-delay: var(--d, 0ms); }
.ach-cell svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.ach-cell.is-lit { color: var(--accent-primary); background-color: var(--accent-subtle-bg); border-color: color-mix(in srgb, var(--accent-primary) 35%, var(--border-subtle)); }
.ach-cell.is-lit b { color: var(--text-primary); font-weight: 600; }
.ach-strip { display: grid; grid-template-columns: repeat(14, minmax(0, 1fr)); gap: 3px; margin-top: var(--space-2); }
.ach-strip i { aspect-ratio: 1; border-radius: var(--radius-xs); background-color: var(--muted-bg); animation: ach-pop var(--dur-2) var(--ease-out) both; animation-delay: var(--d, 0ms); }
.ach-strip i.on { background-color: var(--tier-color, var(--accent-primary)); }
.ach-steps { display: flex; align-items: center; margin-top: var(--space-3); }
.ach-step { flex: none; width: 22px; height: 22px; border-radius: var(--radius-pill); border: 2px solid var(--border-strong); display: flex; align-items: center; justify-content: center; color: var(--on-accent); }
.ach-step svg { width: 12px; height: 12px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.ach-step.on { border-color: var(--tier-color); background-color: var(--tier-color); }
.ach-step-line { flex: 1; height: 2px; background-color: var(--border-strong); }
.ach-step-line.on { background-color: var(--tier-color); }
.ach-step-labels { display: flex; justify-content: space-between; margin-top: var(--space-1); font-size: var(--fs-micro); color: var(--text-muted); }

/* ---------- لوح فتح إنجاز / ارتقاء رتبة ---------- */
.ach-sheet-body { display: flex; flex-direction: column; align-items: center; text-align: center; padding-block: var(--space-2) var(--space-2); }
.ach-stage { position: relative; width: 100%; height: 190px; display: flex; align-items: center; justify-content: center; }
.ach-stage .ach-medal { position: relative; width: 120px; height: 120px; animation: ach-pop var(--dur-3) var(--ease-spring) 60ms both; }
.ach-stage.is-big .ach-medal { width: 136px; height: 136px; }
.ach-pulse { position: absolute; width: 120px; height: 120px; border-radius: var(--radius-pill); border: 2px solid var(--tier-color, var(--accent-primary)); opacity: 0; animation: ach-pulse var(--dur-3) var(--ease-out) both; animation-delay: var(--d, 120ms); }
.ach-particle { position: absolute; inset-block-start: 50%; inset-inline-start: 50%; width: 8px; height: 8px; margin: -4px; opacity: 0; background-color: var(--c, var(--accent-primary)); border-radius: var(--rad, var(--radius-xs)); animation: ach-burst var(--ach-burst-dur) var(--ease-out) both; animation-delay: var(--d, 140ms); }
.ach-old { position: absolute; width: 120px; height: 120px; animation: ach-leave var(--dur-2) var(--ease-out) both; }
.ach-flash { position: fixed; inset: 0; z-index: 310; pointer-events: none; background-color: color-mix(in srgb, var(--accent-primary) 22%, transparent); opacity: 0; animation: ach-flash var(--dur-2) var(--ease-out) both; }
.ach-sheet-body .ach-kicker { color: var(--accent-primary); font-weight: 600; opacity: 1; animation: ach-rise var(--dur-2) var(--ease-out) 260ms both; }
.ach-unlock-title { margin: var(--space-1) 0 var(--space-2); font-size: var(--fs-headline); font-weight: 700; animation: ach-rise var(--dur-2) var(--ease-out) 320ms both; }
.ach-unlock-desc { margin: 0 0 var(--space-3); color: var(--text-secondary); font-size: var(--fs-callout); animation: ach-rise var(--dur-2) var(--ease-out) 380ms both; }
.ach-xp-chip { display: inline-flex; align-items: center; gap: var(--space-1); padding: var(--space-1) var(--space-3); border-radius: var(--radius-pill); background-color: var(--accent-subtle-bg); color: var(--accent-primary); font-weight: 700; font-variant-numeric: tabular-nums; animation: ach-rise var(--dur-2) var(--ease-spring) 440ms both; }
.ach-rankline { display: flex; align-items: center; gap: var(--space-3); margin-bottom: var(--space-3); font-weight: 600; animation: ach-rise var(--dur-2) var(--ease-out) 380ms both; }
.ach-rankline span:first-child { color: var(--text-muted); }
.ach-rankline svg { width: 18px; height: 18px; fill: none; stroke: var(--accent-primary); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.ach-sheet-actions { display: flex; flex-direction: column; gap: var(--space-2); width: 100%; margin-top: var(--space-4); }

/* ---------- إشعار صغير ---------- */
.ach-toast { position: fixed; inset-inline: var(--space-4); inset-block-start: calc(var(--space-4) + env(safe-area-inset-top)); z-index: 320; display: flex; align-items: center; gap: var(--space-3); max-width: 420px; margin-inline: auto; padding: var(--space-3) var(--space-4); border-radius: var(--radius-lg); border: 1px solid var(--border-subtle); background-color: var(--bg-surface-1); box-shadow: var(--shadow-float); animation: ach-toast-in var(--dur-2) var(--ease-spring) both; }
.ach-toast .ach-badge { width: 40px; height: 40px; }
.ach-toast-text { flex: 1; min-width: 0; }
.ach-toast-text b { display: block; font-size: var(--fs-body); }
.ach-toast-text span { font-size: var(--fs-small); color: var(--text-secondary); }

/* ---------- سطر الرتبة في «أسبوعك» ---------- */
.ach-week-row { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) var(--space-4); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); background-color: var(--bg-surface-1); color: inherit; text-decoration: none; }
.ach-week-row .ach-medal { width: 44px; height: 44px; flex: none; }
.ach-week-row .ach-bar { margin-top: var(--space-1); }
.ach-week-row .row-chevron svg { width: 18px; height: 18px; fill: none; stroke: var(--text-muted); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- الميدالية (أيقونة SVG خطية) ---------- */
.ach-medal .m-hex { fill: var(--medal-fill, var(--on-color-soft)); stroke: var(--medal-line, var(--on-color)); stroke-width: 3; stroke-linejoin: round; stroke-dasharray: 300; animation: ach-draw-m var(--dur-3) var(--ease-out) both; }
.ach-medal .m-inner { fill: none; stroke: var(--medal-line, var(--on-color)); stroke-width: 1.5; opacity: 0.55; stroke-linejoin: round; }
.ach-medal .m-glyph { fill: none; stroke: var(--medal-line, var(--on-color)); stroke-width: 3.2; stroke-linecap: round; stroke-linejoin: round; }
.ach-medal .m-star { fill: var(--medal-line, var(--on-color)); stroke: none; }
.ach-medal.is-solid { --medal-fill: var(--accent-primary); --medal-line: var(--on-accent); filter: none; }
.ach-medal.is-tier { --medal-fill: color-mix(in srgb, var(--tier-color) 16%, var(--bg-surface-1)); --medal-line: var(--tier-color); }

/* ---------- الحركة (transform/opacity/stroke-dashoffset فقط) ---------- */
@keyframes ach-draw { from { stroke-dashoffset: var(--len, 300); } }
@keyframes ach-draw-m { from { stroke-dashoffset: 300; } to { stroke-dashoffset: 0; } }
@keyframes ach-fill { from { transform: scaleX(0); } }
@keyframes ach-rise { from { opacity: 0; transform: translateY(10px); } }
@keyframes ach-pop { 0% { opacity: 0; transform: scale(0.4); } 100% { opacity: 1; transform: scale(1); } }
@keyframes ach-twinkle { 0% { opacity: 0; transform: scale(0.2); } 60% { opacity: 1; transform: scale(1.25); } 100% { opacity: 0.9; transform: scale(1); } }
@keyframes ach-pulse { 0% { opacity: 0.55; transform: scale(0.9); } 100% { opacity: 0; transform: scale(2); } }
@keyframes ach-burst { 0% { opacity: 0; transform: translate(0, 0) scale(0.4) rotate(0deg); } 18% { opacity: 1; } 100% { opacity: 0; transform: translate(var(--tx), var(--ty)) scale(1) rotate(var(--r)); } }
@keyframes ach-shine { to { transform: translateX(130%); } }
@keyframes ach-leave { to { opacity: 0; transform: scale(0.7); } }
@keyframes ach-flash { 0% { opacity: 0; } 35% { opacity: 1; } 100% { opacity: 0; } }
@keyframes ach-toast-in { from { opacity: 0; transform: translateY(-140%); } }
@keyframes ach-flip { 0% { transform: rotateY(0deg); } 100% { transform: rotateY(180deg); } }
.ach-flip-card { position: relative; transform-style: preserve-3d; }
.ach-flip-card.is-flipping { animation: ach-flip var(--dur-3) var(--ease-out) both; }
.ach-flip-card .face { backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.ach-flip-card .face.back { position: absolute; inset: 0; transform: rotateY(180deg); }

@media (prefers-reduced-motion: reduce) {
  .ach-page *, .ach-sheet-body *, .ach-toast, .ach-flash { animation: none !important; }
  .ach-flash, .ach-pulse, .ach-particle { display: none; }
}


/* ---------- الانتقال بين الرتب: تسلسل (شحن ← تبديل ← تشكيل الميدالية الجديدة ← احتفال) ---------- */
/* توقيتا التسلسل على الغلاف كله فيصلان إلى الاسم والنص والأزرار لا إلى المسرح وحده */
.is-rank { --forge-at: 900ms; --boom-at: 1260ms; }
.rk-ring { position: absolute; width: 168px; height: 168px; transform: rotate(-90deg) scaleY(-1); animation: rk-ring-out var(--dur-2) var(--ease-out) 780ms both; }
.rk-track { fill: none; stroke: var(--border-subtle); stroke-width: 4; }
.rk-fill { fill: none; stroke: var(--accent-primary); stroke-width: 4; stroke-linecap: round; animation: rk-ring-fill var(--dur-3) var(--ease-out) 80ms both; }
.rk-old { position: absolute; width: 120px; height: 120px; animation: rk-old-in var(--dur-2) var(--ease-out) both, rk-shake var(--dur-2) linear 480ms both, ach-leave var(--dur-2) var(--ease-out) 800ms both; }
.rk-old .ach-medal { width: 100%; height: 100%; animation: none; }
.rk-spark { position: absolute; inset-block-start: 50%; inset-inline-start: 50%; width: 6px; height: 6px; margin: -3px; border-radius: var(--radius-pill); background-color: var(--accent-primary); opacity: 0; animation: rk-converge var(--dur-3) var(--ease-out) both; animation-delay: calc(360ms + var(--i) * 30ms); }
.rk-new { position: absolute; width: 136px; height: 136px; }
.rk-new .ach-medal { width: 100%; height: 100%; animation: none; }
.ach-medal .m-fill { display: none; }
.is-forge .m-fill { display: block; fill: var(--medal-fill, var(--on-color-soft)); stroke: none; animation: rk-fade var(--dur-2) var(--ease-out) calc(var(--forge-at) + 320ms) both; }
.is-forge .m-hex { fill: none; animation: ach-draw-m var(--dur-3) var(--ease-out) var(--forge-at) both; }
.is-forge .m-inner { animation: rk-fade var(--dur-2) var(--ease-out) calc(var(--forge-at) + 360ms) both; }
.is-forge .m-glyph { stroke-dasharray: 34; animation: rk-draw-g var(--dur-2) var(--ease-out) both; animation-delay: calc(var(--forge-at) + 440ms + var(--i, 0) * 110ms); }
.is-forge .m-star { transform-box: fill-box; transform-origin: center; animation: ach-pop var(--dur-2) var(--ease-spring) both; animation-delay: calc(var(--forge-at) + 720ms + var(--i, 0) * 90ms); }
.rk-new .ach-shine::after, .rk-new.ach-shine::after { animation-delay: calc(var(--forge-at) + 900ms); }
.rk-names { position: relative; height: 2.6rem; width: 100%; margin-bottom: var(--space-2); }
.rk-names span { position: absolute; inset-inline: 0; top: 0; font-size: var(--fs-headline); font-weight: 700; line-height: 2.6rem; }
.rk-old-name { color: var(--text-muted); animation: rk-name-in var(--dur-2) var(--ease-out) 280ms both, rk-name-out var(--dur-2) var(--ease-out) var(--boom-at) both; }
.rk-new-name { color: var(--accent-primary); animation: rk-name-in var(--dur-2) var(--ease-spring) calc(var(--boom-at) + 60ms) both; }
.rk-late { animation: ach-rise var(--dur-2) var(--ease-out) calc(var(--boom-at) + 360ms) both; }
.is-rank .ach-more, .is-rank .ach-unlock-desc { animation: ach-rise var(--dur-2) var(--ease-out) calc(var(--boom-at) + 360ms) both; }
.rk-late-2 { animation: ach-rise var(--dur-2) var(--ease-spring) calc(var(--boom-at) + 460ms) both; }
.rk-stage .ach-pulse, .rk-stage .ach-particle { animation-delay: calc(var(--boom-at) + var(--d, 0ms)); }
.ach-flash.is-late { animation-delay: var(--boom-at, 1260ms); }
.ach-face-sealed { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: var(--fs-hero); font-weight: 700; color: var(--text-muted); }
.ach-reveal-hex { position: relative; width: 120px; height: 120px; transform-style: preserve-3d; animation: ach-flip var(--dur-3) var(--ease-out) 260ms both; }
.ach-reveal-hex .face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.ach-reveal-hex .face.back { transform: rotateY(180deg); }
.ach-reveal-hex .ach-medal { width: 100%; height: 100%; animation: none; }
.ach-reveal-hex .ach-seal-hex { fill: var(--muted-bg); stroke: var(--border-strong); stroke-width: 3; stroke-dasharray: 6 7; stroke-linejoin: round; }
.ach-stage.is-reveal .ach-particle { animation-delay: calc(640ms + var(--d, 0ms)); }
.ach-more { margin: 0 0 var(--space-3); font-size: var(--fs-small); color: var(--text-muted); animation: ach-rise var(--dur-2) var(--ease-out) 420ms both; }
.ach-off-row { margin-top: var(--space-2); }

@keyframes rk-ring-fill { from { stroke-dashoffset: var(--from, 440); } to { stroke-dashoffset: 0; } }
@keyframes rk-ring-out { to { opacity: 0; } }
@keyframes rk-old-in { from { opacity: 0; transform: scale(0.8); } }
@keyframes rk-shake { 0% { transform: rotate(0deg); } 20% { transform: rotate(-4deg); } 40% { transform: rotate(4deg); } 60% { transform: rotate(-3deg); } 80% { transform: rotate(3deg); } 100% { transform: rotate(0deg); } }
@keyframes rk-converge { 0% { opacity: 0; transform: rotate(var(--a)) translateY(-96px) scale(1); } 40% { opacity: 1; } 100% { opacity: 0; transform: rotate(var(--a)) translateY(-4px) scale(0.4); } }
@keyframes rk-fade { from { opacity: 0; } }
@keyframes rk-draw-g { from { stroke-dashoffset: 34; } }
@keyframes rk-name-in { from { opacity: 0; transform: translateY(70%); } }
@keyframes rk-name-out { to { opacity: 0; transform: translateY(-70%); } }
@media (prefers-reduced-motion: reduce) {
  .rk-ring, .rk-old, .rk-spark, .rk-old-name { display: none; }
}
