/*
 * Blog article (show) and editor (edit/new).
 *
 * Headings are serif, everything else stays in the site sans. These rules are
 * deliberately unlayered with `.font-loaded` / `.trix-content` aware selectors:
 * actiontext.css resets `.trix-content * { margin: 0 }` and application.css sets
 * `.font-loaded * { font-family: Lexend }`, both of which beat Tailwind's layered
 * utilities, so utility classes on the article never took effect.
 *
 * Content comes from Trix: lines are <div>s separated by <br>, so paragraphs get
 * no margin of their own (the spacing is already in the markup).
 *
 * One column rhythm: header, body, footer and the sections around them share
 * --blog-measure; the cover image breaks out to --blog-wide.
 */

:root {
  --font-serif: "Newsreader", "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --blog-measure: 44rem;
  --blog-wide: 60rem;
}

.blog-column,
.blog-article .blog-header,
.blog-article .blog-body,
.blog-article .blog-footer {
  width: 100%;
  max-width: var(--blog-measure);
  margin-inline: auto;
}

/* ---------- Serif headings (everywhere in the blog) ---------- */

.blog-serif,
.font-loaded .blog-serif,
.font-loaded .blog-serif * {
  font-family: var(--font-serif);
  font-optical-sizing: auto;
}

.blog-title {
  font-size: clamp(2.125rem, 1.5rem + 2.8vw, 3.5rem);
  font-weight: 600;
  line-height: 1.08;
  letter-spacing: -0.022em;
  text-wrap: balance;
  overflow-wrap: break-word;
  color: var(--page-text);
}

.blog-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.75rem;
  margin-top: 1.25rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.blog-meta a {
  color: var(--page-text);
}

.blog-meta a:hover {
  text-decoration: underline;
}

.blog-cover {
  width: 100%;
  max-width: var(--blog-wide);
  margin: 2rem auto 2.5rem;
}

.blog-cover img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 1rem;
  box-shadow: var(--card-shadow);
}

/* Full bleed on phones: the page gutter is 1rem. */
@media (max-width: 639px) {
  .blog-cover {
    width: calc(100% + 2rem);
    max-width: none;
    margin-inline: -1rem;
  }

  .blog-cover img {
    border-radius: 0;
    box-shadow: none;
  }
}

.blog-footer {
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.875rem;
  color: var(--text-muted);
}

/* ---------- Prose: the rendered article and the Trix editor share this ---------- */

.blog-prose .trix-content,
trix-editor.blog-prose {
  font-size: 1.0625rem;
  line-height: 1.8;
  color: var(--blog-body);
  overflow-wrap: break-word;
  word-break: normal;
  hyphens: manual;
  -webkit-hyphens: manual;

  @media (min-width: 768px) {
    font-size: 1.125rem;
  }

  & :is(h1, h2, h3, h4, h5, h6),
  & :is(h1, h2, h3, h4, h5, h6) * {
    font-family: var(--font-serif);
    font-optical-sizing: auto;
  }

  & :is(h1, h2, h3, h4, h5, h6) {
    color: var(--page-text);
    font-weight: 600;
    line-height: 1.2;
    letter-spacing: -0.015em;
    text-wrap: balance;
    hyphens: manual;
    -webkit-hyphens: manual;
    margin: 1.5rem 0 0.5rem;
  }

  /* Trix drops trailing <br>s inside headings; they would leave a gap under the heading. */
  & :is(h1, h2, h3) br {
    display: none;
  }

  & :is(h1, h2, h3, h4) > strong {
    font-size: inherit;
    font-weight: inherit;
  }

  & h1 {
    font-size: 2rem;
    line-height: 1.15;
  }

  & h2 {
    font-size: 1.625rem;
  }

  & h3 {
    font-size: 1.3125rem;
  }

  & :is(h4, h5, h6) {
    font-size: 1.125rem;
  }

  @media (min-width: 768px) {
    & h1 {
      font-size: 2.375rem;
    }

    & h2 {
      font-size: 1.875rem;
    }

    & h3 {
      font-size: 1.4375rem;
    }
  }

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

  & strong {
    font-weight: 600;
    color: var(--page-text);
  }

  & em {
    font-style: italic;
  }

  & a {
    color: var(--admin-inline-link);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 0.2em;
    text-decoration-color: color-mix(in srgb, currentColor 45%, transparent);
    transition: text-decoration-color 150ms ease-out, color 150ms ease-out;
    overflow-wrap: anywhere;
  }

  & a:hover {
    color: var(--admin-inline-link-hover);
    text-decoration-color: currentColor;
  }

  & ul,
  & ol {
    margin: 0.5rem 0 1.5rem;
    padding-left: 1.5rem;
  }

  & ul {
    list-style: disc;
  }

  & ol {
    list-style: decimal;
  }

  & li {
    margin: 0 0 0.4rem;
    padding-left: 0.375rem;
  }

  & li::marker {
    color: var(--text-muted);
  }

  & :is(ul, ol) :is(ul, ol) {
    margin: 0.4rem 0 0.4rem;
  }

  & blockquote {
    margin: 2rem 0;
    padding: 0.25rem 0 0.25rem 1.25rem;
    border: 0 solid var(--pill-amber-text);
    border-left-width: 3px;
    font-family: var(--font-serif);
    font-size: 1.25em;
    font-style: italic;
    line-height: 1.55;
    color: var(--page-text);
  }

  & blockquote * {
    font-family: var(--font-serif);
  }

  & pre {
    display: block;
    width: 100%;
    margin: 1.75rem 0;
    padding: 1rem 1.25rem;
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    background: var(--surface);
    color: var(--page-text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.875rem;
    line-height: 1.65;
    white-space: pre;
    overflow-x: auto;
  }

  & code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9em;
  }

  & hr {
    margin: 2.5rem 0;
    border: 0;
    border-top: 1px solid var(--border);
  }

  & img {
    max-width: 100%;
    height: auto;
  }

  & .attachment {
    display: block;
    margin: 2rem 0;
  }

  & .attachment--preview {
    text-align: center;
  }

  & .attachment--preview img {
    display: inline-block;
    border-radius: 0.75rem;
  }

  & .attachment__caption {
    margin: 0.75rem 0 0;
    font-size: 0.875rem;
    line-height: 1.5;
    text-align: center;
    color: var(--text-muted);
  }

  & .attachment__caption::before {
    content: none;
  }

  & .attachment__caption .attachment__size {
    margin-left: 0.5rem;
  }
}

/* Reading view only: Trix separates paragraphs with empty <div><br></div> lines,
   which at full line height stack on top of heading margins. Keep a half line. */
.blog-body.blog-prose .trix-content > div:has(> br:only-child) {
  height: 0.6em;
  overflow: hidden;
}

/* Reading view only: a bare filename under an image is noise, a real caption stays. */
.blog-body.blog-prose .attachment--preview .attachment__caption:has(.attachment__name) {
  display: none;
}

/* ---------- Edit / new ---------- */

.blog-form-page {
  width: 100%;
  max-width: 52rem;
  margin-inline: auto;
  padding-bottom: 5rem;
}

.blog-form-title-input {
  display: block;
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.5rem 0;
  border: 0;
  border-bottom: 2px solid var(--border);
  border-radius: 0;
  background: transparent;
  color: var(--page-text);
  font-family: var(--font-serif);
  font-size: clamp(1.75rem, 1.3rem + 2vw, 2.75rem);
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.02em;
  outline: none;
  transition: border-color 150ms ease-out;
}

.font-loaded .blog-form-title-input {
  font-family: var(--font-serif);
}

.blog-form-title-input:focus {
  border-bottom-color: var(--pill-blue-text);
}

.blog-form-title-input::placeholder {
  color: var(--text-muted);
}

.blog-form-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--page-text);
}

.blog-form-hint {
  font-size: 0.8125rem;
  font-weight: 400;
  color: var(--text-muted);
}

/* The editor is a paper-like surface that mirrors the article's typography. */
.blog-editor-wrap {
  margin-top: 0.75rem;
}

.blog-editor-wrap trix-toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 0.5rem 0;
  background: var(--page-bg);
}

.blog-editor-wrap trix-toolbar .trix-button-row {
  gap: 0.5rem;
  overflow-x: auto;
  scrollbar-width: none;
}

.blog-editor-wrap trix-toolbar .trix-button-group {
  flex: none;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface-elevated);
  overflow: hidden;
}

.blog-editor-wrap trix-toolbar .trix-button {
  border-bottom: 0;
  color: var(--page-text);
}

.blog-editor-wrap trix-toolbar .trix-button:not(:first-child) {
  border-left-color: var(--border);
}

.blog-editor-wrap trix-toolbar .trix-button.trix-active {
  background: var(--pill-blue-bg);
  color: var(--pill-blue-text);
}

.blog-editor-wrap trix-toolbar .trix-button--icon::before {
  opacity: 0.75;
}

/* Trix toolbar icons are dark-on-transparent SVGs; flip them on dark surfaces. */
@media (prefers-color-scheme: dark) {
  .blog-editor-wrap trix-toolbar .trix-button--icon::before {
    filter: invert(1);
  }
}

.blog-editor-wrap trix-toolbar .trix-button--icon.trix-active::before {
  opacity: 1;
}

.blog-editor-wrap trix-toolbar .trix-button--icon:disabled::before {
  opacity: 0.2;
}

.blog-editor-wrap trix-toolbar .trix-dialog {
  background: var(--surface-elevated);
  border-top: 2px solid var(--pill-blue-text);
  box-shadow: var(--card-shadow);
  color: var(--page-text);
}

.blog-editor-wrap trix-toolbar .trix-input--dialog {
  border: 1px solid var(--border);
  background: var(--page-bg);
  color: var(--page-text);
}

trix-editor.blog-prose {
  min-height: 24rem;
  margin: 0;
  padding: 1.25rem 1rem 2rem;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  background: var(--surface-elevated);
  box-shadow: var(--dashboard-filter-shadow);
  transition: border-color 150ms ease-out, box-shadow 150ms ease-out;

  @media (min-width: 640px) {
    padding: 2rem 2.5rem 3rem;
  }

  &:focus {
    border-color: var(--pill-blue-text);
    box-shadow: 0 0 0 3px var(--input-ring-default);
  }

  &:empty::before {
    color: var(--text-muted);
  }

  & .attachment__caption {
    color: var(--text-muted);
  }
}

@media (prefers-reduced-motion: reduce) {
  .blog-prose .trix-content a,
  trix-editor.blog-prose,
  .blog-form-title-input {
    transition: none;
  }
}
