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

/* === Custom Properties === */
:root {
  --bg: #fff;
  --fg: #1a1a1a;
  --fg-muted: #666;
  --accent: #0077b6;
  --accent-hover: #005a8c;
  --border: #e0e0e0;
  --bg-card: #f7f7f7;
  --bg-nav: #fff;
  --shadow-nav: 0 1px 3px rgba(0,0,0,0.08);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  --content-width: 65ch;
  --nav-height: 48px;
}

html.dark {
  --bg: #1a1a1a;
  --fg: #e0e0e0;
  --fg-muted: #999;
  --accent: #4da8da;
  --accent-hover: #7cc5e8;
  --border: #333;
  --bg-card: #242424;
  --bg-nav: #222;
  --shadow-nav: 0 1px 3px rgba(0,0,0,0.3);
}

/* === Base === */
html {
  font-size: 100%; /* 16px default, respects browser/OS font size */
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  font-size: 1.125rem; /* 18px at default */
  line-height: 1.6;
  color: var(--fg);
  background: var(--bg);
  min-height: 100vh;
}

@media (max-width: 768px) {
  body { font-size: 1.0625rem; }
}

/* === Navigation === */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-nav);
  box-shadow: var(--shadow-nav);
}

.nav-bar {
  display: flex;
  align-items: center;
  height: 56px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
  gap: 0.5rem;
}

.nav-logo {
  font-weight: 700;
  font-size: 1.125rem;
  color: var(--fg);
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
}

.nav-spacer { flex: 1; }

.nav-sections-btn {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  background: none;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.5rem 0.75rem;
  font-family: var(--font);
  font-size: 0.9375rem;
  color: var(--fg);
  cursor: pointer;
  min-height: 44px;
  transition: background 0.15s, border-color 0.15s;
}

.nav-sections-btn:hover { background: var(--bg-card); border-color: var(--fg-muted); }
.nav-sections-btn[aria-expanded="true"] { background: var(--bg-card); border-color: var(--accent); }
.nav-sections-btn[aria-expanded="true"] .nav-chevron { transform: rotate(180deg); }

.nav-sections-label { font-weight: 500; }

.nav-chevron { transition: transform 0.2s ease; flex-shrink: 0; }

.nav-icon-btn {
  background: none;
  border: none;
  color: var(--fg-muted);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

.nav-icon-btn:hover { background: var(--bg-card); color: var(--fg); }

.nav-search-btn { gap: 0.25rem; }

.search-kbd {
  font-family: var(--font);
  font-size: 0.6875rem;
  font-weight: 500;
  color: var(--fg-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  padding: 0.0625rem 0.375rem;
  line-height: 1.4;
}

@media (max-width: 768px) {
  .search-kbd { display: none; }
}

/* === Sections Dropdown === */
.sections-dropdown {
  background: var(--bg-nav);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  animation: dropdown-in 0.15s ease-out;
}

.sections-dropdown[hidden] { display: none; }

@keyframes dropdown-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

html.dark .sections-dropdown { box-shadow: 0 4px 12px rgba(0,0,0,0.3); }

.sections-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.375rem;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.75rem 1rem;
}

.section-link {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem 1rem;
  border-radius: 0.5rem;
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--fg);
  text-decoration: none;
  background: var(--bg-card);
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  min-height: 48px;
}

.section-link:hover { border-color: var(--accent); transform: translateY(-1px); }
.section-link.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* === Breadcrumb === */
.breadcrumb {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.25rem 1rem 0.5rem;
  font-size: 0.8125rem;
  color: var(--fg-muted);
}

.breadcrumb a { color: var(--fg-muted); text-decoration: none; }
.breadcrumb a:hover { color: var(--accent); text-decoration: underline; }
.bc-sep { margin: 0 0.375rem; }

/* === Search === */
.search-container {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 1rem;
}

.search-container[hidden] { display: none; }

#pf-input {
  width: 100%;
  max-width: var(--content-width);
  margin: 0 auto;
  display: block;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--bg);
  color: var(--fg);
  outline: none;
}

#pf-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(0,119,182,0.2); }

#pf-results {
  max-width: var(--content-width);
  margin: 0.5rem auto 0;
}

.pf-result {
  display: block;
  padding: 0.75rem;
  text-decoration: none;
  color: var(--fg);
  border-radius: 0.375rem;
  transition: background 0.1s;
}

.pf-result:hover { background: var(--bg); }
.pf-result strong { display: block; color: var(--accent); margin-bottom: 0.125rem; }
.pf-result span { font-size: 0.875rem; color: var(--fg-muted); }
.pf-result mark { background: rgba(0,119,182,0.15); color: inherit; border-radius: 2px; padding: 0 2px; }

/* === Content === */
.content {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 1.5rem 1rem 4rem;
}

.wiki-article h1 { font-size: 1.75rem; font-weight: 700; margin-top: 0; margin-bottom: 1rem; line-height: 1.25; }
.wiki-article h2 { font-size: 1.375rem; font-weight: 700; margin-top: 2rem; margin-bottom: 0.75rem; line-height: 1.3; border-bottom: 1px solid var(--border); padding-bottom: 0.375rem; }
.wiki-article h3 { font-size: 1.125rem; font-weight: 700; margin-top: 1.5rem; margin-bottom: 0.5rem; }

.wiki-article p { margin-bottom: 1rem; }

.wiki-article a { color: var(--accent); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }
.wiki-article a:hover { color: var(--accent-hover); }

.wiki-article ul, .wiki-article ol { margin-bottom: 1rem; padding-left: 1.5rem; }
.wiki-article li { margin-bottom: 0.375rem; }
.wiki-article li > ul, .wiki-article li > ol { margin-bottom: 0; margin-top: 0.25rem; }

.wiki-article strong { font-weight: 700; }

.wiki-article blockquote {
  border-left: 3px solid var(--accent);
  padding: 0.75rem 1rem;
  margin: 1rem 0;
  background: var(--bg-card);
  border-radius: 0 0.375rem 0.375rem 0;
  color: var(--fg);
}

.wiki-article table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-size: 0.9375rem;
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.wiki-article th, .wiki-article td {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  text-align: left;
}

.wiki-article th { background: var(--bg-card); font-weight: 700; }

.wiki-article hr { border: none; border-top: 1px solid var(--border); margin: 2rem 0; }

.wiki-article code {
  font-family: 'SF Mono', SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.875em;
  background: var(--bg-card);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
}

.wiki-article pre {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 1rem;
  overflow-x: auto;
  margin: 1rem 0;
}

.wiki-article pre code { background: none; padding: 0; }

/* === Collapsible Sections (index page) === */
.collapsible-section {
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  margin-bottom: 0.5rem;
  overflow: hidden;
}

.collapsible-section summary {
  font-size: 1.375rem;
  font-weight: 700;
  line-height: 1.3;
  padding: 0.75rem 1rem;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background 0.15s;
  user-select: none;
  -webkit-user-select: none;
}

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

.collapsible-section summary::after {
  content: '';
  width: 0.5rem;
  height: 0.5rem;
  border-right: 2px solid var(--fg-muted);
  border-bottom: 2px solid var(--fg-muted);
  transform: rotate(-45deg);
  transition: transform 0.2s ease;
  flex-shrink: 0;
  margin-left: 0.75rem;
}

.collapsible-section[open] summary::after { transform: rotate(45deg); }

.collapsible-section summary:hover { background: var(--bg-card); }

.collapsible-section > :not(summary) { padding: 0 1rem; }
.collapsible-section > ul, .collapsible-section > ol { padding: 0 1rem 1rem 2.5rem; }
.collapsible-section > p:last-child { padding-bottom: 1rem; }
.collapsible-section > h3 { padding: 0 1rem; }

/* === Footnotes === */
.footnotes { margin-top: 3rem; font-size: 0.875rem; color: var(--fg-muted); }
.footnotes h2 { font-size: 1rem; border-bottom: 1px solid var(--border); }
.footnote-list { padding-left: 1.5rem; }
.footnote-list li { margin-bottom: 0.375rem; }
.footnote-ref { text-decoration: none; color: var(--accent); font-size: 0.75rem; }
.footnote-back { text-decoration: none; color: var(--fg-muted); margin-left: 0.25rem; }

/* === Category listing === */
.category-grid {
  list-style: none;
  padding: 0;
  display: grid;
  gap: 0.75rem;
}

.category-card {
  display: block;
  padding: 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  text-decoration: none;
  color: var(--fg);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.category-card:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(0,119,182,0.1); }
.category-card strong { color: var(--accent); display: block; margin-bottom: 0.25rem; }
.category-card span { font-size: 0.875rem; color: var(--fg-muted); }

/* === Footer === */
.site-footer {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 2rem 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.8125rem;
  color: var(--fg-muted);
  text-align: center;
}

.site-footer a { color: var(--fg-muted); }

/* === Safe areas === */
@supports (padding: env(safe-area-inset-bottom)) {
  .site-footer { padding-bottom: calc(2rem + env(safe-area-inset-bottom)); }
  .nav-bar { padding-left: calc(1rem + env(safe-area-inset-left)); padding-right: calc(1rem + env(safe-area-inset-right)); }
}

/* === Word break for long URLs === */
.wiki-article a { word-break: break-word; overflow-wrap: break-word; }

/* === Print === */
@media print {
  .site-header, .search-container, .nav-theme-btn, .nav-search-btn { display: none !important; }
  body { padding-top: 0; font-size: 11pt; }
  .content { max-width: 100%; }
}
