/**
 * ShadowReader - Accessibility Styles
 * Focus indicators, keyboard navigation, and accessibility enhancements
 */

/* ===== Focus Indicators ===== */

/* Default focus style for all interactive elements */
*:focus {
  outline: none;
}

*:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
  box-shadow: 0 0 0 4px rgba(255, 140, 66, 0.2);
  transition: outline var(--duration-fast), box-shadow var(--duration-fast);
}

/* Gothic-styled focus for buttons */
button:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
  box-shadow: 
    0 0 0 4px rgba(255, 140, 66, 0.2),
    var(--glow-candle);
  position: relative;
}

button:focus-visible::before {
  content: '';
  position: absolute;
  inset: -6px;
  border: 1px solid var(--color-accent);
  border-radius: inherit;
  opacity: 0.5;
  pointer-events: none;
  animation: focusPulse 2s ease-in-out infinite;
}

/* Focus for inputs */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-color: var(--color-accent);
  box-shadow: 
    0 0 0 3px rgba(255, 140, 66, 0.15),
    var(--glow-candle);
}

/* Focus for links */
a:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
  box-shadow: 0 0 0 4px rgba(255, 140, 66, 0.2);
  text-shadow: var(--glow-candle);
}

/* Focus for navigation arrows */
.nav-arrow:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 4px;
  box-shadow: 
    0 0 0 6px rgba(255, 140, 66, 0.2),
    var(--glow-candle-intense);
  transform: scale(1.05);
}

.nav-arrow:focus-visible .arrow-icon {
  filter: drop-shadow(var(--glow-candle));
}

/* Focus for header icons */
.header-icon:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
  box-shadow: 
    0 0 0 4px rgba(255, 140, 66, 0.2),
    var(--glow-candle-medium);
  transform: translateY(-2px);
}

.header-icon:focus-visible .icon-svg {
  filter: drop-shadow(var(--glow-candle));
}

/* Focus for note items */
.note-item:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  box-shadow: 
    0 0 0 4px rgba(255, 140, 66, 0.15),
    var(--glow-ethereal);
  background-color: rgba(255, 140, 66, 0.05);
}

/* Focus for theme buttons */
.theme-btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  box-shadow: 
    0 0 0 4px rgba(255, 140, 66, 0.2),
    var(--glow-candle);
  transform: scale(1.05);
}

/* Focus for wax seal close buttons */
.wax-seal-close:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 4px;
  box-shadow: 
    0 0 0 6px rgba(255, 140, 66, 0.25),
    var(--glow-candle-intense);
  transform: scale(1.1) rotate(5deg);
}

/* Focus for ghost assistant minimize button */
.assistant-minimize-btn:focus-visible,
.ghost-assistant-minimized:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
  box-shadow: 
    0 0 0 5px rgba(255, 140, 66, 0.2),
    var(--glow-candle-medium);
}

/* Focus for toggle switches */
.toggle-switch input:focus-visible + .toggle-slider {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  box-shadow: 
    0 0 0 4px rgba(255, 140, 66, 0.2),
    var(--glow-candle);
}

/* Focus for range sliders */
input[type="range"]:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 4px;
  box-shadow: 0 0 0 6px rgba(255, 140, 66, 0.15);
}

input[type="range"]:focus-visible::-webkit-slider-thumb {
  box-shadow: var(--glow-candle-intense);
  transform: scale(1.2);
}

input[type="range"]:focus-visible::-moz-range-thumb {
  box-shadow: var(--glow-candle-intense);
  transform: scale(1.2);
}

/* ===== Focus Animations ===== */
@keyframes focusPulse {
  0%, 100% {
    opacity: 0.5;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.02);
  }
}

/* ===== Skip Links ===== */
.skip-link {
  position: absolute;
  top: -100px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  background-color: var(--color-surface);
  color: var(--color-text-primary);
  padding: var(--spacing-md) var(--spacing-lg);
  border: var(--border-ornate);
  border-radius: var(--radius-md);
  font-family: var(--font-gothic);
  font-size: var(--font-size-md);
  text-decoration: none;
  box-shadow: var(--shadow-deep), var(--glow-candle-intense);
  transition: top var(--duration-fast);
}

.skip-link:focus {
  top: var(--spacing-md);
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
}

/* ===== Keyboard Navigation Hints ===== */
.keyboard-hint {
  display: inline-block;
  padding: 2px 6px;
  background-color: var(--color-brown-medium);
  border: 1px solid var(--color-parchment-dark);
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: var(--font-size-xs);
  color: var(--color-text-secondary);
  margin-left: var(--spacing-xs);
}

/* ===== Screen Reader Only Content ===== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

.sr-only-focusable:focus,
.sr-only-focusable:active {
  position: static;
  width: auto;
  height: auto;
  overflow: visible;
  clip: auto;
  white-space: normal;
}

/* ===== High Contrast Mode Support ===== */
@media (prefers-contrast: high) {
  *:focus-visible {
    outline-width: 3px;
    outline-offset: 4px;
  }

  button:focus-visible,
  .nav-arrow:focus-visible,
  .header-icon:focus-visible {
    outline-width: 3px;
    box-shadow: none;
  }
}

/* ===== Reduced Motion Support ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  *:focus-visible::before {
    animation: none;
  }

  button:focus-visible,
  .nav-arrow:focus-visible,
  .header-icon:focus-visible {
    transition: none;
    transform: none;
  }

  @keyframes focusPulse {
    0%, 100% {
      opacity: 0.5;
      transform: none;
    }
  }

  /* Disable page flip animations */
  .page-flipping,
  .page-flipping-back {
    animation: none !important;
    transition: none !important;
  }

  /* Disable atmospheric effects animations */
  .atmospheric-effects * {
    animation: none !important;
    transition: none !important;
  }

  /* Disable ghost breathing animation */
  .ghost-glow {
    animation: none !important;
  }

  /* Disable candle flicker */
  .candle-flame {
    animation: none !important;
  }

  /* Disable fog movement */
  .fog-particle {
    animation: none !important;
  }

  /* Disable sparkle animations */
  .sparkle {
    animation: none !important;
  }

  /* Disable cursor trail */
  .cursor-trail {
    display: none !important;
  }
}

/* Body class for manual reduced motion control */
body.reduce-motion *,
body.reduce-motion *::before,
body.reduce-motion *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
  scroll-behavior: auto !important;
}

body.reduce-motion .page-flipping,
body.reduce-motion .page-flipping-back {
  animation: none !important;
  transition: none !important;
}

body.reduce-motion .atmospheric-effects * {
  animation: none !important;
  transition: none !important;
}

body.reduce-motion .ghost-glow {
  animation: none !important;
}

body.reduce-motion .candle-flame {
  animation: none !important;
}

body.reduce-motion .fog-particle {
  animation: none !important;
}

body.reduce-motion .sparkle {
  animation: none !important;
}

body.reduce-motion .cursor-trail {
  display: none !important;
}

/* ===== Focus Within (for containers) ===== */
.reading-area:focus-within {
  /* Subtle indication that reading area has focus */
}

.notes-sidebar:focus-within {
  /* Subtle indication that notes sidebar has focus */
}

.settings-modal:focus-within {
  /* Subtle indication that settings modal has focus */
}

/* ===== Disabled State Accessibility ===== */
button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: var(--opacity-disabled);
}

button:disabled:focus,
input:disabled:focus,
select:disabled:focus,
textarea:disabled:focus {
  outline: none;
  box-shadow: none;
}

/* ===== ARIA Live Regions ===== */
[aria-live="polite"],
[aria-live="assertive"] {
  /* Ensure live regions are accessible but not visually intrusive */
}

/* ===== Focus Trap Indicator (for modals) ===== */
.focus-trap-active {
  /* Visual indicator that focus is trapped in a modal */
}

/* ===== Keyboard Navigation Active State ===== */
body.keyboard-navigation-active *:focus {
  /* Enhanced focus styles when keyboard navigation is detected */
}

/* ===== Tab Order Indicators (for development/debugging) ===== */
.show-tab-order *[tabindex]:not([tabindex="-1"])::after {
  content: attr(tabindex);
  position: absolute;
  top: 0;
  right: 0;
  background: var(--color-accent);
  color: var(--color-black);
  padding: 2px 4px;
  font-size: 10px;
  border-radius: var(--radius-sm);
  z-index: 9999;
}

/* ===== Text Contrast and Readability ===== */

/* Ensure minimum contrast ratios for text */
body {
  /* Base text color has 4.5:1 contrast ratio against background */
  color: var(--color-text-primary);
  background-color: var(--color-background);
}

/* Ensure readable text in all contexts */
.page-text,
.note-text,
.message-bubble,
.settings-modal {
  /* Ensure text is readable with atmospheric effects */
  position: relative;
  z-index: 2;
}

/* Enhance contrast for important text */
.page-text {
  color: var(--color-parchment);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* Ensure buttons have sufficient contrast */
button {
  color: var(--color-text-primary);
}

button:hover,
button:focus {
  color: var(--color-accent-hover);
}

/* Ensure links have sufficient contrast */
a {
  color: var(--color-accent);
}

a:hover,
a:focus {
  color: var(--color-accent-hover);
}

/* Support text resize up to 200% */
html {
  font-size: 16px; /* Base font size */
}

@media (min-width: 1024px) {
  html {
    font-size: 16px; /* Maintain base size on desktop */
  }
}

/* Ensure text remains readable when zoomed */
* {
  max-width: 100%;
}

.page-text,
.note-text,
.message-bubble p {
  /* Allow text to reflow when zoomed */
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
}

/* Ensure minimum font sizes for readability */
.page-text {
  font-size: max(16px, 1rem);
  line-height: var(--line-height-base);
}

.note-text {
  font-size: max(14px, 0.875rem);
  line-height: var(--line-height-base);
}

/* Ensure sufficient line height for readability */
p {
  line-height: var(--line-height-base);
  margin-bottom: 1em;
}

/* Ensure text remains visible with atmospheric effects */
.atmospheric-effects ~ * {
  /* Ensure content is above atmospheric effects */
  position: relative;
  z-index: var(--z-content);
}

/* High contrast mode adjustments */
@media (prefers-contrast: high) {
  body {
    background-color: #000;
    color: #fff;
  }

  .page-text,
  .note-text {
    color: #fff;
    text-shadow: none;
  }

  button,
  a {
    color: #fff;
    border: 2px solid #fff;
  }

  button:hover,
  button:focus,
  a:hover,
  a:focus {
    background-color: #fff;
    color: #000;
  }

  /* Disable atmospheric effects in high contrast mode */
  .atmospheric-effects {
    display: none !important;
  }
}

/* Ensure text selection is visible */
::selection {
  background-color: var(--color-accent);
  color: var(--color-black);
}

::-moz-selection {
  background-color: var(--color-accent);
  color: var(--color-black);
}

/* Ensure placeholder text has sufficient contrast */
::placeholder {
  color: var(--color-text-secondary);
  opacity: 0.8;
}

/* Ensure disabled text is still readable */
:disabled {
  color: var(--color-text-secondary);
  opacity: 0.6;
}

/* Support for forced colors mode (Windows High Contrast) */
@media (forced-colors: active) {
  * {
    forced-color-adjust: auto;
  }

  button,
  a,
  input,
  select,
  textarea {
    border: 1px solid currentColor;
  }

  .atmospheric-effects {
    display: none !important;
  }
}

/* Ensure text remains readable at different zoom levels */
@media (min-resolution: 2dppx) {
  .page-text,
  .note-text {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
}
