/* =========================================================================
   江山时序 · 移动端完美适配层 (Mobile Perfect Overlay)
   -------------------------------------------------------------------------
   - 只改 UI / 布局 / 触控 / 可读性，不改任何历史数据、GeoJSON、事件文本、
     人物行迹、年号与渲染逻辑。
   - 在 css/style.css 之后加载；全部规则限定在 <=900px（少量 <=640px 与
     横屏矮机身），桌面端完全不受影响。
   - 编年栏在移动端由“压缩地图的 grid 行”改为覆盖地图底部的 Sheet；
     溯源卡片 / 行迹 / 挑战仍复用原有 is-hidden / is-sheet-expanded /
     is-mobile-chronicle-open 等状态类，不新增业务状态。
   ========================================================================= */

.mp-grabber {
  display: none;
}

@media (max-width: 900px) {
  :root {
    --mp-touch: 44px;
    --mp-sheet-radius: 20px 20px 14px 14px;
    --mp-sheet-shadow: 0 -8px 30px rgba(43, 38, 34, 0.22), 0 2px 8px rgba(43, 38, 34, 0.10);
    --mp-peek-height: 62px;
  }

  html, body {
    overscroll-behavior-y: none;
    -webkit-tap-highlight-color: transparent;
  }

  button, input, .key-entry, .search-results-list li, .layer-chip {
    touch-action: manipulation;
  }

  /* ---- 骨架：地图永远占满剩余高度，Sheet 覆盖而非挤压地图 ---- */
  #atlas-shell {
    height: 100vh;
    height: 100dvh;
    height: 100svh;
  }

  #nav-bar {
    padding-top: calc(8px + env(safe-area-inset-top, 0px));
    padding-left: calc(10px + env(safe-area-inset-left, 0px));
    padding-right: calc(10px + env(safe-area-inset-right, 0px));
  }

  #workspace-body {
    position: relative;
    grid-template-columns: 1fr !important;
    grid-template-rows: 1fr !important;
    transition: none;
  }

  #viewport-frame {
    grid-column: 1;
    grid-row: 1;
    min-height: 0;
  }

  /* ---- 顶部按钮：最小触控高度 ---- */
  .action-btn {
    min-height: 40px;
    padding: 9px 11px;
    font-size: 13px;
  }

  .layers-popover {
    padding: 12px;
  }

  .layer-chip {
    min-height: 40px;
    padding: 8px 10px;
    font-size: 13px;
    align-items: center;
  }

  /* ---- 搜索：16px 输入避免 iOS 自动放大；结果项一指高 ---- */
  #map-search-box {
    top: calc(8px + env(safe-area-inset-top, 0px));
  }

  .search-input-wrap {
    min-height: 44px;
    padding: 6px 13px;
  }

  #inp-map-search {
    font-size: 16px;
    line-height: 1.4;
    padding: 4px 0;
  }

  .search-results-list {
    max-height: min(58dvh, 460px);
    padding: 6px;
  }

  .search-results-list li {
    min-height: 44px;
    padding: 9px 11px;
    font-size: 14px;
    line-height: 1.45;
  }

  .search-results-list li > div {
    white-space: normal;
  }

  /* ---- 图例：可点按的政权条目，避开底部编年 peek 条 ---- */
  #map-key {
    bottom: calc(var(--mp-peek-height) + 20px);
    left: 8px;
    padding: 8px 11px;
    font-size: 12.5px;
    max-width: calc(100% - 16px);
  }

  .key-entry.is-interactive {
    min-height: 32px;
    padding: 5px 8px;
    margin: -2px -6px;
  }

  .key-phase-badge, .key-heading {
    font-size: 12.5px;
  }

  /* 沉浸模式（人物 / 行迹 / 挑战）下编年 peek 已由原逻辑隐藏，图例回到底部 */
  #workspace-body.has-mobile-overlay #map-key,
  #workspace-body.is-pane-collapsed:not(.is-mobile-chronicle-open) #map-key {
    bottom: 8px;
  }

  #canvas-br-dock {
    bottom: calc(var(--mp-peek-height) + 20px);
    right: 8px;
  }

  #workspace-body.has-mobile-overlay #canvas-br-dock,
  #workspace-body.is-pane-collapsed:not(.is-mobile-chronicle-open) #canvas-br-dock {
    bottom: 8px;
  }

  .canvas-reset-btn {
    min-height: 36px;
    padding: 7px 11px;
    font-size: 12.5px;
  }

  /* ---- 底部 Sheet 抓手条（由 mobile-perfect.js 插入） ---- */
  .mp-grabber {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 18px;
    flex-shrink: 0;
    cursor: grab;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
  }

  .mp-grabber::after {
    content: "";
    width: 38px;
    height: 5px;
    border-radius: 999px;
    background: rgba(43, 38, 34, 0.30);
  }

  .mp-grabber:active {
    cursor: grabbing;
  }

  /* ---- 编年 Sheet：覆盖地图底部，不再压缩地图 ---- */
  #chronicle-pane {
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    top: auto;
    z-index: 16;
    width: auto;
    max-height: var(--mp-peek-height);
    overflow: hidden;
    border: 1px solid var(--hairline);
    border-radius: var(--mp-sheet-radius);
    background: rgba(250, 246, 237, 0.98);
    box-shadow: var(--mp-sheet-shadow);
    padding: 0 14px 8px;
    gap: 0;
  }

  /* 收起态只留朝代名 + 年份 + 史志按钮，座次行展开后才看 */
  #workspace-body:not(.is-mobile-chronicle-open) #chronicle-pane {
    justify-content: flex-start;
  }

  #workspace-body:not(.is-mobile-chronicle-open) #chronicle-pane .seat-row {
    display: none;
  }

  #workspace-body:not(.is-mobile-chronicle-open) #chronicle-pane #dynasty-title {
    font-size: 20px;
  }

  #workspace-body.is-mobile-chronicle-open #chronicle-pane {
    max-height: min(74dvh, 620px);
    height: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
    padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  }

  #workspace-body.is-mobile-chronicle-open #chronicle-pane .dynasty-banner {
    position: sticky;
    top: 0;
    z-index: 4;
    background: rgba(250, 246, 237, 0.98);
    padding-top: 2px;
  }

  .mobile-chronicle-btn {
    min-height: 36px;
    padding: 8px 13px;
    font-size: 13px;
  }

  #workspace-body.is-mobile-chronicle-open #dynasty-summary,
  #workspace-body.is-mobile-chronicle-open .dynasty-desc {
    font-size: 14.5px;
    line-height: 1.7;
  }

  #workspace-body.is-mobile-chronicle-open #milestone-list li {
    min-height: 44px;
    padding: 9px 10px;
    font-size: 13.5px;
    line-height: 1.55;
  }

  .polity-backbar {
    min-height: 36px;
    padding: 8px 14px;
    font-size: 13px;
  }

  /* ---- 点地溯源 / 点人溯事 Sheet ---- */
  #genealogy-card {
    left: 8px;
    right: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    width: auto;
    max-height: min(68dvh, 540px);
    border-radius: var(--mp-sheet-radius);
    box-shadow: var(--mp-sheet-shadow);
    padding: 0 14px 12px;
  }

  #genealogy-card.is-sheet-expanded {
    top: calc(8px + env(safe-area-inset-top, 0px));
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    max-height: none;
  }

  .genealogy-header {
    align-items: center;
  }

  .genealogy-title {
    font-size: 18px;
    line-height: 1.3;
  }

  .genealogy-tag {
    font-size: 11px;
  }

  .genealogy-sub {
    font-size: 12.5px;
    line-height: 1.5;
  }

  .genealogy-body {
    font-size: 13.5px;
    line-height: 1.6;
  }

  .gen-expand-btn,
  .icon-close {
    min-width: 40px;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .gen-expand-btn {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  .gen-filter-pill {
    min-height: 32px;
    padding: 6px 12px;
    font-size: 12.5px;
  }

  /* ---- 行迹 / 挑战 / 事件气球 ---- */
  #journey-card,
  #journey-card.is-custom-route {
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    padding: 10px 12px;
    border-radius: 14px;
  }

  .jc-nav-btn {
    min-height: 40px;
    padding: 8px 12px;
    font-size: 13px;
  }

  .jc-close-btn {
    min-height: 36px;
    min-width: 36px;
    padding: 7px 10px;
  }

  #challenge-card {
    top: calc(10px + env(safe-area-inset-top, 0px));
    left: 8px;
    right: 8px;
    width: auto;
    max-height: min(72dvh, 560px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--mp-sheet-radius);
  }

  .challenge-prompt {
    font-size: 15px;
    line-height: 1.6;
  }

  #act-next-round {
    min-height: 44px;
  }

  #milestone-balloon {
    font-size: 13px;
    line-height: 1.55;
    max-width: min(320px, calc(100vw - 24px));
  }

  #milestone-balloon .ep-close {
    width: 36px;
    height: 36px;
  }

  #milestone-balloon .ep-place {
    min-height: 34px;
    padding: 7px 11px;
    font-size: 13px;
  }

  /* ---- 底部时间轴：更粗的轨道、更大的步进与播放键 ---- */
  #scrubber-dock {
    padding-left: calc(10px + env(safe-area-inset-left, 0px));
    padding-right: calc(10px + env(safe-area-inset-right, 0px));
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    gap: 10px;
  }

  #scrubber-track {
    height: 40px;
    border-radius: 10px;
  }

  #scrubber-handle {
    width: 6px;
    margin-left: -3px;
    top: -7px;
    bottom: -7px;
    border-radius: 3px;
  }

  #scrubber-Bubble {
    font-size: 13px;
    padding: 5px 11px;
  }

  .round-btn {
    width: 44px;
    height: 44px;
  }

  .play-speed-btn {
    min-width: 44px;
    height: 40px;
    padding: 0 10px;
    font-size: 13px;
  }

  .step-nav-btn {
    width: 40px;
    height: 40px;
    font-size: 20px;
    border-radius: 10px;
  }

  .year-stepper input {
    width: 74px;
    height: 40px;
    font-size: 16px;
    line-height: 1.2;
  }

  .unit-label {
    font-size: 14px;
  }

  /* 键盘弹出时不让输入框被时间轴遮住：输入框本身已是 16px，iOS 不再自动放大 */
  #inp-year, #inp-map-search {
    font-size: 16px;
  }
}

@media (max-width: 640px) {
  #nav-bar {
    padding-top: calc(6px + env(safe-area-inset-top, 0px));
  }

  .brand-group .seal-mark {
    font-size: 20px;
  }

  .action-btn {
    min-height: 40px;
    padding: 9px 9px;
  }

  #map-search-box:not(:focus-within):not(.is-expanded) {
    width: 44px;
    height: 44px;
  }

  #map-search-box:not(:focus-within):not(.is-expanded) .search-input-wrap {
    width: 44px;
    height: 44px;
  }

  #scrubber-dock {
    grid-template-rows: 44px auto;
  }

  #scrubber-track {
    height: 38px;
  }

  #scrubber-Bubble {
    font-size: 12.5px;
  }

  .round-btn {
    width: 44px;
    height: 44px;
  }

  #genealogy-card {
    max-height: min(70dvh, 560px);
  }

  #workspace-body.is-mobile-chronicle-open #chronicle-pane {
    max-height: min(76dvh, 640px);
  }
}

@media (max-width: 380px) {
  .brand-group .seal-mark {
    font-size: 18px;
  }

  #dynasty-title {
    font-size: 18px;
  }

  #workspace-body:not(.is-mobile-chronicle-open) #chronicle-pane #dynasty-title {
    font-size: 18px;
  }

  .search-results-list li {
    font-size: 13.5px;
  }
}

/* 横屏矮机身：顶栏与时间轴收紧，地图优先；Sheet 仍可全屏阅读 */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 480px) {
  #nav-bar {
    padding-top: 4px;
    padding-bottom: 4px;
  }

  .action-btn {
    min-height: 36px;
    padding: 7px 9px;
  }

  #scrubber-dock {
    padding-top: 6px;
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
  }

  #scrubber-track {
    height: 34px;
  }

  #chronicle-pane {
    max-height: 52px;
  }

  #workspace-body.is-mobile-chronicle-open #chronicle-pane {
    max-height: min(86dvh, 640px);
  }

  #genealogy-card {
    max-height: min(80dvh, 560px);
  }
}

/* 桌面端不受影响；同时尊重减动效偏好 */
@media (prefers-reduced-motion: reduce) {
  #chronicle-pane, #genealogy-card, #scrubber-track, #map-search-box {
    transition: none !important;
  }
}
