/* Text to Speech for a novel chapter — the floating player (components/reader-tts), the lit speaker buttons and the
   mark on the sentence being read. Driven by assets/scripts/reader-tts.js. Linked from index.php's chapter view for an
   unlocked novel chapter only, after reader-ui.css: the accent (--rd-ac*) and the bar offsets (--rd-bot-off,
   --rd-menu-w) come from there, and so do the slider and the switch inside the panel (.rd-range, .rd-switch).
   Colours are the theme tokens, so the light theme, the colour schemes, the gradient themes and a reader's own accent
   need nothing extra.

   Per-site options (plugins/settings.php) are all wired in this file — see the list at the top of
   components/reader-tts/index.php: UI_ROUND through the --ui-r-* / --ui-rc-* tokens (each with the as-designed value
   as its fallback), UI_CONTROLS, UI_PLAIN and ROW_PANELS in the blocks at the end. */

/* ── The panel: a card floating over the foot of the text, centred on the reading column ── */
#tts-panel {
    position: fixed;
    left: 0;
    right: 0;
    /* phones: above the bottom bar, and down with it when the bars slide away */
    bottom: calc(var(--rd-bot-off, 0px) + .6rem + env(safe-area-inset-bottom, 0px));
    z-index: 53; /* over the bars (50), under the reader menu (54) and the chapters panel (60) */
    width: min(34rem, calc(100% - 1.2rem));
    margin: 0 auto;
    overflow: hidden; /* the progress line and the option rows end at the panel's corners */
    border-radius: var(--ui-rc-2xl, 1rem);
    border: 1px solid rgb(var(--c-white) / .09);
    background: rgb(var(--c-surface-2));
    box-shadow: 0 16px 48px rgba(0, 0, 0, .5), 0 2px 8px rgba(0, 0, 0, .3);
    opacity: 0;
    transform: translateY(12px);
    transition: opacity .2s, transform .24s cubic-bezier(.2, .8, .2, 1), bottom .3s ease,
        right .28s cubic-bezier(.2, .8, .2, 1);
}

#tts-panel,
#tts-panel * {
    box-sizing: border-box;
}

#tts-panel.is-open {
    opacity: 1;
    transform: none;
}

#tts-panel[hidden] {
    display: none;
}

html[data-theme="light"] #tts-panel {
    box-shadow: 0 14px 40px rgba(0, 0, 0, .16), 0 1px 4px rgba(0, 0, 0, .08);
}

@media (min-width: 768px) {
    #tts-panel {
        bottom: 1.25rem;
    }

    /* the docked reader menu / chapters panel takes its slot from the reading column: stay centred on what is left */
    .rd-menu-open #tts-panel,
    html.rd-chapters-open #tts-panel {
        right: var(--rd-menu-w, 24rem);
        width: min(34rem, calc(100% - var(--rd-menu-w, 24rem) - 1.2rem));
    }
}

/* ── Head: icon tile, title over the chapter, how far the reading is, × ── */
.tts-head {
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .7rem .7rem .7rem .8rem;
}

/* the tile: the speaker — and, while the chapter is being read, four bars moving like a level meter */
.tts-tile {
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: var(--ui-r-lg, .6rem);
    background: rgba(var(--rd-ac), .14);
    color: var(--rd-ac-fg);
}

.tts-tile-ico {
    width: 18px;
    height: 18px;
}

.tts-eq {
    display: none;
    align-items: flex-end;
    gap: 2px;
    height: 14px;
}

.tts-eq i {
    width: 3px;
    height: 100%;
    border-radius: 1px;
    background: currentColor;
    transform-origin: 50% 100%;
    animation: tts-eq 1.1s ease-in-out infinite;
}

.tts-eq i:nth-child(1) { animation-delay: -.9s; }
.tts-eq i:nth-child(2) { animation-delay: -.35s; }
.tts-eq i:nth-child(3) { animation-delay: -.65s; }
.tts-eq i:nth-child(4) { animation-delay: -.1s; }

@keyframes tts-eq {
    0%, 100% { transform: scaleY(.3); }
    50% { transform: scaleY(1); }
}

#tts-panel.is-reading .tts-tile-ico {
    display: none;
}

#tts-panel.is-reading .tts-eq {
    display: flex;
}

.tts-head-text {
    flex: 1;
    min-width: 0;
}

.tts-title {
    margin: 0;
    font-size: .875rem;
    font-weight: 700;
    line-height: 1.3;
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .92));
}

.tts-sub {
    margin: .05rem 0 0;
    overflow: hidden;
    font-size: .7rem;
    line-height: 1.35;
    white-space: nowrap;
    text-overflow: ellipsis;
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .45));
}

.tts-pct {
    flex: none;
    font-size: .72rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--rd-ac-light);
}

.tts-pct[hidden] {
    display: none;
}

/* the chapter read so far: a line across the panel under the head, filled in the accent */
.tts-progress {
    height: 3px;
    background: rgb(var(--c-white) / .07);
}

.tts-progress-fill {
    display: block;
    width: 100%;
    height: 100%;
    background: var(--rd-ac-fg);
    transform: scaleX(0);
    transform-origin: 0 50%;
    transition: transform .35s ease;
}

/* ── Icon buttons: ×, back, forward — the reader menu's hairline tiles ── */
.tts-ctl {
    position: relative;
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 1px solid rgb(var(--c-white) / .08);
    border-radius: var(--ui-r-lg, .6rem);
    background: rgb(var(--c-white) / .05);
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .72));
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation; /* quick taps on back / forward step, they don't zoom the page */
    transition: background .15s, color .15s, border-color .15s, opacity .15s;
}

.tts-ctl:hover {
    background: rgb(var(--c-white) / .1);
    color: rgb(var(--c-white));
}

.tts-ctl[aria-disabled="true"] {
    opacity: .38;
    pointer-events: none;
}

.tts-close {
    width: 32px;
    height: 32px;
}

.tts-close svg {
    width: 15px;
    height: 15px;
}

.tts-step {
    width: 44px;
    height: 44px;
}

.tts-step svg {
    width: 18px;
    height: 18px;
}

.tts-body {
    display: flex;
    flex-direction: column;
    gap: .65rem;
    padding: .8rem;
}

/* ── Back · Play · Forward ── */
.tts-transport {
    display: flex;
    align-items: center;
    gap: .5rem;
}

/* Play / Pause: the reader's brand button (the end card's "Next chapter") */
.tts-play {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: .5rem;
    height: 44px;
    padding: 0 1rem;
    border: 1px solid transparent;
    border-radius: var(--ui-r-lg, .6rem);
    background: var(--rd-ac-pill);
    background-origin: border-box;
    color: #fff;
    font: inherit;
    font-size: .875rem;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: filter .15s, transform .1s, background .15s;
}

.tts-play:hover {
    filter: brightness(1.08);
}

.tts-play:active {
    transform: scale(.99);
}

.tts-play:focus-visible,
.tts-ctl:focus-visible {
    outline: 2px solid var(--rd-ac-fg);
    outline-offset: 2px;
}

.tts-ico {
    display: inline-flex;
}

.tts-ico svg {
    width: 16px;
    height: 16px;
}

.tts-play .tts-ico-pause,
.tts-play.is-playing .tts-ico-play {
    display: none;
}

.tts-play.is-playing .tts-ico-pause {
    display: inline-flex;
}

/* a line under the buttons when there is something to say: no voices, speech blocked, the chapter is over */
.tts-note {
    margin: -.15rem .1rem 0;
    font-size: .72rem;
    line-height: 1.45;
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .6));
}

.tts-note[hidden] {
    display: none;
}

/* ── The options: one card, a row each, hairlines between (the reader menu's grouped rows) ── */
.tts-opts {
    overflow: hidden;
    border: 1px solid rgb(var(--c-white) / .08);
    border-radius: var(--ui-rc-lg, .6rem);
    background: rgb(var(--c-white) / .05);
}

.tts-row {
    position: relative;
    display: flex;
    align-items: center;
    gap: .6rem;
    min-height: 42px;
    padding: .3rem .65rem .3rem .55rem;
}

.tts-row + .tts-row {
    border-top: 1px solid rgb(var(--c-white) / .06);
}

.tts-row-ico {
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border-radius: var(--ui-r-md, .45rem);
    background: rgb(var(--c-white) / .06);
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .6));
}

.tts-row-ico svg {
    width: 14px;
    height: 14px;
}

.tts-row-label {
    flex: none;
    font-size: .78rem;
    font-weight: 600;
    line-height: 1.25;
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .78));
}

.tts-row-val {
    min-width: 0;
    overflow: hidden;
    font-size: .76rem;
    font-weight: 600;
    line-height: 1.25;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-variant-numeric: tabular-nums;
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .88));
}

/* Voice: the row is the button — the browser's own list lies over it, unseen */
.tts-row-voice .tts-row-val {
    flex: 1;
    text-align: right;
}

.tts-row-chev {
    flex: none;
    width: 14px;
    height: 14px;
    color: rgb(var(--c-white) / calc(var(--ta-b, 0) + var(--ta-m, 1) * .45));
}

.tts-voice-sel {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 0;
    -webkit-appearance: none;
    appearance: none;
    opacity: 0;
    font: inherit;
    font-size: 16px; /* under 16px iOS zooms the page when the list opens */
    cursor: pointer;
}

.tts-voice-sel:disabled {
    cursor: default;
}

.tts-voice-sel option,
.tts-voice-sel optgroup {
    background: rgb(var(--c-surface));
    color: rgb(var(--c-white));
}

.tts-row-voice {
    transition: background .15s;
}

.tts-row-voice:hover {
    background: rgb(var(--c-white) / .04);
}

.tts-row-voice:focus-within {
    box-shadow: inset 0 0 0 2px rgba(var(--rd-ac), .55);
}

/* Speed: name, slider, figure on one line */
.tts-row-speed .tts-rate {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
}

.tts-rate-val {
    flex: none;
    min-width: 2.3rem;
    text-align: right;
}

/* next chapter: the name takes the room (and a second line on a narrow phone), the switch keeps its size */
.tts-row-next .tts-row-label {
    flex: 1 1 auto;
    min-width: 0;
}

/* the switch is painted from its state here (reader.js paints its own switches inline) */
#tts-panel .rd-switch[aria-checked="true"] {
    background: var(--rd-ac-fg);
}

#tts-panel .rd-switch[aria-checked="true"] .rd-switch-knob {
    transform: translateX(16px);
}

#tts-panel .rd-switch:focus-visible {
    outline: 2px solid var(--rd-ac-fg);
    outline-offset: 2px;
}

@media (max-width: 420px) {
    .tts-body {
        padding: .7rem .65rem;
    }

    .tts-head {
        padding: .6rem .6rem .6rem .65rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    #tts-panel {
        transform: none;
        transition: opacity .15s;
    }

    .tts-progress-fill {
        transition: none;
    }

    /* the level meter stands still, its bars at four heights */
    .tts-eq i { animation: none; }
    .tts-eq i:nth-child(1) { transform: scaleY(.55); }
    .tts-eq i:nth-child(2) { transform: scaleY(1); }
    .tts-eq i:nth-child(3) { transform: scaleY(.4); }
    .tts-eq i:nth-child(4) { transform: scaleY(.75); }
}

/* ── UI_CONTROLS: the row controls' designs (assets/styles/ui-controls.css) on back · Play · forward and × ── */
/* 2 · joined — back, Play and forward are one bar: the gap closes, the hairlines overlap, the inner corners square */
html[data-ui-controls="2"] .tts-transport {
    gap: 0;
}

html[data-ui-controls="2"] .tts-transport > :not(:first-child) {
    margin-left: -1px;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}

html[data-ui-controls="2"] .tts-transport > :not(:last-child) {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

html[data-ui-controls="2"] .tts-transport > :is(:hover, :focus-visible) {
    z-index: 1;
}

/* 3 · solid — filled tiles, no hairline */
html[data-ui-controls="3"] .tts-ctl {
    border-color: transparent;
    background: rgb(var(--c-white) / .1);
}

html[data-ui-controls="3"] .tts-ctl:is(:hover, :focus-visible) {
    border-color: transparent;
    background: rgb(var(--c-white) / .17);
}

/* 4 · outline — hairlines only; Play is outlined in the accent, not filled */
html[data-ui-controls="4"] .tts-ctl {
    border-color: rgb(var(--c-white) / .18);
    background: transparent;
}

html[data-ui-controls="4"] .tts-ctl:is(:hover, :focus-visible) {
    border-color: rgb(var(--c-white) / .45);
    background: transparent;
}

html[data-ui-controls="4"] .tts-play,
html[data-ui-controls="4"] .tts-play:hover {
    background: transparent;
    box-shadow: inset 0 0 0 1px rgba(var(--rd-ac), .9);
    color: var(--rd-ac-fg);
    filter: none;
}

html[data-ui-controls="4"] .tts-play:is(:hover, :focus-visible) {
    background: rgba(var(--rd-ac), .12);
}

/* 5 · accent — back and forward in the accent too: a tint beside Play's fill; × stays quiet */
html[data-ui-controls="5"] .tts-step {
    border-color: transparent;
    background: rgba(var(--rd-ac), .14);
    color: var(--rd-ac-fg);
}

html[data-ui-controls="5"] .tts-step:is(:hover, :focus-visible) {
    border-color: transparent;
    background: rgba(var(--rd-ac), .24);
    color: var(--rd-ac-fg);
}

/* ── UI_PLAIN: no hairlines on the panel, the icon buttons and the option card (the dividers between the rows stay).
   The outline design's buttons would be bare glyphs then, so they keep a faint fill. ── */
html[data-ui-plain] #tts-panel,
html[data-ui-plain] #tts-panel :is(.tts-ctl, .tts-opts) {
    border-color: transparent !important;
}

html[data-ui-plain][data-ui-controls="4"] #tts-panel .tts-ctl {
    background: rgb(var(--c-white) / .06);
}

html[data-ui-plain][data-ui-controls="4"] #tts-panel .tts-ctl:is(:hover, :focus-visible) {
    background: rgb(var(--c-white) / .12);
}

/* ── ROW_PANELS: from sm up the panel is one of the row panels (assets/styles/row-panels.css) — the header bar's
   colour, their hairline and corner, the option card on their white tint ── */
@media (min-width: 640px) {
    html[data-row-panels] #tts-panel {
        border-color: rgb(var(--c-white) / .07);
        border-radius: var(--ui-rc-2xl, 1.25rem);
        background: rgb(var(--c-surface));
    }

    html[data-row-panels] #tts-panel .tts-opts {
        background: rgb(var(--c-white) / .04);
    }
}

/* ── Speaker buttons while the panel is open or the chapter is being read with it closed (the menu tile is an
   .rd-btn: reader-ui.css's .is-on paints it) ── */
#reader-nav .tts-btn.is-on,
#reader-bottom-nav .tts-btn.is-on {
    background: rgba(var(--rd-ac), .16);
}

#reader-nav .tts-btn.is-on svg,
#reader-bottom-nav .tts-btn.is-on svg {
    color: var(--rd-ac-fg);
}

/* the phone bar's buttons are as tall as the bar: the lit one is a 40px tile inside it, not a slab from edge to edge */
#reader-bottom-nav .tts-btn.is-on {
    height: 40px;
}

/* ── The sentence being read ──
   Browsers with the CSS Custom Highlight API mark the exact sentence (reader-tts.js writes that rule itself, with the
   accent as a plain colour); the others get this class on the paragraph that holds it. */
.reader-note .prose-note .tts-current {
    border-radius: .25rem;
    background: rgba(var(--rd-ac), .24);
    box-shadow: 0 0 0 .2rem rgba(var(--rd-ac), .24);
}
