/* === Variables === */
:root {
  --font-body: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  --font-mono: "SF Mono", SFMono-Regular, Consolas, monospace;
  --bg: #fff;
  --bg-alt: #f5f5f5;
  --text: #555;
  --heading: #111;
  --link: #111;
  --border: #e8e8e8;
  --border-alt: #c8c8c8;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --glass-bg: rgba(255, 255, 255, .72);
  --glass-border: rgba(0, 0, 0, .06);
  --shadow: 0 4px 12px rgba(0, 0, 0, .06);
  --transition: .25s ease;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

.dark-mode,
:root[dark] {
  --bg: #151515;
  --bg-alt: #1e1e1e;
  --text: #c0c0c0;
  --heading: #eee;
  --link: #eee;
  --border: #2a2a2a;
  --border-alt: #444;
  --glass-bg: rgba(21, 21, 21, .72);
  --glass-border: rgba(255, 255, 255, .08);
  --shadow: 0 4px 12px rgba(0, 0, 0, .3);
}

/* === Reset & Base === */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  font: 17px/1.6 var(--font-body);
  color: var(--text);
  background: var(--bg);
  transition: background var(--transition), color var(--transition);
  overflow-x: hidden;
  width: 100%;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
}

a {
  color: var(--link);
  text-decoration: none;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: var(--font-ui);
  color: var(--heading);
  line-height: 1.3;
  font-weight: 600;
}

p {
  margin-bottom: 16px;
}

ul,
ol {
  padding-left: 1.5em;
}

table {
  border-collapse: collapse;
  width: 100%;
}

th,
td {
  padding: 8px 12px;
  border: 1px solid var(--border);
  text-align: left;
  font-family: var(--font-ui);
  font-size: 15px;
}

.list-reset {
  list-style: none;
  padding: 0;
  margin: 0;
}

pre {
  overflow-x: auto;
}

/* === Layout === */
.global-wrap {
  min-height: 100vh;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 20px;
}

/* === Home === */
.home {
  max-width: 560px;
  margin: 0 auto;
  padding: 96px 20px 80px;
}

.home__header {
  margin-bottom: 56px;
}

.home__name {
  font-size: 22px;
  font-weight: 600;
  color: var(--heading);
  letter-spacing: -.015em;
  margin-bottom: 14px;
}

.home__bio {
  font-size: 15px;
  line-height: 1.65;
  color: var(--text);
  margin-bottom: 20px;
  max-width: 440px;
}

.home__links {
  display: flex;
  align-items: center;
  gap: 16px;
  color: var(--text);
  font-size: 17px;
}

.home__links a {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  text-decoration: none;
  transition: color var(--transition), transform var(--transition);
}

.home__links a:hover {
  color: var(--heading);
  transform: translateY(-1px);
}

/* === Writings list === */
.home__section-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--heading);
  margin-bottom: 16px;
}

.home__post {
  display: block;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  transition: opacity var(--transition);
}

.home__post:first-of-type {
  border-top: 1px solid var(--border);
}

.home__post:hover {
  opacity: .7;
}

.home__post-title {
  font-size: 16px;
  font-weight: 500;
  color: var(--heading);
}

/* === Theme Toggle === */
.toggle-theme {
  position: fixed;
  z-index: 5;
  right: calc(32px + var(--safe-right));
  bottom: calc(32px + var(--safe-bottom));
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  box-shadow: var(--shadow);
  border: 1px solid var(--glass-border);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  backdrop-filter: saturate(180%) blur(12px);
  background: var(--glass-bg);
  color: var(--heading);
  cursor: pointer;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), background 0.2s, box-shadow 0.2s;
  outline: none;
}

.toggle-theme:hover {
  transform: scale(1.1);
  background: var(--bg-alt);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .12);
}

.toggle-theme:active {
  transform: scale(0.95);
}

.toggle-sun,
.toggle-moon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-moon {
  display: none;
}

.dark-mode .toggle-moon {
  display: flex;
}

.dark-mode .toggle-sun {
  display: none;
}

@media(max-width:576px) {
  .toggle-theme {
    right: calc(20px + var(--safe-right));
    bottom: calc(20px + var(--safe-bottom));
    width: 40px;
    height: 40px;
  }
}

/* === Code Copy === */
pre.highlight {
  position: relative;
}

.copy-code {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 8px;
  font-size: 12px;
  font-family: var(--font-ui);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text);
}

/* === Article Layout (bv-article) === */
.bv-article {
  font-size: 17px;
  line-height: 1.6;
  color: var(--text);
  max-width: 820px;
  margin: 0 auto;
  overflow-wrap: break-word;
  word-wrap: break-word;
}

.katex {
  font-size: 1.1em !important;
}

.bv-article h1 {
  font-size: 44px;
  font-weight: 800;
  line-height: 1.15;
  margin: 4rem 0 2.5rem;
  letter-spacing: -0.02em;
}

.bv-article h2 {
  font-size: 28px;
  font-weight: 600;
  margin: 2rem 0 .75rem;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

.bv-article h3 {
  font-size: 22px;
  margin: 1.5rem 0 .5rem;
}

.bv-article h4 {
  font-size: 18px;
  margin: 1.2rem 0 .4rem;
  font-weight: 600;
}

.bv-article p {
  margin-bottom: 1.25rem;
}

.bv-article a {
  color: var(--link);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.bv-article figure {
  margin: 2rem 0;
  text-align: center;
}

.bv-article figure img,
.bv-article figure object[type="image/svg+xml"] {
  max-width: 100%;
  margin: 0 auto;
  display: block;
}

.bv-article figure img[src$=".svg"],
.bv-article figure object[type="image/svg+xml"] {
  opacity: 0.666;
  /* Match #000 vectors to #555 var(--text) light mode */
}

.bv-article figcaption {
  font-size: 14px;
  color: var(--text);
  margin-top: 8px;
  font-style: italic;
}

.bv-article .l-body {
  max-width: 100%;
  margin: 1.5rem auto;
}

.bv-article .l-page {
  max-width: 110%;
  margin-left: -5%;
}

.bv-article .l-screen-inset {
  max-width: 130%;
  margin-left: -15%;
}

.bv-article ol,
.bv-article ul {
  margin-bottom: 1.25rem;
}

.bv-article li {
  margin-bottom: .5rem;
}

.bv-article table {
  margin: 1.5rem auto;
}

.bv-article th {
  background: var(--bg-alt);
  font-weight: 600;
}

.bv-article .bv-sidenote {
  float: right;
  clear: right;
  width: 200px;
  margin: 0 -240px 8px 16px;
  font-size: 13px;
  line-height: 1.5;
}

.bv-article .bv-sidenote-number {
  font-size: 11px;
  vertical-align: super;
  font-weight: 600;
  color: var(--heading);
}

@media(max-width:992px) {
  .bv-article .bv-sidenote {
    float: none;
    width: 100%;
    margin: 8px 0 16px;
    padding: 12px;
    background: var(--bg-alt);
    border-radius: var(--radius-sm);
  }

  .bv-article .l-page,
  .bv-article .l-screen-inset {
    max-width: 100%;
    margin-left: 0;
  }
}

/* === Mobile Article Styles === */
@media(max-width:768px) {
  .bv-article {
    padding: 0 20px;
    font-size: 16px;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .bv-article h1 {
    font-size: 28px;
    margin: 2rem 0 1.25rem;
    word-break: break-word;
    hyphens: auto;
  }

  .bv-article h2 {
    font-size: 22px;
    margin: 1.5rem 0 .6rem;
  }

  .bv-article h3 {
    font-size: 19px;
  }

  .bv-article h4 {
    font-size: 16px;
  }

  .bv-article p {
    overflow-wrap: break-word;
  }

  .bv-article figure {
    margin: 1.25rem 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .bv-article figure img,
  .bv-article figure object[type="image/svg+xml"] {
    max-width: 100%;
    height: auto;
  }

  .bv-article .definition,
  .bv-article .theorem {
    padding: 14px 16px;
    overflow-x: auto;
  }

  .bv-article table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  .bv-article .l-body {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .bv-article ol,
  .bv-article ul {
    padding-left: 1.2em;
  }

  .bv-footer {
    padding: 1.5rem 20px;
  }

  .footer-container {
    padding: 0;
  }

  .markdown-code,
  .citation-block {
    font-size: 11px;
    padding: 12px;
  }
}

@media(max-width:480px) {
  .bv-article {
    padding: 0 14px;
    font-size: 15px;
  }

  .bv-article h1 {
    font-size: 24px;
    margin: 1.5rem 0 1rem;
  }

  .bv-article h2 {
    font-size: 20px;
  }

  .bv-footer {
    padding: 1.25rem 14px;
  }
}

/* Prevent KaTeX math from overflowing on any screen */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0;
  max-width: 100%;
}

.katex-display>.katex {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 2px;
}

.bv-article .bv-section-label {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: .5rem;
}

.bv-article .definition,
.bv-article .theorem {
  margin: 1.5rem 0;
  padding: 16px 20px;
  background: var(--bg-alt);
  border-left: 3px solid var(--border-alt);
  border-radius: var(--radius-sm);
}

.bv-article .citation,
.bv-article .footnote {
  font-size: 11px;
  vertical-align: super;
  color: var(--link);
  cursor: pointer;
}

/* === Article Footer === */
.bv-footer {
  margin-top: 3rem;
  padding: 2rem 0;
  border-top: 1px solid var(--border);
}

.footer-container {
  max-width: 820px;
  margin: 0 auto;
}

.footer-section {
  margin-bottom: 1.5rem;
}

.footer-section h3 {
  font-size: 18px;
  margin-bottom: 12px;
}

.footer-list {
  font-size: 15px;
  line-height: 1.6;
}

.reference-item {
  margin-bottom: 8px;
}

.markdown-code,
.citation-block {
  background: var(--bg-alt);
  padding: 16px;
  border-radius: var(--radius-md);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
}

/* === Visualization Controls === */
.bv-step-btn,
.bv-basis-btn {
  font-family: var(--font-ui);
  font-size: 13px;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--heading);
  cursor: pointer;
}

.bv-step-btn:hover,
.bv-basis-btn:hover {
  background: var(--bg-alt);
}

.bv-basis-btn.is-selected {
  border-color: var(--heading);
  background: var(--bg-alt);
  font-weight: 600;
}

:root[dark] .bv-basis-btn.is-selected {
  border-color: #fff;
}

.bv-analysis-label {
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
}

/* === K-Maps === */
.kmap-container {
  display: inline-block;
}

.kmap-table {
  border-collapse: collapse;
}

.kmap-table td {
  width: 40px;
  height: 40px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 18px;
  border: 1px solid var(--border);
}

.kmap-table .kmap-header {
  background: var(--bg-alt);
  font-weight: 600;
  font-size: 14px;
}

.kmap-table .kmap-one {
  font-weight: 700;
  color: var(--heading);
}

.kmap-covering {
  position: absolute;
  border: 2.5px solid;
  border-radius: 8px;
  pointer-events: none;
}

.kmap-cell-label {
  font-size: 11px;
  opacity: .6;
  position: absolute;
  bottom: 2px;
  right: 4px;
}

.function-label {
  font-family: var(--font-mono);
  font-size: 13px;
  text-align: center;
  margin-top: 4px;
}

:root[dark] .kmap-table td {
  border-color: #444;
}

/* === K-map SVG Styles === */
.kmap-border {
  fill: none;
  stroke: var(--heading);
  stroke-width: 1.5;
}

.kmap-line {
  stroke: var(--heading);
  stroke-width: 1;
}

.kmap-line-thin {
  stroke: var(--border);
  stroke-width: 0.5;
}

/* === SVG Figure Dark Mode === */
.dark-mode .bv-article figure img[src$=".svg"],
:root[dark] .bv-article figure img[src$=".svg"],
.dark-mode .bv-article figure object[type="image/svg+xml"],
:root[dark] .bv-article figure object[type="image/svg+xml"] {
  filter: invert(1) hue-rotate(180deg);
  opacity: 0.73;
  /* Match inverted vectors exactly to #c0c0c0 var(--text) */
}

/* === Simon / QPE Viz === */
.simon-highlight-row {
  background: rgba(0, 0, 0, .1) !important;
}

.simon-highlight-active {
  background: rgba(0, 0, 0, .15) !important;
}

:root[dark] .simon-highlight-row {
  background: rgba(255, 255, 255, .05) !important;
}

:root[dark] .simon-highlight-active {
  background: rgba(255, 255, 255, .1) !important;
}

.ticks {
  font: 10px sans-serif;
}

.track,
.track-inset,
.track-overlay {
  stroke-linecap: round;
}

.track {
  stroke: #e6e6e6;
  stroke-opacity: .5;
  stroke-width: 10px;
}

.track-inset {
  stroke: #e6e6e6;
  stroke-width: 8px;
}

.track-overlay {
  pointer-events: stroke;
  stroke-width: 50px;
  stroke: transparent;
  cursor: pointer;
}

.handle {
  fill: #ff9800;
  stroke: #e65100;
  stroke-width: 1.5px;
  cursor: pointer;
}

.figtext {
  font: bold 13px/18px "Helvetica Neue", sans-serif;
  color: #333;
}

.ticktext {
  font: 11px "Helvetica Neue", sans-serif;
  fill: #999;
}