/*
  Aakash Kattelu — shared styles.
  Layout: an outline rail on the left, one reading column, and a right
  margin for side notes (or a post's own figures). Below 960px everything
  collapses to a single column.
*/

/* ---------- Color scheme ---------- */

:root {
  /* Follows the system unless the <meta name="color-scheme"> was pinned by the toggle. */
  color-scheme: light dark;

  &:has(> head > meta[name="color-scheme"][content="dark"]) { color-scheme: dark; }
  &:has(> head > meta[name="color-scheme"][content="light"]) { color-scheme: light; }

  --bg: light-dark(#fffff8, #161614);
  --ink: light-dark(#111, #e4e2d8);
  --muted: light-dark(#555, #aaa89e);
  --faded: light-dark(#6b6b6b, #8f8d84);
  --rule: light-dark(#ccc, #3d3c37);
  --accent: light-dark(#8b1a1a, #e8998f);
  --panel: light-dark(#f1f0e6, #201f1c);

  /* Non-color values that change with the scheme (see the dark blocks below). */
  --eq-filter: none;

  --serif: Newsreader, "Iowan Old Style", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, Menlo, monospace;

  --bar: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --stick: calc(var(--safe-top) + var(--bar) + 20px);
  --gutter: 32px;
  --rail: 150px;
  --rail-gap: 50px;
  --measure: 580px;

  accent-color: var(--accent);
}

/* Dark scheme, for values light-dark() can't express. Two selectors: system dark (not pinned light), or pinned dark. */
@media (prefers-color-scheme: dark) {
  :root:not(:has(> head > meta[name="color-scheme"][content="light"])) {
    --eq-filter: invert(.88);
    & .when-light { display: none; }
    & .when-dark { display: revert; }
  }
}
:root:has(> head > meta[name="color-scheme"][content="dark"]) {
  --eq-filter: invert(.88);
  & .when-light { display: none; }
  & .when-dark { display: revert; }
}
.when-dark { display: none; }

/* ---------- Base ---------- */

html {
  scroll-padding-top: calc(var(--safe-top) + var(--bar) + 16px);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
  @view-transition { navigation: auto; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1.1875rem;
  line-height: 1.52;
  font-size-adjust: from-font;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Paint the notch / status-bar strip on phones. */
body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  height: var(--safe-top);
  z-index: 30;
  background: var(--bg);
}

img, video { max-width: 100%; }
a { color: var(--ink); text-decoration-thickness: 1px; text-underline-offset: 3px; }
a:hover { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
section[tabindex="-1"]:focus { outline: none; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.vh {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.skip {
  position: absolute;
  left: 12px;
  top: calc(var(--safe-top) + 8px);
  z-index: 40;
  padding: 8px 14px;
  background: var(--ink);
  color: var(--bg);
  font-size: 1rem;
  transform: translateY(-200%);
}
.skip:focus { transform: none; color: var(--bg); }

.shell {
  padding-left: max(var(--gutter), env(safe-area-inset-left, 0px));
  padding-right: max(var(--gutter), env(safe-area-inset-right, 0px));
}
.wrap { max-width: 1200px; margin: 0 auto; }

/* ---------- Header ---------- */

.top { padding-block: 28px 0; view-transition-name: site-header; }
.topin {
  display: flex;
  align-items: center;
  gap: var(--rail-gap);
  border-bottom: 1px solid var(--rule);
  padding-bottom: 16px;
  margin-bottom: 44px;
}
a.brand {
  width: var(--rail);
  flex: none;
  font-size: 1.25rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.navlinks { display: flex; gap: 28px; font-size: 1.0625rem; }
.navlinks a { text-decoration: none; color: var(--muted); }
.navlinks a:hover { color: var(--ink); }
.navlinks a[aria-current="page"] {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 5px;
}
.tools { display: flex; gap: 8px; margin-left: auto; }

.btn {
  font-size: 1rem;
  min-height: 44px;
  min-width: 64px;
  padding: 0 14px;
  background: transparent;
  border: 1px solid var(--rule);
  cursor: pointer;
}
.btn:hover { border-color: var(--ink); }
.btn[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* Compact bar that slides in once the header has scrolled away (posts only). */
.bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 20;
  padding-top: var(--safe-top);
  background: var(--bg);
  border-bottom: 1px solid var(--rule);
  transform: translateY(-105%);
  transition: transform .22s ease;
}
.bar.show { transform: none; }
.barin { height: var(--bar); display: flex; align-items: center; gap: var(--rail-gap); }
.bar a.brand { font-size: 1.0625rem; }
.where { display: flex; align-items: center; gap: 10px; min-width: 0; font-size: 1rem; color: var(--muted); }
.where .bt { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 22em; }
.where a.bw {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}
.where a.bw:hover { color: var(--ink); text-decoration: underline; }
[data-current-section] { overflow: hidden; text-overflow: ellipsis; max-width: 14em; }
.bsec {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 6px;
  margin: 0 -6px;
  background: none;
  border: 0;
  cursor: pointer;
  color: var(--ink);
  font-style: italic;
  white-space: nowrap;
}
.bsec svg { width: 12px; height: 12px; }
.icons { display: flex; gap: 4px; margin-left: auto; }
.ib {
  width: 40px; height: 40px;
  display: grid;
  place-items: center;
  background: none;
  border: 1px solid transparent;
  cursor: pointer;
  color: var(--muted);
}
.ib:hover { color: var(--ink); border-color: var(--rule); }
.ib[aria-pressed="true"] { color: var(--bg); background: var(--ink); }
.ib svg { width: 20px; height: 20px; }

/* Reading progress, driven by the scroll position where supported (decorative, no fallback). */
.prog { display: none; }
@supports ((animation-timeline: scroll()) and (animation-range: 0% 100%)) {
  .prog {
    display: block;
    position: absolute;
    left: 0; bottom: -1px;
    width: 100%; height: 2px;
    background: var(--accent);
    transform-origin: 0 50%;
    animation: read-progress linear both;
    animation-timeline: scroll(root block);
  }
}
@keyframes read-progress { from { transform: scaleX(0); } to { transform: scaleX(1); } }

/* Section menu opened from the bar. */
.sheet {
  position: fixed;
  inset: auto;
  top: calc(var(--safe-top) + var(--bar));
  left: max(16px, calc((100vw - 1200px) / 2 + var(--rail) + var(--rail-gap)));
  margin: 0;
  min-width: 240px;
  padding: 8px 18px 12px 0;
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--rule);
  border-top: 0;
  box-shadow: 0 10px 24px -18px rgb(0 0 0 / .5);
}
@supports not selector(:popover-open) {
  [popover] { display: none; }
}

/* ---------- Outline ---------- */

.toc {
  scroll-target-group: auto;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--rule);
  font-size: 1.0625rem;
  line-height: 1.3;
}
.toc a { text-decoration: none; padding: 8px 0 8px 14px; color: var(--muted); }
.toc a:hover { color: var(--ink); }
.toc a:is(:target-current, .target-current) {
  color: var(--ink);
  font-weight: 650;
  box-shadow: inset 2px 0 0 var(--ink);
}

/* ---------- Post ---------- */

.layout { display: flex; gap: var(--rail-gap); align-items: stretch; }
.left { display: none; width: var(--rail); flex: none; }
.left .toc { position: sticky; top: var(--stick); }

.main { flex: 1 1 auto; min-width: 0; max-width: 600px; margin: 0 auto; padding-bottom: 72px; }
.main p { margin: 0 0 16px; }
.main h1 {
  font-size: 3.125rem;
  font-weight: 400;
  line-height: 1.1;
  margin: 0 0 8px;
  text-wrap: balance;
  letter-spacing: -.005em;
}
.main h2 {
  font-size: 1.75rem;
  font-weight: 400;
  font-style: italic;
  line-height: 1.25;
  margin: 40px 0 12px;
  text-wrap: balance;
}
.meta { color: var(--muted); font-size: 1rem; margin-bottom: 24px; }
.main code { font-family: var(--mono); font-size: 1rem; }

/* Numbered lists hang their numbers in the margin, in old-style figures. */
.main ol { list-style: none; margin: 0 0 18px; padding: 0 0 0 30px; counter-reset: n; }
.main ol > li { counter-increment: n; position: relative; margin-bottom: 10px; }
.main ol > li::before {
  content: counter(n);
  position: absolute;
  left: -30px;
  width: 20px;
  text-align: right;
  color: var(--faded);
  font-variant-numeric: oldstyle-nums tabular-nums;
  transition: color .15s;
}
.main ul { list-style: none; margin: 6px 0 0; padding: 0 0 0 20px; }
.main ul > li { position: relative; margin-bottom: 6px; }
.main ul > li::before { content: "\2013"; position: absolute; left: -20px; color: var(--faded); }

/* Skim: fade the running text, keep key sentences and names. */
.k, .name { transition: font-weight .15s, color .15s; }
.skim .skimmable :is(p, li) { color: var(--faded); font-weight: 260; }
.skim .skimmable .k { color: var(--ink); font-weight: 700; }
.skim .skimmable .name { color: var(--ink); font-weight: 650; }

.mtoc { border-block: 1px solid var(--rule); margin: 0 0 28px; font-size: 1.0625rem; }
.mtoc summary { min-height: 44px; line-height: 44px; cursor: pointer; }
.mtoc .toc { margin-bottom: 12px; }

/* Notes: inline boxes on small screens, side notes in the margin on wide ones. */
.note {
  display: block;
  border-block: 1px solid var(--rule);
  padding: 10px 0 10px 14px;
  margin: 8px 0 14px;
  font-size: 1rem;
  line-height: 1.4;
  font-weight: 400;
  color: var(--muted);
}
.js .note.collapsible:not(.open) { display: none; }
.ref {
  display: inline-block;
  font-size: .9375rem;
  min-width: 44px;
  min-height: 44px;
  margin: -14px -10px;
  background: transparent;
  border: 0;
  color: var(--accent);
  vertical-align: super;
  cursor: pointer;
  line-height: 1;
}

.foot {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  border-top: 1px solid var(--rule);
  margin-top: 48px;
  padding-top: 18px;
  font-size: 1.0625rem;
}
.foot .lbl {
  display: block;
  font-family: var(--mono);
  font-size: .75rem;
  letter-spacing: .04em;
  color: var(--faded);
  margin-bottom: 4px;
}
.foot .when { color: var(--muted); }

/* Equations rendered as SVG; inverted in the dark scheme. */
.eq { overflow-x: auto; text-align: center; margin: 4px 0 18px; }
.eq img { display: inline-block; vertical-align: middle; max-width: none; height: auto; filter: var(--eq-filter); }

.arrows {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--rule);
  padding-top: 12px;
  margin-top: 18px;
  line-height: 44px;
}
.arrows a, .arrows span { min-width: 44px; }
.arrows a:last-child { text-align: right; }
.arrows .off { color: var(--rule); }

/* ---------- Home and Writing ---------- */

.page .wrap { display: grid; grid-template-columns: minmax(0, 1fr); }
.intro { font-size: 1.6875rem; font-weight: 300; line-height: 1.4; margin: 0 0 40px; max-width: 640px; }
.intro strong { font-weight: 600; }

.about { padding-bottom: 72px; max-width: 880px; }
.about::after { content: ""; display: block; clear: both; }
.bio { min-width: 0; max-width: 640px; }
.face {
  float: right;
  width: 120px;
  height: 150px;
  margin: 6px 0 12px 20px;
  object-fit: cover;
  filter: grayscale(.15);
}
.about .intro { font-size: 1.5rem; margin-bottom: 18px; }
.past { margin: 0 0 22px; color: var(--muted); line-height: 1.55; }
.elsewhere { display: flex; flex-wrap: wrap; column-gap: 24px; font-size: 1.0625rem; margin: 0; }
.elsewhere a { display: inline-flex; align-items: center; min-height: 44px; }

.posts { list-style: none; margin: 0; padding: 0 0 72px; }
.entry { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; border-top: 1px solid var(--rule); padding: 24px 0; }
.posts .entry:first-child { border-top: 0; padding-top: 4px; }
.date { color: var(--muted); font-size: 1rem; }
a.pt { font-size: 1.9375rem; line-height: 1.2; text-decoration: none; text-wrap: balance; }
.entry p { margin: 8px 0 0; color: var(--muted); font-size: 1.125rem; }

.notfound { padding-bottom: 96px; }
.notfound h1 { font-size: 2.25rem; font-weight: 400; font-style: italic; margin: 0 0 12px; }

/* ---------- Phone ---------- */

@media (max-width: 640px) {
  :root { --gutter: 20px; }
  .top { padding-block: 12px 0; }
  .topin { gap: 12px; margin-bottom: 22px; }
  a.brand { width: auto; }
  .navlinks { gap: 18px; }
  .barin { gap: 14px; }
  /* Phone bar: "Writing / Section", no room for the name or the post title. */
  .bar a.brand, .bar .bt, .bar .bt-sep { display: none; }
  .sheet { left: 0; right: 0; min-width: 0; }
  .main h1 { font-size: 2.25rem; }
  .main h2 { font-size: 1.5625rem; }
  .main ol { padding-left: 26px; }
  .main ol > li::before { left: -26px; width: 16px; }
  .about { padding-top: 4px; }
  .face { width: 96px; height: 120px; margin: 4px 0 8px 16px; }
  .about .intro { font-size: 1.3125rem; line-height: 1.38; }
  .past { font-size: 1.0625rem; }
  .intro { font-size: 1.4375rem; }
  a.pt { font-size: 1.625rem; }
}

/* Phones narrower than the header needs: drop the nav to its own row. */
@media (max-width: 400px) {
  .topin { flex-wrap: wrap; row-gap: 4px; }
  .navlinks { order: 3; width: 100%; }
}

/* ---------- Wide: side notes in the margin ---------- */

@media (min-width: 960px) {
  .main { max-width: none; margin: 0; }
  .col { width: var(--measure); max-width: 100%; margin-left: max(0px, calc(50% - var(--measure) / 2)); }
  .ref { display: none !important; }
  .note, .js .note.collapsible:not(.open) {
    display: block;
    float: right;
    clear: right;
    width: 280px;
    margin: 0 -328px 8px 0;
    padding: 0;
    border: 0;
    font-size: .9375rem;
  }
  .mtoc { display: none; }
  .page .wrap { grid-template-columns: var(--rail) minmax(0, 1fr); column-gap: var(--rail-gap); }
  .page .intro, .page .about { grid-column: 2; }
  .page .posts { grid-column: 1 / -1; }
  .entry { grid-template-columns: var(--rail) minmax(0, 640px); column-gap: var(--rail-gap); align-items: start; }
  .date { padding-top: 8px; }
  .about { display: flex; align-items: flex-start; gap: 48px; }
  .about::after { display: none; }
  .bio { flex: 1 1 auto; }
  .face { order: 1; float: none; width: 192px; height: 240px; flex: none; margin: 8px 0 0; }
}

/* ---------- Widest: room for the outline rail ---------- */

@media (min-width: 1240px) {
  .left { display: block; padding-top: var(--rail-offset, 150px); }
  .mtoc { display: none; }
  .main ol { padding-left: 0; }
  .main ol > li::before { left: -34px; width: 22px; }
  /* Center the reading column on the page, not on what's left after the rail. */
  .col { margin-left: max(0px, calc(50% - var(--measure) / 2 - (var(--rail) + var(--rail-gap)) / 2)); }
}
