/* Mobile-specific styles (scaffold). */
/* Matches assets/js/device.js: plain width alone misclassifies large/landscape
   phones as desktop, so touch devices without hover count as mobile too,
   capped at 1024px to exclude touch-enabled desktop monitors. */
@media (max-width: 768px), (hover: none) and (pointer: coarse) and (max-width: 1024px) {
  body[data-scale="5"] .word-node {
    width: auto !important;
    min-width: calc(270.443px * var(--word-node-scale-factor)) !important;
    max-width: none !important;
    height: calc(189.155px * var(--word-node-scale-factor)) !important;
    box-sizing: border-box !important;
  }

  body[data-scale="5"] .word-node .terms .term-ori {
    font-size: calc(49.345px * var(--word-node-scale-factor)) !important;
  }

  #proposer {
    max-width: 39vw !important;
    box-sizing: border-box !important;
    top: 17vh !important;
    left: 11vw !important;
    transform: none !important;
  }

  #proposer .proposer-text .proposer-primary {
    font-size: 32.469px !important;
    line-height: 30px !important;
  }

  #proposer .proposer-text {
    gap: 2px !important;
  }

  #proposer .proposer-text .proposer-primary,
  #proposer .proposer-text .proposer-ori {
    margin: 0 !important;
  }

  html:lang(zh) #proposer .proposer-text .proposer-primary {
    font-size: 24px !important;
    line-height: 24px !important;
    text-align: center !important;
    max-width: 80% !important;
  }

  #image {
    max-width: calc(38vw * var(--word-node-scale-factor)) !important;
    box-sizing: border-box !important;
    top: 20vh !important;
    left: 48vw !important;
    transform: none !important;
  }

  #image img,
  #image img {
    min-width: 0 !important;
    width: calc(100% - 20px) !important;
    max-width: calc(100% - 20px) !important;
    max-height: none !important;
    height: auto !important;
    padding: 0 !important;
    margin: calc(14px * var(--word-node-scale-factor)) 10px 10px !important;
  }

  #proposer .detail-title {
    margin: calc(14px * var(--word-node-scale-factor)) !important;
  }

  #proposer img {
    width: 34vw !important;
    max-width: 34vw !important;
    max-height: none !important;
    height: auto !important;
    margin: calc(14px * var(--word-node-scale-factor)) !important;
    padding: 0 !important;
  }

  #comment {
    width: 75vw !important;
    max-width: none !important;
    box-sizing: border-box !important;
    left: 40px !important;
    top: 62vh !important;
    transform: none !important;
  }

  #comment .comment-abs-content {
    font-size: calc(19.738px * var(--word-node-scale-factor)) !important;
    line-height: 24.672px !important;
  }

  #comment #comment-content > p {
    font-size: calc(12.336px * var(--word-node-scale-factor)) !important;
  }

  body[data-scale="5"] .word-node .detail-title,
  body[data-scale="5"] .detail-title {
    font-size: calc(13.159px * var(--word-node-scale-factor)) !important;
    margin: calc(14px * var(--word-node-scale-factor)) !important;
    gap: calc(6px * var(--word-node-scale-factor)) !important;
  }

  body[data-scale="5"] .word-node .detail-title::before,
  body[data-scale="5"] .detail-title::before {
    width: calc(8px * var(--word-node-scale-factor)) !important;
    height: calc(8px * var(--word-node-scale-factor)) !important;
  }

  body[data-scale="5"] .word-node .terms {
    margin: calc(14px * var(--word-node-scale-factor)) !important;
  }

  #side-menu {
    --menu-min-width: auto !important;
    --menu-gap: 0 !important;
    --menu-align: stretch !important;
    left: 0;
    right: 0;
    top: 0;
    bottom: auto;
    width: 100% !important;
    min-width: 0 !important;
    gap: 0 !important;
    align-items: stretch !important;
    pointer-events: none;
    transition: none !important;
  }

  /* Disable compact hover layout shift on mobile. */
  #side-menu.menu-compact:hover {
    --menu-min-width: auto !important;
    --menu-gap: 0 !important;
    --menu-align: stretch !important;
    width: 100% !important;
  }

  #side-menu.menu-search-lock,
  #side-menu.menu-search-lock:hover,
  #side-menu.menu-search-lock.menu-compact,
  #side-menu.menu-search-lock.menu-compact:hover {
    --menu-min-width: auto !important;
    --menu-gap: 0 !important;
    --menu-align: stretch !important;
    width: 100% !important;
  }

  #upper-menu {
    width: 100%;
    padding: calc(env(safe-area-inset-top, 0px) + 12px) 14px 0;
    box-sizing: border-box;
    pointer-events: none;
  }

  #dunes-icon {
    position: fixed;
    top: 60px;
    left: 20px;
    column-gap: 4px !important;
    z-index: 11010;
    pointer-events: auto;
  }

  #upper-menu .Chinese {
    font-size: 18.722px !important;
  }

  #upper-menu .English {
    font-size: 13.846px !important;
  }

  #buttons {
    --buttons-width: calc(30px * 3 + 53.79px + 8px * 3);
    position: fixed;
    top: 60px;
    right: 20px;
    z-index: 11011;
    pointer-events: auto;
    height: 30px;
  }

  #buttons.search-expanded #menu-search {
    width: 100% !important;
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
    z-index: 2 !important;
  }

  #buttons.search-expanded #menu-search input {
    opacity: 1 !important;
    width: 100% !important;
    pointer-events: auto !important;
  }

  #upper-menu .small-icon {
    width: 30px !important;
    height: 30px !important;
    border-radius: 15px !important;
  }

  #upper-menu #about-button {
    width: 53.79px !important;
    height: 30px !important;
  }

  #upper-menu #about-button p {
    font-size: 16.3px !important;
  }

  #language-icon {
    width: 17.919px !important;
    height: 16.216px !important;
  }

  #shuffle-icon {
    width: 22.82px !important;
    height: 22.82px !important;
  }

  #search-icon {
    width: 14.67px !important;
    height: 14.67px !important;
  }

  #search-results {
    position: fixed !important;
    top: calc(60px + 30px + 8px) !important;
    right: 20px !important;
    left: auto !important;
    bottom: auto !important;
    width: calc(30px * 3 + 53.79px + 8px * 3) !important;
    max-height: min(42vh, 320px) !important;
    z-index: 11020 !important;
    pointer-events: auto;
  }

  /* Hide scale bar block on mobile. */
  #lower-menu {
    display: none !important;
  }

  #year-menu {
    position: fixed;
    left: 50%;
    bottom: calc(60px + var(--app-bottom-inset));
    transform: translateX(-50%);
    width: min(
      366px,
      calc(
        100vw - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 28px
      )
    );
    margin-top: 0;
    z-index: 11010;
    pointer-events: auto;
  }

  #side-menu #year-menu {
    transform: translateX(-50%) !important;
  }

  #yearContainer {
    width: 100%;
  }

  #year-menu.mobile-hidden {
    display: none !important;
    opacity: 0 !important;
    pointer-events: none !important;
  }

  /* Keep top/bottom menu parts always visible on mobile. */
  #side-menu.menu-compact #dunes-icon,
  #side-menu.menu-compact #buttons,
  #side-menu.menu-compact #year-menu,
  #side-menu.menu-compact:hover #dunes-icon,
  #side-menu.menu-compact:hover #buttons,
  #side-menu.menu-compact:hover #year-menu {
    opacity: 1 !important;
    pointer-events: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    transform: none !important;
  }

  #side-menu.menu-compact:hover #lower-menu {
    display: none !important;
    opacity: 0 !important;
    pointer-events: none !important;
    height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
  }

  /* Prevent compact/hover rules from removing centering transform. */
  #side-menu.menu-compact #year-menu,
  #side-menu.menu-compact:hover #year-menu {
    transform: translateX(-50%) !important;
  }

  /* Disable hover-only visual feedback on mobile. */
  .menu-search-results .search-result-item:hover {
    background: transparent;
  }

  .year-indicator:hover {
    background-color: #43403B;
  }

  #side-menu .menu-part,
  #side-menu #buttons,
  #side-menu #year-menu,
  .menu-search-results .search-result-item,
  .year-indicator {
    transition: none !important;
  }

  /* Keep search usable/animated on mobile. */
  #menu-search {
    transition: width 0.35s ease, box-shadow 0.35s ease !important;
  }

  #menu-search input {
    transition: opacity 0.35s ease, width 0.35s ease !important;
  }

  /* Mobile floating/about panel: bottom-sheet overlay that slides up from
     the bottom, leaving a gap at the top of the screen for a horizontal
     tab bar (instead of the desktop side tabs). */
  #floating-panel,
  #about-panel {
    top: 56px !important;
    right: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: calc(var(--app-vh) - 56px) !important;
    border-radius: 16px 16px 0 0 !important;
    z-index: 12020 !important;
    transform: translateY(100%) !important;
    transition: transform 0.45s ease-in-out !important;
  }

  /* .panel-main's height is hardcoded to var(--app-vh) (full screen) in
     floatingPanel.css, but on mobile the panel itself is shifted down and
     shortened by 56px above. Without this override panel-main is 56px
     taller than its visible container, and that extra 56px of scrollable
     content falls below the bottom of the actual viewport where it can
     never be scrolled into view. */
  #floating-panel .panel-main,
  #about-panel .panel-main {
    height: 100% !important;
  }

  #floating-panel.hidden,
  #about-panel.hidden {
    transform: translateY(100%) !important;
  }

  #floating-panel:not(.hidden),
  #about-panel:not(.hidden) {
    transform: translateY(0) !important;
  }

  #floating-panel .panel-entry,
  #floating-panel .panel-comment,
  #about-panel .panel-entry,
  #about-panel .panel-comment {
    border-radius: 16px 16px 0 0 !important;
    overflow: visible !important;
  }

  #floating-panel .panel-entry:not(.active),
  #floating-panel .panel-comment:not(.active),
  #about-panel .panel-entry:not(.active),
  #about-panel .panel-comment:not(.active) {
    display: none !important;
    transform: none !important;
  }

  /* Horizontal tab bar sitting in the gap above the sheet, sliding up
     together with the panel. */
  #floating-panel .panel-tabs,
  #about-panel .panel-tabs {
    display: flex !important;
    position: absolute !important;
    flex-direction: row !important;
    top: -40px !important;
    left: 50% !important;
    bottom: auto !important;
    right: auto !important;
    transform: translateX(-50%) !important;
    width: auto !important;
    height: 40px !important;
    gap: 8px !important;
    z-index: 20 !important;
    pointer-events: auto !important;
  }

  #floating-panel .panel-tabs button,
  #about-panel .panel-tabs button {
    opacity: 1 !important;
    pointer-events: auto !important;
    width: auto !important;
    height: 40px !important;
    padding: 0 20px !important;
    -webkit-clip-path: none !important;
    clip-path: none !important;
    border-radius: 10px 10px 0 0 !important;
    box-shadow: none !important;
    white-space: nowrap !important;
  }

  /* Tab colour follows the tab's own identity, not whichever themed
     sub-panel (.panel-entry dark / .panel-comment light) currently
     happens to render this duplicated tab bar. On desktop the "other"
     button in each pair is invisible (opacity 0) so this never showed;
     on mobile both buttons are always visible, so 词条/ABOUT must stay
     dark-bg/light-text and 回声/DEVLOG must stay light-bg/dark-text
     regardless of active state or container. */
  #floating-panel .panel-tabs button[data-tab="entry"],
  #about-panel .panel-tabs button[data-tab="about"] {
    background: #43403B !important;
    color: #FAF4E5 !important;
  }

  #floating-panel .panel-tabs button[data-tab="comment"],
  #about-panel .panel-tabs button[data-tab="devlog"] {
    background: #EEE9DB !important;
    color: #43403B !important;
  }

  /* Desktop rotates the English label to fit the narrow vertical side
     tab (see updateTabLabels() in main.preview.js). The mobile bar is
     horizontal, so force the label upright even if that inline rotation
     is still applied (e.g. right after a resize, before the next
     language/render update runs). */
  #floating-panel .panel-tabs button span,
  #about-panel .panel-tabs button span {
    display: inline-block !important;
    transform: none !important;
  }

  #floating-panel .panel-slide,
  #about-panel .panel-slide {
    display: none !important;
  }

  #floating-panel .panel-top img {
    align-self: center !important;
    width: calc(100% - 40px) !important;
    max-width: calc(100% - 40px) !important;
    margin-left: 20px !important;
    margin-right: 20px !important;
    box-sizing: border-box !important;
  }

  #floating-panel .panel-bottom .diagram-caption,
  #about-panel .panel-bottom .diagram-caption {
    color: #FAF4E5;
    text-align: justify;
    font-family: "ChillDINGothic";
    font-size: 15px;
    font-style: normal;
    font-weight: 400;
    line-height: 20px;
    letter-spacing: 0.75px;
  }

  #floating-panel #section-example .diagram-stage,
  #about-panel #section-example .diagram-stage {
    --diagram-arrow-slot-mobile: 56px;
    padding: 24px var(--diagram-arrow-slot-mobile) 12px var(--diagram-arrow-slot-mobile) !important;
    height: auto !important;
    min-height: 0 !important;
    align-items: flex-end !important;
  }

  #floating-panel .panel-comment .note-images-carousel .diagram-stage,
  #about-panel .panel-comment .note-images-carousel .diagram-stage {
    --diagram-arrow-slot-mobile: 56px;
    padding: 26px var(--diagram-arrow-slot-mobile) 0 var(--diagram-arrow-slot-mobile) !important;
    height: auto !important;
    min-height: 0 !important;
    align-items: flex-end !important;
  }

  #floating-panel .panel-comment .note-body img,
  #about-panel .panel-comment .note-body img {
    padding: 24px 0 0 0 !important;
  }

  #floating-panel #section-example .diagram-image,
  #about-panel #section-example .diagram-image,
  #floating-panel .panel-comment .note-images-carousel .diagram-image,
  #about-panel .panel-comment .note-images-carousel .diagram-image {
    height: auto !important;
    max-height: 300px !important;
    object-position: center bottom !important;
    padding: 0 !important;
  }

  #floating-panel .panel-bottom .diagram-caption,
  #about-panel .panel-bottom .diagram-caption {
    margin-top: 0 !important;
    padding-top: 0 !important;
  }

  #floating-panel .panel-bottom section,
  #about-panel .panel-bottom section {
    flex-direction: column !important;
    align-items: flex-start !important;
  }

  #floating-panel .panel-bottom .left-title,
  #about-panel .panel-bottom .left-title {
    width: auto !important;
  }

  #floating-panel .panel-comment .panel-bottom .left-title,
  #about-panel .panel-comment .panel-bottom .left-title {
    --comment-title-line-height-mobile: 20px;
    line-height: var(--comment-title-line-height-mobile) !important;
    padding-bottom: calc(var(--comment-title-line-height-mobile) * 2) !important;
  }

  #about-panel .panel-bottom section > div {
    padding-top: 0 !important;
  }

  #floating-panel .panel-comment .panel-bottom .note-body,
  #floating-panel .panel-comment .panel-bottom .note-body *,
  #about-panel .panel-comment .panel-bottom .note-body,
  #about-panel .panel-comment .panel-bottom .note-body * {
    text-align: left !important;
  }

  #floating-panel .panel-comment .panel-bottom .note-body > br,
  #about-panel .panel-comment .panel-bottom .note-body > br {
    display: none !important;
  }

  /* Mobile echoes: no fold/expand interaction (renderCommentSection skips
     wiring it up via isMobileLayout()) — every echo is laid out in full,
     one after another on a single continuously scrollable page, separated
     by a divider line. No hover affordances either. */
  #floating-panel .panel-comment .panel-bottom section:not(#section-contributors):not(#section-contact):not(#section-editors),
  #about-panel .panel-comment .panel-bottom section:not(#section-contributors):not(#section-contact):not(#section-editors) {
    max-height: none !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
    cursor: default !important;
    transform: none !important;
    transition: none !important;
    box-shadow: none !important;
    background-color: transparent !important;
    padding-top: 24px !important;
    padding-bottom: 24px !important;
    align-items: flex-start !important;
    border-top: 1px solid rgba(67, 64, 59, 0.15);
  }

  #floating-panel .panel-comment .panel-bottom section:not(#section-contributors):not(#section-contact):not(#section-editors):first-child,
  #about-panel .panel-comment .panel-bottom section:not(#section-contributors):not(#section-contact):not(#section-editors):first-child {
    border-top: none;
  }

  #floating-panel .panel-comment.active .panel-bottom section:hover,
  #about-panel .panel-comment.active .panel-bottom section:hover {
    transform: none !important;
  }

  #floating-panel .panel-comment .panel-bottom section .note-like-toggle:hover,
  #about-panel .panel-comment .panel-bottom section .note-like-toggle:hover {
    transform: none !important;
  }

  #floating-panel .panel-comment .panel-bottom section .note-body,
  #about-panel .panel-comment .panel-bottom section .note-body {
    display: block !important;
  }

  #floating-panel .panel-comment .panel-bottom section .note-like-toggle,
  #about-panel .panel-comment .panel-bottom section .note-like-toggle {
    top: 26px !important;
  }

  #floating-panel .panel-comment .panel-bottom section .left-title,
  #about-panel .panel-comment .panel-bottom section .left-title {
    padding-bottom: 16px !important;
  }

  /* The fold-navigation CTA has no purpose in the always-expanded mobile
     layout — scrolling down reaches the next echo naturally. */
  #floating-panel .panel-comment .panel-bottom .note-read-more,
  #about-panel .panel-comment .panel-bottom .note-read-more {
    display: none !important;
  }

  #floating-close-btn {
    position: absolute;
    top: calc(env(safe-area-inset-top, 0px) + 20px);
    right: 18px;
    width: 37px;
    height: 37px;
    border: none;
    border-radius: 37px;
    background: #EEE9DB;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 12030;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  #floating-close-btn .close-icon {
    width: 14.32px;
    height: 14.32px;
    display: block;
  }

  #floating-close-btn .close-icon line {
    stroke: #43403B;
    stroke-width: 2.25;
    stroke-linecap: round;
  }

  /* Relation lines are not usable on the full-screen mobile view; the
     related-words chip array below replaces them entirely. */
  #connection-lines {
    display: none !important;
  }

  .related-word-chip {
    flex: 0 0 auto;
    border: none;
    border-radius: 999px;
    background: #EEE9DB;
    color: #43403B;
    font-family: "Barlow Condensed";
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.1px;
    line-height: 1;
    padding: 6px 18px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
  }

  .related-word-chip:active {
    background: #F9D67A !important;
  }

  /* Related-words chip array shown while a word is merely focused/zoomed
     (word-details overlay visible, full entry panel not open yet). Cleared
     and hidden by relationManager.js as soon as the entry/comment or about
     panel opens (see isAnyPanelOpen()). */
  #mobile-focused-related-words {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--app-bottom-inset) + 28px);
    z-index: 900;
    box-sizing: border-box;
    flex-wrap: wrap;
    align-content: flex-start;
    justify-content: center;
    gap: var(--related-row-gap, 8px);
    max-height: calc(var(--related-row-height, 32px) * 2 + var(--related-row-gap, 8px));
    overflow: hidden;
    padding: 10px 16px;
    pointer-events: none;
    background: none;
  }

  #mobile-focused-related-words.has-items {
    display: flex;
    pointer-events: auto;
  }
}

@media (max-width: 1024px) and (hover: none), (max-width: 1024px) and (pointer: coarse) {
  #side-menu.menu-compact:hover,
  #side-menu.menu-search-lock:hover,
  #side-menu.menu-search-lock.menu-compact:hover {
    --menu-min-width: auto !important;
    --menu-gap: 0 !important;
    --menu-align: stretch !important;
    width: 100% !important;
  }

  #side-menu.menu-compact:hover #buttons,
  #side-menu.menu-search-lock.menu-compact:hover #buttons {
    transform: none !important;
  }
}
