/* Latte Web house style for every demo page.
 *
 * Base element styles live in the `latte-site` cascade layer. Any style a page
 * writes itself is unlayered, so it always wins, whatever its specificity or
 * order: demos keep full control of their own layout.
 *
 * The top bar is unlayered on purpose and uses its own class names, so page
 * resets such as `* { margin: 0; padding: 0 }` cannot flatten it.
 *
 * Demo pages stay light: many demos paint light backgrounds without setting a
 * text colour, and a dark theme would make that text unreadable.
 */

@layer latte-site {
  :root {
    color-scheme: light;
    --latte-bg: #fbf7f2;
    --latte-fg: #2b1d14;
    --latte-muted: #6f5a4a;
    --latte-accent: #8a4b2a;
    --latte-accent-fg: #fffaf4;
    --latte-card: #ffffff;
    --latte-line: #e7dccf;
    --latte-code: #f3ebe1;
    --latte-focus: #c26a35;
    --latte-font:
      system-ui, -apple-system, 'Segoe UI', Roboto, 'PingFang SC',
      'Noto Sans SC', sans-serif;
    --latte-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  }

  html {
    background: var(--latte-bg);
  }

  body {
    font: 17px/1.6 var(--latte-font);
    color: var(--latte-fg);
    background: var(--latte-bg);
  }

  h1 {
    margin: 1.25rem 0 0.75rem;
    font-size: 2rem;
    line-height: 1.2;
    letter-spacing: -0.01em;
  }

  h2 {
    margin: 1.75rem 0 0.5rem;
    font-size: 1.3rem;
    line-height: 1.3;
  }

  p {
    margin: 0.5rem 0 1rem;
  }

  a {
    color: var(--latte-accent);
    text-underline-offset: 0.15em;
  }

  code,
  kbd,
  samp,
  pre {
    font-family: var(--latte-mono);
    font-size: 0.9em;
  }

  code {
    padding: 0.1em 0.35em;
    border-radius: 0.3em;
    background: var(--latte-code);
  }

  pre {
    padding: 0.9rem 1rem;
    overflow-x: auto;
    border-radius: 0.5rem;
    background: var(--latte-code);
  }

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

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

  /* Only right and bottom margins: positioned demo elements keep their left/top. */
  button {
    margin: 0 0.5rem 0.5rem 0;
    padding: 0.4rem 0.95rem;
    border: 1px solid var(--latte-accent);
    border-radius: 0.5rem;
    background: var(--latte-accent);
    color: var(--latte-accent-fg);
    cursor: pointer;
  }

  button:hover {
    filter: brightness(1.08);
  }

  button:disabled {
    opacity: 0.55;
    cursor: not-allowed;
  }

  input,
  select,
  textarea {
    max-width: 100%;
    padding: 0.35rem 0.55rem;
    border: 1px solid var(--latte-line);
    border-radius: 0.4rem;
    background: var(--latte-card);
    color: var(--latte-fg);
  }

  /* A textarea sized in columns (cols="70") is wider than a phone; with its
   * padding and border counted in its width, it stops at the screen edge. */
  textarea {
    box-sizing: border-box;
  }

  /* On a touch screen, controls are big enough for a thumb: 40 px, close to
   * the 44 px that phone makers advise, keeps rows of buttons compact. A
   * checkbox or radio button grows from its 13 px default to 24 px. */
  @media (width < 48rem) {
    button,
    select,
    input:not([type='checkbox'], [type='radio'], [type='range']) {
      min-height: 2.5rem;
    }

    input[type='checkbox'],
    input[type='radio'] {
      width: 1.5rem;
      height: 1.5rem;
      vertical-align: middle;
    }
  }

  output {
    display: inline-block;
    margin: 0.25rem 0 0.75rem;
    padding: 0.3rem 0.7rem;
    border-radius: 0.4rem;
    background: var(--latte-code);
    font-family: var(--latte-mono);
  }

  :focus-visible {
    outline: 3px solid var(--latte-focus);
    outline-offset: 2px;
  }

  hr {
    border: 0;
    border-top: 1px solid var(--latte-line);
  }

  /* The one-line summary under the heading of pages built from the catalog. */
  .latte-lead {
    margin-top: -0.25rem;
    color: var(--latte-muted);
    font-size: 1.05rem;
  }
}

/* The page frame is unlayered so that universal resets such as
 * `* { margin: 0; padding: 0 }` do not push content against the window edge.
 * A page's own `body` rules still win: same specificity, and they come later. */
body {
  /* border-box: a page that sets body { width: 100% } must not overflow by the padding. */
  box-sizing: border-box;
  max-width: 64rem;
  margin: 0 auto;
  padding: 0 1.25rem 3rem;
}

/* The top bar: brand link to the learning index, and the page's README. */
.latte-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 1rem;
  margin: 0 0 1rem;
  padding: 0.8rem 0;
  border-bottom: 1px solid #e7dccf;
  font:
    600 0.9rem/1.4 system-ui,
    -apple-system,
    'Segoe UI',
    sans-serif;
  letter-spacing: 0.01em;
}

/* Pages may style `nav a` (the motion demo makes such links 180 px blocks);
 * the bar keeps its own look. */
.latte-bar a {
  display: inline;
  width: auto;
  height: auto;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: #8a4b2a;
  text-decoration: none;
}

.latte-bar a:hover {
  text-decoration: underline;
}

.latte-bar .latte-home::before {
  content: '';
  display: inline-block;
  width: 0.85em;
  height: 0.85em;
  margin-right: 0.45em;
  vertical-align: -0.08em;
  border-radius: 0.2em 0.2em 0.35em 0.35em;
  background: linear-gradient(#fffaf2 0 25%, #e3c7a4 25% 75%, #6b4226 75% 100%);
  box-shadow: inset 0 0 0 1.5px #2b1d14;
}

.latte-bar .latte-crumb {
  color: #6f5a4a;
  font-weight: 500;
}

.latte-bar .latte-links {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-left: auto;
}

/* The language switch (assets/language.js). Pages may style `button`, so the
 * bar's switch keeps its own look, like the bar's links. */
.latte-bar .latte-language {
  margin: 0;
  padding: 0.1rem 0.55rem;
  border: 1px solid #e7dccf;
  border-radius: 999px;
  background: transparent;
  color: #8a4b2a;
  font: inherit;
  cursor: pointer;
}

.latte-bar .latte-language:hover {
  border-color: #8a4b2a;
}

.latte-bar .latte-language[hidden] {
  display: none;
}

/* On a phone the bar keeps to one line: the section's name goes, since the
 * page's heading names the page. Padding above and below an inline link
 * enlarges what a thumb can hit without moving the line. */
@media (width < 30rem) {
  .latte-bar .latte-crumb {
    display: none;
  }
}

@media (width < 48rem) {
  .latte-bar a {
    padding-block: 0.6rem;
  }

  .latte-bar .latte-language {
    min-height: 2.25rem;
    padding: 0.2rem 0.75rem;
  }
}

/* One language at a time: language.js sets data-language on <html>, and the
 * other language's elements disappear. Only hiding is needed, so every
 * element keeps the display its page gives it. */
:root:not([data-language='zh']) [data-l='zh'],
:root[data-language='zh'] [data-l='en'] {
  display: none !important;
}

/* A demo that needs its own fixed width, such as a 500 px box in a CSS lesson,
 * sits in this frame. On a narrow screen the frame scrolls sideways instead of
 * the page, and the demo keeps the sizes its lesson measures. The padding
 * leaves room for the guide's dashed outline; the negative margin keeps the
 * demo where it was. */
.latte-wide {
  overflow-x: auto;
  padding: 0.5rem;
  margin: -0.5rem;
}

/* The guide panel beside a demo (assets/guide.js). Unlayered with its own class
 * names, like the top bar, so page styles for `button` or `li` leave it alone. */
.latte-guide {
  box-sizing: border-box;
  margin: 2rem 0 0;
  padding: 1rem 1.1rem;
  border: 1px solid #e7dccf;
  border-radius: 0.8rem;
  background: #fffdf9;
  color: #2b1d14;
  box-shadow: 0 8px 24px rgb(43 29 20 / 8%);
  font:
    400 0.95rem/1.5 system-ui,
    -apple-system,
    'Segoe UI',
    'PingFang SC',
    'Noto Sans SC',
    sans-serif;
  text-align: left;
}

.latte-guide .latte-guide-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

/* The heading takes the free space, so the buttons after it sit on the right. */
.latte-guide .latte-guide-top .latte-guide-head {
  flex: 1;
  margin: 0;
}

.latte-guide button.latte-guide-lang,
.latte-guide button.latte-guide-fold {
  margin: 0;
  padding: 0.1rem 0.6rem;
  background: transparent;
  color: #8a4b2a;
  font-size: 0.8rem;
}

/* Only the sheet on a narrow screen folds (below). */
.latte-guide button.latte-guide-fold {
  display: none;
}

@media (min-width: 72rem) {
  /* The demo keeps its own width; the panel takes a fixed column on the right. */
  html.latte-guided body {
    max-width: min(64rem, calc(100% - 26rem));
    margin-left: max(0px, calc((100% - 26rem - 64rem) / 2));
    margin-right: 26rem;
  }

  .latte-guide {
    position: fixed;
    top: 1rem;
    right: 1rem;
    width: 24rem;
    max-height: calc(100vh - 2rem);
    margin: 0;
    overflow: auto;
  }
}

.latte-guide p,
.latte-guide dl,
.latte-guide ol {
  margin: 0 0 0.6rem;
  padding: 0;
}

.latte-guide .latte-guide-head,
.latte-guide .latte-guide-label {
  color: #6f5a4a;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.latte-guide .latte-guide-title {
  font-size: 1.05rem;
  font-weight: 700;
}

/* A long name such as `PerformanceObserver.supportedEntryTypes` is wider than
 * the panel on a phone; it breaks instead of pushing the page sideways. */
.latte-guide code {
  padding: 0.05em 0.3em;
  border-radius: 0.3em;
  background: #f3ebe1;
  font:
    0.9em ui-monospace,
    SFMono-Regular,
    Menlo,
    Consolas,
    monospace;
  overflow-wrap: anywhere;
}

.latte-guide button {
  margin: 0 0.5rem 0.6rem 0;
  padding: 0.35rem 0.9rem;
  border: 1px solid #8a4b2a;
  border-radius: 0.5rem;
  background: #8a4b2a;
  color: #fffaf4;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.4;
  cursor: pointer;
}

.latte-guide button.latte-guide-nav {
  margin-bottom: 0;
  background: transparent;
  color: #8a4b2a;
}

.latte-guide .latte-guide-buttons {
  margin-bottom: 0.8rem;
  padding-bottom: 0.7rem;
  border-bottom: 1px solid #e7dccf;
}

.latte-guide button:disabled {
  opacity: 0.45;
  cursor: default;
}

/* A long label, such as `Object.getPrototypeOf(instance) === Fn.prototype`,
 * wraps within 60 % of the width, so its value never shrinks to a letter per
 * line. */
.latte-guide .latte-guide-watch {
  display: grid;
  grid-template-columns: fit-content(60%) minmax(0, 1fr);
  gap: 0.2rem 0.75rem;
  align-items: baseline;
  font-size: 0.85rem;
}

.latte-guide .latte-guide-watch dt {
  color: #6f5a4a;
  overflow-wrap: anywhere;
}

.latte-guide .latte-guide-watch dd {
  margin: 0;
  font:
    700 0.95em/1.5 ui-monospace,
    SFMono-Regular,
    Menlo,
    Consolas,
    monospace;
  overflow-wrap: anywhere;
}

/* The line sits on the console, so a page that prints nothing has no stray rule. */
.latte-guide .latte-guide-console {
  margin-top: 0.8rem;
  padding-top: 0.7rem;
  border-top: 1px solid #e7dccf;
}

.latte-guide .latte-guide-lines {
  max-height: 16rem;
  overflow: auto;
  list-style: none;
  font:
    0.82rem/1.45 ui-monospace,
    SFMono-Regular,
    Menlo,
    Consolas,
    monospace;
}

.latte-guide .latte-guide-line {
  margin: 0;
  padding: 0.1rem 0.5rem;
  border-left: 3px solid #e7dccf;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* A printed line or a readout is a <samp>; it keeps the panel's font instead
 * of the browser's default monospace size. */
.latte-guide .latte-guide-line samp,
.latte-guide .latte-guide-watch samp {
  font: inherit;
}

.latte-guide .latte-guide-line.warn {
  border-left-color: #c9a227;
}

.latte-guide .latte-guide-line.error {
  border-left-color: #b3261e;
  color: #b3261e;
}

.latte-guide .latte-guide-line.match {
  border-left-color: #8a4b2a;
  background: #f3ebe1;
}

/* Below 72rem the panel loses its column. On most screens it becomes a sheet
 * over the bottom of the window; on a short, wide screen, such as a phone on
 * its side, it is a narrower column on the right. Either way the reader sees
 * the demo while stepping through it, and every button suits a thumb.
 * assets/guide.js reads --latte-guide-sheet to know which one is shown. */
@media (width < 72rem) {
  /* Previous and Next share the row. */
  .latte-guide .latte-guide-buttons {
    display: flex;
    gap: 0.5rem;
  }

  .latte-guide button.latte-guide-nav {
    flex: 1;
    min-height: 2.5rem;
    margin: 0;
  }

  .latte-guide button.latte-guide-lang,
  .latte-guide button.latte-guide-fold {
    min-height: 2.25rem;
    padding: 0.2rem 0.8rem;
  }

  .latte-guide .latte-guide-lines {
    max-height: 9rem;
  }
}

/* The sheet. An empty block after the page's content lets its end scroll
 * above the sheet; unlike padding, it also works on a page that fixes the
 * height of body. svh is the height with the browser's toolbars shown, so the
 * sheet never covers more than half. The fold button shrinks it to its top
 * row. */
@media (width < 33rem), (width < 72rem) and (height >= 35rem) {
  html.latte-guided body::after {
    content: '';
    display: block;
    height: calc(min(50vh, 30rem) + 1rem);
    height: calc(min(50svh, 30rem) + 1rem);
  }

  .latte-guide {
    --latte-guide-sheet: 1;
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 100;
    max-width: 40rem;
    max-height: min(50vh, 30rem);
    max-height: min(50svh, 30rem);
    margin: 0 auto;
    overflow: auto;
    overscroll-behavior: contain;
    border-bottom: 0;
    border-radius: 1rem 1rem 0 0;
    box-shadow: 0 -6px 24px rgb(43 29 20 / 14%);
  }

  .latte-guide button.latte-guide-fold {
    display: inline-block;
  }

  .latte-guide.latte-guide-folded > :not(.latte-guide-top) {
    display: none;
  }

  .latte-guide.latte-guide-folded .latte-guide-top {
    margin-bottom: 0;
  }
}

/* The column on a short, wide screen: too little height for a sheet. A page
 * that sets body { width: 100% } still stops at the column. */
@media (33rem <= width < 72rem) and (height < 35rem) {
  html.latte-guided body {
    max-width: calc(100% - min(24rem, 42vw));
    margin: 0 min(24rem, 42vw) 0 0;
  }

  .latte-guide {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    width: min(24rem, 42vw);
    margin: 0;
    overflow: auto;
    overscroll-behavior: contain;
    border-width: 0 0 0 1px;
    border-radius: 0;
    box-shadow: -6px 0 24px rgb(43 29 20 / 10%);
  }
}
