/* =========================================================
   GHOST MARGINS WRITING FLOW — 2026-09-27
   Draft-first work creation + manuscript-first chapter editor.
   ========================================================= */

/* ---------- New Work: start writing first, catalog later ---------- */
.new-work.quick-start-layout,
.new-work {
    width: min(1120px, calc(100% - 40px));
}

.quick-start-work-form {
    gap: 16px !important;
}

.new-work-quick-start {
    padding: clamp(18px, 3vw, 30px);
    border: 1px solid var(--gm-border, #b7a48f);
    border-radius: 12px;
    background: var(--gm-card, var(--gm-card-bg, #fffaf0));
    color: var(--gm-card-text, inherit);
}

.new-work-quick-copy {
    max-width: 760px;
    margin-bottom: 22px;
}

.editor-kicker {
    display: inline-block;
    margin-bottom: 5px;
    font: 700 .74rem/1.2 var(--font-ui);
    letter-spacing: .13em;
    text-transform: uppercase;
    color: var(--gm-interior-muted, currentColor);
    opacity: .76;
}

.new-work-quick-copy h3 {
    margin: 0 0 8px;
    font-size: clamp(1.45rem, 3vw, 2rem);
    line-height: 1.12;
}

.new-work-quick-copy p {
    max-width: 680px;
    margin: 0;
    line-height: 1.55;
    color: var(--gm-interior-muted, inherit);
}

.new-work-quick-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.65fr) minmax(210px, .7fr);
    gap: 16px;
    align-items: end;
}

.new-work-quick-start .form-field input,
.new-work-quick-start .form-field select {
    min-height: 52px;
    border-radius: 8px;
}

.optional-label {
    margin-left: 6px;
    font-weight: 500;
    font-size: .78rem;
    opacity: .62;
}

.work-details-later {
    overflow: hidden;
    border: 1px solid var(--gm-border, #b7a48f);
    border-radius: 10px;
    background: color-mix(in srgb, var(--gm-card, #fffaf0) 78%, transparent);
}

.work-details-later > summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    min-height: 66px;
    padding: 13px 18px;
    box-sizing: border-box;
    cursor: pointer;
    list-style: none;
    font-family: var(--font-ui);
}

.work-details-later > summary::-webkit-details-marker { display: none; }
.work-details-later > summary strong { display: block; font-size: 1rem; }
.work-details-later > summary small { display: block; margin-top: 3px; opacity: .66; }
.details-chevron { font-size: 1.35rem; transition: transform .16s ease; }
.work-details-later[open] .details-chevron { transform: rotate(180deg); }
.work-details-later[open] > summary { border-bottom: 1px solid var(--gm-border, #b7a48f); }

.work-details-later-body {
    display: grid;
    gap: 14px;
    padding: 14px;
}

.work-details-later .form-section {
    margin: 0 !important;
    border-radius: 8px;
}

.quick-start-actions {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 10px !important;
    margin-top: 0 !important;
}

.quick-start-actions button {
    width: auto !important;
    min-width: 0 !important;
    min-height: 48px;
    padding-inline: 20px;
}

.quick-start-actions .primary-start-writing {
    order: -1;
    min-width: 180px !important;
}

.quick-start-actions .secondary-save-draft {
    opacity: .9;
}

/* ---------- Chapter editor: manuscript first ---------- */
.chapter-editor-shell {
    width: min(1500px, calc(100% - 42px));
    max-width: none !important;
    margin: 0 auto !important;
}

.chapter-editor-shell #new-chapter-form,
.chapter-editor-shell #edit-chapter-form {
    gap: 10px !important;
}

.chapter-editor-heading {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 18px;
    margin: 0 0 14px;
    padding: 0 2px 12px;
    border-bottom: 1px solid var(--gm-border, #aaa);
}

.chapter-editor-title-block { min-width: 0; }
.chapter-editor-heading h2 {
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: clamp(1.45rem, 2.4vw, 2rem);
}

.chapter-editor-status {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 0 0 auto;
    min-height: 34px;
    font: 650 .9rem/1.2 var(--font-ui);
    color: var(--gm-interior-muted, inherit);
}

#draft-saved-message:empty { display: none; }
#draft-saved-message:not(:empty)::before {
    content: "·";
    margin-right: 10px;
    opacity: .5;
}

.chapter-editor-shell .form-section {
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
}

.chapter-meta-section .two-column {
    grid-template-columns: minmax(105px, .22fr) minmax(0, 1.5fr) !important;
    gap: 12px !important;
}

.chapter-meta-section .form-field label {
    margin-bottom: 4px;
    font: 650 .78rem/1.2 var(--font-ui);
    opacity: .72;
}

.chapter-meta-section input {
    min-height: 44px;
    border-radius: 7px;
}

.chapter-writing-section > .form-field > label {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0,0,0,0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.chapter-editor-shell .chapter-toolbar {
    position: sticky;
    top: 0;
    z-index: 45;
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: center;
    gap: 6px !important;
    width: 100%;
    margin: 0 !important;
    padding: 8px 6px !important;
    box-sizing: border-box;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
    -ms-overflow-style: none;
    border-top: 1px solid var(--gm-border, #aaa);
    border-bottom: 1px solid var(--gm-border, #aaa);
    background: var(--gm-page, #f6efe3) !important;
}

.chapter-editor-shell .chapter-toolbar::-webkit-scrollbar { display: none; }
.chapter-editor-shell .chapter-toolbar > * { flex: 0 0 auto !important; }
.chapter-editor-shell .chapter-toolbar button {
    min-width: 42px !important;
    width: auto !important;
    height: 40px !important;
    padding-inline: 12px !important;
    border-radius: 7px !important;
}
.chapter-editor-shell .chapter-toolbar button[data-format] {
    width: 42px !important;
    min-width: 42px !important;
    padding-inline: 0 !important;
}
.chapter-editor-shell .chapter-toolbar select {
    width: auto !important;
    min-width: 92px !important;
    height: 40px !important;
    margin: 0 !important;
    border-radius: 7px !important;
}

.chapter-editor-shell .chapter-rich-editor,
.chapter-editor-shell #chapter-text,
.chapter-editor-shell #edit-chapter-text {
    width: 100% !important;
    min-height: clamp(620px, 72vh, 1000px) !important;
    margin: 0 !important;
    padding: clamp(24px, 4vw, 54px) clamp(24px, 6vw, 84px) !important;
    box-sizing: border-box !important;
    border: 0 !important;
    border-radius: 0 0 8px 8px !important;
    outline: 1px solid var(--gm-border, #aaa);
    outline-offset: -1px;
    background: var(--ui-field, #fffaf0) !important;
    color: var(--ui-ink, inherit) !important;
    font-family: var(--font-reading) !important;
    font-size: clamp(1.05rem, 1.25vw, 1.2rem) !important;
    line-height: 1.72 !important;
    overflow-y: visible !important;
}

.chapter-editor-shell .chapter-rich-editor:focus,
.chapter-editor-shell #chapter-text:focus,
.chapter-editor-shell #edit-chapter-text:focus {
    outline: 2px solid var(--ui-focus, var(--gm-accent, #a17b4f));
    outline-offset: -2px;
}

.chapter-notes-panel {
    margin: 4px 0 0;
    border-top: 1px solid var(--gm-border, #aaa);
    border-bottom: 1px solid var(--gm-border, #aaa);
}

.chapter-notes-panel > summary {
    padding: 12px 4px;
    cursor: pointer;
    font: 650 .92rem/1.2 var(--font-ui);
}
.chapter-notes-panel > summary span { font-weight: 500; opacity: .55; margin-left: 5px; }
.chapter-notes-panel .form-field { padding: 0 0 12px; }
.chapter-notes-panel textarea { min-height: 100px !important; }

.chapter-editor-actions {
    display: flex !important;
    flex-wrap: wrap !important;
    justify-content: flex-start !important;
    gap: 8px !important;
    margin: 4px 0 0 !important;
}
.chapter-editor-actions > button,
.chapter-editor-actions > .button-link {
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    min-height: 44px !important;
    padding: 9px 14px !important;
}

.sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

@media (min-width: 901px) {
    .chapter-editor-shell .chapter-toolbar {
        top: 0;
    }
}

@media (max-width: 900px) {
    .new-work,
    .chapter-editor-shell {
        width: calc(100% - 20px) !important;
        margin-inline: auto !important;
    }

    .new-work > h2 {
        margin: 14px 0 12px;
    }

    .new-work-quick-start {
        padding: 16px;
    }

    .new-work-quick-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .new-work-quick-copy { margin-bottom: 16px; }
    .new-work-quick-copy h3 { font-size: 1.35rem; }
    .new-work-quick-copy p { font-size: .94rem; }

    .work-details-later > summary {
        min-height: 58px;
        padding: 11px 13px;
    }
    .work-details-later > summary small { font-size: .76rem; }
    .work-details-later-body { padding: 8px; }
    .work-details-later .form-section { padding: 14px !important; }

    .quick-start-actions {
        display: grid !important;
        grid-template-columns: 1fr !important;
    }
    .quick-start-actions button { width: 100% !important; }
    .quick-start-actions .primary-start-writing { min-width: 0 !important; }

    .chapter-editor-heading {
        align-items: center;
        margin: 10px 0 8px;
        padding: 0 2px 9px;
    }
    .chapter-editor-heading h2 { font-size: 1.12rem; }
    .editor-kicker { font-size: .65rem; }
    .chapter-editor-status { font-size: .84rem; gap: 5px; }
    #draft-saved-message { display: none; }

    .chapter-meta-section .two-column {
        grid-template-columns: 86px minmax(0, 1fr) !important;
        gap: 8px !important;
    }
    .chapter-meta-section input {
        min-height: 40px;
        padding: 8px 10px !important;
    }

    .chapter-editor-shell .chapter-toolbar {
        top: var(--gm-mobile-header-height, 68px);
        margin-inline: -10px !important;
        width: calc(100% + 20px) !important;
        padding: 7px 10px !important;
        background: var(--gm-page, #f6efe3) !important;
        box-shadow: 0 5px 12px rgba(0,0,0,.06);
    }

    .chapter-editor-shell .chapter-toolbar button,
    .chapter-editor-shell .chapter-toolbar select {
        height: 38px !important;
    }

    .chapter-editor-shell .chapter-rich-editor,
    .chapter-editor-shell #chapter-text,
    .chapter-editor-shell #edit-chapter-text {
        min-height: 68vh !important;
        margin-inline: -10px !important;
        width: calc(100% + 20px) !important;
        padding: 24px 18px 44px !important;
        border-radius: 0 !important;
        font-size: 1.08rem !important;
        line-height: 1.68 !important;
    }

    .chapter-editor-actions {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 8px !important;
    }
    .chapter-editor-actions > button,
    .chapter-editor-actions > .button-link {
        width: 100% !important;
        min-width: 0 !important;
        justify-content: center !important;
    }
}

@media (max-width: 460px) {
    .chapter-editor-shell { width: calc(100% - 16px) !important; }
    .chapter-editor-heading { gap: 8px; }
    .chapter-editor-title-block { max-width: 62%; }
    .chapter-editor-status { white-space: nowrap; }

    .chapter-editor-shell .chapter-toolbar {
        margin-inline: -8px !important;
        width: calc(100% + 16px) !important;
        padding-inline: 8px !important;
    }

    .chapter-editor-shell .chapter-rich-editor,
    .chapter-editor-shell #chapter-text,
    .chapter-editor-shell #edit-chapter-text {
        margin-inline: -8px !important;
        width: calc(100% + 16px) !important;
        padding-inline: 16px !important;
    }

    .chapter-editor-actions {
        grid-template-columns: 1fr !important;
    }
}

/* Beat older chapter-action grid rules without touching their behavior elsewhere. */
#new-chapter-form .form-actions.chapter-editor-actions,
#edit-chapter-form .form-actions.chapter-editor-actions {
    display: flex !important;
    flex-flow: row wrap !important;
    grid-template-columns: none !important;
    justify-content: flex-start !important;
    align-items: center !important;
    gap: 8px !important;
    margin: 4px 0 0 !important;
}

#new-chapter-form .form-actions.chapter-editor-actions > button,
#new-chapter-form .form-actions.chapter-editor-actions > .button-link,
#edit-chapter-form .form-actions.chapter-editor-actions > button,
#edit-chapter-form .form-actions.chapter-editor-actions > .button-link {
    grid-column: auto !important;
    grid-row: auto !important;
    flex: 0 1 auto !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    min-height: 40px !important;
    margin: 0 !important;
    padding: 8px 12px !important;
}

#new-chapter-form .form-actions.chapter-editor-actions .cancel-button,
#edit-chapter-form .form-actions.chapter-editor-actions .button-link {
    margin-left: auto !important;
}

@media (max-width: 900px) {
    #new-chapter-form .form-actions.chapter-editor-actions,
    #edit-chapter-form .form-actions.chapter-editor-actions {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        align-items: stretch !important;
    }

    #new-chapter-form .form-actions.chapter-editor-actions > button,
    #new-chapter-form .form-actions.chapter-editor-actions > .button-link,
    #edit-chapter-form .form-actions.chapter-editor-actions > button,
    #edit-chapter-form .form-actions.chapter-editor-actions > .button-link {
        width: 100% !important;
        max-width: none !important;
        margin: 0 !important;
        justify-content: center !important;
    }
}

@media (max-width: 460px) {
    #new-chapter-form .form-actions.chapter-editor-actions,
    #edit-chapter-form .form-actions.chapter-editor-actions {
        grid-template-columns: 1fr !important;
    }
}

/* ========================================
   Scheduled chapter publishing
   ======================================== */
.chapter-schedule-panel {
    margin: 12px 0 0;
    padding: 16px;
    border: 1px solid var(--gm-border, #b8aa96);
    border-radius: 10px;
    background: var(--ui-field, #fffaf0);
    color: var(--ui-ink, inherit);
}

.chapter-schedule-panel[hidden] {
    display: none !important;
}

.chapter-schedule-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 12px;
}

.chapter-schedule-heading strong {
    display: block;
    margin-bottom: 3px;
    font: 700 1rem/1.2 var(--font-ui);
}

.chapter-schedule-heading p,
.chapter-schedule-note {
    margin: 0;
    opacity: .72;
    font: 500 .86rem/1.45 var(--font-ui);
}

.chapter-schedule-timezone {
    flex: 0 0 auto;
    padding: 5px 8px;
    border: 1px solid var(--gm-border, #b8aa96);
    border-radius: 999px;
    opacity: .8;
    font: 650 .76rem/1.2 var(--font-ui);
}

.chapter-schedule-panel > label {
    display: block;
    margin-bottom: 5px;
    font: 650 .82rem/1.2 var(--font-ui);
}

.chapter-schedule-panel input[type="datetime-local"] {
    width: min(100%, 360px);
    min-height: 44px;
    padding: 8px 10px;
    border: 1px solid var(--gm-border, #b8aa96);
    border-radius: 7px;
    background: var(--ui-field, #fffaf0);
    color: var(--ui-ink, inherit);
    box-sizing: border-box;
}

.chapter-schedule-note {
    margin-top: 7px;
}

.chapter-schedule-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}

@media (max-width: 460px) {
    .chapter-schedule-panel {
        margin-inline: -8px;
        border-radius: 0;
        border-left: 0;
        border-right: 0;
    }

    .chapter-schedule-heading {
        display: block;
    }

    .chapter-schedule-timezone {
        display: inline-block;
        margin-top: 8px;
    }

    .chapter-schedule-panel input[type="datetime-local"] {
        width: 100%;
    }

    .chapter-schedule-actions > button {
        flex: 1 1 140px;
    }
}


/* ---------- v17 chapter-toolbar fit pass ---------- */
@media (min-width: 901px) {
    .chapter-editor-shell .chapter-toolbar {
        gap: 5px !important;
        padding-inline: 5px !important;
    }
    .chapter-editor-shell .chapter-toolbar button {
        padding-inline: 10px !important;
    }
    .chapter-editor-shell .chapter-toolbar select {
        min-width: 78px !important;
        max-width: 190px;
    }
    .chapter-editor-shell #chapter-font { min-width: 118px !important; }
    .chapter-editor-shell #chapter-size { min-width: 82px !important; }
    .chapter-editor-shell #chapter-align { min-width: 88px !important; }
    .chapter-editor-shell .scene-break-button { padding-inline: 11px !important; }
    .chapter-editor-shell .text-conversation-button { padding-inline: 11px !important; }
    .chapter-editor-shell #writer-tools-toggle { padding-inline: 11px !important; }
}

@media (max-width: 900px) {
    /* A little trailing breathing room keeps the last toolbar control from
       looking accidentally sliced off while still allowing horizontal swipe. */
    .chapter-editor-shell .chapter-toolbar::after {
        content: "";
        flex: 0 0 12px;
        width: 12px;
        height: 1px;
    }
    .chapter-editor-shell .chapter-toolbar {
        scroll-padding-inline: 8px 20px;
    }
}

/* Edit Work chapter state action wrapper added in v17. */
.edit-work-chapter-state-action {
    display: contents;
}
@media (max-width: 600px) {
    body .edit-work-manager .edit-work-chapter-state-action {
        display: block;
        flex: 0 1 auto;
    }
    body .edit-work-manager .edit-work-chapter-state-action > button {
        width: auto !important;
        min-height: 36px !important;
        padding: 7px 12px !important;
        font-size: .83rem !important;
    }
}


/* ---------- v19: editor toolbar fit ---------- */
@media (min-width: 901px) {
    .chapter-editor-shell .chapter-toolbar {
        overflow-x: hidden !important;
        gap: 4px !important;
        padding-inline: 4px !important;
    }
    .chapter-editor-shell .chapter-toolbar button {
        padding-inline: 9px !important;
    }
    .chapter-editor-shell #chapter-font {
        min-width: 106px !important;
        max-width: 150px !important;
    }
    .chapter-editor-shell #chapter-size {
        min-width: 72px !important;
        max-width: 112px !important;
    }
    .chapter-editor-shell #chapter-align {
        min-width: 78px !important;
        max-width: 104px !important;
    }
    .chapter-editor-shell .scene-break-button,
    .chapter-editor-shell .text-conversation-button,
    .chapter-editor-shell #writer-tools-toggle {
        padding-inline: 9px !important;
        white-space: nowrap !important;
    }
}


/* ---------- v20: chapter toolbar clipping repair ---------- */
.chapter-editor-shell .chapter-writing-section,
.chapter-editor-shell .chapter-writing-section > .form-field {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
}

@media (min-width: 901px) {
    /* Desktop should show every tool. If the window is a little narrow, wrap
       cleanly instead of silently chopping the last control off. */
    .chapter-editor-shell .chapter-toolbar {
        display: flex !important;
        flex-wrap: wrap !important;
        overflow-x: visible !important;
        overflow-y: visible !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        box-sizing: border-box !important;
        gap: 5px !important;
        padding: 7px 5px !important;
    }

    .chapter-editor-shell .chapter-toolbar::after {
        display: none !important;
    }

    .chapter-editor-shell .chapter-toolbar > * {
        flex: 0 0 auto !important;
        max-width: 100% !important;
    }

    .chapter-editor-shell #chapter-font {
        min-width: 104px !important;
        width: 132px !important;
        max-width: 132px !important;
    }

    .chapter-editor-shell #chapter-size {
        min-width: 76px !important;
        width: 90px !important;
        max-width: 90px !important;
    }

    .chapter-editor-shell #chapter-align {
        min-width: 82px !important;
        width: 96px !important;
        max-width: 96px !important;
    }

    .chapter-editor-shell .scene-break-button,
    .chapter-editor-shell .text-conversation-button,
    .chapter-editor-shell #writer-tools-toggle {
        padding-inline: 10px !important;
        white-space: nowrap !important;
    }
}

@media (max-width: 900px) {
    /* Phones keep the one-line writing-app toolbar. It scrolls horizontally
       rather than clipping, with enough tail room to reveal the final tool. */
    .chapter-editor-shell .chapter-toolbar {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        padding: 8px 8px 8px 6px !important;
        box-sizing: border-box !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-inline: contain;
        scrollbar-width: none;
    }

    .chapter-editor-shell .chapter-toolbar::-webkit-scrollbar {
        display: none !important;
    }

    .chapter-editor-shell .chapter-toolbar > * {
        flex: 0 0 auto !important;
    }

    .chapter-editor-shell .chapter-toolbar::after {
        content: "" !important;
        display: block !important;
        flex: 0 0 22px !important;
        width: 22px !important;
        height: 1px !important;
    }
}
