/* Markdown letter modal — editorial paper with per-letter EN/FR toggle */
.modal-root {
    position: fixed; inset: 0; z-index: 60;
    display: none;
    align-items: center; justify-content: center;
    padding: 16px;
    padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.modal-root.open { display: flex; animation: modalFadeIn var(--t-mid) ease both; }
@keyframes modalFadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-backdrop {
    position: absolute; inset: 0;
    background: rgba(58,42,44,.32);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.modal-paper {
    position: relative;
    width: min(720px, 100%);
    max-height: 88vh;
    overflow-y: auto;
    background:
      linear-gradient(180deg, rgba(255,255,255,.6) 0%, rgba(255,255,255,0) 6%),
      linear-gradient(180deg, var(--paper) 0%, #fff5ee 100%);
    border: 1px solid var(--paper-edge);
    border-radius: 24px;
    box-shadow: 0 40px 100px -30px rgba(146,82,106,.5);
    padding: 36px clamp(22px, 5vw, 56px) 36px;
    animation: paperUp var(--t-mid) cubic-bezier(.2,.7,.2,1) both;
    -webkit-overflow-scrolling: touch;
}
@keyframes paperUp { from { opacity: 0; transform: translateY(14px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } }

/* Sending → letter has been slipped away. The compose paper itself does a
   small lift-and-fade so the modal feels like it's stepping aside for the
   slip overlay rather than being yanked away. */
.modal-paper.sending {
    animation: paperOff 360ms cubic-bezier(.4,.7,.2,1) both;
}
@keyframes paperOff {
    0%   { opacity: 1; transform: translateY(0) scale(1); }
    60%  { opacity: .35; transform: translateY(-6px) scale(.985); }
    100% { opacity: 0; transform: translateY(-22px) scale(.96); }
}

/* Modal top controls — close button + per-letter language toggle */
.modal-controls {
    position: sticky; top: 0;
    display: flex; align-items: center; justify-content: space-between;
    gap: 10px;
    padding-bottom: 14px;
    margin-bottom: 6px;
    z-index: 4;
    background: linear-gradient(180deg, var(--paper) 0%, var(--paper) 60%, rgba(255,249,245,0));
}

.letter-controls-right { display: inline-flex; align-items: center; gap: 8px; }
.letter-delete {
    width: 36px; height: 36px;
    display: grid; place-items: center;
    border-radius: 50%;
    color: var(--ink-faint);
    background: rgba(255,255,255,.7);
    border: 1px solid var(--hairline);
    transition: all var(--t-fast);
    flex-shrink: 0;
}
.letter-delete:hover { color: var(--rose-deep); border-color: rgba(217,122,139,.45); }

/* Retry-translation — sits beside delete, only shown to the author of a
   partially-translated firestore letter. Click → calls Gemini again and
   patches the missing language in place. */
.letter-retry-translate {
    width: 36px; height: 36px;
    display: grid; place-items: center;
    border-radius: 50%;
    color: var(--ink-faint);
    background: rgba(255,255,255,.7);
    border: 1px solid var(--hairline);
    transition: all var(--t-fast);
    flex-shrink: 0;
    position: relative;
}
.letter-retry-translate:hover { color: var(--rose-deep); border-color: rgba(217,122,139,.45); }
body[data-current-role="hins"] .letter-retry-translate:hover { color: var(--hins-blue); border-color: rgba(58,109,171,.45); }
.letter-retry-translate .retry-icon { transition: transform .35s ease; }
.letter-retry-translate:hover .retry-icon { transform: rotate(-60deg); }

.letter-retry-translate .retry-spinner {
    position: absolute;
    inset: 6px;
    border: 2px solid rgba(178, 88, 103, .25);
    border-top-color: var(--rose-deep);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
    display: none;
}
body[data-current-role="hins"] .letter-retry-translate .retry-spinner {
    border-color: rgba(58,109,171,.25);
    border-top-color: var(--hins-blue);
}
.letter-retry-translate.busy { pointer-events: none; }
.letter-retry-translate.busy .retry-icon { opacity: 0; }
.letter-retry-translate.busy .retry-spinner { display: block; }

.modal-paper .close,
.thread-modal-paper .close {
    width: 36px; height: 36px;
    display: grid; place-items: center;
    border-radius: 50%;
    color: var(--ink-soft);
    background: rgba(255,255,255,.7);
    border: 1px solid var(--hairline);
    transition: all var(--t-fast);
    flex-shrink: 0;
}
.modal-paper .close:hover,
.thread-modal-paper .close:hover { color: var(--rose-deep); transform: rotate(90deg); }

/* Per-letter EN/FR toggle */
.letter-lang {
    position: relative;
    display: inline-flex;
    align-items: center;
    background: rgba(255,255,255,.65);
    border: 1px solid var(--hairline);
    border-radius: 999px;
    padding: 3px;
    font-size: 0.7rem;
    font-weight: 500;
    letter-spacing: .04em;
    user-select: none;
}
.letter-lang button {
    position: relative; z-index: 2;
    padding: 5px 11px;
    color: var(--ink-soft);
    border-radius: 999px;
    transition: color var(--t-fast);
    min-width: 28px;
}
.letter-lang button[aria-pressed="true"] { color: white; }
.letter-lang::before {
    content: '';
    position: absolute; top: 3px; bottom: 3px;
    width: calc(50% - 3px);
    background: linear-gradient(135deg, var(--pink-300), var(--rose-deep));
    border-radius: 999px;
    transition: transform var(--t-mid);
    box-shadow: 0 4px 14px -4px rgba(178,88,103,.5);
    left: 3px;
}
.letter-lang[data-active="fr"]::before { transform: translateX(100%); }

/* Missing-language button — when a letter only has one language (because
   the Gemini translation didn't go through), we DISABLE that side instead
   of hiding the toggle. Reading "FR" with a strikethrough makes it obvious
   why nothing happens on click. */
.letter-lang button.letter-lang-missing,
.letter-lang button[disabled] {
    color: var(--ink-faint);
    text-decoration: line-through;
    text-decoration-color: rgba(178, 88, 103, .55);
    text-decoration-thickness: 1px;
    cursor: not-allowed;
    opacity: .55;
}
.letter-lang[data-partial="1"]::before {
    /* Hide the moving pill on a partial-translation letter — there's nothing
       for it to "select" between. */
    display: none;
}
.letter-lang[data-partial="1"] button[aria-pressed="true"] {
    color: var(--ink);
}

/* Blue accent on the toggle pill when Hins is signed in (used by compose). */
body[data-current-role="hins"] .compose-lang::before {
    background: linear-gradient(135deg, var(--hins-blue-soft), var(--hins-blue));
    box-shadow: 0 4px 14px -4px rgba(58, 109, 171, .5);
}

/* Letter view itself */
.letter-view header { margin-bottom: 22px; }
.letter-view .letter-meta {
    display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
    font-size: 0.7rem; color: var(--ink-faint);
    text-transform: uppercase; letter-spacing: .16em;
    margin-bottom: 12px;
}
.letter-view .letter-title {
    margin: 0;
    font-family: 'Fraunces', serif;
    font-style: italic;
    font-weight: 400;
    font-size: clamp(1.8rem, 4vw, 2.6rem);
    line-height: 1.1;
    color: var(--ink);
}
.letter-view .ornament {
    display: block;
    margin: 22px auto 22px;
    opacity: .6;
}
.letter-body {
    font-family: 'Cormorant Garamond', serif;
    font-size: 1.22rem;
    line-height: 1.78;
    color: var(--ink);
    word-wrap: break-word;
}
.letter-body p { margin: 0 0 18px; }
.letter-body p:first-of-type::first-letter {
    font-family: 'Fraunces', serif;
    font-style: italic;
    font-size: 3.4rem;
    line-height: 1;
    float: left;
    padding: 6px 10px 0 0;
    color: var(--rose-deep);
}
.letter-body em      { color: var(--rose-deep); }
.letter-body strong  { color: var(--ink); font-weight: 600; }
.letter-body h1, .letter-body h2, .letter-body h3 {
    font-family: 'Fraunces', serif; font-style: italic; font-weight: 400;
    margin: 28px 0 10px; color: var(--ink);
}
.letter-body h1 { font-size: 1.8rem; }
.letter-body h2 { font-size: 1.45rem; }
.letter-body h3 { font-size: 1.2rem; }
.letter-body blockquote {
    margin: 22px 0;
    padding: 4px 0 4px 18px;
    border-left: 2px solid var(--pink-300);
    color: var(--ink-soft); font-style: italic;
}
.letter-body ul, .letter-body ol { margin: 0 0 18px 1.4em; }
.letter-body li { margin-bottom: 6px; }
.letter-body a {
    color: var(--rose-deep);
    text-decoration: underline;
    text-decoration-color: var(--pink-200);
    text-underline-offset: 3px;
    word-break: break-word;
}
.letter-body code {
    background: var(--blush);
    color: var(--wine);
    padding: 1px 6px; border-radius: 4px;
    font-family: 'JetBrains Mono', monospace; font-size: .9em;
}
.letter-body img {
    display: block;
    max-width: 100%;
    width: 100%;
    height: auto;
    border-radius: 12px;
    margin: 18px auto;
    background: var(--blush);
    box-shadow: 0 18px 36px -22px rgba(146,82,106,.45);
    transition: transform var(--t-mid);
}
.letter-body img:hover { transform: scale(1.01); }
.letter-body p:has(> img:only-child) { text-align: center; margin: 0 0 12px; }
.letter-body p:has(> img:only-child) + p {
    /* small italic caption directly after a standalone image */
    margin-top: -6px;
    text-align: center;
    font-style: italic;
    color: var(--ink-soft);
    font-size: 0.95rem;
}
.letter-body hr {
    border: 0;
    height: 14px;
    background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 60 14' fill='none'><path d='M0 7 H22' stroke='%23d97a8b' stroke-width='1'/><path d='M38 7 H60' stroke='%23d97a8b' stroke-width='1'/><circle cx='30' cy='7' r='3' stroke='%23d97a8b' fill='%23fdf6f3'/></svg>") center/auto 100% no-repeat;
    margin: 28px 0;
    opacity: .7;
}

.letter-view footer {
    margin-top: 30px;
    padding-top: 22px;
    border-top: 1px solid var(--hairline-2);
    display: flex; justify-content: space-between; align-items: center;
    gap: 12px; flex-wrap: wrap;
}
.letter-view .signature { font-family: 'Caveat', cursive; font-size: 1.4rem; color: var(--rose-deep); }
