/* VOICE: tender, stationery, a reading-journal workshop
   IDEA: a worktable — the settings panel on the left, the poster laid on the right
   PICTURE: the poster itself, rendered live on a canvas
   COLOUR and TYPE: powder pink, plum-brown ink, coral accent; Nunito for the tool, Lora for the poster
   LAYOUT: settings in a narrow column, sticky preview; on mobile: title, preview, settings */

@layer reset, tokens, base, layout, components, utilities;

/* ---- reset ---- */
@layer reset {
  * {
    box-sizing: border-box;
    min-inline-size: 0;
  }

  /* !important: the hidden attribute must hide the element whatever display a component sets */
  [hidden] { display: none !important; }
}

/* ---- tokens ---- */
@layer tokens {
  :root {
    color-scheme: light dark;
    --bg: light-dark(#ebe5de, #221b1f);
    --panel: light-dark(#fbf9f6, #2e2529);
    --ink: light-dark(#4a2f33, #f4e6e8);
    --soft: light-dark(#7e5f63, #c9adb2);
    --line: light-dark(#ddd2c7, #4a3a40);
    --acc: light-dark(#b3555e, #f0929a);
    --accInk: light-dark(#fff, #2a1a1e);
    /* border of input fields: at least 3:1 against --field in both themes (--line stays for cards and panels) */
    --field-line: light-dark(#968f87, #866974);
    /* accent for text set on --bg or --acc-tint: at least 4.5:1 */
    --acc-text: light-dark(#a14d55, #f0929a);
    /* section chevron, in the accent colour; an image does not go through light-dark(), hence the same key as color-scheme: system, unless data-theme */
    --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23b3555e' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M7 10l5 5 5-5'/%3E%3C/svg%3E");
    /* background of fields and card buttons */
    --field: light-dark(#fff, #241d20);
    /* pale accent tint: badges, line numbers, hover */
    --acc-tint: light-dark(#fbe3e3, #3a2c32);
    /* very pale accent wash: card hover, drop zone */
    --acc-wash: light-dark(#fdf1f1, #2c2226);
    --danger: #c0392b;
    --ring: 3px solid var(--acc);
    --gutter: max(4vw, 16px);
    --r-pill: 999px;
    --r-field: 10px;
    --r-card: 14px;
    --font-ui: Nunito, system-ui, sans-serif;
    --t-config: border-color .15s, background-color .15s, box-shadow .15s, transform .15s, color .15s;
    --press: translateY(0) scale(.98);

    @media (prefers-color-scheme: dark) {
      &:not([data-theme=light]) { --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23f0929a' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M7 10l5 5 5-5'/%3E%3C/svg%3E"); }
    }

    &[data-theme=light] { color-scheme: light; }

    &[data-theme=dark] {
      color-scheme: dark;
      --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23f0929a' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M7 10l5 5 5-5'/%3E%3C/svg%3E");
    }
  }
}

/* ---- elements ---- */
@layer base {
  /* the book editor is open on a touch screen: the page stays put behind it */
  :root.ed-lock, :root.ed-lock body { overflow: hidden; }

  body {
    margin: 0;
    overflow-x: clip;
    font: 1rem/1.5 var(--font-ui);
    color: var(--ink);
    background: var(--bg);

    @media print { background: none; }
  }

  button, input, select {
    font: inherit;
    color: inherit;
  }

  :focus-visible {
    outline: var(--ring);
    outline-offset: 2px;
  }

  h1 {
    margin: 0;
    font: 700 clamp(1.625rem, 3.4vw, 2.25rem)/1.1 Lora, serif;
    text-wrap: balance;
  }

  input[type=text], input[type=number], select {
    inline-size: 100%;
    min-block-size: 44px;
    padding: 8px 12px;
    font-size: 1rem;
    background: var(--field);
    border: 1.5px solid var(--field-line);
    border-radius: var(--r-field);
  }

  input[type=range] {
    inline-size: 100%;
    min-block-size: 30px;
    accent-color: var(--acc);
  }

  input[type=color] {
    inline-size: 44px;
    block-size: 44px;
    padding: 2px;
    background: none;
    border: 1.5px solid var(--field-line);
    border-radius: var(--r-field);
    cursor: pointer;
  }

  textarea {
    inline-size: 100%;
    min-block-size: 150px;
    padding: 10px 12px;
    font: .9375rem/1.5 var(--font-ui);
    background: var(--field);
    border: 1.5px solid var(--field-line);
    border-radius: var(--r-field);
    resize: vertical;
  }

  /* the browsers' grey falls under 4.5:1 on the dark field */
  ::placeholder {
    color: var(--soft);
    opacity: 1;
  }

  details {
    padding: 2px 16px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--r-card);
    transition: box-shadow .2s;

    &[open] { padding-block-end: 16px; }
    &:hover { box-shadow: 0 4px 14px rgba(150, 70, 80, .08); }

    &[open] > summary {
      margin-block-end: 4px;
      border-block-end: 1px solid var(--line);

      &::after { transform: rotate(180deg); }
    }
  }

  summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-block-size: 52px;
    padding: 12px 0;
    font-size: 1.0625rem;
    font-weight: 800;
    list-style: none;
    cursor: pointer;
    user-select: none;

    &::after {
      content: "";
      flex: none;
      inline-size: 30px;
      block-size: 30px;
      background: var(--acc-tint) var(--chevron) center/18px no-repeat;
      border-radius: 50%;
      transition: transform .25s ease, background-color .2s;
    }

    &:hover::after { background-color: light-dark(#f6cfd2, #4a3840); }

    /* last: Firefox ignores this pseudo-element, and the rule with it */
    &::-webkit-details-marker { display: none; }
  }
}

/* ---- layout ---- */
@layer layout {
  header {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 6px 18px;
    padding: 20px var(--gutter) 8px;

    & p {
      margin: 0;
      font-size: .9375rem;
      color: var(--soft);
    }

    /* on a phone the language sits beside the title, which wraps anyway; the title basis leaves no room for the tools, which take the second row */
    @media (max-width: 480px) {
      align-items: center;
      gap: 8px;

      & h1 { flex: 1 1 60%; }
    }
  }

  .app {
    display: grid;
    grid-template-columns: minmax(0, 380px) minmax(0, 1fr);
    grid-template-areas: "ctl stage";
    align-items: start;
    gap: 20px;
    padding: 8px var(--gutter) 40px;

    @media (max-width: 900px) {
      grid-template-columns: minmax(0, 1fr);
      grid-template-areas: "stage" "ctl";
    }
  }

  .ctl {
    grid-area: ctl;
    display: grid;
    gap: 12px;

    /* a section reached from a chip starts below the sticky chip bar */
    & > details {
      @media (max-width: 900px) { scroll-margin-block-start: 64px; }
    }
  }

  .stage {
    grid-area: stage;
    position: sticky;
    inset-block-start: 12px;
    display: grid;
    justify-items: center;
    gap: 12px;

    /* in a single column the preview follows the flow; relative anchors the .peekb badge */
    @media (max-width: 900px) { position: relative; }

    & canvas {
      display: block;
      inline-size: auto;
      block-size: auto;
      min-block-size: 320px;
      max-inline-size: 100%;
      max-block-size: calc(100svh - 360px);
      background: #fff;
      border-radius: var(--r-field);
      box-shadow: 0 10px 30px rgba(120, 50, 60, .22);

      @media (max-width: 900px) { max-block-size: none; }
    }
  }
}

/* ---- components ---- */
@layer components {
  /* header: language, history, surprise, tips */
  #bSurprise { margin-inline-start: 0; }

  /* tips and About, kept together: on a phone they close the tools row, or wrap as a pair */
  .hx {
    display: flex;
    align-self: center;
    gap: 4px;

    @media (max-width: 480px) { margin-inline-start: auto; }
  }

  .undo {
    display: flex;
    align-self: center;
    gap: 4px;
  }

  .ub {
    display: grid;
    place-items: center;
    inline-size: 42px;
    block-size: 42px;
    padding: 0;
    color: var(--ink);
    background: var(--panel);
    border: 1.5px solid var(--line);
    border-radius: 50%;
    cursor: pointer;

    &:hover:not(:disabled) {
      color: var(--acc);
      border-color: var(--acc);
    }

    &:disabled {
      opacity: .35;
      cursor: default;
    }
  }

  #langSw {
    display: flex;
    align-self: center;
    margin-inline-start: auto;
    padding: 3px;
    background: var(--panel);
    border: 1.5px solid var(--line);
    border-radius: var(--r-pill);


    & button {
      min-inline-size: 48px;
      min-block-size: 38px;
      font-weight: 800;

      color: var(--soft);
      background: none;
      border: 0;
      border-radius: var(--r-pill);
      cursor: pointer;
      transition: var(--t-config);

      &[aria-pressed=true] {
        color: var(--accInk);
        background: var(--acc);
      }

      &:hover:not([aria-pressed=true]) {
        color: var(--acc-text);
        background: var(--acc-tint);
      }
    }
  }

  /* buttons */
  .bar {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
  }

  .btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-block-size: 44px;
    padding: 8px 16px;
    font-weight: 700;
    color: var(--acc);
    background: transparent;
    border: 1.5px solid var(--acc);
    border-radius: var(--r-pill);
    cursor: pointer;
    transition: var(--t-config);

    &.main {
      color: var(--accInk);
      background: var(--acc);
    }

    &:hover {
      color: var(--accInk);
      background: var(--acc);
      box-shadow: 0 4px 12px rgba(150, 70, 80, .18);
      transform: translateY(-1px);

      & .bi:not(.caret) { transform: rotate(-20deg); }
    }

    &:active { transform: var(--press); }

    /* after :hover: an armed clear button stays red on hover */
    &.warn {
      position: relative;
      overflow: hidden;
      color: #fff;
      background: var(--danger);
      border-color: var(--danger);

      &::after {
        content: "";
        position: absolute;
        inset-block-end: 0;
        inset-inline-start: 0;
        inline-size: 100%;
        block-size: 4px;
        background: rgba(255, 255, 255, .85);
        animation: resetbar 4s linear forwards;
      }

      & .rs > span:first-child { visibility: hidden; }
      & .rs > span:last-child { visibility: visible; }
    }
  }

  @keyframes resetbar { to { inline-size: 0; } }

  /* after .btn: the Surprise me text sits on --bg; .btn:hover keeps --accInk */
  .hbtn {
    align-self: center;
    color: var(--acc-text);
  }

  .bar3 {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 6px;

    & .btn {
      flex: 1 1 auto;
      justify-content: center;
      gap: 6px;
      inline-size: 100%;
      min-block-size: 42px;
      padding: 8px 10px;
      font-size: .875rem;
    }
  }

  .rs {
    display: grid;

    & > span {
      grid-area: 1 / 1;
      text-align: center;
    }

    & > span:last-child { visibility: hidden; }
  }

  .bi {
    flex: none;
    inline-size: 18px;
    block-size: 18px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: transform .2s;
  }

  .caret {
    inline-size: 14px;
    block-size: 14px;
  }

  .sortw {
    position: relative;
    display: flex;

    & > .btn { flex: 1 1 auto; }
  }

  .menu {
    position: absolute;
    inset-block-start: calc(100% + 6px);
    inset-inline-start: 0;
    z-index: 15;
    display: grid;
    min-inline-size: 200px;
    padding: 6px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(60, 20, 30, .22);

    &[hidden] { display: none; }

    & button {
      display: flex;
      align-items: center;
      gap: 10px;
      min-block-size: 40px;
      padding: 6px 12px;
      font: inherit;
      font-size: .9375rem;
      text-align: start;
      color: var(--ink);
      background: none;
      border: 0;
      border-radius: 8px;
      cursor: pointer;

      /* --acc-text: the accent alone reaches only 4.13:1 on this light tint */
      &:hover, &:focus-visible {
        color: var(--acc-text);
        background: light-dark(#fbeaea, #3a2c32);
      }

      /* the background tint is not enough to show focus: the same ring as everywhere */
      &:focus-visible {
        outline: var(--ring);
        outline-offset: 2px;
      }
    }

    & .bi {
      inline-size: 18px;
      block-size: 18px;
      color: var(--acc);
    }
  }

  /* export */
  .exp {
    display: grid;
    gap: 10px;
    inline-size: 100%;
    max-inline-size: 560px;
    padding: 14px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 18px;
  }

  .xmore {
    padding: 0;
    background: none;
    border: 0;
    border-radius: 0;

    &[open] { padding-block-end: 0; }
    &:hover { box-shadow: none; }

    & > summary {
      min-block-size: 44px;
      padding: 4px 0;
      font-size: .9375rem;
      font-weight: 700;
      color: var(--acc-text);

      &::after {
        inline-size: 24px;
        block-size: 24px;
        background-size: 15px;
      }

      @media (min-width: 901px) { display: none; }
    }

    &[open] > summary {
      margin-block-end: 8px;
      border: 0;
    }
  }

  .xg {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;

    @media (max-width: 600px) { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  }

  .xb {
    display: flex;
    align-items: center;
    gap: 12px;
    min-block-size: 52px;
    padding: 8px 12px;
    text-align: start;
    color: var(--ink);
    background: var(--field);
    border: 1.5px solid var(--line);
    border-radius: var(--r-card);
    cursor: pointer;
    transition: border-color .15s, transform .15s, box-shadow .15s;

    &:hover {
      border-color: var(--acc);
      box-shadow: 0 4px 12px rgba(150, 70, 80, .12);
      transform: translateY(-1px);
    }

    & span {
      display: grid;
      gap: 2px;
      min-inline-size: 0;
      line-height: 1.2;
    }

    & b { font-size: .9375rem; }

    & small {
      font-size: .8125rem;
      color: var(--soft);
    }

    &.main {
      min-block-size: 62px;
      color: var(--accInk);
      background: var(--acc);
      border-color: var(--acc);

      & small { color: inherit; }

      & b { font-size: 1.0625rem; }
    }

    &:not(.main) .ic { color: var(--acc); }

    &.busy {
      opacity: .8;
      pointer-events: none;

      & .ic {
        border-radius: 50%;
        border: 3px solid currentColor;
        border-inline-end-color: transparent;
        animation: spin .8s linear infinite;
      }

      & .ic path { display: none; }
    }

    &.ok .ic path { d: path("M5 12l5 5L20 7"); }
  }

  .ic {
    flex: none;
    inline-size: 26px;
    block-size: 26px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  @keyframes spin { to { transform: rotate(1turn); } }

  #bStk {
    grid-column: 1 / -1;
    order: 9;
  }

  /* transient message; physical left and translate: centred on the screen */
  #msg {
    position: fixed;
    inset-block-start: 24px;
    left: 50%;
    /* above the book editor (20): a message can carry an Undo for it */
    z-index: 30;
    max-inline-size: min(92vw, 460px);
    padding: 12px 22px;
    font-size: .9375rem;
    font-weight: 700;
    text-align: center;
    color: var(--panel);
    background: var(--ink);
    border-radius: var(--r-pill);
    box-shadow: 0 8px 24px rgba(60, 20, 30, .28);
    opacity: 0;
    translate: -50% -12px;
    pointer-events: none;
    transition: opacity .25s, translate .25s;

    &.on {
      opacity: 1;
      translate: -50% 0;
    }

    /* a message with an action (Undo after a deletion) takes clicks */
    &.act { pointer-events: auto; }

    & button {
      margin-inline-start: 6px;
      padding: 0;
      font: inherit;
      color: inherit;
      text-decoration: underline;
      background: none;
      border: 0;
      cursor: pointer;
    }
  }

  /* collapsed help panel */
  .gram {
    margin-block-start: 6px;
    padding: 0;
    background: none;
    border: 0;

    /* like any open details: the bottom margin of details[open] is still owed */
    &[open] { padding-block-end: 16px; }

    & > summary {
      min-block-size: 0;
      margin: 0;
      padding: 4px 0;
      font-size: .875rem;
      font-weight: 700;
      color: var(--acc);
      border: 0;

      &::after {
        inline-size: 22px;
        block-size: 22px;
        background-size: 14px;
      }
    }

    & code {
      padding: 2px 6px;
      font-size: .78125rem;
      overflow-wrap: anywhere;
      background: var(--acc-tint);
      border-radius: 6px;
    }
  }

  /* setting fields */
  .f {
    display: grid;
    gap: 4px;
    margin-block-start: 10px;
  }

  .f > span, .lab {
    font-size: .875rem;
    font-weight: 700;
    color: var(--soft);
  }

  .row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    align-items: end;
    gap: 10px;
  }

  .checks {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 14px;
    margin-block-start: 10px;

    /* beside a field in a row: centred on the 44px control */
    .row > & {
      align-items: center;
      min-block-size: 44px;
    }

    /* under a field heading: the field's own gap separates them */
    .f > & { margin-block-start: 0; }

    & label {
      display: flex;
      align-items: center;
      gap: 8px;
      min-block-size: 36px;
      font-size: .9375rem;
      cursor: pointer;
    }

    & input {
      inline-size: 20px;
      block-size: 20px;
      accent-color: var(--acc);
    }
  }

  /* colour rows: presets and the free picker, five 44px cells to a row */
  .sw {
    display: grid;
    grid-template-columns: repeat(5, 44px);
    gap: 12px;
    margin-block-start: 6px;

    & button, & .chip {
      display: grid;
      place-items: center;
      inline-size: 44px;
      block-size: 44px;
      border: 2px solid var(--field-line);
      border-radius: 50%;
      transition: var(--t-config);
    }

    & button {
      cursor: pointer;

      &:hover {
        border-color: var(--acc);
        box-shadow: 0 3px 10px rgba(150, 70, 80, .2);
        transform: scale(1.08);
      }

      &:active { transform: var(--press); }
      &[aria-pressed=true] { outline: var(--ring); outline-offset: 2px; }
    }

    /* the chosen colour also carries a check mark, so selection does not rest on the ring alone */
    & button[aria-pressed=true]::after, & .swc.is-on .chip::after {
      content: "\2713";
      font-size: 1.125rem;
      font-weight: 800;
      color: #4a2f33;
    }

    & button[data-dark][aria-pressed=true]::after, & .swc.is-on[data-dark] .chip::after { color: #fff; }

    /* keyboard focus: a thin ink ring outside where the selection ring sits */
    & button:focus-visible, & .swc:has(input:focus-visible) .chip {
      outline: 2px solid var(--ink);
      outline-offset: 6px;
    }

    /* the free picker: the native input lies invisible over a chip, a rainbow until a colour of its own is chosen */
    & .swc {
      position: relative;
      inline-size: 44px;
      block-size: 44px;
      cursor: pointer;

      & input {
        position: absolute;
        inset: 0;
        inline-size: 100%;
        block-size: 100%;
        padding: 0;
        border: 0;
        opacity: 0;
        cursor: pointer;
      }

      & .chip { background: conic-gradient(#e66, #eb5, #9c6, #5bc, #78d, #c7c, #e66); }

      & .chip::after {
        content: "+";
        display: grid;
        place-items: center;
        inline-size: 22px;
        block-size: 22px;
        font-weight: 800;
        color: #4a2f33;
        background: #fff;
        border-radius: 50%;
      }

      &:hover .chip { border-color: var(--acc); }

      &.is-on .chip {
        background: var(--c);
        outline: var(--ring);
        outline-offset: 2px;
      }

      &.is-on .chip::after { inline-size: auto; block-size: auto; background: none; }
    }
  }

  .hint.warn {
    font-weight: 700;
    color: light-dark(#a8322a, #f3978c);
  }

  .swn {
    font-weight: 600;
    color: var(--ink);
  }

  .hint {
    margin: 6px 0 0;
    font-size: .8125rem;
    color: var(--soft);

    & a {
      font-weight: 700;
      color: var(--acc-text);
    }
  }

  /* moods and styles */
  .ambs {
    display: grid;
    gap: 12px;
    margin-block-start: 6px;
  }

  .ambg {
    display: grid;
    gap: 6px;
  }

  .ambl {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
    gap: 8px;

    & button {
      display: flex;
      align-items: center;
      gap: 10px;
      min-block-size: 50px;
      padding: 6px 10px;
      font-size: .875rem;
      font-weight: 700;
      line-height: 1.2;
      text-align: start;
      background: var(--field);
      border: 1.5px solid var(--line);
      border-radius: 12px;
      cursor: pointer;
      transition: var(--t-config);
      -webkit-touch-callout: none;
      -webkit-user-select: none;
      user-select: none;

      &:hover { border-color: var(--acc); }

      /* the applied theme keeps its mark while another one is previewed */
      &[aria-pressed=true]::after {
        content: "\2713";
        margin-inline-start: auto;
        font-size: 1.125rem;
        font-weight: 800;
        color: var(--acc-text);
      }
    }

    & i {
      flex: none;
      inline-size: 30px;
      block-size: 38px;
      border: 1px solid rgba(60, 30, 40, .18);
      border-radius: 5px;
    }
  }

  .styles {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: minmax(54px, 1fr);
    gap: 8px;
    margin-block-start: 6px;

    & button {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-start;
      gap: 5px;
      block-size: 100%;
      min-block-size: 54px;
      padding: 6px;
      font-size: .875rem;
      font-weight: 700;
      line-height: 1.2;
      text-align: center;
      background: var(--field);
      border: 1.5px solid var(--line);
      border-radius: var(--r-field);
      cursor: pointer;
      transition: var(--t-config);

      &[aria-pressed=true] .si { --sic: var(--field); }

      &:hover .si { transform: scale(1.08); }
    }
  }

  /* the forbidden shelf: a theme card whose swatch is blurred behind a padlock */
  .ambl button.tease {
    border-style: dashed;

    & i {
      position: relative;
      overflow: hidden;
      background: none;

      &::before {
        content: "";
        position: absolute;
        inset: -6px;
        background: var(--sw);
        filter: blur(4px);
      }

      &::after {
        content: "\1F512";
        position: absolute;
        inset: 0;
        display: grid;
        place-items: center;
        font-size: 1rem;
      }
    }
  }

  /* a text button: closes the forbidden shelf, makes a colour legible */
  button.lnk {
    justify-self: start;
    padding: 4px 0;
    font: inherit;
    font-size: .8125rem;
    color: var(--acc-text);
    text-decoration: underline;
    background: none;
    border: 0;
    cursor: pointer;
  }

  /* shared hover and selected look of the mood and style buttons */
  :is(.styles, .ambl) button {
    &[aria-pressed=true] {
      background: var(--acc-tint);
      border-color: var(--acc);
      box-shadow: 0 0 0 2px var(--acc);
    }

    &:hover:not([aria-pressed=true]) {
      background: var(--acc-wash);
      border-color: var(--acc);
      box-shadow: 0 4px 12px rgba(150, 70, 80, .14);
      transform: translateY(-1px);
    }

    &[aria-pressed=true]:hover { box-shadow: 0 0 0 2px var(--acc), 0 4px 12px rgba(150, 70, 80, .2); }

    &:active { transform: var(--press); }
  }

  .si {
    --sic: var(--acc-tint);
    --sil: light-dark(#fff, #2a2024);
    flex: none;
    inline-size: 24px;
    block-size: 28px;
    color: var(--acc);
    transition: transform .15s;
  }

  /* mobile navigation between sections */
  .jump {
    display: none;
    padding: 2px 0 6px;

    @media (hover: none) and (pointer: coarse) and (max-width: 900px) { display: block; }

    @media (max-width: 900px) {
      position: sticky;
      inset-block-start: 0;
      z-index: 5;
      padding: 8px 0;
      background: var(--bg);
    }

    & a {
      display: inline-flex;
      align-items: center;
      flex: none;
      min-block-size: 38px;
      padding: 8px 14px;
      font-size: .875rem;
      font-weight: 700;
      color: var(--ink);
      text-decoration: none;
      background: var(--panel);
      border: 1.5px solid var(--line);
      border-radius: var(--r-pill);

      &:hover {
        color: var(--acc);
        border-color: var(--acc);
      }
    }

    & > div {
      display: flex;
      gap: 6px;
      padding-inline-end: 40px;
      overflow-x: auto;
      scrollbar-width: none;
      mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent);

      /* last: Firefox ignores this pseudo-element, and the rule with it */
      &::-webkit-scrollbar { display: none; }
    }
  }

  /* pages and preview */
  .pager {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-block-start: -4px;

    & .ub {
      font-size: .9375rem;

      &.pend {
        color: var(--accInk);
        background: var(--acc);
        border-color: var(--acc);
      }
    }

    & span {
      min-inline-size: 4ch;
      font-weight: 700;
      text-align: center;
      color: var(--soft);
    }
  }

  .stagehint {
    margin: -4px 0 0;
    font-size: .8125rem;
    text-align: center;
    color: var(--soft);
  }

  .stage .peekb {
    position: absolute;
    inset-block-start: 10px;
    left: 50%;
    z-index: 3;
    padding: 6px 14px;
    font-size: .875rem;
    font-weight: 700;
    white-space: nowrap;
    color: var(--accInk);
    background: var(--acc);
    border-radius: var(--r-pill);
    box-shadow: 0 4px 14px rgba(60, 20, 30, .25);
    transform: translateX(-50%);
    pointer-events: none;

    &[hidden] { display: none; }
  }

  #peekBox {
    position: fixed;
    inset-block-start: 12px;
    left: 50%;
    z-index: 25;
    display: grid;
    justify-items: center;
    gap: 6px;
    padding: 8px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--r-card);
    box-shadow: 0 16px 40px rgba(60, 20, 30, .3);
    transform: translateX(-50%);
    pointer-events: none;

    &[hidden] { display: none; }

    & canvas {
      display: block;
      inline-size: 200px;
      block-size: auto;
      border-radius: 6px;
    }

    & b { font-size: .875rem; }
  }

  #cv {
    &.pick { cursor: pointer; }
    &.grabbing { cursor: grabbing; }
  }

  /* reading list */
  #listNumIn [data-n] { cursor: grab; }
  #listNum.grabbing, #listNum.grabbing [data-n] { cursor: grabbing; }

  .ldrop {
    position: absolute;
    inset-inline: 0;
    z-index: 2;
    block-size: 3px;
    background: var(--acc);
    pointer-events: none;
  }

  .lw {
    position: relative;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    overflow: hidden;
    background: var(--field);
    border: 1.5px solid var(--field-line);
    border-radius: var(--r-field);

    &:focus-within {
      outline: var(--ring);
      outline-offset: 2px;
    }

    /* the focus ring is carried by the .lw frame (focus-within) */
    & textarea {
      min-block-size: 160px;
      padding: 10px 12px;
      overflow-x: hidden;
      overflow-y: auto;
      font: .875rem/22px var(--font-ui);
      white-space: pre-wrap;
      overflow-wrap: anywhere;
      border: 0;
      border-radius: 0;
      outline: none;
    }
  }

  /* invisible copy of the field, measured by the script: same metrics as .lw textarea */
  .lmirror {
    position: absolute;
    inset-block-start: 0;
    inset-inline-start: 0;
    box-sizing: border-box;
    padding: 10px 12px;
    font: .875rem/22px var(--font-ui);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    visibility: hidden;
    pointer-events: none;
  }

  .lnum {
    min-inline-size: 34px;
    margin: 0;
    padding-block: 10px;
    padding-inline: 10px 8px;
    overflow: hidden;
    font: 700 .75rem/22px var(--font-ui);
    text-align: end;
    color: var(--acc-text);
    background: var(--acc-tint);
    border-inline-end: 1px solid var(--line);
    user-select: none;
  }

  .selbtn {
    display: flex;
    gap: 6px;

    & select {
      flex: 1;
      min-inline-size: 0;
    }
  }

  .ib {
    display: grid;
    place-items: center;
    flex: none;
    inline-size: 44px;
    block-size: 44px;
    padding: 0;
    color: var(--acc);
    background: var(--field);
    border: 1.5px solid var(--line);
    border-radius: var(--r-field);
    cursor: pointer;

    &:hover {
      color: var(--accInk);
      background: var(--acc);
      border-color: var(--acc);
    }

    & .bi {
      inline-size: 20px;
      block-size: 20px;
    }
  }

  .lh {
    display: flex;
    align-items: center;
    gap: 8px;

    & label {
      font-size: .875rem;
      font-weight: 700;
      color: var(--soft);
    }
  }

  .qm {
    position: relative;
    flex: none;
    inline-size: 24px;
    block-size: 24px;
    padding: 0;
    font-size: .8125rem;
    font-weight: 800;
    line-height: 1;
    color: var(--acc);
    background: none;
    border: 1.5px solid var(--acc);
    border-radius: 50%;
    cursor: pointer;

    &[aria-expanded=true], &:hover {
      color: var(--accInk);
      background: var(--acc);
    }

    &::before {
      content: "";
      position: absolute;
      inset: -10px;
    }
  }

  /* line syntax help */
  .grambox {
    margin: 4px 0 2px;
    padding: 2px 12px 8px;
    background: var(--acc-wash);
    border: 1px solid var(--line);
    border-radius: var(--r-field);

    & code {
      padding: 2px 6px;
      font: 600 .78125rem/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
      overflow-wrap: anywhere;
      background: var(--acc-tint);
      border-radius: 6px;
    }

    & .gk {
      display: inline-block;
      margin-inline-end: 4px;
      padding: 1px 6px;
      color: var(--acc-text);
      background: var(--acc-tint);
      border-radius: 6px;
    }

    & .gx {
      margin-block-end: 10px;
      padding: 8px 10px;
      overflow-x: auto;
      line-height: 2;
      white-space: normal;
      background: var(--field);
      border: 1px solid var(--line);
      border-radius: 8px;

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

      & .gk { margin: 0 2px; }
    }

    & .gt-t { color: var(--ink); }
    & .gt-s { color: var(--soft); }

    & .gt-a {
      font-style: italic;
      color: var(--ink);
    }

    & .gl {
      display: grid;
      grid-template-columns: auto minmax(0, 1fr);
      align-items: center;
      gap: 6px 12px;
      margin: 0;

      & dt {
        display: flex;
        flex-wrap: wrap;
        gap: 2px;
      }

      & dd {
        display: flex;
        align-items: center;
        gap: 6px;
        margin: 0;
        font-size: .84375rem;
        color: var(--ink);
      }

      & code:not(.gk) {
        padding: 0;
        background: none;
      }
    }
  }

  .gsw {
    flex: none;
    inline-size: 12px;
    block-size: 12px;
    background: #c96560;
    border-radius: 50%;
  }

  .gn {
    margin: 10px 0 0;
    font-size: .8125rem;
    color: var(--soft);
  }

  /* book editor */
  #bookEd {
    position: fixed;
    z-index: 20;
    inline-size: min(340px, calc(100vw - 24px));
    max-block-size: calc(100dvh - 24px);
    padding: 12px 16px 16px;
    overflow: auto;
    overscroll-behavior: contain;
    color: var(--ink);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: 0 18px 50px rgba(60, 20, 30, .3);

    @media (max-width: 600px) {
      /* !important: the script places the panel with inline left/top; on mobile it docks at the bottom */
      inset-inline-start: 12px !important;
      inset-inline-end: 12px;
      inset-block-start: auto !important;
      inset-block-end: 12px;
      inline-size: auto;
    }

    &.drop {
      background: var(--acc-wash);
      outline: 3px dashed var(--acc);
      outline-offset: -6px;
    }

    & .btn {
      gap: 6px;
      min-block-size: 40px;
      padding: 6px 12px;
      font-size: .875rem;

      & .bi {
        inline-size: 16px;
        block-size: 16px;
      }
    }
  }

  #bookEd .be-h {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-block-end: 8px;
    border-block-end: 1px solid var(--line);

    & select {
      flex: 1;
      min-inline-size: 0;
      min-block-size: 40px;
      padding: 6px 10px;
      font-size: .9375rem;
      font-weight: 700;
      background: var(--field);
      border: 1.5px solid var(--field-line);
      border-radius: var(--r-field);
    }

    & button {
      display: grid;
      place-items: center;
      inline-size: 40px;
      block-size: 40px;
      padding: 0;
      color: var(--ink);
      background: none;
      border: 1.5px solid var(--line);
      border-radius: 50%;
      cursor: pointer;

      &:hover {
        color: var(--acc);
        border-color: var(--acc);
      }
    }
  }

  /* the edited book's picture, left of the title and author */
  #bookEd .be-id {
    display: grid;
    grid-template-columns: 112px minmax(0, 1fr);
    align-items: end;
    gap: 12px;

    & canvas {
      align-self: center;
      inline-size: 112px;
      block-size: 130px;
      margin-block-start: 10px;
    }
  }

  #bookEd .be-act {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-block-start: 12px;
    padding-block-start: 10px;
    border-block-start: 1px solid var(--line);

    & .btn {
      flex: 1 1 0;
      justify-content: center;
      padding: 6px 8px;

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

    }
  }

  /* the live status: what the editor does with each change, and the way back */
  #bookEd .be-live {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px 6px;
    margin: 12px 0 0;
    font-size: .8125rem;
    color: var(--soft);

    & .bi {
      inline-size: 16px;
      block-size: 16px;
      color: var(--acc-text);
    }

    & button.lnk:disabled {
      text-decoration: none;
      opacity: .5;
      cursor: default;
    }
  }

  /* Reset, quiet and apart, at the start; Done, the main action, at the end */
  #bookEd .be-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-block-start: 10px;

    & .reset {
      color: light-dark(#a8322a, #f3978c);
      border-color: transparent;

      &:hover {
        color: #fff;
        background: var(--danger);
      }
    }

    & .main { padding-inline: 20px; }
  }

  #beDrop {
    display: flex;
    align-items: center;
    gap: 12px;
    min-block-size: 64px;
    margin-block-start: 4px;
    padding: 8px 12px;
    font-size: .875rem;
    font-weight: 600;
    color: var(--soft);
    border: 2px dashed var(--line);
    border-radius: 12px;
    cursor: pointer;
    transition: border-color .15s, background-color .15s;

    /* --acc-text for the text: the accent alone reaches only 4.35:1 on --acc-wash; the border keeps the accent */
    &:hover, #bookEd.drop & {
      color: var(--acc-text);
      background: var(--acc-wash);
      border-color: var(--acc);
    }

    & span {
      display: flex;
      align-items: center;
      gap: 8px;
    }
  }

  #beThumb {
    flex: none;
    inline-size: 40px;
    block-size: 52px;
    object-fit: cover;
    border-radius: 4px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, .2);
  }

  #beStars {
    display: flex;
    gap: 2px;
    margin-block-start: 4px;

    & button {
      inline-size: 34px;
      block-size: 38px;
      padding: 0;
      font-size: 1.625rem;
      line-height: 1;
      /* unlit star: a state, so the same contrast as the field border */
      color: var(--field-line);
      background: none;
      border: 0;
      cursor: pointer;

      &.on { color: #e0a94a; }
      &:hover { transform: scale(1.15); }
    }
  }

  #beDate {
    inline-size: 100%;
    min-block-size: 40px;
    padding: 6px 10px;
    font-size: .9375rem;
    background: var(--field);
    border: 1.5px solid var(--field-line);
    border-radius: var(--r-field);
  }

  /* the book editor's Auto: the colour the palette gives this book, dashed and marked A until it is the choice */
  #beCol button.auto {
    border-style: dashed;

    &:not([aria-pressed=true])::after {
      content: "A";
      font-size: .875rem;
      font-weight: 800;
      color: #4a2f33;
    }

    &[data-dark]:not([aria-pressed=true])::after { color: #fff; }
  }

  /* About */
  #about {
    inline-size: min(460px, calc(100vw - 32px));
    padding: 20px 22px 16px;
    color: var(--ink);
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--r-card);
    box-shadow: 0 18px 50px rgba(60, 20, 30, .3);

    &::backdrop { background: rgba(40, 20, 28, .35); }

    & .about-h {
      display: flex;
      align-items: center;
      gap: 14px;
      margin-block-end: 8px;

      & h2 { margin: 0; }
    }

    & h2 {
      margin: 0 0 8px;
      font: 700 1.375rem/1.2 Lora, serif;
    }

    & h3 {
      margin: 14px 0 4px;
      font-size: .9375rem;
      font-weight: 800;
    }

    & p, & ul {
      margin: 0 0 8px;
      font-size: .9375rem;
    }

    & ul { padding-inline-start: 1.2em; }

    & a {
      font-weight: 700;
      color: var(--acc-text);
    }

    & .about-ver {
      font-size: .8125rem;
      color: var(--soft);
    }

    & .bar {
      justify-content: space-between;
      align-items: center;
      margin-block-start: 12px;
    }
  }

  /* tips; physical left: --ax is an x position measured by the script, and centring goes through translateX */
  #tip {
    position: fixed;
    z-index: 30;
    inline-size: min(300px, calc(100vw - 24px));
    padding: 14px 16px 12px;
    font-size: .9375rem;
    line-height: 1.4;
    color: var(--panel);
    background: var(--ink);
    border-radius: var(--r-card);
    box-shadow: 0 16px 40px rgba(40, 10, 20, .35);

    &[hidden] { display: none; }

    &::before {
      content: "";
      position: absolute;
      left: var(--ax, 50%);
      inline-size: 14px;
      block-size: 14px;
      background: inherit;
      transform: translateX(-50%) rotate(45deg);
    }

    &.below::before { inset-block-start: -7px; }
    &.above::before { inset-block-end: -7px; }

    & .tb {
      display: flex;
      align-items: center;
      gap: 8px;
      margin-block-start: 12px;

      & span {
        margin-inline-end: auto;
        font-size: .8125rem;
        opacity: .75;
      }
    }

    & button {
      min-block-size: 36px;
      padding: 4px 14px;
      font: inherit;
      font-size: .875rem;
      font-weight: 700;
      border-radius: var(--r-pill);
      cursor: pointer;
    }

    & .tn {
      color: var(--accInk);
      background: var(--acc);
      border: 0;
    }

    & .ts {
      color: inherit;
      background: none;
      border: 1.5px solid currentColor;
      opacity: .8;
    }
  }

  /* enlarging and printing */
  #ov {
    position: fixed;
    inset: 0;
    z-index: 9;
    display: none;
    place-items: center;
    padding: 20px;
    overflow: auto;
    background: rgba(30, 15, 20, .8);

    &.on { display: grid; }

    & img {
      max-inline-size: 100%;
      max-block-size: 85svh;
      border-radius: 8px;
    }

    & p {
      margin: 10px 0 0;
      text-align: center;
      color: #fff;
    }
  }

  #printImg {
    display: none;

    @media print {
      display: block;
      inline-size: 210mm;
      block-size: 297mm;
      object-fit: contain;
    }
  }

  /* last: a tip's highlight beats the radius of the designated block (.exp) */
  .tipon {
    border-radius: var(--r-field);
    outline: var(--ring);
    outline-offset: 4px;
    transition: outline-color .2s;
  }
}

/* ---- utilities ---- */
@layer utilities {
  [data-pct] { white-space: nowrap; }

  @media (prefers-reduced-motion: reduce) {
    /* !important: the user's setting stops every animation, whatever the component */
    *, *::before, *::after {
      animation: none !important;
      transition: none !important;
    }
  }

  @media print {
    @page {
      size: A4 portrait;
      margin: 0;
    }

    /* !important: when printing, only the poster comes out, even when a panel is open */
    header, .app, #ov, #msg { display: none !important; }
  }
}
