@use '../abstracts' as *;

// ============================================================
// Prose — the rules post content shares between the front end
// (.entry-content, wordpress/_content.scss) and the block editor's canvas
// (editor.scss). Include it inside the content container.
//
// From the template's .post-content, merged with the toolkit's
// classic-theme-kit (_blocks.scss, PLAYBOOK). What the template never shows
// (tables, code, pullquotes …) is built from the same tokens and feel.
// ============================================================

@mixin rules {
  font-size: 17.5px; font-weight: 300; color: var(--prose);

  @include below(mobile) { font-size: 16.5px; }

  // ---- Rhythm: the owl — resets in :where(), before the owl (PLAYBOOK §1) ----
  // (headings too: the editor canvas has no global reset, the front end does)
  :where(p, ul, ol, dl, figure, blockquote, pre, table, h1, h2, h3, h4, h5, h6) { margin: 0; }
  > * + * { margin-top: 1.5em; }

  // ---- Headings ----
  :where(h1, h2, h3, h4, h5, h6) {
    font-family: $font-serif;
    font-weight: 400;
    color: var(--ink);
    line-height: 1.5;
  }

  h1 { font-size: 32px; margin-top: 2em; }
  h2 { font-size: 27px; margin-top: 2em; }
  h3 { font-size: 22px; margin-top: 1.8em; }
  h4 { font-size: 19px; margin-top: 1.6em; }
  h5, h6 { font-size: 17px; margin-top: 1.5em; }
  h6 { color: var(--ink-soft); }

  > :first-child { margin-top: 0; }

  .has-drop-cap:not(:focus)::first-letter {
    font-family: $font-serif;
    color: var(--accent-deep);
  }

  .has-drop-cap:not(:focus)::after {
    content: "";
    display: block;
    clear: both;
  }

  // ---- Links ----
  // Colour inside :where(), so a link colour set in the editor wins
  // (core-css-traps v6: "the prose link rule outranks an author's link colour")
  a:where(:not(.wp-element-button):not(.wp-block-button__link)) {
    text-decoration: underline;
    text-underline-offset: 3px;
    text-decoration-color: var(--rule-strong);

    &:hover { text-decoration-color: currentColor; }
  }

  a:where(:not(.wp-element-button):not(.wp-block-button__link):not(.has-link-color *)) {
    color: var(--accent-deep);
  }

  strong, b { font-weight: 600; color: var(--ink); }

  // Justified text keeps the author's choice; hyphenation closes the wide
  // gaps it opens in a narrow column
  .has-text-align-justify,
  [style*="text-align:justify"],
  [style*="text-align: justify"] { hyphens: auto; }

  // ---- Lists ----
  // Only lists an author writes: a list a block builds for its own layout
  // (Navigation, Query Loop, Comments, Latest Posts, Social Links …) carries
  // a wp-block-* class and keeps core's look (unit-test-findings, "Theme block
  // category" v5). Archives, Categories and Page List are drawn as plain
  // bulleted lists, as TT1 does. :where() keeps each rule's specificity.
  $prose-list: ':where(:not([class*="wp-block-"]):not(.blocks-gallery-grid), .wp-block-list, .wp-block-archives-list, .wp-block-categories-list, .wp-block-page-list, .wp-block-page-list ul)';

  ul#{$prose-list}, ol#{$prose-list} { padding-left: 1.4em; }
  :is(ul, ol)#{$prose-list} > li + li { margin-top: 0.5em; }
  li > :is(ul, ol)#{$prose-list} { margin-top: 0.5em; }
  :is(ul, ol)#{$prose-list} > li::marker { color: var(--accent); }

  dt { font-weight: 500; color: var(--ink); }
  dd { margin-left: 1.4em; }
  dd + dt { margin-top: 0.8em; }

  // ---- Quotes ----
  blockquote:not(.wp-block-pullquote blockquote) {
    background: var(--glass);
    @include glass-blur(8px);
    border: 1px solid var(--hairline);
    border-left: 4px solid var(--accent);
    border-radius: var(--radius-md);
    padding: 26px 30px;
    font-family: $font-serif;
    font-style: italic; font-size: 19px; color: var(--ink);
    box-shadow: var(--shadow-soft);

    > * + * { margin-top: 0.8em; }

    cite,
    .wp-block-quote__citation {
      display: block; margin-top: 12px;
      font-family: $font-sans;
      font-style: normal; font-size: 14px; color: var(--ink-faint);
    }
  }

  // Large quote style — same box, bigger text
  .wp-block-quote.is-style-large:not(.is-style-plain) {
    font-size: 23px;
    padding: 30px 34px;

    p { font-size: inherit; font-style: inherit; }
  }

  // ---- Pullquote — rules above and below, centred (PLAYBOOK §3, §6) ----
  .wp-block-pullquote {
    padding: 28px 0;
    border-top: 1px solid var(--rule-strong);
    border-bottom: 1px solid var(--rule-strong);
    text-align: center;

    blockquote { margin: 0; padding: 0; border: none; background: none; box-shadow: none; }

    p {
      font-family: $font-serif;
      font-style: italic;
      font-size: 23px;
      line-height: 1.6;
      color: var(--ink);
    }

    cite {
      display: block; margin-top: 12px;
      font-style: normal; font-size: 14px; color: var(--ink-faint);
    }

    // WordPress 5.0 saved an inline border-color, and core's
    // :where([style*=border-color]) { border-style: solid } now draws all
    // four sides: a box. Keep the design's rules above and below only.
    &:not(.is-style-solid-color) { border-left: 0; border-right: 0; }

    &.is-style-solid-color {
      padding: 36px 32px;
      border: none;
      border-radius: var(--radius-md);

      // Core's 32px is larger than the post title on a phone; the same size
      // as the regular pullquote, centred like it
      blockquote { max-width: 100%; text-align: center; }
      blockquote p { font-size: 23px; }
    }
  }

  // A right-aligned quote takes its bar with it
  .wp-block-quote.has-text-align-right:not(.is-style-plain) {
    border-left: 1px solid var(--hairline);
    border-right: 4px solid var(--accent);
  }

  // ---- Images and captions ----
  figure img,
  .wp-block-image img {
    border-radius: var(--radius-md);
    filter: var(--img-filter);
  }

  // Template: figures get 2em above and below (not the owl's 1.5em)
  // (top margin only after a sibling — 0 when the post opens with an image)
  > figure:not(.alignleft):not(.alignright) { margin-bottom: 2em; }
  > * + figure:not(.alignleft):not(.alignright) { margin-top: 2em; }

  // Never wider than its own size: a 150px image stays 150px (core never
  // enlarges an Image block; unit-test-findings "Images and floats")
  figure:not(.wp-block-gallery):not(.wp-block-table):not(.wp-block-embed) > img {
    display: block;
    max-width: 100%;
    height: auto;
    box-shadow: var(--shadow-soft);
  }

  figcaption,
  .wp-element-caption,
  .wp-caption-text,
  .gallery-caption {
    text-align: center; font-size: 13.5px; color: var(--ink-faint); margin-top: 12px;
    margin-bottom: 0; // cancels core's 1em on .wp-block-image figcaption
  }

  // ---- Code ----
  code, kbd, samp {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.88em;
  }

  :not(pre) > :is(code, kbd) {
    padding: 2px 6px;
    border-radius: 6px;
    background: var(--glass-strong);
    color: var(--ink);
  }

  pre,
  .wp-block-code,
  .wp-block-preformatted {
    padding: 20px 24px;
    border-radius: var(--radius-md);
    background: var(--code-bg);
    color: var(--code-ink);
    font-size: 14.5px;
    line-height: 1.7;
    white-space: pre;  // cancels core's pre-wrap: layout kept, scrolls (PLAYBOOK §5)
    overflow-x: auto;

    code { padding: 0; background: none; color: inherit; font-size: inherit; white-space: inherit; }
  }

  // Verse — a <pre>, but poetry, not code (PLAYBOOK §5)
  .wp-block-verse {
    padding: 22px 26px;
    border-left: 3px solid var(--accent);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    background: var(--glass);
    color: var(--ink);
    font-family: $font-serif;
    font-size: inherit;
    font-style: italic;
    white-space: pre-wrap;
    overflow-wrap: break-word;
  }

  // ---- Tables ----
  table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15.5px;
  }

  th, td {
    padding: 10px 14px;
    border: 1px solid var(--rule-strong);
    text-align: left;
    vertical-align: top;
  }

  thead th, th { background: var(--glass); font-weight: 500; color: var(--ink); }

  .wp-block-table {
    overflow-x: auto;

    &.is-style-stripes {
      border-bottom: none;
      table { border-collapse: separate; border-spacing: 0; }
      // Core paints the odd rows #f0f0f0 on the <tr>: under our glass cells it
      // stayed light grey in dark mode, behind light text (axe, 1.4:1)
      tbody tr:nth-child(odd) { background-color: transparent; }
      tbody tr:nth-child(odd) td { background: var(--glass); }
      th, td { border-width: 0 0 1px; }
    }

    figcaption { margin-top: 10px; }
  }

  // ---- Separators ----
  hr,
  .wp-block-separator {
    height: 0;
    margin-top: 2.4em;
    margin-bottom: 2.4em;
    border: none;
    border-top: 1px solid var(--rule-strong);
    opacity: 1;
  }

  .wp-block-separator:not(.is-style-wide):not(.is-style-dots):not(.alignwide):not(.alignfull) {
    max-width: 96px;
    margin-inline: auto;
  }

  // Dots draw their row with ::before: the hr rule's height 0 hid it
  .wp-block-separator.is-style-dots {
    height: auto;
    overflow: visible;
    border: none;
    &::before { color: var(--ink-faint); }
  }

  // ---- Buttons ----
  .wp-block-button__link,
  .wp-element-button {
    display: inline-block;
    padding: 12px 28px;
    border-radius: $pill-radius;
    font-family: $font-sans;
    font-size: 15.5px;
    font-weight: 500;
    line-height: 1.4;
    text-decoration: none;
    transition: transform 0.25s ease;

    &:hover { transform: translateY(-2px); }
  }

  .wp-block-button__link:not(.has-background) { background: var(--btn-grad); box-shadow: var(--shadow-btn); }
  .wp-block-button__link:not(.has-text-color) { color: var(--on-accent); }

  // "Squared" — now squares the div in core, not the link
  .wp-block-button.is-style-squared > .wp-block-button__link { border-radius: 0; }

  // A Button saved without a Buttons block (WordPress 5.0's markup) gets no
  // centring: core's rule is in the Buttons block's sheet
  .wp-block-button.aligncenter { text-align: center; }

  .wp-block-button.is-style-outline > .wp-block-button__link {
    background: transparent;
    border: 1px solid currentColor;
    box-shadow: none;

    &:not(.has-text-color) { color: var(--accent-deep); }
  }

  // ---- Listing blocks in a post (Archives, Categories, Latest Posts …) ----
  // (the prose list rule above leaves these to core)
  .wp-block-latest-posts > li + li,
  .wp-block-rss > li + li { margin-top: 0.9em; }

  .wp-block-latest-posts__post-date,
  .wp-block-latest-comments__comment-date,
  .wp-block-rss__item-publish-date {
    font-size: 13.5px;
    color: var(--ink-faint);
  }

  .wp-block-latest-comments__comment-excerpt p {
    font-size: 15px;
    color: var(--ink-soft);
  }

  // The tag cloud as the post's own tag chips (core sets an inline size on
  // each tag — PLAYBOOK §12)
  .wp-block-tag-cloud {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;

    a {
      padding: 6px 16px;
      border: 1px solid var(--hairline);
      border-radius: $pill-radius;
      background: var(--glass);
      color: var(--ink-soft);
      font-size: 13.5px !important;
      line-height: 1.75;
      text-decoration: none;

      &:hover { color: var(--accent-deep); }
    }
  }

  // Dropdowns (Archives, Categories): the design's underlined field
  select {
    @include underline-field;
    flex: none;
    max-width: 100%;
    padding-right: 24px;
    cursor: pointer;
  }

  .wp-block-archives-dropdown label,
  .wp-block-categories-dropdown label,
  .wp-block-search__label {
    display: block;
    font-size: 14px;
    color: var(--ink-soft);
  }

  // The Search block as the theme's search form
  .wp-block-search__inside-wrapper {
    display: flex;
    gap: 12px;
    max-width: 420px;
    padding: 0;
    border: 0;
  }

  .wp-block-search__input { @include underline-field; }

  .wp-block-search__button {
    @include text-button;
    margin: 0;
    box-shadow: none;
    transform: none;
  }

  // ---- Theme blocks in a post (unit-test-findings "Theme block category") ----
  // A title block's link is a title, not a prose link
  .wp-block-site-title a,
  .wp-block-post-title a {
    color: inherit;
    text-decoration: none;

    &:hover { color: var(--accent-deep); }
  }

  // Query pagination: the theme's page chips
  .wp-block-query-pagination {
    gap: 8px;
    font-size: 14.5px;

    .page-numbers { @include page-chip; }

    a:not(.page-numbers) { color: var(--ink-soft); text-decoration: none; }
    a:not(.page-numbers):hover { color: var(--accent-deep); }
  }

  // The Comments block: the theme's comment type
  .wp-block-comment-author-name a,
  .wp-block-comment-date a,
  .wp-block-comment-reply-link a,
  .wp-block-comment-edit-link a { text-decoration: none; }

  .wp-block-comment-author-name a { color: var(--ink); font-weight: 500; }
  .wp-block-comment-date a { color: var(--ink-faint); }
  .wp-block-comment-reply-link a { color: var(--accent-deep); }

  // ---- Cover ----
  // Covers saved before WordPress 5.4 keep the image as an inline
  // background-image, and core no longer sizes it: it showed at natural
  // size, tiled (unit-test-findings "The Cover's oldest markup")
  .wp-block-cover[style*="background-image"] {
    background-size: cover;
    background-position: center;
  }

  // On a photograph or an overlay the accent link vanishes: the cover's own
  // text colour, underlined (TT1)
  .wp-block-cover a { color: inherit; text-decoration-color: currentColor; }

  // ---- File ----
  // Core sizes it at 0.8em, which assumes a ~20px body (core-css-traps v6)
  .wp-block-file {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 14px;
    font-size: 15.5px;

    .wp-block-file__button {
      display: inline-block;
      padding: 6px 18px;
      border-radius: $pill-radius;
      background: var(--btn-grad);
      color: var(--on-accent);
      font-size: 14px;
      font-weight: 500;
      line-height: 1.5;
      text-decoration: none;
    }
  }

  // ---- Padding on coloured backgrounds (core misses some — PLAYBOOK §4) ----
  .wp-block-group.has-background { padding: 22px 26px; border-radius: var(--radius-md); }
  .wp-block-column.has-background { padding: 18px 22px; border-radius: var(--radius-md); }
  p.has-background { padding: 18px 22px; border-radius: var(--radius-md); }

  .wp-block-media-text.has-background {
    border-radius: var(--radius-md);
    overflow: hidden;
  }

  // Full width runs to the screen's edges: no corners there
  .wp-block-media-text.alignfull { border-radius: 0; }

  // Stacked on a phone, core puts the text 0px under the image (core stacks
  // at 600px)
  @media (max-width: 600px) {
    .wp-block-media-text.is-stacked-on-mobile .wp-block-media-text__content {
      padding-top: 18px;
      padding-bottom: 22px;
    }
  }

  // ---- Columns, galleries ----
  .wp-block-columns { gap: 24px; }

  .wp-block-gallery {
    gap: 10px;

    img { border-radius: var(--radius-sm); }
  }

  // ---- Left / right / centre alignments ----
  .alignleft {
    float: left;
    // 0.4em on top lines the image up with the text's first line (PLAYBOOK §8)
    margin: 0.4em 1.75em 1em 0;
    max-width: 50%;
  }

  .alignright {
    float: right;
    margin: 0.4em 0 1em 1.75em;
    max-width: 50%;
  }

  // A floated block comes before the paragraph it sits beside, and that
  // paragraph starts 1.5em (the owl) below the previous block. Add the same
  // 1.5em, so the image still lines up with the paragraph's first line.
  // (An image inside a paragraph keeps the plain 0.4em.)
  > * + .alignleft,
  > * + .alignright { margin-top: 1.9em; }

  .aligncenter {
    clear: both;
    margin-left: auto;
    margin-right: auto;
  }

  img.aligncenter { display: block; }

  // No room to wrap on phones: back in the flow, with the owl's gap
  @include below(mobile) {
    .alignleft,
    .alignright {
      float: none;
      max-width: 100%;
      margin: 0;
    }

    > * + .alignleft,
    > * + .alignright { margin-top: 1.5em; }
  }
}
