/* Custom overrides: keep out of themes/stellar to avoid updates overwriting changes. */

body {
  /* Undo global thin antialiasing so Latin text looks darker and less washed out. */
  -webkit-font-smoothing: auto;
  -moz-osx-font-smoothing: auto;
}

/* Twikoo OwO panel: keep overlay behavior, but make background opaque. */
.cmt-body.twikoo .OwO .OwO-body,
.l_body[layout="moments"] .twikoo .OwO .OwO-body,
.l_body[layout="moments"] .moment-comments .OwO .OwO-body {
  background: var(--bg-a100) !important;
  background-color: var(--bg-a100) !important;
  color: var(--text-p1) !important;
  opacity: 1 !important;
  backdrop-filter: none !important;
  border: 1px solid var(--block-border);
  border-radius: 12px;
  overflow: hidden;
}

/* Round inner areas if OwO uses split sections */
.cmt-body.twikoo .OwO .OwO-body .OwO-items,
.cmt-body.twikoo .OwO .OwO-body .OwO-bar,
.l_body[layout="moments"] .twikoo .OwO .OwO-body .OwO-items,
.l_body[layout="moments"] .twikoo .OwO .OwO-body .OwO-bar,
.l_body[layout="moments"] .moment-comments .OwO .OwO-body .OwO-items,
.l_body[layout="moments"] .moment-comments .OwO .OwO-body .OwO-bar {
  border-radius: 12px;
}

/* Active tab underline color */
.cmt-body.twikoo .OwO .OwO-bar .OwO-packages .OwO-package-active,
.cmt-body.twikoo .OwO .OwO-bar .OwO-packages .OwO-package.active,
.cmt-body.twikoo .OwO .OwO-bar .OwO-packages li.active,
.cmt-body.twikoo .OwO .OwO-bar .OwO-packages li.OwO-package-active,
.l_body[layout="moments"] .twikoo .OwO .OwO-bar .OwO-packages .OwO-package-active,
.l_body[layout="moments"] .twikoo .OwO .OwO-bar .OwO-packages .OwO-package.active,
.l_body[layout="moments"] .twikoo .OwO .OwO-bar .OwO-packages li.active,
.l_body[layout="moments"] .twikoo .OwO .OwO-bar .OwO-packages li.OwO-package-active,
.l_body[layout="moments"] .moment-comments .OwO .OwO-bar .OwO-packages .OwO-package-active,
.l_body[layout="moments"] .moment-comments .OwO .OwO-bar .OwO-packages .OwO-package.active,
.l_body[layout="moments"] .moment-comments .OwO .OwO-bar .OwO-packages li.active,
.l_body[layout="moments"] .moment-comments .OwO .OwO-bar .OwO-packages li.OwO-package-active {
  background: transparent !important;
  color: var(--twikoo-theme, var(--theme)) !important;
  border-bottom: 2px solid var(--twikoo-theme, var(--theme)) !important;
}

/* Markdown tables: keep header style, zebra-strip tbody rows only. */
:root {
  --table-row-bg-odd: #ffffff;
  --table-row-bg-even: #f6fff6; /* super light green */
}

:root[data-theme="dark"] {
  --table-row-bg-odd: transparent;
  --table-row-bg-even: rgba(120, 200, 120, 0.08);
}

.md-text table:not([class]) tbody tr:nth-child(odd) {
  background: var(--table-row-bg-odd);
}

.md-text table:not([class]) tbody tr:nth-child(even) {
  background: var(--table-row-bg-even);
}

/*
 * ================================================================
 * Mobile App navigation and reading fixes
 * ================================================================
 *
 * Stellar switches to its phone layout at 667px, so every rule in this
 * section uses the same breakpoint. Desktop and tablet/desktop layouts
 * above that width are therefore left unchanged.
 */

/* Generated mobile UI is hidden by default so desktop rendering is untouched. */
.app-bottom-nav,
.app-personal-nav,
.app-context-actions,
.app-search-layer {
  display: none;
}

/*
 * A leading Markdown-style title prefix such as "[Kelvin Wedge]" is rendered
 * as a separate category/topic chip on every screen size.
 *
 * This is presentation-only:
 * - the original SEO title and URL are unchanged;
 * - the excerpt remains a separate sibling and is not modified;
 * - titles without a leading [Category] are unaffected.
 */
.post-list .post-card.post .post-title .app-title-topic,
.article.banner.top h1.text.title .app-title-topic {
  display: table;
  width: fit-content;
  max-width: 100%;
  margin-bottom: 7px;
  padding: 3px 9px;
  border-radius: 999px;
  color: var(--theme);
  background: color-mix(in srgb, var(--theme) 12%, transparent);
  font-size: 12px;
  line-height: 1.35;
  font-weight: 700;
  letter-spacing: 0.025em;
}

.post-list .post-card.post .post-title .app-title-main,
.article.banner.top h1.text.title .app-title-main {
  display: block;
}

/*
 * Banner titles sit over an image, so give their chip a slightly stronger
 * frosted background to preserve contrast on bright and dark cover artwork.
 */
.article.banner.top h1.text.title .app-title-topic {
  color: white;
  background: color-mix(in srgb, var(--theme) 72%, rgba(0, 0, 0, 0.22));
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.16);
}

@media screen and (max-width: 667px) {
  :root {
    /* Shared dimensions make bottom spacing and the fixed bar agree. */
    --app-bottom-nav-height: 60px;
    --app-bottom-nav-gap: 10px;
  }

  /*
   * Replace Stellar's two floating sidebar buttons on phones.
   * The new bottom navigation provides Home, Search, Proj and Bio instead,
   * removing the previous 48×96px block that covered article content.
   */
  .float-panel {
    display: none !important;
  }

  /*
   * Reserve space below the main content so the fixed navigation never
   * covers the final paragraph, paginator, comments or footer.
   * safe-area-inset-bottom handles iPhones with a Home Indicator.
   */
  .l_body .l_main {
    padding-bottom: calc(
      var(--gap-max) + var(--app-bottom-nav-height) +
      var(--app-bottom-nav-gap) * 2 + env(safe-area-inset-bottom, 0px)
    );
  }

  /*
   * Stellar deliberately makes page-level banners edge-to-edge and square on
   * phones. Profile's tag banner sits inside the padded article and therefore
   * already has the desired inset-card appearance.
   *
   * Apply that same visual treatment to direct page banners used by normal
   * posts, Wiki articles and Moments:
   * - 16px space on both sides;
   * - rounded, clipped corners;
   * - automatic width so the margins do not create horizontal overflow.
   *
   * This selector does not target `.tag-plugin.banner`, so Profile retains its
   * existing dimensions. The rule is mobile-only, leaving desktop untouched.
   */
  .l_main > .article.banner.top {
    width: auto;
    margin-right: 1rem;
    margin-left: 1rem;
    border-radius: 18px;
    overflow: hidden;
  }

  /*
   * Five blog tabs must share the phone width after adding Topics.
   *
   * The labels have very different lengths, so equal columns make
   * "Categories" truncate even though "Tags" leaves unused space. Use
   * proportional widths instead:
   * - Categories and Archives receive more room;
   * - Tags receives less room;
   * - Recent and Topics retain a normal share.
   *
   * This preserves the full labels without making every tab unnecessarily
   * small. It remains mobile-only and does not change the desktop navigation.
   */
  .navbar.top nav.post {
    display: flex;
    width: 100%;
  }

  .navbar.top nav.post > a {
    flex: 1 1 0;
    min-width: 0;
    margin: 4px 1px;
    padding: 4px 2px;
    text-align: center;
    font-size: clamp(11px, 3.15vw, 14px);
  }

  /* Recent */
  .navbar.top nav.post > a:nth-child(1) {
    flex-grow: 1;
  }

  /* Categories: the longest label needs the largest share. */
  .navbar.top nav.post > a:nth-child(2) {
    flex-grow: 1.42;
  }

  /* Tags: the shortest label can release space to its neighbours. */
  .navbar.top nav.post > a:nth-child(3) {
    flex-grow: 0.72;
  }

  /* Topics */
  .navbar.top nav.post > a:nth-child(4) {
    flex-grow: 1;
  }

  /* Archives is slightly longer than Recent and Topics. */
  .navbar.top nav.post > a:nth-child(5) {
    flex-grow: 1.18;
  }

  .app-bottom-nav {
    position: fixed;
    z-index: 1000000;
    left: 12px;
    right: 12px;
    bottom: calc(var(--app-bottom-nav-gap) + env(safe-area-inset-bottom, 0px));
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    height: var(--app-bottom-nav-height);
    padding: 4px;
    box-sizing: border-box;
    border: 1px solid color-mix(in srgb, var(--text) 10%, transparent);
    border-radius: 20px;
    background: color-mix(in srgb, var(--card) 88%, transparent);
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.16);
    -webkit-backdrop-filter: blur(18px) saturate(160%);
    backdrop-filter: blur(18px) saturate(160%);
  }

  .app-bottom-nav__item {
    appearance: none;
    -webkit-appearance: none;
    min-width: 0;
    min-height: 48px;
    margin: 0;
    padding: 3px 2px 2px;
    border: 0;
    border-radius: 15px;
    background: transparent;
    color: var(--text-p2);
    text-decoration: none;
    font: inherit;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    -webkit-tap-highlight-color: transparent;
  }

  .app-bottom-nav__item svg {
    width: 23px;
    height: 23px;
    fill: currentColor;
    flex: 0 0 auto;
  }

  /*
   * Short text remains under each icon for clarity.
   * At 10px, all four labels fit comfortably even on a 320px-wide screen.
   */
  .app-bottom-nav__item span {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 10px;
    line-height: 12px;
    font-weight: 600;
    letter-spacing: 0.02em;
  }

  .app-bottom-nav__item.is-active {
    color: var(--theme);
    background: color-mix(in srgb, var(--theme) 13%, transparent);
  }

  .app-bottom-nav__item:focus-visible {
    outline: 2px solid var(--theme);
    outline-offset: -2px;
  }

  /*
   * Context buttons preserve Stellar's original sidebars without adding more
   * permanent bottom-bar items:
   * - left button: Wiki document/article tree;
   * - right button: headings on the current page.
   * Each button is generated only when that sidebar content actually exists.
   */
  .app-context-actions {
    position: fixed;
    z-index: 1000001;
    left: 18px;
    right: 18px;
    bottom: calc(
      var(--app-bottom-nav-height) + var(--app-bottom-nav-gap) * 2 +
      env(safe-area-inset-bottom, 0px)
    );
    display: flex;
    justify-content: space-between;
    pointer-events: none;
  }

  .app-context-action {
    pointer-events: auto;
    appearance: none;
    -webkit-appearance: none;
    width: 46px;
    height: 46px;
    padding: 10px;
    border: 1px solid color-mix(in srgb, var(--text) 10%, transparent);
    border-radius: 16px;
    color: var(--text-p1);
    background: color-mix(in srgb, var(--card) 90%, transparent);
    box-shadow: 0 7px 22px rgba(0, 0, 0, 0.14);
    -webkit-backdrop-filter: blur(18px) saturate(160%);
    backdrop-filter: blur(18px) saturate(160%);
    -webkit-tap-highlight-color: transparent;
  }

  /*
   * When a page has only the right-side TOC button, keep it right-aligned.
   */
  .app-context-action--toc:first-child {
    margin-left: auto;
  }

  .app-context-action svg {
    display: block;
    width: 100%;
    height: 100%;
    fill: currentColor;
  }

  .app-context-action.is-active {
    color: var(--theme);
    background: color-mix(in srgb, var(--theme) 15%, var(--card));
  }

  /*
   * Search uses its own sheet and backdrop. It temporarily contains Stellar's
   * original #search-wrapper, so search indexing and result rendering are reused.
   */
  .app-search-layer {
    position: fixed;
    z-index: 1000002;
    inset: 0;
    pointer-events: none;
  }

  .app-search-layer.is-open {
    display: block;
    pointer-events: auto;
  }

  .app-search-layer__backdrop {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 0;
    background: rgba(0, 0, 0, 0.28);
  }

  .app-search-sheet {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: calc(
      var(--app-bottom-nav-height) + var(--app-bottom-nav-gap) * 2 +
      env(safe-area-inset-bottom, 0px)
    );
    max-height: min(70vh, 620px);
    padding: 10px;
    box-sizing: border-box;
    overflow: hidden;
    border: 1px solid color-mix(in srgb, var(--text) 10%, transparent);
    border-radius: 20px;
    background: color-mix(in srgb, var(--card) 96%, transparent);
    box-shadow: 0 14px 42px rgba(0, 0, 0, 0.24);
    -webkit-backdrop-filter: blur(22px) saturate(170%);
    backdrop-filter: blur(22px) saturate(170%);
  }

  .app-search-sheet__header {
    height: 38px;
    padding: 0 2px 6px 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--text-p1);
  }

  .app-search-sheet__header button {
    appearance: none;
    -webkit-appearance: none;
    width: 38px;
    height: 38px;
    padding: 9px;
    margin: 0;
    border: 0;
    border-radius: 12px;
    color: var(--text-p2);
    background: var(--block);
  }

  .app-search-sheet__header svg {
    width: 100%;
    height: 100%;
    fill: currentColor;
  }

  .app-search-sheet__content {
    max-height: calc(min(70vh, 620px) - 54px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .app-search-sheet .search-wrapper {
    width: auto;
    padding-bottom: 0;
  }

  .app-search-sheet #search-result {
    max-height: calc(min(70vh, 620px) - 112px);
  }

  html.app-search-open,
  html.app-search-open body {
    overflow: hidden;
  }

  /*
   * iOS zooms the viewport when an input smaller than 16px receives focus.
   * Restricting this adjustment to mobile avoids changing desktop search UI.
   */
  input.search-input {
    font-size: 16px !important;
  }

  /*
   * Search now has its own bottom sheet. When the Wiki tree sidebar opens,
   * hide the duplicate search row so that sidebar is dedicated to navigation.
   * The rule does not affect desktop, and it does not affect the search sheet
   * because #search-wrapper is temporarily moved outside `.l_left`.
   */
  .l_body[leftbar] .l_left #search-wrapper {
    display: none;
  }

  /*
   * Profile, Moments and Friends form one personal section on phones.
   *
   * The theme's desktop banner content (avatar, title, subtitle and breadcrumb)
   * remains in the generated HTML and is hidden only at this breakpoint. This
   * keeps desktop rendering unchanged and avoids editing the Stellar theme.
   */
  .app-personal-banner {
    position: relative;
    min-height: 180px;
    margin-top: calc(12px + env(safe-area-inset-top, 0px));
    overflow: hidden;
    border-radius: 18px;
  }

  /*
   * Moments normally renders an additional mobile site header containing the
   * site avatar and signature. The banner is the section header here, so that
   * duplicate header is removed on this page only.
   */
  .l_body[layout="moments"] .l_main > header.header.mobile-only {
    display: none !important;
  }

  /*
   * Hide the original banner text on phones, but do not remove it from HTML.
   * Direct-child targeting avoids hiding the new navigation placed alongside it.
   */
  .app-personal-banner > .content {
    display: none !important;
  }

  /*
   * A dark lower gradient separates the controls from the detailed banner art.
   * It improves legibility without dimming the whole illustration.
   */
  .app-personal-banner::after {
    content: "";
    position: absolute;
    z-index: 1;
    right: 0;
    bottom: 0;
    left: 0;
    height: 58%;
    pointer-events: none;
    background: linear-gradient(
      to bottom,
      transparent,
      rgba(4, 13, 20, 0.2) 35%,
      rgba(4, 13, 20, 0.7) 100%
    );
  }

  .app-personal-nav {
    position: absolute;
    z-index: 2;
    right: 10px;
    bottom: 10px;
    left: 10px;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 3px;
    padding: 4px;
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: 15px;
    background: rgba(7, 20, 29, 0.58);
    box-shadow: 0 7px 22px rgba(0, 0, 0, 0.22);
    -webkit-backdrop-filter: blur(14px) saturate(145%);
    backdrop-filter: blur(14px) saturate(145%);
  }

  .app-personal-nav__item {
    min-width: 0;
    min-height: 42px;
    padding: 7px 5px;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    color: rgba(255, 255, 255, 0.84);
    background: transparent;
    border-radius: 11px;
    text-decoration: none;
    text-shadow: 0 1px 5px rgba(0, 0, 0, 0.42);
    -webkit-tap-highlight-color: transparent;
  }

  /*
   * The active page becomes a light inset pill. It is conspicuous enough to
   * orient the user while remaining visually connected to the banner artwork.
   */
  .app-personal-nav__item.is-active {
    color: var(--theme);
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 3px 11px rgba(0, 0, 0, 0.17);
    text-shadow: none;
  }

  .app-personal-nav__item:not(.is-active):active {
    background: rgba(255, 255, 255, 0.13);
  }

  .app-personal-nav__item svg {
    width: 19px;
    height: 19px;
    flex: 0 0 auto;
    fill: currentColor;
  }

  .app-personal-nav__item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 12px;
    line-height: 1.2;
    font-weight: 700;
  }

  /*
   * Footer badges have fixed intrinsic widths and previously wrapped on phones.
   * Their height now scales with viewport width, while nowrap and zero-width
   * flex children let the browser reduce them proportionally on narrow screens.
   */
  .page-footer.footnote .text > div > p {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    gap: 2px;
    width: 100%;
    margin-right: 0;
    margin-left: 0;
    overflow: hidden;
    white-space: nowrap;
  }

  .page-footer.footnote .text > div > p > a {
    display: block;
    flex: 0 1 auto;
    min-width: 0;
    line-height: 0;
  }

  .page-footer.footnote .text > div > p > a > img {
    display: block;
    width: auto;
    max-width: 100%;
    height: clamp(12px, 4vw, 16px);
    object-fit: contain;
  }

  .page-footer.footnote .text > div > p > img {
    display: block;
    flex: 0 0 auto;
    width: clamp(10px, 3.2vw, 14px);
    height: clamp(10px, 3.2vw, 14px);
    object-fit: contain;
  }

  /*
   * The same category chip is slightly smaller on phones to save vertical space.
   */
  .post-list .post-card.post .post-title .app-title-topic,
  .article.banner.top h1.text.title .app-title-topic {
    font-size: 11px;
  }

  /*
   * Long URLs should wrap instead of widening the whole document.
   * Code blocks and tables keep their own existing horizontal scroll behavior.
   */
  article.md-text a,
  .article-footer a,
  .md-text.content a {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /*
   * MathJax display equations remain horizontally scrollable inside their own
   * box, while the article itself is clipped to the viewport width.
   */
  .l_main,
  article.md-text,
  .md-text.content {
    min-width: 0;
    max-width: 100%;
  }

  .md-text.content {
    overflow-x: clip !important;
  }

  .MathJax_SVG_Display {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
  }
}

/*
 * Very narrow phones need slightly tighter outer spacing, but the touch targets
 * stay at least 48px tall.
 */
@media screen and (max-width: 360px) {
  .app-bottom-nav {
    left: 8px;
    right: 8px;
  }

  .app-personal-nav__item {
    gap: 3px;
    padding-right: 3px;
    padding-left: 3px;
  }

  .app-personal-nav__item span {
    font-size: 11px;
  }
}
