/* Loading overlay — hamster-in-a-wheel loader + progress %.
 *
 * Driven by window.sgLoading (main.js). The overlay is *deferred*: nothing is
 * painted until the work has already run for sgLoading.delay (2s), so quick
 * saves never flash a spinner.
 */
.sg-load-overlay {
  position: fixed; inset: 0; z-index: 5000; display: none;
  align-items: center; justify-content: center;
  background: rgba(8, 20, 38, .58); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  opacity: 0; transition: opacity .2s;
}
.sg-load-overlay.show { display: flex; opacity: 1; }
/* belt and braces: if the overlay is ever left in a half-state it must not be
   able to swallow clicks while invisible */
.sg-load-overlay:not(.show) { pointer-events: none; }
.sg-load-pop {
  background: var(--bg-card, #fff);
  border-radius: var(--r-xl, 20px);
  padding: 26px 34px 24px;
  box-shadow: 0 24px 60px rgba(0,0,0,.34);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  min-width: 300px; max-width: calc(100vw - 32px);
  transform: translateY(10px) scale(.97); transition: transform .22s cubic-bezier(.2,.9,.3,1.2);
}
.sg-load-overlay.show .sg-load-pop { transform: none; }
.sg-load-label {
  font-family: var(--font-display, inherit); font-weight: 700;
  color: var(--text-main, #12304a); font-size: 1.02rem; margin-top: 10px;
}
.sg-load-bar {
  width: 100%; height: 8px; border-radius: 999px;
  background: var(--bg-soft, #e3f0fa); overflow: hidden; margin-top: 6px;
}
.sg-load-bar > i {
  display: block; height: 100%; width: 0%; border-radius: 999px;
  background: linear-gradient(90deg, var(--sk, #0ea5e9), var(--sk-deep, #0369a1));
  transition: width .25s ease;
}
.sg-load-pct {
  font-family: var(--font-display, inherit); font-weight: 700;
  font-size: 1.15rem; color: var(--sk-ink, #045b8a);
  font-variant-numeric: tabular-nums; letter-spacing: -.01em;
}
.sg-load-sub { font-size: .76rem; color: var(--text-muted, #4e6a86); }

/* ══ hamster in a wheel (design reference — keep the geometry verbatim) ══ */
.wheel-and-hamster {
  --dur: 1s;
  position: relative;
  width: 12em;
  height: 12em;
  font-size: 12px;
}
.wheel,
.hamster,
.hamster div,
.spoke { position: absolute; }
.wheel,
.spoke { border-radius: 50%; top: 0; left: 0; width: 100%; height: 100%; }
.wheel {
  background: radial-gradient(100% 100% at center,hsla(0,0%,60%,0) 47.8%,hsl(0,0%,60%) 48%);
  z-index: 2;
}
.hamster {
  animation: hamster var(--dur) ease-in-out infinite;
  top: 50%;
  left: calc(50% - 3.5em);
  width: 7em;
  height: 3.75em;
  transform: rotate(4deg) translate(-0.8em,1.85em);
  transform-origin: 50% 0;
  z-index: 1;
}
.hamster__head {
  animation: hamsterHead var(--dur) ease-in-out infinite;
  background: hsl(30,90%,55%);
  border-radius: 70% 30% 0 100% / 40% 25% 25% 60%;
  box-shadow: 0 -0.25em 0 hsl(30,90%,80%) inset, 0.75em -1.55em 0 hsl(30,90%,90%) inset;
  top: 0; left: -2em; width: 2.75em; height: 2.5em;
  transform-origin: 100% 50%;
}
.hamster__ear {
  animation: hamsterEar var(--dur) ease-in-out infinite;
  background: hsl(0,90%,85%);
  border-radius: 50%;
  box-shadow: -0.25em 0 hsl(30,90%,55%) inset;
  top: -0.25em; right: -0.25em; width: 0.75em; height: 0.75em;
  transform-origin: 50% 75%;
}
.hamster__eye {
  animation: hamsterEye var(--dur) linear infinite;
  background-color: hsl(0,0%,0%);
  border-radius: 50%;
  top: 0.375em; left: 1.25em; width: 0.5em; height: 0.5em;
}
.hamster__nose {
  background: hsl(0,90%,75%);
  border-radius: 35% 65% 85% 15% / 70% 50% 50% 30%;
  top: 0.75em; left: 0; width: 0.2em; height: 0.25em;
}
.hamster__body {
  animation: hamsterBody var(--dur) ease-in-out infinite;
  background: hsl(30,90%,90%);
  border-radius: 50% 30% 50% 30% / 15% 60% 40% 40%;
  box-shadow: 0.1em 0.75em 0 hsl(30,90%,55%) inset, 0.15em -0.5em 0 hsl(30,90%,80%) inset;
  top: 0.25em; left: 2em; width: 4.5em; height: 3em;
  transform-origin: 17% 50%;
  transform-style: preserve-3d;
}
.hamster__limb--fr,
.hamster__limb--fl {
  clip-path: polygon(0 0,100% 0,70% 80%,60% 100%,0% 100%,40% 80%);
  top: 2em; left: 0.5em; width: 1em; height: 1.5em;
  transform-origin: 50% 0;
}
.hamster__limb--fr {
  animation: hamsterFRLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30,90%,80%) 80%,hsl(0,90%,75%) 80%);
  transform: rotate(15deg) translateZ(-1px);
}
.hamster__limb--fl {
  animation: hamsterFLLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30,90%,90%) 80%,hsl(0,90%,85%) 80%);
  transform: rotate(15deg);
}
.hamster__limb--br,
.hamster__limb--bl {
  border-radius: 0.75em 0.75em 0 0;
  clip-path: polygon(0 0,100% 0,100% 30%,70% 90%,70% 100%,30% 100%,40% 90%,0% 30%);
  top: 1em; left: 2.8em; width: 1.5em; height: 2.5em;
  transform-origin: 50% 30%;
}
.hamster__limb--br {
  animation: hamsterBRLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30,90%,80%) 90%,hsl(0,90%,75%) 90%);
  transform: rotate(-25deg) translateZ(-1px);
}
.hamster__limb--bl {
  animation: hamsterBLLimb var(--dur) linear infinite;
  background: linear-gradient(hsl(30,90%,90%) 90%,hsl(0,90%,85%) 90%);
  transform: rotate(-25deg);
}
.hamster__tail {
  animation: hamsterTail var(--dur) linear infinite;
  background: hsl(0,90%,85%);
  border-radius: 0.25em 50% 50% 0.25em;
  box-shadow: 0 -0.2em 0 hsl(0,90%,75%) inset;
  top: 1.5em; right: -0.5em; width: 1em; height: 0.5em;
  transform: rotate(30deg) translateZ(-1px);
  transform-origin: 0.25em 0.25em;
}
.spoke {
  animation: spoke var(--dur) linear infinite;
  background: radial-gradient(100% 100% at center,hsl(0,0%,60%) 4.8%,hsla(0,0%,60%,0) 5%),
    linear-gradient(hsla(0,0%,55%,0) 46.9%,hsl(0,0%,65%) 47% 52.9%,hsla(0,0%,65%,0) 53%) 50% 50% / 99% 99% no-repeat;
}

@keyframes hamster {
  from, to { transform: rotate(4deg) translate(-0.8em,1.85em); }
  50% { transform: rotate(0) translate(-0.8em,1.85em); }
}
@keyframes hamsterHead {
  from, 25%, 50%, 75%, to { transform: rotate(0); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(8deg); }
}
@keyframes hamsterEye {
  from, 90%, to { transform: scaleY(1); }
  95% { transform: scaleY(0); }
}
@keyframes hamsterEar {
  from, 25%, 50%, 75%, to { transform: rotate(0); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(12deg); }
}
@keyframes hamsterBody {
  from, 25%, 50%, 75%, to { transform: rotate(0); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(-2deg); }
}
@keyframes hamsterFRLimb {
  from, 25%, 50%, 75%, to { transform: rotate(50deg) translateZ(-1px); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(-30deg) translateZ(-1px); }
}
@keyframes hamsterFLLimb {
  from, 25%, 50%, 75%, to { transform: rotate(-30deg); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(50deg); }
}
@keyframes hamsterBRLimb {
  from, 25%, 50%, 75%, to { transform: rotate(-60deg) translateZ(-1px); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(20deg) translateZ(-1px); }
}
@keyframes hamsterBLLimb {
  from, 25%, 50%, 75%, to { transform: rotate(20deg); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(-60deg); }
}
@keyframes hamsterTail {
  from, 25%, 50%, 75%, to { transform: rotate(30deg) translateZ(-1px); }
  12.5%, 37.5%, 62.5%, 87.5% { transform: rotate(10deg) translateZ(-1px); }
}
@keyframes spoke {
  from { transform: rotate(0); }
  to { transform: rotate(-1turn); }
}

@media (prefers-reduced-motion: reduce) {
  .wheel-and-hamster *, .wheel-and-hamster { animation: none !important; }
  .sg-load-pop { transition: none; }
}
@media (max-width: 575px) {
  .wheel-and-hamster { font-size: 9px; }
  .sg-load-pop { min-width: 0; width: calc(100vw - 40px); padding: 20px 22px 18px; }
}

/* ═══════════════════════════════════════════════════════════════════════
   ปุ่มปิด/ย่อ · แถบวิ่งเมื่อวัด % ไม่ได้ · สถานะค้าง/ล้มเหลว · เม็ดย่อมุมจอ
   (ออกแบบใหม่ 20 ส.ค. 2569 ตามที่เจ้าของแจ้ง)
   ═══════════════════════════════════════════════════════════════════════ */
.sg-load-pop { position: relative; padding-top: 34px; }
.sg-load-tools {
  position: absolute; top: 8px; right: 8px; display: flex; gap: 2px;
}
.sg-load-btn {
  display: grid; place-items: center; width: 30px; height: 30px; padding: 0;
  border: 0; border-radius: 50%; background: transparent; cursor: pointer;
  color: var(--text-muted, #4e6a86); font-size: .82rem; line-height: 1;
  transition: background .15s, color .15s;
}
.sg-load-btn:hover { background: var(--bg-soft, #e3f0fa); color: var(--text-main, #12304a); }
.sg-load-btn:focus-visible { outline: 3px solid rgba(14,165,233,.45); outline-offset: 2px; }

/* วัด % ไม่ได้ = **ไม่โชว์ตัวเลข** แล้วใช้แถบวิ่งแทน
   เดิมมันไต่เลขเอง 0→95 แล้วกระโดด 100 ตอนหน้าเปลี่ยน ซึ่งอ่านว่า "เสร็จแล้ว"
   ทั้งที่งานจริงเพิ่งเริ่ม — ตัวเลขที่ไม่ได้มาจากของจริงคือการโกหก */
.sg-load-pop.is-indeterminate .sg-load-pct { display: none; }
.sg-load-pop.is-indeterminate .sg-load-bar > i {
  width: 38% !important; border-radius: 999px;
  animation: sgLoadSlide 1.15s cubic-bezier(.45,.05,.55,.95) infinite;
}
@keyframes sgLoadSlide {
  0%   { transform: translateX(-105%); }
  100% { transform: translateX(275%); }
}

/* ค้าง — เตือนจากของจริง (ไม่มีความคืบหน้าเข้ามาเกิน 12 วินาที) ไม่ใช่ตามเวลาที่ผ่านไป */
.sg-load-pop.is-stuck .sg-load-sub { color: var(--gold-ink, #9c6406); font-weight: 600; }
.sg-load-pop.is-stuck .sg-load-bar > i { background: linear-gradient(90deg, var(--gold, #f2b01e), var(--gold-ink, #9c6406)); }

/* ล้มเหลว — ค้างไว้ให้อ่าน ไม่หายเอง */
.sg-load-pop.is-fail .wheel-and-hamster { display: none; }
.sg-load-pop.is-fail .sg-load-pct { display: none; }
.sg-load-pop.is-fail .sg-load-bar { display: none; }
.sg-load-pop.is-fail .sg-load-label { color: var(--red-ink, #c22a2f); }
.sg-load-pop.is-fail .sg-load-sub { color: var(--text-main, #12304a); text-align: center; max-width: 34ch; }
.sg-load-pop.is-fail::before {
  content: "\F337"; font-family: "bootstrap-icons"; font-size: 2.6rem; line-height: 1;
  color: var(--red-ink, #c22a2f); margin-bottom: 4px;
}

/* ── เม็ดย่อมุมจอ ─────────────────────────────────────────────────────────
   ย่อแล้วงาน **ยังทำอยู่** — เม็ดนี้คือสิ่งเดียวที่บอกว่ายังมีอะไรค้างอยู่
   ถ้าไม่มี ผู้ใช้จะคิดว่ากดปิดแล้วงานหยุด แล้วไปกดสั่งซ้ำอีกรอบ */
.sg-load-chip {
  position: fixed; right: 18px; bottom: 18px; z-index: 5001;
  display: none; align-items: center; gap: 10px;
  padding: 8px 16px 8px 8px; border: 0; border-radius: 999px; cursor: pointer;
  background: var(--bg-card, #fff); color: var(--text-main, #12304a);
  box-shadow: 0 10px 30px rgba(2,20,40,.24), 0 0 0 1px rgba(2,20,40,.06);
  font: inherit; font-size: .84rem; font-weight: 700;
  transform: translateY(12px); opacity: 0;
  transition: transform .24s cubic-bezier(.2,1.2,.4,1), opacity .2s, box-shadow .18s;
}
.sg-load-chip.show { display: flex; transform: none; opacity: 1; }
.sg-load-chip:hover { box-shadow: 0 14px 36px rgba(2,20,40,.3), 0 0 0 1px rgba(2,20,40,.08); }
/* วงแหวนความคืบหน้า — conic-gradient อ่าน --p ที่ JS ตั้งให้ */
.sg-load-chip-ring {
  --p: 0;
  position: relative; width: 30px; height: 30px; border-radius: 50%;
  background: conic-gradient(var(--sk-deep, #0369a1) calc(var(--p) * 1%), var(--bg-soft, #e3f0fa) 0);
  display: grid; place-items: center; flex: none;
}
.sg-load-chip-ring > b {
  width: 22px; height: 22px; border-radius: 50%; background: var(--bg-card, #fff);
  display: block;
}
.sg-load-chip-ring::after {
  content: ""; position: absolute; width: 8px; height: 8px; border-radius: 50%;
  background: var(--sk-deep, #0369a1);
  animation: sgChipPulse 1.4s ease-in-out infinite;
}
@keyframes sgChipPulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: .35; transform: scale(.7); } }
.sg-load-chip-tx { font-variant-numeric: tabular-nums; min-width: 3ch; }
.sg-load-chip.is-stuck .sg-load-chip-ring { background: conic-gradient(var(--gold-ink, #9c6406) calc(var(--p) * 1%), var(--bg-soft, #e3f0fa) 0); }
.sg-load-chip.is-stuck .sg-load-chip-ring::after { background: var(--gold-ink, #9c6406); }

html.dark .sg-load-btn:hover { background: rgba(255,255,255,.08); }
html.dark .sg-load-chip { box-shadow: 0 10px 30px rgba(0,0,0,.5), 0 0 0 1px rgba(255,255,255,.07); }

@media (max-width: 575px) {
  .sg-load-chip { right: 12px; bottom: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .sg-load-pop.is-indeterminate .sg-load-bar > i,
  .sg-load-chip-ring::after { animation: none; }
  .sg-load-chip { transition: none; }
}
