/* ---------------------------------------------------------------------------
   The reader (reader.js)

   The rest of the app is a mono workbench. The reader is a page: a serif text
   column, generous leading (the room above each line is where the dependency
   arcs are drawn), and bars that fade while you read.
   --------------------------------------------------------------------------- */
#reader-view {
    --rd-bg: var(--site-paper);
    --rd-ink: var(--site-ink);
    --rd-dim: var(--site-muted);
    --rd-faint: var(--site-line);
    --rd-rule: var(--site-line);
    --rd-card: var(--site-panel);
    --rd-active: var(--primary-color);
    --rd-head: var(--secondary-color);
    --rd-dep: var(--accent-color);
    --rd-serif: var(--site-serif);
    --rd-mono: var(--site-font);

    position: fixed;
    inset: 0;
    z-index: 1800;
    display: flex;
    flex-direction: column;
    background:
        radial-gradient(ellipse at 50% 35%, rgba(229, 192, 123, 0.035), transparent 60%),
        var(--rd-bg);
    color: var(--rd-ink);
}

#reader-view.hidden { display: none; }

#reader-view { overscroll-behavior: none; }

body.reading { overflow: hidden; }

/* Bars ------------------------------------------------------------------- */
.rd-bar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 24px;
    transition: opacity 0.6s ease;
    flex-shrink: 0;
}

#reader-view.is-idle .rd-bar { opacity: 0.18; }
#reader-view.is-idle .rd-bar:hover,
#reader-view.is-idle .rd-bar:focus-within { opacity: 1; }

.rd-top { border-bottom: 1px solid transparent; }

.rd-where {
    display: flex;
    flex-direction: column;
    min-width: 0;
    flex: 0 1 auto;
    font-family: var(--rd-mono);
    line-height: 1.3;
}

.rd-title {
    color: var(--rd-active);
    font-size: 0.85rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 28vw;
}

.rd-position {
    color: var(--rd-dim);
    font-size: 0.75rem;
}

/* One tick per segment: where you are in the whole text, and which parts
   already have their words analysed. */
.rd-ticks {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    gap: 3px;
    min-width: 0;
    height: 28px;
    /* Dense editions must not paint segment targets over the controls. */
    overflow-x: auto;
    overscroll-behavior-x: contain;
}

.rd-tick {
    position: relative;
    flex: 1 1 0;
    max-width: 28px;
    min-width: 2px;
    height: 100%;
    cursor: pointer;
    box-shadow: none;
}

/* The tick you see is a thin bar; the button around it is the full height of
   the strip, so a finger (or an imprecise pointer) still lands on it. */
.rd-tick::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    top: 50%;
    height: 4px;
    transform: translateY(-50%);
    border-radius: 2px;
    background: var(--rd-faint);
    transition: height 0.15s ease, background 0.15s ease;
}

.rd-tick:hover::after { height: 10px; background: var(--rd-dim); }
.rd-tick.is-analysed::after { background: color-mix(in srgb, var(--rd-dep) 45%, var(--rd-faint)); }
.rd-tick.is-current::after { background: var(--rd-active); height: 8px; }

.rd-controls {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

#reader-view button {
    font-family: var(--rd-mono);
    font-size: 0.8rem;
    background: transparent;
    color: var(--rd-dim);
    border: 1px solid var(--rd-rule);
    border-radius: 6px;
    padding: 6px 12px;
    box-shadow: none;
    text-transform: none;
    letter-spacing: 0;
    cursor: pointer;
    transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

#reader-view button:hover:not(:disabled) {
    color: var(--rd-ink);
    border-color: var(--rd-dim);
    background: rgba(255, 255, 255, 0.03);
    transform: none;
}

#reader-view button:focus-visible {
    outline: 2px solid var(--rd-active);
    outline-offset: 2px;
}

#reader-view button:disabled { opacity: 0.3; cursor: default; }

/* The bar buttons' look must not reach the ticks, which are buttons too. */
#reader-view .rd-tick,
#reader-view .rd-tick:hover:not(:disabled) { padding: 0; border: none; background: transparent; }

.rd-translation-btn[aria-pressed="true"] {
    color: var(--rd-active) !important;
    border-color: color-mix(in srgb, var(--rd-active) 50%, transparent) !important;
}

#reader-view kbd {
    font-family: var(--rd-mono);
    font-size: .75rem;
    padding: 1px 5px;
    border: 1px solid var(--rd-faint);
    border-bottom-width: 2px;
    border-radius: 4px;
    color: var(--rd-dim);
    background: rgba(0, 0, 0, 0.2);
}

.rd-close { font-size: 1.1rem !important; line-height: 1; padding: 4px 10px !important; }

.rd-bottom {
    justify-content: space-between;
    /* Clear of an iPhone's home indicator when the page runs edge to edge. */
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.rd-prev, .rd-next { font-size: 1rem !important; padding: 6px 14px !important; flex-shrink: 0; }

.rd-bump { animation: rd-bump 0.3s ease; }
@keyframes rd-bump {
    25% { transform: translateX(-4px); }
    75% { transform: translateX(4px); }
}

.rd-status {
    flex: 1;
    min-width: 0;
    text-align: center;
    color: var(--rd-dim);
    font-family: var(--rd-mono);
    font-size: 0.78rem;
    min-height: 1.5em;
}

.rd-status.is-busy::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    margin-right: 10px;
    border-radius: 50%;
    background: var(--rd-active);
    animation: rd-breathe 1.4s ease-in-out infinite;
    vertical-align: middle;
}

@keyframes rd-breathe {
    50% { opacity: 0.25; transform: scale(0.7); }
}

#reader-view .rd-inline-action {
    margin-left: 6px;
    white-space: normal;
    padding: 3px 10px;
    color: var(--rd-active);
    border-color: color-mix(in srgb, var(--rd-active) 40%, transparent);
}

#reader-view .rd-inline-action.too-expensive {
    color: var(--danger-color);
    border-color: color-mix(in srgb, var(--danger-color) 40%, transparent);
}

/* The page --------------------------------------------------------------- */
.rd-stage {
    flex: 1 1 auto;
    overflow-y: auto;
    /* Horizontal swipes walk the segments. Without these the browser also
       reads a rightward swipe as its own Back gesture, and leaves the reader. */
    overscroll-behavior: none;
    touch-action: pan-y;
    outline: none;
    padding: 6vh 24px 12vh;
    scrollbar-width: thin;
    scrollbar-color: var(--rd-faint) transparent;
}

.rd-page {
    position: relative;
    max-width: 40rem;
    margin: 0 auto;
}

.rd-arcs {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
    z-index: 0;
}

.rd-text {
    position: relative;
    z-index: 1;
    font-family: var(--rd-serif);
    font-size: clamp(1.3rem, 1rem + 1vw, 1.8rem);
    line-height: 2.25;
    color: var(--rd-ink);
    font-feature-settings: 'kern', 'liga', 'onum';
    hyphens: manual;
    overflow-wrap: break-word;
}

.rd-text:lang(bo) { line-height: 2.6; }

.rd-empty {
    color: var(--rd-dim);
    font-style: italic;
    text-align: center;
}

.rd-sentence { transition: opacity 0.25s ease; }

/* Resting on a word brings its sentence forward and lets the rest recede. */
.rd-text.is-explaining .rd-sentence { opacity: 0.38; }
.rd-text.is-explaining .rd-sentence.is-current { opacity: 1; }

.rd-word {
    cursor: help;
    border-radius: 4px;
    outline: none;
    transition: color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

.rd-text.is-analysed .rd-word:hover { background: rgba(255, 255, 255, 0.05); }

.rd-word.is-active {
    color: var(--rd-active);
    background: color-mix(in srgb, var(--rd-active) 13%, transparent) !important;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--rd-active) 13%, transparent);
}

.rd-word.is-head {
    color: var(--rd-head);
    box-shadow: inset 0 -2px 0 color-mix(in srgb, var(--rd-head) 70%, transparent);
}

.rd-word.is-dependent {
    color: color-mix(in srgb, var(--rd-dep) 85%, var(--rd-ink));
    box-shadow: inset 0 -1px 0 color-mix(in srgb, var(--rd-dep) 55%, transparent);
}

.rd-arc path {
    fill: none;
    stroke-width: 1.5;
    stroke-linecap: round;
}

.rd-arc-head path { stroke: var(--rd-head); }
.rd-arc-dep path { stroke: var(--rd-dep); stroke-opacity: 0.75; stroke-width: 1.2; }

.rd-arc-label rect {
    fill: var(--rd-bg);
    stroke: var(--rd-rule);
    stroke-width: 1;
}

.rd-arc-label text {
    font-family: var(--rd-mono);
    font-size: 12px;
    letter-spacing: 0.02em;
}

.rd-arc-head .rd-arc-label text { fill: var(--rd-head); }
.rd-arc-dep .rd-arc-label text { fill: var(--rd-dep); }

/* The translation, laid under the passage when asked for. */
.rd-translation {
    display: none;
    position: relative;
    z-index: 1;
    margin-top: 2.2rem;
    padding-top: 1.6rem;
    border-top: 1px solid var(--rd-rule);
    font-family: var(--rd-serif);
    font-style: italic;
    font-size: clamp(1.05rem, 0.9rem + 0.5vw, 1.3rem);
    line-height: 1.85;
    color: color-mix(in srgb, var(--rd-ink) 72%, var(--rd-bg));
}

#reader-view.show-translation .rd-translation {
    display: block;
    animation: rd-rise 0.35s ease both;
}

@keyframes rd-rise {
    from { opacity: 0; transform: translateY(6px); }
}

.rd-translation-empty {
    font-family: var(--rd-mono);
    font-style: normal;
    font-size: 0.8rem;
    color: var(--rd-dim);
    margin: 0;
}

/* The word card ---------------------------------------------------------- */
.rd-tip {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1900;
    width: min(400px, calc(100vw - 32px));
    max-height: min(70vh, 34rem);
    overflow-y: auto;
    padding: 16px 18px 14px;
    background: var(--rd-card);
    border: 1px solid var(--rd-rule);
    border-top: 2px solid var(--rd-active);
    border-radius: 10px;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.55), 0 2px 6px rgba(0, 0, 0, 0.35);
    color: var(--rd-ink);
    font-family: var(--rd-mono);
    font-size: 0.8rem;
    line-height: 1.55;
    opacity: 0;
    visibility: hidden;
    transform: translateY(4px);
    transition: opacity 0.16s ease, transform 0.16s ease, visibility 0s linear 0.16s;
    pointer-events: none;
}

.rd-tip.is-visible {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition: opacity 0.16s ease, transform 0.16s ease;
}

.rd-tip.is-below { transform: translateY(-4px); }
.rd-tip.is-visible.is-below { transform: none; }
.rd-tip.is-pinned { pointer-events: auto; }

.rd-tip p { margin: 0; }

.rd-tip-header {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 4px 12px;
    margin-bottom: 8px;
}

.rd-tip-word {
    font-family: var(--rd-serif);
    font-size: 1.55rem;
    line-height: 1.2;
    color: var(--rd-active);
}

.rd-tip-lemma {
    font-family: var(--rd-serif);
    font-size: 1.05rem;
    color: var(--rd-ink);
}

.rd-tip-from {
    font-family: var(--rd-mono);
    font-size: .75rem;
    color: var(--rd-dim);
    margin-right: 6px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

#reader-view .rd-tip-close {
    margin-left: auto;
    min-width: 32px;
    min-height: 44px;
    padding: 0 8px;
    font-size: 1.1rem;
    line-height: 1;
    color: var(--rd-dim);
}

.rd-tip-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 10px;
}

.rd-chip {
    padding: 2px 8px;
    border-radius: 999px;
    border: 1px solid var(--rd-rule);
    color: var(--rd-dim);
    font-size: .75rem;
    background: rgba(0, 0, 0, 0.18);
}

.rd-chip-pos {
    color: var(--rd-bg);
    background: color-mix(in srgb, var(--rd-active) 85%, var(--rd-bg));
    border-color: transparent;
    font-weight: 600;
}

.rd-tip-gloss {
    font-family: var(--rd-serif);
    font-size: 1.12rem;
    line-height: 1.4;
    color: var(--rd-ink);
    margin-bottom: 10px !important;
}

.rd-tip-place {
    padding: 8px 0 2px;
    border-top: 1px dashed var(--rd-rule);
}

.rd-tip-role { color: var(--rd-ink); margin-bottom: 4px !important; }

.rd-tip-links {
    list-style: none;
    margin: 0 0 4px;
    padding: 0;
    color: var(--rd-dim);
}

.rd-tip-links li { padding: 1px 0; }
.rd-tip-links b { font-family: var(--rd-serif); font-weight: 400; font-size: 0.95rem; }
.rd-link-mark { display: inline-block; width: 1.4em; }
.rd-link-head b, .rd-link-head .rd-link-mark { color: var(--rd-head); }
.rd-link-deps b, .rd-link-deps .rd-link-mark { color: var(--rd-dep); }
.rd-link-root .rd-link-mark { color: var(--rd-active); }
.rd-link-rel { color: var(--rd-faint); }

.rd-tip-note {
    margin-top: 8px !important;
    padding: 8px 10px;
    border-left: 2px solid var(--rd-faint);
    background: rgba(0, 0, 0, 0.16);
    border-radius: 0 6px 6px 0;
    font-family: var(--rd-serif);
    font-style: italic;
    font-size: 0.95rem;
    line-height: 1.45;
    color: color-mix(in srgb, var(--rd-ink) 85%, var(--rd-bg));
}

.rd-tip-sentence {
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px dashed var(--rd-rule);
}

.rd-tip-label {
    display: block;
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--rd-faint);
    margin-bottom: 2px;
}

.rd-tip-literal,
.rd-tip-structure {
    font-family: var(--rd-serif);
    font-size: 0.92rem;
    line-height: 1.45;
    color: var(--rd-dim);
}

.rd-tip-structure { margin-top: 4px; }
.rd-tip-literal + .rd-tip-label { margin-top: 8px; }

.rd-tip-hint {
    margin-top: 10px !important;
    font-size: .75rem;
    color: var(--rd-faint);
    text-align: right;
}

/* Keys ------------------------------------------------------------------- */
.rd-help {
    position: fixed;
    inset: 0;
    z-index: 1950;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(10, 12, 15, 0.6);
    backdrop-filter: blur(3px);
}

.rd-help.hidden { display: none; }

.rd-help-card {
    width: min(30rem, 100%);
    padding: 22px 26px;
    background: var(--rd-card);
    border: 1px solid var(--rd-rule);
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    font-family: var(--rd-mono);
    font-size: 0.8rem;
}

.rd-help-card h3 {
    font-size: 0.9rem;
    margin-bottom: 14px;
    padding: 0;
    text-shadow: none;
}

.rd-help-card h3::before { display: none; }

.rd-help-card dl {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 8px 18px;
    margin: 0;
}

.rd-help-card dt { color: var(--rd-ink); white-space: nowrap; }
.rd-help-card dd { margin: 0; color: var(--rd-dim); }

.rd-help-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 18px;
    margin: 16px 0 0;
    font-size: 0.75rem;
}

.rd-legend-head { color: var(--rd-head); }
.rd-legend-dep { color: var(--rd-dep); }

/* Ways in, on the working screens: outlined, so the way into reading stands
   apart from the yellow work buttons beside it. (It shipped once as yellow
   text on the yellow every button gets — a blank bar.) */
.read-btn {
    background-color: rgba(229, 192, 123, 0.08);
    border: 1px solid rgba(229, 192, 123, 0.6);
    color: var(--primary-color);
}

.read-btn:hover {
    background-color: rgba(229, 192, 123, 0.18);
}

/* Narrow screens: the card docks at the bottom as a sheet, the ticks go. */
@media (max-width: 640px) {
    .rd-bar { padding: 10px 16px; gap: 10px; }
    .rd-ticks { display: none; }
    .rd-title { max-width: 50vw; }
    .rd-stage { padding: 4vh 16px 10vh; }
    .rd-translation-btn kbd { display: none; }

    .rd-tip {
        left: 0 !important;
        right: 0;
        top: auto !important;
        bottom: 0;
        width: 100%;
        max-height: 55vh;
        border-radius: 14px 14px 0 0;
        transform: translateY(20px);
    }
    .rd-tip.is-visible { transform: none; }

    /* Room under a short passage, so a word near its end can be lifted above
       the sheet instead of disappearing beneath it. */
    #reader-view.has-sheet .rd-stage { padding-bottom: 65vh; }
}

@media (prefers-reduced-motion: reduce) {
    #reader-view *,
    .rd-tip { transition: none !important; animation: none !important; }
}

/* Touch screens: the ticks get a finger's height, and the resting bars stay
   legible — there is no pointer to move, so a faded "Analyse" button would
   be found only by someone who already knew it was there. */
@media (pointer: coarse) {
    .rd-ticks { height: 44px; }
    #reader-view.is-idle .rd-bar { opacity: 0.6; }
}
