/* Tokens: values from SPEC.md "Visual system". */
:root {
  --bg: #eef6f0;
  --card: #fffafd;
  --card-border: #f6d3e3;
  --ink: #1f2a22;
  --body: #3a3f3b;
  --muted: #5a675e;
  --accent: #3d7a48;
  --pink-divider: #f9d9e8;
  --pink-border: #f6d3e3;
  --pink-edge: #f2b8d2;
  --pink-soft: #fbe3ef;
  --card-shadow: #f7cfe1;
  /* Shared by cards and the photo frame. */
  --frame-shadow: 7px 7px 0 var(--card-shadow);
  /* Recycled-paper flecks: inline SVG noise, two layers with co-prime tile sizes so no repeat shows. */
  --paper: url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27211%27%20height%3D%27211%27%3E%3Cfilter%20id%3D%27f%27%20x%3D%270%27%20y%3D%270%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%27.75%27%20numOctaves%3D%272%27%20seed%3D%273%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%200.184%20%200%200%200%200%200.420%20%200%200%200%200%200.231%20%2040%200%200%200%20-33.40%27/%3E%3CfeComponentTransfer%3E%3CfeFuncA%20type%3D%27linear%27%20slope%3D%270.5%27/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23f%29%27/%3E%3C/svg%3E"), url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27317%27%20height%3D%27317%27%3E%3Cfilter%20id%3D%27f%27%20x%3D%270%27%20y%3D%270%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%27.26%27%20numOctaves%3D%272%27%20seed%3D%2711%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%200.816%20%200%200%200%200%200.404%20%200%200%200%200%200.604%20%2040%200%200%200%20-32.20%27/%3E%3CfeComponentTransfer%3E%3CfeFuncA%20type%3D%27linear%27%20slope%3D%270.75%27/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23f%29%27/%3E%3C/svg%3E");

  --serif: Newsreader, Georgia, "Times New Roman", serif;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --cjk: "Long Cang", "Kaiti SC", "STKaiti", "KaiTi", serif;
  color-scheme: light dark;
}

/* Dark tokens apply from the system setting unless the visitor picked light with the switch,
   or from an explicit dark pick. Keep the two lists identical (tests/check_site.py checks). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16201a;
    --card: #1e2a23;
    --card-border: #33463a;
    --ink: #eef6f0;
    --body: #c9d6cc;
    --muted: #87968c;
    --accent: #8fd19e;
    --pink-divider: #5a3a4a;
    --pink-border: #33463a;
    --pink-edge: #d98fb0;
    --pink-soft: #4a2e3c;
    --card-shadow: #8a5a70;
    /* Softer than light mode: pale specks on dark green read much louder. */
    --paper: url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27211%27%20height%3D%27211%27%3E%3Cfilter%20id%3D%27f%27%20x%3D%270%27%20y%3D%270%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%27.95%27%20numOctaves%3D%272%27%20seed%3D%273%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%201.000%20%200%200%200%200%201.000%20%200%200%200%200%201.000%20%2040%200%200%200%20-33.40%27/%3E%3CfeComponentTransfer%3E%3CfeFuncA%20type%3D%27linear%27%20slope%3D%270.08%27/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23f%29%27/%3E%3C/svg%3E"), url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27317%27%20height%3D%27317%27%3E%3Cfilter%20id%3D%27f%27%20x%3D%270%27%20y%3D%270%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%27.26%27%20numOctaves%3D%272%27%20seed%3D%2711%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%200.851%20%200%200%200%200%200.561%20%200%200%200%200%200.690%20%2040%200%200%200%20-32.20%27/%3E%3CfeComponentTransfer%3E%3CfeFuncA%20type%3D%27linear%27%20slope%3D%270.3%27/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23f%29%27/%3E%3C/svg%3E");
  }
}
:root[data-theme="dark"] {
  --bg: #16201a;
  --card: #1e2a23;
  --card-border: #33463a;
  --ink: #eef6f0;
  --body: #c9d6cc;
  --muted: #87968c;
  --accent: #8fd19e;
  --pink-divider: #5a3a4a;
  --pink-border: #33463a;
  --pink-edge: #d98fb0;
  --pink-soft: #4a2e3c;
  --card-shadow: #8a5a70;
  /* Softer than light mode: pale specks on dark green read much louder. */
  --paper: url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27211%27%20height%3D%27211%27%3E%3Cfilter%20id%3D%27f%27%20x%3D%270%27%20y%3D%270%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%27.95%27%20numOctaves%3D%272%27%20seed%3D%273%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%201.000%20%200%200%200%200%201.000%20%200%200%200%200%201.000%20%2040%200%200%200%20-33.40%27/%3E%3CfeComponentTransfer%3E%3CfeFuncA%20type%3D%27linear%27%20slope%3D%270.08%27/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23f%29%27/%3E%3C/svg%3E"), url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27317%27%20height%3D%27317%27%3E%3Cfilter%20id%3D%27f%27%20x%3D%270%27%20y%3D%270%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%27.26%27%20numOctaves%3D%272%27%20seed%3D%2711%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%200.851%20%200%200%200%200%200.561%20%200%200%200%200%200.690%20%2040%200%200%200%20-32.20%27/%3E%3CfeComponentTransfer%3E%3CfeFuncA%20type%3D%27linear%27%20slope%3D%270.3%27/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23f%29%27/%3E%3C/svg%3E");
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }

/* Base */
*, *::before, *::after { box-sizing: border-box; }
html {
  scroll-behavior: smooth;
  /* Phones in landscape otherwise enlarge text on their own. */
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
  -webkit-tap-highlight-color: color-mix(in srgb, var(--pink-edge) 35%, transparent);
}
body {
  margin: 0;
  background-color: var(--bg); background-image: var(--paper); background-attachment: fixed;
  color: var(--body);
  font-family: var(--sans);
  /* Plex's lowercase height; a fallback font is scaled to match so nothing jumps while fonts load. */
  font-size-adjust: 0.516;
  font-size: 1.0625rem;
  line-height: 1.65;
  overflow-wrap: anywhere;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p, ul { margin: 0; }
a { color: var(--accent); }

.skip {
  position: absolute; left: 1rem; top: -3rem;
  background: var(--card); color: var(--ink); padding: .5rem .75rem; border-radius: 6px; z-index: 10;
}
.skip:focus { top: 1rem; }

/* Layout: sticky sidebar on desktop, top header on phones */
.layout { min-height: 100vh; }
.sidebar {
  padding: 1.5rem 1rem;
  border-bottom: 6px solid var(--pink-divider);
}
main { padding: 0 1rem 4rem; }
section { padding-top: 3rem; scroll-margin-top: 1rem; max-width: 70ch; }
/* Lets the short last section scroll up into the active-nav band in js/site.js. */
section:last-child { min-height: 75vh; }

/* Phones, including phones turned sideways (too short for the sidebar): identity, then Menu
   and icons on one row; the open menu drops below them. The phone and sidebar queries in this file
   are complementary; the .98 keeps fractional viewport sizes (zoom, odd pixel ratios) from matching neither:
   (max-width: 699.98px), (max-height: 499.98px) vs. (min-width: 700px) and (min-height: 500px). */
@media (max-width: 699.98px), (max-height: 499.98px) {
  .sidebar { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; padding: .9rem 1rem; }
  .identity { flex-basis: 100%; display: flex; flex-wrap: wrap; align-items: baseline; column-gap: .6rem; }
  .identity .focus { flex-basis: 100%; margin-top: .35rem; }
  .sidebar .nav-toggle { margin: 0; }
  .sidebar .social { margin: 0 0 0 auto; }
  .site-nav { order: 1; flex-basis: 100%; }
  .site-nav ul { margin-top: 0; }
  /* Sticky Menu bar: the header sticks with the rows above Menu pulled off the top of the screen.
     js/site.js measures --bar-hide (those rows) and --bar-h (what stays visible). */
  .js .sidebar {
    position: sticky; top: calc(-1 * var(--bar-hide, 0px)); z-index: 3;
    background-color: var(--bg); background-image: var(--paper); background-attachment: fixed;
  }
  .js .label { top: var(--bar-h, 0px); }
  /* Keeps link targets and keyboard focus from scrolling under the bar. */
  .js:root { scroll-padding-top: var(--bar-h, 0px); }
}
/* Tablets: a narrower sidebar beside the content. */
@media (min-width: 700px) and (min-height: 500px) {
  .layout { display: grid; grid-template-columns: 240px minmax(0, 1fr); }
  .sidebar {
    /* dvh tracks mobile browser toolbars as they show and hide; vh is the fallback. */
    position: sticky; top: 0; height: 100vh; height: 100dvh; overflow-y: auto;
    padding: 2.5rem 1.5rem;
    border-bottom: 0; border-right: 6px solid var(--pink-divider);
    display: flex; flex-direction: column; gap: 2rem;
  }
  main { padding: 1rem 2.5rem 6rem; }
  section { padding-top: 3.5rem; }
  /* Puts the ABOUT capitals level with the name capitals in the sidebar (tests/browser_check.py measures it). */
  #about { padding-top: .42rem; }
  .nav-toggle { display: none; }
}
/* Wide screens: sidebar and reading column keep their widths, and the extra
   width becomes whitespace split 1:2:6 (left margin, gap, right margin). */
@media (min-width: 1100px) and (min-height: 500px) {
  .layout { grid-template-columns: minmax(0, 1fr) 300px minmax(3.5rem, 2fr) minmax(0, 70ch) minmax(3.5rem, 6fr); }
  .sidebar { grid-column: 2; padding: 3.5rem 2rem; }
  main { grid-column: 4; padding: 1rem 0 6rem; }
  #about { padding-top: 1.42rem; }
}

/* Type scale: SPEC.md "Type" table */
.name {
  font-family: var(--serif); font-weight: 500; font-size: 2rem; line-height: 1.05;
  color: var(--ink); font-size-adjust: none;
}
.name-zh { font-family: var(--cjk); font-size: 1.4rem; line-height: 1.3; color: var(--muted); margin-top: .25rem; font-size-adjust: none; }
/* Focus areas stand in for a job title: one green line separated by middots. */
.focus { list-style: none; padding: 0; margin: .9rem 0 0; font-size: .95rem; font-weight: 600; line-height: 1.4; color: var(--accent); }
.focus li { display: inline; white-space: nowrap; }
.focus li:not(:last-child)::after { content: " ·"; }
/* Pinned while its section is on screen, so the reader always sees where they are.
   Same fixed paper as the page, so the pinned band blends in. */
.label {
  background-color: var(--bg); background-image: var(--paper); background-attachment: fixed;
  font-size: 1.0625rem; font-weight: 600; line-height: 1.2; letter-spacing: .12em; text-transform: uppercase;
  color: var(--accent); margin-bottom: .5rem;
  position: sticky; top: 0; z-index: 2; padding: .9rem 0 .75rem;
}
.lead {
  font-family: var(--serif); font-weight: 500; font-size: 1.25rem; line-height: 1.35;
  color: var(--ink); font-size-adjust: none; margin-bottom: 1.25rem;
}
.meta { font-size: .875rem; line-height: 1.4; color: var(--muted); }
.card-title { font-size: 1.0625rem; font-weight: 600; line-height: 1.4; color: var(--ink); }
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  font: 500 .9375rem/1 var(--sans);
  padding: .7rem 1rem; min-height: 44px; border-radius: 8px;
  background: var(--accent); color: var(--bg); text-decoration: none; border: 0; cursor: pointer;
}
.btn[disabled], .btn[aria-disabled="true"] {
  background: transparent; color: var(--muted); border: 1.5px dashed var(--pink-edge); cursor: default;
}

@media (min-width: 700px) and (min-height: 500px) {
  .name-zh { font-size: 1.5rem; }
  .lead { font-size: 1.5rem; }
}
@media (min-width: 1100px) and (min-height: 500px) {
  .name { font-size: 2.5rem; }
  .name-zh { font-size: 1.75rem; }
}
@media (max-width: 699.98px), (max-height: 499.98px) {
  body { font-size: 1rem; }
}

/* Sidebar nav */
.site-nav ul { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: .25rem .5rem; margin-top: 1rem; }
.site-nav a {
  display: block; padding: .3rem .6rem; border-radius: 6px;
  font-size: .95rem; font-weight: 500; line-height: 1.5; color: var(--muted); text-decoration: none;
}
.site-nav a[aria-current="true"] { color: var(--ink); background: var(--pink-soft); }
/* Hover styles only for mouse and trackpad: phones keep :hover on after a tap until the next one. */
@media (hover: hover) { .site-nav a:hover { color: var(--ink); background: var(--pink-soft); } }
@media (min-width: 700px) and (min-height: 500px) {
  .site-nav ul { flex-direction: column; margin-top: 0; }
  .site-nav a { border-left: 3px solid transparent; border-radius: 0 6px 6px 0; }
  .site-nav a[aria-current="true"] { border-left-color: var(--accent); }
}
@media (max-width: 699.98px), (max-height: 499.98px) {
  .site-nav a { display: flex; align-items: center; min-height: 44px; }
}
.nav-toggle {
  display: none; min-height: 44px;
  margin-top: 1rem; font: 500 .9375rem/1 var(--sans); color: var(--ink);
  background: var(--card); border: 1px solid var(--card-border); border-radius: 8px; padding: .6rem .9rem; cursor: pointer;
}

/* About intro: photo above the positioning line on phones, beside it on desktop */
.intro { display: flex; flex-direction: column; gap: 1.25rem; margin-bottom: 1.25rem; }
.intro .lead { margin-bottom: 0; }
.photo {
  width: 128px; height: 128px; object-fit: cover; border-radius: 0;
  border: 1.5px solid var(--ink); box-shadow: var(--frame-shadow);
}
@media (min-width: 600px) {
  .intro { flex-direction: row; align-items: center; gap: 1.75rem; }
  .photo { width: 150px; height: 150px; flex-shrink: 0; }
}

/* Prose sections */
.prose p + p { margin-top: 1rem; }
.lead-small { font-size: 1.0625rem; font-weight: 500; color: var(--ink); }
.collaborators { font-size: .95rem; color: var(--muted); }
.email { font-size: 1.125rem; font-weight: 600; color: var(--ink); }
.card .card-title + .meta { margin-top: .1rem; }
.card .meta + p { margin-top: .5rem; }
.card .card-title + p { margin-top: .5rem; }

/* Projects and resume */
.subhead { font-size: 1.0625rem; font-weight: 600; color: var(--ink); margin: 1.45rem 0 .2rem; }
/* Publications and academic detail fold away by default; the native disclosure works without JS and from the keyboard.
   The vertical padding makes a 44px tap target; .subhead margins are trimmed by the same amount. */
.fold > summary { display: flex; align-items: baseline; gap: .5rem; padding: .55rem 0; cursor: pointer; list-style: none; width: fit-content; }
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary::before {
  content: "+"; width: 1em; flex: none; text-align: center;
  font-size: 1.35em; font-weight: 400; line-height: 1; color: var(--accent);
  transition: transform .2s ease;
}
/* The plus turns a quarter-diagonal into an x when open. */
.fold[open] > summary::before { transform: rotate(45deg); }
/* Stacked dropdowns (end of Academics) read as one list. */
.fold + .fold > summary { margin-top: 0; }
@media (hover: hover) { .fold > summary:hover { color: var(--accent); } }
.fold-body h3 { font-size: 1rem; font-weight: 600; color: var(--ink); margin: 1.5rem 0 .5rem; }
.fold-body h3:first-child { margin-top: 0; }
/* Same dotted divider as between publications, here between research themes. */
.fold-body h3:not(:first-child) { border-top: 1.5px dotted var(--pink-edge); padding-top: 1.25rem; margin-top: 1.25rem; }
.pubs { padding-left: 1.25rem; margin: 0; }
.pub { padding: .8rem 0; }
.pub + .pub { border-top: 1.5px dotted var(--pink-edge); }
.pub-links { display: inline-flex; gap: 1rem; margin-left: .25rem; font-weight: 600; }
/* Small controls get an invisible 44px tap area; how big they look stays the same. */
.theme-toggle, .social a, .pub-links a { position: relative; }
.theme-toggle::after, .social a::after, .pub-links a::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: max(100%, 44px); height: max(100%, 44px); transform: translate(-50%, -50%);
}
.card-action { margin-top: 1rem; }

/* Cards */
/* Ink outline with a solid offset shadow: printed-zine feel instead of a rounded pastel panel. */
.card {
  background: var(--card); border: 1.5px solid var(--ink); border-radius: 0;
  box-shadow: var(--frame-shadow);
  padding: 1.1rem 1.25rem; margin: 0 7px 1.5rem 0;
}

/* Phone menu: collapsed only when JS is running, so it stays usable without JS */
@media (max-width: 699.98px), (max-height: 499.98px) {
  .js .nav-toggle { display: inline-block; }
  .js #site-nav:not(.open) { display: none; }
}

/* Text links: pink highlight sweeps across */
.prose a:not(.btn) {
  color: var(--accent); font-weight: 500;
  text-decoration: underline; text-decoration-color: var(--pink-edge); text-underline-offset: 3px;
  background: linear-gradient(var(--pink-soft), var(--pink-soft)) left / 0% 100% no-repeat;
  border-radius: 3px; padding: 0 .1em; margin: 0 -.1em;
  transition: background-size .35s ease;
}
.prose a:not(.btn):focus-visible { background-size: 100% 100%; }
@media (hover: hover) { .prose a:not(.btn):hover { background-size: 100% 100%; } }

/* Social icons: playful tilt and grow */
.social { display: flex; align-items: center; gap: .75rem; margin-top: 1rem; }
/* Sidebar: the theme switch drops to a second row, under the first icon. */
@media (min-width: 700px) and (min-height: 500px) { .social { display: grid; grid-template-columns: repeat(3, 40px); row-gap: 1rem; margin-top: 0; } }
/* Phones: keep the header short by putting the switch in the icon row instead. */
@media (max-width: 699.98px), (max-height: 499.98px) { .theme-toggle { order: -1; } }
.social a {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  color: var(--accent); background: var(--card); border: 1.5px solid var(--pink-edge);
  transition: transform .25s ease, background-color .25s ease;
}
.social a:focus-visible { transform: rotate(-10deg) scale(1.12); background: var(--pink-soft); }
@media (hover: hover) { .social a:hover { transform: rotate(-10deg) scale(1.12); background: var(--pink-soft); } }

/* Theme switch: the knob slides under the icon of the active theme. Hidden until js/site.js wires it up. */
.theme-toggle {
  position: relative; width: 40px; height: 22px; padding: 0 3.5px; border-radius: 11px;
  display: inline-flex; align-items: center; justify-content: space-between;
  color: var(--muted); background: var(--card); border: 1.5px solid var(--pink-edge); cursor: pointer;
}
.theme-toggle[hidden] { display: none; }
.theme-toggle svg { position: relative; z-index: 1; }
.theme-toggle .knob {
  position: absolute; top: 2px; left: 2px; width: 15px; height: 15px; border-radius: 50%;
  background: var(--accent); transition: transform .2s ease;
}
.theme-toggle[aria-checked="true"] .knob { transform: translateX(18px); }
.theme-toggle[aria-checked="false"] .sun, .theme-toggle[aria-checked="true"] .moon { color: var(--card); }
@media (hover: hover) { .theme-toggle:hover { background: var(--pink-soft); } }

/* Experience cards tilt toward the cursor (js/site.js sets the transform) */
/* The transparent outline and hidden backface make Chrome anti-alias the ink border while tilted. */
#experience .card, #projects .card {
  transition: transform .2s ease-out; will-change: transform;
  outline: 1px solid transparent; backface-visibility: hidden;
}

/* Keyboard focus */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 4px; }
@media (hover: hover) { .btn:hover:not([disabled]) { filter: brightness(1.08); } }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .card, .social a, .prose a { transition: none; transform: none !important; }
  .theme-toggle .knob { transition: none; }
  .fold > summary::before { transition: none; }
}
