/* mux Documentation Theme — cohesive with the desktop app
   ---------------------------------------------------------
   Goals
   - Honor the app's color tokens and typography
   - Improve readability (spacing, line-length, contrast)
   - Sharpen components (sidebar, code, tables, kbd, details)
   - Keep mdBook structure intact; override with CSS variables only
*/

:root {
  /* Core tokens mirrored from src/styles/colors.tsx */
  --color-background: hsl(0deg 2.51% 10.04%);
  --color-background-secondary: hsl(0deg 0% 0%);
  --color-border: hsl(240 2% 25%);
  --color-text: hsl(0 0% 83%);
  --color-text-secondary: hsl(0 0% 42%);

  /* Brand/Mode accents */
  --color-plan-mode: hsl(210 70% 40%);
  --color-plan-mode-hover: color-mix(in srgb, var(--color-plan-mode), white 20%);
  --color-exec-mode: hsl(268.56 94.04% 55.19%);
  --color-edit-mode: hsl(120 50% 35%);
  --color-interrupted: hsl(38 92% 50%);

  /* Docs-specific tokens */
  --color-code: hsl(214.52deg 100% 71.11%);
  --link-color: rgb(62, 140, 218);
  --link-hover: var(--color-plan-mode-hover);
  --content-max-width: 78ch;
  --radius-lg: 8px;
  --radius-md: 6px;
  --radius-sm: 4px;
  --radius-xs: 3px;
  --shadow-1: 0 1px 1px hsl(0 0% 0% / 0.25);
  --shadow-2: 0 2px 10px hsl(0 0% 0% / 0.35);

  /* Typography tokens (fallbacks match src/styles/fonts.tsx) */
  --font-primary:
    system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica Neue", Arial,
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  --font-monospace:
    "Monaco", "Menlo", "Ubuntu Mono", "Consolas", "Courier New", monospace, "Apple Color Emoji",
    "Segoe UI Emoji", "Segoe UI Symbol";

  /* mdBook variable overrides */
  --bg: var(--color-background) !important;
  --fg: var(--color-text) !important;
  --sidebar-bg: var(--color-background-secondary) !important;
  --sidebar-fg: var(--color-text) !important;
  --icons: var(--color-text-secondary) !important;
  --table-alternate-bg: var(--color-background-secondary) !important;
  --line-height: 1.6;
}

/* Base layout and typography */
html,
body {
  background: var(--color-background) !important;
  color: var(--color-text) !important;
  font-family: var(--font-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

.content .page,
.content {
  line-height: var(--line-height);
  background: var(--color-background) !important;
}

.content .chapter {
  max-width: var(--content-max-width);
  margin: 0 auto;
  padding: 2rem 2.25rem;
  line-height: var(--line-height);
  letter-spacing: 0.01em;
}

/* Headings */
h2,
h3 {
  margin-block-start: 1em;
}
h4,
h5 {
  margin-block-start: 1em;
}
.content .chapter h1,
.content .chapter h2,
.content .chapter h3,
.content .chapter h4,
.content .chapter h5,
.content .chapter h6 {
  color: var(--color-text);
  border-bottom-color: var(--color-border) !important;
}

.content .chapter h1 {
  font-size: 2rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--color-border);
  margin-top: 0.5rem;
}
.content .chapter h2 {
  font-size: 1.55rem;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--color-border);
  margin-top: 2.2rem;
}
.content .chapter h3 {
  font-size: 1.25rem;
  margin-top: 1.8rem;
  color: color-mix(in srgb, var(--color-text), white 5%);
}

/* Heading anchors */
.header .anchor {
  color: var(--color-text-secondary) !important;
}
.header .anchor:focus-visible {
  outline: 2px solid var(--color-plan-mode);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}

/* Links */
a {
  color: var(--link-color) !important;
  text-underline-offset: 2px;
  text-decoration-thickness: 0.08em;
}
a:hover {
  color: var(--link-hover) !important;
  text-decoration: none;
}
a:focus-visible {
  outline: 2px solid var(--color-plan-mode);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}

/* Sidebar */
.sidebar {
  background: var(--sidebar-bg) !important;
  border-right: 1px solid var(--color-border) !important;
}
.sidebar .part-title {
  color: color-mix(in srgb, var(--color-text), white 20%) !important;
  font-weight: 700;
}
.sidebar .chapter li.chapter-item a {
  color: var(--color-text) !important;
}
.sidebar .chapter li.chapter-item a.active {
  color: var(--color-plan-mode) !important;
  background: transparent !important;
}
.sidebar .chapter li.chapter-item a:hover {
  color: var(--link-hover) !important;
  background: color-mix(in srgb, var(--color-background-secondary), white 8%);
}
.sidebar-resize-handle {
  background: var(--color-background-secondary) !important;
}

/* Menu bar */
#menu-bar {
  background: var(--color-background-secondary) !important;
  border-bottom: 1px solid var(--color-border) !important;
}
.icon-button {
  color: var(--color-text-secondary) !important;
}
.icon-button:hover {
  color: var(--color-text) !important;
}
#theme-toggle,
.theme-popup {
  display: none !important;
}

/* Search */
#searchbar {
  background: var(--color-background-secondary) !important;
  border: 1px solid var(--color-border) !important;
  color: var(--color-text) !important;
  border-radius: var(--radius-sm);
}
#searchbar:focus {
  border-color: var(--color-plan-mode) !important;
  outline: none;
  box-shadow: 0 0 0 3px hsl(from var(--color-plan-mode) h s l / 0.15);
}

/* Paragraph rhythm */
.content .chapter p {
  margin: 0.9rem 0;
}

/* Lists */
.content .chapter ul,
.content .chapter ol {
  padding-left: 1.4rem;
  margin: 0.75rem 0;
}
.content .chapter li {
  margin: 0.35rem 0;
}
.content .chapter li::marker {
  color: var(--color-text-secondary);
}

.content ul {
  margin-block-start: 0;
  line-height: 1.6;
}

.content p {
  line-height: var(--line-height);
}

/* Remove margin between p and following list */
p:has(+ ul),
p:has(+ ol) {
  margin-block-end: 0.5em;
}

/* Inline code */
:not(pre) > code {
  background: var(--color-background-secondary) !important;
  color: var(--color-code) !important;
  padding: 2px 6px;
  font-family: var(--font-monospace);
  font-size: 0.92em;
  border-radius: var(--radius-xs);
}

/* Code blocks */
pre {
  background: var(--color-background-secondary) !important;
  border: 1px solid var(--color-border) !important;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-1);
  overflow: auto;
  margin: 1rem 0;
}
pre > code {
  background: transparent !important;
  color: var(--color-code) !important;
  display: block;
  padding: 0.9rem 1rem;
  font-family: var(--font-monospace);
  white-space: pre-wrap;
}

/* Code blocks inside lists */
.content .chapter li > pre,
.content .chapter li > div > pre {
  margin: 0.75rem 0 0.9rem;
}

/* Tables */
table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  margin: 1.1em 0;
  background: var(--color-background-secondary) !important;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
}
table thead {
  background: color-mix(in srgb, var(--color-background-secondary), white 8%);
}
table thead tr:first-child th:first-child {
  border-top-left-radius: var(--radius-md);
}
table thead tr:first-child th:last-child {
  border-top-right-radius: var(--radius-md);
}
table tbody tr:last-child td:first-child {
  border-bottom-left-radius: var(--radius-md);
}
table tbody tr:last-child td:last-child {
  border-bottom-right-radius: var(--radius-md);
}
table th,
table td {
  padding: 0.75em 1em;
  border-right: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  text-align: left !important;
  color: var(--color-text) !important;
}
table th:first-child,
table td:first-child {
  border-left: none;
}
table th:last-child,
table td:last-child {
  border-right: none;
}
table thead th {
  border-bottom: 2px solid var(--color-border) !important;
}
table tbody tr:last-child td {
  border-bottom: none;
}
table th {
  font-weight: 700;
  font-size: 1.05em;
  color: color-mix(in srgb, var(--color-text), white 10%) !important;
}
table tbody tr:hover {
  background: color-mix(in srgb, var(--color-background-secondary), white 2%) !important;
}
table code {
  background: var(--color-background) !important;
  color: var(--color-code) !important;
  font-family: var(--font-monospace);
  font-size: 0.9em;
}

/* Blockquotes with GitHub-style alerts support */
blockquote {
  border-left: 3px solid var(--color-plan-mode);
  background: color-mix(in srgb, var(--color-background-secondary), white 3%);
  padding: 1rem 1.2rem;
  margin: 1.5rem 0;
  border-radius: var(--radius-md);
  color: var(--color-text);
}

blockquote p {
  margin: 0.5rem 0;
}

blockquote p:first-child {
  margin-top: 0;
}

blockquote p:last-child {
  margin-bottom: 0;
}

/* GitHub-style alert callouts */
/* NOTE: blue */
blockquote:has(p:first-child > strong:first-child:is([class*="Note"], [class*="note"])),
blockquote > p:first-child:has(strong:first-child:is([class*="Note"], [class*="note"])) {
  border-left-color: hsl(210 70% 50%);
}
blockquote:has(p:first-child > strong:first-child:is([class*="Note"], [class*="note"]))
  p:first-child
  strong:first-child,
blockquote > p:first-child strong:first-child:is([class*="Note"], [class*="note"]) {
  color: hsl(210 70% 60%) !important;
}

/* TIP: green */
blockquote:has(p:first-child > strong:first-child:is([class*="Tip"], [class*="TIP"])),
blockquote > p:first-child:has(strong:first-child:is([class*="Tip"], [class*="TIP"])) {
  border-left-color: hsl(120 50% 45%);
}
blockquote:has(p:first-child > strong:first-child:is([class*="Tip"], [class*="TIP"]))
  p:first-child
  strong:first-child,
blockquote > p:first-child strong:first-child:is([class*="Tip"], [class*="TIP"]) {
  color: hsl(120 50% 55%) !important;
}

/* IMPORTANT: purple */
blockquote:has(p:first-child > strong:first-child:is([class*="Important"], [class*="IMPORTANT"])),
blockquote > p:first-child:has(strong:first-child:is([class*="Important"], [class*="IMPORTANT"])) {
  border-left-color: hsl(268 94% 60%);
}
blockquote:has(p:first-child > strong:first-child:is([class*="Important"], [class*="IMPORTANT"]))
  p:first-child
  strong:first-child,
blockquote > p:first-child strong:first-child:is([class*="Important"], [class*="IMPORTANT"]) {
  color: hsl(268 94% 65%) !important;
}

/* WARNING: orange/yellow */
blockquote:has(p:first-child > strong:first-child:is([class*="Warning"], [class*="WARNING"])),
blockquote > p:first-child:has(strong:first-child:is([class*="Warning"], [class*="WARNING"])) {
  border-left-color: hsl(38 92% 50%);
}
blockquote:has(p:first-child > strong:first-child:is([class*="Warning"], [class*="WARNING"]))
  p:first-child
  strong:first-child,
blockquote > p:first-child strong:first-child:is([class*="Warning"], [class*="WARNING"]) {
  color: hsl(38 92% 60%) !important;
}

/* CAUTION: red */
blockquote:has(p:first-child > strong:first-child:is([class*="Caution"], [class*="CAUTION"])),
blockquote > p:first-child:has(strong:first-child:is([class*="Caution"], [class*="CAUTION"])) {
  border-left-color: hsl(0 70% 55%);
}
blockquote:has(p:first-child > strong:first-child:is([class*="Caution"], [class*="CAUTION"]))
  p:first-child
  strong:first-child,
blockquote > p:first-child strong:first-child:is([class*="Caution"], [class*="CAUTION"]) {
  color: hsl(0 70% 60%) !important;
}

/* Default strong styling in blockquotes */
blockquote strong {
  color: var(--color-plan-mode);
}

/* kbd (for keybinds page) */
kbd {
  display: inline-block;
  padding: 0.15em 0.45em;
  font-family: var(--font-monospace);
  font-size: 0.9em;
  line-height: 1.2;
  color: color-mix(in srgb, var(--color-text), white 5%);
  background: var(--color-background-secondary);
  border: 1px solid var(--color-border);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  box-shadow: inset 0 -1px 0 hsl(0 0% 0% / 0.25);
}

/* Details/Summary */
details {
  background: color-mix(in srgb, var(--color-background-secondary), white 1%);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  margin: 1rem 0;
}
details > summary {
  cursor: pointer;
  list-style: none;
  font-weight: 600;
}
details > summary::-webkit-details-marker {
  display: none;
}
details > summary::before {
  content: "▸";
  display: inline-block;
  margin-right: 0.5rem;
  color: var(--color-text-secondary);
  transition: transform 0.2s ease;
}
details[open] > summary::before {
  transform: rotate(90deg);
}

/* Navigation buttons */
.nav-chapters {
  color: var(--color-text-secondary) !important;
}
.nav-chapters:hover {
  color: var(--color-text) !important;
  background: color-mix(in srgb, var(--color-background-secondary), white 2%) !important;
}

/* Selection */
::selection {
  background: hsl(from var(--color-plan-mode) h s l / 0.25);
  color: white;
}

/* Fix mermaid blocks background to blend in */
.mermaid {
  background: var(--color-background-secondary);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 0.75rem;
}

/* Images */
.content .chapter img {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-1);
}

/* Small utilities */
.hr-dim {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 1.5rem 0;
}

/* Respect prefers-reduced-motion */
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}

/* Add logo before menu title */
.menu-title::before {
  content: "";
  display: inline-block;
  width: 32px;
  height: 32px;
  margin-right: 8px;
  vertical-align: middle;
  background-image: url("../img/logo.webp");
  background-size: contain;
  background-repeat: no-repeat;
}

/* Page TOC (Table of Contents) overrides */
@media only screen and (min-width: 1440px) {
  .pagetoc a {
    /* Reduce vertical spacing for more compact TOC */
    padding-top: 2px !important;
    padding-bottom: 2px !important;
    line-height: 1.4;
    font-size: 0.9em;
  }

  /* Tighter indentation for nested levels */
  .pagetoc .pagetoc-H2 {
    padding-left: 15px !important;
  }
  .pagetoc .pagetoc-H3 {
    padding-left: 30px !important;
  }
  .pagetoc .pagetoc-H4 {
    padding-left: 45px !important;
  }

  /* Add subtle visual hierarchy */
  .pagetoc .pagetoc-H2 a {
    font-weight: 500;
  }
  .pagetoc .pagetoc-H3 a {
    font-weight: 400;
    opacity: 0.9;
  }
  .pagetoc .pagetoc-H4 a {
    font-weight: 400;
    opacity: 0.8;
  }
}

/* Code block wrapper with line numbers and copy button (from mux app) */
.code-block-wrapper {
  position: relative;
  margin: 0.75em 0;
}

/* Code block with line numbers - each line is a grid row */
.code-block-container {
  display: grid;
  grid-template-columns: auto 1fr;
  background: var(--color-background-secondary);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 6px 0;
  font-family: var(--font-monospace);
  font-size: 12px;
  line-height: 1.6;
  overflow-x: auto;
}

.line-number {
  background: rgba(0, 0, 0, 0.2);
  padding: 0 8px 0 6px;
  text-align: right;
  color: rgba(255, 255, 255, 0.4);
  user-select: none;
  border-right: 1px solid rgba(255, 255, 255, 0.2);
}

.code-line {
  padding: 0 8px;
  white-space: pre;
}

.code-line code {
  background: transparent;
  padding: 0;
}

/* Reusable copy button styles */
.copy-button {
  padding: 6px 8px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  transition:
    color 0.2s,
    background 0.2s,
    border-color 0.2s;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.copy-button:hover {
  background: rgba(0, 0, 0, 0.8);
  color: rgba(255, 255, 255, 0.9);
  border-color: rgba(255, 255, 255, 0.2);
}

.copy-icon {
  width: 14px;
  height: 14px;
}

.copy-feedback {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.9);
}

/* Code block specific positioning */
.code-copy-button {
  position: absolute;
  bottom: 8px;
  right: 8px;
  opacity: 0;
  transition: opacity 0.2s;
}

.code-block-wrapper:hover .code-copy-button {
  opacity: 1;
}

/* Hide mdBook's default highlight.js styles for transformed blocks */
.code-block-wrapper pre,
.code-block-wrapper .hljs {
  display: none;
}
