/* ══════════════════════════════════════════════════════════════════════════
   Post content — SSR canonical block rendering
   Companion to app/Libraries/RenderEngine/Shared/PostContentRenderer.php.

   ── Design rule ────────────────────────────────────────────────────────
   The editor is the source of truth. Every rule here must reproduce what
   the author sees inside the contenteditable. When the two diverge, this
   file is wrong.

   ── File order is load-bearing ─────────────────────────────────────────
   Section 4 (WRAP MODES) comes AFTER section 3 (components) on purpose.
   The wrap rules and the component rules have identical specificity
   (0,2,0), so source order decides the winner. Put the wrap rules first
   and every figure / shape / button silently loses its float the moment
   it also matches a component rule. Do not reorder these sections.

   Structure:
     1. Container + base
     2. Typography (headings, bold, links, lists, inline)
     3. Components (default layout for each block type)
     4. WRAP MODES  ← must be after 3
     5. Responsive collapse
     6. Reduced motion
   ══════════════════════════════════════════════════════════════════════════ */

/* ══════════════════════════════════════════════════════════════════════════
   1. Container + base
   ══════════════════════════════════════════════════════════════════════════ */

.ps-post-content {
    /* container-type: inline-size does two useful things here:
       (a) it establishes a container query context so [data-ps-wrap="wide"]
           and ["full"] can break out relative to this element's width,
           not the viewport;
       (b) it creates a new block formatting context, which means floats
           are CONTAINED — a floated figure at the bottom of the post
           cannot push the following footer out of place. */
    container-type: inline-size;
    --ps-content-gap: 1.5rem;
    --ps-content-radius: 8px;
    --ps-content-accent: var(--ac, #4f46e5);
}

.ps-post-content > * + * { margin-top: var(--ps-content-gap); }
.ps-post-content p { margin: 0 0 1em; }
.ps-post-content p:last-child { margin-bottom: 0; }

/* Explicit clearfix for the case where container-type containment is
   unsupported (very old Safari). Harmless when the BFC above is active. */
.ps-post-content::after { content: ""; display: table; clear: both; }

/* ══════════════════════════════════════════════════════════════════════════
   2. Typography
   ══════════════════════════════════════════════════════════════════════════ */

/* ── Inline emphasis ───────────────────────────────────────────────────
   The host shell may ship a CSS reset that flattens <b>/<strong> to
   normal weight — Tailwind v4 preflight, a custom base.css, prose
   utilities. Two-class specificity beats any element-only reset.

   700 rather than "bolder": 700 maps to a weight Lora (and every other
   catalogue serif) actually ships, so the browser uses real bold.
   "bolder" resolves to a synthetic approximation when the weight is
   absent — the smeared look that gives synthetic bold its bad name. */
.ps-post-content b,
.ps-post-content strong { font-weight: 700; }

/* ── Headings ──────────────────────────────────────────────────────────
   Inline style wins by cascade order — an author-styled heading keeps
   its font-size / weight. */
.ps-post-content h1,
.ps-post-content h2,
.ps-post-content h3,
.ps-post-content h4,
.ps-post-content h5,
.ps-post-content h6 {
    font-family: var(--ff-display, inherit);
    font-weight: 600;
    letter-spacing: -0.02em;
    color: var(--ink, #0f172a);
    margin: 2em 0 .6em;
    line-height: 1.2;
}
.ps-post-content h1 { font-size: 2rem; }
.ps-post-content h2 { font-size: 1.75rem; }
.ps-post-content h3 { font-size: 1.375rem; }
.ps-post-content h4 { font-size: 1.125rem; }
.ps-post-content h5,
.ps-post-content h6 { font-size: 1rem; }

/* ── Links ─────────────────────────────────────────────────────────── */
.ps-post-content a {
    color: var(--ps-content-accent);
    text-decoration: underline;
    text-decoration-color: color-mix(in srgb, var(--ps-content-accent) 35%, transparent);
    text-underline-offset: 3px;
    transition: text-decoration-color .2s ease;
}
.ps-post-content a:hover { text-decoration-color: var(--ps-content-accent); }

/* Elements that are links but must not look like prose links. */
.ps-post-content .ps-btn,
.ps-post-content .ps-btn:hover,
.ps-post-content figure a,
.ps-post-content .ps-gallery a,
.ps-post-content .ps-embed a {
    text-decoration: none;
    color: inherit;
}

/* ── Lists ─────────────────────────────────────────────────────────── */
.ps-post-content ul,
.ps-post-content ol { padding-left: 1.5em; margin: 0.5em 0; }
.ps-post-content ul { list-style: disc; }
.ps-post-content ol { list-style: decimal; }
.ps-post-content ul ul     { list-style: circle; }
.ps-post-content ul ul ul  { list-style: square; }
.ps-post-content li { margin: .2em 0; }

/* ── Inline elements ───────────────────────────────────────────────── */
.ps-post-content mark {
    background: #fef08a;
    padding: 0 .15em;
    border-radius: 3px;
}
.ps-post-content code {
    background: #f1f5f9;
    padding: .12em .4em;
    border-radius: 5px;
    font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
    font-size: .9em;
}
.ps-post-content pre {
    background: #0f172a;
    color: #e2e8f0;
    padding: 1.1em 1.35em;
    border-radius: var(--ps-content-radius);
    overflow-x: auto;
    font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
    font-size: .875em;
    line-height: 1.6;
}
.ps-post-content pre code { background: none; padding: 0; color: inherit; font-size: inherit; }
.ps-post-content blockquote {
    border-left: 3px solid var(--ps-content-accent);
    padding: .25em 0 .25em 1.35em;
    margin: var(--ps-content-gap) 0;
    font-style: italic;
    color: var(--muted, #475569);
}

/* ══════════════════════════════════════════════════════════════════════════
   3. Components — default (unwrapped) layout for each block type
   ══════════════════════════════════════════════════════════════════════════ */

/* ── 3.1 Figure / image ─────────────────────────────────────────────── */

.ps-post-content .ps-figure {
    margin: var(--ps-content-gap) 0;
    padding: 0;
    box-sizing: border-box;
}
.ps-post-content .ps-figure img {
    display: block;
    width: var(--ps-img-w, 100%);
    max-width: 100%;
    height: auto;
    margin-inline: auto;
    border-radius: var(--ps-img-radius, var(--ps-content-radius));
    object-fit: var(--ps-img-fit, cover);
}
.ps-post-content .ps-figure figcaption {
    margin-top: .65em;
    font-size: .875em;
    font-style: italic;
    color: var(--muted, #64748b);
    text-align: center;
}

/* ── 3.2 Gallery ────────────────────────────────────────────────────── */

.ps-post-content .ps-gallery {
    display: grid;
    grid-template-columns: repeat(var(--ps-gallery-cols, 3), minmax(0, 1fr));
    gap: var(--ps-gallery-gap, 12px);
    margin: var(--ps-content-gap) 0;
}
.ps-post-content .ps-gallery .ps-figure { margin: 0; }

/* ── 3.3 Video / embed ──────────────────────────────────────────────── */

.ps-post-content .ps-embed {
    position: relative;
    width: var(--ps-embed-w, 100%);
    aspect-ratio: var(--ps-embed-aspect, 16 / 9);
    margin: var(--ps-content-gap) 0;
    border-radius: var(--ps-content-radius);
    overflow: hidden;
    background: #0f172a;
}
.ps-post-content .ps-embed iframe,
.ps-post-content .ps-embed video {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
}

/* ── 3.4 Table ─────────────────────────────────────────────────────── */

.ps-post-content .ps-table {
    width: 100%;
    border-collapse: collapse;
    margin: var(--ps-content-gap) 0;
    font-size: .95em;
    table-layout: auto;
}
.ps-post-content .ps-table th,
.ps-post-content .ps-table td {
    padding: .7em .95em;
    border: 1px solid var(--line, #e2e8f0);
    vertical-align: top;
    text-align: left;
    word-break: break-word;
}
.ps-post-content .ps-table thead th {
    background: #f8fafc;
    font-weight: 600;
    color: var(--ink, #0f172a);
}
.ps-post-content .ps-table tbody tr:nth-child(even) { background: #fbfcfe; }

/* ── 3.5 Button ────────────────────────────────────────────────────── */

.ps-post-content .ps-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .5em;
    padding: var(--ps-btn-pad-y, .75rem) var(--ps-btn-pad-x, 1.5rem);
    background: var(--ps-btn-bg, #4f46e5);
    color: var(--ps-btn-fg, #ffffff);
    border: var(--ps-btn-border, none);
    border-radius: var(--ps-btn-radius, 6px);
    font-weight: 600;
    font-size: 1em;
    line-height: 1.2;
    text-decoration: none;
    cursor: pointer;
    transition:
        background-color .18s ease,
        color .18s ease,
        transform .12s ease;
}
.ps-post-content .ps-btn:hover {
    background: var(--ps-btn-hover-bg, var(--ps-btn-bg, #4338ca));
    color: var(--ps-btn-hover-fg, var(--ps-btn-fg, #ffffff));
    text-decoration: none;
}

/* ── 3.6 Icon ──────────────────────────────────────────────────────── */

.ps-post-content .ps-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--ps-icon-size, 1.25em);
    height: var(--ps-icon-size, 1.25em);
    color: var(--ps-icon-fg, currentColor);
    vertical-align: -0.18em;
    line-height: 1;
    transition: color .18s ease;
}
.ps-post-content .ps-icon:hover {
    color: var(--ps-icon-hover-fg, var(--ps-icon-fg, currentColor));
}
.ps-post-content .ps-icon svg,
.ps-post-content .ps-icon svg * {
    width: 100%;
    height: 100%;
    display: block;
    fill: currentColor;
    stroke: currentColor;
}

/* ── 3.7 Divider ───────────────────────────────────────────────────── */

.ps-post-content .ps-divider {
    display: block;
    width: var(--ps-div-width, 100%);
    margin: var(--ps-div-margin, 1.5rem 0);
    border: 0;
    border-top: var(--ps-div-weight, 1px) solid var(--ps-div-color, #e2e8f0);
    height: 0;
    opacity: 1;
}

/* ── 3.8 Shape ─────────────────────────────────────────────────────── */

/* Default is BLOCK. editor/wrap.js declares DEFAULT_MODE = 'block' and
   removes the data-ps-wrap attribute entirely when a shape sits in its
   default mode — so a shape with no data-ps-wrap must produce block
   layout, not inline. */
.ps-post-content .ps-shape {
    display: block;
    margin-inline: auto;
    width: var(--ps-shape-w, 140px);
    height: var(--ps-shape-h, 140px);
    vertical-align: middle;
    opacity: var(--ps-shape-opacity, 1);
    transform: rotate(var(--ps-shape-rotate, 0deg));
    shape-outside: var(--ps-shape-outside, inset(0));
    overflow: visible;
}
.ps-post-content .ps-shape .ps-shape-main {
    fill: var(--ps-shape-fill, #4f46e5);
    stroke: var(--ps-shape-stroke, none);
    stroke-width: var(--ps-shape-stroke-w, 0);
    transition: fill .18s ease;
}
.ps-post-content .ps-shape:hover .ps-shape-main {
    fill: var(--ps-shape-hover-fill, var(--ps-shape-fill, #4f46e5));
}

/* ── 3.9 Equation ──────────────────────────────────────────────────── */

.ps-post-content .ps-equation {
    display: inline-block;
    font-size: var(--ps-eq-size, 1em);
    vertical-align: baseline;
    line-height: 1;
}
.ps-post-content .ps-equation--block {
    display: block;
    margin: var(--ps-content-gap) 0;
    text-align: center;
}
.ps-post-content .ps-equation:not([data-ps-rendered]) {
    min-width: 2em;
    min-height: 1.2em;
    background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
    background-size: 200% 100%;
    animation: ps-eq-shimmer 1.4s ease-in-out infinite;
    border-radius: 4px;
}
@keyframes ps-eq-shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}
.ps-post-content .ps-equation[data-ps-rendered] { background: none; animation: none; }

/* ── 3.10 Drop cap ─────────────────────────────────────────────────── */

.ps-post-content .ps-drop-cap {
    float: left;
    font-size: 3.2em;
    line-height: .85;
    font-weight: 700;
    padding: .05em .08em 0 0;
    margin: .05em .04em 0 0;
}

/* ══════════════════════════════════════════════════════════════════════════
   4. WRAP MODES — the final word on layout flow
   ══════════════════════════════════════════════════════════════════════════
   Eight values, defined by editor/wrap.js:

     inline | block | left | right | tight-left | tight-right | wide | full

   "block" is DEFAULT_MODE and is expressed by the ABSENCE of the
   attribute. So there is no rule that fires when no attribute is present
   — each component's own default (section 3) is what takes over.

   ── Why this section is last ────────────────────────────────────────────
   These rules and the component rules in section 3 have the SAME
   specificity (0,2,0). Source order is the tiebreaker. Wrapping rules
   must come after the component defaults they are meant to override.
   Moving this section above section 3 silently breaks every float.
   ══════════════════════════════════════════════════════════════════════════ */

.ps-post-content [data-ps-wrap] { --ps-wrap-gap: var(--ps-content-gap); }

/* ── inline ─────────────────────────────────────────────────────────── */
.ps-post-content [data-ps-wrap="inline"] {
    display: inline-block;
    float: none;
    vertical-align: middle;
    margin: 0 .35em;
    max-width: none;
}

/* ── block ──────────────────────────────────────────────────────────── */
.ps-post-content [data-ps-wrap="block"] {
    display: block;
    float: none;
    margin-inline: auto;
    max-width: none;
}

/* ── left / tight-left ──────────────────────────────────────────────── */
.ps-post-content [data-ps-wrap="left"],
.ps-post-content [data-ps-wrap="tight-left"] {
    display: block;
    float: left;
    margin-left: 0;
    margin-right: var(--ps-wrap-gap);
    max-width: 50%;
}

/* ── right / tight-right ────────────────────────────────────────────── */
.ps-post-content [data-ps-wrap="right"],
.ps-post-content [data-ps-wrap="tight-right"] {
    display: block;
    float: right;
    margin-left: var(--ps-wrap-gap);
    margin-right: 0;
    max-width: 50%;
}

/* ── tight-* get shape-outside when the element declares it ────────────
   The editor's shape.js sets --ps-shape-outside on the SVG when the
   author selects tight-left / tight-right (from ShapeLibrary's polygon
   data). For images and other non-shape elements the fallback
   `inset(0)` produces rectangular text-wrap — visually identical to
   plain left / right wrap. */
.ps-post-content [data-ps-wrap="tight-left"],
.ps-post-content [data-ps-wrap="tight-right"] {
    shape-margin: .5rem;
    shape-outside: var(--ps-shape-outside, inset(0));
}

/* ── wide ───────────────────────────────────────────────────────────── */
.ps-post-content [data-ps-wrap="wide"] {
    display: block;
    float: none;
    width: calc(100% + 6rem);
    max-width: none;
    margin-left: -3rem;
    margin-right: -3rem;
}

/* ── full ───────────────────────────────────────────────────────────── */
.ps-post-content [data-ps-wrap="full"] {
    display: block;
    float: none;
    width: 100cqw;
    max-width: 100cqw;
    margin-left: calc(50% - 50cqw);
    margin-right: calc(50% - 50cqw);
}

/* ── Wrapped-figure image fills its wrap box ──────────────────────────
   In wrap mode the FIGURE is the float, and the figure's width is
   dictated by the wrap rule (max-width: 50%). The image inside must
   fill that box — not honour its own --ps-img-w, which was set for the
   unwrapped layout where the figure spans the full column. Without this
   an image with `--ps-img-w: 300px` would poke out of a 50% figure on
   a narrow column. */
.ps-post-content .ps-figure[data-ps-wrap] img,
.ps-post-content [data-ps-wrap] .ps-figure img {
    width: 100%;
}

/* ── Wrapped shape rotates around its wrap origin, not its box ──────── */
.ps-post-content .ps-shape[data-ps-wrap="left"],
.ps-post-content .ps-shape[data-ps-wrap="right"],
.ps-post-content .ps-shape[data-ps-wrap="tight-left"],
.ps-post-content .ps-shape[data-ps-wrap="tight-right"] {
    transform-origin: center;
}

/* ══════════════════════════════════════════════════════════════════════════
   5. Responsive collapse
   ══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 640px) {
    /* Wide / full breakout is meaningless on a phone — one column is the
       full width. */
    .ps-post-content [data-ps-wrap="wide"],
    .ps-post-content [data-ps-wrap="full"] {
        width: 100%;
        margin-left: 0;
        margin-right: 0;
    }
    /* Floats collapse to centered blocks. Tight-wrap shape-outside is a
       desktop affordance; a phone has no room for text alongside an
       image. */
    .ps-post-content [data-ps-wrap="left"],
    .ps-post-content [data-ps-wrap="right"],
    .ps-post-content [data-ps-wrap="tight-left"],
    .ps-post-content [data-ps-wrap="tight-right"] {
        float: none;
        max-width: 100%;
        margin: 1em auto;
        shape-outside: none;
    }
    .ps-post-content .ps-table { min-width: 480px; }
    .ps-post-content { overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* ══════════════════════════════════════════════════════════════════════════
   6. Reduced motion
   ══════════════════════════════════════════════════════════════════════════ */

@media (prefers-reduced-motion: reduce) {
    .ps-post-content .ps-equation:not([data-ps-rendered]) { animation: none; }
    .ps-post-content .ps-btn,
    .ps-post-content .ps-icon,
    .ps-post-content .ps-shape .ps-shape-main { transition: none; }
}


/* ── 3.8 Shape ─────────────────────────────────────────────────────── */

.ps-post-content .ps-shape {
    display: block;
    margin-inline: auto;                 /* default: center */
    width: var(--ps-shape-w, 140px);
    height: var(--ps-shape-h, 140px);
    vertical-align: middle;
    opacity: var(--ps-shape-opacity, 1);
    transform: rotate(var(--ps-shape-rotate, 0deg));
    shape-outside: var(--ps-shape-outside, inset(0));
    overflow: visible;
}

/* Alignment — read the keyword the panel writes into the style attribute.
   --ps-shape-align is a keyword, so it can't feed margin-inline directly. */
.ps-post-content .ps-shape[style*="--ps-shape-align: left"],
.ps-post-content .ps-shape[style*="--ps-shape-align:left"] {
    margin-inline: 0 auto;
}
.ps-post-content .ps-shape[style*="--ps-shape-align: right"],
.ps-post-content .ps-shape[style*="--ps-shape-align:right"] {
    margin-inline: auto 0;
}

.ps-post-content .ps-shape .ps-shape-main {
    fill: var(--ps-shape-fill, #4f46e5);
    stroke: var(--ps-shape-stroke, none);
    stroke-width: var(--ps-shape-stroke-w, 0);
    transition: fill .18s ease;
}
.ps-post-content .ps-shape:hover .ps-shape-main {
    fill: var(--ps-shape-hover-fill, var(--ps-shape-fill, #4f46e5));
}
