/* =========================================================
   CODEX MOBILE UTILITY PANEL
   ========================================================= */

@media (max-width: 1099px), (max-height: 699px) {
  #codex-content.codex-regions-index .codex-region-tile-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: clamp(10px, calc(var(--codex-mobile-image-width) * 0.028), 16px) !important;
  }

  #codex-content.codex-regions-index .codex-region-tile {
    min-width: 0;
  }

  #codex-content.codex-regions-index .codex-region-tile-name {
    font-size: clamp(0.78rem, calc(var(--codex-mobile-image-width) * 0.032), 1.02rem);
    line-height: 1.1;
  }

  #codex-content.codex-regions-index .codex-region-tile-details {
    font-size: clamp(0.68rem, calc(var(--codex-mobile-image-width) * 0.026), 0.86rem);
    line-height: 1.15;
  }

  #codex-content.codex-home {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    align-items: center !important;
    justify-items: center !important;
    align-content: center !important;
    justify-content: center !important;
    overflow: hidden !important;
  }

  #codex-home-section-buttons.codex-home-section-buttons {
    width: min(100%, calc(var(--codex-mobile-image-width, 100vw) * 0.76)) !important;
    max-width: min(520px, 88%) !important;

    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 0 !important;
    row-gap: 0 !important;

    justify-self: center !important;
    align-self: center !important;
    margin: 0 auto !important;
    padding: 0 !important;
  }

  #codex-home-section-buttons .codex-home-section-row {
    min-height: clamp(64px, calc(var(--codex-mobile-image-width, 100vw) * 0.168), 84px) !important;
    padding: clamp(10px, calc(var(--codex-mobile-image-width, 100vw) * 0.028), 15px)
             clamp(12px, calc(var(--codex-mobile-image-width, 100vw) * 0.035), 20px) !important;

    align-items: center !important;
    margin: 0 !important;
    border-radius: 0 !important;
  }

  #codex-home-section-buttons .codex-home-section-row + .codex-home-section-row {
    margin-top: 0 !important;
  }

  #codex-home-section-buttons .codex-row-main {
    gap: 2px !important;
  }

  #codex-home-section-buttons .codex-row-title {
    font-size: clamp(0.98rem, calc(var(--codex-mobile-image-width, 100vw) * 0.04), 1.26rem) !important;
    line-height: 1.05 !important;
  }

  #codex-home-section-buttons .codex-row-meta {
    font-size: clamp(0.72rem, calc(var(--codex-mobile-image-width, 100vw) * 0.027), 0.9rem) !important;
    line-height: 1.15 !important;
  }

  .codex-mobile-utility-panel {
    position: absolute;
    inset: 0;
    z-index: 10030;

    display: flex;
    align-items: center;
    justify-content: center;

    padding: max(18px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
    box-sizing: border-box;

    background: rgba(24, 13, 5, 0.18);
    backdrop-filter: blur(0.8px);

    opacity: 0;
    pointer-events: none;
    transition: opacity 160ms ease;
  }

  .codex-mobile-utility-panel.open {
    opacity: 1;
    pointer-events: auto;
  }

  .codex-mobile-utility-sheet {
    position: relative;
    isolation: isolate;

    width: min(calc(100vw - 28px), 440px);
    max-width: calc(100vw - 28px);
    max-height: min(78dvh, 620px);

    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 12px;

    box-sizing: border-box;
    padding: clamp(26px, 7vw, 38px) clamp(22px, 6vw, 34px);

    color: #2f1d10;
  }

  .codex-mobile-utility-sheet::before {
    content: "";
    position: absolute;
    inset: -36px -28px;
    z-index: -1;

    background-image: url("../assets/codex/Mobile_Panel.png");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100% 100%;

    opacity: 0.96;
    pointer-events: none;
  }

  .codex-mobile-utility-heading h3 {
    margin: 0;

    font-family: 'Marcellus SC', Georgia, serif;
    font-size: clamp(1.02rem, 5vw, 1.28rem);
    line-height: 1.15;
    text-align: center;
    letter-spacing: 0.06em;
  }

  .codex-mobile-utility-body {
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;

    display: grid;
    gap: 9px;
  }

  .codex-mobile-utility-close,
  .codex-mobile-utility-option {
    appearance: none;
    -webkit-appearance: none;

    min-height: 46px;
    box-sizing: border-box;
    padding: 8px 12px;

    border: 1px solid rgba(72, 43, 18, 0.24);
    border-radius: 4px;

    background: rgba(246, 232, 196, 0.52);
    color: #2f1d10;

    font-family: 'Marcellus SC', Georgia, serif;
    font-size: 0.96rem;
    line-height: 1;
    letter-spacing: 0.035em;
    text-align: center;
  }

  .codex-mobile-utility-option.is-active {
    background: rgba(94, 55, 22, 0.14);
    border-color: rgba(72, 43, 18, 0.42);
    font-weight: 600;
  }

  .codex-mobile-utility-close {
    justify-self: center;
    min-width: 120px;
  }
}

@media (max-width: 420px) {
  #codex-home-section-buttons.codex-home-section-buttons {
    width: min(100%, calc(var(--codex-mobile-image-width, 100vw) * 0.82)) !important;
    max-width: 92% !important;
    gap: 0 !important;
    row-gap: 0 !important;
  }

  #codex-home-section-buttons .codex-home-section-row {
    min-height: 62px !important;
    padding-block: 10px !important;
  }
}

@media (min-width: 1100px) and (min-height: 700px) {
  .codex-mobile-utility-panel {
    display: none !important;
  }
}

/* =========================================================
   MOBILE SEARCH RESULTS CLEANUP
   ========================================================= */

@media (max-width: 1099px), (max-height: 699px) {
  #codex-modal {
    --codex-mobile-content-h: 72.7%;
  }

  #codex-content,
  #codex-content.codex-search-page {
    padding-bottom: 4px !important;
  }

  .codex-mobile-search-page-shell {
    gap: 10px;
    padding-top: 8px;
    padding-bottom: 0;
  }

  .codex-mobile-search-query-summary {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: baseline;
    gap: 10px;
    margin: 0 0 2px;
    padding: 4px 2px 8px;
    font-family: 'Marcellus SC', Georgia, serif;
    font-size: clamp(0.78rem, 3.15vw, 0.92rem);
    line-height: 1.2;
    letter-spacing: 0.035em;
    color: #3a2614;
    opacity: 0.86;
    overflow: hidden;
    white-space: nowrap;
  }

  .codex-mobile-search-summary-query {
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    text-align: left;
  }

  .codex-mobile-search-summary-count {
    flex: 0 0 auto;
    white-space: nowrap;
    text-align: right;
  }

  .codex-search-results-shell {
    padding-top: 8px !important;
    padding-bottom: 4px !important;
    mask-image: linear-gradient(to bottom, transparent 0, black 18px, black calc(100% - 18px), transparent 100%) !important;
    -webkit-mask-image: linear-gradient(to bottom, transparent 0, black 18px, black calc(100% - 18px), transparent 100%) !important;
  }

  #codex-global-search-modal {
    align-items: flex-start;
    justify-content: center;
    padding-top: clamp(96px, 18dvh, 150px);
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }

  .codex-global-search-panel {
    position: relative;
    width: min(calc(100vw - 28px), 460px) !important;
    max-width: calc(100vw - 28px) !important;
    box-sizing: border-box;
    padding: clamp(18px, 5vw, 28px);
    border: none;
    border-radius: 18px;
    background: transparent;
    box-shadow: 0 12px 30px rgba(25, 12, 4, 0.28);
    isolation: isolate;
  }

  .codex-global-search-panel::before {
    content: "";
    position: absolute;
    inset: -32px -24px;
    z-index: -1;
    background-image: url("../assets/codex/Mobile_Panel_Small.png");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100% 100%;
    opacity: 0.96;
    pointer-events: none;
  }

  #codex-global-search-input {
    position: relative;
    z-index: 1;
    padding: 12px 16px;
    font-size: 1.05rem;
  }
}
