.mobile-map-tools { display: none; }

@media (max-width: 780px), (pointer: coarse) and (max-width: 900px), (pointer: coarse) and (max-width: 1100px) and (max-height: 600px) {
  #overview {
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--bar));
    height: calc(100dvh - var(--bar) - env(safe-area-inset-top));
    max-height: none;
    overflow: hidden;
    margin: 0;
    padding: 0;
  }
  .dashboard { display: flex; flex: 1; min-height: 0; }
  .dashboard > .intel, .dashboard > .left-hud, #overview > .overview-head { display: none; }
  #map-panel { flex: 1; border: 0; border-radius: 0; }
  #map-panel .map-stage { flex: 1; min-height: 0; height: auto; }
  .mobile-map-tools {
    display: block;
    padding-inline: max(10px, env(safe-area-inset-left)) max(10px, env(safe-area-inset-right));
    padding-bottom: 4px;
    border-block-end: 1px solid var(--line);
    background: var(--panel);
  }
  .mobile-map-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
  .mobile-map-heading strong, #open-mobile-menu { display: inline-flex; align-items: center; gap: 7px; }
  .mobile-map-heading strong { font: var(--hud-heading-font); letter-spacing: .1px; }
  .mobile-map-heading strong .icon { color: var(--cyan); }
  #open-mobile-menu {
    min-height: 42px; padding: 0 4px 0 8px; border: 0; background: transparent;
    color: var(--text); font: 600 14px Barlow, sans-serif;
  }
  #mobile-map-filters .region-tabs {
    display: flex; flex-flow: row nowrap; gap: 5px;
    overflow-x: auto; overscroll-behavior-x: contain;
    padding: 2px 1px 5px; scrollbar-width: thin;
  }
  #mobile-map-filters button { flex: none; min-height: 35px; padding-inline: 10px; white-space: nowrap; }
  .command-dock, [dir='rtl'] .command-dock {
    inset-inline: 0; bottom: calc(8px + env(safe-area-inset-bottom));
    margin-inline: auto; transform: none; z-index: 6;
    width: min(78vw, 310px); display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
    padding: 4px; gap: 3px;
  }
  .command-dock .dock-item {
    display: flex; flex-direction: column; justify-content: center; gap: 3px;
    min-width: 0; min-height: 44px; padding: 3px; font-size: 11px;
    line-height: 1.05; text-align: center; overflow-wrap: anywhere; white-space: normal;
  }
  .command-dock .dock-item .icon { width: 18px; height: 18px; }
  .map-zoom { bottom: calc(76px + env(safe-area-inset-bottom)); right: max(10px, env(safe-area-inset-right)); }
  .map-zoom .icon-button { width: 36px; height: 36px; }
  .mobile-map-dialog {
    inset: 0; margin: auto; padding: 0; width: min(470px, calc(100% - 20px));
    max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    border: 1px solid #35505d; background: var(--panel); overflow: hidden;
  }
  .mobile-map-dialog[open] { display: flex; flex-direction: column; }
  .mobile-menu-heading {
    display: flex; align-items: center; justify-content: space-between; flex: none;
    padding: 7px 10px 7px 16px; border-bottom: 1px solid var(--line);
  }
  .mobile-menu-heading h2 { margin: 0; font: var(--hud-heading-font); }
  .mobile-map-dialog .icon-button { width: 40px; height: 40px; }
  #mobile-menu-items, #mobile-panel-body { min-height: 0; overflow: auto; overscroll-behavior: contain; }
  #mobile-menu-items { padding: 4px 10px; }
  #mobile-menu-items button {
    display: flex; gap: 12px; align-items: center; width: 100%; min-height: 46px;
    padding: 6px 8px; border: 0; background: transparent; color: var(--text);
    text-align: start; font: 600 16px/1.2 Barlow, sans-serif;
  }
  #mobile-menu-items button + button { border-block-start: 1px solid var(--line); }
  #mobile-menu-items button:is(:active, :focus-visible) { background: var(--soft); }
  #mobile-menu-items .icon { color: var(--cyan); flex: none; }
  #mobile-menu-items .mobile-menu-next { margin-inline-start: auto; color: var(--muted); width: 14px; }
  [dir='rtl'] .mobile-menu-next { transform: rotate(180deg); }
  .mobile-panel-close { position: absolute; z-index: 1; inset-block-start: 4px; inset-inline-end: 6px; }
  #mobile-panel-body { display: grid; gap: 6px; }
  #mobile-panel-body > .panel { position: static; width: 100%; border: 0; border-radius: 0; }
  #mobile-panel-body > .panel:first-child .panel-head { padding-inline-end: 52px; min-height: 48px; height: auto; }
  #mobile-panel-body .directory-head { flex-wrap: wrap; gap: 6px; padding-block: 8px; }
  #mobile-panel-body .search-box { min-width: 0; max-width: 100%; }
  #mobile-panel-body .feed-list { flex: auto; max-height: 62dvh; overflow: auto; }
}

@media (pointer: coarse) and (orientation: landscape) and (min-width: 440px) and (max-width: 1100px) and (max-height: 600px) {
  .mobile-map-tools { display: flex; align-items: center; gap: 12px; padding-block: 2px; }
  .mobile-map-heading { flex: none; gap: 12px; }
  #mobile-map-filters { flex: 1; min-width: 0; }
  #mobile-map-filters .region-tabs { padding-block: 3px; }
  .map-zoom { top: 8px; bottom: auto; }
}

.command-dock .command-tab, #mobile-language, #mobile-menu-items #mobile-menu-share { display: none; }
.directory-scroll, .rank-list, .feed-list, #full-standings, #defenders-list, #drawer-defenders-list,
#reclaim-cities, #command-panel { overscroll-behavior: contain; }

@media (max-width: 768px), (pointer: coarse) and (max-width: 1100px) and (max-height: 600px) {
  :root { --bar: 86px; --command-size: calc(50px + env(safe-area-inset-bottom)); }
  body :is(button, .dock-item) { min-width: 44px !important; min-height: 44px !important; }
  dialog::backdrop { -webkit-backdrop-filter: none; backdrop-filter: none; }
  .rank-list { --rank-row-height: 44px; }
  #enemy-influence-bars { --entry-height: 44px; }
  .masthead {
    display: grid;
    grid-template-columns: auto auto 1fr auto minmax(44px, auto);
    grid-template-areas: "brand live . languages profile" "slogan slogan slogan slogan slogan";
    gap: 1px 4px;
    padding-inline: max(5px, env(safe-area-inset-left)) max(5px, env(safe-area-inset-right));
  }
  .masthead .mast-actions { display: contents; }
  .masthead #share-game, .masthead #utc-clock, .masthead #header-languages { display: none; }
  #mobile-language {
    grid-area: languages; display: block; padding: 3px; border: 1px solid var(--line);
    color: var(--cyan); background: var(--soft); border-radius: 5px; font: 600 13px var(--mono);
  }
  .masthead #dock-auth { grid-area: profile; min-width: 44px; overflow: hidden; }
  .masthead #header-languages { direction: ltr; }
  .masthead.language-open #header-languages {
    display: flex; align-items: center; position: absolute; inset: 100% 0 auto; gap: 0;
    justify-self: stretch; justify-content: safe center; max-width: 100%; overflow-x: auto;
    overscroll-behavior: contain; background: var(--panel); border: 1px solid var(--line);
  }
  .lang-switcher { max-width: 100%; overflow-x: auto; overscroll-behavior: contain; }
  #boost-languages, #rulers-languages { justify-content: safe center; }
  #mobile-menu-items #mobile-menu-share { display: flex; }
  #overview { height: calc(100vh - var(--bar) - var(--command-size)); height: calc(100dvh - var(--bar) - var(--command-size) - env(safe-area-inset-top)); }
  .mobile-map-heading { display: none; }
  .mobile-map-tools, #mobile-map-filters .region-tabs { padding-block: 0; }
  #mobile-map-filters button { padding-block: 2px; }
  .command-dock, [dir='rtl'] .command-dock {
    width: min(100%, 460px); bottom: env(safe-area-inset-bottom); padding: 2px; gap: 0;
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
  .command-dock .dock-item, .command-dock #open-mobile-menu {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; height: 44px; padding: 2px; font: 600 11px/1.05 var(--display);
    letter-spacing: 0; text-transform: uppercase; overflow: hidden;
  }
  .command-dock .dock-item > span, .command-dock #open-mobile-menu span {
    max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .command-dock .desktop-wars { display: none; }
  .command-dock .command-tab { display: flex; }
  .map-zoom {
    top: auto; bottom: 5px; right: max(5px, env(safe-area-inset-right));
    border: 0; box-shadow: inset 0 0 0 1px var(--line);
  }
  .map-zoom .icon-button { width: 44px; height: 44px; }
  .command-panel-open > .masthead, .command-panel-open .mobile-map-tools { display: none; }
  .command-panel-open #overview { height: calc(100dvh - var(--command-size)); padding-top: env(safe-area-inset-top); }
  #command-panel {
    flex: none; max-height: calc(25.75dvh - var(--command-size)); width: 100%; overflow: auto;
    scrollbar-width: thin; background: var(--panel); border-block-end: 1px solid var(--line);
  }
  #command-panel > .panel { position: static; width: 100%; border: 0; border-radius: 0; }
  #command-panel .panel-head { min-height: 36px; height: auto; padding: 3px 8px; gap: 6px; }
  #command-panel .directory-head { flex-wrap: wrap; }
  #command-panel .search-box { width: auto; min-width: 95px; flex: 1; }
  #command-panel .feed-list { max-height: none; overflow: visible; }
  #command-panel .region-tabs { display: flex; flex-wrap: nowrap; overflow-x: auto; overscroll-behavior: contain; }
  #command-panel .region-tabs button { flex: none; }
}

@media (max-width: 768px) and (min-height: 780px) {
  :root { --bar: clamp(118px, 15dvh, 124px); }
  .masthead {
    grid-template-columns: auto auto minmax(0,1fr);
    grid-template-areas: "brand live profile" "language language share" "slogan slogan slogan";
  }
  #mobile-language { display: none; }
  .masthead #header-languages {
    display: flex; gap: 0; grid-area: language; grid-column: 1 / -1;
    justify-self: start; max-width: calc(100% - 48px);
  }
  .masthead #share-game { display: flex; grid-area: share; width: 44px; padding: 0; justify-content: center; }
  .masthead #share-game span {
    position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%);
  }
  #command-panel { max-height: calc(28dvh - var(--command-size)); }
  #command-panel #ranking .panel-head { padding-block: 0; }
}

@media (max-width: 768px) and (max-height: 520px) {
  :root { --bar: 74px; }
  .masthead .slogan-banner { padding-block: 0; line-height: 1.02; }
}

/* On short portrait screens a single scroll step reveals three more countries. */
@media (orientation: portrait) and (max-width: 768px) and (max-height: 809px),
       (orientation: portrait) and (max-width: 360px) and (max-height: 880px) {
  #command-panel { max-height: calc(25.75dvh - var(--command-size)); }
  #command-panel #ranking { display: grid; grid-template-columns: minmax(0,1fr) 44px; align-items: start; }
  #command-panel #ranking .panel-head { grid-area: 1 / 2; padding: 0; border: 0; }
  #command-panel #ranking h2 { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; }
  #command-panel .rank-list {
    grid-area: 1 / 1; display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); grid-auto-rows: 44px;
    block-size: 44px; padding: 0; scroll-snap-type: y proximity;
  }
  #command-panel .rank-row {
    display: grid; grid-template-columns: auto auto 1fr; grid-template-rows: 1fr 1fr;
    gap: 0 3px; padding: 2px 4px; border-inline-end: 1px solid var(--line); scroll-snap-align: start;
  }
  #command-panel .rank-row .rank-value { grid-column: 3; grid-row: 1; justify-self: end; }
  #command-panel .rank-row .rank-name { grid-column: 1 / -1; grid-row: 2; text-align: start; }
}

@media (max-width: 768px) and (max-height: 665px), (max-width: 370px) and (max-height: 670px),
       (pointer: coarse) and (max-width: 1100px) and (max-height: 600px) {
  .mobile-map-tools { display: none; }
}

@media (orientation: landscape) and (max-width: 768px) and (max-height: 600px),
       (orientation: landscape) and (pointer: coarse) and (max-width: 1100px) and (max-height: 600px) {
  body > .masthead {
    position: absolute; inset-block-start: 0; inset-inline-start: 0; width: clamp(282px, 46vw, 400px);
    border: 1px solid var(--line); border-radius: 0 0 6px 0;
    --brand-size: 15px;
  }
  body > .masthead .brand img { width: 24px; height: 24px; }
  body > .masthead .slogan-banner { font-size: var(--brand-size); }
  #overview, .command-panel-open #overview { height: 100dvh; padding-top: 0; }
  .command-dock, [dir='rtl'] .command-dock { width: clamp(270px, 53%, 400px); }
  .command-panel-open #map-panel { flex-direction: row; --side-panel: 26vw; --tab-rows: 2; }
  .command-panel-open #command-panel {
    width: var(--side-panel); max-height: none; align-self: stretch; margin-top: calc(var(--tab-rows) * 44px + 6px);
    border-block-end: 0; border-inline-end: 1px solid var(--line);
  }
  .command-panel-open .map-stage { min-width: 0; }
  .command-panel-open .command-dock, [dir='rtl'] .command-panel-open .command-dock {
    inset: 0 auto auto 0; inset-inline: 0 auto; width: 26vw; grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (orientation: landscape) and (max-width: 540px) and (max-height: 600px) {
  .command-panel-open #map-panel { --tab-rows: 3; }
  .command-panel-open .command-dock, [dir='rtl'] .command-panel-open .command-dock { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #command-panel #ranking .panel-head { padding: 0 2px; gap: 2px; }
  #command-panel #ranking h2 { display: block; min-width: 0; }
  #command-panel #ranking h2 .icon { display: none; }
}

/* Smartphone: Ziele, Ranking and Feed open as a half-screen panel so several results stay visible.
   Portrait: half the screen height. Landscape: half the screen width. Overrides the earlier quarter-height limits. */
@media (orientation: portrait) and (max-width: 768px) {
  #command-panel { max-height: 50dvh; }
  #command-panel .rank-list { block-size: auto; }
}
@media (orientation: landscape) and (max-width: 768px) and (max-height: 600px),
       (orientation: landscape) and (pointer: coarse) and (max-width: 1100px) and (max-height: 600px) {
  .command-panel-open #map-panel { --side-panel: 50vw; }
  .command-panel-open .command-dock, [dir='rtl'] .command-panel-open .command-dock { width: 50vw; }
}

/* Smartphone city window ("Fordere den Stadtherrscher heraus"): the ruler sits in a small box next to the city.
   Only "Herrscher", the player's name with flag and the social links are shown; avatar, team line and defenders are hidden. */
@media (max-width: 780px), (pointer: coarse) and (max-width: 900px), (pointer: coarse) and (max-width: 1100px) and (max-height: 600px) {
  #action-drawer[open] { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 10px; align-content: start; }
  #action-drawer[open] > * { grid-column: 1 / -1; }
  #action-drawer[open] > .dialog-top { grid-row: 1; }
  #action-drawer[open] > #drawer-city-title { grid-column: 1; grid-row: 2; }
  #action-drawer[open] > #drawer-location { grid-column: 1; grid-row: 3; }
  #action-drawer[open] > #drawer-places { grid-column: 1; grid-row: 4; }
  #action-drawer[open] > .ruler-card {
    grid-column: 2; grid-row: 2 / span 3; align-self: start; display: block;
    max-width: min(46vw, 190px); margin-top: 2px; padding: 6px 8px;
  }
  #action-drawer .ruler-card .ruler-avatar, #action-drawer #drawer-ruler-meta,
  #action-drawer #drawer-defenders, #action-drawer .ruler-card .held-by { display: none !important; }
  #action-drawer .ruler-card small { font-size: 11px; }
  #action-drawer .ruler-card strong { font-size: 13px; overflow-wrap: anywhere; }
}

/* Smartphone: no page zoom. Together with maximum-scale=1 and user-scalable=no in index.html this blocks
   double-tap zoom (iOS ignores user-scalable for pinch). The map keeps its own zoom (touch-action: none in world-map.css). */
@media (max-width: 780px), (pointer: coarse) {
  html, body { touch-action: pan-x pan-y; }
}

/* iPhone/smartphone portrait: Ziele, Ranking and Feed open as a bottom sheet that slides up above the dock.
   The header stays visible, the grab handle closes the sheet (tap or swipe down). Rows show only city, country and price. */
.sheet-handle { display: none; }
@media (orientation: portrait) and (max-width: 768px) {
  .command-panel-open > .masthead { display: grid; }
  .command-panel-open #overview {
    height: calc(100dvh - var(--bar) - var(--command-size) - env(safe-area-inset-top)); padding-top: 0;
  }
  #command-panel {
    position: fixed; inset-inline: 0; bottom: var(--command-size); z-index: 7;
    max-height: min(68dvh, calc(100dvh - var(--bar) - var(--command-size) - 12px));
    border: 1px solid var(--line); border-block-end: 0; border-radius: 16px 16px 0 0;
    box-shadow: 0 -12px 40px #000a; padding-bottom: 6px;
    animation: tw-sheet-up .26s cubic-bezier(.2, .8, .2, 1);
  }
  #command-panel .sheet-handle {
    display: block; position: sticky; top: 0; z-index: 3; width: 100%;
    height: 28px; min-height: 28px !important; padding: 0; border: 0; background: var(--panel);
    cursor: grab; touch-action: none;
  }
  #command-panel .sheet-handle::before {
    content: ''; display: block; width: 42px; height: 5px; margin: auto; border-radius: 3px; background: #5b6b7c;
  }
  #command-panel .directory-head { flex-wrap: wrap; row-gap: 6px; padding-block: 4px 6px; }
  #command-panel .directory-head .search-box { flex: 1 1 100%; width: 100%; max-width: none; }
  #command-panel .directory-scroll { max-height: none; overflow: visible; scroll-snap-type: none; }
  #command-panel .city-places--compact { display: none; }
  #command-panel .directory-scroll td { padding-block: 9px; }
  #command-panel .region-tabs { padding-block: 4px; }
}
@keyframes tw-sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@media (prefers-reduced-motion: reduce) { #command-panel { animation: none; } }
