:root {
  --green: #04aa6d;
  --green-dark: #059862;
  --green-soft: #d9f2e6;
  --bg: #ffffff;
  --text: #1a1a1a;
  --muted: #555;
  --border: #e5e5e5;
  --sidebar-w: 260px;
  --topbar-h: 56px;
  --code-bg: #f1f1f1;
  --note-bg: #ffffcc;
  --tip-bg: #e7f3fe;
  --example-bg: #e7f7ef;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
}

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

a:hover {
  text-decoration: underline;
}

/* Top bar */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  background: var(--green);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem;
  z-index: 1000;
}

.topbar .brand {
  font-weight: 700;
  font-size: 1.15rem;
  color: #fff;
  white-space: nowrap;
  text-decoration: none;
}

.topbar .brand:hover {
  text-decoration: none;
  opacity: 0.95;
}

.menu-btn {
  display: none;
  background: transparent;
  border: 2px solid #fff;
  color: #fff;
  border-radius: 4px;
  padding: 0.25rem 0.6rem;
  cursor: pointer;
  font-size: 1rem;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
  margin-left: auto;
  max-width: 320px;
  width: 100%;
  height: 36px;
  padding: 0 0.35rem 0 0.75rem;
  background: rgba(255, 255, 255, 0.94);
  border: 2px solid transparent;
  border-radius: 999px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.search-wrap:hover {
  background: #fff;
}

.search-wrap:focus-within {
  background: #fff;
  border-color: rgba(255, 255, 255, 0.9);
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.35), 0 2px 8px rgba(0, 0, 0, 0.12);
}

.search-wrap .search-icon {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  color: #6b7280;
  pointer-events: none;
  transition: color 0.15s ease;
}

.search-wrap:focus-within .search-icon,
.search-wrap.has-query .search-icon {
  color: var(--green-dark);
}

.search-wrap input {
  flex: 1;
  min-width: 0;
  height: 100%;
  padding: 0 0.55rem 0 0.5rem;
  border: none;
  border-radius: 999px;
  font-size: 0.92rem;
  font-family: inherit;
  color: var(--text);
  background: transparent;
  outline: none;
  box-shadow: none;
}

.search-wrap input::placeholder {
  color: #8a8a8a;
}

/* Native clear (x) on type=search */
.search-wrap input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
  height: 14px;
  width: 14px;
  margin-right: 0.2rem;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M18 6L6 18M6 6l12 12'/%3E%3C/svg%3E")
    center / contain no-repeat;
  cursor: pointer;
  opacity: 0.7;
}

.search-wrap input[type="search"]::-webkit-search-cancel-button:hover {
  opacity: 1;
}

.sidebar .search-status {
  margin: 0.35rem 0.85rem 0.5rem;
  padding: 0.4rem 0.65rem;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--green-dark);
  background: var(--green-soft);
  border-radius: 6px;
}

.sidebar .search-status.empty {
  color: #7a5a00;
  background: var(--note-bg);
  font-weight: 500;
}

/* Layout */
.layout {
  display: flex;
  padding-top: var(--topbar-h);
  min-height: 100vh;
}

.sidebar {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  overflow-y: auto;
  background: #f1f1f1;
  border-right: 1px solid var(--border);
  padding: 0.75rem 0 2rem;
  z-index: 900;
}

.sidebar h3 {
  margin: 1rem 1rem 0.35rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #777;
}

.sidebar a {
  display: block;
  padding: 0.35rem 1rem;
  color: #000;
  font-size: 0.92rem;
  border-left: 4px solid transparent;
}

.sidebar a:hover {
  background: #ddd;
  text-decoration: none;
}

.sidebar a.active {
  background: #ccc;
  border-left-color: var(--green);
  font-weight: 600;
}

.sidebar a.hidden {
  display: none;
}

.sidebar.has-filter a:not(.hidden) {
  border-left-color: var(--green-soft);
}

.sidebar.has-filter a:not(.hidden):hover,
.sidebar.has-filter a.active {
  border-left-color: var(--green);
}

.sidebar .chapter-label {
  margin: 0.85rem 1rem 0.25rem;
  font-weight: 700;
  font-size: 0.88rem;
  color: #333;
}

.sidebar .nav-group {
  margin: 0.15rem 0;
}

.sidebar .nav-group-head {
  display: flex;
  align-items: stretch;
  gap: 0;
}

.sidebar .nav-group-toggle {
  flex: 0 0 1.75rem;
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  color: #555;
  cursor: pointer;
  font-size: 0.7rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sidebar .nav-group-toggle::before {
  content: "▸";
  display: block;
  transition: transform 0.15s ease;
}

.sidebar .nav-group.is-open .nav-group-toggle::before {
  transform: rotate(90deg);
}

.sidebar .nav-group-toggle:hover {
  color: #000;
  background: #e8e8e8;
}

.sidebar a.nav-group-link {
  flex: 1;
  border-left: none;
  padding-left: 0.35rem;
  font-weight: 600;
}

.sidebar .nav-group-body {
  display: none;
  padding-left: 0.5rem;
}

.sidebar .nav-group.is-open .nav-group-body {
  display: block;
}

.sidebar .nav-group-body a {
  font-size: 0.88rem;
  padding-left: 1.1rem;
}

.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  max-width: 920px;
  padding: 1.5rem 2rem 4rem;
}

.main h1 {
  font-size: 2rem;
  margin: 0 0 0.75rem;
  font-weight: 700;
}

.main h2 {
  font-size: 1.4rem;
  margin: 1.75rem 0 0.6rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.25rem;
}

.main h3 {
  font-size: 1.15rem;
  margin: 1.25rem 0 0.5rem;
}

.main p,
.main li {
  color: var(--text);
}

.lead {
  font-size: 1.05rem;
  color: var(--muted);
  margin-bottom: 1.25rem;
}

/* Cards */
.box {
  border-left: 5px solid var(--green);
  background: var(--green-soft);
  padding: 0.9rem 1.1rem;
  margin: 1.1rem 0;
  border-radius: 0 4px 4px 0;
}

.box.definition {
  background: var(--green-soft);
  border-left-color: var(--green);
}

.box.example {
  background: var(--example-bg);
  border-left-color: #2e8b57;
}

.box.note {
  background: var(--note-bg);
  border-left-color: #ff9800;
}

.box.tip {
  background: var(--tip-bg);
  border-left-color: #2196f3;
}

.box.solution {
  background: #f3e8ff;
  border-left-color: #7c3aed;
}

.reveal-answer {
  margin: 0.35rem 0 1.25rem;
  border: 1px dashed #c4b5fd;
  border-radius: 8px;
  background: #faf8ff;
  overflow: hidden;
}

.reveal-answer > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  padding: 0.65rem 1rem;
  font-weight: 600;
  font-size: 0.92rem;
  color: #6d28d9;
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.reveal-answer > summary::-webkit-details-marker {
  display: none;
}

.reveal-answer > summary::before {
  content: "▸";
  font-size: 0.85rem;
  transition: transform 0.15s ease;
}

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

.reveal-answer > summary:hover {
  background: #f3e8ff;
}

.reveal-answer .reveal-open {
  display: none;
}

.reveal-answer[open] .reveal-closed {
  display: none;
}

.reveal-answer[open] .reveal-open {
  display: inline;
}

.reveal-answer .box.solution {
  margin: 0;
  border-radius: 0;
  border-left-width: 5px;
  border-top: 1px solid #e9d5ff;
}

.reveal-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.5rem 0 1rem;
}

.btn-reveal {
  padding: 0.4rem 0.85rem;
  border-radius: 6px;
  border: 1px solid #c4b5fd;
  background: #fff;
  color: #6d28d9;
  font: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
}

.btn-reveal:hover {
  background: #f3e8ff;
}

.final-answer {
  margin: 0.75rem 0 0;
  font-weight: 700;
  font-size: 1.05rem;
}

.download-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin: 1rem 0 1.25rem;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  background: var(--green);
  color: #fff !important;
  font-weight: 600;
  font-size: 0.92rem;
  text-decoration: none !important;
  border: 2px solid var(--green);
}

.btn-download:hover {
  background: var(--green-dark);
  border-color: var(--green-dark);
}

.btn-download.secondary {
  background: #fff;
  color: var(--green-dark) !important;
}

.btn-download.secondary:hover {
  background: var(--green-soft);
}

.exam-toc {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin: 1rem 0 1.5rem;
}

.exam-toc a {
  display: grid;
  grid-template-columns: 2.2rem 1fr;
  grid-template-rows: auto auto;
  column-gap: 0.75rem;
  row-gap: 0.15rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: inherit;
  text-decoration: none !important;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.exam-toc a:hover {
  border-color: var(--green);
  background: var(--green-soft);
}

.exam-toc .exam-letter {
  grid-row: 1 / span 2;
  align-self: center;
  width: 2.2rem;
  height: 2.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  font-weight: 700;
}

.exam-toc .exam-name {
  font-weight: 700;
  font-size: 1.02rem;
}

.exam-toc .exam-topics {
  font-size: 0.85rem;
  color: var(--muted);
}

.exam-table-wrap {
  overflow-x: auto;
  margin: 1rem 0;
}

.exam-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}

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

.exam-table th {
  background: #f5f5f5;
}

.exam-table tr:nth-child(even) td {
  background: #fafafa;
}

.muted-note {
  font-size: 0.88rem;
  color: var(--muted);
  margin-top: -0.35rem;
}

.main pre {
  background: var(--code-bg);
  border-radius: 6px;
  padding: 0.85rem 1rem;
  overflow-x: auto;
  font-size: 0.88rem;
  line-height: 1.45;
}

.main pre code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.box .box-title {
  font-weight: 700;
  margin: 0 0 0.4rem;
  color: #000;
}

.box p:last-child,
.box ul:last-child {
  margin-bottom: 0;
}

.takeaways {
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem 1.2rem;
  margin: 1.5rem 0;
}

.takeaways h3 {
  margin-top: 0;
}

pre,
code {
  font-family: Consolas, "Courier New", monospace;
}

code {
  background: var(--code-bg);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  font-size: 0.92em;
}

pre {
  background: var(--code-bg);
  padding: 1rem;
  overflow-x: auto;
  border-radius: 4px;
  border-left: 4px solid var(--green);
}

pre code {
  background: none;
  padding: 0;
}

.formula {
  background: #fafafa;
  border: 1px solid var(--border);
  padding: 1rem;
  margin: 1rem 0;
  text-align: center;
  border-radius: 4px;
  overflow-x: auto;
}

.figure {
  margin: 1.4rem 0;
  text-align: center;
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem 0.75rem 0.75rem;
}

.figure svg {
  max-width: 100%;
  width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
  min-height: 120px;
}

.figure-caption {
  font-size: 0.9rem;
  color: var(--muted);
  margin: 0.6rem 0 0;
  line-height: 1.4;
}

table {
  border-collapse: collapse;
  width: 100%;
  margin: 1rem 0;
}

th,
td {
  border: 1px solid #ccc;
  padding: 0.5rem 0.75rem;
  text-align: left;
}

th {
  background: var(--green);
  color: #fff;
}

tr:nth-child(even) {
  background: #f9f9f9;
}

/* Nav bottom */
.page-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 2.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.page-nav a {
  display: inline-block;
  background: var(--green);
  color: #fff;
  padding: 0.65rem 1.1rem;
  border-radius: 4px;
  font-weight: 600;
  text-decoration: none;
  max-width: 48%;
}

.page-nav a:hover {
  background: var(--green-dark);
  text-decoration: none;
}

.page-nav a.disabled {
  visibility: hidden;
}

.page-nav .prev {
  text-align: left;
}

.page-nav .next {
  text-align: right;
  margin-left: auto;
}

/* Home */
.hero {
  background: linear-gradient(135deg, #04aa6d 0%, #028a58 100%);
  color: #fff;
  padding: 2rem;
  border-radius: 8px;
  margin-bottom: 1.5rem;
}

.hero h1 {
  color: #fff;
  margin: 0 0 0.5rem;
}

.hero p {
  margin: 0 0 1.25rem;
  opacity: 0.95;
  font-size: 1.1rem;
}

.btn {
  display: inline-block;
  background: #fff;
  color: var(--green-dark);
  font-weight: 700;
  padding: 0.7rem 1.3rem;
  border-radius: 4px;
  text-decoration: none;
}

.btn:hover {
  background: #f0f0f0;
  text-decoration: none;
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
  margin: 1.5rem 0;
}

.chapter-card {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.chapter-card:hover {
  border-color: var(--green);
  box-shadow: 0 2px 8px rgba(4, 170, 109, 0.15);
  text-decoration: none;
}

.chapter-card .num {
  color: var(--green);
  font-weight: 700;
  font-size: 0.85rem;
}

.chapter-card .title {
  display: block;
  color: #000;
  font-weight: 700;
  margin: 0.25rem 0;
}

.chapter-card .desc {
  color: var(--muted);
  font-size: 0.9rem;
}

.breadcrumb {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

.breadcrumb a {
  color: var(--muted);
}

@media (max-width: 900px) {
  .menu-btn {
    display: inline-block;
  }

  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.15);
  }

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

  .main {
    margin-left: 0;
    padding: 1.25rem 1rem 3rem;
  }

  .search-wrap {
    max-width: min(200px, 42vw);
    height: 34px;
    padding-left: 0.6rem;
  }

  .search-wrap input {
    font-size: 0.85rem;
    padding-left: 0.4rem;
  }

  .topbar .brand-short {
    display: inline;
  }

  .topbar .brand-full {
    display: none;
  }
}

@media (min-width: 901px) {
  .topbar .brand-short {
    display: none;
  }
}

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  top: var(--topbar-h);
  background: rgba(0, 0, 0, 0.35);
  z-index: 850;
}

body.sidebar-open .overlay {
  display: block;
}

@media (min-width: 901px) {
  body.sidebar-open .overlay {
    display: none;
  }
}
