/* =========================================================================
   AI Text to Speech – front-end "read aloud" reader
   Clean pill button that expands into a seekable audio track when playing.
   ========================================================================= */

.ai-tts-reader {
    /* Theming tokens (accent is overridden inline per site settings) */
    --ai-tts-accent: #0073aa;
    --ai-tts-surface: #ffffff;
    --ai-tts-border: rgba(0, 0, 0, 0.10);
    --ai-tts-text: #1a1a2e;
    --ai-tts-muted: #6b7280;
    --ai-tts-rail: rgba(0, 0, 0, 0.10);

    display: block;
    width: 100%;
    max-width: 790px;
    margin: 0 0 14px;
    box-sizing: border-box;
    font-family: inherit;
    -webkit-font-smoothing: antialiased;
}

.ai-tts-reader *,
.ai-tts-reader *::before,
.ai-tts-reader *::after {
    box-sizing: border-box;
}

/* The visually-hidden native audio element */
.ai-tts-reader .ai-tts-audio {
    display: none;
}

/* ---- The bar ----------------------------------------------------------- */
/* Padding and border are reserved in EVERY state, so when playback starts the
   box keeps exactly the same size — only the surface colours fade in, the
   geometry never changes and nothing below jumps. */
.ai-tts-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    border: 1px solid transparent;
    border-radius: 999px;
    padding: 6px 10px 6px 6px;
    background: transparent;
    transition: background-color .25s ease, border-color .25s ease;
}

/* When active (playing or paused), only the surface + border colours fade in.
   No box-shadow — the reader stays flat and clean. */
.ai-tts-reader[data-state="playing"] .ai-tts-bar,
.ai-tts-reader[data-state="paused"] .ai-tts-bar {
    background: var(--ai-tts-surface);
    border-color: var(--ai-tts-border);
}

/* ---- Primary toggle (play / pause / resume) ---------------------------- */
.ai-tts-toggle {
    display: inline-flex;
    align-items: center;
    gap: 9px;
    min-height: 44px;
    padding: 0 20px 0 17px;
    margin: 0;
    border: 0;
    border-radius: 999px;
    background: var(--ai-tts-accent);
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    white-space: nowrap;
    -webkit-appearance: none;
    appearance: none;
    box-shadow: none;
    transition: background-color .18s ease, transform .08s ease;
}

/* Stay flat on every interaction — neutralises theme button shadows/recolours.
   The extra .ai-tts-reader scope raises specificity above typical
   ".entry-content button:hover" theme rules without needing !important. */
.ai-tts-reader .ai-tts-toggle:hover,
.ai-tts-reader .ai-tts-toggle:focus,
.ai-tts-reader .ai-tts-toggle:active {
    color: #fff;
    box-shadow: none;
    text-decoration: none;
}

/* Clean "darken on hover / press" instead of a washed-out brightness filter */
.ai-tts-reader .ai-tts-toggle:hover {
    background: color-mix(in srgb, var(--ai-tts-accent) 90%, #000);
}

.ai-tts-reader .ai-tts-toggle:active {
    background: color-mix(in srgb, var(--ai-tts-accent) 82%, #000);
    transform: translateY(1px);
}

.ai-tts-reader .ai-tts-toggle:focus-visible {
    outline: 2px solid var(--ai-tts-accent);
    outline-offset: 2px;
}

/* The toggle keeps the exact same size in every state, so starting or pausing
   playback never resizes the button or nudges the surrounding layout. */

/* Icon swapping by state */
.ai-tts-toggle .ai-tts-ico {
    display: none;
    align-items: center;
    justify-content: center;
}

.ai-tts-reader[data-state="idle"] .ai-tts-ico-play,
.ai-tts-reader[data-state="paused"] .ai-tts-ico-play,
.ai-tts-reader[data-state="playing"] .ai-tts-ico-pause,
.ai-tts-reader[data-state="loading"] .ai-tts-ico-spin {
    display: inline-flex;
}

/* Reserve room for the widest label so swapping the text keeps the button a
   constant width and the time read-out next to it never shifts.
   (em scales with each height variant's font-size.) */
.ai-tts-toggle .ai-tts-label {
    display: inline-block;
    min-width: 5.5em;
}

/* ---- Time labels ------------------------------------------------------- */
.ai-tts-time {
    font-variant-numeric: tabular-nums;
    font-size: 13px;
    color: var(--ai-tts-muted);
    min-width: 38px;
    text-align: center;
    flex: 0 0 auto;
}

/* ---- Seek / progress track --------------------------------------------- */
.ai-tts-seek {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    min-height: 38px;        /* generous hit area for touch / mouse */
    cursor: pointer;
    touch-action: none;
}

.ai-tts-rail {
    position: relative;
    width: 100%;
    height: 6px;
    border-radius: 999px;
    background: var(--ai-tts-rail);
}

.ai-tts-buffered {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0;
    border-radius: 999px;
    background: currentColor;
    color: var(--ai-tts-accent);
    opacity: 0.18;
}

.ai-tts-fill {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0;
    border-radius: 999px;
    background: var(--ai-tts-accent);
}

.ai-tts-thumb {
    position: absolute;
    top: 50%;
    right: 0;
    width: 14px;
    height: 14px;
    transform: translate(50%, -50%);
    border-radius: 50%;
    background: var(--ai-tts-accent);
    box-shadow: 0 0 0 3px var(--ai-tts-surface), 0 1px 3px rgba(0, 0, 0, 0.3);
    opacity: 0;
    transition: opacity .15s ease;
}

.ai-tts-seek:hover .ai-tts-thumb,
.ai-tts-seek:focus-visible .ai-tts-thumb,
.ai-tts-reader[data-state="paused"] .ai-tts-thumb {
    opacity: 1;
}

.ai-tts-seek:focus-visible {
    outline: 2px solid var(--ai-tts-accent);
    outline-offset: 4px;
    border-radius: 999px;
}

/* ---- Stop / collapse button -------------------------------------------- */
.ai-tts-stop {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    flex: 0 0 auto;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--ai-tts-muted);
    cursor: pointer;
    transition: background-color .2s ease, color .2s ease;
}

.ai-tts-stop:hover {
    background: var(--ai-tts-rail);
    color: var(--ai-tts-text);
}

.ai-tts-stop:focus-visible {
    outline: 2px solid var(--ai-tts-accent);
    outline-offset: 2px;
}

/* ---- State visibility: only show the track once it's running ----------- */
.ai-tts-reader[data-state="idle"] .ai-tts-current,
.ai-tts-reader[data-state="idle"] .ai-tts-seek,
.ai-tts-reader[data-state="idle"] .ai-tts-duration,
.ai-tts-reader[data-state="idle"] .ai-tts-stop,
.ai-tts-reader[data-state="loading"] .ai-tts-current,
.ai-tts-reader[data-state="loading"] .ai-tts-seek,
.ai-tts-reader[data-state="loading"] .ai-tts-duration,
.ai-tts-reader[data-state="loading"] .ai-tts-stop {
    display: none;
}

/* Reveal animation for the expanding controls */
.ai-tts-reader[data-state="playing"] .ai-tts-current,
.ai-tts-reader[data-state="playing"] .ai-tts-seek,
.ai-tts-reader[data-state="playing"] .ai-tts-duration,
.ai-tts-reader[data-state="playing"] .ai-tts-stop,
.ai-tts-reader[data-state="paused"] .ai-tts-current,
.ai-tts-reader[data-state="paused"] .ai-tts-seek,
.ai-tts-reader[data-state="paused"] .ai-tts-duration,
.ai-tts-reader[data-state="paused"] .ai-tts-stop {
    animation: ai-tts-fade-in .28s ease both;
}

@keyframes ai-tts-fade-in {
    from { opacity: 0; transform: translateY(2px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* Spinner */
.ai-tts-spinner {
    animation: ai-tts-spin 0.8s linear infinite;
    transform-origin: center;
}

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

/* ---- Height variants --------------------------------------------------- */
.ai-tts-reader.ai-tts-h-medium .ai-tts-toggle { min-height: 48px; font-size: 16px; }

.ai-tts-reader.ai-tts-h-compact .ai-tts-toggle { min-height: 38px; font-size: 14px; padding: 0 16px 0 14px; }
.ai-tts-reader.ai-tts-h-compact .ai-tts-seek { min-height: 34px; }

/* ---- Dark mode --------------------------------------------------------- */
@media (prefers-color-scheme: dark) {
    .ai-tts-reader {
        --ai-tts-surface: #1f2430;
        --ai-tts-border: rgba(255, 255, 255, 0.12);
        --ai-tts-text: #f3f4f6;
        --ai-tts-muted: #9aa3b2;
        --ai-tts-rail: rgba(255, 255, 255, 0.16);
    }
}

/* ---- Responsive -------------------------------------------------------- */
@media (max-width: 480px) {
    .ai-tts-bar { gap: 8px; }
    .ai-tts-time { font-size: 12px; min-width: 34px; }
    .ai-tts-reader[data-state="playing"] .ai-tts-label,
    .ai-tts-reader[data-state="paused"] .ai-tts-label {
        /* Keep the label but allow it to be dropped on very tight layouts */
        max-width: 92px;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

/* ---- Reduced motion ---------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
    .ai-tts-bar,
    .ai-tts-toggle,
    .ai-tts-thumb,
    .ai-tts-stop {
        transition: none;
    }
    .ai-tts-reader[data-state] .ai-tts-current,
    .ai-tts-reader[data-state] .ai-tts-seek,
    .ai-tts-reader[data-state] .ai-tts-duration,
    .ai-tts-reader[data-state] .ai-tts-stop {
        animation: none;
    }
    .ai-tts-spinner {
        animation-duration: 1.6s;
    }
}
