/* ========================================
   Back To Top — Premium Institutional Button
   Mobile-first · Accessible · Performant
   ======================================== */

:root {
  --btt-primary: #aa8432;
  --btt-primary-deep: #7a0606;
  --btt-primary-soft: #c41a1a;
  --btt-secondary: #6b594c;
  --btt-icon: #ffffff;
  --btt-ring-track: rgba(159, 8, 8, 0.18);
  --btt-ring-fill: #d4a017;
  --btt-shadow: 0 4px 14px rgba(159, 8, 8, 0.28);
  --btt-shadow-hover: 0 10px 28px rgba(159, 8, 8, 0.42);
  --btt-size: 48px;
  --btt-inset: 20px;
  --btt-duration: 320ms;
  --btt-ease: cubic-bezier(0.22, 1, 0.36, 1);
}

@media (min-width: 768px) {
  :root {
    --btt-size: 54px;
    --btt-inset: 28px;
  }
}

.back-to-top {
  --btt-progress: 0;
  --btt-tint: var(--btt-primary);

  position: fixed !important;
  bottom: var(--btt-inset) !important;
  right: var(--btt-inset) !important;
  z-index: 99999 !important;
  width: var(--btt-size);
  height: var(--btt-size);
  padding: 0;
  margin: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--btt-icon);
  cursor: pointer;
  display: grid !important;
  place-items: center;
  isolation: isolate;
  overflow: visible;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translate3d(0, 18px, 0) scale(0.88);
  transition:
    opacity var(--btt-duration) var(--btt-ease),
    visibility var(--btt-duration) var(--btt-ease),
    transform var(--btt-duration) var(--btt-ease),
    box-shadow var(--btt-duration) var(--btt-ease);
  -webkit-tap-highlight-color: transparent;
  will-change: transform, opacity;
}

.back-to-top.is-visible,
.back-to-top.show {
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
  transform: translate3d(0, 0, 0) scale(1);
  animation: btt-soft-pulse 3.2s var(--btt-ease) infinite;
}

.back-to-top__core {
  position: absolute;
  inset: 5px;
  border-radius: 50%;
  background: var(--btt-tint);
  background-image: linear-gradient(
    145deg,
    var(--btt-primary-soft),
    var(--btt-tint) 48%,
    var(--btt-primary-deep)
  );
  box-shadow: var(--btt-shadow);
  display: grid;
  place-items: center;
  overflow: hidden;
  transition:
    background var(--btt-duration) var(--btt-ease),
    box-shadow var(--btt-duration) var(--btt-ease),
    transform var(--btt-duration) var(--btt-ease);
}

.back-to-top__icon {
  position: relative;
  z-index: 2;
  font-size: 1.35rem;
  line-height: 1;
  color: var(--btt-icon);
  transition: transform var(--btt-duration) var(--btt-ease);
}

.back-to-top__ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
  pointer-events: none;
  z-index: 1;
}

.back-to-top__ring-track {
  fill: none;
  stroke: var(--btt-ring-track);
  stroke-width: 2.5;
}

.back-to-top__ring-progress {
  fill: none;
  stroke: var(--btt-ring-fill);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: 100;
  stroke-dashoffset: calc(100 - var(--btt-progress));
  transition: stroke-dashoffset 120ms linear;
}

.back-to-top__tooltip {
  position: absolute;
  right: calc(100% + 12px);
  top: 50%;
  transform: translate3d(6px, -50%, 0);
  padding: 0.4rem 0.7rem;
  border-radius: 6px;
  background: var(--btt-secondary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  white-space: nowrap;
  line-height: 1.2;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(24, 24, 24, 0.18);
  transition:
    opacity 250ms var(--btt-ease),
    visibility 250ms var(--btt-ease),
    transform 250ms var(--btt-ease);
}

.back-to-top__tooltip::after {
  content: "";
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  border: 5px solid transparent;
  border-left-color: var(--btt-secondary);
}

.back-to-top__ripple {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
  z-index: 3;
  overflow: hidden;
}

.back-to-top__ripple-wave {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
  transform: scale(0);
  animation: btt-ripple 520ms ease-out forwards;
  pointer-events: none;
}

/* ----- Interactions ----- */
@media (hover: hover) and (pointer: fine) {
  .back-to-top.is-visible:hover,
  .back-to-top.show:hover {
    animation: none;
    transform: translate3d(0, -4px, 0) scale(1.06);
  }

  .back-to-top.is-visible:hover .back-to-top__core,
  .back-to-top.show:hover .back-to-top__core {
    background: linear-gradient(
      145deg,
      var(--btt-primary-soft),
      var(--btt-primary) 40%,
      var(--btt-primary-deep)
    );
    box-shadow: var(--btt-shadow-hover);
  }

  .back-to-top.is-visible:hover .back-to-top__icon,
  .back-to-top.show:hover .back-to-top__icon {
    transform: translateY(-2px);
  }

  .back-to-top.is-visible:hover .back-to-top__tooltip,
  .back-to-top.show:hover .back-to-top__tooltip {
    opacity: 1;
    visibility: visible;
    transform: translate3d(0, -50%, 0);
  }
}

.back-to-top.is-visible:active .back-to-top__core,
.back-to-top.show:active .back-to-top__core {
  transform: scale(0.94);
  box-shadow: 0 2px 8px rgba(159, 8, 8, 0.3);
}

.back-to-top:focus-visible {
  outline: none;
}

.back-to-top:focus-visible .back-to-top__core {
  outline: 3px solid #e0c56a;
  outline-offset: 3px;
  box-shadow: var(--btt-shadow-hover);
}

/* ----- Keyframes ----- */
@keyframes btt-soft-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(159, 8, 8, 0);
  }
  50% {
    box-shadow: 0 0 0 8px rgba(159, 8, 8, 0.08);
  }
}

@keyframes btt-ripple {
  to {
    transform: scale(2.4);
    opacity: 0;
  }
}

/* ----- Reduced motion ----- */
@media (prefers-reduced-motion: reduce) {
  .back-to-top,
  .back-to-top__core,
  .back-to-top__icon,
  .back-to-top__tooltip,
  .back-to-top__ring-progress {
    transition: none !important;
    animation: none !important;
  }

  .back-to-top.is-visible {
    transform: none;
  }

  .back-to-top.is-visible:hover {
    transform: none;
  }
}

/* Safe area (notched phones) */
@supports (padding: max(0px)) {
  .back-to-top {
    bottom: max(var(--btt-inset), env(safe-area-inset-bottom));
    right: max(var(--btt-inset), env(safe-area-inset-right));
  }
}
