/* This file is part of Moodle - https://moodle.org/

Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Moodle.  If not, see <https://www.gnu.org/licenses/>. */
/**
 * Plugin version and other meta-data are defined here.
 *
 * @package     local_accessibility
 * @copyright   2023 Ponlawat Weerapanpisit <ponlawat_w@outlook.co.th>
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/* ============================================================
   Profile CSS-only effects (highcontrast, highsaturation,
   lowsaturation, stopanimations, bigcursor) + reading guide/mask.
   ============================================================ */

/* Filter is applied to #page (Moodle's content wrapper), not body.
   The FAB/panel are moved to be direct children of <body> precisely
   to escape page-level filters — putting the filter on body itself
   would make body a new containing block for their position:fixed,
   breaking their viewport-relative positioning. */
#page.accessibility-highcontrast {
    filter: contrast(1.6) !important;
}

#page.accessibility-highsaturation {
    filter: saturate(2.2) !important;
}

#page.accessibility-lowsaturation {
    filter: saturate(0.35) !important;
}

/* CSS filter on #page makes #page the containing block for any
   position:absolute/fixed descendant that previously had none closer
   (filter behaves like transform for this purpose). Elements positioned
   with top+bottom (e.g. mod_assign's grader side panel) compute their
   height from their containing block's height; if #page is shorter than
   the viewport, that height calculation goes wrong (often to ~0),
   hiding the content. Rather than patch each affected Moodle panel
   individually with hardcoded offsets (fragile — those offsets vary by
   layout/breakpoint), just make #page's own height match the viewport
   it would have had as a containing block without the filter. This is
   pure CSS, no DOM changes, so it can't break Atto/event delegation the
   way moving panels out of #page did. */
#page.accessibility-highcontrast,
#page.accessibility-highsaturation,
#page.accessibility-lowsaturation {
    min-height: 100vh;
}

/* Snap animations/transitions to their end state instead of pausing
   mid-frame — pausing can freeze fade-in content (e.g. SCORM/Storyline
   slides) at opacity:0, making it look like nothing rendered. */
body.accessibility-stopanimations *,
body.accessibility-stopanimations *::before,
body.accessibility-stopanimations *::after {
    animation-duration: 0.01ms !important;
    animation-delay: -1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0s !important;
    scroll-behavior: auto !important;
}

body.accessibility-bigcursor,
body.accessibility-bigcursor * {
    cursor: url("data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='48' height='48' viewBox='0 0 24 24'%3E%3Cpath d='M3 2l7.07 17.97 2.51-7.39 7.39-2.51z' fill='%23000' stroke='%23fff' stroke-width='1.2'/%3E%3C/svg%3E") 4 4, auto !important;
}

/* Reading guide: horizontal line that follows the cursor */
.acc-readingguide-el {
    position: fixed;
    left: 0;
    width: 100%;
    height: 2.5rem;
    margin-top: -1.25rem;
    background: rgba(255, 230, 0, 0.35);
    border-top: 2px solid rgba(220, 170, 0, 0.8);
    border-bottom: 2px solid rgba(220, 170, 0, 0.8);
    pointer-events: none;
    z-index: 100000;
}

/* Reading mask: dims everything except a band around the cursor */
.acc-mask {
    position: fixed;
    left: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.65);
    pointer-events: none;
    z-index: 99999;
}

.acc-masktop {
    top: 0;
}

.acc-maskbottom {
    bottom: 0;
}

.local-accessibility-buttoncontainer {
    position: fixed;
    bottom: 130px;
    right: 20px;
    z-index: 1039;
}
body.dir-rtl .local-accessibility-buttoncontainer {
    right: unset;
    left: 2rem;
}

#page.drawers.show-drawer-right .local-accessibility-buttoncontainer {
    right: calc(315px + 20px);
}
body.dir-rtl #page.drawers.show-drawer-right .local-accessibility-buttoncontainer {
    right: unset;
    left: calc(315px + 20px);
}

#local-accessibility-buttoncontainer button {
    font-size: 16px;
    width: 36px;
    height: 36px;
    line-height: 16px;
    padding: 0;
    text-align: center;
    border-radius: 50%;
}

.local-accessibility-panel {
    display: none;
    position: fixed;
    bottom: 8rem;
    right: 2rem;
    z-index: 1040;
    box-shadow: 0 4px 24px rgba(20, 38, 145, 0.25) !important;
    border: 2px solid #0046b8 !important;
    border-radius: 12px !important;
    overflow: hidden;
    flex-direction: column;
    /* Without a height cap, flex children never overflow, so .card-body's
       overflow-y:auto never kicks in — the panel just grows past the
       viewport instead of scrolling internally. */
    max-height: calc(100vh - 10rem);
}

/* Override theme .card radius on panel */
.local-accessibility-panel.card {
    border-radius: 12px !important;
}
body.dir-rtl .local-accessibility-panel {
    right: unset;
    left: 2rem;
}

/* Header icon — SVG has no intrinsic width/height, only viewBox. */
.local-accessibility-panel .uveg-a11y-icon-header {
    width: 1.25rem;
    height: 1.25rem;
    flex: none;
}

/* Widget icons — source pix/icon.svg files ship hardcoded width/height
   (e.g. 800px); CSS width/height on the inlined <svg> overrides those
   presentation attributes. */
.local-accessibility-panel .acc-widget-icon {
    display: inline-flex;
    width: 1.1rem;
    height: 1.1rem;
    flex: none;
}
.local-accessibility-panel .acc-widget-icon svg {
    width: 100%;
    height: 100%;
}

/* FIX #2: panel radius 12px to match widget cards */
.local-accessibility-panel > .card-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    background: #fff;
    padding: 1rem;
}

.local-accessibility-panel .card-header {
    background: #0046b8 !important;
    color: #fff !important;
    border-bottom: none;
    border-radius: 0;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.local-accessibility-panel .card-header .acc-header-left {
    color: #fff;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex: 1 1 auto;
    min-width: 0;
}

.local-accessibility-panel .card-header .acc-header-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex: none;
    white-space: nowrap;
}

.local-accessibility-panel .card-header .acc-reset-link,
.local-accessibility-panel .card-header .acc-close-link {
    color: rgba(255, 255, 255, 0.8);
    white-space: nowrap;
}

.local-accessibility-panel .card-header .acc-reset-link:hover,
.local-accessibility-panel .card-header .acc-close-link:hover {
    color: #fff;
}

/* FIX #4: section headers align with body padding */
.local-accessibility-panel .acc-section-header {
    background: transparent;
    color: #0046b8;
    font-weight: 600;
    padding: 0.6rem 0;
    border-bottom: 1px solid rgba(20, 38, 145, 0.15);
}

/* FIX #2 + #6 + #7: widget cards — 12px radius, subtle shadow, proper padding */
.local-accessibility-panel .acc-widget-card {
    background: #f0f4ff !important;
    border: 1px solid rgba(20, 38, 145, 0.12) !important;
    border-radius: 0.75rem !important;
    margin-bottom: 0.5rem;
    box-shadow: 0 1px 4px rgba(20, 38, 145, 0.08);
    padding: 0.75rem !important;
}

.local-accessibility-panel .acc-widget-inner {
    padding: 0;
}

.local-accessibility-panel .acc-widget-title strong {
    color: #0046b8;
}

/* Active pills */
.local-accessibility-panel .acc-pill.active,
.local-accessibility-panel .btn-toggler.btn-primary {
    background: #0046b8 !important;
    border-color: #0046b8 !important;
    color: #fff !important;
}

/* FIX #1: inactive pills use same bg as widget cards, not theme celeste */
.local-accessibility-panel .acc-pill:not(.active) {
    background: #fff !important;
    border: 1px solid rgba(20, 38, 145, 0.3) !important;
    border-radius: 20px !important;
    color: #0046b8;
}

.local-accessibility-panel .acc-pill:not(.active):hover {
    background: rgba(20, 38, 145, 0.08) !important;
}

/* FIX #6: font face buttons — pill shape to match other pills */
/* Override theme .btn-light inside widget cards */
html body .local-accessibility-panel .acc-widget-card .btn-light,
html body .local-accessibility-panel .accessibility_fontface-classbtn,
html body .local-accessibility-panel .accessibility_fontface-resetbtn {
    background: #fff !important;
    border: 1px solid rgba(20, 38, 145, 0.3) !important;
    border-radius: 20px !important;
    color: #0046b8 !important;
    padding: 0.25rem 1rem !important;
    transition: background 0.15s ease;
}

html body .local-accessibility-panel .accessibility_fontface-classbtn:hover,
html body .local-accessibility-panel .accessibility_fontface-resetbtn:hover {
    background: rgba(20, 38, 145, 0.08) !important;
}

html body .local-accessibility-panel .accessibility_fontface-classbtn.active,
html body .local-accessibility-panel .accessibility_fontface-classbtn.btn-primary {
    background: #0046b8 !important;
    border-color: #0046b8 !important;
    color: #fff !important;
}

body.path-mod-scorm #region-main > h2 {
    display: none !important;
}

/* Botón scroll-to-top — posicionado dentro del buttonContainer, al lado del FAB */
#a11y-scroll-top {
    position: absolute;
    top: 0;
    /* FAB en derecha (default): scroll-top a la izquierda */
    right: calc(100% + 10px);
    left: auto;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #0046b8;
    border: 2px solid #0046b8;
    color: #fff;
    font-size: 16px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, background 0.15s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    z-index: 1;
}
/* FAB en izquierda: scroll-top a la derecha con más espacio */
#local-accessibility-buttoncontainer.acc-open-right #a11y-scroll-top {
    left: calc(100% + 10px);
    right: auto;
}
#a11y-scroll-top.a11y-scroll-top-show {
    opacity: 1;
    pointer-events: auto;
}
#a11y-scroll-top:hover {
    background: #1c61ac;
    border-color: #1c61ac;
}

/* Active profile indicator — green dot at top-right of FAB */
.acc-active-indicator {
    display: none;
    position: absolute;
    top: -3px;
    right: -3px;
    width: 13px;
    height: 13px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #fff;
    z-index: 3;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
    cursor: default;
}

.acc-active-indicator.visible {
    display: block;
    animation: acc-indicator-pulse 2.4s ease-in-out infinite;
}

@keyframes acc-indicator-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55), 0 1px 4px rgba(0,0,0,0.25); }
    50%       { box-shadow: 0 0 0 5px rgba(34, 197, 94, 0),   0 1px 4px rgba(0,0,0,0.25); }
}

/* CSS tooltip — appears above the dot on hover */
.acc-active-indicator::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 7px);
    right: -4px;
    background: #1a1a2e;
    color: #fff;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.4;
    padding: 5px 10px;
    border-radius: 6px;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translateY(4px);
    transition: opacity 0.15s ease, transform 0.15s ease;
}

/* Small arrow pointing down from tooltip */
.acc-active-indicator::before {
    content: '';
    position: absolute;
    bottom: calc(100% + 1px);
    right: 3px;
    border: 5px solid transparent;
    border-top-color: #1a1a2e;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s ease;
}

.acc-active-indicator:hover::after,
.acc-active-indicator:hover::before {
    opacity: 1;
    transform: translateY(0);
}

.h5p-embed .local-accessibility-buttoncontainer {
    display: none;
}

/* ============================================================
   Floating profile quick-access buttons (FAB expansion)
   ============================================================ */

/* FIX #5: FAB main button — solid UVEG blue, no gradient, consistent with floating btns */
html body #local-accessibility-buttoncontainer .acc-fab-main {
    position: relative;
    z-index: 2;
    background: #0046b8 !important;
    border: 2px solid #0046b8 !important;
    box-shadow: 0 4px 14px rgba(20, 38, 145, 0.35) !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-main:hover {
    background: #1c61ac !important;
    border-color: #1c61ac !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-main svg * {
    stroke-width: 33 !important;
}

/* Constrain container to button size so abs menu doesn't expand it.
   UVEG: width:auto NO restringe (el contenedor se inflaba a ~viewport y el menú
   absoluto, anclado a right:0/bottom:100% del contenedor, se despegaba del FAB al
   reposicionarlo cerca del centro). Fijar al tamaño del botón hace que el menú
   ancle al botón. overflow:visible deja que el menú sobresalga. */
#local-accessibility-buttoncontainer.acc-fab-group {
    width: 44px !important;
    height: 44px !important;
    overflow: visible;
}

/* Menu container — stacks upward from FAB (default) */
.acc-fab-menu {
    position: absolute;
    bottom: 100%;
    top: auto;
    right: 0;
    display: flex;
    flex-direction: column-reverse;
    align-items: flex-end;
    gap: 0.5rem;
    padding-bottom: 0.5rem;
    padding-top: 0;
    pointer-events: none;
    z-index: 1;
}

/* Cuando no hay espacio arriba: abrir hacia abajo */
.acc-fab-group.acc-open-down .acc-fab-menu {
    bottom: auto;
    top: 100%;
    flex-direction: column;
    padding-bottom: 0;
    padding-top: 0.5rem;
    align-items: flex-end;
}

/* Each item = button + label */
.acc-fab-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    opacity: 0;
    transform: scale(0.5) translateY(10px);
    transition: opacity 0.2s ease, transform 0.2s ease;
    transition-delay: calc(var(--btn-index, 0) * 30ms);
    pointer-events: none;
}

/* Expanded state */
.acc-fab-group.expanded .acc-fab-menu {
    pointer-events: auto;
}

.acc-fab-group.expanded .acc-fab-item {
    opacity: 1;
    transform: scale(1) translateY(0);
    pointer-events: auto;
}

/* Collapse animation — reverse stagger */
.acc-fab-group:not(.expanded) .acc-fab-item {
    transition-delay: calc((8 - var(--btn-index, 0)) * 20ms);
}

/* Round floating button — !important to override theme Focus !important on button tag */
html body #local-accessibility-buttoncontainer .acc-fab-btn {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    border-radius: 50% !important;
    border: 2px solid #0046b8 !important;
    background: #fff !important;
    color: #0046b8 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    cursor: pointer;
    font-size: 16px !important;
    line-height: 1 !important;
    padding: 0 !important;
    transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15) !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-btn:hover,
html body #local-accessibility-buttoncontainer .acc-fab-btn:focus {
    background: #0046b8 !important;
    color: #fff !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-btn:focus:not(:hover):not(.active) {
    background: #fff !important;
    color: #0046b8 !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-btn:focus-visible {
    outline: 2px solid #1c61ac;
    outline-offset: 2px;
}

/* Active state for profiles/magnifier — distinct from hover with white ring */
html body #local-accessibility-buttoncontainer .acc-fab-btn.active {
    background: #0046b8 !important;
    color: #fff !important;
    border: 3px solid #fff !important;
    box-shadow: 0 0 0 2px #0046b8, 0 2px 12px rgba(20, 38, 145, 0.4) !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-btn.active:hover {
    background: #1c61ac !important;
    border-color: #fff !important;
}

/* SVG icons inside floating buttons — override theme stroke-width: 60 on container svgs */
html body #local-accessibility-buttoncontainer .acc-fab-btn svg {
    width: 20px !important;
    height: 20px !important;
}

html body #local-accessibility-buttoncontainer .acc-fab-btn svg * {
    stroke-width: 2 !important;
}

.acc-fab-btn i.fa {
    font-size: 16px;
    line-height: 1;
}

/* Left-side label pill */
.acc-fab-label {
    background: #0046b8;
    color: #fff;
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    pointer-events: none;
    user-select: none;
}

/* Responsive: hide labels on small screens */
@media (max-width: 479.98px) {
    .acc-fab-label {
        display: none;
    }
}

/* ============================================================
   UVEG: FAB arrastrable + dirección de apertura por mitad de viewport
   ============================================================ */

/* El botón principal es agarrable; touch-action:none evita scroll al arrastrar. */
html body #local-accessibility-buttoncontainer .acc-fab-main {
    cursor: grab;
    touch-action: none;
}
html body #local-accessibility-buttoncontainer .acc-fab-main.acc-dragging {
    cursor: grabbing;
}

/* Cuando el FAB está en la mitad izquierda (clase puesta por JS) las opciones
   se despliegan hacia la DERECHA: menú alineado a la izquierda del botón y
   etiquetas a la derecha (row-reverse). Sin la clase = comportamiento actual. */
#local-accessibility-buttoncontainer.acc-open-right .acc-fab-menu {
    right: auto;
    left: 0;
    align-items: flex-start;
}
#local-accessibility-buttoncontainer.acc-open-right .acc-fab-item {
    flex-direction: row-reverse;
}
#local-accessibility-buttoncontainer.acc-open-down.acc-open-right .acc-fab-menu {
    align-items: flex-start;
}

