@media (max-width: 768px) and (orientation: portrait) {
  /* ── Prevent double-tap zoom on iOS ── */
  * {
    touch-action: manipulation;
  }

  /* ── Game container: switch from fixed canvas to fluid column ── */
  #game {
    width: 100vw !important;
    height: 100dvh !important;
    transform: none !important;
    display: flex !important;
    flex-direction: column;
    overflow: hidden;
  }

  /* ── Top HUD: tug meter full width, badges below ── */
  #top-hud {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    width: 100% !important;
    padding: 2px 4px !important;
    gap: 0 !important;
    flex-shrink: 0;
    z-index: 42;
    flex-wrap: wrap !important;
  }
  #top-hud > * { pointer-events: auto; }
  #top-meters {
    order: -1;
    width: 100% !important;
    flex: none !important;
    padding: 0 !important;
  }
  .tug-meter { min-width: 0; width: 100%; }
  .tug-value { font-size: 0.45rem; }
  .tug-label { font-size: 0.4rem; }
  /* Badge order: bufo (left), zoognal (center), hacker (right) */
  #mobile-bufo-badge {
    order: 1;
    flex: 1;
    background: rgba(22, 33, 62, 0.9);
    border: 1px solid #2a3a5e;
    border-top: none;
    border-radius: 0 0 8px 8px;
    padding: 0.15rem 0.4rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-family: 'Arial Black', 'Helvetica Neue', Arial, sans-serif;
    font-style: italic;
  }
  #mobile-bufo-badge.hidden {
    display: none !important;
  }
  .bufo-badge-label {
    color: #22d3ee;
    font-size: 0.45rem;
    font-weight: bold;
    letter-spacing: 1px;
  }
  #mobile-bufo-display {
    color: #22d3ee;
    font-weight: bold;
    font-size: 0.8rem;
    white-space: nowrap;
  }
  #objective-hint {
    position: fixed !important;
    left: 50% !important;
    top: auto !important;
    transform: translateX(-50%) !important;
    bottom: calc(22vh + 80px) !important;
    font-size: 0.6rem !important;
    z-index: 44;
  }
  #currency-badge {
    order: 2;
    flex: 1;
    padding: 0.15rem 0.4rem !important;
  }
  .currency-label { font-size: 0.45rem !important; }
  #currency-display { font-size: 0.8rem !important; }
  #hacker-badge {
    order: 3;
    flex: 1;
    padding: 0.15rem 0.4rem !important;
  }
  .hacker-label { font-size: 0.45rem !important; }
  #hacker-display { font-size: 0.8rem !important; }

  /* ── Markets row (created by mobileLayout.js) ── */
  #mobile-markets-row {
    display: flex;
    gap: 4px;
    padding: 2px 4px;
    flex-shrink: 0;
  }
  #mobile-markets-row #bufo-wallet,
  #mobile-markets-row #token-market {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    flex: 1;
    min-width: 0;
    z-index: auto !important;
    font-size: 0.55rem;
  }
  #mobile-markets-row canvas {
    width: 100% !important;
    height: 40px !important;
  }
  #mobile-markets-row .market-header,
  #mobile-markets-row .bufo-header {
    font-size: 0.55rem;
    padding: 2px 4px;
  }
  #mobile-markets-row .market-price-row,
  #mobile-markets-row .bufo-balance-row {
    font-size: 0.55rem;
  }
  #mobile-markets-row button {
    font-size: 0.5rem;
    padding: 3px 6px;
  }
  #mobile-markets-row .bufo-btn-row {
    gap: 2px;
  }
  #mobile-markets-row .bufo-balance-row {
    display: none !important;
  }
  #mobile-markets-row #bufo-wallet,
  #mobile-markets-row #token-market {
    max-height: 140px;
  }

  /* ── Panel bar with live mini-panels ── */
  #mobile-panel-bar {
    display: flex;
    gap: 3px;
    padding: 2px 4px;
    flex-shrink: 0;
    height: 22vh;
    min-height: 120px;
    max-height: 200px;
  }
  .mobile-mini-panel {
    flex: 1;
    min-width: 0;
    background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
    border: 1px solid #334155;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    cursor: pointer;
  }
  .mobile-mini-panel:active {
    border-color: #4ade80;
  }
  .mobile-mini-panel-header {
    color: #4ade80;
    font-family: 'Arial Black', 'Helvetica Neue', Arial, sans-serif;
    font-style: italic;
    font-size: 0.5rem;
    font-weight: bold;
    text-align: center;
    padding: 3px 2px;
    text-transform: uppercase;
    letter-spacing: 1px;
    border-bottom: 1px solid #334155;
    flex-shrink: 0;
    background: rgba(0,0,0,0.3);
  }
  .mobile-mini-panel-body {
    flex: 1;
    overflow: hidden;
    pointer-events: none;
    font-size: 0.45rem;
  }
  /* Scale down content inside mini-panels for preview */
  .mobile-mini-panel-body > * {
    transform: scale(0.75);
    transform-origin: top left;
    width: 133%;
  }
  .mobile-mini-panel-body #inbox {
    max-height: none !important;
    overflow: hidden !important;
  }
  .mobile-mini-panel-body #tech-shop-list {
    max-height: none !important;
    overflow: hidden !important;
  }
  .mobile-mini-panel-body .build-panel {
    display: block !important;
    position: relative !important;
    top: auto !important;
    bottom: auto !important;
    left: auto !important;
    width: 133% !important;
    height: auto !important;
    z-index: auto !important;
  }
  .mobile-mini-panel-body .build-panel-header {
    display: none !important;
  }

  /* ── Server screen: fills remaining space ── */
  #server-screen {
    position: relative !important;
    flex: 1 !important;
    min-height: 0;
    overflow: hidden;
  }

  /* ── Server content: spread nodes edge-to-edge ── */
  #server-content {
    transform: scaleX(1.55);
    transform-origin: 51% 0;
  }
  /* Counter-scale elements inside the stretched #server-content */
  #attachment-banner {
    transform: translateX(-50%) scaleX(calc(1 / 1.55)) !important;
    font-size: 0.55rem !important;
    max-width: 80vw !important;
    white-space: normal !important;
    text-align: center !important;
    padding: 4px 10px !important;
  }
  .server-node {
    transform: translate(-50%, -50%) scaleX(calc(1 / 1.55)) !important;
  }
  .pest-icon {
    transform: translate(-50%, -50%) scaleX(calc(1 / 1.55)) !important;
    width: 32px !important;
    height: 32px !important;
  }
  .pest-icon--mega {
    width: 44px !important;
    height: 44px !important;
  }
  /* Keep connection line strokes consistent */
  #server-content svg line,
  #server-content svg .connection-line {
    vector-effect: non-scaling-stroke;
  }
  /* Smaller server buttons for mobile */
  .server-btn {
    width: 44px !important;
    height: 44px !important;
    font-size: 1.2rem !important;
  }
  .server-maintenance-bar {
    height: 3px !important;
  }

  /* ── Side panels hidden (accessed via overlay) ── */
  #side-panels {
    display: none !important;
  }

  /* ── Build panel hidden in server-content (accessed via overlay) ── */
  #server-content > .build-panel {
    display: none !important;
  }

  /* ── Frog panel: adjust since news banner moves out ── */
  #frog-panel {
    bottom: -5px !important;
    z-index: 10;
  }
  #frog-thumb {
    height: 160px !important;
  }
  #moisturize-btn {
    width: 120px !important;
    height: 36px !important;
    font-size: 0.7rem !important;
    top: auto !important;
    bottom: 10px !important;
    margin: 0 auto !important;
  }

  /* Hide server indicator lights on mobile */
  .server-status-bar .status-lights {
    display: none !important;
  }
  #bottle-nanolube {
    transform: scale(0.7);
  }

  /* ── News banner: moved outside server-screen by JS ── */
  #news-banner {
    position: relative !important;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    height: 36px !important;
    flex-shrink: 0;
    font-size: 0.6rem;
  }
  #news-scoring-marker {
    left: 40px !important;
  }
  #news-translation-marker {
    right: 80px !important;
  }
  #news-track-english {
    clip-path: inset(0 80px 0 0) !important;
  }
  #news-track-garbled {
    clip-path: inset(0 0 0 calc(100% - 80px)) !important;
  }

  /* ── Status bar: moved outside server-screen by JS ── */
  .server-status-bar {
    position: relative !important;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    flex-shrink: 0;
    font-size: 0.45rem;
    padding: 2px 4px !important;
    gap: 0.5rem !important;
  }
  .server-status-bar .status-label {
    font-size: 0.35rem;
  }
  .server-status-bar .status-value {
    font-size: 0.5rem;
  }
  #connect-servers-btn {
    font-size: 0.45rem !important;
    padding: 2px 8px !important;
  }

  /* ── Dev tools: hidden by default, toggled with "d" key ── */
  #dev-cheat-btn {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    font-size: 0.5rem;
    padding: 2px 6px;
  }
  #dev-phase-panel {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    flex-wrap: wrap;
    font-size: 0.45rem;
    gap: 2px;
  }
  #mobile-dev-row {
    display: none;
    gap: 4px;
    padding: 2px 4px;
    flex-shrink: 0;
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,0.5);
  }
  #mobile-dev-row.visible {
    display: flex;
  }

  /* ── Fullscreen panel overlay (created by mobileLayout.js) ── */
  #mobile-panel-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    flex-direction: column;
  }
  #mobile-panel-overlay.hidden {
    display: none !important;
  }
  .mobile-overlay-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
  }
  .mobile-overlay-content {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    max-height: 100dvh;
  }
  .mobile-overlay-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #334155;
    flex-shrink: 0;
  }
  .mobile-overlay-title {
    font-family: 'Arial Black', 'Helvetica Neue', Arial, sans-serif;
    font-style: italic;
    font-size: 1rem;
    font-weight: bold;
    color: #4ade80;
    text-transform: uppercase;
    letter-spacing: 1px;
  }
  .mobile-overlay-close {
    background: none;
    border: 1px solid #475569;
    color: #94a3b8;
    font-size: 1.2rem;
    width: 36px;
    height: 36px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .mobile-overlay-close:active {
    background: #334155;
  }
  .mobile-overlay-body {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    -webkit-overflow-scrolling: touch;
  }

  /* ── Overlay panel content overrides ── */
  /* Inbox in overlay */
  .mobile-overlay-body #inbox {
    max-height: none !important;
    overflow: visible !important;
  }
  .mobile-overlay-body .email-row {
    font-size: 0.7rem;
    padding: 8px;
  }

  /* Tech shop in overlay */
  .mobile-overlay-body .build-panel {
    display: block !important;
    position: relative !important;
    width: 100% !important;
  }
  .mobile-overlay-body .tech-shop-row {
    font-size: 0.7rem;
    padding: 6px 8px;
  }

  /* ThinkTank in mini-panel and overlay */
  .mobile-mini-panel-body #tech-shop-panel,
  .mobile-overlay-body #tech-shop-panel {
    display: flex !important;
    flex-direction: column !important;
    max-height: none !important;
    overflow: visible !important;
    position: relative !important;
    min-height: 200px;
    flex: 1;
  }
  /* Override the global .hidden rule when inside mobile containers */
  .mobile-mini-panel-body #tech-shop-panel.hidden,
  .mobile-overlay-body #tech-shop-panel.hidden {
    display: flex !important;
  }
  .mobile-mini-panel-body #tech-shop-panel > .panel-header,
  .mobile-overlay-body #tech-shop-panel > .panel-header {
    display: none !important;
  }
  .mobile-mini-panel-body #tech-shop-list,
  .mobile-overlay-body #tech-shop-list {
    max-height: none !important;
    overflow: visible !important;
  }
  .mobile-mini-panel-body #tech-shop-list {
    display: flex !important;
    flex-direction: column !important;
  }
  .mobile-mini-panel-body .upgrade-card--next {
    order: -1 !important;
  }
  .mobile-overlay-body #tech-shop-panel {
    min-height: 80dvh;
  }
  #thinktank-lock-overlay {
    position: absolute !important;
    inset: 0 !important;
    width: 100% !important;
    height: 100% !important;
    pointer-events: auto !important;
    z-index: 50;
  }
  /* Hide upgrade cards when the lock overlay is present */
  #tech-shop-panel:has(#thinktank-lock-overlay) #tech-shop-list {
    visibility: hidden !important;
  }
  /* Hide actual lock overlay in mini-panel (too complex for preview) */
  .mobile-mini-panel-body #thinktank-lock-overlay {
    display: none !important;
  }
  /* Simple lock message in mini-panel */
  #mobile-thinktank-lock-msg {
    font-family: 'Arial Black', 'Helvetica Neue', Arial, sans-serif;
    font-style: italic;
    font-size: 9px;
    color: #ff4455;
    text-align: center;
    padding: 16px 4px;
    letter-spacing: 0.5px;
  }
  .mobile-overlay-body .upgrade-card {
    font-size: 0.7rem;
  }

  /* ── Tutorial arrows: bounce downward on mobile ── */
  .tutorial-arrow--down {
    animation: hint-bounce-down 0.8s ease-in-out infinite;
  }
  #email-open-hint {
    display: none !important;
  }
  @keyframes hint-bounce-down {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(10px); }
  }

  /* ── Fullscreen button: hidden on mobile ── */
  #fullscreen-btn {
    display: none !important;
  }

  /* ── Hover tooltips: hidden on mobile ── */
  #hover-tooltip-layer {
    display: none !important;
  }

  /* ── Server node hover z-index still works ── */
  .server-node:hover {
    z-index: 500;
  }

  /* ── Popups & modals: fit mobile viewport ── */
  #email-modal {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    z-index: 200 !important;
    display: flex !important;
    align-items: flex-start !important;
    justify-content: center !important;
    padding-top: 2vh !important;
  }
  .email-modal-backdrop {
    position: fixed !important;
    inset: 0 !important;
  }
  .email-modal-window {
    width: 94vw !important;
    max-width: 94vw !important;
    max-height: 94dvh !important;
    overflow-y: auto !important;
    font-size: 0.75rem;
  }
  .email-modal-scroll {
    max-height: none !important;
    overflow-y: visible !important;
  }
  .email-body {
    font-size: 0.7rem !important;
    padding: 0.4rem 0.5rem !important;
  }
  .email-body-image {
    max-width: 100% !important;
    max-height: 30vh !important;
  }
  .reply-btn {
    font-size: 0.7rem !important;
    padding: 0.6rem 0.5rem !important;
    min-height: 44px;
  }
  .modal-avatar {
    display: none !important;
  }
  .modal-titlebar {
    font-size: 0.65rem !important;
    padding: 3px 6px !important;
  }
  .modal-toolbar {
    font-size: 0.55rem !important;
  }
  .email-field {
    font-size: 0.6rem !important;
  }
  .feature-popup-window {
    width: 85vw !important;
    max-width: 85vw !important;
    max-height: 80dvh !important;
    overflow-y: auto;
    padding: 1rem !important;
  }
  .tech-unlock-window {
    width: 85vw !important;
    max-width: 85vw !important;
    max-height: 80dvh !important;
    overflow-y: auto;
  }
  .pest-battle-window {
    width: 90vw !important;
    max-width: 90vw !important;
    max-height: 85dvh !important;
    overflow-y: auto;
  }
  .headline-newspaper {
    width: 90vw !important;
    max-width: 90vw !important;
    max-height: 85dvh !important;
    overflow-y: auto;
  }

  /* ── Ad popups (SNILFS): force into viewport ── */
  .ad-popup {
    position: fixed !important;
    max-width: 85vw !important;
    max-height: 80dvh !important;
    overflow-y: auto;
  }

  /* ── PondCorp welcome letter ── */
  #mobile-welcome-letter {
    position: fixed;
    inset: 0;
    z-index: 99999;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .mobile-letter-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
  }
  .mobile-letter-content {
    position: relative;
    width: 90vw;
    max-height: 90dvh;
    overflow-y: auto;
    background: #ece9d8;
    border: 3px outset #d4d0c8;
    padding: 16px 20px;
    font-family: Tahoma, Verdana, 'MS Sans Serif', sans-serif;
    font-size: 0.8rem;
    color: #222;
    line-height: 1.5;
  }
  .mobile-letter-logo {
    display: block;
    margin: 0 auto 12px;
    height: 40px;
  }
  .mobile-letter-content p {
    margin: 8px 0;
  }
  .mobile-letter-content ul {
    margin: 8px 0;
    padding-left: 20px;
  }
  .mobile-letter-content li {
    margin: 4px 0;
  }
  .mobile-letter-btn {
    display: block;
    margin: 16px auto 0;
    padding: 10px 24px;
    background: #4ade80;
    color: #000;
    border: 2px solid #22c55e;
    border-radius: 4px;
    font-family: 'Arial Black', 'Helvetica Neue', Arial, sans-serif;
    font-style: italic;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    letter-spacing: 1px;
  }
  .mobile-letter-btn:active {
    background: #22c55e;
  }

  /* ── Background image: cover the server area ── */
  #server-bg {
    object-fit: cover;
    object-position: center;
  }

  /* ── Glitch layers follow server-screen ── */
  .server-bg-glitch-idle,
  .server-bg-glitch {
    position: absolute;
    inset: 0;
  }
}
