/**
 * Vanilia (<vanilia-editor>) — standalone stylesheet.
 * Generated from src/styles/editor.css via `node scripts/pack.mjs`.
 * Customize via CSS variables (--ve-accent, --ve-bg, --ve-radius, --ve-min-height, etc.).
 */
@layer ve-tokens, ve-base, ve-chrome, ve-surface, ve-popovers;

@layer ve-tokens {
  :root {
    --ve-accent: oklch(0.58 0.19 258);
    --ve-accent-contrast: #ffffff;
    --ve-danger: oklch(0.58 0.21 25);

    --ve-bg: light-dark(#ffffff, #14161a);
    --ve-surface-bg: light-dark(#ffffff, #14161a);
    --ve-chrome-bg: light-dark(#f7f8fa, #1b1e24);
    --ve-popover-bg: light-dark(#ffffff, #1e222a);
    --ve-border: light-dark(#e2e5ec, #2c313c);
    --ve-border-strong: light-dark(#cbd2df, #3c4352);
    --ve-text: light-dark(#171a21, #eceff4);
    --ve-text-muted: light-dark(#5c6577, #97a1b3);
    --ve-hover-bg: light-dark(#eaedf3, #272c36);
    --ve-code-bg: light-dark(#f2f4f8, #20242d);
    --ve-shadow: light-dark(
      0 10px 30px -10px rgb(15 23 42 / 0.18),
      0 12px 34px -8px rgb(0 0 0 / 0.55)
    );

    --ve-radius: 12px;
    --ve-radius-sm: 7px;
    --ve-min-height: 280px;
    --ve-max-height: none;
    --ve-padding-x: 20px;
    --ve-padding-y: 18px;
    --ve-font-size: 15px;
    --ve-line-height: 1.65;
    --ve-font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --ve-font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

    --ve-highlight-bg: oklch(0.88 0.16 95 / 0.55);
    --ve-highlight-active-bg: oklch(0.75 0.2 55 / 0.8);
    --ve-highlight-active-text: #111111;

    --ve-icon-size: 16px;
    --ve-icon-size-sm: 14px;
    --ve-icon-stroke: 2;
  }

  vanilia-editor {
    display: block;
    color-scheme: inherit;
  }
}

@layer ve-base {
  .ve-root {
    --_ve-accent-soft: var(--ve-accent-soft, oklch(from var(--ve-accent) l c h / 0.14));
    --_ve-accent-ring: var(--ve-accent-ring, oklch(from var(--ve-accent) l c h / 0.35));
    --_ve-accent-border: var(--ve-accent-border, oklch(from var(--ve-accent) l c h / 0.25));
    --_ve-accent-hover: var(--ve-accent-hover, oklch(from var(--ve-accent) calc(l - 0.06) c h));
    --_ve-danger-soft: var(--ve-danger-soft, oklch(from var(--ve-danger) l c h / 0.13));

    container-type: inline-size;
    container-name: ve-editor;
    position: relative;
    display: flex;
    flex-direction: column;
    background: var(--ve-bg);
    color: var(--ve-text);
    border: 1px solid var(--ve-border);
    border-radius: var(--ve-radius);
    font-family: var(--ve-font-sans);
    transition: border-color 140ms ease, box-shadow 140ms ease;

    &:focus-within {
      border-color: var(--ve-accent);
      box-shadow: 0 0 0 3px var(--_ve-accent-ring);
    }

    &.is-disabled {
      opacity: 0.65;
      pointer-events: none;
    }

    *,
    *::before,
    *::after {
      box-sizing: border-box;
    }
  }
}

@layer ve-chrome {
  .ve-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    padding: 8px 10px;
    background: var(--ve-chrome-bg);
    border-block-end: 1px solid var(--ve-border);
    border-start-start-radius: calc(var(--ve-radius) - 1px);
    border-start-end-radius: calc(var(--ve-radius) - 1px);

    &[hidden] {
      display: none;
    }
  }

  .ve-btn-group {
    display: inline-flex;
    align-items: center;
    gap: 2px;
  }

  .ve-sep {
    display: inline-block;
    width: 1px;
    height: 18px;
    margin-inline: 3px;
    background: var(--ve-border);
    flex-shrink: 0;
  }

  .ve-btn {
    appearance: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    min-width: 30px;
    height: 30px;
    padding: 0 6px;
    margin: 0;
    border: 1px solid transparent;
    border-radius: var(--ve-radius-sm);
    background: transparent;
    color: var(--ve-text);
    font: 500 13px / 1 var(--ve-font-sans);
    cursor: pointer;
    transition: background-color 110ms ease, color 110ms ease, border-color 110ms ease;

    & .ve-icon {
      display: block;
      flex-shrink: 0;
      inline-size: var(--ve-icon-size);
      block-size: var(--ve-icon-size);
    }

    & svg.ve-icon {
      stroke-width: var(--ve-icon-stroke);
    }

    & .ve-icon--custom {
      display: grid;
      place-items: center;
      font-size: var(--ve-icon-size);
      line-height: 1;

      & > svg,
      & > img {
        display: block;
        inline-size: 100%;
        block-size: 100%;
      }
    }

    &:hover:not(:disabled) {
      background: var(--ve-hover-bg);
    }

    &:focus-visible {
      outline: 2px solid var(--ve-accent);
      outline-offset: 1px;
    }

    &.is-active {
      background: var(--_ve-accent-soft);
      color: var(--ve-accent);
      border-color: var(--_ve-accent-border);
    }

    &:disabled {
      opacity: 0.35;
      cursor: not-allowed;
    }

    &[hidden] {
      display: none;
    }

    &.ve-btn--sm {
      min-width: 26px;
      height: 26px;
      padding: 0 6px;
      font-size: 12px;

      & .ve-icon {
        inline-size: var(--ve-icon-size-sm);
        block-size: var(--ve-icon-size-sm);
      }

      & .ve-icon--custom {
        font-size: var(--ve-icon-size-sm);
      }
    }

    &.ve-btn--text {
      padding-inline: 8px;
    }

    &.ve-btn--primary {
      background: var(--ve-accent);
      color: var(--ve-accent-contrast);
      &:hover:not(:disabled) {
        background: var(--_ve-accent-hover);
      }
    }

    &.ve-btn--danger {
      color: var(--ve-danger);
      &:hover:not(:disabled) {
        background: var(--_ve-danger-soft);
      }
    }

    &.ve-btn--upload {
      width: 100%;
      height: 34px;
      border: 1px dashed var(--ve-border-strong);
      background: var(--ve-chrome-bg);
      &:hover:not(:disabled) {
        border-color: var(--ve-accent);
        color: var(--ve-accent);
      }
    }
  }

  .ve-input {
    appearance: none;
    width: 100%;
    height: 34px;
    padding: 0 10px;
    border: 1px solid var(--ve-border-strong);
    border-radius: var(--ve-radius-sm);
    background: var(--ve-bg);
    color: var(--ve-text);
    font: 400 13px / 1.2 var(--ve-font-sans);

    &:focus {
      outline: none;
      border-color: var(--ve-accent);
      box-shadow: 0 0 0 2px var(--_ve-accent-ring);
    }

    &.ve-input--sm {
      height: 26px;
      padding: 0 8px;
      font-size: 12px;
    }
  }

  .ve-find-bar {
    padding: 8px 10px;
    background: var(--ve-chrome-bg);
    border-block-end: 1px solid var(--ve-border);

    &[hidden] {
      display: none;
    }

    & .ve-find-row {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 6px;
    }

    & .ve-input {
      width: auto;
      min-width: 140px;
      flex: 1 1 150px;
    }

    & .ve-find-count {
      font-size: 12px;
      color: var(--ve-text-muted);
      font-variant-numeric: tabular-nums;
      min-width: 44px;
      text-align: center;
    }
  }

  .ve-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 12px;
    background: var(--ve-chrome-bg);
    border-block-start: 1px solid var(--ve-border);
    border-end-start-radius: calc(var(--ve-radius) - 1px);
    border-end-end-radius: calc(var(--ve-radius) - 1px);
    font-size: 12px;
    color: var(--ve-text-muted);
  }

  .ve-status {
    color: var(--ve-accent);
    font-weight: 500;
  }

  @container ve-editor (max-width: 520px) {
    .ve-toolbar {
      padding: 6px;
      gap: 2px;
    }
    .ve-btn {
      min-width: 28px;
      height: 28px;
    }
    .ve-sep {
      margin-inline: 1px;
    }
  }
}

@layer ve-surface {
  .ve-stage {
    position: relative;
    background: var(--ve-surface-bg);
    max-height: var(--ve-max-height);
    overflow-y: auto;
  }

  /* toolbar="none": the stage becomes the top edge of the rounded frame */
  .ve-root:has(> .ve-toolbar[hidden]) .ve-stage {
    border-start-start-radius: calc(var(--ve-radius) - 1px);
    border-start-end-radius: calc(var(--ve-radius) - 1px);
  }

  .ve-surface {
    position: relative;
    min-height: var(--ve-min-height);
    padding: var(--ve-padding-y) var(--ve-padding-x);
    outline: none;
    font-size: var(--ve-font-size);
    line-height: var(--ve-line-height);
    color: var(--ve-text);
    overflow-wrap: break-word;

    &[data-empty]::before {
      content: attr(data-placeholder);
      position: absolute;
      inset-block-start: var(--ve-padding-y);
      inset-inline-start: var(--ve-padding-x);
      color: var(--ve-text-muted);
      pointer-events: none;
      opacity: 0.75;
    }

    &.ve-drag-over {
      background: var(--_ve-accent-soft);
      outline: 2px dashed var(--ve-accent);
      outline-offset: -6px;
    }

    & > :first-child {
      margin-block-start: 0;
    }

    & > :last-child {
      margin-block-end: 0;
    }

    & p {
      margin-block: 0.65em;
    }

    & h1 {
      font-size: 1.85em;
      font-weight: 750;
      line-height: 1.25;
      letter-spacing: -0.02em;
      margin-block: 0.8em 0.35em;
    }

    & h2 {
      font-size: 1.45em;
      font-weight: 700;
      line-height: 1.3;
      letter-spacing: -0.015em;
      margin-block: 0.85em 0.35em;
    }

    & h3 {
      font-size: 1.18em;
      font-weight: 650;
      line-height: 1.35;
      margin-block: 0.8em 0.3em;
    }

    & ul,
    & ol {
      margin-block: 0.65em;
      padding-inline-start: 1.6em;
    }

    & li {
      margin-block: 0.25em;
    }

    & blockquote {
      margin-block: 0.9em;
      margin-inline: 0;
      padding-block: 4px;
      padding-inline-start: 14px;
      border-inline-start: 3px solid var(--ve-accent);
      color: var(--ve-text-muted);
      font-style: italic;
    }

    & code {
      font-family: var(--ve-font-mono);
      font-size: 0.88em;
      padding: 0.12em 0.38em;
      border-radius: 5px;
      background: var(--ve-code-bg);
    }

    & pre {
      margin-block: 0.9em;
      padding: 12px 14px;
      border-radius: var(--ve-radius-sm);
      background: var(--ve-code-bg);
      border: 1px solid var(--ve-border);
      font-family: var(--ve-font-mono);
      font-size: 13px;
      line-height: 1.55;
      overflow-x: auto;

      & code {
        padding: 0;
        background: transparent;
      }
    }

    & hr {
      border: 0;
      border-block-start: 1px solid var(--ve-border-strong);
      margin-block: 1.25em;
    }

    & a {
      color: var(--ve-accent);
      text-decoration: underline;
      text-underline-offset: 2px;
    }

    & s,
    & del {
      text-decoration: line-through;
    }

    & u {
      text-decoration: underline;
    }

    & img {
      max-width: 100%;
      height: auto;
      border-radius: 6px;
      cursor: pointer;
      transition: box-shadow 120ms ease;

      &:hover {
        box-shadow: 0 0 0 2px var(--_ve-accent-ring);
      }

      &[data-align="left"] {
        display: block;
        margin-left: 0;
        margin-right: auto;
      }

      &[data-align="center"] {
        display: block;
        margin-left: auto;
        margin-right: auto;
      }

      &[data-align="right"] {
        display: block;
        margin-left: auto;
        margin-right: 0;
      }
    }
  }

  /* Rendered HTML preview container matching editor surface typography */
  .ve-prose {
    font-size: var(--ve-font-size);
    line-height: var(--ve-line-height);
    color: var(--ve-text, inherit);
    overflow-wrap: break-word;

    & > :first-child { margin-block-start: 0; }
    & > :last-child { margin-block-end: 0; }
    & p { margin-block: 0.65em; }
    & h1 { font-size: 1.85em; font-weight: 750; line-height: 1.25; letter-spacing: -0.02em; margin-block: 0.8em 0.35em; }
    & h2 { font-size: 1.45em; font-weight: 700; line-height: 1.3; letter-spacing: -0.015em; margin-block: 0.85em 0.35em; }
    & h3 { font-size: 1.18em; font-weight: 650; line-height: 1.35; margin-block: 0.8em 0.3em; }
    & ul, & ol { margin-block: 0.65em; padding-inline-start: 1.6em; }
    & li { margin-block: 0.25em; }
    & blockquote {
      margin-block: 0.9em;
      margin-inline: 0;
      padding-block: 4px;
      padding-inline-start: 14px;
      border-inline-start: 3px solid var(--ve-accent);
      color: var(--ve-text-muted);
      font-style: italic;
    }
    & code {
      font-family: var(--ve-font-mono);
      font-size: 0.88em;
      padding: 0.12em 0.38em;
      border-radius: 5px;
      background: var(--ve-code-bg);
    }
    & pre {
      margin-block: 0.9em;
      padding: 12px 14px;
      border-radius: var(--ve-radius-sm);
      background: var(--ve-code-bg);
      border: 1px solid var(--ve-border);
      font-family: var(--ve-font-mono);
      font-size: 13px;
      line-height: 1.55;
      overflow-x: auto;
      & code { padding: 0; background: transparent; }
    }
    & hr { border: 0; border-block-start: 1px solid var(--ve-border-strong); margin-block: 1.25em; }
    & a { color: var(--ve-accent); text-decoration: underline; text-underline-offset: 2px; }
    & s, & del { text-decoration: line-through; }
    & u { text-decoration: underline; }
    & img {
      max-width: 100%;
      height: auto;
      border-radius: 6px;
      &[data-align="left"] { display: block; margin-left: 0; margin-right: auto; }
      &[data-align="center"] { display: block; margin-left: auto; margin-right: auto; }
      &[data-align="right"] { display: block; margin-left: auto; margin-right: 0; }
    }
  }


  /* Custom Image Resize Overlay (lives outside contenteditable) */
  .ve-img-selection {
    position: absolute;
    pointer-events: none;
    border: 2px solid var(--ve-accent);
    border-radius: 6px;
    z-index: 20;

    &[hidden] {
      display: none;
    }
  }

  .ve-img-handle {
    position: absolute;
    pointer-events: auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    width: 12px;
    height: 12px;
    padding: 0;
    border: 2px solid var(--ve-accent);
    border-radius: 999px;
    background: #ffffff;
    box-shadow: 0 1px 4px rgb(0 0 0 / 0.25);
    transition: transform 90ms ease;

    /* Generous invisible hit area so fingers easily grab handles without scrolling */
    &::after {
      content: "";
      position: absolute;
      inset: -14px;
      touch-action: none;
    }

    &:hover {
      transform: scale(1.2);
    }

    @media (pointer: coarse) {
      width: 16px;
      height: 16px;
      &::after {
        inset: -16px;
      }
    }

    &.ve-img-handle--nw {
      top: -6px;
      left: -6px;
    }
    &.ve-img-handle--ne {
      top: -6px;
      right: -6px;
    }
    &.ve-img-handle--se {
      bottom: -6px;
      right: -6px;
    }
    &.ve-img-handle--sw {
      bottom: -6px;
      left: -6px;
    }
    &.ve-img-handle--w {
      left: -5px;
      top: calc(50% - 14px);
      width: 8px;
      height: 28px;
      border-radius: 4px;
    }
    &.ve-img-handle--e {
      right: -5px;
      top: calc(50% - 14px);
      width: 8px;
      height: 28px;
      border-radius: 4px;
    }

    @media (pointer: coarse) {
      &.ve-img-handle--nw { top: -8px; left: -8px; }
      &.ve-img-handle--ne { top: -8px; right: -8px; }
      &.ve-img-handle--se { bottom: -8px; right: -8px; }
      &.ve-img-handle--sw { bottom: -8px; left: -8px; }
      &.ve-img-handle--w { left: -6px; width: 10px; height: 34px; top: calc(50% - 17px); }
      &.ve-img-handle--e { right: -6px; width: 10px; height: 34px; top: calc(50% - 17px); }
    }
  }

  .ve-stage.is-resizing,
  .ve-stage.is-resizing * {
    touch-action: none !important;
    user-select: none !important;
    -webkit-user-select: none !important;
  }

  .ve-img-badge {
    position: absolute;
    inset-block-end: 8px;
    inset-inline-start: 50%;
    transform: translateX(-50%);
    padding: 3px 8px;
    border-radius: 999px;
    background: rgb(15 23 42 / 0.82);
    color: #ffffff;
    font: 600 11px / 1.3 var(--ve-font-mono);
    white-space: nowrap;
    backdrop-filter: blur(6px);

    &[hidden] {
      display: none;
    }
  }
}

@layer ve-popovers {
  .ve-virtual-anchor {
    position: fixed;
    pointer-events: none;
    visibility: hidden;
  }

  .ve-popover {
    inset: auto;
    overflow: visible;
    margin: 0;
    padding: 5px;
    border: 1px solid var(--ve-border-strong);
    border-radius: 10px;
    background: var(--ve-popover-bg);
    color: var(--ve-text);
    box-shadow: var(--ve-shadow);
    font-family: var(--ve-font-sans);

    opacity: 0;
    transform: translateY(4px) scale(0.98);
    transition:
      opacity 120ms ease,
      transform 120ms ease,
      display 120ms allow-discrete,
      overlay 120ms allow-discrete;

    &:popover-open {
      opacity: 1;
      transform: translateY(0) scale(1);

      @starting-style {
        opacity: 0;
        transform: translateY(4px) scale(0.98);
      }
    }
  }

  .ve-img-toolbar,
  .ve-bubble-menu {
    position: fixed;
    display: inline-flex;
    align-items: center;
    gap: 3px;
    max-width: calc(100vw - 16px);

    &:not(:popover-open) {
      display: none;
    }
  }

  .ve-img-toolbar {
    flex-wrap: wrap;
    justify-content: center;
  }

  .ve-bubble-menu {
    white-space: nowrap;
  }

  @supports (anchor-name: --a) {
    .ve-bubble-menu {
      position-area: top center;
      position-try-fallbacks: flip-block;
      margin-block: 8px;
    }
  }

  .ve-img-alt-form {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px;

    & .ve-input {
      width: 210px;
    }
  }

  .ve-form-popover {
    position: fixed;
    width: 290px;
    padding: 12px;
  }

  .ve-popover-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .ve-field {
    display: flex;
    flex-direction: column;
    gap: 4px;

    & span {
      font-size: 12px;
      font-weight: 550;
      color: var(--ve-text-muted);
    }
  }

  .ve-popover-actions {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .ve-spacer {
    flex: 1;
  }

  .ve-divider-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    color: var(--ve-text-muted);

    &::before,
    &::after {
      content: "";
      flex: 1;
      height: 1px;
      background: var(--ve-border);
    }
  }
}

/* CSS Custom Highlight API paint styles (non-destructive Find & Replace) */
::highlight(ve-find) {
  background-color: var(--ve-highlight-bg);
  color: inherit;
}

::highlight(ve-find-active) {
  background-color: var(--ve-highlight-active-bg);
  color: var(--ve-highlight-active-text);
}
