/*
 * Standalone styles for static book pages.
 * Keep the visual language aligned with apps/web/app/globals.css without
 * depending on Tailwind's runtime CDN or the Next.js application bundle.
 */

:root {
  --book-background: hsl(220 20% 98%);
  --book-foreground: hsl(222 47% 11%);
  --book-card: hsl(0 0% 100%);
  --book-muted: hsl(215 20% 45%);
  --book-border: hsl(214 32% 88%);
  --book-primary: hsl(221 83% 53%);
  --book-code-background: hsl(222 47% 11%);
  --book-code-foreground: hsl(210 40% 98%);
  --book-sidebar: hsl(220 20% 96%);
}

@media (prefers-color-scheme: dark) {
  :root {
    --book-background: hsl(220 25% 10%);
    --book-foreground: hsl(210 40% 98%);
    --book-card: hsl(220 25% 14%);
    --book-muted: hsl(215 20% 68%);
    --book-border: hsl(217 33% 25%);
    --book-primary: hsl(217 91% 65%);
    --book-code-background: hsl(222 47% 5%);
    --book-code-foreground: hsl(210 40% 98%);
    --book-sidebar: hsl(220 25% 13%);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body.book-body {
  background: var(--book-background);
  color: var(--book-foreground);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
  line-height: 1.6;
}

a {
  color: inherit;
}

.book-topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 3.5rem;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--book-border);
  background: color-mix(in srgb, var(--book-card) 94%, transparent);
  backdrop-filter: blur(12px);
}

.book-breadcrumb {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  font-size: 0.875rem;
}

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

.book-breadcrumb a,
.book-toc a {
  color: var(--book-foreground);
  text-decoration: none;
}

.book-breadcrumb a:hover,
.book-toc a:hover {
  color: var(--book-primary);
  text-decoration: underline;
}

.book-current-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--book-foreground);
  font-weight: 600;
}

.book-layout {
  display: grid;
  grid-template-columns: minmax(13rem, 17rem) minmax(0, 1fr);
  min-height: calc(100vh - 3.5rem);
}

.book-sidebar {
  position: sticky;
  top: 3.5rem;
  align-self: start;
  height: calc(100vh - 3.5rem);
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
  padding: 1.25rem 1rem;
  border-right: 1px solid var(--book-border);
  background: var(--book-sidebar);
}

.book-sidebar-title {
  margin-bottom: 0.75rem;
  color: var(--book-foreground);
  font-weight: 700;
}

.book-toc,
.book-toc ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.book-toc li {
  margin: 0.2rem 0;
}

.book-toc li > ul {
  margin: 0.25rem 0 0.45rem 0.55rem;
  padding-left: 0.6rem;
  border-left: 1px solid var(--book-border);
}

.book-toc-label {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  padding: 0.2rem 0.35rem;
  border-radius: 0.35rem;
  color: var(--book-muted);
  font-size: 0.875rem;
  line-height: 1.35;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.book-toc-section {
  color: var(--book-foreground);
  font-weight: 700;
}

.book-toc-label.current {
  background: color-mix(in srgb, var(--book-primary) 12%, transparent);
  color: var(--book-primary);
  font-weight: 700;
}

.book-toc-folder {
  color: var(--book-foreground);
  font-weight: 700;
  cursor: default;
}

.book-mobile-toc {
  display: none;
}

.book-main {
  min-width: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 3rem clamp(1.25rem, 5vw, 4rem);
  /* The grid column owns the full area after the sidebar; the reading
   * measure is centered inside that area instead of centering a max-width
   * grid item, which becomes visually biased on very wide screens. */
  display: flex;
  justify-content: center;
}

.book-content {
  width: 100%;
  max-width: 52rem;
  margin: 0 auto;
}

.book-title {
  margin: 0 0 0.75rem;
  color: var(--book-foreground);
  font-size: clamp(1.9rem, 4vw, 2.75rem);
  line-height: 1.15;
  letter-spacing: -0.025em;
}

.book-description {
  margin: 0 0 2rem;
  color: var(--book-muted);
  font-size: 1.125rem;
}

.book-author {
  margin: -1.25rem 0 2rem;
  color: var(--book-muted);
  font-size: 0.9rem;
}

.book-cover {
  width: min(22rem, 100%);
  aspect-ratio: 16 / 9;
  margin: 0 0 2rem;
}

.book-cover img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  border: 1px solid var(--book-border);
  border-radius: 0.65rem;
  box-shadow: 0 18px 35px color-mix(in srgb, var(--book-foreground) 18%, transparent);
}

.book-prose {
  color: var(--book-foreground);
  font-size: 1.05rem;
}

.book-prose h1,
.book-prose h2,
.book-prose h3,
.book-prose h4,
.book-prose h5,
.book-prose h6 {
  margin: 2rem 0 0.8rem;
  color: var(--book-foreground);
  line-height: 1.25;
}

.book-prose h1 { font-size: 2rem; }
.book-prose h2 { padding-bottom: 0.4rem; border-bottom: 1px solid var(--book-border); font-size: 1.65rem; }
.book-prose h3 { font-size: 1.35rem; }
.book-prose h4 { font-size: 1.15rem; }

.book-prose p {
  margin: 0 0 1rem;
}

.book-prose a {
  color: var(--book-primary);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.book-prose ul,
.book-prose ol {
  margin: 0 0 1rem;
  padding-left: 1.5rem;
}

.book-prose li {
  margin: 0.25rem 0;
}

.book-prose blockquote {
  margin: 1rem 0;
  padding: 0.25rem 1rem;
  border-left: 4px solid var(--book-border);
  color: var(--book-muted);
  font-style: italic;
}

.book-prose hr {
  margin: 2rem 0;
  border: 0;
  border-top: 1px solid var(--book-border);
}

.book-prose img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.25rem auto;
  border-radius: 0.5rem;
}

.book-prose table {
  display: block;
  width: 100%;
  margin: 1.25rem 0;
  overflow-x: auto;
  border-collapse: collapse;
}

.book-prose th,
.book-prose td {
  min-width: 8rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--book-border);
  text-align: left;
  vertical-align: top;
}

.book-prose th {
  background: var(--book-sidebar);
  font-weight: 700;
}

/*
 * book-* presentation blocks.  A directive is a semantic wrapper around
 * regular Markdown; these rules provide the shared layout vocabulary without
 * coupling chapters to a framework-specific component.
 */
.book-prose .book-block {
  --book-block-min-width: 0px;
  margin: 1.5rem 0;
}

.book-prose .book-width-content {
  width: fit-content;
  max-width: 100%;
}

.book-prose .book-width-wide,
.book-prose .book-width-full {
  width: calc(100% + clamp(2rem, 8vw, 8rem));
  max-width: calc(100% + clamp(2rem, 8vw, 8rem));
  margin-right: calc(-1 * clamp(1rem, 4vw, 4rem));
  margin-left: calc(-1 * clamp(1rem, 4vw, 4rem));
}

.book-prose .book-width-full {
  width: calc(100% + clamp(3rem, 14vw, 14rem));
  max-width: calc(100% + clamp(3rem, 14vw, 14rem));
  margin-right: calc(-1 * clamp(1.5rem, 7vw, 7rem));
  margin-left: calc(-1 * clamp(1.5rem, 7vw, 7rem));
}

.book-prose .book-max-width-content { max-width: 52rem; }
.book-prose .book-max-width-main { max-width: 66rem; }
.book-prose .book-max-width-viewport { max-width: none; }

.book-prose .book-align-center { margin-right: auto; margin-left: auto; }
.book-prose .book-align-right { margin-left: auto; }

.book-prose .book-table-block {
  overflow: visible;
}

.book-prose .book-table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
}

.book-prose .book-table-block > table,
.book-prose .book-table-block > .book-table-scroll > table {
  display: table;
  width: 100%;
  max-width: 100%;
  min-width: max(100%, var(--book-block-min-width));
  table-layout: fixed;
  margin: 0;
  overflow: visible;
}

/* Legacy direct-table markup remains scrollable while newly generated
 * markup uses .book-table-scroll so the toolbar stays outside the scroller. */
.book-prose .book-table-block > table { overflow-x: auto; }

/*
 * Wide tables use the expanded block on large screens, but their columns
 * still share that width instead of growing to the longest cell. This keeps
 * long explanations readable and reserves horizontal scrolling for content
 * that explicitly opts into it on small screens.
 */
.book-prose .book-table-block > table th,
.book-prose .book-table-block > table td,
.book-prose .book-table-block > .book-table-scroll > table th,
.book-prose .book-table-block > .book-table-scroll > table td {
  min-width: 0;
  max-width: 28rem;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
}

/*
 * A table may opt into explicit column widths when its headers have very
 * different amounts of text. The values are emitted as CSS custom
 * properties by the shared directive renderer, so the Markdown remains
 * portable and the same rules work for static and dynamic book pages.
 */
.book-prose .book-table-block[data-table-column-widths] > table tr > :nth-child(1),
.book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table tr > :nth-child(1) { width: var(--book-table-column-1-width, auto); }
.book-prose .book-table-block[data-table-column-widths] > table tr > :nth-child(2),
.book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table tr > :nth-child(2) { width: var(--book-table-column-2-width, auto); }
.book-prose .book-table-block[data-table-column-widths] > table tr > :nth-child(3),
.book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table tr > :nth-child(3) { width: var(--book-table-column-3-width, auto); }
.book-prose .book-table-block[data-table-column-widths] > table tr > :nth-child(4),
.book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table tr > :nth-child(4) { width: var(--book-table-column-4-width, auto); }
.book-prose .book-table-block[data-table-column-widths] > table tr > :nth-child(5),
.book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table tr > :nth-child(5) { width: var(--book-table-column-5-width, auto); }
.book-prose .book-table-block[data-table-column-widths] > table tr > :nth-child(6),
.book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table tr > :nth-child(6) { width: var(--book-table-column-6-width, auto); }

/* Explicitly weighted tables are wide tables, not compressed card layouts.
 * Keep a readable desktop canvas so the block's horizontal scroller is used
 * when the six columns cannot fit in the main reading column. */
@media (min-width: 801px) {
  .book-prose .book-table-block[data-table-column-widths] > table,
  .book-prose .book-table-block[data-table-column-widths] > .book-table-scroll > table {
    min-width: max(100%, 76rem);
  }
}

.book-table-toolbar {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  margin-bottom: 0.75rem;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--book-border);
  border-radius: 0.55rem;
  background: var(--book-card);
  color: var(--book-muted);
  font-size: 0.875rem;
}

.book-table-toolbar[hidden] { display: none; }
.book-table-filter {
  flex: 1 1 auto;
  min-width: 0;
}
.book-table-filter summary {
  width: fit-content;
  cursor: pointer;
  color: var(--book-foreground);
  font-weight: 700;
}
.book-table-column-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 10rem), 1fr));
  gap: 0.35rem 0.65rem;
  max-height: 12rem;
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  overflow-y: auto;
  border-top: 1px solid var(--book-border);
}
.book-table-column-option {
  display: inline-flex;
  align-items: flex-start;
  gap: 0.25rem;
  min-width: 0;
  white-space: normal;
  overflow-wrap: anywhere;
}
.book-table-toolbar-actions {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: wrap;
  gap: 0.45rem;
  align-self: flex-start;
}
.book-table-toolbar-actions button {
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--book-border);
  border-radius: 0.35rem;
  background: transparent;
  color: var(--book-foreground);
  cursor: pointer;
}
.book-table-toolbar-actions button:hover { border-color: var(--book-primary); color: var(--book-primary); }

.book-table-sticky-cell {
  position: sticky;
  z-index: 2;
  background: var(--book-card);
  box-shadow: 1px 0 0 var(--book-border);
}
.book-prose th.book-table-sticky-cell { background: var(--book-sidebar); z-index: 3; }

.book-prose .book-callout {
  padding: 1rem 1.15rem;
  border: 1px solid var(--book-border);
  border-left: 0.3rem solid var(--book-primary);
  border-radius: 0.55rem;
  background: var(--book-card);
}
.book-prose .book-callout > :first-child { margin-top: 0; }
.book-prose .book-callout > :last-child { margin-bottom: 0; }
.book-callout-title { margin-bottom: 0.45rem; color: var(--book-foreground); font-weight: 700; }
.book-callout-warning { border-left-color: hsl(35 90% 48%); }
.book-callout-history { border-left-color: hsl(270 65% 60%); }
.book-callout-source { border-left-color: hsl(160 55% 42%); }

.book-prose .book-figure-block { text-align: center; }
.book-figure-block figcaption { margin-top: 0.5rem; color: var(--book-muted); font-size: 0.875rem; }
.book-figure-source { display: block; margin-top: 0.2rem; font-size: 0.8rem; }
.book-figure-block[data-figure-zoom="true"] img { cursor: zoom-in; }
.book-figure-block.book-figure-zoomed { overflow-x: auto; }
.book-figure-block.book-figure-zoomed img { max-width: none; cursor: zoom-out; }

.book-prose .book-grid-block {
  display: grid;
  gap: 1rem;
}
.book-grid-columns-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.book-grid-columns-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.book-grid-columns-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.book-prose .book-card-block {
  padding: 1rem 1.1rem;
  border: 1px solid var(--book-border);
  border-radius: 0.6rem;
  background: var(--book-card);
}
.book-card-title { margin: 0 0 0.65rem; color: var(--book-foreground); font-size: 1.1rem; }
.book-prose .book-divider-ornament {
  height: auto;
  margin: 2.25rem auto;
  border: 0;
  text-align: center;
}
.book-prose .book-divider-ornament::after { content: '◆  ·  ◆'; color: var(--book-muted); letter-spacing: 0.4rem; }

.book-prose code {
  padding: 0.15rem 0.35rem;
  border-radius: 0.3rem;
  background: color-mix(in srgb, var(--book-border) 55%, transparent);
  font-size: 0.9em;
}

.book-prose pre {
  margin: 1.25rem 0;
  padding: 1rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid var(--book-border);
  border-radius: 0.5rem;
  background: var(--book-code-background);
  color: var(--book-code-foreground);
}

.book-prose pre code {
  display: block;
  padding: 0;
  background: transparent;
  color: inherit;
}

/* highlight.js GitHub-dark-compatible token colors used by BookReader. */
.book-prose pre code.hljs,
.book-prose code.hljs {
  color: #c9d1d9;
  background: transparent;
}
.book-prose .hljs-keyword,
.book-prose .hljs-selector-tag,
.book-prose .hljs-literal,
.book-prose .hljs-section,
.book-prose .hljs-link { color: #ff7b72; }
.book-prose .hljs-string,
.book-prose .hljs-attr,
.book-prose .hljs-attribute,
.book-prose .hljs-regexp { color: #a5d6ff; }
.book-prose .hljs-title,
.book-prose .hljs-title.class_,
.book-prose .hljs-title.function_ { color: #d2a8ff; }
.book-prose .hljs-number,
.book-prose .hljs-variable,
.book-prose .hljs-built_in { color: #79c0ff; }
.book-prose .hljs-comment,
.book-prose .hljs-quote { color: #8b949e; }
.book-prose .hljs-name,
.book-prose .hljs-selector-class,
.book-prose .hljs-selector-id { color: #7ee787; }

@media (max-width: 800px) {
  .book-topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 0.35rem;
    padding: 0.75rem 1rem;
  }

  .book-layout {
    display: block;
  }

  .book-sidebar {
    display: none;
  }

  .book-mobile-toc {
    display: block;
    margin-bottom: 1.5rem;
    border: 1px solid var(--book-border);
    border-radius: 0.5rem;
    background: var(--book-card);
  }

  .book-mobile-toc summary {
    cursor: pointer;
    padding: 0.7rem 0.9rem;
    color: var(--book-foreground);
    font-weight: 700;
  }

  .book-mobile-toc .book-toc {
    padding: 0 0.75rem 0.75rem;
  }

  .book-main {
    padding: 1.5rem 1rem 2rem;
  }

  .book-prose .book-width-wide,
  .book-prose .book-width-full {
    width: 100%;
    max-width: 100%;
    margin-right: 0;
    margin-left: 0;
  }

  /* mobile=scroll deliberately keeps a natural-width table for touch
   * panning; mobile=columns and the default mode continue to wrap in place. */
  .book-prose .book-table-block.book-mobile-scroll > table,
  .book-prose .book-table-block.book-mobile-scroll > .book-table-scroll > table {
    width: max-content;
    max-width: none;
    table-layout: auto;
  }

  .book-prose .book-table-block.book-mobile-scroll > table th,
  .book-prose .book-table-block.book-mobile-scroll > table td,
  .book-prose .book-table-block.book-mobile-scroll > .book-table-scroll > table th,
  .book-prose .book-table-block.book-mobile-scroll > .book-table-scroll > table td {
    min-width: 8rem;
    max-width: none;
    overflow-wrap: normal;
    word-break: normal;
  }

  /* Column filtering keeps the wide-table canvas for touch scrolling, but
   * uses equal widths for the currently visible columns so one custom
   * desktop ratio cannot leave the mobile selection cramped. */
  .book-prose .book-table-block.book-mobile-columns[data-table-column-widths] > table tr > *,
  .book-prose .book-table-block.book-mobile-columns[data-table-column-widths] > .book-table-scroll > table tr > * {
    width: auto;
  }

  .book-prose .book-table-block.book-mobile-columns[data-table-column-widths] > table,
  .book-prose .book-table-block.book-mobile-columns[data-table-column-widths] > .book-table-scroll > table {
    min-width: max(100%, 44rem);
  }

  .book-prose .book-grid-block {
    grid-template-columns: 1fr;
  }

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

  .book-table-filter,
  .book-table-toolbar-actions {
    width: 100%;
  }

  .book-table-toolbar-actions button {
    flex: 1 1 9rem;
  }
}

/* At medium desktop widths the wide/full presentation block must not bleed
 * back into the book sidebar.  The table keeps its own horizontal scroller,
 * so removing the negative outer margins here preserves access to every
 * column without painting over the sidebar scrollbar. */
@media (min-width: 801px) and (max-width: 1280px) {
  .book-prose .book-width-wide,
  .book-prose .book-width-full {
    width: 100%;
    max-width: 100%;
    margin-right: 0;
    margin-left: 0;
  }
}
