/* The public pages' languages (crates/tally-web/src/i18n, docs/tally/i18n.md).

   1. The language menu in every public header (i18n/switcher.rs): a disclosure styled as
      the theme menu beside it, whose button shows the page's language by its flag and
      code, opening onto the five languages as links. Without a script <details> opens and
      closes natively; with js/theme.js it is a keyboard menu that closes on a click
      outside. Motion: the list unfolds, its rows slide in one after another, the flag
      turns over on hover, the check draws in; none of it for prefers-reduced-motion.
   2. Typography that follows <html lang>: German and French hyphenate long words rather
      than overflow; Japanese takes its glyphs from the system's Japanese fonts (the brand
      faces cover Latin only), breaks lines by Japanese rules, keeps a heading's phrases
      together, and drops the tight Latin tracking. Text marked lang="en" inside a
      translated page (the product's own screens, sources) keeps English rules. */

/* ------------------------------------------------------------------ 1. The menu */

.lang-menu {
  position: relative;
  flex: none;
}

.lang-menu > summary {
  list-style: none;
}

.lang-menu > summary::-webkit-details-marker {
  display: none;
}

.lang-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 8px 0 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius-control);
  background: var(--surface);
  color: var(--ink-2);
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: .02em;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  transition: border-color .2s ease, color .2s ease;
}

.lang-button:hover,
.lang-menu[open] .lang-button {
  border-color: var(--faint);
  color: var(--ink);
}

.lang-button:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

.lang-flag,
.lang-item-flag {
  vertical-align: middle;
}

.lang-code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

.lang-chevron {
  width: 10px;
  height: 7px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.lang-menu[open] .lang-chevron {
  transform: rotate(180deg);
}

.lang-list {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 40;
  min-width: 196px;
  margin: 0;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-control);
  box-shadow: var(--shadow-menu);
  transform-origin: top right;
}

.lang-list-title {
  margin: 0;
  padding: 5px 10px 7px;
  color: var(--faint);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11.5px;
  line-height: 1.3;
}

.lang-items {
  display: grid;
  margin: 0;
  padding: 0;
  list-style: none;
}

.lang-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 5px;
  color: var(--ink-2);
  font-size: 14px;
  line-height: 1.3;
  text-decoration: none;
}

.lang-item:hover,
.lang-item:focus-visible {
  background: var(--surface-2);
  color: var(--ink);
  outline-offset: -2px;
}

.lang-item[aria-current="true"] {
  color: var(--ink);
  font-weight: 600;
}

.lang-item-name {
  flex: 1;
  white-space: nowrap;
}

.lang-item-code {
  color: var(--faint);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  letter-spacing: .04em;
}

.lang-check {
  flex: none;
  width: 10px;
  height: 6px;
  margin: -3px 2px 0 2px;
  border-left: 2px solid var(--brand);
  border-bottom: 2px solid var(--brand);
  opacity: 0;
  transform: rotate(-45deg);
}

.lang-item[aria-current="true"] .lang-check {
  opacity: 1;
}

.lang-item[aria-current="true"] .lang-item-code {
  display: none;
}

/* On a phone the menu never runs past the screen, and its button is the flag alone (the
   menu names every language when it opens), so the masthead's actions keep one line. */
@media (max-width: 560px) {
  /* The mastheads put the menu toward the left on a phone: it opens rightwards. */
  .lang-list {
    left: 0;
    right: auto;
    min-width: 176px;
    max-width: calc(100vw - 32px);
    transform-origin: top left;
  }
}

@media (max-width: 460px) {
  .lang-button {
    gap: 5px;
    padding: 0 7px;
  }

  .lang-code {
    display: none;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .lang-chevron {
    transition: transform .25s var(--ease-out);
  }

  .lang-flag {
    transition: transform .5s var(--ease-out);
  }

  .lang-button:hover .lang-flag {
    transform: rotateY(180deg);
  }

  .lang-menu[open] .lang-list {
    animation: lang-unfold .24s var(--ease-out) both;
  }

  .lang-menu[open] .lang-items > li {
    animation: lang-row .32s var(--ease-out) both;
  }

  .lang-menu[open] .lang-items > li:nth-child(2) { animation-delay: .03s; }
  .lang-menu[open] .lang-items > li:nth-child(3) { animation-delay: .06s; }
  .lang-menu[open] .lang-items > li:nth-child(4) { animation-delay: .09s; }
  .lang-menu[open] .lang-items > li:nth-child(5) { animation-delay: .12s; }

  .lang-menu[open] .lang-item[aria-current="true"] .lang-check {
    animation: lang-check .32s .14s var(--ease-out) both;
  }

  .lang-item-flag {
    transition: transform .3s var(--ease-out);
  }

  .lang-item:hover .lang-item-flag,
  .lang-item:focus-visible .lang-item-flag {
    transform: scale(1.18);
  }
}

@keyframes lang-unfold {
  from {
    opacity: 0;
    transform: translate3d(0, -4px, 0) scale(.97);
  }
}

@keyframes lang-row {
  from {
    opacity: 0;
    transform: translate3d(0, -3px, 0);
  }
}

@keyframes lang-check {
  from {
    opacity: 0;
    transform: rotate(-45deg) scale(.3);
  }
}

/* ------------------------------------------------------------ 2. Typography */

/* German and French compounds and long words break with hyphens rather than overflow. */
:lang(de),
:lang(fr) {
  hyphens: auto;
  -webkit-hyphens: auto;
  overflow-wrap: break-word;
}

/* Spanish too, more gently: only very long words need it. */
:lang(es) {
  overflow-wrap: break-word;
}

/* Controls, labels and code never hyphenate: a button wraps between words or not at all. */
:lang(de) :is(button, .button, summary, label, code, pre, .lang-item, .theme-item),
:lang(fr) :is(button, .button, summary, label, code, pre, .lang-item, .theme-item) {
  hyphens: manual;
  -webkit-hyphens: manual;
}

/* Japanese: the brand faces cover Latin only, so Japanese glyphs come from the system's
   Japanese fonts, named so every platform picks a good one (Latin stays in the brand
   faces). */
:root:lang(ja) {
  --font: Inter, -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN",
    "Noto Sans JP", "Yu Gothic UI", "Yu Gothic", Meiryo, "Segoe UI", sans-serif;
  --font-display: "Bricolage Grotesque", "Hiragino Sans", "Hiragino Kaku Gothic ProN",
    "Noto Sans JP", "Yu Gothic UI", "Yu Gothic", Meiryo, sans-serif;
}

body:lang(ja) {
  line-break: strict;
  word-break: normal;
  overflow-wrap: anywhere;
}

:lang(ja) :is(h1, h2, h3, h4, .chapter-n, .guide-kicker, .button, summary) {
  word-break: auto-phrase;
}

/* Tight Latin tracking reads cramped in Japanese; its letters keep their natural spacing,
   and its headings don't need the display face's heaviest weight to stand out. */
:lang(ja) :is(h1, h2, h3, .hero-title, .chapter-title, .guide-title, .guide-heading) {
  letter-spacing: 0;
}

:lang(ja) :is(.hero-title, .guide-title) {
  line-height: 1.25;
}

/* A table's cells keep a word whole ("読み取り", "アクセス"): breaking anywhere would let
   the table squeeze a short column down to a character or two. */
:lang(ja) :is(th, td) {
  word-break: auto-phrase;
  overflow-wrap: break-word;
}

/* Japanese body text reads better a little taller. */
:lang(ja) :is(p, li, dd) {
  line-height: 1.8;
}

/* Words of up to nine letters never break (no "Stri-pe", "Be-such"); longer ones break at
   least four letters in from either end. Headings don't hyphenate on wide screens, where
   they have the room: a broken word reads badly at that size. */
:lang(de),
:lang(fr) {
  hyphenate-limit-chars: 10 4 4;
}

:lang(de) :is(h1, h2, h3, .hero-title, .chapter-title, .guide-title),
:lang(fr) :is(h1, h2, h3, .hero-title, .chapter-title, .guide-title) {
  hyphens: manual;
  -webkit-hyphens: manual;
}

@media (max-width: 560px) {
  :lang(de) :is(h1, h2, h3, .chapter-title, .guide-title),
  :lang(fr) :is(h1, h2, h3, .chapter-title, .guide-title) {
    hyphens: auto;
    -webkit-hyphens: auto;
  }
}

/* The landing's rotating source on a phone: the English is sized so its longest source
   fits one line (landing.css); the translations' sources carry their article ("welcher
   Reddit-Thread", "quelle réponse de ChatGPT") and run longer, so they may take two
   lines (every source shares one grid cell, so the headline never jumps) at a size that
   keeps their longest word on the screen. */
@media (max-width: 460px) {
  :root:not(:lang(en)) .rotor-word {
    white-space: normal;
  }

  :lang(de) .hero-title,
  :lang(fr) .hero-title,
  :lang(es) .hero-title {
    font-size: 8.6vw;
  }

  :lang(ja) .hero-title {
    font-size: 8.2vw;
  }
}

/* The docs' menu button on a phone names the documentation in one word ("Documentation",
   "Documentación"): it keeps it whole and leaves the page's name the room to shorten. */
.docs-menu-label {
  flex: none;
  white-space: nowrap;
  hyphens: manual;
  -webkit-hyphens: manual;
}

/* What is in English on purpose keeps English rules inside a translated page. */
[lang="en"] {
  hyphens: manual;
  -webkit-hyphens: manual;
  line-break: auto;
  word-break: normal;
}
