:root {
  --bg: #f7f8f7;
  --panel: #ffffff;
  --panel-soft: #fbfcfa;
  --ink: #17201f;
  --muted: #68736f;
  --line: #e1e7e3;
  --teal: #0f766e;
  --teal-strong: #0b5f59;
  --amber: #b7791f;
  --input-bg: #ffffff;
  --button-bg: #ffffff;
  --topbar-bg: rgba(255, 255, 255, 0.96);
  --hover-bg: #f1f6f2;
  --active-field-bg: #f3faf8;
  --active-section-bg: #fbf5e9;
  --active-section-text: #6d470a;
  --pill-bg: #eef7f6;
  --subtle-bg: #edf4ef;
  --quote-bg: #fbf5e9;
  --quote-text: #563806;
  --code-bg: #101615;
  --code-text: #e6f5ef;
  --inline-code-bg: #eef3ef;
  --inline-code-text: #1c4d47;
  --danger-bg: #fff1ee;
  --danger-text: #a53629;
  --error-text: #b9382c;
  --scrollbar-track: #eef2ef;
  --scrollbar-thumb: #b9c5bf;
  --scrollbar-thumb-hover: #87958f;
  --focus-ring: rgba(15, 118, 110, 0.16);
  --shadow: 0 10px 30px rgba(22, 32, 29, 0.06);
  --shadow-popover: 0 20px 60px rgba(22, 32, 29, 0.16);
  color-scheme: light;
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  --bg: #111714;
  --panel: #18201d;
  --panel-soft: #151c19;
  --ink: #f2f6f3;
  --muted: #a8b4ae;
  --line: #2d3a34;
  --teal: #5eead4;
  --teal-strong: #2dd4bf;
  --amber: #f0bd69;
  --input-bg: #111714;
  --button-bg: #18201d;
  --topbar-bg: rgba(17, 23, 20, 0.96);
  --hover-bg: #202a25;
  --active-field-bg: #17302c;
  --active-section-bg: #302516;
  --active-section-text: #f0bd69;
  --pill-bg: #20342f;
  --subtle-bg: #202a25;
  --quote-bg: #302516;
  --quote-text: #f3d7a4;
  --code-bg: #0a0f0d;
  --code-text: #d8fff6;
  --inline-code-bg: #22312b;
  --inline-code-text: #88f7e7;
  --danger-bg: #321d1b;
  --danger-text: #ffafa5;
  --error-text: #ffafa5;
  --scrollbar-track: #111714;
  --scrollbar-thumb: #33433c;
  --scrollbar-thumb-hover: #5eead4;
  --focus-ring: rgba(94, 234, 212, 0.18);
  --shadow: none;
  --shadow-popover: 0 20px 60px rgba(0, 0, 0, 0.42);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
  scrollbar-width: thin;
}

*::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

*::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb {
  min-height: 44px;
  border: 2px solid var(--scrollbar-track);
  border-radius: 999px;
  background: var(--scrollbar-thumb);
}

*::-webkit-scrollbar-thumb:hover {
  background: var(--scrollbar-thumb-hover);
}

*::-webkit-scrollbar-corner {
  background: var(--scrollbar-track);
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  overflow-x: hidden;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:disabled,
input:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: clamp(16px, 3vw, 32px);
  background: var(--bg);
}

.login-panel {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 28px;
  width: min(460px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: clamp(28px, 6vw, 58px);
  box-shadow: var(--shadow);
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: var(--ink);
  font-size: 1.02rem;
  font-weight: 800;
  letter-spacing: 0;
}

.brand-icon {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 8px;
  background: var(--teal);
  color: var(--bg);
  font-weight: 800;
}

.login-copy h1 {
  max-width: 10ch;
  margin: 0 0 14px;
  color: var(--ink);
  font-size: clamp(2.4rem, 6vw, 4rem);
  line-height: 1;
  letter-spacing: 0;
}

.login-copy p {
  max-width: 39rem;
  margin: 0;
  color: var(--muted);
  font-size: 1.02rem;
  line-height: 1.7;
}

.login-form {
  display: grid;
  gap: 14px;
  max-width: 420px;
}

.field-label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.text-input,
.search-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--ink);
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  outline: 0;
  transition:
    border-color 160ms ease,
    box-shadow 160ms ease,
    background 160ms ease;
}

.text-input,
.search-input {
  min-height: 44px;
  padding: 0 13px;
}

.text-input:focus,
.search-input:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.13);
}

.primary-btn,
.tiny-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  border-radius: 8px;
  font-weight: 750;
  line-height: 1;
}

.primary-btn {
  background: var(--teal);
  color: var(--bg);
  padding: 0 16px;
}

.primary-btn:hover {
  background: var(--teal-strong);
}

.tiny-btn {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  background: var(--button-bg);
  color: var(--ink);
  font-size: 0.82rem;
}

.error-text {
  min-height: 20px;
  margin: 0;
  color: var(--error-text);
  font-size: 0.9rem;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: grid;
  grid-template-columns: minmax(156px, 220px) minmax(260px, 680px) minmax(140px, auto);
  gap: clamp(14px, 2vw, 24px);
  align-items: center;
  min-height: 72px;
  padding: 12px clamp(18px, 3vw, 36px);
  border-bottom: 1px solid var(--line);
  background: var(--topbar-bg);
  backdrop-filter: blur(12px);
}

.topbar .brand-mark {
  min-width: 150px;
}

.search-wrap {
  position: relative;
  width: 100%;
  justify-self: center;
}

.search-input {
  padding-left: 42px;
  padding-right: 76px;
  background: var(--panel);
}

.search-symbol {
  position: absolute;
  top: 50%;
  left: 14px;
  color: var(--muted);
  pointer-events: none;
  transform: translateY(-50%);
}

.search-hotkey {
  position: absolute;
  top: 50%;
  right: 10px;
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--subtle-bg);
  color: var(--muted);
  padding: 0 8px;
  font-size: 0.76rem;
  font-weight: 750;
  transform: translateY(-50%);
  pointer-events: none;
}

.user-actions {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.theme-toggle,
.role-badge,
.logout-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--button-bg);
  color: var(--muted);
  padding: 0;
  flex: 0 0 auto;
  transition:
    background 160ms ease,
    border-color 160ms ease,
    color 160ms ease,
    transform 160ms ease;
}

.theme-toggle:hover,
.logout-button:hover {
  background: var(--hover-bg);
  border-color: rgba(15, 118, 110, 0.34);
  color: var(--teal);
}

.theme-toggle:active,
.logout-button:active {
  transform: scale(0.96);
}

.theme-toggle[aria-pressed="true"] {
  background: var(--button-bg);
  border-color: rgba(240, 189, 105, 0.42);
  color: var(--amber);
}

.role-badge {
  background: var(--subtle-bg);
  color: var(--ink);
}

.logout-button {
  font: inherit;
}

.theme-icon,
.header-icon {
  display: block;
  width: 19px;
  height: 19px;
  fill: currentColor;
}

.theme-icon-stroke {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.main-grid {
  display: grid;
  grid-template-columns: minmax(230px, 290px) minmax(0, 1fr) minmax(320px, 376px);
  gap: clamp(16px, 1.7vw, 24px);
  width: 100%;
  margin: 0 auto;
  padding: 22px clamp(18px, 2.6vw, 36px) 64px;
}

.main-grid.viewer {
  grid-template-columns: minmax(240px, 290px) minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 88px;
  align-self: start;
  display: grid;
  gap: 14px;
  min-width: 0;
  max-height: calc(100vh - 108px);
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

article {
  min-width: 0;
}

.course-nav,
.content-toolbar,
.empty-state {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: none;
}

.course-nav {
  padding: 16px;
}

.nav-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.nav-heading.stacked {
  align-items: stretch;
  flex-direction: column;
}

.nav-heading h2 {
  margin: 2px 0 0;
  color: var(--ink);
  font-size: 1rem;
  font-weight: 850;
  letter-spacing: 0;
  text-transform: none;
}

.nav-heading span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.78rem;
}

.nav-kicker {
  margin: 0;
  color: var(--teal);
  font-size: 0.72rem;
  font-weight: 850;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.field-select-label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.field-select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--ink);
  padding: 0 36px 0 12px;
  font-size: 0.96rem;
  font-weight: 760;
  letter-spacing: 0;
  text-transform: none;
}

.category-list,
.section-list,
.result-list {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.category-item {
  margin-bottom: 14px;
}

.category-button,
.section-button,
.result-button {
  width: 100%;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.category-button {
  display: grid;
  gap: 8px;
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--panel-soft);
}

.category-button strong {
  font-size: 1rem;
}

.category-button span {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.category-button.active {
  border-color: rgba(15, 118, 110, 0.42);
  background: var(--active-field-bg);
  box-shadow: inset 3px 0 0 var(--teal);
}

.category-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.count-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 24px;
  border-radius: 999px;
  background: var(--subtle-bg);
  color: var(--muted);
  padding: 0 8px;
  font-size: 0.76rem;
  font-weight: 800;
}

.section-list {
  margin: 10px 0 0;
  padding: 0 0 0 10px;
  border-left: 1px solid var(--line);
}

.section-button {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  padding: 10px;
  border: 1px solid transparent;
}

.section-button:hover,
.category-button:hover,
.result-button:hover {
  background: var(--hover-bg);
}

.section-button.active {
  border-color: var(--line);
  background: var(--active-section-bg);
  color: var(--active-section-text);
}

.section-button strong {
  font-size: 0.9rem;
}

.section-button span {
  color: var(--muted);
  font-size: 0.77rem;
  line-height: 1.45;
}

.section-index {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--subtle-bg);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 850;
}

.section-nav-copy {
  display: grid;
  min-width: 0;
  gap: 3px;
}

.nav-empty {
  margin: 10px 0 0;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  padding: 12px;
  font-size: 0.88rem;
}

.content-shell {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 18px;
}

.content-shell > * {
  min-width: 0;
}

.section-header {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: clamp(28px, 4vw, 46px);
}

.section-copy {
  min-width: 0;
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--teal);
  font-size: 0.74rem;
  font-weight: 850;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.section-copy h1 {
  max-width: none;
  margin: 0;
  color: var(--ink);
  font-size: clamp(2rem, 4vw, 3.5rem);
  line-height: 1.04;
  letter-spacing: 0;
}

.section-copy p {
  max-width: 86ch;
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.65;
}

.content-toolbar {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 14px;
}

.section-meta {
  display: flex;
  min-width: 0;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.86rem;
}

.section-meta strong {
  color: var(--ink);
}

.reading-surface {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: none;
}

.markdown-body {
  min-width: 0;
  max-width: none;
  margin: 0;
  padding: clamp(34px, 5vw, 64px);
  color: var(--ink);
  font-size: 1.06rem;
  line-height: 1.82;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
  color: var(--ink);
  line-height: 1.18;
  letter-spacing: 0;
}

.markdown-body h1 {
  margin: 0 0 24px;
  font-size: clamp(1.8rem, 3.6vw, 2.6rem);
}

.markdown-body h2 {
  margin: 38px 0 12px;
  font-size: clamp(1.35rem, 2.4vw, 1.8rem);
}

.markdown-body h3 {
  margin: 28px 0 10px;
  font-size: 1.16rem;
}

.markdown-body p {
  margin: 0 0 18px;
}

.markdown-body ul,
.markdown-body ol {
  margin: 0 0 22px;
  padding-left: 24px;
}

.markdown-body li + li {
  margin-top: 8px;
}

.markdown-body blockquote {
  margin: 28px 0;
  border-left: 3px solid var(--amber);
  background: var(--quote-bg);
  padding: 16px 18px;
  color: var(--quote-text);
}

.markdown-body pre {
  max-width: 100%;
  overflow: auto;
  margin: 24px 0;
  border-radius: 8px;
  background: var(--code-bg);
  color: var(--code-text);
  padding: 16px 18px;
  line-height: 1.55;
}

.markdown-body code {
  border-radius: 5px;
  background: var(--inline-code-bg);
  padding: 2px 6px;
  color: var(--inline-code-text);
  font-size: 0.92em;
}

.markdown-body pre code {
  background: transparent;
  padding: 0;
  color: inherit;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.markdown-body a {
  color: var(--teal);
  font-weight: 700;
}

.markdown-body .math-block {
  max-width: 100%;
  overflow-x: auto;
  margin: 26px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  padding: 18px 20px;
}

.markdown-body .katex {
  color: var(--ink);
  font-size: 1.02em;
}

.markdown-body .katex-display {
  margin: 0;
}

.markdown-body .math-fallback {
  display: inline-block;
  max-width: 100%;
  overflow-x: auto;
  border-radius: 5px;
  background: var(--inline-code-bg);
  padding: 2px 6px;
  color: var(--inline-code-text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
  vertical-align: middle;
}

.markdown-body .math-block .math-fallback {
  display: block;
  background: transparent;
  padding: 0;
  color: var(--ink);
  white-space: pre-wrap;
}

.markdown-body .mermaid-shell {
  max-width: 100%;
  overflow-x: auto;
  margin: 28px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  padding: 18px;
}

.markdown-body .mermaid-diagram {
  display: flex;
  min-width: min-content;
  justify-content: center;
  color: var(--ink);
}

.markdown-body .mermaid-diagram svg {
  display: block;
  max-width: 100%;
  height: auto;
}

.markdown-body .mermaid-error {
  min-width: 0;
  justify-content: flex-start;
  border-radius: 6px;
  background: var(--danger-bg);
  padding: 10px 12px;
  color: var(--danger-text);
  font-size: 0.94rem;
  font-weight: 700;
}

.section-asset {
  display: block;
  margin: 26px 0;
}

.section-asset img {
  display: block;
  width: min(100%, 920px);
  max-height: 540px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
}

.asset-caption {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.86rem;
}

.asset-missing {
  display: inline-flex;
  border: 1px dashed var(--line);
  border-radius: 6px;
  background: var(--panel-soft);
  color: var(--muted);
  padding: 4px 8px;
  font-size: 0.9rem;
}

.markdown-body hr {
  height: 1px;
  margin: 32px 0;
  border: 0;
  background: var(--line);
}

.markdown-body table {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin: 24px 0;
  border-collapse: collapse;
  font-size: 0.94rem;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--line);
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}

.markdown-body th {
  background: var(--subtle-bg);
}

.search-popover {
  position: fixed;
  z-index: 30;
  top: 66px;
  left: 50%;
  width: min(620px, calc(100vw - 28px));
  max-height: min(560px, calc(100vh - 90px));
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow-popover);
  transform: translateX(-50%);
}

.search-popover header {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.search-popover h2 {
  margin: 0;
  font-size: 0.95rem;
}

.search-popover p {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.result-list {
  padding: 10px;
}

.result-button {
  display: grid;
  gap: 5px;
  padding: 12px;
}

.result-button strong {
  font-size: 0.96rem;
}

.result-button span {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.empty-state {
  padding: 28px;
  color: var(--muted);
  line-height: 1.6;
}

.empty-state h2 {
  margin: 0 0 8px;
  color: var(--ink);
}

.hidden {
  display: none !important;
}

@media (max-width: 1320px) {
  .main-grid,
  .main-grid.viewer {
    grid-template-columns: 270px minmax(0, 1fr);
  }
}

@media (max-width: 820px) {
  .login-shell {
    padding: 0;
  }

  .login-panel {
    min-height: 100vh;
    border-width: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .topbar {
    grid-template-columns: 1fr auto;
  }

  .topbar .search-wrap {
    grid-column: 1 / -1;
    order: 3;
  }

  .search-hotkey {
    display: none;
  }

  .user-actions {
    gap: 6px;
  }

  .main-grid,
  .main-grid.viewer {
    grid-template-columns: 1fr;
    padding-top: 16px;
  }

  .sidebar {
    position: static;
    max-height: none;
    overflow: visible;
  }

  .sidebar {
    order: 1;
  }

  .content-shell {
    order: 2;
  }

  .content-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .section-header {
    padding: 28px;
  }
}

@media (max-width: 520px) {
  .login-panel {
    padding: 24px;
  }

  .topbar {
    padding: 10px 12px;
  }

  .theme-toggle,
  .role-badge,
  .logout-button {
    width: 38px;
    height: 38px;
    min-height: 38px;
  }

  .brand-icon {
    width: 34px;
    height: 34px;
  }

  .topbar .brand-mark {
    min-width: 0;
  }

  .section-header {
    padding: 20px;
  }

  .section-copy h1 {
    max-width: none;
    font-size: 2rem;
  }

  .markdown-body {
    padding: 24px;
    font-size: 1rem;
    line-height: 1.74;
  }

  .markdown-body .math-block {
    margin: 22px 0;
    padding: 14px 12px;
  }

  .markdown-body .mermaid-shell {
    margin: 22px 0;
    padding: 12px;
  }

  .markdown-body pre,
  .markdown-body pre code {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }

  .primary-btn {
    flex: 1;
  }
}
