/* template/css/tts.css — Text-to-Speech player styles.
 *
 * Two elements:
 *   .tts-trigger — small button injected into the header (.site-utils) or
 *                  footer (.footer-controls). Opens the panel on click.
 *   .tts-panel   — compact controls bar docked to the top or bottom of the
 *                  viewport, centered horizontally. Lives in document.body
 *                  so its position is unaffected by the header/footer.
 *
 * Word highlight: when the "Highlight" toggle is on, the JS wraps every
 * word in the readable content with <span class="tts-word">. The .is-current
 * class on a span paints the word being spoken right now.
 */

/* ===== Trigger button =================================================== */
.tts-trigger {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    /* Horizontal margin keeps the button from butting up against the search
       icon (header) or the cookie-prefs button (footer). */
    margin: 0 8px;
    padding: 6px 10px;
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 6px;
    background: transparent;
    color: var(--fg, #1f2937);
    font: inherit;
    font-size: 0.875rem;
    cursor: pointer;
    line-height: 1;
    transition: background-color 150ms ease, border-color 150ms ease;
}
.tts-trigger:hover {
    background: rgba(0, 0, 0, 0.05);
    border-color: var(--primary-color, #2563eb);
}
[data-theme="dark"] .tts-trigger:hover {
    background: rgba(255, 255, 255, 0.08);
}
.tts-trigger:focus-visible {
    outline: 2px solid var(--primary-color, #2563eb);
    outline-offset: 2px;
}
.tts-trigger.is-active {
    background: var(--primary-color, #2563eb);
    color: #fff;
    border-color: var(--primary-color, #2563eb);
}
.tts-trigger-icon {
    width: 18px;
    height: 18px;
    fill: currentColor;
    flex: 0 0 auto;
}
@media (max-width: 480px) {
    .tts-trigger-label { display: none; }
    .tts-trigger {
        padding: 6px;
        width: 36px;
        height: 36px;
        justify-content: center;
    }
}

/* ===== Sticky panel — docked top or bottom of viewport =================== */
.tts-panel {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1200;
    background: var(--header-bg, #ffffff);
    color: var(--fg, #1f2937);
    border: 1px solid var(--border-color, #e5e7eb);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
    font-family: inherit;
    /* Width is content-driven up to a sensible ceiling so the bar never
       stretches across a wide desktop monitor. The ceiling drops further
       at narrow breakpoints (see media queries below). */
    width: max-content;
    max-width: calc(100vw - 16px);
}
.tts-panel[hidden] { display: none; }

.tts-panel--bottom {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    border-radius: 10px;
}
.tts-panel--top {
    top: calc(env(safe-area-inset-top, 0px) + 12px);
    border-radius: 10px;
}

.tts-panel-inner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 8px;
    padding: 5px 8px;
}

/* Main playback controls — compact pill row */
.tts-controls {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    flex: 0 0 auto;
}
.tts-btn {
    width: 34px;
    height: 32px;
    padding: 0;
    border: 1px solid transparent;
    border-radius: 6px;
    background: transparent;
    color: var(--fg, #1f2937);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background-color 120ms ease, transform 120ms ease;
}
.tts-btn:hover     { background: rgba(0, 0, 0, 0.06); }
.tts-btn:active    { transform: scale(0.95); }
.tts-btn:focus-visible {
    outline: 2px solid var(--primary-color, #2563eb);
    outline-offset: 1px;
}
[data-theme="dark"] .tts-btn:hover { background: rgba(255, 255, 255, 0.08); }
.tts-btn svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

/* Play/pause is the accent button. */
.tts-playpause {
    width: 42px;
    background: var(--primary-color, #2563eb);
    color: #fff;
    border-color: var(--primary-color, #2563eb);
    margin: 0 2px;
}
.tts-playpause:hover { background: var(--primary-color, #2563eb); filter: brightness(1.08); }
.tts-playpause svg   { width: 20px; height: 20px; }
.tts-playpause .tts-icon-play,
.tts-playpause .tts-icon-pause { display: none; }
.tts-playpause--play  .tts-icon-play  { display: block; }
.tts-playpause--pause .tts-icon-pause { display: block; }

/* Meta row: status text + highlight toggle */
.tts-meta {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: 0 1 auto;
    min-width: 0;
}
.tts-status {
    font-size: 0.72rem;
    color: var(--muted, #6b7280);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

/* Highlight pill toggle */
.tts-btn-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    height: 28px;
    font: inherit;
    font-size: 0.78rem;
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 999px;
    background: transparent;
    color: var(--fg, #1f2937);
    cursor: pointer;
    line-height: 1;
}
.tts-btn-toggle svg { width: 14px; height: 14px; fill: currentColor; }
.tts-btn-toggle:hover { background: rgba(0, 0, 0, 0.05); }
[data-theme="dark"] .tts-btn-toggle:hover { background: rgba(255, 255, 255, 0.08); }
.tts-btn-toggle.is-active {
    background: var(--primary-color, #2563eb);
    color: #fff;
    border-color: var(--primary-color, #2563eb);
}

/* Speed slider */
.tts-speed {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.72rem;
    color: var(--muted, #6b7280);
}
.tts-speed-input {
    width: 70px;
    accent-color: var(--primary-color, #2563eb);
}
.tts-speed-val {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    color: var(--fg, #1f2937);
    min-width: 2.2em;
    text-align: right;
}

/* Voice picker.
   Native <select> arrows on every browser overlap the option text when
   the text is long. We strip the native chrome (appearance: none) and
   paint our own arrow as a background-image, then reserve right-padding
   so the text never sits beneath it. text-overflow: ellipsis truncates
   the visible selected-option text in browsers that honour it on
   <select> (Chrome / Firefox / Edge — Safari shows full text but the
   reserved padding still prevents arrow overlap). */
.tts-voice {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.72rem;
    color: var(--muted, #6b7280);
}
.tts-voice select {
    -webkit-appearance: none;
       -moz-appearance: none;
            appearance: none;
    padding: 2px 22px 2px 6px;   /* 22px right-padding reserves room for the custom arrow */
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 6px;
    background-color: var(--header-bg, #ffffff);
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 6'%3e%3cpath fill='%236b7280' d='M0 0l5 6 5-6z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 7px center;
    background-size: 8px 5px;
    color: var(--fg, #1f2937);
    font: inherit;
    font-size: 0.72rem;
    max-width: 130px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}
[data-theme="dark"] .tts-voice select {
    /* lighter arrow for dark backgrounds */
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 6'%3e%3cpath fill='%23cbd5e1' d='M0 0l5 6 5-6z'/%3e%3c/svg%3e");
}

/* Close (×) */
.tts-close {
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--muted, #6b7280);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
}
.tts-close:hover { background: rgba(0, 0, 0, 0.06); color: var(--fg, #1f2937); }
[data-theme="dark"] .tts-close:hover { background: rgba(255, 255, 255, 0.08); }
.tts-close svg { width: 16px; height: 16px; fill: currentColor; }

/* ===== Sentence highlight ================================================
   Each sentence in the readable content is wrapped in <span class="tts-sentence">
   when highlight is on. The span is inline so it doesn't break the visual
   flow of the surrounding text (including across line wraps — the highlight
   simply paints behind each line fragment, the way native text-selection does).
   .is-current is applied by the JS when the matching sentence starts speaking,
   and removed when the next sentence starts. */
.tts-sentence {
    transition: background-color 120ms ease;
    border-radius: 2px;
    /* box-decoration-break makes the background paint cleanly across line
       breaks instead of being cut at the right edge of the first line. */
    -webkit-box-decoration-break: clone;
            box-decoration-break: clone;
    /* Slight inline padding so the highlight has a little breathing room
       around the words at the start/end of each line. */
    padding: 1px 2px;
    margin: 0 -2px;
}
.tts-sentence.is-current {
    background-color: rgba(250, 204, 21, 0.35); /* warm yellow, softer than word-level */
    color: inherit;
}
[data-theme="dark"] .tts-sentence.is-current {
    background-color: rgba(250, 204, 21, 0.25);
}

/* ===== Mobile layout ===================================================== */
/* Below 600px the panel narrows further and we shrink the optional widgets
   so they fit on a phone without the bar overflowing the screen. */
@media (max-width: 600px) {
    .tts-panel { max-width: calc(100vw - 12px); }
    .tts-panel-inner { gap: 4px 6px; padding: 4px 6px; }
    .tts-status { font-size: 0.7rem; max-width: 110px; }
    .tts-voice select {
        max-width: 92px;
        font-size: 0.68rem;
        padding: 2px 4px;
    }
    .tts-voice > span { display: none; }     /* drop the "Voice" label, the dropdown is self-explanatory */
    .tts-speed > .tts-speed-label { display: none; }  /* drop the "Speed" label, keep the value */
    .tts-speed-input { width: 56px; }
    .tts-btn-toggle span { display: none; }  /* highlight pill becomes icon-only */
    .tts-btn-toggle { padding: 0; width: 28px; justify-content: center; }
}
@media (max-width: 420px) {
    /* Two-row layout: playback controls on row 1, settings on row 2. */
    .tts-controls { order: 1; }
    .tts-meta     { order: 2; flex: 1 1 auto; }
    .tts-close    { order: 3; }
    .tts-speed    { order: 4; }
    .tts-voice    { order: 5; flex: 1 1 auto; min-width: 0; }
    .tts-voice select { max-width: 100%; flex: 1 1 auto; min-width: 0; }
}

/* Hide the player entirely when printing. */
@media print {
    .tts-trigger,
    .tts-panel { display: none !important; }
    .tts-sentence.is-current { background: transparent !important; }
}
