.mobile-polity-stats { display: none; }
.topbar-actions {
  display: contents;
}
#loading .loading-retry {
  margin-top: 18px;
  justify-self: center;
  min-height: 44px;
  min-width: 84px;
  font: 14px 'Segoe UI', 'Microsoft YaHei', sans-serif;
  letter-spacing: 0;
}

@media (max-width: 760px), (max-height: 540px) and (pointer: coarse) {
  :root {
    --phone-header: calc(52px + env(safe-area-inset-top));
    --phone-year: calc(58px + env(safe-area-inset-bottom));
  }

  .topbar, html[lang=en] .topbar {
    height: var(--phone-header);
    gap: 8px;
    padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  }

  .topbar .edition { display: none; }
  .topbar .brand { font-size: 17px; }
  .topbar .brand-mark { display: none; }
  .topbar .search-wrap {
    height: 40px;
    flex: 1;
    min-width: 0;
    padding: 0 8px;
  }
  .topbar .search-wrap input { font-size: 16px; }
  #results {
    position: fixed;
    inset: calc(var(--phone-header) + 4px) 8px auto auto;
    width: min(420px, calc(100vw - 16px));
    max-height: 52dvh;
    overscroll-behavior: contain;
  }
  #results button { min-height: 44px; gap: 8px; }

  .topbar #mobile-menu-toggle {
    min-width: 44px;
    min-height: 44px;
    padding: 0 8px;
    font-size: 13px;
    color: var(--gold);
    border: 1px solid var(--line);
    border-radius: 4px;
  }
  #mobile-menu-toggle[aria-expanded="true"] { background: #c9ba7920; }
  .topbar-actions {
    position: absolute;
    top: calc(100% + 4px);
    right: max(8px, env(safe-area-inset-right));
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    width: min(300px, calc(100vw - 16px));
    max-height: 60dvh;
    overflow: auto;
    padding: 8px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 5px;
    box-shadow: 0 8px 24px #0008;
  }
  .topbar-actions:not(.is-open) { display: none; }
  .topbar .topbar-actions button, html[lang=en] .topbar .topbar-actions button,
  .topbar .topbar-actions #locale-select {
    width: 100%;
    max-width: none;
    min-height: 44px;
    padding: 6px;
    font-size: 13px;
    border: 1px solid var(--line);
    border-radius: 3px;
    white-space: normal;
  }
  .topbar .topbar-actions .subtle { display: block; }

  .empire-bar {
    top: var(--phone-header);
    height: 0;
    padding: 0;
    border: 0;
    box-shadow: none;
  }
  .empire-bar .polity-selector, .empire-bar #empire-stats { display: none; }
  .topbar #mobile-polity-toggle {
    flex: 0 1 auto;
    min-width: 44px;
    min-height: 44px;
    max-width: 23vw;
    padding: 0 6px;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--gold);
    font-size: 13px;
    border: 1px solid var(--line);
    border-radius: 4px;
  }
  .topbar-actions #polity-select {
    max-width: 100%;
    width: 100%;
    min-height: 44px;
    font-size: 14px;
  }
  .mobile-polity-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    margin-bottom: 13px;
  }
  .mobile-polity-stats .stat {
    min-width: 0;
    padding: 8px;
    text-align: left;
    border: 1px solid var(--line);
  }
  .mobile-polity-stats .livelihood-stat { grid-column: 1 / -1; }
  .mobile-polity-stats .livelihood-stat strong { white-space: normal; }
  .annual-controls {
    position: fixed;
    inset: auto 0 0;
    min-height: var(--phone-year);
    gap: 2px;
    justify-content: space-between;
    padding: 4px max(6px, env(safe-area-inset-right)) calc(4px + env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    background: var(--panel);
    box-shadow: 0 -3px 16px #0006;
  }
  .annual-actions { flex: 0 1 auto; }
  .annual-controls #main-reign { display: none; }
  .annual-controls .primary-read-year, #year-event-menu summary {
    display: grid;
    align-content: center;
    min-height: 44px;
    padding: 4px 5px;
    font-size: 11px;
    text-align: center;
  }
  #year-event-menu summary { display: flex; gap: 2px; align-items: center; }
  #year-events {
    top: auto;
    bottom: 100%;
    right: 6px;
    width: min(340px, calc(100vw - 12px));
    max-height: 48dvh;
    overscroll-behavior: contain;
  }
  #year-events button { min-height: 44px; }
  .year-controls { gap: 0; }
  .year-controls button { min-width: 40px; min-height: 44px; }
  .year-controls #play { min-width: 40px; min-height: 40px; margin-right: 1px; }
  .year-entry input { width: 48px; min-height: 44px; font-size: 22px; }

  #mobile-view-toggle {
    position: fixed;
    top: calc(var(--phone-header) + 5px);
    left: 7px;
    z-index: 3;
    padding: 0 10px;
    min-height: 44px;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: var(--panel);
    color: var(--gold);
    white-space: nowrap;
  }
  #mobile-view-toggle[aria-expanded="true"] { border-color: var(--gold); }
  .toolbar, .detail-open .toolbar, .court-open .toolbar {
    top: calc(var(--phone-header) + 53px);
    left: 7px;
    right: auto;
    width: min(370px, calc(100vw - 14px));
    height: auto;
    max-height: min(48dvh, 250px);
    max-width: none;
    flex-wrap: wrap;
    gap: 3px;
    overflow: auto;
    overscroll-behavior: contain;
    padding: 5px;
    border-radius: 4px;
  }
  .toolbar:not(.is-open) { display: none; }
  .toolbar button, html[lang=en] .toolbar button, .toolbar #admin-level {
    min-height: 44px;
    padding: 0 8px;
    font-size: 12px;
    border-color: var(--line);
  }
  .toolbar button span.layer-name { display: inline; font-size: inherit; }
  .toolbar button span[data-i18n] { display: inline; margin: 0; font-size: inherit; }
  .toolbar i { display: none; }
  .map-subviews {
    top: calc(var(--phone-header) + 5px);
    left: 112px;
    max-width: calc(100vw - 119px);
    flex-wrap: nowrap;
    overflow-x: auto;
  }
  .map-subviews button { min-height: 42px; padding: 3px 7px; flex: none; white-space: nowrap; }
  #culture-subviews { left: 7px; top: calc(var(--phone-header) + 54px); max-width: calc(100vw - 14px); }
  #mobile-view-toggle[aria-expanded="true"] ~ .map-subviews { visibility: hidden; }
  .map-caption, .map-subviews:not([hidden]) ~ .map-caption { display: none; }
  .map-controls, .detail-open .map-controls, .detail-open.court-open .map-controls {
    top: auto;
    right: max(7px, env(safe-area-inset-right));
    bottom: calc(var(--phone-year) + 10px);
    gap: 1px;
  }
  .map-controls button, .detail-open .map-controls button { width: 40px; height: 40px; }
  .legend-toggle {
    bottom: calc(var(--phone-year) + 10px);
    min-height: 44px;
  }
  .legend, .legend.power-legend {
    bottom: calc(var(--phone-year) + 59px);
    max-height: min(40dvh, 340px);
    width: min(270px, calc(100vw - 65px));
    overscroll-behavior: contain;
  }
  .legend button { min-height: 34px; }
  #return-content { bottom: calc(var(--phone-year) + 59px); min-height: 44px; }
  #toast { bottom: calc(var(--phone-year) + 15px); }

  #detail {
    inset: auto 7px calc(var(--phone-year) + 4px);
    width: auto;
    max-height: 49dvh;
    min-height: 0;
    border-radius: 12px 12px 4px 4px;
  }
  #detail.mobile-expanded { max-height: calc(100dvh - var(--phone-year) - var(--phone-header) - 6px); }
  #detail .popup-actions #mobile-detail-expand { width: auto; min-width: 44px; padding-inline: 8px; font-size: 13px; }
  .detail-open:has(#detail.mobile-expanded) .map-controls { visibility: hidden; }
  .detail-open .map-controls {
    bottom: calc(var(--phone-year) + min(49dvh, 420px) + 12px);
    flex-direction: row;
  }
  .detail-open .legend-toggle { visibility: hidden; }
  #detail-content, #modal-body { overflow-wrap: anywhere; }

  #modal[open] {
    inset: auto 0 0;
    width: 100%;
    max-width: none;
    max-height: 88dvh;
    margin: 0;
    border-radius: 14px 14px 0 0;
  }
  #modal-body { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
  .culture-grid { grid-template-columns: minmax(0, 1fr); }
  .culture-catalog .content-filters { flex-wrap: wrap; gap: 10px; }
  .culture-catalog .content-filters input[data-culture-query] { flex: 1 0 100%; width: 100%; }
  .culture-catalog .content-filters select { flex: 1; min-height: 44px; }
  .culture-catalog .content-filters label { font-size: 13px; }
  .culture-card span { font-size: 14px; }
  #detail .popup-actions button, #modal .popup-actions button, #city-layout-dialog .sandbox-close {
    width: 44px;
    height: 44px;
  }
  .journey {
    left: 7px;
    right: 60px;
    bottom: calc(var(--phone-year) + 59px);
    flex-wrap: wrap;
    gap: 4px;
    padding-block: 8px;
  }
  .detail-open .journey { display: none; }
}
.mobile-menu-en, html[lang=en] .mobile-menu-zh { display: none; }
html[lang=en] .mobile-menu-en { display: inline; }

@media (max-width: 640px) {
  .visual-stage { grid-template-columns: minmax(0, 1fr); gap: 18px; }
  .visual-image-area { min-height: 0; }
  .visual-notes > p { font-size: 14px; line-height: 1.85; }
  .visual-switch { flex-wrap: wrap; }
}
