* {
  box-sizing: border-box;
}

/* Chinese typeface. Previously nothing here named a CJK font at all: the UI
   stack below holds no Han glyphs, so every character came from whatever the
   browser fell back to -- PingFang SC on macOS, Microsoft YaHei on Windows,
   Noto Sans CJK on Linux. Three platforms, three faces, none of them chosen.
   Naming them makes the reader look the same everywhere it runs.

   Ordering matters and is deliberate: the Latin UI fonts come FIRST in the
   body stack, and the CJK families after. A browser takes the first family
   that actually has the glyph, so Latin renders from the UI font and only Han
   characters reach the CJK stack. Putting the CJK families first would hand
   them the Latin text too, and their bundled Latin glyphs are wide and ugly.

   Bundling a webfont was considered and rejected: a full Noto Sans SC is
   10-16MB, the extension already ships a 9MB dictionary, and every target
   platform already provides a perfectly good face. */
:root {
  --zh-hei: "PingFang SC", "Hiragino Sans GB", "Heiti SC", "Microsoft YaHei",
    "Noto Sans SC", "Source Han Sans SC", sans-serif;
  --zh-kai: "Kaiti SC", "STKaiti", "KaiTi", "Kai", "Noto Serif SC", serif;
  --zh-font: var(--zh-hei);
}

/* Set on <html> by lib/zhfont.js from the Options page's font setting. */
:root.zh-textbook {
  --zh-font: var(--zh-kai);
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", var(--zh-font);
  color: #1a1a1a;
  background: #fafafa;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: #b91c1c;
  color: #f5e6c8;
}

header h1 {
  font-size: 16px;
  margin: 0;
}

#settings-link {
  color: #f5e6c8;
  text-decoration: none;
  font-size: 22px;
}

#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  padding: 12px 14px;
  border-bottom: 1px solid #e2e2e2;
  background: #fff;
}

#controls label,
#controls .control-field {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: #555;
  gap: 4px;
}

#controls select {
  font-size: 13px;
  padding: 4px 6px;
}

/* Text-size stepper. This is the one control in the row that isn't a native
   form element, so every part of its box is matched to the <select>s beside
   it by measurement rather than by eye -- computed styles read off a real
   select in this panel:

     border-color   rgb(118, 118, 118)   Chrome's UA stylesheet value for
                                         form controls. Not `ButtonBorder`:
                                         that system keyword resolves to pure
                                         black here, so it does NOT track what
                                         a <select> actually draws.
     height         27px                 See .stepper-value's line box.

   border-radius is the exception, and a warning about this method: a select's
   computed border-radius is 0, but macOS Chrome paints its corners rounded
   through the native theme rather than through the CSS property, so the
   computed value describes nothing you can see. Trusting it here made the
   stepper the one hard-cornered box in a row of soft ones. 4px matches what
   the selects actually render, and is eyeballed on purpose -- there is no
   property to read it off. */
.stepper {
  display: flex;
  align-items: stretch;
  border: 1px solid rgb(118, 118, 118);
  border-radius: 4px;
  background: #fff;
  overflow: hidden;
}

.stepper-btn {
  border: none;
  background: transparent;
  padding: 4px 9px;
  font-size: 14px;
  line-height: 1;
  color: #333;
  cursor: pointer;
}

.stepper-btn:hover:not(:disabled) {
  background: #f0f0f0;
}

.stepper-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

/* Tabular figures and a fixed width so stepping 90% -> 100% doesn't shove
   the buttons sideways under the pointer mid-click.

   This element is the tallest child of the stepper, so it alone sets the
   control's height -- and it is now built the way a select is rather than
   padded until it happens to match: the same 4px vertical padding, over a
   content box pinned to the same 17px a select's is.

   Pinning that line box is the whole point. It was previously left at
   `normal` with the padding bumped to 5px to make up the shortfall, which
   quietly tied the control's height to whatever line box Chrome computes for
   13px text -- not a fixed number, and not ours. That arithmetic only reaches
   27px if the line box is 15px; it measures 16px in the currently pinned
   Chromium, which took the stepper to 28px against the selects' 27 and is
   what the parity test caught. Nothing here reads a font metric now, so the
   same drift can't come back. */
.stepper-value {
  min-width: 44px;
  padding: 4px 2px;
  line-height: 17px;
  border-left: 1px solid #d0d0d0;
  border-right: 1px solid #d0d0d0;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  text-align: center;
  color: #333;
}

/* Overrides the column layout #controls label sets above -- a checkbox reads
   left-to-right with its label, not stacked above it like the selects. */
#controls label.checkbox-field {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  /* Selects size to content; a bare checkbox+label pair should too, or it
     stretches to match the tallest column in the flex row. */
  align-self: flex-end;
}

#translate-btn {
  padding: 7px 14px;
  font-size: 13px;
  border: none;
  border-radius: 4px;
  background: #b91c1c;
  color: #fff;
  cursor: pointer;
}

#translate-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

#translate-btn:hover:not(:disabled) {
  background: #9a1717;
}

#status {
  padding: 8px 14px;
  font-size: 12px;
  color: #555;
}

#status.error {
  color: #b91c1c;
}

.hidden {
  display: none;
}

main {
  padding: 14px;
}

/* Sized through --block-scale, never with a font-size of its own:
   .article-title also carries .chinese (see sidepanel.js), so it stays
   annotated (ruby, pinyin, click-to-define) exactly like a paragraph and
   still tracks the reader's text size.

   It sits above the largest in-body heading on purpose. Body headings used
   to be rendered as plain paragraphs, so weight alone was enough to mark the
   title out; now that a section heading is visibly bigger than body text,
   leaving the title at body size would rank the article's own headline below
   its subheadings. */
.title-block {
  --block-scale: 1.4;
}

.article-title {
  font-weight: 600;
}

/* No rule under the title. A rule in this document means "a new section
   starts here" (see .paragraph-pair.heading below), and the title isn't one --
   it's already unmistakable at 1.4x and semibold. Keeping one here would be a
   second, different meaning for the same mark. */
.title-block {
  margin-bottom: 22px;
}

/* Previous/Next paging controls, shown under the last translated paragraph. */
.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 6px;
  padding-top: 14px;
  border-top: 1px solid #e2e2e2;
}

.pager-btn {
  padding: 6px 12px;
  font-size: 12px;
  border: 1px solid #d8d8d8;
  border-radius: 4px;
  background: #fff;
  color: #1a1a1a;
  cursor: pointer;
}

.pager-btn:hover:not(:disabled) {
  background: #f5f5f5;
}

.pager-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.pager-info {
  font-size: 11px;
  color: #777;
  text-align: center;
  flex: 1;
}

.pager-jump {
  font-size: 12px;
  padding: 4px 6px;
  border: 1px solid #d8d8d8;
  border-radius: 4px;
  background: #fff;
  color: #1a1a1a;
}

.pager-jump:disabled {
  opacity: 0.4;
}

/* Paragraphs are separated by space alone, not a rule. Every pair used to end
   in a dashed line, which put seven horizontal rules on a typical screen and
   made the reader look like a form -- and they carried no information, since
   consecutive paragraphs are already parted by a blank line and by the grey
   "Show translation" toggle that terminates each pair. The one job the rule
   did have (making it obvious which paragraph an expanded translation belongs
   to) that toggle was already doing: the English sits directly under it, in
   smaller grey type.
   Slightly more margin than before, since the gap is now doing the whole job
   the rule used to share. */
.paragraph-pair {
  margin-bottom: 20px;
}

/* Reader text size. The +/- stepper (sidepanel.html) sets --text-scale on
   #result as a plain multiplier -- 1 is 100%, the default -- and every reader
   size derives from it. One knob drives the hanzi and its pinyin together, so
   the two can never drift out of proportion the way three independently
   hand-tuned size classes could.

   Leading stays constant across the whole range on purpose. line-height is
   unitless, so it is *already* a multiple of the font size and scales itself;
   the +/-0.1 taper the three fixed steps used to carry was fitted to those
   three points and does not extrapolate -- continued out to 200% it would add
   most of a blank line of dead space per line, in a panel that is short on
   vertical room to begin with. */
#result {
  --text-scale: 1;
  /* A second multiplier, set per block: 1 for body text, more for a heading
     or the article title. Kept separate from --text-scale so the reader's
     size choice and the document's own hierarchy compose instead of
     competing -- a heading is 1.25x whatever size the reader picked, at every
     step of the range.

     The multiplication lives in the font-size declarations below rather than
     in a --zh-size token here, and that is not a style preference. A custom
     property's var() references are substituted on the element that DECLARES
     it, so a --zh-size computed here would bake in THIS element's
     --block-scale of 1; a descendant raising --block-scale could not reach
     back into it. Resolving at the point of use puts the substitution on the
     element that actually inherits the raised value. */
  --block-scale: 1;
  --zh-base: 20px;
  --rt-base: 11px;
  --zh-leading: 2.0;
  --zh-leading-ruby: 2.7;
}

/* Leading is divided by --block-scale, and that division is the whole trick.
   line-height is a multiple of font size, so a block scaled up for hierarchy
   got proportionally more leading too: the 1.4x title came out at 2.7 x 28px
   = 75.6px between wrapped lines, against 54px for body text, and the second
   line of a wrapped title usually has no ruby above it at all -- so all of
   that headroom was empty. Dividing keeps the *absolute* line spacing
   constant down the page: only the characters grow, not the gaps between
   them, which is also the usual typographic rule that larger type wants
   tighter relative leading.

   Only --block-scale is compensated, never --text-scale. They mean different
   things: if the reader makes ALL the text bigger, the spacing should grow
   with it; if one block is bigger because it's a heading, it shouldn't. Body
   text divides by 1 and is provably unchanged.

   The ruby annotation sets its own floor anyway -- measured at ~44.5px for a
   28px title, below which the line box stops shrinking no matter what this
   says -- so the division can't collapse an annotation into the line above. */
.chinese {
  font-size: calc(var(--zh-base) * var(--text-scale) * var(--block-scale));
  line-height: calc(var(--zh-leading) / var(--block-scale));
}

/* Headings ---------------------------------------------------------------

   The source article's structure is worth keeping: a section heading
   rendered as one more paragraph leaves the reader no way to tell where a
   section begins, and a short heading ("Buy a notebook") reads as a stray
   sentence. Size and weight carry that, and both stay proportional to the
   reader's own text size via --block-scale above.

   Six HTML levels collapse to two tiers (see headingTier() in sidepanel.js):
   real articles use two or three, and which numbers they use varies by CMS,
   so per-level sizes would render the same visual rank differently from site
   to site. */
.paragraph-pair.heading .chinese {
  font-weight: 600;
}

.paragraph-pair.heading-major {
  --block-scale: 1.25;
}

.paragraph-pair.heading-minor {
  --block-scale: 1.12;
}

/* The only rule in the document, and it means one thing: a section starts
   here. It sits ABOVE the heading, not below, because a heading belongs to
   the text under it -- hence the tight bottom margin and the generous space
   above, which is what makes it read as opening a section rather than closing
   the previous one.
   An article with no headings therefore has no rules at all, which is the
   right outcome rather than a missing feature: there are no sections to
   mark. */
.paragraph-pair.heading {
  margin-top: 26px;
  margin-bottom: 10px;
  padding-top: 22px;
  border-top: 1px dashed #e2e2e2;
}

/* A heading that opens the article sits directly under the title, where a
   rule would read as the title's own underline rather than as a break between
   two sections -- there is no previous section for it to close. */
.title-block + .paragraph-pair.heading {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

/* Click-to-define ------------------------------------------------------- */

.word {
  cursor: pointer;
  border-radius: 2px;
  /* The pinned banner is sticky, so anything scrolled to the very top ends up
     underneath it and cannot be hovered or clicked. Keep scrolling clear of it. */
  scroll-margin-top: 96px;
}

/* Difficulty marking. --level-color is set per word from the HSK ramp.
   "easy" words (below target) carry no mark at all, so the marks stand out.
   Thickness is the same across every rank and HSK level so the ramp reads as
   one consistent mark; colour and pinyin do the work of distinguishing them. */
.word.at {
  text-decoration: underline;
  text-decoration-color: var(--level-color);
  /* Thicker than usual: the ramp is deliberately bright and pastel, which a
     hairline rule could not carry. */
  text-decoration-thickness: 3px;
  text-underline-offset: 3px;
}

/* above/unknown words are always <ruby> (see sidepanel.js). The underline
   goes on the inner .hanzi span, not the <ruby> element: ruby widens its base
   box to fit the wider pinyin annotation, so a decoration on <ruby> itself
   follows that widened box rather than the glyphs, leaving the underline
   spanning the pinyin's width with blank space on either side of the
   characters. Wrapping the base text in its own (still-inline!) span is
   enough to scope the decoration to it. Do NOT make this inline-block: that
   opts the span out of the browser's ruby column layout entirely, so
   annotations stop tracking their base and drift onto neighbouring words. */
ruby.word.above > .hanzi,
ruby.word.unknown > .hanzi {
  text-decoration: underline;
  text-decoration-color: var(--level-color);
  text-decoration-thickness: 3px;
  text-underline-offset: 3px;
}

.word.unknown > .hanzi {
  text-decoration-style: dotted;
}

.word:hover {
  background: #fdf0d5;
}

/* Hovered word. */
.word.active {
  background: #fdf0d5;
}

/* Pinned word stays marked so the banner's subject is obvious in the text. */
.word.pinned {
  background: #f6d68a;
  box-shadow: 0 0 0 1px #d9a441;
}

/* Sticky reference banner ------------------------------------------------ */

#pinned {
  position: sticky;
  top: 0;
  z-index: 50;
  background: #fffdf5;
  border-bottom: 1px solid #e8dcc0;
  padding: 8px 30px 9px 14px;
}

.pinned-close {
  position: absolute;
  top: 6px;
  right: 8px;
  border: none;
  background: none;
  font-size: 16px;
  line-height: 1;
  color: #999;
  cursor: pointer;
  padding: 2px 4px;
}

.pinned-close:hover {
  color: #333;
}

/* Slightly smaller than the popover's, as it always has been: the banner is
   sticky and sits above the text the reader is scrolling, so it has to earn
   its vertical space. Kept in step with the popover's size, not left behind. */
#pinned .entry-word {
  font-size: 20px;
}

#pinned .entry-gloss {
  margin-top: 3px;
}

/* Pinyin above the word, shown only for above-target and unknown words. */
ruby.word {
  ruby-align: center;
  /* Keep an annotated word from breaking across lines mid-word. */
  white-space: nowrap;
  /* Chinese words sit flush against each other with no space, which is
     correct for the hanzi -- but two adjacent multi-syllable pinyin
     annotations then also sit flush, and read as one merged run (e.g.
     "zhàng fu" + "guǎng fàn" -> "zhàng fuguǎng fàn"). A small gap after each
     annotated word breaks that up. em-based so it scales with text size. */
  margin-right: 0.15em;
}

ruby.word > rt {
  /* Pinyin is much wider than the hanzi it sits over, and ruby widens the base
     to fit — which pulls characters apart and makes the line ragged. A small,
     slightly condensed rt keeps that stretching to a minimum. */
  font-size: calc(var(--rt-base) * var(--text-scale) * var(--block-scale));
  letter-spacing: -0.2px;
  /* Darker ink than the underline — the pastel would be illegible here. */
  color: var(--level-ink, var(--level-color));
  font-weight: 600;
  line-height: 1.1;
  user-select: none;
}

/* Ruby needs headroom above the line; the class is set on #result only when
   something is actually annotated, so unmarked text keeps its normal rhythm. */
#result.has-ruby .chinese {
  line-height: calc(var(--zh-leading-ruby) / var(--block-scale));
}

/* "Show pinyin" unchecked. Underlines and colour still mark difficulty --
   only the reading itself hides -- so the reader can test themselves before
   revealing it. Collapse the ruby headroom back to normal leading too, or
   every line keeps the gap the (now invisible) pinyin used to need. */
#result.hide-pinyin rt {
  display: none;
}

#result.hide-pinyin.has-ruby .chinese {
  line-height: calc(var(--zh-leading) / var(--block-scale));
}

.popover {
  position: fixed;
  z-index: 100;
  /* Interactive, not click-through: it holds a real button (save/unsave --
     see .entry-save) that the reader needs to be able to hover onto and
     click without the popover vanishing out from under the pointer first.
     sidepanel.js keeps it open on popover:mouseenter and only schedules a
     hide on popover:mouseleave, the same hover-a-menu pattern any
     interactive tooltip needs -- see the mouseout/mouseenter/mouseleave
     listeners there. */
  max-width: 300px;
  background: #fff;
  border: 1px solid #d8d8d8;
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.16);
  padding: 10px 12px;
}

.popover.hidden {
  display: none;
}

/* Optimistic-fetch's "next page is ready" notice (see maybePrefetchNext in
   sidepanel.js) -- brief and out of the way, at the bottom of the panel,
   since it's informational only and nothing needs to be done with it. */
.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 200;
  background: #1a1a1a;
  color: #fff;
  font-size: 12px;
  padding: 8px 14px;
  border-radius: 20px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  transition: opacity 0.2s ease;
}

.toast.hidden {
  display: none;
}

.entry-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.entry-word {
  font-size: 22px;
  font-weight: 600;
}

.entry-level {
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  background: #b91c1c;
  border-radius: 10px;
  padding: 2px 8px;
  white-space: nowrap;
}

.entry-level.off-list {
  color: #777;
  background: #ededed;
  font-weight: 500;
}

.entry-save {
  font-size: 11px;
  font-weight: 600;
  color: #b91c1c;
  background: #fff;
  border: 1px solid #b91c1c;
  border-radius: 10px;
  padding: 2px 8px;
  white-space: nowrap;
  cursor: pointer;
}

.entry-save:hover {
  background: #fdf0e5;
}

.entry-save.saved {
  color: #fff;
  background: #b91c1c;
}

.entry-pinyin {
  font-size: 15px;
  color: #b91c1c;
  margin-top: 3px;
}

.entry-gloss {
  font-size: 15px;
  color: #333;
  line-height: 1.5;
  margin-top: 6px;
}

/* Multiple distinct senses (e.g. 现代: "modern" vs "Hyundai, South Korean
   company") render as this instead of the plain <div> above -- browser-
   numbered, like a paper dictionary's definition list. */
ol.entry-gloss {
  margin: 6px 0 0;
  padding-left: 18px;
}

.entry-gloss li {
  margin-bottom: 2px;
}

.entry-gloss li:last-child {
  margin-bottom: 0;
}

.english-toggle {
  font-size: 11px;
  color: #999;
  cursor: pointer;
  margin-top: 4px;
  user-select: none;
}

.english {
  font-size: 12px;
  color: #777;
  margin-top: 4px;
  display: none;
}

/* Shared by the title block and every paragraph pair -- both carry
   .gloss-block (see sidepanel.js's appendGlossToggle). */
.gloss-block.show-english .english {
  display: block;
}

/* Hovering a sentence on either side of a gloss-block highlights its
   counterpart on the other (see the sentence-pair listeners in
   sidepanel.js). Same tone as the click-to-define word hover, so it reads
   as the same "you're looking at this" language. */
.sentence-highlight {
  background: #fdf0d5;
  border-radius: 2px;
}

/* First-run state: no API key saved, so there is nothing to translate with.
   Deliberately occupies the space a translation would, rather than being a
   one-line error under the button -- with the button disabled, this is the
   only thing to act on, so it should read as the content of the panel. */
#setup {
  margin: 4px 12px 12px;
  padding: 14px 16px;
  border: 1px solid #e2e2e2;
  border-radius: 6px;
  background: #fbfbfb;
}

#setup h2 {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 600;
}

#setup .setup-intro {
  margin: 0 0 12px;
  font-size: 12px;
  line-height: 1.5;
  color: #555;
}

#setup ol {
  margin: 0;
  padding-left: 20px;
  font-size: 12px;
  line-height: 1.65;
  color: #333;
}

#setup li { margin-bottom: 5px; }

#setup .setup-free {
  display: inline-block;
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 3px;
  background: #15803d;
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

#setup .setup-open {
  margin-top: 12px;
  padding: 6px 14px;
  font-size: 12px;
  border: 1px solid #b91c1c;
  border-radius: 4px;
  background: #b91c1c;
  color: #fff;
  cursor: pointer;
}

#setup .setup-open:hover { background: #9a1717; }

#setup .setup-note {
  margin: 10px 0 0;
  font-size: 11px;
  line-height: 1.5;
  color: #777;
}
