/**
 * THATCHESS UX ENHANCEMENTS
 * Focus states, tooltips, loading states, last move animation
 * "ese ajedrez" - ajedrez humano
 */

/* ========================================
   ENHANCED FOCUS STATES
   Keyboard navigation support
   ======================================== */

/* Base focus-visible styles */
:focus-visible {
  outline: 2px solid var(--tc-accent-amber, #fbbf24);
  outline-offset: 2px;
}

/* Remove default focus for mouse users */
:focus:not(:focus-visible) {
  outline: none;
}

/* UMV Controls focus states */
.umv-item:focus-visible {
  outline: 2px solid var(--tc-accent-amber, #fbbf24);
  outline-offset: 2px;
  background: var(--tc-gray-700, #262626);
}

.umv-trigger:focus-visible {
  outline: 2px solid var(--tc-accent-amber, #fbbf24);
  outline-offset: 2px;
}

/* Board squares focus for keyboard navigation */
.square:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 3px var(--tc-accent-amber, #fbbf24);
}

/* Buttons enhanced focus */
.tc-btn:focus-visible,
button:focus-visible {
  outline: 2px solid var(--tc-accent-amber, #fbbf24);
  outline-offset: 2px;
}

/* Tab buttons focus */
.tab-btn:focus-visible {
  outline: 2px solid var(--tc-accent-amber, #fbbf24);
  outline-offset: -2px;
}

/* Move list buttons focus */
#moves button:focus-visible,
.move-btn:focus-visible {
  outline: 2px solid var(--tc-accent-amber, #fbbf24);
  outline-offset: 1px;
  background: var(--tc-gray-700, #262626);
}

/* Input fields focus */
input[type="text"]:focus-visible,
input[type="password"]:focus-visible,
input[type="email"]:focus-visible,
textarea:focus-visible {
  outline: none;
  outline-offset: 0;
  border-color: inherit;
  box-shadow: none;
}

/* Neutralize extra focus rings on inputs */
.tc-input:focus,
input:focus,
textarea:focus {
  outline: none;
  box-shadow: none;
  border-color: var(--tc-white-pure, #fff);
}

/* Skip to content link (accessibility) */
.skip-link {
  position: absolute;
  top: -100px;
  left: 0;
  background: var(--tc-gray-900, #121212);
  color: var(--tc-white-pure, #fff);
  padding: var(--tc-space-3, 0.75rem) var(--tc-space-4, 1rem);
  z-index: 9999;
  transition: top 0.2s ease;
}

.skip-link:focus {
  top: 0;
}

/* ========================================
   TOOLTIPS
   Informative tooltips for UMV controls
   ======================================== */

/* Tooltip container */
[data-tooltip] {
  position: relative;
}

/* Tooltip bubble */
[data-tooltip]::before {
  content: attr(data-tooltip);
  position: absolute;
  right: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%);
  padding: 6px 10px;
  background: var(--tc-gray-900, #121212);
  color: var(--tc-gray-100, #f5f5f5);
  font-family: var(--tc-font-body, 'Space Grotesk', sans-serif);
  font-size: var(--tc-text-tiny, 0.75rem);
  font-weight: 500;
  letter-spacing: 0.02em;
  white-space: nowrap;
  border: 1px solid var(--tc-gray-700, #262626);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  pointer-events: none;
  z-index: 1000;
}

/* Tooltip arrow */
[data-tooltip]::after {
  content: '';
  position: absolute;
  right: calc(100% + 4px);
  top: 50%;
  transform: translateY(-50%);
  border: 6px solid transparent;
  border-left-color: var(--tc-gray-900, #121212);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  pointer-events: none;
  z-index: 1000;
}

/* Show tooltip on hover/focus */
[data-tooltip]:hover::before,
[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::before,
[data-tooltip]:focus-visible::after {
  opacity: 1;
  visibility: visible;
}

/* Tooltip position variants */
[data-tooltip-pos="top"]::before {
  right: auto;
  left: 50%;
  top: auto;
  bottom: calc(100% + 12px);
  transform: translateX(-50%);
}

[data-tooltip-pos="top"]::after {
  right: auto;
  left: 50%;
  top: auto;
  bottom: calc(100% + 4px);
  transform: translateX(-50%) rotate(-90deg);
}

[data-tooltip-pos="bottom"]::before {
  right: auto;
  left: 50%;
  top: calc(100% + 12px);
  bottom: auto;
  transform: translateX(-50%);
}

[data-tooltip-pos="bottom"]::after {
  right: auto;
  left: 50%;
  top: calc(100% + 4px);
  bottom: auto;
  transform: translateX(-50%) rotate(90deg);
}

/* Right positioned tooltip (for sidebar buttons) */
[data-tooltip-pos="right"]::before {
  right: auto;
  left: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%);
}

[data-tooltip-pos="right"]::after {
  right: auto;
  left: calc(100% + 4px);
  top: 50%;
  transform: translateY(-50%) rotate(180deg);
}

/* ========================================
   LOADING STATES
   Skeleton loaders and spinners
   ======================================== */

/* Skeleton pulse animation */
@keyframes tc-skeleton-pulse {
  0%, 100% {
    opacity: 0.4;
  }
  50% {
    opacity: 0.7;
  }
}

/* Skeleton base */
.tc-skeleton {
  background: linear-gradient(
    90deg,
    var(--tc-gray-800, #1a1a1a) 25%,
    var(--tc-gray-700, #262626) 50%,
    var(--tc-gray-800, #1a1a1a) 75%
  );
  background-size: 200% 100%;
  animation: tc-skeleton-shimmer 1.5s ease-in-out infinite;
  border-radius: 4px;
}

@keyframes tc-skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* Skeleton variants */
.tc-skeleton-text {
  height: 1em;
  width: 100%;
  margin-bottom: 0.5em;
}

.tc-skeleton-text:last-child {
  width: 70%;
}

.tc-skeleton-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.tc-skeleton-button {
  height: 36px;
  width: 120px;
}

/* Spinner */
.tc-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--tc-gray-700, #262626);
  border-top-color: var(--tc-accent-amber, #fbbf24);
  border-radius: 50%;
  animation: tc-spin 0.8s linear infinite;
}

.tc-spinner-sm {
  width: 14px;
  height: 14px;
  border-width: 1.5px;
}

.tc-spinner-lg {
  width: 32px;
  height: 32px;
  border-width: 3px;
}

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

/* Loading overlay */
.tc-loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 10, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.tc-loading-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* Button loading state */
.tc-btn.loading {
  position: relative;
  color: transparent;
  pointer-events: none;
}

.tc-btn.loading::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: tc-spin 0.8s linear infinite;
  left: 50%;
  top: 50%;
  margin-left: -8px;
  margin-top: -8px;
}

/* Panel loading state */
.panel-loading {
  position: relative;
  min-height: 100px;
}

.panel-loading::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--tc-gray-900, #121212);
  opacity: 0.8;
  z-index: 10;
}

.panel-loading::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 24px;
  height: 24px;
  margin: -12px 0 0 -12px;
  border: 2px solid var(--tc-gray-600, #404040);
  border-top-color: var(--tc-crimson, #dc2626);
  border-radius: 50%;
  animation: tc-spin 0.8s linear infinite;
  z-index: 11;
}

/* ========================================
   LAST MOVE ANIMATION
   Enhanced animation for the existing last-from/last-to system
   ======================================== */

/* Override existing last-move styles with animations */
.square.last-from::after {
  animation: tc-lastmove-from 0.5s ease-out forwards;
}

.square.last-to::after {
  animation: tc-lastmove-to 0.6s ease-out forwards;
}

@keyframes tc-lastmove-from {
  0% {
    background: rgba(4, 120, 87, 0.4);
    box-shadow: inset 0 0 0 3px rgba(4, 120, 87, 0.7);
    transform: scale(1.02);
  }
  100% {
    background: rgba(4, 120, 87, 0.18);
    box-shadow: inset 0 0 0 2px rgba(4, 120, 87, 0.5);
    transform: scale(1);
  }
}

@keyframes tc-lastmove-to {
  0% {
    background: rgba(4, 120, 87, 0.55);
    box-shadow: inset 0 0 0 3px rgba(4, 120, 87, 0.85);
    transform: scale(1.05);
  }
  40% {
    background: rgba(4, 120, 87, 0.45);
    box-shadow: inset 0 0 0 2px rgba(4, 120, 87, 0.75);
  }
  100% {
    background: rgba(4, 120, 87, 0.35);
    box-shadow: inset 0 0 0 2px rgba(4, 120, 87, 0.7);
    transform: scale(1);
  }
}

/* ========================================
   KEYBOARD NAVIGATION INDICATOR
   Shows when user is navigating with keyboard
   ======================================== */

body.keyboard-nav *:focus-visible {
  outline-width: 3px;
}

/* Visual indicator that keyboard nav is active */
body.keyboard-nav::before {
  content: '';
  position: fixed;
  top: 8px;
  right: 8px;
  width: 8px;
  height: 8px;
  background: var(--tc-accent-amber, #fbbf24);
  border-radius: 50%;
  opacity: 0.6;
  z-index: 9999;
  pointer-events: none;
  animation: tc-keyboard-indicator 2s ease-in-out infinite;
}

@keyframes tc-keyboard-indicator {
  0%, 100% {
    opacity: 0.4;
  }
  50% {
    opacity: 0.8;
  }
}

/* ========================================
   REDUCED MOTION SUPPORT
   ======================================== */

@media (prefers-reduced-motion: reduce) {
  .tc-skeleton {
    animation: tc-skeleton-pulse 2s ease-in-out infinite;
  }

  .tc-spinner,
  .tc-btn.loading::after,
  .panel-loading::after {
    animation-duration: 1.5s;
  }

  .square.last-move-from::after,
  .square.last-move-to::after {
    animation: none;
    opacity: 0.2;
  }

  [data-tooltip]::before,
  [data-tooltip]::after {
    transition: none;
  }
}
