/* ==========================================================
   Accessibility Module UI Styles
========================================================== */

#acc-module-btn {
  position: fixed;
  width: 60px;
  height: 60px;
  background-color: #0056b3;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  cursor: grab;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 999999;
  transition: transform 0.3s ease;
  border: 2px solid #fff;
  bottom: 20px;
  left: 20px;
  user-select: none;
}

#acc-module-btn svg {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
}

#acc-module-btn:hover {
  transform: scale(1.1);
  background-color: #004494;
}

#acc-module-panel {
  position: fixed;
  width: 320px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  z-index: 999998;
  display: none;
  flex-direction: column;
  overflow: hidden;
  font-family: Arial, sans-serif !important;
  border: 1px solid #e0e0e0;
  bottom: 90px;
  left: 20px;
}

#acc-module-panel.acc-active {
  display: flex;
}

.acc-panel-header {
  background-color: #0056b3;
  color: white;
  padding: 15px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.acc-header-icon svg {
  width: 20px;
  height: 20px;
  margin-right: 5px;
  vertical-align: middle;
}

.acc-panel-header .acc-close-btn {
  cursor: pointer;
}

.acc-panel-header .acc-close-btn svg {
  width: 20px;
  height: 20px;
}

.acc-panel-body {
  padding: 15px;
  max-height: 60vh;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.acc-item {
  background-color: #f8f9fa;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px;
  text-align: center;
  cursor: pointer;
  font-size: 13px;
  color: #333;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.acc-item:hover {
  background-color: #e9ecef;
}

.acc-item.acc-active-item {
  background-color: #0056b3;
  color: white;
  border-color: #004494;
}

.acc-item svg {
  width: 20px;
  height: 20px;
  margin-bottom: 5px;
  flex-shrink: 0;
}

.acc-item-text {
  line-height: 1.2;
  margin-bottom: 5px;
}

.acc-dots-container {
  display: flex;
  gap: 4px;
  justify-content: center;
  margin-top: auto;
}

.acc-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: #ddd;
  border: 1px solid #ccc;
}

.acc-active-item .acc-dot {
  background-color: rgba(255, 255, 255, 0.4);
  border-color: rgba(255, 255, 255, 0.6);
}

.acc-active-item .acc-dot.acc-dot-active {
  background-color: #fff;
  border-color: #fff;
  box-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}

.acc-item.acc-active-item .acc-dot-active {
  background-color: #fff;
}

.acc-reset-btn {
  grid-column: span 2;
  background-color: #dc3545;
  color: white;
  border: none;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: bold;
  cursor: pointer;
  margin-top: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.acc-reset-btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.acc-reset-btn:hover {
  background-color: #c82333;
}

/* ==========================================================
   Accessibility Feature Classes (Applied to body/html)
========================================================== */

/* 1. Contrast */
body.acc-contrast-invert>*:not(#acc-module-btn):not(#acc-module-panel):not(#acc-mode-elements) {
  filter: invert(100%) hue-rotate(180deg) !important;
}

body.acc-contrast-invert {
  background-color: #fff !important;
}

/* Remove old re-invert logic as it's no longer needed with the new strategy */
body.acc-contrast-invert img:not(#acc-module-panel *) {
  filter: invert(100%) hue-rotate(180deg) !important;
}

body.acc-contrast-dark,
body.acc-contrast-dark *:not(#acc-module-panel):not(#acc-module-panel *):not(#acc-mode-elements):not(#acc-reading-mask):not(#acc-reading-guide) {
  background-color: #000000 !important;
  color: #ffff00 !important;
  border-color: #ffff00 !important;
}

body.acc-contrast-light,
body.acc-contrast-light *:not(#acc-module-panel):not(#acc-module-panel *):not(#acc-mode-elements):not(#acc-reading-mask):not(#acc-reading-guide) {
  background-color: #ffffff !important;
  color: #000000 !important;
  border-color: #000000 !important;
}

/* 2. Highlight Links */
body.acc-highlight-links a:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *),
body.acc-highlight-links button:not(.acc-reset-btn):not(.acc-item):not(#acc-module-btn):not(#acc-module-panel *) {
  text-decoration: underline !important;
  background-color: #ffff00 !important;
  color: #000000 !important;
  font-weight: bold !important;
}

/* 3. Text Size - handled via JS root font-size scaling (document.documentElement.style.fontSize).
   rem/em elements scale proportionally; px-hardcoded large headings are unaffected naturally. */

/* 4. Letter Spacing - 3 levels */
body.acc-letter-spacing-1 *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  letter-spacing: 0.05em !important;
  word-spacing: 0.08em !important;
}

body.acc-letter-spacing-2 *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  letter-spacing: 0.1em !important;
  word-spacing: 0.14em !important;
}

body.acc-letter-spacing-3 *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  letter-spacing: 0.16em !important;
  word-spacing: 0.22em !important;
}

/* 5. Stop Animations */
body.acc-stop-animations,
body.acc-stop-animations * {
  animation: none !important;
  transition: none !important;
  scroll-behavior: auto !important;
}

/* 6. Hide Images */
body.acc-hide-images img,
body.acc-hide-images svg,
body.acc-hide-images picture,
body.acc-hide-images video,
body.acc-hide-images canvas,
body.acc-hide-images iframe {
  display: none !important;
}

body.acc-hide-images *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  background-image: none !important;
}

/* 7. Dyslexia Font */
@font-face {
  font-family: "OpenDyslexic";
  src: url("https://raw.githubusercontent.com/antijingoist/open-dyslexic/master/compiled/OpenDyslexic-Regular.otf") format("opentype");
}

body.acc-dyslexia-font *:not(i):not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  font-family:
    "OpenDyslexic",
    Comic Sans MS,
    sans-serif !important;
}

/* 8. Big Cursor & Reading Guides */
body.acc-big-cursor-active,
body.acc-big-cursor-active * {
  cursor:
    url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"><path d="M8,4 L8,38 L16,30 L22,44 L27,42 L21,28 L32,28 Z" fill="%23111111" stroke="white" stroke-width="2.5" stroke-linejoin="round"/></svg>') 8 4,
    auto !important;
}

body.acc-white-cursor-active,
body.acc-white-cursor-active * {
  cursor:
    url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"><path d="M8,4 L8,38 L16,30 L22,44 L27,42 L21,28 L32,28 Z" fill="white" stroke="%23111111" stroke-width="2.5" stroke-linejoin="round"/></svg>') 8 4,
    auto !important;
}

#acc-reading-guide {
  position: fixed;
  left: 0;
  width: 100%;
  height: 4px;
  background-color: #ff0000;
  z-index: 9999997;
  pointer-events: none;
  display: none;
}

#acc-focus-top,
#acc-focus-bottom {
  position: fixed;
  left: 0;
  width: 100%;
  background: rgba(0, 0, 0, 0.75);
  z-index: 9999996;
  pointer-events: none;
  display: none;
}

#acc-focus-top {
  top: 0;
  height: 0;
}

#acc-focus-bottom {
  top: 0;
  bottom: 0;
}

#acc-reading-mask {
  position: fixed;
  width: 800px;
  height: 500px;
  background: transparent !important;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.7);
  z-index: 9999997;
  pointer-events: none;
  display: none;
  border: 2px solid rgba(255, 255, 255, 0.3);
}

/* 9. Line Height - relative to page's natural line-height (set via --acc-lh JS variable) */
body.acc-line-height-1 *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *),
body.acc-line-height-2 *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *),
body.acc-line-height-3 *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  line-height: var(--acc-lh) !important;
}

/* 10. Text Align */
body.acc-text-align-left *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  text-align: left !important;
}

body.acc-text-align-center *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  text-align: center !important;
}

body.acc-text-align-right *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  text-align: right !important;
}

body.acc-text-align-justify *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  text-align: justify !important;
}

/* 11. Bold Text */
body.acc-bold-text *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *) {
  font-weight: bold !important;
}

/* 12. Saturation */
/* #acc-mode-elements excluded: filter on a parent breaks position:fixed for children (tooltip) */
body.acc-saturation-high *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *):not(#acc-mode-elements):not(#acc-mode-elements *) {
  filter: saturate(200%) !important;
}

body.acc-saturation-low *:not(#acc-module-btn):not(#acc-module-panel):not(#acc-module-panel *):not(#acc-mode-elements):not(#acc-mode-elements *) {
  filter: grayscale(100%) !important;
}

/* Ensure the module itself stays above and unaffected */
#acc-module-btn,
#acc-module-panel {
  z-index: 2147483647 !important;
  pointer-events: auto !important;
}

/* SVG icons inside the module should not be affected by hide-images */
#acc-module-btn svg,
#acc-module-panel svg {
  display: inline-block !important;
}

/* Alt Text Helper Tooltip */
#acc-alt-tooltip {
  position: fixed;
  background: rgba(0, 0, 0, 0.82);
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 18px;
  line-height: 1.4;
  pointer-events: none;
  display: none;
  z-index: 2147483646;
  max-width: 320px;
  white-space: normal;
  word-wrap: break-word;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
}

/* Alt tooltip — contrast mode overrides */
body.acc-contrast-dark #acc-alt-tooltip {
  background: #000 !important;
  color: #ffff00 !important;
  border: 1px solid #ffff00;
}

body.acc-contrast-light #acc-alt-tooltip {
  background: #fff !important;
  color: #000 !important;
  border: 1px solid #000;
}

body.acc-contrast-invert #acc-alt-tooltip {
  filter: invert(100%) hue-rotate(180deg) !important;
}

/* Alt tooltip — text size overrides */
body.acc-text-size-1 #acc-alt-tooltip {
  font-size: calc(18px * 1.2) !important;
}

body.acc-text-size-2 #acc-alt-tooltip {
  font-size: calc(18px * 1.4) !important;
}

body.acc-text-size-3 #acc-alt-tooltip {
  font-size: calc(18px * 1.6) !important;
}

/* Corner Positioning Classes */
/* 0: Bottom-Left (Default) */
body.acc-pos-0 #acc-module-btn {
  bottom: 20px;
  left: 20px;
  right: auto;
  top: auto;
}

body.acc-pos-0 #acc-module-panel {
  bottom: 90px;
  left: 20px;
  right: auto;
  top: auto;
}

/* 1: Top-Left */
body.acc-pos-1 #acc-module-btn {
  top: 20px;
  left: 20px;
  right: auto;
  bottom: auto;
}

body.acc-pos-1 #acc-module-panel {
  top: 90px;
  left: 20px;
  right: auto;
  bottom: auto;
}

/* 2: Top-Right */
body.acc-pos-2 #acc-module-btn {
  top: 20px;
  right: 20px;
  left: auto;
  bottom: auto;
}

body.acc-pos-2 #acc-module-panel {
  top: 90px;
  right: 90px;
  left: auto;
  bottom: auto;
}

/* 3: Bottom-Right */
body.acc-pos-3 #acc-module-btn {
  bottom: 80px;
  right: 20px;
  left: auto;
  top: auto;
}

body.acc-pos-3 #acc-module-panel {
  bottom: 90px;
  right: 100px;
  left: auto;
  top: auto;
}

/* Panel Size Classes */
body.acc-panel-size-1 #acc-module-panel {
  transform: scale(1.15);
  transform-origin: bottom left;
}

body.acc-panel-size-2 #acc-module-panel {
  transform: scale(1.3);
  transform-origin: bottom left;
}

/* Adjust transform origin based on position */
body.acc-pos-1.acc-panel-size-1 #acc-module-panel,
body.acc-pos-1.acc-panel-size-2 #acc-module-panel {
  transform-origin: top left;
}

body.acc-pos-2.acc-panel-size-1 #acc-module-panel,
body.acc-pos-2.acc-panel-size-2 #acc-module-panel {
  transform-origin: top right;
}

body.acc-pos-3.acc-panel-size-1 #acc-module-panel,
body.acc-pos-3.acc-panel-size-2 #acc-module-panel {
  transform-origin: bottom right;
}

body.acc-panel-size-1.acc-pos-1 #acc-module-panel {
  transform: scale(1.15);
}

body.acc-panel-size-2.acc-pos-1 #acc-module-panel {
  transform: scale(1.3);
}

body.acc-panel-size-1.acc-pos-2 #acc-module-panel {
  transform: scale(1.15);
}

body.acc-panel-size-2.acc-pos-2 #acc-module-panel {
  transform: scale(1.3);
}

body.acc-panel-size-1.acc-pos-3 #acc-module-panel {
  transform: scale(1.15);
}

body.acc-panel-size-2.acc-pos-3 #acc-module-panel {
  transform: scale(1.3);
}

/* Ensure body doesn't clip the module */
html,
body {
  overflow-x: hidden;
}

/* Mobile Responsive Improvements */
@media (max-width: 768px) {
  #acc-module-panel {
    width: 90vw;
    max-width: 320px;
    left: 50% !important;
    right: auto !important;
    transform: translateX(-50%);
    bottom: 90px !important;
    top: auto !important;
  }

  body.acc-pos-0 #acc-module-panel,
  body.acc-pos-1 #acc-module-panel,
  body.acc-pos-2 #acc-module-panel,
  body.acc-pos-3 #acc-module-panel {
    left: 50% !important;
    right: auto !important;
    transform: translateX(-50%);
    bottom: 90px !important;
    top: auto !important;
  }

  #acc-module-btn {
    width: 50px;
    height: 50px;
    font-size: 24px;
    bottom: 20px !important;
    left: 50% !important;
    right: auto !important;
    top: auto !important;
    transform: translateX(-50%);
  }

  body.acc-pos-0 #acc-module-btn,
  body.acc-pos-1 #acc-module-btn,
  body.acc-pos-2 #acc-module-btn,
  body.acc-pos-3 #acc-module-btn {
    left: 50% !important;
    right: auto !important;
    top: auto !important;
    bottom: 20px !important;
    transform: translateX(-50%);
  }

  .acc-panel-body {
    max-height: 50vh;
    font-size: 12px;
  }

  .acc-item {
    padding: 8px 5px;
    font-size: 11px;
  }

  .acc-item svg {
    width: 18px;
    height: 18px;
  }
}

@media (max-width: 480px) {
  #acc-module-panel {
    width: 95vw;
  }

  .acc-panel-body {
    padding: 10px;
    gap: 8px;
    max-height: 45vh;
  }

  .acc-item {
    padding: 6px 4px;
    font-size: 10px;
  }

  .acc-item svg {
    width: 16px;
    height: 16px;
    margin-bottom: 3px;
  }

  .acc-panel-header {
    padding: 12px;
    font-size: 16px;
  }

  .acc-reset-btn {
    padding: 10px;
    font-size: 13px;
  }
}