:root {
  --paper: #f3efe6;
  --ink: #1d1a16;
  --muted: #655d53;
  --line: #ddd4c4;
  --accent: #1d4a40;
  --sidebar: #e8e2d6;
  --hit: #fff1b8;
  --current: #ffd56a;
  color-scheme: light;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
  font-size: 17px;
  line-height: 1.75;
}

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

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  padding: 1.4rem 1rem 1.5rem;
  background: var(--sidebar);
  border-right: 1px solid var(--line);
}

.brand {
  display: inline-block;
  margin: 0;
  color: inherit;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-decoration: none;
}

.brand-sub {
  margin: 0.15rem 0 1rem;
  color: var(--muted);
  font-size: 0.88rem;
}

.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.sidebar a {
  display: block;
  padding: 0.4rem 0.65rem;
  border-radius: 8px;
  color: var(--ink);
  text-decoration: none;
}

.sidebar a:hover { background: rgba(255, 255, 255, 0.55); }

.sidebar a[aria-current="true"] {
  background: var(--accent);
  color: #f6f3ec;
}

.hint {
  margin: 1.4rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.6;
}

.main {
  min-width: 0;
  padding: 0 1.5rem 3rem;
}

.searchbar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin: 0 -1.5rem;
  padding: 0.75rem 1.5rem;
  background: rgba(243, 239, 230, 0.94);
  border-bottom: 1px solid transparent;
  backdrop-filter: blur(8px);
}

.searchbar:focus-within { border-bottom-color: var(--line); }

.searchbar input {
  flex: 1;
  min-width: 0;
  padding: 0.45rem 0.75rem;
  border: 1px solid #d3c9b8;
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
}

.searchbar button {
  padding: 0.4rem 0.7rem;
  border: 1px solid #d3c9b8;
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.searchbar button:disabled {
  color: #a3988a;
  cursor: default;
}

#count {
  margin: 0;
  min-width: 6.5rem;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.chapter {
  max-width: 44rem;
  margin: 0 auto;
  padding: 1.6rem 0 2.2rem;
  scroll-margin-top: 4.5rem;
}

.pager {
  max-width: 44rem;
  margin: 0 auto 1.2rem;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
}

.pager a {
  color: var(--accent);
  text-decoration: none;
}

.pager a:hover { text-decoration: underline; }

.quiz-score {
  margin: 0.8rem 0;
  font-weight: 700;
  color: var(--accent);
}

.quiz-choices { margin: 0.4rem 0 0.2rem; }

.quiz-choice {
  display: block;
  width: 100%;
  margin: 0.4rem 0;
  padding: 0.55rem 0.75rem;
  border: 1px solid #d3c9b8;
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
  line-height: 1.55;
  text-align: left;
  cursor: pointer;
}

.quiz-choice:hover:not(:disabled) { border-color: var(--accent); }

.quiz-choice.is-right {
  background: #e7f2ee;
  border-color: var(--accent);
}

.quiz-choice.is-wrong {
  background: #fdecea;
  border-color: #a33b32;
}

.quiz-choice:disabled { cursor: default; }

.quiz-why {
  margin: 0.35rem 0 1rem;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
}

.quiz-why.is-right { background: #e7f2ee; }
.quiz-why.is-wrong { background: #fdecea; }

.quiz-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 0.4rem;
}

#quiz-reset {
  padding: 0.4rem 0.8rem;
  border: 1px solid #d3c9b8;
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

h1, h2, h3 {
  line-height: 1.35;
  scroll-margin-top: 4.5rem;
}

h1 { margin: 0 0 0.4rem; font-size: 1.85rem; }
h2 { margin: 2rem 0 0.6rem; font-size: 1.35rem; }
h3 { margin: 1.5rem 0 0.45rem; font-size: 1.12rem; }

.en-title {
  margin-top: 0;
  color: var(--muted);
  font-family: Palatino, "Palatino Linotype", "Times New Roman", serif;
  font-size: 1.05rem;
}

p { margin: 0.7rem 0; }

ul, ol { margin: 0.6rem 0; padding-left: 1.4rem; }
li { margin: 0.2rem 0; }
li::marker { color: var(--accent); }

table {
  width: 100%;
  margin: 1rem 0 1.2rem;
  border-collapse: collapse;
  font-size: 0.95rem;
}

th, td {
  padding: 0.45rem 0.6rem;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th { background: #ebe4d6; font-weight: 700; }

code {
  padding: 0.05em 0.35em;
  border-radius: 4px;
  background: #e8e0d2;
  font-family: Consolas, "Microsoft JhengHei", monospace;
  font-size: 0.92em;
}

.math-block {
  margin: 0.9rem 0;
  overflow-x: auto;
  scroll-margin-top: 4.5rem;
}

.note-figure {
  margin: 1.2rem 0 1.4rem;
  scroll-margin-top: 4.5rem;
}

.note-figure img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  padding: 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.note-figure .slot { display: none; }

.note-figure.missing img { display: none; }

.note-figure.missing .slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 11rem;
  padding: 1rem;
  border: 1.5px dashed #b7ad9c;
  border-radius: 8px;
  background: #f7f3ea;
  color: var(--muted);
  text-align: center;
}

.note-figure figcaption {
  margin-top: 0.4rem;
  color: var(--muted);
  font-size: 0.92rem;
  text-align: center;
}

.is-hit { background: var(--hit); border-radius: 4px; }
tr.is-hit td { background: var(--hit); }
.is-current { background: var(--current); box-shadow: inset 3px 0 0 var(--accent); }
tr.is-current td { background: var(--current); }

footer {
  max-width: 44rem;
  margin: 0 auto;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.88rem;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky;
    z-index: 3;
    height: auto;
    padding-bottom: 0.8rem;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .sidebar nav {
    flex-direction: row;
    gap: 0.35rem;
    overflow-x: auto;
  }
  .sidebar a { white-space: nowrap; }
  .hint { display: none; }
  .searchbar { top: 0; flex-wrap: wrap; }
  #count { min-width: 0; }
}

@media print {
  .sidebar, .searchbar, footer { display: none; }
  .layout { display: block; }
  .chapter { break-before: page; max-width: none; }
  .chapter:first-of-type { break-before: auto; }
  body { background: #fff; }
}
