/* Version history dialog ("Versioner", versions.js). Only #modal-versions and vh- classes;
   nothing here changes an existing rule. Colours come from the theme tokens in styles.css,
   except inside the paper page, which is white in both themes and uses fixed page colours. */

/* Above the phone drawers (z-index 1500), below the toasts (9999) */
#modal-versions.modal-backdrop {
    z-index: 1600;
}

#modal-versions .vh-dialog {
    width: min(1120px, 96vw);
    max-width: 96vw;
    height: min(88vh, 920px);
    overflow: hidden;
}
#modal-versions .vh-dialog:focus {
    outline: none;
}
#modal-versions .modal-header h3:focus {
    outline: none;
}
#modal-versions .modal-header h3:focus-visible {
    /* Hug the word, not the whole header, so the title never looks like a text field */
    width: fit-content;
    outline: 2px solid var(--ink);
    outline-offset: 2px;
}

/* ---------- Notices ---------- */
.vh-notice {
    margin: 12px 24px 0;
    padding: 12px 14px;
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: 4px;
    font-family: var(--font-ui);
    font-size: 14px;
    line-height: 1.5;
    color: var(--ink);
}
.vh-notice p {
    margin: 0;
}
.vh-notice p + p {
    margin-top: 6px;
}
.vh-notice.vh-notice-error {
    background: var(--red-bg);
    border-color: var(--red);
}
.vh-whole {
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 640px;
    font-size: 15px;
    line-height: 1.5;
    color: var(--ink);
}
.vh-whole p {
    margin: 0;
}
.vh-whole-actions {
    margin-top: 4px;
}

/* ---------- Two panes ---------- */
.vh-body {
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    flex: 1;
    min-height: 0;
}
.vh-list-pane {
    border-right: 1px solid var(--line);
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 0;
}
.vh-detail-pane {
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
}

/* ---------- Draft form ---------- */
.vh-draft-form {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--line);
}
.vh-draft-form label,
.vh-rename-form label {
    font-size: 13px;
    font-weight: 600;
    color: var(--ink);
}
.vh-draft-row {
    display: flex;
    gap: 8px;
}
.vh-draft-form input,
.vh-rename-form input,
.vh-compare-bar select {
    min-width: 0;
    height: 44px;
    padding: 0 12px;
    background: var(--surface);
    border: 1px solid var(--muted);
    border-radius: 4px;
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: 14px;
}
.vh-draft-form input {
    flex: 1;
}
.vh-draft-form input::placeholder,
.vh-rename-form input::placeholder {
    color: var(--muted);
    opacity: 1;
}
.vh-draft-form input:focus,
.vh-rename-form input:focus,
.vh-compare-bar select:focus {
    border-color: var(--ink);
}
.vh-draft-form input[aria-invalid="true"],
.vh-rename-form input[aria-invalid="true"] {
    border-color: var(--red);
}
.vh-field-error {
    margin: 0;
    font-size: 13px;
    color: var(--red);
}
.vh-hint {
    margin: 0;
    font-size: 13px;
    line-height: 1.45;
    color: var(--muted);
}

/* ---------- Current text, filter, list ---------- */
.vh-current {
    margin: 0;
    font-size: 13px;
    line-height: 1.45;
    color: var(--muted);
}
.vh-filter,
.vh-tabs {
    display: flex;
    gap: 0;
}
.vh-seg {
    min-height: 36px;
    padding: 0 12px;
    background: transparent;
    border: 1px solid var(--line-strong);
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
}
.vh-seg + .vh-seg {
    border-left: 0;
}
.vh-seg:first-child {
    border-radius: 4px 0 0 4px;
}
.vh-seg:last-child {
    border-radius: 0 4px 4px 0;
}
.vh-seg:hover {
    background: var(--fill);
}
.vh-seg[aria-pressed="true"] {
    background: var(--ink);
    border-color: var(--ink);
    color: var(--on-ink);
    font-weight: 600;
}
.vh-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    border-top: 1px solid var(--line);
}
.vh-list li {
    border-bottom: 1px solid var(--line);
}
.vh-row {
    width: 100%;
    min-height: 56px;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 3px;
    background: transparent;
    border: 0;
    border-radius: 0;
    color: var(--ink);
    font-family: var(--font-ui);
    text-align: left;
    cursor: pointer;
}
.vh-row:hover {
    background: var(--fill);
}
.vh-row[aria-current="true"] {
    background: var(--surface-2);
    box-shadow: inset 3px 0 0 var(--ink);
}
.vh-row-top {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 8px;
    max-width: 100%;
}
.vh-row-name {
    font-size: 14px;
    font-weight: 600;
    overflow-wrap: anywhere;
}
.vh-row-meta {
    font-size: 13px;
    line-height: 1.4;
    color: var(--muted);
    overflow-wrap: anywhere;
}
.vh-badge {
    display: inline-block;
    padding: 2px 6px;
    border: 1px solid var(--line-strong);
    border-radius: 2px;
    font-family: var(--font-editor);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
    white-space: nowrap;
}
.vh-words {
    display: inline-flex;
    gap: 8px;
    align-items: baseline;
}
.vh-delta {
    font-variant-numeric: tabular-nums;
}
.vh-empty {
    margin: 0;
    padding: 12px 0;
    font-size: 14px;
    line-height: 1.5;
    color: var(--muted);
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
}
.vh-empty p {
    margin: 0;
}
.vh-empty-title {
    color: var(--ink);
    font-weight: 600;
}
.vh-list-footer {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: auto;
    padding-top: 8px;
}
.vh-refresh-row {
    display: flex;
    align-items: center;
    gap: 10px;
}
.vh-refreshing {
    font-size: 13px;
    color: var(--muted);
}
.vh-explainer {
    margin: 0;
    font-size: 13px;
    line-height: 1.45;
    color: var(--muted);
}
.vh-link {
    min-height: 44px;
    min-width: 44px;
    padding: 0 8px;
    background: transparent;
    border: 0;
    border-radius: 4px;
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: 13px;
    font-weight: 500;
    text-decoration: underline;
    text-underline-offset: 0.2em;
    cursor: pointer;
}
.vh-link:first-child {
    margin-left: -8px;
}
.vh-link:hover {
    text-decoration-thickness: 2px;
}
.vh-link:disabled,
.vh-seg:disabled,
.vh-row:disabled {
    cursor: not-allowed;
    opacity: .55;
}

/* ---------- Detail ---------- */
.vh-pick {
    margin: auto;
    padding: 24px;
    max-width: 360px;
    text-align: center;
    font-size: 14px;
    line-height: 1.5;
    color: var(--muted);
}
.vh-back {
    display: none;
    align-self: flex-start;
    min-height: 44px;
    margin: 8px 0 0 8px;
    padding: 0 10px;
    background: transparent;
    border: 0;
    border-radius: 4px;
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}
.vh-detail-head {
    padding: 16px 24px 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.vh-detail-title {
    margin: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 10px;
    font-family: var(--font-ui);
    font-size: 18px;
    font-weight: 600;
    line-height: 1.3;
    color: var(--ink);
    overflow-wrap: anywhere;
}
.vh-detail-title:focus {
    outline: none;
}
.vh-detail-title:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 2px;
}
.vh-detail-meta {
    margin: 0;
    font-size: 13px;
    line-height: 1.45;
    color: var(--muted);
    overflow-wrap: anywhere;
}
.vh-inline-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    align-items: center;
}
.vh-rename-form {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-width: 480px;
}
.vh-rename-form .vh-inline-actions {
    margin-top: 2px;
    gap: 8px;
}
.vh-detail-pane .vh-tabs {
    padding: 4px 24px 12px;
    border-bottom: 1px solid var(--line);
}
.vh-scroll {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 16px;
    background: var(--paper);
    display: flex;
    flex-direction: column;
    gap: 12px;
}
#vh-changes {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.vh-caption,
.vh-direction {
    margin: 0;
    font-size: 13px;
    line-height: 1.45;
    color: var(--muted);
}
.vh-summary {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    color: var(--ink);
}
.vh-compare-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    font-size: 13px;
    color: var(--ink);
}
.vh-compare-bar > label:first-child {
    font-weight: 600;
}
.vh-compare-bar select {
    max-width: 100%;
    cursor: pointer;
}
.vh-check {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    cursor: pointer;
}
.vh-check input {
    width: 18px;
    height: 18px;
    accent-color: var(--ink);
}
.vh-stale {
    margin: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
}
.vh-stale.hidden {
    display: none;
}
.vh-titlepage,
.vh-title-changes {
    margin: 0;
    padding: 10px 14px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 4px;
    font-size: 13px;
    line-height: 1.5;
    color: var(--ink);
    list-style: none;
}
.vh-titlepage {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 20px;
}
.vh-titlepage div {
    display: flex;
    gap: 6px;
}
.vh-titlepage dt {
    color: var(--muted);
}
.vh-titlepage dd {
    margin: 0;
    overflow-wrap: anywhere;
}
.vh-title-changes li + li {
    margin-top: 4px;
}
.vh-tc-label {
    font-weight: 600;
}
.vh-title-changes del {
    text-decoration: line-through;
    color: var(--muted);
}
.vh-title-changes ins {
    text-decoration: underline;
    text-underline-offset: 0.2em;
}
.vh-actions {
    padding: 12px 24px;
    border-top: 1px solid var(--line);
    background: var(--surface-2);
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.vh-action-buttons {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 10px;
}
.vh-actions .vh-hint {
    text-align: right;
}

/* ---------- The paper page (preview and changes) ---------- */
#modal-versions .vh-paper {
    gap: 0;
}
#modal-versions .vh-paper .screenplay-page {
    height: auto !important;
    min-height: 0 !important;
    max-width: 100%;
    margin: 0 auto;
    padding-top: 16mm;
    padding-bottom: 16mm;
    zoom: var(--vh-zoom, 1);
    flex: none;
}
#modal-versions .vh-paper .screenplay-page > .element:first-child {
    margin-top: 0 !important;
}

/* Diff marks: background on the block, a +/−/~ glyph in one gutter left of the text column */
#modal-versions .vh-paper div.element.vh-mark-added {
    background: #E7ECF6 !important;
    box-shadow: -8px 0 0 0 #E7ECF6, 8px 0 0 0 #E7ECF6;
}
#modal-versions .vh-paper div.element.vh-mark-removed {
    background: #F6E7E4 !important;
    box-shadow: -8px 0 0 0 #F6E7E4, 8px 0 0 0 #F6E7E4;
}
#modal-versions .vh-paper div.element.vh-mark-changed {
    box-shadow: -8px 0 0 0 #FAF9F6, 8px 0 0 0 #FAF9F6;
    background: #FAF9F6 !important;
}
#modal-versions .vh-paper div.element[data-vh-mark]::after {
    content: attr(data-vh-mark);
    content: attr(data-vh-mark) / "";
    position: absolute;
    top: 0;
    left: -26px;
    width: 14px;
    text-align: center;
    font-family: var(--font-ui);
    font-size: 14px;
    font-weight: 700;
    line-height: 1.3;
    font-style: normal;
    text-transform: none;
    text-decoration: none;
}
#modal-versions .vh-paper div.element.vh-mark-added::after { color: #2E3F6E; }
#modal-versions .vh-paper div.element.vh-mark-removed::after { color: #962117; }
#modal-versions .vh-paper div.element.vh-mark-changed::after { color: #5E5850; }
#modal-versions .vh-paper div.element.character[data-vh-mark]::after { left: calc(-2.2in - 26px); }
#modal-versions .vh-paper div.element.parenthetical[data-vh-mark]::after { left: calc(-1.6in - 26px); }
#modal-versions .vh-paper div.element.dialogue[data-vh-mark]::after { left: calc(-1.0in - 26px); }
#modal-versions .vh-paper div.element.transition[data-vh-mark]::after { left: calc(-4.0in - 26px); }

/* Inline word changes: the editor resets colours and backgrounds of everything inside a block,
   so these need the higher specificity and !important */
#modal-versions .vh-paper div.element ins.vh-ins {
    background: #DCE3F2 !important;
    color: #1E1B16 !important;
    text-decoration: underline !important;
    text-decoration-color: #2E3F6E !important;
    text-decoration-thickness: 2px !important;
    text-underline-offset: 0.15em;
}
#modal-versions .vh-paper div.element del.vh-del {
    background: #F6E7E4 !important;
    color: #962117 !important;
    text-decoration: line-through !important;
    text-decoration-thickness: 1.5px !important;
}
/* A deletion right next to an insertion gets a hair of space, so the two words don't run together */
#modal-versions .vh-paper div.element del.vh-del + ins.vh-ins,
#modal-versions .vh-paper div.element ins.vh-ins + del.vh-del {
    margin-left: 2px;
}
#modal-versions .vh-paper div.element.vh-mark-removed del.vh-del {
    background: transparent !important;
}
#modal-versions .vh-paper p.vh-type-note {
    margin: -0.6em 0 1.15em;
    min-height: 0;
    font-family: var(--font-ui);
    font-size: 12px;
    line-height: 1.4;
    color: #5E5850;
    white-space: normal;
}
#modal-versions .vh-paper .vh-gap {
    display: block;
    width: 100%;
    min-height: 44px;
    margin: 0.8em 0;
    padding: 8px 10px;
    background: transparent;
    border: 0;
    border-top: 1px dashed #D3CDC2;
    border-bottom: 1px dashed #D3CDC2;
    color: #5E5850;
    font-family: var(--font-ui);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
}
#modal-versions .vh-paper .vh-gap:hover {
    background: #F3F0EA;
    color: #1E1B16;
}
#modal-versions .vh-paper .vh-gap:focus-visible {
    outline: 2px solid #1E1B16;
    outline-offset: 2px;
}
#modal-versions .vh-paper .visually-hidden {
    text-transform: none;
}
#modal-versions .vh-paper div.element:focus {
    outline: none;
}

/* Focus ring on every new control */
#modal-versions .vh-row:focus-visible,
#modal-versions .vh-seg:focus-visible,
#modal-versions .vh-link:focus-visible,
#modal-versions .vh-back:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: -2px;
}

/* ---------- Narrow: one pane at a time ---------- */
@media (max-width: 1099px) {
    .vh-body {
        grid-template-columns: minmax(0, 1fr);
    }
    .vh-list-pane {
        border-right: 0;
    }
    .vh-body[data-view="list"] .vh-detail-pane,
    .vh-body[data-view="detail"] .vh-list-pane {
        display: none;
    }
    .vh-back {
        display: inline-flex;
        align-items: center;
    }
    .vh-detail-head {
        padding-top: 4px;
    }
}

/* ---------- Phone (same breakpoint as MOBILE_QUERY in app.js) ---------- */
@media (max-width: 850px) {
    #modal-versions.modal-backdrop {
        padding: 0;
        height: 100vh;
        height: 100dvh;
    }
    #modal-versions .vh-dialog {
        width: 100vw;
        max-width: 100vw;
        height: 100vh;
        height: 100dvh;
        border: 0;
        border-radius: 0;
    }
    #modal-versions .modal-header {
        padding: 8px 8px 8px 16px;
    }
    .vh-notice {
        margin: 12px 16px 0;
    }
    .vh-whole {
        padding: 16px;
    }
    .vh-list-pane {
        padding: 16px;
    }
    .vh-draft-form input,
    .vh-rename-form input,
    .vh-compare-bar select {
        font-size: 16px; /* no zoom on iOS */
    }
    .vh-seg {
        min-height: 44px;
        flex: 1;
    }
    /* The whole detail scrolls, with the buttons kept at the bottom */
    .vh-detail-pane {
        overflow-y: auto;
    }
    .vh-detail-head {
        padding: 4px 16px 8px;
    }
    .vh-detail-pane .vh-tabs {
        padding: 4px 16px 12px;
    }
    .vh-scroll {
        flex: none;
        overflow: visible;
        padding: 16px;
    }
    .vh-actions {
        position: sticky;
        bottom: 0;
        margin-top: auto;
        padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
    }
    .vh-actions .vh-hint {
        text-align: left;
    }
    .vh-action-buttons {
        flex-direction: column-reverse;
        align-items: stretch;
    }
    .vh-action-buttons .btn {
        width: 100%;
    }
    .vh-compare-bar select {
        width: 100%;
    }
    #modal-versions .vh-paper .screenplay-page {
        zoom: 1;
        padding-top: 16px;
        padding-bottom: 24px;
    }
    /* Phone blocks have no indent: one gutter in the page padding */
    #modal-versions .vh-paper div.element[data-vh-mark]::after,
    #modal-versions .vh-paper div.element.character[data-vh-mark]::after,
    #modal-versions .vh-paper div.element.parenthetical[data-vh-mark]::after,
    #modal-versions .vh-paper div.element.dialogue[data-vh-mark]::after,
    #modal-versions .vh-paper div.element.transition[data-vh-mark]::after {
        left: -13px;
        width: 10px;
        font-size: 12px;
    }
    #modal-versions .vh-paper div.element.vh-mark-added,
    #modal-versions .vh-paper div.element.vh-mark-removed,
    #modal-versions .vh-paper div.element.vh-mark-changed {
        box-shadow: none; /* the page padding is too narrow for the side bleed */
    }
}

/* Only a message (local account, database not set up): a small dialog */
#modal-versions .vh-dialog.vh-compact {
    width: min(560px, calc(100vw - 32px));
    height: auto;
    border: 1px solid var(--ink);
    border-radius: 4px;
}

@media (prefers-reduced-motion: reduce) {
    #modal-versions * {
        transition: none !important;
    }
}
