:root {
  --bg: #f4efe7;
  --paper: #fffdf8;
  --paper-strong: #fff7ea;
  --ink: #1f2430;
  --muted: #5f6878;
  --line: #ddd0bd;
  --accent: #0d5c63;
  --accent-soft: #d9efef;
  --code: #f5f0e8;
  --shadow: 0 20px 50px rgba(46, 35, 20, 0.1);
  --bg-radial: rgba(13, 92, 99, 0.12);
  --bg-grad-start: #f6f1e8;
  --bg-grad-end: #efe5d5;
  --sidebar-bg: rgba(255, 253, 248, 0.82);
  --sidebar-border: rgba(95, 104, 120, 0.15);
  --surface-border: rgba(95, 104, 120, 0.12);
  --surface-border-strong: rgba(95, 104, 120, 0.22);
  --surface-current: rgba(13, 92, 99, 0.08);
  --surface-hover: rgba(13, 92, 99, 0.05);
  --content-bg: rgba(255, 253, 248, 0.94);
  --sans: "Avenir Next", "Hiragino Sans", "Noto Sans JP", sans-serif;
  --serif: "Iowan Old Style", "Yu Mincho", "Hiragino Mincho ProN", serif;
}

html[data-theme="dark"] {
  --bg: #10161e;
  --paper: #18212b;
  --paper-strong: #1f2a36;
  --ink: #ebf1f7;
  --muted: #a8b3c3;
  --line: #314053;
  --accent: #7ed8cc;
  --accent-soft: rgba(126, 216, 204, 0.14);
  --code: #202a36;
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.34);
  --bg-radial: rgba(126, 216, 204, 0.16);
  --bg-grad-start: #0d141c;
  --bg-grad-end: #121b25;
  --sidebar-bg: rgba(18, 27, 37, 0.86);
  --sidebar-border: rgba(168, 179, 195, 0.14);
  --surface-border: rgba(168, 179, 195, 0.14);
  --surface-border-strong: rgba(168, 179, 195, 0.2);
  --surface-current: rgba(126, 216, 204, 0.12);
  --surface-hover: rgba(126, 216, 204, 0.08);
  --content-bg: rgba(16, 22, 30, 0.94);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background:
    radial-gradient(circle at top left, var(--bg-radial), transparent 30%),
    linear-gradient(180deg, var(--bg-grad-start) 0%, var(--bg-grad-end) 100%);
}

a {
  color: var(--accent);
  cursor: pointer;
}

.glossary-link,
.chapter-ref-link {
  position: relative;
  text-decoration-style: dotted;
  text-underline-offset: 0.14em;
}

.glossary-link::after,
.chapter-ref-link::after {
  content: attr(data-term) " とは: " attr(data-tooltip);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 10px);
  z-index: 20;
  width: min(280px, 70vw);
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(31, 36, 48, 0.96);
  color: #fffdf8;
  font-size: 0.84rem;
  line-height: 1.55;
  text-align: left;
  box-shadow: 0 14px 28px rgba(31, 36, 48, 0.24);
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(6px);
  transition: opacity 140ms ease, transform 140ms ease;
}

.glossary-link::before,
.chapter-ref-link::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: calc(100% + 4px);
  z-index: 20;
  border: 6px solid transparent;
  border-top-color: rgba(31, 36, 48, 0.96);
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(6px);
  transition: opacity 140ms ease, transform 140ms ease;
}

.glossary-link:hover::after,
.glossary-link:hover::before,
.glossary-link:focus-visible::after,
.glossary-link:focus-visible::before,
.chapter-ref-link:hover::after,
.chapter-ref-link:hover::before,
.chapter-ref-link:focus-visible::after,
.chapter-ref-link:focus-visible::before {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.glossary-link:focus-visible,
.chapter-ref-link:focus-visible {
  outline: none;
}

.layout-shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 28px 22px 40px;
  border-right: 1px solid var(--sidebar-border);
  background: var(--sidebar-bg);
  backdrop-filter: blur(20px);
}

.sidebar-header h1,
.sidebar-section h2,
.content h1,
.content h2,
.content h3 {
  font-family: var(--serif);
}

.content h1,
.content h2,
.content h3,
.content h4,
.content h5,
.content h6 {
  font-family: var(--sans);
}

.sidebar-header h1 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.25;
}

.sidebar-home-link {
  color: inherit;
  text-decoration: none;
}

.sidebar-home-link:hover,
.sidebar-home-link:focus-visible {
  color: var(--accent);
  outline: none;
}

.theme-toggle {
  margin-top: 14px;
  border: 1px solid var(--surface-border);
  border-radius: 999px;
  padding: 8px 12px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 180ms ease, border-color 180ms ease, transform 180ms ease;
}

.theme-toggle:hover,
.theme-toggle:focus-visible {
  background: var(--accent-soft);
  border-color: var(--accent);
  outline: none;
  transform: translateX(2px);
}

.sidebar-section {
  margin-top: 28px;
}

.sidebar-section h2 {
  margin: 0 0 12px;
  font-size: 0.96rem;
}

.sidebar-links,
.toc-links {
  display: grid;
  gap: 8px;
}

.toc-links {
  gap: 10px;
  margin-top: 6px;
  margin-left: 8px;
  padding-left: 8px;
  border-left: 1px solid var(--surface-border);
}

.chapter-group {
  border-radius: 16px;
}

.chapter-summary {
  display: block;
  list-style: none;
}

.chapter-summary::-webkit-details-marker {
  display: none;
}

.sidebar-link,
.toc-link {
  display: block;
  text-decoration: none;
  border-radius: 14px;
  cursor: pointer;
}

.sidebar-link {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 10px 12px;
  background: transparent;
  color: var(--ink);
  transition: background 180ms ease, transform 180ms ease;
}

.chapter-summary .sidebar-link {
  position: relative;
  padding-left: 28px;
}

.chapter-summary .sidebar-link::before {
  content: "▸";
  position: absolute;
  left: 10px;
  top: 11px;
  color: var(--muted);
  transition: transform 180ms ease;
}

.chapter-group[open] > .chapter-summary .sidebar-link::before {
  transform: rotate(90deg);
}

.sidebar-link:hover,
.sidebar-link:focus-visible,
.toc-link:hover,
.toc-link:focus-visible {
  background: var(--accent-soft);
  outline: none;
  transform: translateX(2px);
}

.sidebar-link-current {
  background: var(--surface-current);
}

.sidebar-order {
  display: inline-block;
  margin-bottom: 0;
  flex: 0 0 auto;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.sidebar-title {
  display: inline-block;
  font-size: 0.94rem;
  line-height: 1.45;
}

.toc-link {
  padding: 6px 10px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.4;
}

.toc-top-link {
  color: var(--ink);
}

.toc-level-3 {
  margin-left: 18px;
  font-size: 0.88rem;
}

.toc-group {
  border-radius: 16px;
}

.toc-summary {
  display: flex;
  align-items: center;
  list-style: none;
}

.toc-summary::-webkit-details-marker {
  display: none;
}

.toc-summary::before {
  content: "▸";
  flex: 0 0 auto;
  margin-right: 6px;
  color: var(--muted);
  transition: transform 180ms ease;
}

.toc-group[open] > .toc-summary::before {
  transform: rotate(90deg);
}

.toc-summary .toc-link {
  flex: 1 1 auto;
  padding-left: 0;
}

.toc-children {
  display: grid;
  gap: 4px;
  padding-top: 4px;
}

.toc-empty {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.content-shell {
  min-width: 0;
  padding: 18px;
}

.sidebar-toggle {
  display: none;
  margin-bottom: 16px;
  border: 0;
  border-radius: 999px;
  padding: 12px 16px;
  background: var(--accent);
  color: white;
  font: inherit;
  font-weight: 700;
  box-shadow: var(--shadow);
}

.content {
  max-width: 980px;
  margin: 0 auto;
  padding: 28px clamp(18px, 3vw, 38px);
  border: 1px solid var(--surface-border);
  border-radius: 32px;
  background: var(--content-bg);
  box-shadow: var(--shadow);
}

.content > :first-child {
  margin-top: 0;
}

.index-hero {
  margin-bottom: 2rem;
}

.index-kicker {
  margin: 0 0 0.6rem;
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
}

.index-lead {
  max-width: 48rem;
  color: var(--muted);
}

.chapter-list-section {
  margin-top: 1.25rem;
}

.chapter-list {
  margin: 0;
  padding: 0;
  list-style: none;
  border-top: 1px solid var(--surface-border);
}

.chapter-list-item {
  margin: 0;
  border-bottom: 1px solid var(--surface-border);
}

.chapter-list-link {
  display: flex;
  align-items: baseline;
  gap: 14px;
  padding: 15px 2px;
  text-decoration: none;
  color: inherit;
  transition: background 180ms ease, transform 180ms ease;
}

.chapter-list-link:hover,
.chapter-list-link:focus-visible {
  background: var(--surface-hover);
  outline: none;
  transform: translateX(2px);
}

.chapter-list-order {
  display: inline-block;
  flex: 0 0 auto;
  min-width: 2.2rem;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.chapter-list-title {
  font-size: 1.08rem;
  line-height: 1.5;
  color: var(--ink);
}

.content h1 {
  font-size: clamp(2rem, 5vw, 3.25rem);
  line-height: 1.08;
  letter-spacing: -0.03em;
}

.content h2 {
  margin-top: 2.8rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--surface-border);
  font-size: clamp(1.5rem, 3vw, 2.1rem);
}

.content h3 {
  margin-top: 2rem;
  font-size: 1.25rem;
}

.content p,
.content li,
.content blockquote {
  font-size: 1rem;
  line-height: 1.9;
}

.content ul,
.content ol {
  padding-left: 1.5rem;
}

.content img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.5rem auto;
  border-radius: 18px;
}

.content table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  overflow: hidden;
  border-radius: 16px;
  background: var(--paper);
}

.content th,
.content td {
  padding: 12px 14px;
  border: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.content code {
  padding: 0.18em 0.42em;
  border-radius: 8px;
  background: var(--code);
  font-size: 0.92em;
}

.content pre {
  overflow-x: auto;
  padding: 18px 20px;
  border-radius: 0;
  background: #101722;
  color: #edf2f7;
  font-size: 0.98rem;
  line-height: 1.75;
  border: 1px solid var(--surface-border-strong);
}

.content pre code {
  padding: 0;
  background: transparent;
  color: inherit;
  font-size: inherit;
  line-height: inherit;
  font-family: "SFMono-Regular", "JetBrains Mono", "Menlo", "Consolas", monospace;
}

.hljs-comment,
.hljs-quote {
  color: #7f8ea3;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-subst {
  color: #ff7b72;
}

.hljs-number,
.hljs-literal,
.hljs-variable,
.hljs-template-variable,
.hljs-tag .hljs-attr {
  color: #f2cc60;
}

.hljs-string,
.hljs-doctag {
  color: #9ece6a;
}

.hljs-title,
.hljs-section,
.hljs-selector-id {
  color: #7aa2f7;
}

.hljs-type,
.hljs-class .hljs-title {
  color: #73daca;
}

.hljs-attribute,
.hljs-name,
.hljs-tag {
  color: #e0af68;
}

.hljs-regexp,
.hljs-link {
  color: #bb9af7;
}

.hljs-built_in,
.hljs-builtin-name {
  color: #2ac3de;
}

.hljs-meta {
  color: #c0caf5;
}

.hljs-emphasis {
  font-style: italic;
}

.hljs-strong {
  font-weight: 700;
}

.content blockquote {
  margin: 1.5rem 0;
  padding: 0.4rem 1rem;
  border-left: 4px solid #bfa37a;
  color: #5b4f43;
  background: rgba(191, 163, 122, 0.08);
}

.callout {
  margin: 1.5rem 0;
  padding: 1rem 1.1rem;
  border: 1px solid rgba(95, 104, 120, 0.12);
  border-radius: 22px;
  background: var(--paper-strong);
  color: var(--ink);
}

.callout-title {
  margin-bottom: 0.5rem;
  font-size: 0.84rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.callout-abstract {
  border: 0;
  border-radius: 6px;
  padding: 0.95rem 1.45rem 1.15rem;
  background: #dceff1;
  box-shadow: none;
  color: #1f2430;
}

.callout-abstract .callout-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.7rem;
  color: #08b7c5;
  font-size: 0.96rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
}

.callout-abstract .callout-title::before {
  content: "";
  display: inline-flex;
  width: 1.05rem;
  height: 1.05rem;
  align-items: center;
  justify-content: center;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2308b7c5' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='5' y='4' width='14' height='16' rx='2'/%3E%3Cpath d='M9 4.5h6'/%3E%3Cpath d='M9 10h6'/%3E%3Cpath d='M9 14h4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

.callout-abstract p {
  margin: 0;
  color: #1f2430;
  font-size: 1.02rem;
  line-height: 1.85;
  font-weight: 600;
}

.callout-tip {
  border: 0;
  border-radius: 6px;
  padding: 0.95rem 1.45rem 1.15rem;
  background: #dceff1;
  box-shadow: none;
  color: #1f2430;
}

.callout-tip .callout-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.7rem;
  color: #08b7c5;
  font-size: 0.96rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
}

.callout-tip .callout-title::before {
  content: "";
  display: inline-flex;
  width: 1.05rem;
  height: 1.05rem;
  align-items: center;
  justify-content: center;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2308b7c5' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M8 4h7l3 3v11a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z'/%3E%3Cpath d='M15 4v4h4'/%3E%3Cpath d='M9 11h6'/%3E%3Cpath d='M9 15h4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

.callout-tip p {
  margin: 0;
  color: #1f2430;
  font-size: 1.02rem;
  line-height: 1.85;
  font-weight: 600;
}

.callout-note,
.callout-summary {
  border: 0;
  border-radius: 6px;
  padding: 0.95rem 1.45rem 1.15rem;
  background: #dceff1;
  box-shadow: none;
  color: #1f2430;
}

.callout-note .callout-title,
.callout-summary .callout-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.7rem;
  color: #08b7c5;
  font-size: 0.96rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
}

.callout-note .callout-title::before,
.callout-summary .callout-title::before {
  content: "";
  display: inline-flex;
  width: 1.05rem;
  height: 1.05rem;
  align-items: center;
  justify-content: center;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

.callout-note .callout-title::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2308b7c5' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4 4 8v5c0 5 3.4 6.9 8 7 4.6-.1 8-2 8-7V8z'/%3E%3Cpath d='M12 10v3'/%3E%3Cpath d='M12 16h.01'/%3E%3C/svg%3E");
}

.callout-summary .callout-title::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2308b7c5' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='5' y='5' width='14' height='14' rx='2'/%3E%3Cpath d='M8 10h8'/%3E%3Cpath d='M8 14h5'/%3E%3C/svg%3E");
}

.callout-note p,
.callout-summary p {
  margin: 0;
  color: #1f2430;
  font-size: 1.02rem;
  line-height: 1.85;
  font-weight: 600;
}

.callout-abstract :is(p, li, strong, em, code),
.callout-tip :is(p, li, strong, em, code),
.callout-note :is(p, li, strong, em, code),
.callout-summary :is(p, li, strong, em, code) {
  color: #1f2430;
}

html[data-theme="dark"] .callout-abstract,
html[data-theme="dark"] .callout-tip,
html[data-theme="dark"] .callout-note,
html[data-theme="dark"] .callout-summary {
  background: #79aeb5;
  color: #15202b;
}

html[data-theme="dark"] .callout-abstract .callout-title,
html[data-theme="dark"] .callout-tip .callout-title,
html[data-theme="dark"] .callout-note .callout-title,
html[data-theme="dark"] .callout-summary .callout-title {
  color: #007f89;
}

html[data-theme="dark"] .callout-abstract .callout-title::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23007f89' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='5' y='4' width='14' height='16' rx='2'/%3E%3Cpath d='M9 4.5h6'/%3E%3Cpath d='M9 10h6'/%3E%3Cpath d='M9 14h4'/%3E%3C/svg%3E");
}

html[data-theme="dark"] .callout-tip .callout-title::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23007f89' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M8 4h7l3 3v11a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z'/%3E%3Cpath d='M15 4v4h4'/%3E%3Cpath d='M9 11h6'/%3E%3Cpath d='M9 15h4'/%3E%3C/svg%3E");
}

html[data-theme="dark"] .callout-note .callout-title::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23007f89' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4 4 8v5c0 5 3.4 6.9 8 7 4.6-.1 8-2 8-7V8z'/%3E%3Cpath d='M12 10v3'/%3E%3Cpath d='M12 16h.01'/%3E%3C/svg%3E");
}

html[data-theme="dark"] .callout-summary .callout-title::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23007f89' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='5' y='5' width='14' height='14' rx='2'/%3E%3Cpath d='M8 10h8'/%3E%3Cpath d='M8 14h5'/%3E%3C/svg%3E");
}

html[data-theme="dark"] .callout-abstract :is(p, li, strong, em, code),
html[data-theme="dark"] .callout-tip :is(p, li, strong, em, code),
html[data-theme="dark"] .callout-note :is(p, li, strong, em, code),
html[data-theme="dark"] .callout-summary :is(p, li, strong, em, code) {
  color: #15202b;
}

.mermaid {
  display: flex;
  justify-content: center;
  overflow-x: auto;
  padding: 1rem;
  background: white;
}

@media (max-width: 960px) {
  .layout-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, 320px);
    z-index: 20;
    transform: translateX(-102%);
    transition: transform 220ms ease;
    box-shadow: var(--shadow);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .content-shell {
    padding: 12px;
  }

  .content {
    padding: 22px 16px;
    border-radius: 24px;
  }
}
