/*
 * HumanProse editor flow: streamed rewrites with Cancel, the review mode
 * (accept or reject each change), the restored-draft notice, long drafts
 * in parts and the compact result actions on phones.
 *
 * Colours come from the design tokens (--hx-*), which hp-design.css
 * redefines for the dark theme; spacing uses logical properties so the
 * Arabic (right-to-left) layout mirrors on its own.
 */

/* ---------- 1. Live progress while the rewrite streams in ---------- */
body.hp-ui .hp-stream-bar {
    display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px;
    margin: 16px 20px 0; padding: 10px 10px 10px 16px;
    border: 1px solid var(--hx-line); border-radius: 16px; background: var(--hx-surface-2); color: var(--hx-ink);
}
body.hp-ui .hp-stream-bar[hidden] { display: none; }
@media (min-width: 640px) { body.hp-ui .hp-stream-bar { margin-inline: 28px; } }
body.hp-ui .hp-stream-dot {
    flex: none; width: 10px; height: 10px; border-radius: 50%;
    background: var(--hx-quill); box-shadow: 0 0 0 0 color-mix(in srgb, var(--hx-quill) 55%, transparent);
}
body.hp-ui .hp-stream-status {
    flex: 1 1 200px; display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; min-width: 0; margin: 0;
    font-size: .86rem; font-weight: 700; font-variant-numeric: tabular-nums;
}
body.hp-ui .hp-stream-part {
    display: inline-flex; align-items: center; min-height: 24px; padding: 0 10px; border-radius: 999px;
    background: var(--hx-royal-soft); color: var(--hx-royal-ink); font-size: .76rem; font-weight: 800; letter-spacing: .02em;
}
body.hp-ui .hp-stream-part[hidden] { display: none; }
body.hp-ui .hp-stream-meter {
    position: relative; flex: 1 1 120px; height: 6px; overflow: hidden; border-radius: 999px; background: var(--hx-line);
}
body.hp-ui .hp-stream-meter > span {
    position: absolute; inset: 0; border-radius: inherit; background: var(--hx-royal);
    transform: scaleX(.03); transform-origin: left center; transition: transform .35s var(--hx-ease, ease);
}
[dir="rtl"] body.hp-ui .hp-stream-meter > span { transform-origin: right center; }
body.hp-ui .hp-stream-cancel {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px; flex: none;
    min-height: 44px; min-width: 44px; padding: 0 16px; border-radius: 999px; cursor: pointer;
    border: 1px solid var(--hx-line-2); background: var(--hx-surface); color: var(--hx-ink);
    font: 700 .84rem/1 var(--hx-f-body);
}
body.hp-ui .hp-stream-cancel:hover { border-color: var(--hx-danger); color: var(--hx-danger); }
body.hp-ui .hp-stream-cancel svg { width: 16px; height: 16px; }

/* A caret at the end of the text while it is being written. */
body.hp-ui #outputCard.is-writing #outputText:not(:empty)::after {
    content: ""; display: inline-block; width: 2px; height: 1.05em; margin-inline-start: 2px; vertical-align: -.15em;
    background: var(--hx-quill);
}
body.hp-ui #outputCard.is-writing #outputText { scroll-margin-bottom: 24px; }
body.hp-ui #outputCard { scroll-margin-top: 84px; }

@media (prefers-reduced-motion: no-preference) {
    body.hp-ui .hp-stream-dot { animation: hpStreamPulse 1.4s ease-out infinite; }
    body.hp-ui #outputCard.is-writing #outputText:not(:empty)::after { animation: hpStreamCaret 1s steps(2, start) infinite; }
}
@media (prefers-reduced-motion: reduce) {
    body.hp-ui .hp-stream-meter > span { transition: none; }
}
@keyframes hpStreamPulse {
    0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--hx-quill) 55%, transparent); }
    100% { box-shadow: 0 0 0 9px transparent; }
}
@keyframes hpStreamCaret { to { visibility: hidden; } }

/* ---------- 2. Review mode: accept or reject each change ---------- */
body.hp-ui .hp-review-bar {
    display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px;
    margin: 12px 20px 0; padding: 10px 12px; border: 1px solid var(--hx-line); border-radius: 16px; background: var(--hx-surface);
}
body.hp-ui .hp-review-bar[hidden] { display: none; }
@media (min-width: 640px) { body.hp-ui .hp-review-bar { margin-inline: 28px; } }
body.hp-ui .hp-review-count { margin: 0; flex: 1 1 220px; font-size: .86rem; font-weight: 800; color: var(--hx-ink); font-variant-numeric: tabular-nums; }
body.hp-ui .hp-review-actions { display: flex; flex-wrap: wrap; gap: 8px; }
body.hp-ui .hp-review-btn {
    display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 14px; border-radius: 999px; cursor: pointer;
    border: 1px solid var(--hx-line-2); background: var(--hx-surface); color: var(--hx-ink); font: 700 .8rem/1 var(--hx-f-body);
}
body.hp-ui .hp-review-btn:hover { border-color: var(--hx-royal); color: var(--hx-royal-ink); }
body.hp-ui .hp-review-btn svg { width: 15px; height: 15px; }
body.hp-ui .hp-review-done { background: var(--hx-royal); border-color: var(--hx-royal); color: #fff; }
body.hp-ui .hp-review-done:hover { background: var(--hx-royal-hover); color: #fff; }
body.hp-ui .hp-review-help { flex: 1 1 100%; margin: 0; font-size: .78rem; color: var(--hx-ink-3); }
body.hp-ui .hp-review-save { flex: 1 1 100%; margin: 0; font-size: .8rem; font-weight: 700; color: var(--hx-ok); }
body.hp-ui .hp-review-save:empty { display: none; }

/* One change: the draft's words, the rewrite's words, then Accept and Reject. */
body.hp-ui .hp-rv { border-radius: 6px; }
body.hp-ui .hp-rv-actions { display: inline-flex; gap: 3px; margin-inline: 4px 2px; vertical-align: .05em; white-space: nowrap; }
body.hp-ui .hp-rv-btn {
    display: inline-grid; place-items: center; width: 28px; height: 28px; padding: 0; border-radius: 50%; cursor: pointer;
    border: 1px solid var(--hx-line-2); background: var(--hx-surface); color: var(--hx-ink-2);
    font-size: 0; line-height: 0; vertical-align: middle; transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
body.hp-ui .hp-rv-btn svg { width: 14px; height: 14px; }
body.hp-ui .hp-rv-accept:hover { border-color: var(--hx-ok); color: var(--hx-ok); }
body.hp-ui .hp-rv-reject:hover { border-color: var(--hx-danger); color: var(--hx-danger); }
body.hp-ui .hp-rv-accept[aria-pressed="true"] { background: var(--hx-ok); border-color: var(--hx-ok); color: var(--hx-surface); }
body.hp-ui .hp-rv-reject[aria-pressed="true"] { background: var(--hx-danger); border-color: var(--hx-danger); color: var(--hx-surface); }
body.hp-ui .hp-rv-btn:focus-visible { outline: 2px solid var(--hx-royal); outline-offset: 2px; }
body.hp-ui .hp-rv:focus-within { background: color-mix(in srgb, var(--hx-royal) 8%, transparent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--hx-royal) 25%, transparent); }

/* Accepted: only the new words, underlined in green. */
html body.hp-ui #outputDiff .hp-rv[data-state="accepted"] del.hp-diff-del { display: none; }
html body.hp-ui #outputDiff .hp-rv[data-state="accepted"] ins.hp-diff-ins {
    text-decoration-color: var(--hx-ok) !important;
    background: color-mix(in srgb, var(--hx-ok) 16%, transparent) !important;
}
/* Rejected: the draft's words are back (no strike), the rewrite's words go. */
html body.hp-ui #outputDiff .hp-rv[data-state="rejected"] ins.hp-diff-ins { display: none; }
html body.hp-ui #outputDiff .hp-rv[data-state="rejected"] del.hp-diff-del {
    color: inherit !important; text-decoration: none !important;
    background: color-mix(in srgb, var(--hx-warn) 14%, transparent);
    box-shadow: inset 0 -2px 0 color-mix(in srgb, var(--hx-warn) 70%, transparent);
}
@media (forced-colors: active) {
    body.hp-ui .hp-rv-btn[aria-pressed="true"] { forced-color-adjust: none; background: Highlight; color: HighlightText; border-color: Highlight; }
}

/* ---------- 3. The draft is back after signing in ---------- */
body.hp-ui .hp-draft-notice {
    display: flex; align-items: center; gap: 10px; margin: 0; padding: 10px 12px 10px 20px;
    border-bottom: 1px solid var(--hx-line); background: var(--hx-ok-soft); color: var(--hx-ink);
}
body.hp-ui .hp-draft-notice[hidden] { display: none; }
body.hp-ui .hp-draft-notice > svg { flex: none; width: 18px; height: 18px; color: var(--hx-ok); }
body.hp-ui .hp-draft-notice p { margin: 0; flex: 1; font-size: .86rem; font-weight: 600; }
body.hp-ui .hp-draft-notice strong { font-weight: 800; }
body.hp-ui .hp-draft-notice-close {
    display: grid; place-items: center; width: 44px; height: 44px; flex: none; border: 0; border-radius: 12px;
    background: transparent; color: var(--hx-ink-2); cursor: pointer;
}
body.hp-ui .hp-draft-notice-close:hover { background: var(--hx-surface); color: var(--hx-ink); }
body.hp-ui .hp-draft-notice-close svg { width: 16px; height: 16px; }

/* ---------- 4. Long drafts in parts ---------- */
body.hp-ui .hp-parts-offer {
    display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; padding: 14px 20px;
    border-top: 1px solid color-mix(in srgb, var(--hx-quill) 45%, transparent); background: var(--hx-quill-soft); color: var(--hx-ink);
}
body.hp-ui .hp-parts-offer[hidden] { display: none; }
@media (min-width: 640px) { body.hp-ui .hp-parts-offer { padding-inline: 24px; } }
body.hp-ui .hp-parts-offer > svg { flex: none; width: 20px; height: 20px; color: var(--hx-quill-ink); }
body.hp-ui .hp-parts-offer p { margin: 0; flex: 1 1 260px; font-size: .86rem; line-height: 1.5; }
body.hp-ui .hp-parts-start {
    display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 18px; border: 0; border-radius: 999px;
    background: var(--hx-royal); color: #fff; font: 800 .86rem/1 var(--hx-f-body); cursor: pointer; white-space: nowrap;
}
body.hp-ui .hp-parts-start:hover { background: var(--hx-royal-hover); }
body.hp-ui .hp-parts-start[hidden] { display: none; }

/* ---------- 5. Result actions on phones: Copy, then icon buttons ---------- */
@media (max-width: 640px) {
    html body.hp-ui #workspace .result-action-row {
        display: flex !important; flex-wrap: wrap; align-items: center; gap: 8px; width: 100%;
    }
    html body.hp-ui #workspace .result-action-row > button {
        width: auto; min-width: 44px; min-height: 44px; padding: 0 12px; justify-content: center;
    }
    html body.hp-ui #workspace .result-action-row > button:not(#copyBtn):not([data-hp-diff-toggle]) {
        width: 44px; padding: 0; flex: none;
    }
    html body.hp-ui #workspace .result-action-row > button:not(#copyBtn):not([data-hp-diff-toggle]) :is(.hp-ra-label, span) {
        position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
        overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
    }
    html body.hp-ui #workspace .result-action-row > [data-hp-diff-toggle] { order: -2; flex: 1 0 100%; }
    html body.hp-ui #workspace .result-action-row > #copyBtn {
        order: -1; flex: 1 1 auto;
        background: var(--hx-royal) !important; border-color: var(--hx-royal) !important; color: #fff !important;
    }
    html body.hp-ui #workspace .result-action-row > #copyBtn:disabled { opacity: .45; }
    html body.hp-ui #workspace .result-action-row svg { width: 18px; height: 18px; }

    body.hp-ui .hp-review-btn { min-height: 44px; }
    body.hp-ui .hp-review-help { display: none; }
    body.hp-ui .hp-rv-btn { width: 32px; height: 32px; }
    body.hp-ui .hp-stream-bar { padding-inline-start: 12px; }
}

/* ---------- 6. Ink that dries while the rewrite streams (hp-ink.js) ---------- */
body.hp-ui { --hp-ink-wet: var(--hx-quill); }
html[data-hp-theme="dark"] body.hp-ui { --hp-ink-wet: var(--hx-quill-ink); }
/* The nib takes over from the caret. */
body.hp-ui #outputCard.is-writing #outputText.hp-inking:not(:empty)::after { display: none; }
body.hp-ui .hp-ink-nib {
    display: inline-block; width: .74em; height: 1.16em; margin-inline-start: .08em; vertical-align: -.07em;
    line-height: 0; pointer-events: none; user-select: none;
}
body.hp-ui .hp-ink-nib svg { display: block; width: 100%; height: 100%; overflow: visible; transform-origin: 6.5% 94.5%; }
body.hp-ui .hp-ink-nib-vane { fill: color-mix(in srgb, var(--hx-quill) 58%, var(--hx-surface)); stroke: var(--hx-quill); stroke-width: 2.2; stroke-linejoin: round; }
body.hp-ui .hp-ink-nib-rib { stroke: var(--hx-quill-ink); stroke-width: 2.4; stroke-linecap: round; opacity: .7; }
body.hp-ui .hp-ink-nib-tip { fill: var(--hx-quill-ink); }
body.hp-ui .hp-ink-nib-slit { stroke: var(--hx-surface); stroke-width: 1.4; stroke-linecap: round; }
html[data-hp-theme="dark"] body.hp-ui .hp-ink-nib-tip { fill: var(--hx-quill); }
/* The copy that fades where the nib stood when the rewrite ends (placed by hp-ink.js). */
body.hp-ui .hp-ink-nib.is-leaving { position: absolute; z-index: 4; margin: 0; }

@media (prefers-reduced-motion: no-preference) {
    body.hp-ui #outputText .hp-ink { animation: hpInkDry .6s cubic-bezier(.33, .1, .25, 1) both; }
    body.hp-ui #outputText .hp-ink-nib { animation: hpNibIn .28s var(--hx-ease, ease-out) both; }
    body.hp-ui #outputText .hp-ink-nib svg { animation: hpNibWrite .72s ease-in-out .28s infinite alternate; }
    body.hp-ui .hp-ink-nib.is-leaving { animation: hpNibOut .42s cubic-bezier(.4, 0, .7, .4) forwards; }
}
@media (prefers-reduced-motion: reduce) {
    body.hp-ui .hp-ink-nib { display: none; }
}
@keyframes hpInkDry {
    0% { color: var(--hp-ink-wet); opacity: .64; }
    28% { color: var(--hp-ink-wet); opacity: .9; }
    100% { color: var(--hx-ink); opacity: 1; }
}
@keyframes hpNibIn { from { opacity: 0; transform: translate(-.12em, .1em); } to { opacity: 1; transform: none; } }
@keyframes hpNibWrite { from { transform: rotate(-4deg); } to { transform: rotate(3deg) translate(.02em, -.03em); } }
@keyframes hpNibOut { to { opacity: 0; transform: translate(6px, -9px) rotate(16deg); } }

/* ---------- 7. Phones: the writing controls as a bottom sheet (hp-controls-sheet.js) ---------- */
/* Desktop and tablet: the sheet wrapper adds no box and the phone-only parts are not there. */
body.hp-ui .hp-csheet { display: contents; }
body.hp-ui :is(.hp-csheet-backdrop, .hp-csheet-bar, .hp-csheet-foot, .hp-style-trigger) { display: none; }

@media (max-width: 640px) {
    /* The sheet is fixed to the screen, so nothing above it may be a containing block (the paper never tilts on phones). */
    body.hp-ui #workspace .hp-sheet-wrap { perspective: none; }
    body.hp-ui #workspace .hp-sheet { transform: none; }
    body.hp-ui #workspace .controls-panel { display: contents; }

    /* The bar under the editor: Writing style and Humanize, held at the bottom of the screen while the editor is in view. */
    body.hp-ui #workspace .mobile-action-bar {
        position: sticky; bottom: max(10px, env(safe-area-inset-bottom)); z-index: 30;
        display: flex; flex-wrap: wrap; align-items: stretch; gap: 8px;
        margin: 14px 0 0; padding: 7px;
        border: 1px solid var(--hx-line); border-radius: 20px;
        background: var(--hx-surface);
        box-shadow: 0 18px 38px -20px rgba(8, 26, 19, .5), 0 2px 8px rgba(8, 26, 19, .06);
        -webkit-backdrop-filter: none; backdrop-filter: none;
    }
    body.hp-ui #workspace .mobile-action-bar #humanizeBtn {
        flex: 999 1 150px; min-width: 0; min-height: 54px; padding: 0 12px; gap: 8px; font-size: .8rem;
    }
    body.hp-ui #workspace .mobile-action-bar #humanizeBtn svg { width: 18px; height: 18px; }
    body.hp-ui #workspace .hp-style-trigger {
        display: inline-flex; flex: 1 1 auto; align-items: center; gap: 9px; min-width: 0; min-height: 54px; padding: 0 10px 0 12px;
        border: 1px solid var(--hx-line-2); border-radius: 15px; background: var(--hx-surface); color: var(--hx-ink);
        text-align: start; cursor: pointer; transition: border-color .18s ease, background-color .18s ease;
    }
    body.hp-ui #workspace .hp-style-trigger:hover { border-color: var(--hx-quill); }
    body.hp-ui #workspace .hp-style-trigger:focus-visible { outline: 2px solid var(--hx-focus); outline-offset: 2px; }
    body.hp-ui #workspace .hp-style-trigger[aria-expanded="true"] { border-color: var(--hx-quill); background: var(--hx-quill-soft); }
    body.hp-ui .hp-style-trigger-icon { flex: none; width: 20px; height: 20px; color: var(--hx-royal-ink); }
    body.hp-ui .hp-style-trigger-text { display: grid; gap: 1px; min-width: 0; }
    body.hp-ui .hp-style-trigger-label {
        font: 700 .62rem/1.2 var(--hx-f-body); letter-spacing: .12em; text-transform: uppercase; color: var(--hx-quill-ink); white-space: nowrap;
    }
    body.hp-ui .hp-style-trigger-mode {
        overflow: hidden; font: 400 1.14rem/1.1 var(--hx-f-display); color: var(--hx-ink); white-space: nowrap; text-overflow: ellipsis;
    }
    body.hp-ui .hp-style-trigger-chevron { flex: none; width: 15px; height: 15px; margin-inline-start: auto; color: var(--hx-ink-3); transform: rotate(180deg); }
    html[lang="ar"] body.hp-ui .hp-style-trigger-label { letter-spacing: 0; font-size: .7rem; }
}
@media (max-width: 419px) {
    /* Narrow phones: the words carry the button, so Humanize keeps its label on one line. */
    body.hp-ui #workspace .hp-style-trigger { padding-inline-start: 13px; }
    body.hp-ui #workspace .hp-style-trigger-icon { display: none; }
}
@media (max-width: 380px) {
    body.hp-ui #workspace .hp-style-trigger { gap: 6px; padding-inline: 11px 8px; }
    body.hp-ui #workspace .mobile-action-bar #humanizeBtn { flex-basis: 140px; padding-inline: 10px; font-size: .76rem; }
    body.hp-ui #workspace .mobile-action-bar #humanizeBtn svg { display: none; }
}
@media (max-width: 640px) {

    /* The sheet itself: the controls panel's own content, slid up from the bottom. */
    body.hp-ui #workspace .hp-csheet {
        position: fixed; inset: auto 0 0; z-index: 61;
        display: flex; flex-direction: column;
        max-height: min(86dvh, 760px); margin: 0; padding: 0 18px;
        overflow-y: auto; overscroll-behavior: contain;
        border: 1px solid var(--hx-line); border-bottom: 0; border-radius: 26px 26px 0 0;
        background: var(--hx-surface); color: var(--hx-ink);
        box-shadow: 0 -26px 60px -26px rgba(8, 26, 19, .55);
        transform: translateY(calc(100% + 32px)); visibility: hidden;
        transition: transform .38s var(--hx-ease, ease), visibility 0s linear .38s;
    }
    body.hp-ui #workspace .hp-csheet.is-open {
        transform: translateY(var(--hp-csheet-drag, 0px)); visibility: visible;
        transition: transform .38s var(--hx-ease, ease), visibility 0s;
    }
    body.hp-ui #workspace .hp-csheet.is-dragging { transition: none; }
    body.hp-ui #workspace .hp-csheet:focus { outline: none; }
    body.hp-ui #workspace .hp-csheet [data-hp-csheet-drag] { touch-action: none; }
    body.hp-ui #workspace .hp-csheet-bar {
        position: sticky; top: 0; z-index: 2; display: grid; grid-template-columns: 44px 1fr 44px; align-items: center;
        margin: 0 -18px 2px; padding: 4px 8px 0; background: var(--hx-surface); cursor: grab;
    }
    body.hp-ui #workspace .hp-csheet.is-dragging .hp-csheet-bar { cursor: grabbing; }
    body.hp-ui .hp-csheet-grip { grid-column: 2; justify-self: center; width: 42px; height: 5px; border-radius: 999px; background: var(--hx-line-2); }
    body.hp-ui .hp-csheet-close {
        grid-column: 3; display: grid; place-items: center; width: 44px; height: 44px; padding: 0;
        border: 0; border-radius: 50%; background: transparent; color: var(--hx-ink-2); cursor: pointer;
    }
    body.hp-ui .hp-csheet-close:hover { background: var(--hx-surface-2); color: var(--hx-ink); }
    body.hp-ui .hp-csheet-close:focus-visible { outline: 2px solid var(--hx-focus); outline-offset: -2px; }
    body.hp-ui .hp-csheet-close svg { width: 18px; height: 18px; }
    body.hp-ui #workspace .hp-csheet-foot {
        position: sticky; bottom: 0; z-index: 2; display: block;
        margin: 16px -18px 0; padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
        border-top: 1px solid var(--hx-line); background: var(--hx-surface);
    }
    body.hp-ui .hp-csheet-done {
        width: 100%; min-height: 50px; border: 0; border-radius: 15px; cursor: pointer;
        background: var(--hx-royal); color: #fff; font: 800 .92rem/1 var(--hx-f-body); letter-spacing: .02em;
    }
    body.hp-ui .hp-csheet-done:hover { background: var(--hx-royal-hover, var(--hx-royal)); }
    body.hp-ui .hp-csheet-done:focus-visible { outline: 2px solid var(--hx-focus); outline-offset: 2px; }
    body.hp-ui #workspace .hp-csheet-backdrop {
        position: fixed; inset: 0; z-index: 60; display: block;
        background: rgba(5, 14, 10, .5); opacity: 0; transition: opacity .3s ease; touch-action: none;
    }
    body.hp-ui #workspace .hp-csheet-backdrop[hidden] { display: none; }
    body.hp-ui #workspace .hp-csheet-backdrop.is-open { opacity: 1; }
    /* Open: above the header and the floating buttons, and the page holds still. */
    html.hp-csheet-open body.hp-ui #workspace { z-index: 130; }
    html.hp-csheet-open, html.hp-csheet-open body.hp-ui { overflow: hidden; }
}
@media (max-width: 640px) and (prefers-reduced-motion: reduce) {
    body.hp-ui #workspace :is(.hp-csheet, .hp-csheet.is-open, .hp-csheet-backdrop) { transition: none; }
}

/* ---------- 8. Phones: the result's Mode, Input, Output and Time as a 2 x 2 grid ---------- */
@media (max-width: 640px) {
    body.hp-ui #outputCard #resultDetails:not(.hidden) { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
    body.hp-ui #outputCard #resultDetails > div { min-width: 0; padding: 10px 12px; }
    body.hp-ui #outputCard #resultDetails > div > p:last-child { overflow-wrap: anywhere; }
}
